blob: 0810267fef2754246599104adf9e12346479747f [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
David Greenef125a292011-02-08 19:04:41 +000074static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
75
76
David Greenea5f26012011-02-07 19:36:54 +000077/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
78/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000079/// simple subregister reference. Idx is an index in the 128 bits we
80/// want. It need not be aligned to a 128-bit bounday. That makes
81/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000082static SDValue Extract128BitVector(SDValue Vec,
83 SDValue Idx,
84 SelectionDAG &DAG,
85 DebugLoc dl) {
86 EVT VT = Vec.getValueType();
87 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
88
89 EVT ElVT = VT.getVectorElementType();
90
91 int Factor = VT.getSizeInBits() / 128;
92
93 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
94 ElVT,
95 VT.getVectorNumElements() / Factor);
96
97 // Extract from UNDEF is UNDEF.
98 if (Vec.getOpcode() == ISD::UNDEF)
99 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
100
101 if (isa<ConstantSDNode>(Idx)) {
102 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
103
104 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
105 // we can match to VEXTRACTF128.
106 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
107
108 // This is the index of the first element of the 128-bit chunk
109 // we want.
110 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
111 * ElemsPerChunk);
112
113 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
114
115 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
116 VecIdx);
117
118 return Result;
119 }
120
121 return SDValue();
122}
123
124/// Generate a DAG to put 128-bits into a vector > 128 bits. This
125/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000126/// simple superregister reference. Idx is an index in the 128 bits
127/// we want. It need not be aligned to a 128-bit bounday. That makes
128/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000129static SDValue Insert128BitVector(SDValue Result,
130 SDValue Vec,
131 SDValue Idx,
132 SelectionDAG &DAG,
133 DebugLoc dl) {
134 if (isa<ConstantSDNode>(Idx)) {
135 EVT VT = Vec.getValueType();
136 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
137
138 EVT ElVT = VT.getVectorElementType();
139
140 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
141
142 EVT ResultVT = Result.getValueType();
143
144 // Insert the relevant 128 bits.
145 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
146
147 // This is the index of the first element of the 128-bit chunk
148 // we want.
149 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
150 * ElemsPerChunk);
151
152 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
153
154 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
155 VecIdx);
156 return Result;
157 }
158
159 return SDValue();
160}
161
David Greenef125a292011-02-08 19:04:41 +0000162/// Given two vectors, concat them.
163static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
164 DebugLoc dl = Lower.getDebugLoc();
165
166 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
167
168 EVT VT = EVT::getVectorVT(*DAG.getContext(),
169 Lower.getValueType().getVectorElementType(),
170 Lower.getValueType().getVectorNumElements() * 2);
171
172 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
173 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
174
175 // Insert the upper subvector.
176 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
177 DAG.getConstant(
178 // This is half the length of the result
179 // vector. Start inserting the upper 128
180 // bits here.
181 Lower.getValueType().getVectorNumElements(),
182 MVT::i32),
183 DAG, dl);
184
185 // Insert the lower subvector.
186 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
187 return Vec;
188}
189
Chris Lattnerf0144122009-07-28 03:13:23 +0000190static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000191 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
192 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000193
Evan Cheng2bffee22011-02-01 01:14:13 +0000194 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000195 if (is64Bit)
196 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000197 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000198 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000199
Evan Cheng2bffee22011-02-01 01:14:13 +0000200 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000201 if (is64Bit)
202 return new X8664_ELFTargetObjectFile(TM);
203 return new X8632_ELFTargetObjectFile(TM);
204 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000205 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000206 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000207 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000208}
209
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000210X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000211 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000212 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000213 X86ScalarSSEf64 = Subtarget->hasXMMInt();
214 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000216
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000217 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000218 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000219
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000221 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222
223 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000224 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000225
Eric Christopherde5e1012011-03-11 01:05:58 +0000226 // For 64-bit since we have so many registers use the ILP scheduler, for
227 // 32-bit code use the register pressure specific scheduling.
228 if (Subtarget->is64Bit())
229 setSchedulingPreference(Sched::ILP);
230 else
231 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000233
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000234 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000235 // Setup Windows compiler runtime calls.
236 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000237 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
238 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000239 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000240 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000241 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000242 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
243 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000244 }
245
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000246 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000247 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000248 setUseUnderscoreSetJmp(false);
249 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000250 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000251 // MS runtime is weird: it exports _setjmp, but longjmp!
252 setUseUnderscoreSetJmp(true);
253 setUseUnderscoreLongJmp(false);
254 } else {
255 setUseUnderscoreSetJmp(true);
256 setUseUnderscoreLongJmp(true);
257 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000258
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000259 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000261 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000267
Scott Michelfdc40a02009-02-17 22:15:04 +0000268 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000270 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000272 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
274 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000275
276 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
278 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
279 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
280 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
281 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
282 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000283
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
285 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
288 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000289
Evan Cheng25ab6902006-09-08 06:48:29 +0000290 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
292 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000293 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000294 // We have an algorithm for SSE2->double, and we turn this into a
295 // 64-bit FILD followed by conditional FADD for other targets.
296 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000297 // We have an algorithm for SSE2, and we turn this into a 64-bit
298 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000299 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000300 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000301
302 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
303 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
305 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306
Devang Patel6a784892009-06-05 18:48:29 +0000307 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000308 // SSE has no i16 to fp conversion, only i32
309 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000311 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000313 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
315 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000316 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000317 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
319 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000320 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000321
Dale Johannesen73328d12007-09-19 23:55:34 +0000322 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
323 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
325 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000326
Evan Cheng02568ff2006-01-30 22:13:22 +0000327 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
328 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
330 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000331
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000332 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000334 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000336 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
338 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000339 }
340
341 // Handle FP_TO_UINT by promoting the destination to a larger signed
342 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
345 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000346
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
349 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000350 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000351 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000352 // Expand FP_TO_UINT into a select.
353 // FIXME: We would like to use a Custom expander here eventually to do
354 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000356 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000357 // With SSE3 we can use fisttpll to convert to a signed i64; without
358 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000360 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000361
Chris Lattner399610a2006-12-05 18:22:22 +0000362 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000363 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
365 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000366 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000367 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000368 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000369 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000370 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000371 }
Chris Lattner21f66852005-12-23 05:15:23 +0000372
Dan Gohmanb00ee212008-02-18 19:34:53 +0000373 // Scalar integer divide and remainder are lowered to use operations that
374 // produce two results, to match the available instructions. This exposes
375 // the two-result form to trivial CSE, which is able to combine x/y and x%y
376 // into a single instruction.
377 //
378 // Scalar integer multiply-high is also lowered to use two-result
379 // operations, to match the available instructions. However, plain multiply
380 // (low) operations are left as Legal, as there are single-result
381 // instructions for this in x86. Using the two-result multiply instructions
382 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000383 for (unsigned i = 0, e = 4; i != e; ++i) {
384 MVT VT = IntVTs[i];
385 setOperationAction(ISD::MULHS, VT, Expand);
386 setOperationAction(ISD::MULHU, VT, Expand);
387 setOperationAction(ISD::SDIV, VT, Expand);
388 setOperationAction(ISD::UDIV, VT, Expand);
389 setOperationAction(ISD::SREM, VT, Expand);
390 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000391
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000392 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000393 setOperationAction(ISD::ADDC, VT, Custom);
394 setOperationAction(ISD::ADDE, VT, Custom);
395 setOperationAction(ISD::SUBC, VT, Custom);
396 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000397 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
400 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
401 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
402 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000403 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
407 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
408 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
409 setOperationAction(ISD::FREM , MVT::f32 , Expand);
410 setOperationAction(ISD::FREM , MVT::f64 , Expand);
411 setOperationAction(ISD::FREM , MVT::f80 , Expand);
412 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000413
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000416 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
419 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 }
424
Benjamin Kramer1292c222010-12-04 20:32:23 +0000425 if (Subtarget->hasPOPCNT()) {
426 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
427 } else {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
429 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
431 if (Subtarget->is64Bit())
432 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
433 }
434
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
436 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000437
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000438 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000439 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000440 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000442 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
447 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000448 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000453 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000455 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000456 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000458
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000459 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
461 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
462 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
463 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000464 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
466 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000467 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
472 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000473 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000474 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000475 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
477 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
478 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000479 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
481 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
482 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000483 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000485 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000487
Eric Christopher9a9d2752010-07-22 02:48:34 +0000488 // We may not have a libcall for MEMBARRIER so we should lower this.
489 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000490
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000491 // On X86 and X86-64, atomic operations are lowered to locked instructions.
492 // Locked instructions, in turn, have implicit fence semantics (all memory
493 // operations are flushed before issuing the locked instruction, and they
494 // are not buffered), so we can fold away the common pattern of
495 // fence-atomic-fence.
496 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000497
Mon P Wang63307c32008-05-05 19:05:59 +0000498 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000499 for (unsigned i = 0, e = 4; i != e; ++i) {
500 MVT VT = IntVTs[i];
501 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
503 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000504
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000505 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000513 }
514
Evan Cheng3c992d22006-03-07 02:02:57 +0000515 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000516 if (!Subtarget->isTargetDarwin() &&
517 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000518 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000520 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
523 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
524 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
525 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000526 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000527 setExceptionPointerRegister(X86::RAX);
528 setExceptionSelectorRegister(X86::RDX);
529 } else {
530 setExceptionPointerRegister(X86::EAX);
531 setExceptionSelectorRegister(X86::EDX);
532 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
534 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000535
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000539
Nate Begemanacc398c2006-01-25 18:21:52 +0000540 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::VASTART , MVT::Other, Custom);
542 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000543 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::VAARG , MVT::Other, Custom);
545 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000546 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::VAARG , MVT::Other, Expand);
548 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000549 }
Evan Chengae642192007-03-02 23:16:35 +0000550
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
552 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC,
554 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
555 (Subtarget->isTargetCOFF()
556 && !Subtarget->isTargetEnvMacho()
557 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000558
Evan Chengc7ce29b2009-02-13 22:36:38 +0000559 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000560 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000561 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
563 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000564
Evan Cheng223547a2006-01-31 22:28:30 +0000565 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FABS , MVT::f64, Custom);
567 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000568
569 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FNEG , MVT::f64, Custom);
571 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000572
Evan Cheng68c47cb2007-01-05 07:55:56 +0000573 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
575 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000576
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000577 // Lower this to FGETSIGNx86 plus an AND.
578 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
579 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
580
Evan Chengd25e9e82006-02-02 00:28:23 +0000581 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::FSIN , MVT::f64, Expand);
583 setOperationAction(ISD::FCOS , MVT::f64, Expand);
584 setOperationAction(ISD::FSIN , MVT::f32, Expand);
585 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000586
Chris Lattnera54aa942006-01-29 06:26:08 +0000587 // Expand FP immediates into loads from the stack, except for the special
588 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000589 addLegalFPImmediate(APFloat(+0.0)); // xorpd
590 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000591 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592 // Use SSE for f32, x87 for f64.
593 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
595 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
607 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
609 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612
Nate Begemane1795842008-02-14 08:57:00 +0000613 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 addLegalFPImmediate(APFloat(+0.0f)); // xorps
615 addLegalFPImmediate(APFloat(+0.0)); // FLD0
616 addLegalFPImmediate(APFloat(+1.0)); // FLD1
617 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
618 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
619
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
622 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000624 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
628 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
631 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000634
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
637 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000638 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000639 addLegalFPImmediate(APFloat(+0.0)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000643 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
644 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
645 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
646 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000647 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000648
Dale Johannesen59a58732007-08-05 18:49:15 +0000649 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000650 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
652 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
653 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000655 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 addLegalFPImmediate(TmpFlt); // FLD0
657 TmpFlt.changeSign();
658 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000659
660 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 APFloat TmpFlt2(+1.0);
662 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
663 &ignored);
664 addLegalFPImmediate(TmpFlt2); // FLD1
665 TmpFlt2.changeSign();
666 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
667 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000668
Evan Chengc7ce29b2009-02-13 22:36:38 +0000669 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
671 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000673 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000674
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
677 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
678 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::FLOG, MVT::f80, Expand);
681 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
682 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
683 setOperationAction(ISD::FEXP, MVT::f80, Expand);
684 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000685
Mon P Wangf007a8b2008-11-06 05:31:54 +0000686 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000687 // (for widening) or expand (for scalarization). Then we will selectively
688 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
690 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
691 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000707 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
708 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000740 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000741 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
745 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
746 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
747 setTruncStoreAction((MVT::SimpleValueType)VT,
748 (MVT::SimpleValueType)InnerVT, Expand);
749 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
750 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
751 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000752 }
753
Evan Chengc7ce29b2009-02-13 22:36:38 +0000754 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
755 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000756 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000757 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000758 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000759 }
760
Dale Johannesen0488fb62010-09-30 23:57:10 +0000761 // MMX-sized vectors (other than x86mmx) are expected to be expanded
762 // into smaller operations.
763 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
764 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
765 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
766 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
767 setOperationAction(ISD::AND, MVT::v8i8, Expand);
768 setOperationAction(ISD::AND, MVT::v4i16, Expand);
769 setOperationAction(ISD::AND, MVT::v2i32, Expand);
770 setOperationAction(ISD::AND, MVT::v1i64, Expand);
771 setOperationAction(ISD::OR, MVT::v8i8, Expand);
772 setOperationAction(ISD::OR, MVT::v4i16, Expand);
773 setOperationAction(ISD::OR, MVT::v2i32, Expand);
774 setOperationAction(ISD::OR, MVT::v1i64, Expand);
775 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
780 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
781 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
782 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
784 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
785 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
786 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
787 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
789 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
790 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
791 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000792
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000793 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
797 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
798 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
799 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
800 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
801 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
802 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
803 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
804 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
805 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
806 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
807 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000810 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000812
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000813 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
814 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
816 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
817 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
818 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
821 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
822 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
823 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
825 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
826 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
827 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
828 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
829 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
830 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
831 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
832 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
833 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
834 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
835 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
838 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
839 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
840 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000847
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
849 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
850 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
851 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
852 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
853
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
856 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000857 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 // Do not attempt to custom lower non-128-bit vectors
861 if (!VT.is128BitVector())
862 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::BUILD_VECTOR,
864 VT.getSimpleVT().SimpleTy, Custom);
865 setOperationAction(ISD::VECTOR_SHUFFLE,
866 VT.getSimpleVT().SimpleTy, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
868 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
872 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
873 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000877
Nate Begemancdd1eec2008-02-12 22:51:28 +0000878 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
880 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000881 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000882
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000883 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
885 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000886 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000887
888 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000889 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000890 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000891
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000898 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000900 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000902 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000905
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
908 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
909 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
910 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
913 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000914 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000915
Nate Begeman14d12ca2008-02-11 04:19:36 +0000916 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000917 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
920 setOperationAction(ISD::FRINT, MVT::f32, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
922 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
923 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
924 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
925 setOperationAction(ISD::FRINT, MVT::f64, Legal);
926 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000930
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000931 // Can turn SHL into an integer multiply.
932 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000933 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000934
Nate Begeman14d12ca2008-02-11 04:19:36 +0000935 // i8 and i16 vectors are custom , because the source register and source
936 // source memory operand types are not the same width. f32 vectors are
937 // custom since the immediate controlling the insert encodes additional
938 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000943
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948
949 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
951 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000952 }
953 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000954
Nadav Rotem43012222011-05-11 08:12:09 +0000955 if (Subtarget->hasSSE2()) {
956 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
957 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
958 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
959
960 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
961 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
962 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
963
964 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
965 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
966 }
967
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000968 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000970
David Greene9b9838d2009-06-29 16:47:10 +0000971 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
973 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
974 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
975 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000976 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000977
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
979 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
980 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
981 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
984 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
985 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
986 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
987 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
988 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000989
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
991 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
992 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
993 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
994 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
995 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000996
David Greene54d8eba2011-01-27 22:38:56 +0000997 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
998 // insert_vector_elt extract_subvector and extract_vector_elt for
999 // 256-bit types.
1000 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1001 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1002 ++i) {
1003 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1004 // Do not attempt to custom lower non-256-bit vectors
1005 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
1006 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +00001007 continue;
David Greene9b9838d2009-06-29 16:47:10 +00001008 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1009 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001010 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001012 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001013 }
David Greene54d8eba2011-01-27 22:38:56 +00001014 // Custom-lower insert_subvector and extract_subvector based on
1015 // the result type.
1016 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1017 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1018 ++i) {
1019 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1020 // Do not attempt to custom lower non-256-bit vectors
1021 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001022 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001023
1024 if (MVT(VT).getSizeInBits() == 128) {
1025 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001026 }
David Greene54d8eba2011-01-27 22:38:56 +00001027 else if (MVT(VT).getSizeInBits() == 256) {
1028 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1029 }
David Greene9b9838d2009-06-29 16:47:10 +00001030 }
1031
David Greene54d8eba2011-01-27 22:38:56 +00001032 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1033 // Don't promote loads because we need them for VPERM vector index versions.
1034
1035 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1036 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1037 VT++) {
1038 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1039 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1040 continue;
1041 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1042 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1043 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1044 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1045 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1046 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1047 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1048 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1049 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1050 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1051 }
David Greene9b9838d2009-06-29 16:47:10 +00001052 }
1053
Evan Cheng6be2c582006-04-05 23:38:46 +00001054 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001055 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001056
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001057
Eli Friedman962f5492010-06-02 19:35:46 +00001058 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1059 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001060 //
Eli Friedman962f5492010-06-02 19:35:46 +00001061 // FIXME: We really should do custom legalization for addition and
1062 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1063 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001064 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1065 // Add/Sub/Mul with overflow operations are custom lowered.
1066 MVT VT = IntVTs[i];
1067 setOperationAction(ISD::SADDO, VT, Custom);
1068 setOperationAction(ISD::UADDO, VT, Custom);
1069 setOperationAction(ISD::SSUBO, VT, Custom);
1070 setOperationAction(ISD::USUBO, VT, Custom);
1071 setOperationAction(ISD::SMULO, VT, Custom);
1072 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001073 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001074
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001075 // There are no 8-bit 3-address imul/mul instructions
1076 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1077 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001078
Evan Chengd54f2d52009-03-31 19:38:51 +00001079 if (!Subtarget->is64Bit()) {
1080 // These libcalls are not available in 32-bit.
1081 setLibcallName(RTLIB::SHL_I128, 0);
1082 setLibcallName(RTLIB::SRL_I128, 0);
1083 setLibcallName(RTLIB::SRA_I128, 0);
1084 }
1085
Evan Cheng206ee9d2006-07-07 08:33:52 +00001086 // We have target-specific dag combine patterns for the following nodes:
1087 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001088 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001089 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001090 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001091 setTargetDAGCombine(ISD::SHL);
1092 setTargetDAGCombine(ISD::SRA);
1093 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001094 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001095 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001096 setTargetDAGCombine(ISD::ADD);
1097 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001098 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001099 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001100 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001101 if (Subtarget->is64Bit())
1102 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001103
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001104 computeRegisterProperties();
1105
Evan Cheng05219282011-01-06 06:52:41 +00001106 // On Darwin, -Os means optimize for size without hurting performance,
1107 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001108 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001109 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001110 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001111 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1112 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1113 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001114 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001115 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001116
1117 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118}
1119
Scott Michel5b8f82e2008-03-10 15:42:14 +00001120
Owen Anderson825b72b2009-08-11 20:47:22 +00001121MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1122 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001123}
1124
1125
Evan Cheng29286502008-01-23 23:17:41 +00001126/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1127/// the desired ByVal argument alignment.
1128static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1129 if (MaxAlign == 16)
1130 return;
1131 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1132 if (VTy->getBitWidth() == 128)
1133 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001134 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1135 unsigned EltAlign = 0;
1136 getMaxByValAlign(ATy->getElementType(), EltAlign);
1137 if (EltAlign > MaxAlign)
1138 MaxAlign = EltAlign;
1139 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1140 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1141 unsigned EltAlign = 0;
1142 getMaxByValAlign(STy->getElementType(i), EltAlign);
1143 if (EltAlign > MaxAlign)
1144 MaxAlign = EltAlign;
1145 if (MaxAlign == 16)
1146 break;
1147 }
1148 }
1149 return;
1150}
1151
1152/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1153/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001154/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1155/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001156unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001157 if (Subtarget->is64Bit()) {
1158 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001159 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001160 if (TyAlign > 8)
1161 return TyAlign;
1162 return 8;
1163 }
1164
Evan Cheng29286502008-01-23 23:17:41 +00001165 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001166 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001167 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001168 return Align;
1169}
Chris Lattner2b02a442007-02-25 08:29:00 +00001170
Evan Chengf0df0312008-05-15 08:39:06 +00001171/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001172/// and store operations as a result of memset, memcpy, and memmove
1173/// lowering. If DstAlign is zero that means it's safe to destination
1174/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1175/// means there isn't a need to check it against alignment requirement,
1176/// probably because the source does not need to be loaded. If
1177/// 'NonScalarIntSafe' is true, that means it's safe to return a
1178/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1179/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1180/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001181/// It returns EVT::Other if the type should be determined using generic
1182/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001183EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001184X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1185 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001186 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001187 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001188 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001189 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1190 // linux. This is because the stack realignment code can't handle certain
1191 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001192 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001193 if (NonScalarIntSafe &&
1194 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001195 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001196 (Subtarget->isUnalignedMemAccessFast() ||
1197 ((DstAlign == 0 || DstAlign >= 16) &&
1198 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001199 Subtarget->getStackAlignment() >= 16) {
1200 if (Subtarget->hasSSE2())
1201 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001202 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001203 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001204 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001205 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001206 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001207 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001208 // Do not use f64 to lower memcpy if source is string constant. It's
1209 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001210 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001211 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001212 }
Evan Chengf0df0312008-05-15 08:39:06 +00001213 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 return MVT::i64;
1215 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001216}
1217
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001218/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1219/// current function. The returned value is a member of the
1220/// MachineJumpTableInfo::JTEntryKind enum.
1221unsigned X86TargetLowering::getJumpTableEncoding() const {
1222 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1223 // symbol.
1224 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1225 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001226 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001227
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001228 // Otherwise, use the normal jump table encoding heuristics.
1229 return TargetLowering::getJumpTableEncoding();
1230}
1231
Chris Lattnerc64daab2010-01-26 05:02:42 +00001232const MCExpr *
1233X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1234 const MachineBasicBlock *MBB,
1235 unsigned uid,MCContext &Ctx) const{
1236 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1237 Subtarget->isPICStyleGOT());
1238 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1239 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001240 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1241 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001242}
1243
Evan Chengcc415862007-11-09 01:32:10 +00001244/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1245/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001246SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001247 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001248 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001249 // This doesn't have DebugLoc associated with it, but is not really the
1250 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001251 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001252 return Table;
1253}
1254
Chris Lattner589c6f62010-01-26 06:28:43 +00001255/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1256/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1257/// MCExpr.
1258const MCExpr *X86TargetLowering::
1259getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1260 MCContext &Ctx) const {
1261 // X86-64 uses RIP relative addressing based on the jump table label.
1262 if (Subtarget->isPICStyleRIPRel())
1263 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1264
1265 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001266 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001267}
1268
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001269// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001270std::pair<const TargetRegisterClass*, uint8_t>
1271X86TargetLowering::findRepresentativeClass(EVT VT) const{
1272 const TargetRegisterClass *RRC = 0;
1273 uint8_t Cost = 1;
1274 switch (VT.getSimpleVT().SimpleTy) {
1275 default:
1276 return TargetLowering::findRepresentativeClass(VT);
1277 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1278 RRC = (Subtarget->is64Bit()
1279 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1280 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001281 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001282 RRC = X86::VR64RegisterClass;
1283 break;
1284 case MVT::f32: case MVT::f64:
1285 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1286 case MVT::v4f32: case MVT::v2f64:
1287 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1288 case MVT::v4f64:
1289 RRC = X86::VR128RegisterClass;
1290 break;
1291 }
1292 return std::make_pair(RRC, Cost);
1293}
1294
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001295bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1296 unsigned &Offset) const {
1297 if (!Subtarget->isTargetLinux())
1298 return false;
1299
1300 if (Subtarget->is64Bit()) {
1301 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1302 Offset = 0x28;
1303 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1304 AddressSpace = 256;
1305 else
1306 AddressSpace = 257;
1307 } else {
1308 // %gs:0x14 on i386
1309 Offset = 0x14;
1310 AddressSpace = 256;
1311 }
1312 return true;
1313}
1314
1315
Chris Lattner2b02a442007-02-25 08:29:00 +00001316//===----------------------------------------------------------------------===//
1317// Return Value Calling Convention Implementation
1318//===----------------------------------------------------------------------===//
1319
Chris Lattner59ed56b2007-02-28 04:55:35 +00001320#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001321
Michael J. Spencerec38de22010-10-10 22:04:20 +00001322bool
Eric Christopher471e4222011-06-08 23:55:35 +00001323X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1324 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001325 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001326 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001327 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001328 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001329 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001330 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001331}
1332
Dan Gohman98ca4f22009-08-05 01:29:28 +00001333SDValue
1334X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001335 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001337 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001338 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001339 MachineFunction &MF = DAG.getMachineFunction();
1340 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001341
Chris Lattner9774c912007-02-27 05:28:59 +00001342 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001343 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001344 RVLocs, *DAG.getContext());
1345 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001346
Evan Chengdcea1632010-02-04 02:40:39 +00001347 // Add the regs to the liveout set for the function.
1348 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1349 for (unsigned i = 0; i != RVLocs.size(); ++i)
1350 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1351 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001352
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001354
Dan Gohman475871a2008-07-27 21:46:04 +00001355 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001356 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1357 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001358 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1359 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001360
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001361 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001362 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1363 CCValAssign &VA = RVLocs[i];
1364 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001365 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001366 EVT ValVT = ValToCopy.getValueType();
1367
Dale Johannesenc4510512010-09-24 19:05:48 +00001368 // If this is x86-64, and we disabled SSE, we can't return FP values,
1369 // or SSE or MMX vectors.
1370 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1371 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001372 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001373 report_fatal_error("SSE register return with SSE disabled");
1374 }
1375 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1376 // llvm-gcc has never done it right and no one has noticed, so this
1377 // should be OK for now.
1378 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001379 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001380 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001381
Chris Lattner447ff682008-03-11 03:23:40 +00001382 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1383 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001384 if (VA.getLocReg() == X86::ST0 ||
1385 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001386 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1387 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001388 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001389 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001390 RetOps.push_back(ValToCopy);
1391 // Don't emit a copytoreg.
1392 continue;
1393 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001394
Evan Cheng242b38b2009-02-23 09:03:22 +00001395 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1396 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001397 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001398 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001399 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001400 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001401 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1402 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001403 // If we don't have SSE2 available, convert to v4f32 so the generated
1404 // register is legal.
1405 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001406 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001407 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001408 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001409 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001410
Dale Johannesendd64c412009-02-04 00:33:20 +00001411 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001412 Flag = Chain.getValue(1);
1413 }
Dan Gohman61a92132008-04-21 23:59:07 +00001414
1415 // The x86-64 ABI for returning structs by value requires that we copy
1416 // the sret argument into %rax for the return. We saved the argument into
1417 // a virtual register in the entry block, so now we copy the value out
1418 // and into %rax.
1419 if (Subtarget->is64Bit() &&
1420 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1421 MachineFunction &MF = DAG.getMachineFunction();
1422 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1423 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001424 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001425 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001426 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001427
Dale Johannesendd64c412009-02-04 00:33:20 +00001428 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001429 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001430
1431 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001432 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001433 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001434
Chris Lattner447ff682008-03-11 03:23:40 +00001435 RetOps[0] = Chain; // Update chain.
1436
1437 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001438 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001439 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001440
1441 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001442 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001443}
1444
Evan Cheng3d2125c2010-11-30 23:55:39 +00001445bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1446 if (N->getNumValues() != 1)
1447 return false;
1448 if (!N->hasNUsesOfValue(1, 0))
1449 return false;
1450
1451 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001452 if (Copy->getOpcode() != ISD::CopyToReg &&
1453 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001454 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001455
1456 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001457 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001458 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001459 if (UI->getOpcode() != X86ISD::RET_FLAG)
1460 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001461 HasRet = true;
1462 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001463
Evan Cheng1bf891a2010-12-01 22:59:46 +00001464 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001465}
1466
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001467EVT
1468X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001469 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001470 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001471 // TODO: Is this also valid on 32-bit?
1472 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001473 ReturnMVT = MVT::i8;
1474 else
1475 ReturnMVT = MVT::i32;
1476
1477 EVT MinVT = getRegisterType(Context, ReturnMVT);
1478 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001479}
1480
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481/// LowerCallResult - Lower the result values of a call into the
1482/// appropriate copies out of appropriate physical registers.
1483///
1484SDValue
1485X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001486 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487 const SmallVectorImpl<ISD::InputArg> &Ins,
1488 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001489 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001490
Chris Lattnere32bbf62007-02-28 07:09:55 +00001491 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001492 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001493 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001494 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1495 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001496 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001497
Chris Lattner3085e152007-02-25 08:59:22 +00001498 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001499 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001500 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001501 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001502
Torok Edwin3f142c32009-02-01 18:15:56 +00001503 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001504 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001505 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001506 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001507 }
1508
Evan Cheng79fb3b42009-02-20 20:43:02 +00001509 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001510
1511 // If this is a call to a function that returns an fp value on the floating
1512 // point stack, we must guarantee the the value is popped from the stack, so
1513 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1514 // if the return value is not used. We use the FpGET_ST0 instructions
1515 // instead.
1516 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1517 // If we prefer to use the value in xmm registers, copy it out as f80 and
1518 // use a truncate to move it from fp stack reg to xmm reg.
1519 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1520 bool isST0 = VA.getLocReg() == X86::ST0;
1521 unsigned Opc = 0;
1522 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1523 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1524 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1525 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001526 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001527 Ops, 2), 1);
1528 Val = Chain.getValue(0);
1529
1530 // Round the f80 to the right size, which also moves it to the appropriate
1531 // xmm register.
1532 if (CopyVT != VA.getValVT())
1533 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1534 // This truncation won't change the value.
1535 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001536 } else {
1537 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1538 CopyVT, InFlag).getValue(1);
1539 Val = Chain.getValue(0);
1540 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001541 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001543 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001544
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001546}
1547
1548
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001549//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001550// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001551//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001552// StdCall calling convention seems to be standard for many Windows' API
1553// routines and around. It differs from C calling convention just a little:
1554// callee should clean up the stack, not caller. Symbols should be also
1555// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001556// For info on fast calling convention see Fast Calling Convention (tail call)
1557// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001558
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001560/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1562 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001563 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001564
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001566}
1567
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001568/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001569/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570static bool
1571ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1572 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001573 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001574
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001576}
1577
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001578/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1579/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001580/// the specific parameter attribute. The copy will be passed as a byval
1581/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001582static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001583CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001584 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1585 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001586 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001587
Dale Johannesendd64c412009-02-04 00:33:20 +00001588 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001589 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001590 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001591}
1592
Chris Lattner29689432010-03-11 00:22:57 +00001593/// IsTailCallConvention - Return true if the calling convention is one that
1594/// supports tail call optimization.
1595static bool IsTailCallConvention(CallingConv::ID CC) {
1596 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1597}
1598
Evan Cheng485fafc2011-03-21 01:19:09 +00001599bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1600 if (!CI->isTailCall())
1601 return false;
1602
1603 CallSite CS(CI);
1604 CallingConv::ID CalleeCC = CS.getCallingConv();
1605 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1606 return false;
1607
1608 return true;
1609}
1610
Evan Cheng0c439eb2010-01-27 00:07:07 +00001611/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1612/// a tailcall target by changing its ABI.
1613static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001614 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001615}
1616
Dan Gohman98ca4f22009-08-05 01:29:28 +00001617SDValue
1618X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001619 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001620 const SmallVectorImpl<ISD::InputArg> &Ins,
1621 DebugLoc dl, SelectionDAG &DAG,
1622 const CCValAssign &VA,
1623 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001624 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001625 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001626 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001627 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001628 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001629 EVT ValVT;
1630
1631 // If value is passed by pointer we have address passed instead of the value
1632 // itself.
1633 if (VA.getLocInfo() == CCValAssign::Indirect)
1634 ValVT = VA.getLocVT();
1635 else
1636 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001637
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001638 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001639 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001640 // In case of tail call optimization mark all arguments mutable. Since they
1641 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001642 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001643 unsigned Bytes = Flags.getByValSize();
1644 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1645 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001646 return DAG.getFrameIndex(FI, getPointerTy());
1647 } else {
1648 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001649 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001650 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1651 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001652 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001653 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001654 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001655}
1656
Dan Gohman475871a2008-07-27 21:46:04 +00001657SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001659 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660 bool isVarArg,
1661 const SmallVectorImpl<ISD::InputArg> &Ins,
1662 DebugLoc dl,
1663 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001664 SmallVectorImpl<SDValue> &InVals)
1665 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001666 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001668
Gordon Henriksen86737662008-01-05 16:56:59 +00001669 const Function* Fn = MF.getFunction();
1670 if (Fn->hasExternalLinkage() &&
1671 Subtarget->isTargetCygMing() &&
1672 Fn->getName() == "main")
1673 FuncInfo->setForceFramePointer(true);
1674
Evan Cheng1bc78042006-04-26 01:20:17 +00001675 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001676 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001677 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001678
Chris Lattner29689432010-03-11 00:22:57 +00001679 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1680 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001681
Chris Lattner638402b2007-02-28 07:00:42 +00001682 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001683 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001684 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001685 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001686
1687 // Allocate shadow area for Win64
1688 if (IsWin64) {
1689 CCInfo.AllocateStack(32, 8);
1690 }
1691
Duncan Sands45907662010-10-31 13:21:44 +00001692 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001693
Chris Lattnerf39f7712007-02-28 05:46:49 +00001694 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001695 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001696 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1697 CCValAssign &VA = ArgLocs[i];
1698 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1699 // places.
1700 assert(VA.getValNo() != LastVal &&
1701 "Don't support value assigned to multiple locs yet");
1702 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001703
Chris Lattnerf39f7712007-02-28 05:46:49 +00001704 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001705 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001706 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001708 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001714 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001715 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1716 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001717 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001718 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001719 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001720 RC = X86::VR64RegisterClass;
1721 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001722 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001723
Devang Patel68e6bee2011-02-21 23:21:26 +00001724 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001726
Chris Lattnerf39f7712007-02-28 05:46:49 +00001727 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1728 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1729 // right size.
1730 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001731 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001732 DAG.getValueType(VA.getValVT()));
1733 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001734 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001735 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001736 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001737 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001738
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001739 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001740 // Handle MMX values passed in XMM regs.
1741 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001742 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1743 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001744 } else
1745 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001746 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001747 } else {
1748 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001749 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001750 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001751
1752 // If value is passed via pointer - do a load.
1753 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001754 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1755 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001756
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001758 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001759
Dan Gohman61a92132008-04-21 23:59:07 +00001760 // The x86-64 ABI for returning structs by value requires that we copy
1761 // the sret argument into %rax for the return. Save the argument into
1762 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001763 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001764 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1765 unsigned Reg = FuncInfo->getSRetReturnReg();
1766 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001768 FuncInfo->setSRetReturnReg(Reg);
1769 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001772 }
1773
Chris Lattnerf39f7712007-02-28 05:46:49 +00001774 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001775 // Align stack specially for tail calls.
1776 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001777 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001778
Evan Cheng1bc78042006-04-26 01:20:17 +00001779 // If the function takes variable number of arguments, make a frame index for
1780 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001781 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001782 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1783 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001784 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001785 }
1786 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001787 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1788
1789 // FIXME: We should really autogenerate these arrays
1790 static const unsigned GPR64ArgRegsWin64[] = {
1791 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001792 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001793 static const unsigned GPR64ArgRegs64Bit[] = {
1794 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1795 };
1796 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001797 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1798 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1799 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001800 const unsigned *GPR64ArgRegs;
1801 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001802
1803 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001804 // The XMM registers which might contain var arg parameters are shadowed
1805 // in their paired GPR. So we only need to save the GPR to their home
1806 // slots.
1807 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001808 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001809 } else {
1810 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1811 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001812
1813 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001814 }
1815 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1816 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001817
Devang Patel578efa92009-06-05 21:57:13 +00001818 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001819 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001820 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001821 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001822 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001823 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001824 // Kernel mode asks for SSE to be disabled, so don't push them
1825 // on the stack.
1826 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001827
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001828 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001829 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001830 // Get to the caller-allocated home save location. Add 8 to account
1831 // for the return address.
1832 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001833 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001834 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001835 // Fixup to set vararg frame on shadow area (4 x i64).
1836 if (NumIntRegs < 4)
1837 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001838 } else {
1839 // For X86-64, if there are vararg parameters that are passed via
1840 // registers, then we must store them to their spots on the stack so they
1841 // may be loaded by deferencing the result of va_next.
1842 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1843 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1844 FuncInfo->setRegSaveFrameIndex(
1845 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001846 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001847 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001848
Gordon Henriksen86737662008-01-05 16:56:59 +00001849 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001851 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1852 getPointerTy());
1853 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001854 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001855 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1856 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001857 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001858 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001860 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001861 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001862 MachinePointerInfo::getFixedStack(
1863 FuncInfo->getRegSaveFrameIndex(), Offset),
1864 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001865 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001866 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001867 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001868
Dan Gohmanface41a2009-08-16 21:24:25 +00001869 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1870 // Now store the XMM (fp + vector) parameter registers.
1871 SmallVector<SDValue, 11> SaveXMMOps;
1872 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001873
Devang Patel68e6bee2011-02-21 23:21:26 +00001874 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001875 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1876 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001877
Dan Gohman1e93df62010-04-17 14:41:14 +00001878 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1879 FuncInfo->getRegSaveFrameIndex()));
1880 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1881 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001882
Dan Gohmanface41a2009-08-16 21:24:25 +00001883 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001884 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001885 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001886 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1887 SaveXMMOps.push_back(Val);
1888 }
1889 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1890 MVT::Other,
1891 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001893
1894 if (!MemOps.empty())
1895 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1896 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001898 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001901 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001902 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001903 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001904 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001905 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001906 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001907 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001908 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001909
Gordon Henriksen86737662008-01-05 16:56:59 +00001910 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001911 // RegSaveFrameIndex is X86-64 only.
1912 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001913 if (CallConv == CallingConv::X86_FastCall ||
1914 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001915 // fastcc functions can't have varargs.
1916 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001917 }
Evan Cheng25caf632006-05-23 21:06:34 +00001918
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001920}
1921
Dan Gohman475871a2008-07-27 21:46:04 +00001922SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1924 SDValue StackPtr, SDValue Arg,
1925 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001926 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001927 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001928 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001929 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001930 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001931 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001932 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001933
1934 return DAG.getStore(Chain, dl, Arg, PtrOff,
1935 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001936 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001937}
1938
Bill Wendling64e87322009-01-16 19:25:27 +00001939/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001940/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001941SDValue
1942X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001943 SDValue &OutRetAddr, SDValue Chain,
1944 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001945 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001946 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001947 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001948 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001949
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001950 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001951 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1952 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001953 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001954}
1955
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001956/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001957/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001958static SDValue
1959EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001961 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001962 // Store the return address to the appropriate stack slot.
1963 if (!FPDiff) return Chain;
1964 // Calculate the new stack slot for the return address.
1965 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001966 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001967 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001968 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001969 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001970 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001971 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001972 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001973 return Chain;
1974}
1975
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001977X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001978 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001979 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001981 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 const SmallVectorImpl<ISD::InputArg> &Ins,
1983 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001984 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001985 MachineFunction &MF = DAG.getMachineFunction();
1986 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001987 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001988 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001989 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990
Evan Cheng5f941932010-02-05 02:21:12 +00001991 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001992 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001993 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1994 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001995 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001996
1997 // Sibcalls are automatically detected tailcalls which do not require
1998 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001999 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002000 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002001
2002 if (isTailCall)
2003 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002004 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002005
Chris Lattner29689432010-03-11 00:22:57 +00002006 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2007 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002008
Chris Lattner638402b2007-02-28 07:00:42 +00002009 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002010 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002011 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002012 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002013
2014 // Allocate shadow area for Win64
2015 if (IsWin64) {
2016 CCInfo.AllocateStack(32, 8);
2017 }
2018
Duncan Sands45907662010-10-31 13:21:44 +00002019 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002020
Chris Lattner423c5f42007-02-28 05:31:48 +00002021 // Get a count of how many bytes are to be pushed on the stack.
2022 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002023 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002024 // This is a sibcall. The memory operands are available in caller's
2025 // own caller's stack.
2026 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002027 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002028 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002029
Gordon Henriksen86737662008-01-05 16:56:59 +00002030 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002031 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002033 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2035 FPDiff = NumBytesCallerPushed - NumBytes;
2036
2037 // Set the delta of movement of the returnaddr stackslot.
2038 // But only set if delta is greater than previous delta.
2039 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2040 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2041 }
2042
Evan Chengf22f9b32010-02-06 03:28:46 +00002043 if (!IsSibcall)
2044 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002045
Dan Gohman475871a2008-07-27 21:46:04 +00002046 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002047 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002048 if (isTailCall && FPDiff)
2049 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2050 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002051
Dan Gohman475871a2008-07-27 21:46:04 +00002052 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2053 SmallVector<SDValue, 8> MemOpChains;
2054 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002055
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002056 // Walk the register/memloc assignments, inserting copies/loads. In the case
2057 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002058 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2059 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002060 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002061 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002062 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002063 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002064
Chris Lattner423c5f42007-02-28 05:31:48 +00002065 // Promote the value if needed.
2066 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002067 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002068 case CCValAssign::Full: break;
2069 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002070 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002071 break;
2072 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002073 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002074 break;
2075 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002076 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2077 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002078 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002079 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2080 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002081 } else
2082 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2083 break;
2084 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002085 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002086 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002087 case CCValAssign::Indirect: {
2088 // Store the argument.
2089 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002090 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002091 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002092 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002093 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002094 Arg = SpillSlot;
2095 break;
2096 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002097 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002098
Chris Lattner423c5f42007-02-28 05:31:48 +00002099 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002100 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2101 if (isVarArg && IsWin64) {
2102 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2103 // shadow reg if callee is a varargs function.
2104 unsigned ShadowReg = 0;
2105 switch (VA.getLocReg()) {
2106 case X86::XMM0: ShadowReg = X86::RCX; break;
2107 case X86::XMM1: ShadowReg = X86::RDX; break;
2108 case X86::XMM2: ShadowReg = X86::R8; break;
2109 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002110 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002111 if (ShadowReg)
2112 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002113 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002114 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002115 assert(VA.isMemLoc());
2116 if (StackPtr.getNode() == 0)
2117 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2118 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2119 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002120 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002122
Evan Cheng32fe1032006-05-25 00:59:30 +00002123 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002124 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002125 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002126
Evan Cheng347d5f72006-04-28 21:29:37 +00002127 // Build a sequence of copy-to-reg nodes chained together with token chain
2128 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002129 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002130 // Tail call byval lowering might overwrite argument registers so in case of
2131 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002132 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002133 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002134 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002135 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002136 InFlag = Chain.getValue(1);
2137 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002138
Chris Lattner88e1fd52009-07-09 04:24:46 +00002139 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002140 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2141 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002142 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002143 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2144 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002145 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002146 InFlag);
2147 InFlag = Chain.getValue(1);
2148 } else {
2149 // If we are tail calling and generating PIC/GOT style code load the
2150 // address of the callee into ECX. The value in ecx is used as target of
2151 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2152 // for tail calls on PIC/GOT architectures. Normally we would just put the
2153 // address of GOT into ebx and then call target@PLT. But for tail calls
2154 // ebx would be restored (since ebx is callee saved) before jumping to the
2155 // target@PLT.
2156
2157 // Note: The actual moving to ECX is done further down.
2158 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2159 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2160 !G->getGlobal()->hasProtectedVisibility())
2161 Callee = LowerGlobalAddress(Callee, DAG);
2162 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002163 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002164 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002165 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002166
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002167 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002168 // From AMD64 ABI document:
2169 // For calls that may call functions that use varargs or stdargs
2170 // (prototype-less calls or calls to functions containing ellipsis (...) in
2171 // the declaration) %al is used as hidden argument to specify the number
2172 // of SSE registers used. The contents of %al do not need to match exactly
2173 // the number of registers, but must be an ubound on the number of SSE
2174 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002175
Gordon Henriksen86737662008-01-05 16:56:59 +00002176 // Count the number of XMM registers allocated.
2177 static const unsigned XMMArgRegs[] = {
2178 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2179 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2180 };
2181 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002182 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002183 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002184
Dale Johannesendd64c412009-02-04 00:33:20 +00002185 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002186 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002187 InFlag = Chain.getValue(1);
2188 }
2189
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002190
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002191 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002192 if (isTailCall) {
2193 // Force all the incoming stack arguments to be loaded from the stack
2194 // before any new outgoing arguments are stored to the stack, because the
2195 // outgoing stack slots may alias the incoming argument stack slots, and
2196 // the alias isn't otherwise explicit. This is slightly more conservative
2197 // than necessary, because it means that each store effectively depends
2198 // on every argument instead of just those arguments it would clobber.
2199 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2200
Dan Gohman475871a2008-07-27 21:46:04 +00002201 SmallVector<SDValue, 8> MemOpChains2;
2202 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002203 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002204 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002205 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002206 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002207 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2208 CCValAssign &VA = ArgLocs[i];
2209 if (VA.isRegLoc())
2210 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002211 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002212 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002214 // Create frame index.
2215 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002216 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002217 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002218 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002219
Duncan Sands276dcbd2008-03-21 09:14:45 +00002220 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002221 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002222 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002223 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002224 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002225 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002226 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002227
Dan Gohman98ca4f22009-08-05 01:29:28 +00002228 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2229 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002230 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002231 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002232 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002233 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002235 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002236 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002237 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 }
2239 }
2240
2241 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002242 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002243 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002244
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002245 // Copy arguments to their registers.
2246 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002247 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002248 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002249 InFlag = Chain.getValue(1);
2250 }
Dan Gohman475871a2008-07-27 21:46:04 +00002251 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002252
Gordon Henriksen86737662008-01-05 16:56:59 +00002253 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002254 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002255 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002256 }
2257
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002258 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2259 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2260 // In the 64-bit large code model, we have to make all calls
2261 // through a register, since the call instruction's 32-bit
2262 // pc-relative offset may not be large enough to hold the whole
2263 // address.
2264 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002265 // If the callee is a GlobalAddress node (quite common, every direct call
2266 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2267 // it.
2268
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002269 // We should use extra load for direct calls to dllimported functions in
2270 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002271 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002272 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002273 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002274 bool ExtraLoad = false;
2275 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002276
Chris Lattner48a7d022009-07-09 05:02:21 +00002277 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2278 // external symbols most go through the PLT in PIC mode. If the symbol
2279 // has hidden or protected visibility, or if it is static or local, then
2280 // we don't need to use the PLT - we can directly call it.
2281 if (Subtarget->isTargetELF() &&
2282 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002283 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002284 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002285 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002286 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002287 (!Subtarget->getTargetTriple().isMacOSX() ||
2288 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002289 // PC-relative references to external symbols should go through $stub,
2290 // unless we're building with the leopard linker or later, which
2291 // automatically synthesizes these stubs.
2292 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002293 } else if (Subtarget->isPICStyleRIPRel() &&
2294 isa<Function>(GV) &&
2295 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2296 // If the function is marked as non-lazy, generate an indirect call
2297 // which loads from the GOT directly. This avoids runtime overhead
2298 // at the cost of eager binding (and one extra byte of encoding).
2299 OpFlags = X86II::MO_GOTPCREL;
2300 WrapperKind = X86ISD::WrapperRIP;
2301 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002302 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002303
Devang Patel0d881da2010-07-06 22:08:15 +00002304 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002305 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002306
2307 // Add a wrapper if needed.
2308 if (WrapperKind != ISD::DELETED_NODE)
2309 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2310 // Add extra indirection if needed.
2311 if (ExtraLoad)
2312 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2313 MachinePointerInfo::getGOT(),
2314 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002315 }
Bill Wendling056292f2008-09-16 21:48:12 +00002316 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002317 unsigned char OpFlags = 0;
2318
Evan Cheng1bf891a2010-12-01 22:59:46 +00002319 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2320 // external symbols should go through the PLT.
2321 if (Subtarget->isTargetELF() &&
2322 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2323 OpFlags = X86II::MO_PLT;
2324 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002325 (!Subtarget->getTargetTriple().isMacOSX() ||
2326 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002327 // PC-relative references to external symbols should go through $stub,
2328 // unless we're building with the leopard linker or later, which
2329 // automatically synthesizes these stubs.
2330 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002331 }
Eric Christopherfd179292009-08-27 18:07:15 +00002332
Chris Lattner48a7d022009-07-09 05:02:21 +00002333 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2334 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002335 }
2336
Chris Lattnerd96d0722007-02-25 06:40:16 +00002337 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002338 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002339 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002340
Evan Chengf22f9b32010-02-06 03:28:46 +00002341 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002342 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2343 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002344 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002346
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002347 Ops.push_back(Chain);
2348 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002349
Dan Gohman98ca4f22009-08-05 01:29:28 +00002350 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002351 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002352
Gordon Henriksen86737662008-01-05 16:56:59 +00002353 // Add argument registers to the end of the list so that they are known live
2354 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002355 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2356 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2357 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002358
Evan Cheng586ccac2008-03-18 23:36:35 +00002359 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002360 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002361 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2362
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002363 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002364 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002365 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002366
Gabor Greifba36cb52008-08-28 21:40:38 +00002367 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002368 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002369
Dan Gohman98ca4f22009-08-05 01:29:28 +00002370 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002371 // We used to do:
2372 //// If this is the first return lowered for this function, add the regs
2373 //// to the liveout set for the function.
2374 // This isn't right, although it's probably harmless on x86; liveouts
2375 // should be computed from returns not tail calls. Consider a void
2376 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002377 return DAG.getNode(X86ISD::TC_RETURN, dl,
2378 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002379 }
2380
Dale Johannesenace16102009-02-03 19:33:06 +00002381 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002382 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002383
Chris Lattner2d297092006-05-23 18:50:38 +00002384 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002385 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002386 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002387 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002388 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002389 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002390 // pops the hidden struct pointer, so we have to push it back.
2391 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002392 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002393 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002394 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002395
Gordon Henriksenae636f82008-01-03 16:47:34 +00002396 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002397 if (!IsSibcall) {
2398 Chain = DAG.getCALLSEQ_END(Chain,
2399 DAG.getIntPtrConstant(NumBytes, true),
2400 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2401 true),
2402 InFlag);
2403 InFlag = Chain.getValue(1);
2404 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002405
Chris Lattner3085e152007-02-25 08:59:22 +00002406 // Handle result values, copying them out of physregs into vregs that we
2407 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002408 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2409 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002410}
2411
Evan Cheng25ab6902006-09-08 06:48:29 +00002412
2413//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002414// Fast Calling Convention (tail call) implementation
2415//===----------------------------------------------------------------------===//
2416
2417// Like std call, callee cleans arguments, convention except that ECX is
2418// reserved for storing the tail called function address. Only 2 registers are
2419// free for argument passing (inreg). Tail call optimization is performed
2420// provided:
2421// * tailcallopt is enabled
2422// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002423// On X86_64 architecture with GOT-style position independent code only local
2424// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002425// To keep the stack aligned according to platform abi the function
2426// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2427// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002428// If a tail called function callee has more arguments than the caller the
2429// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002430// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002431// original REtADDR, but before the saved framepointer or the spilled registers
2432// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2433// stack layout:
2434// arg1
2435// arg2
2436// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002437// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002438// move area ]
2439// (possible EBP)
2440// ESI
2441// EDI
2442// local1 ..
2443
2444/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2445/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002446unsigned
2447X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2448 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002449 MachineFunction &MF = DAG.getMachineFunction();
2450 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002451 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002452 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002453 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002454 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002455 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002456 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2457 // Number smaller than 12 so just add the difference.
2458 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2459 } else {
2460 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002461 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002462 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002463 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002464 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002465}
2466
Evan Cheng5f941932010-02-05 02:21:12 +00002467/// MatchingStackOffset - Return true if the given stack call argument is
2468/// already available in the same position (relatively) of the caller's
2469/// incoming argument stack.
2470static
2471bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2472 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2473 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002474 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2475 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002476 if (Arg.getOpcode() == ISD::CopyFromReg) {
2477 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002478 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002479 return false;
2480 MachineInstr *Def = MRI->getVRegDef(VR);
2481 if (!Def)
2482 return false;
2483 if (!Flags.isByVal()) {
2484 if (!TII->isLoadFromStackSlot(Def, FI))
2485 return false;
2486 } else {
2487 unsigned Opcode = Def->getOpcode();
2488 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2489 Def->getOperand(1).isFI()) {
2490 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002491 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002492 } else
2493 return false;
2494 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002495 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2496 if (Flags.isByVal())
2497 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002498 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002499 // define @foo(%struct.X* %A) {
2500 // tail call @bar(%struct.X* byval %A)
2501 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002502 return false;
2503 SDValue Ptr = Ld->getBasePtr();
2504 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2505 if (!FINode)
2506 return false;
2507 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002508 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002509 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002510 FI = FINode->getIndex();
2511 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002512 } else
2513 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002514
Evan Cheng4cae1332010-03-05 08:38:04 +00002515 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002516 if (!MFI->isFixedObjectIndex(FI))
2517 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002518 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002519}
2520
Dan Gohman98ca4f22009-08-05 01:29:28 +00002521/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2522/// for tail call optimization. Targets which want to do tail call
2523/// optimization should implement this function.
2524bool
2525X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002526 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002527 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002528 bool isCalleeStructRet,
2529 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002530 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002531 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002532 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002533 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002534 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002535 CalleeCC != CallingConv::C)
2536 return false;
2537
Evan Cheng7096ae42010-01-29 06:45:59 +00002538 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002539 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002540 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002541 CallingConv::ID CallerCC = CallerF->getCallingConv();
2542 bool CCMatch = CallerCC == CalleeCC;
2543
Dan Gohman1797ed52010-02-08 20:27:50 +00002544 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002545 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002546 return true;
2547 return false;
2548 }
2549
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002550 // Look for obvious safe cases to perform tail call optimization that do not
2551 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002552
Evan Cheng2c12cb42010-03-26 16:26:03 +00002553 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2554 // emit a special epilogue.
2555 if (RegInfo->needsStackRealignment(MF))
2556 return false;
2557
Evan Chenga375d472010-03-15 18:54:48 +00002558 // Also avoid sibcall optimization if either caller or callee uses struct
2559 // return semantics.
2560 if (isCalleeStructRet || isCallerStructRet)
2561 return false;
2562
Chad Rosier2416da32011-06-24 21:15:36 +00002563 // An stdcall caller is expected to clean up its arguments; the callee
2564 // isn't going to do that.
2565 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2566 return false;
2567
Chad Rosier871f6642011-05-18 19:59:50 +00002568 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002569 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002570 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002571
2572 // Optimizing for varargs on Win64 is unlikely to be safe without
2573 // additional testing.
2574 if (Subtarget->isTargetWin64())
2575 return false;
2576
Chad Rosier871f6642011-05-18 19:59:50 +00002577 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002578 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2579 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002580
Chad Rosier871f6642011-05-18 19:59:50 +00002581 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2582 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2583 if (!ArgLocs[i].isRegLoc())
2584 return false;
2585 }
2586
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002587 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2588 // Therefore if it's not used by the call it is not safe to optimize this into
2589 // a sibcall.
2590 bool Unused = false;
2591 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2592 if (!Ins[i].Used) {
2593 Unused = true;
2594 break;
2595 }
2596 }
2597 if (Unused) {
2598 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002599 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2600 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002601 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002602 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002603 CCValAssign &VA = RVLocs[i];
2604 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2605 return false;
2606 }
2607 }
2608
Evan Cheng13617962010-04-30 01:12:32 +00002609 // If the calling conventions do not match, then we'd better make sure the
2610 // results are returned in the same way as what the caller expects.
2611 if (!CCMatch) {
2612 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002613 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2614 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002615 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2616
2617 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002618 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2619 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002620 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2621
2622 if (RVLocs1.size() != RVLocs2.size())
2623 return false;
2624 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2625 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2626 return false;
2627 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2628 return false;
2629 if (RVLocs1[i].isRegLoc()) {
2630 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2631 return false;
2632 } else {
2633 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2634 return false;
2635 }
2636 }
2637 }
2638
Evan Chenga6bff982010-01-30 01:22:00 +00002639 // If the callee takes no arguments then go on to check the results of the
2640 // call.
2641 if (!Outs.empty()) {
2642 // Check if stack adjustment is needed. For now, do not do this if any
2643 // argument is passed on the stack.
2644 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002645 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2646 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002647
2648 // Allocate shadow area for Win64
2649 if (Subtarget->isTargetWin64()) {
2650 CCInfo.AllocateStack(32, 8);
2651 }
2652
Duncan Sands45907662010-10-31 13:21:44 +00002653 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002654 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002655 MachineFunction &MF = DAG.getMachineFunction();
2656 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2657 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002658
2659 // Check if the arguments are already laid out in the right way as
2660 // the caller's fixed stack objects.
2661 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002662 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2663 const X86InstrInfo *TII =
2664 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002665 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2666 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002667 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002668 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002669 if (VA.getLocInfo() == CCValAssign::Indirect)
2670 return false;
2671 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002672 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2673 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002674 return false;
2675 }
2676 }
2677 }
Evan Cheng9c044672010-05-29 01:35:22 +00002678
2679 // If the tailcall address may be in a register, then make sure it's
2680 // possible to register allocate for it. In 32-bit, the call address can
2681 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002682 // callee-saved registers are restored. These happen to be the same
2683 // registers used to pass 'inreg' arguments so watch out for those.
2684 if (!Subtarget->is64Bit() &&
2685 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002686 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002687 unsigned NumInRegs = 0;
2688 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2689 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002690 if (!VA.isRegLoc())
2691 continue;
2692 unsigned Reg = VA.getLocReg();
2693 switch (Reg) {
2694 default: break;
2695 case X86::EAX: case X86::EDX: case X86::ECX:
2696 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002697 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002698 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002699 }
2700 }
2701 }
Evan Chenga6bff982010-01-30 01:22:00 +00002702 }
Evan Chengb1712452010-01-27 06:25:16 +00002703
Evan Cheng86809cc2010-02-03 03:28:02 +00002704 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002705}
2706
Dan Gohman3df24e62008-09-03 23:12:08 +00002707FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002708X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2709 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002710}
2711
2712
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002713//===----------------------------------------------------------------------===//
2714// Other Lowering Hooks
2715//===----------------------------------------------------------------------===//
2716
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002717static bool MayFoldLoad(SDValue Op) {
2718 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2719}
2720
2721static bool MayFoldIntoStore(SDValue Op) {
2722 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2723}
2724
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002725static bool isTargetShuffle(unsigned Opcode) {
2726 switch(Opcode) {
2727 default: return false;
2728 case X86ISD::PSHUFD:
2729 case X86ISD::PSHUFHW:
2730 case X86ISD::PSHUFLW:
2731 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002732 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002733 case X86ISD::SHUFPS:
2734 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002735 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002736 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002737 case X86ISD::MOVLPS:
2738 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002739 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002740 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002741 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002742 case X86ISD::MOVSS:
2743 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002744 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002745 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002746 case X86ISD::VUNPCKLPS:
2747 case X86ISD::VUNPCKLPD:
2748 case X86ISD::VUNPCKLPSY:
2749 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002750 case X86ISD::PUNPCKLWD:
2751 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002752 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002753 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002754 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002755 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002756 case X86ISD::PUNPCKHWD:
2757 case X86ISD::PUNPCKHBW:
2758 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002759 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002760 return true;
2761 }
2762 return false;
2763}
2764
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002765static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002766 SDValue V1, SelectionDAG &DAG) {
2767 switch(Opc) {
2768 default: llvm_unreachable("Unknown x86 shuffle node");
2769 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002770 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002771 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002772 return DAG.getNode(Opc, dl, VT, V1);
2773 }
2774
2775 return SDValue();
2776}
2777
2778static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002779 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002780 switch(Opc) {
2781 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002782 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002783 case X86ISD::PSHUFHW:
2784 case X86ISD::PSHUFLW:
2785 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2786 }
2787
2788 return SDValue();
2789}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002790
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002791static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2792 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2793 switch(Opc) {
2794 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002795 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002796 case X86ISD::SHUFPD:
2797 case X86ISD::SHUFPS:
2798 return DAG.getNode(Opc, dl, VT, V1, V2,
2799 DAG.getConstant(TargetMask, MVT::i8));
2800 }
2801 return SDValue();
2802}
2803
2804static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2805 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2806 switch(Opc) {
2807 default: llvm_unreachable("Unknown x86 shuffle node");
2808 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002809 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002810 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002811 case X86ISD::MOVLPS:
2812 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002813 case X86ISD::MOVSS:
2814 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002815 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002816 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002817 case X86ISD::VUNPCKLPS:
2818 case X86ISD::VUNPCKLPD:
2819 case X86ISD::VUNPCKLPSY:
2820 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002821 case X86ISD::PUNPCKLWD:
2822 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002823 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002824 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002825 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002826 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002827 case X86ISD::PUNPCKHWD:
2828 case X86ISD::PUNPCKHBW:
2829 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002830 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002831 return DAG.getNode(Opc, dl, VT, V1, V2);
2832 }
2833 return SDValue();
2834}
2835
Dan Gohmand858e902010-04-17 15:26:15 +00002836SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002837 MachineFunction &MF = DAG.getMachineFunction();
2838 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2839 int ReturnAddrIndex = FuncInfo->getRAIndex();
2840
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002841 if (ReturnAddrIndex == 0) {
2842 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002843 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002844 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002845 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002846 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002847 }
2848
Evan Cheng25ab6902006-09-08 06:48:29 +00002849 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002850}
2851
2852
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002853bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2854 bool hasSymbolicDisplacement) {
2855 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002856 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002857 return false;
2858
2859 // If we don't have a symbolic displacement - we don't have any extra
2860 // restrictions.
2861 if (!hasSymbolicDisplacement)
2862 return true;
2863
2864 // FIXME: Some tweaks might be needed for medium code model.
2865 if (M != CodeModel::Small && M != CodeModel::Kernel)
2866 return false;
2867
2868 // For small code model we assume that latest object is 16MB before end of 31
2869 // bits boundary. We may also accept pretty large negative constants knowing
2870 // that all objects are in the positive half of address space.
2871 if (M == CodeModel::Small && Offset < 16*1024*1024)
2872 return true;
2873
2874 // For kernel code model we know that all object resist in the negative half
2875 // of 32bits address space. We may not accept negative offsets, since they may
2876 // be just off and we may accept pretty large positive ones.
2877 if (M == CodeModel::Kernel && Offset > 0)
2878 return true;
2879
2880 return false;
2881}
2882
Evan Chengef41ff62011-06-23 17:54:54 +00002883/// isCalleePop - Determines whether the callee is required to pop its
2884/// own arguments. Callee pop is necessary to support tail calls.
2885bool X86::isCalleePop(CallingConv::ID CallingConv,
2886 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2887 if (IsVarArg)
2888 return false;
2889
2890 switch (CallingConv) {
2891 default:
2892 return false;
2893 case CallingConv::X86_StdCall:
2894 return !is64Bit;
2895 case CallingConv::X86_FastCall:
2896 return !is64Bit;
2897 case CallingConv::X86_ThisCall:
2898 return !is64Bit;
2899 case CallingConv::Fast:
2900 return TailCallOpt;
2901 case CallingConv::GHC:
2902 return TailCallOpt;
2903 }
2904}
2905
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002906/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2907/// specific condition code, returning the condition code and the LHS/RHS of the
2908/// comparison to make.
2909static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2910 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002911 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002912 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2913 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2914 // X > -1 -> X == 0, jump !sign.
2915 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002916 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002917 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2918 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002919 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002920 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002921 // X < 1 -> X <= 0
2922 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002923 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002924 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002925 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002926
Evan Chengd9558e02006-01-06 00:43:03 +00002927 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002928 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002929 case ISD::SETEQ: return X86::COND_E;
2930 case ISD::SETGT: return X86::COND_G;
2931 case ISD::SETGE: return X86::COND_GE;
2932 case ISD::SETLT: return X86::COND_L;
2933 case ISD::SETLE: return X86::COND_LE;
2934 case ISD::SETNE: return X86::COND_NE;
2935 case ISD::SETULT: return X86::COND_B;
2936 case ISD::SETUGT: return X86::COND_A;
2937 case ISD::SETULE: return X86::COND_BE;
2938 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002939 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002940 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002941
Chris Lattner4c78e022008-12-23 23:42:27 +00002942 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002943
Chris Lattner4c78e022008-12-23 23:42:27 +00002944 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002945 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2946 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002947 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2948 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002949 }
2950
Chris Lattner4c78e022008-12-23 23:42:27 +00002951 switch (SetCCOpcode) {
2952 default: break;
2953 case ISD::SETOLT:
2954 case ISD::SETOLE:
2955 case ISD::SETUGT:
2956 case ISD::SETUGE:
2957 std::swap(LHS, RHS);
2958 break;
2959 }
2960
2961 // On a floating point condition, the flags are set as follows:
2962 // ZF PF CF op
2963 // 0 | 0 | 0 | X > Y
2964 // 0 | 0 | 1 | X < Y
2965 // 1 | 0 | 0 | X == Y
2966 // 1 | 1 | 1 | unordered
2967 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002968 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002969 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002970 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002971 case ISD::SETOLT: // flipped
2972 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002973 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002974 case ISD::SETOLE: // flipped
2975 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002976 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002977 case ISD::SETUGT: // flipped
2978 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002979 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002980 case ISD::SETUGE: // flipped
2981 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002982 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002983 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002984 case ISD::SETNE: return X86::COND_NE;
2985 case ISD::SETUO: return X86::COND_P;
2986 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002987 case ISD::SETOEQ:
2988 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002989 }
Evan Chengd9558e02006-01-06 00:43:03 +00002990}
2991
Evan Cheng4a460802006-01-11 00:33:36 +00002992/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2993/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002994/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002995static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002996 switch (X86CC) {
2997 default:
2998 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002999 case X86::COND_B:
3000 case X86::COND_BE:
3001 case X86::COND_E:
3002 case X86::COND_P:
3003 case X86::COND_A:
3004 case X86::COND_AE:
3005 case X86::COND_NE:
3006 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003007 return true;
3008 }
3009}
3010
Evan Chengeb2f9692009-10-27 19:56:55 +00003011/// isFPImmLegal - Returns true if the target can instruction select the
3012/// specified FP immediate natively. If false, the legalizer will
3013/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003014bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003015 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3016 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3017 return true;
3018 }
3019 return false;
3020}
3021
Nate Begeman9008ca62009-04-27 18:41:29 +00003022/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3023/// the specified range (L, H].
3024static bool isUndefOrInRange(int Val, int Low, int Hi) {
3025 return (Val < 0) || (Val >= Low && Val < Hi);
3026}
3027
3028/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3029/// specified value.
3030static bool isUndefOrEqual(int Val, int CmpVal) {
3031 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003032 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003034}
3035
Nate Begeman9008ca62009-04-27 18:41:29 +00003036/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3037/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3038/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003039static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003040 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003041 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003042 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 return (Mask[0] < 2 && Mask[1] < 2);
3044 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003045}
3046
Nate Begeman9008ca62009-04-27 18:41:29 +00003047bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003048 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 N->getMask(M);
3050 return ::isPSHUFDMask(M, N->getValueType(0));
3051}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003052
Nate Begeman9008ca62009-04-27 18:41:29 +00003053/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3054/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003055static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003056 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003057 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003058
Nate Begeman9008ca62009-04-27 18:41:29 +00003059 // Lower quadword copied in order or undef.
3060 for (int i = 0; i != 4; ++i)
3061 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003062 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003063
Evan Cheng506d3df2006-03-29 23:07:14 +00003064 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 for (int i = 4; i != 8; ++i)
3066 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003067 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003068
Evan Cheng506d3df2006-03-29 23:07:14 +00003069 return true;
3070}
3071
Nate Begeman9008ca62009-04-27 18:41:29 +00003072bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003073 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 N->getMask(M);
3075 return ::isPSHUFHWMask(M, N->getValueType(0));
3076}
Evan Cheng506d3df2006-03-29 23:07:14 +00003077
Nate Begeman9008ca62009-04-27 18:41:29 +00003078/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3079/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003080static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003082 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003083
Rafael Espindola15684b22009-04-24 12:40:33 +00003084 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003085 for (int i = 4; i != 8; ++i)
3086 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003087 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003088
Rafael Espindola15684b22009-04-24 12:40:33 +00003089 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003090 for (int i = 0; i != 4; ++i)
3091 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003092 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003093
Rafael Espindola15684b22009-04-24 12:40:33 +00003094 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003095}
3096
Nate Begeman9008ca62009-04-27 18:41:29 +00003097bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003098 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 N->getMask(M);
3100 return ::isPSHUFLWMask(M, N->getValueType(0));
3101}
3102
Nate Begemana09008b2009-10-19 02:17:23 +00003103/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3104/// is suitable for input to PALIGNR.
3105static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3106 bool hasSSSE3) {
3107 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003108
Nate Begemana09008b2009-10-19 02:17:23 +00003109 // Do not handle v2i64 / v2f64 shuffles with palignr.
3110 if (e < 4 || !hasSSSE3)
3111 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003112
Nate Begemana09008b2009-10-19 02:17:23 +00003113 for (i = 0; i != e; ++i)
3114 if (Mask[i] >= 0)
3115 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003116
Nate Begemana09008b2009-10-19 02:17:23 +00003117 // All undef, not a palignr.
3118 if (i == e)
3119 return false;
3120
3121 // Determine if it's ok to perform a palignr with only the LHS, since we
3122 // don't have access to the actual shuffle elements to see if RHS is undef.
3123 bool Unary = Mask[i] < (int)e;
3124 bool NeedsUnary = false;
3125
3126 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003127
Nate Begemana09008b2009-10-19 02:17:23 +00003128 // Check the rest of the elements to see if they are consecutive.
3129 for (++i; i != e; ++i) {
3130 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003131 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003132 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003133
Nate Begemana09008b2009-10-19 02:17:23 +00003134 Unary = Unary && (m < (int)e);
3135 NeedsUnary = NeedsUnary || (m < s);
3136
3137 if (NeedsUnary && !Unary)
3138 return false;
3139 if (Unary && m != ((s+i) & (e-1)))
3140 return false;
3141 if (!Unary && m != (s+i))
3142 return false;
3143 }
3144 return true;
3145}
3146
3147bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3148 SmallVector<int, 8> M;
3149 N->getMask(M);
3150 return ::isPALIGNRMask(M, N->getValueType(0), true);
3151}
3152
Evan Cheng14aed5e2006-03-24 01:18:28 +00003153/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3154/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003155static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003156 int NumElems = VT.getVectorNumElements();
3157 if (NumElems != 2 && NumElems != 4)
3158 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003159
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 int Half = NumElems / 2;
3161 for (int i = 0; i < Half; ++i)
3162 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003163 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003164 for (int i = Half; i < NumElems; ++i)
3165 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003166 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003167
Evan Cheng14aed5e2006-03-24 01:18:28 +00003168 return true;
3169}
3170
Nate Begeman9008ca62009-04-27 18:41:29 +00003171bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3172 SmallVector<int, 8> M;
3173 N->getMask(M);
3174 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003175}
3176
Evan Cheng213d2cf2007-05-17 18:45:50 +00003177/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003178/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3179/// half elements to come from vector 1 (which would equal the dest.) and
3180/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003181static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003183
3184 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 int Half = NumElems / 2;
3188 for (int i = 0; i < Half; ++i)
3189 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003190 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 for (int i = Half; i < NumElems; ++i)
3192 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003193 return false;
3194 return true;
3195}
3196
Nate Begeman9008ca62009-04-27 18:41:29 +00003197static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3198 SmallVector<int, 8> M;
3199 N->getMask(M);
3200 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003201}
3202
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003203/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3204/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003205bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3206 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003207 return false;
3208
Evan Cheng2064a2b2006-03-28 06:50:32 +00003209 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3211 isUndefOrEqual(N->getMaskElt(1), 7) &&
3212 isUndefOrEqual(N->getMaskElt(2), 2) &&
3213 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003214}
3215
Nate Begeman0b10b912009-11-07 23:17:15 +00003216/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3217/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3218/// <2, 3, 2, 3>
3219bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3220 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003221
Nate Begeman0b10b912009-11-07 23:17:15 +00003222 if (NumElems != 4)
3223 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003224
Nate Begeman0b10b912009-11-07 23:17:15 +00003225 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3226 isUndefOrEqual(N->getMaskElt(1), 3) &&
3227 isUndefOrEqual(N->getMaskElt(2), 2) &&
3228 isUndefOrEqual(N->getMaskElt(3), 3);
3229}
3230
Evan Cheng5ced1d82006-04-06 23:23:56 +00003231/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3232/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003233bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3234 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003235
Evan Cheng5ced1d82006-04-06 23:23:56 +00003236 if (NumElems != 2 && NumElems != 4)
3237 return false;
3238
Evan Chengc5cdff22006-04-07 21:53:05 +00003239 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003240 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003241 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003242
Evan Chengc5cdff22006-04-07 21:53:05 +00003243 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003244 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003245 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003246
3247 return true;
3248}
3249
Nate Begeman0b10b912009-11-07 23:17:15 +00003250/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3251/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3252bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003254
David Greenea20244d2011-03-02 17:23:43 +00003255 if ((NumElems != 2 && NumElems != 4)
3256 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003257 return false;
3258
Evan Chengc5cdff22006-04-07 21:53:05 +00003259 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003261 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003262
Nate Begeman9008ca62009-04-27 18:41:29 +00003263 for (unsigned i = 0; i < NumElems/2; ++i)
3264 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003265 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003266
3267 return true;
3268}
3269
Evan Cheng0038e592006-03-28 00:39:58 +00003270/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3271/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003272static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003273 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003275 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003276 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003277
David Greenea20244d2011-03-02 17:23:43 +00003278 // Handle vector lengths > 128 bits. Define a "section" as a set of
3279 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3280 // sections.
3281 unsigned NumSections = VT.getSizeInBits() / 128;
3282 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3283 unsigned NumSectionElts = NumElts / NumSections;
3284
3285 unsigned Start = 0;
3286 unsigned End = NumSectionElts;
3287 for (unsigned s = 0; s < NumSections; ++s) {
3288 for (unsigned i = Start, j = s * NumSectionElts;
3289 i != End;
3290 i += 2, ++j) {
3291 int BitI = Mask[i];
3292 int BitI1 = Mask[i+1];
3293 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003294 return false;
David Greenea20244d2011-03-02 17:23:43 +00003295 if (V2IsSplat) {
3296 if (!isUndefOrEqual(BitI1, NumElts))
3297 return false;
3298 } else {
3299 if (!isUndefOrEqual(BitI1, j + NumElts))
3300 return false;
3301 }
Evan Cheng39623da2006-04-20 08:58:49 +00003302 }
David Greenea20244d2011-03-02 17:23:43 +00003303 // Process the next 128 bits.
3304 Start += NumSectionElts;
3305 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003306 }
David Greenea20244d2011-03-02 17:23:43 +00003307
Evan Cheng0038e592006-03-28 00:39:58 +00003308 return true;
3309}
3310
Nate Begeman9008ca62009-04-27 18:41:29 +00003311bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3312 SmallVector<int, 8> M;
3313 N->getMask(M);
3314 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003315}
3316
Evan Cheng4fcb9222006-03-28 02:43:26 +00003317/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3318/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003319static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003320 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003321 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003322 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003323 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003324
Nate Begeman9008ca62009-04-27 18:41:29 +00003325 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3326 int BitI = Mask[i];
3327 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003328 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003329 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003330 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003331 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003332 return false;
3333 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003334 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003335 return false;
3336 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003337 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003338 return true;
3339}
3340
Nate Begeman9008ca62009-04-27 18:41:29 +00003341bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3342 SmallVector<int, 8> M;
3343 N->getMask(M);
3344 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003345}
3346
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003347/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3348/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3349/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003350static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003352 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003353 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003354
David Greenea20244d2011-03-02 17:23:43 +00003355 // Handle vector lengths > 128 bits. Define a "section" as a set of
3356 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3357 // sections.
3358 unsigned NumSections = VT.getSizeInBits() / 128;
3359 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3360 unsigned NumSectionElts = NumElems / NumSections;
3361
3362 for (unsigned s = 0; s < NumSections; ++s) {
3363 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3364 i != NumSectionElts * (s + 1);
3365 i += 2, ++j) {
3366 int BitI = Mask[i];
3367 int BitI1 = Mask[i+1];
3368
3369 if (!isUndefOrEqual(BitI, j))
3370 return false;
3371 if (!isUndefOrEqual(BitI1, j))
3372 return false;
3373 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003374 }
David Greenea20244d2011-03-02 17:23:43 +00003375
Rafael Espindola15684b22009-04-24 12:40:33 +00003376 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003377}
3378
Nate Begeman9008ca62009-04-27 18:41:29 +00003379bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3380 SmallVector<int, 8> M;
3381 N->getMask(M);
3382 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3383}
3384
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003385/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3386/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3387/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003388static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003390 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3391 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003392
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3394 int BitI = Mask[i];
3395 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003396 if (!isUndefOrEqual(BitI, j))
3397 return false;
3398 if (!isUndefOrEqual(BitI1, j))
3399 return false;
3400 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003401 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003402}
3403
Nate Begeman9008ca62009-04-27 18:41:29 +00003404bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3405 SmallVector<int, 8> M;
3406 N->getMask(M);
3407 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3408}
3409
Evan Cheng017dcc62006-04-21 01:05:10 +00003410/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3411/// specifies a shuffle of elements that is suitable for input to MOVSS,
3412/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003413static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003414 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003415 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003416
3417 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003418
Nate Begeman9008ca62009-04-27 18:41:29 +00003419 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003420 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003421
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 for (int i = 1; i < NumElts; ++i)
3423 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003424 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003425
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003426 return true;
3427}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003428
Nate Begeman9008ca62009-04-27 18:41:29 +00003429bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3430 SmallVector<int, 8> M;
3431 N->getMask(M);
3432 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003433}
3434
Evan Cheng017dcc62006-04-21 01:05:10 +00003435/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3436/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003437/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003438static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 bool V2IsSplat = false, bool V2IsUndef = false) {
3440 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003441 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003442 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003443
Nate Begeman9008ca62009-04-27 18:41:29 +00003444 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003445 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003446
Nate Begeman9008ca62009-04-27 18:41:29 +00003447 for (int i = 1; i < NumOps; ++i)
3448 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3449 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3450 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003451 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003452
Evan Cheng39623da2006-04-20 08:58:49 +00003453 return true;
3454}
3455
Nate Begeman9008ca62009-04-27 18:41:29 +00003456static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003457 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 SmallVector<int, 8> M;
3459 N->getMask(M);
3460 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003461}
3462
Evan Chengd9539472006-04-14 21:59:03 +00003463/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3464/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003465bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3466 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003467 return false;
3468
3469 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003470 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003471 int Elt = N->getMaskElt(i);
3472 if (Elt >= 0 && Elt != 1)
3473 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003474 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003475
3476 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003477 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 int Elt = N->getMaskElt(i);
3479 if (Elt >= 0 && Elt != 3)
3480 return false;
3481 if (Elt == 3)
3482 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003483 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003484 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003486 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003487}
3488
3489/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3490/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003491bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3492 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003493 return false;
3494
3495 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003496 for (unsigned i = 0; i < 2; ++i)
3497 if (N->getMaskElt(i) > 0)
3498 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003499
3500 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003501 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 int Elt = N->getMaskElt(i);
3503 if (Elt >= 0 && Elt != 2)
3504 return false;
3505 if (Elt == 2)
3506 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003507 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003509 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003510}
3511
Evan Cheng0b457f02008-09-25 20:50:48 +00003512/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3513/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003514bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3515 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003516
Nate Begeman9008ca62009-04-27 18:41:29 +00003517 for (int i = 0; i < e; ++i)
3518 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003519 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003520 for (int i = 0; i < e; ++i)
3521 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003522 return false;
3523 return true;
3524}
3525
David Greenec38a03e2011-02-03 15:50:00 +00003526/// isVEXTRACTF128Index - Return true if the specified
3527/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3528/// suitable for input to VEXTRACTF128.
3529bool X86::isVEXTRACTF128Index(SDNode *N) {
3530 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3531 return false;
3532
3533 // The index should be aligned on a 128-bit boundary.
3534 uint64_t Index =
3535 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3536
3537 unsigned VL = N->getValueType(0).getVectorNumElements();
3538 unsigned VBits = N->getValueType(0).getSizeInBits();
3539 unsigned ElSize = VBits / VL;
3540 bool Result = (Index * ElSize) % 128 == 0;
3541
3542 return Result;
3543}
3544
David Greeneccacdc12011-02-04 16:08:29 +00003545/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3546/// operand specifies a subvector insert that is suitable for input to
3547/// VINSERTF128.
3548bool X86::isVINSERTF128Index(SDNode *N) {
3549 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3550 return false;
3551
3552 // The index should be aligned on a 128-bit boundary.
3553 uint64_t Index =
3554 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3555
3556 unsigned VL = N->getValueType(0).getVectorNumElements();
3557 unsigned VBits = N->getValueType(0).getSizeInBits();
3558 unsigned ElSize = VBits / VL;
3559 bool Result = (Index * ElSize) % 128 == 0;
3560
3561 return Result;
3562}
3563
Evan Cheng63d33002006-03-22 08:01:21 +00003564/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003565/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003566unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003567 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3568 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3569
Evan Chengb9df0ca2006-03-22 02:53:00 +00003570 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3571 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003572 for (int i = 0; i < NumOperands; ++i) {
3573 int Val = SVOp->getMaskElt(NumOperands-i-1);
3574 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003575 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003576 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003577 if (i != NumOperands - 1)
3578 Mask <<= Shift;
3579 }
Evan Cheng63d33002006-03-22 08:01:21 +00003580 return Mask;
3581}
3582
Evan Cheng506d3df2006-03-29 23:07:14 +00003583/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003584/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003585unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003586 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003587 unsigned Mask = 0;
3588 // 8 nodes, but we only care about the last 4.
3589 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003590 int Val = SVOp->getMaskElt(i);
3591 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003592 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003593 if (i != 4)
3594 Mask <<= 2;
3595 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003596 return Mask;
3597}
3598
3599/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003600/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003601unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003602 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003603 unsigned Mask = 0;
3604 // 8 nodes, but we only care about the first 4.
3605 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003606 int Val = SVOp->getMaskElt(i);
3607 if (Val >= 0)
3608 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003609 if (i != 0)
3610 Mask <<= 2;
3611 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003612 return Mask;
3613}
3614
Nate Begemana09008b2009-10-19 02:17:23 +00003615/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3616/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3617unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3618 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3619 EVT VVT = N->getValueType(0);
3620 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3621 int Val = 0;
3622
3623 unsigned i, e;
3624 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3625 Val = SVOp->getMaskElt(i);
3626 if (Val >= 0)
3627 break;
3628 }
3629 return (Val - i) * EltSize;
3630}
3631
David Greenec38a03e2011-02-03 15:50:00 +00003632/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3633/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3634/// instructions.
3635unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3636 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3637 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3638
3639 uint64_t Index =
3640 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3641
3642 EVT VecVT = N->getOperand(0).getValueType();
3643 EVT ElVT = VecVT.getVectorElementType();
3644
3645 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3646
3647 return Index / NumElemsPerChunk;
3648}
3649
David Greeneccacdc12011-02-04 16:08:29 +00003650/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3651/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3652/// instructions.
3653unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3654 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3655 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3656
3657 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003658 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003659
3660 EVT VecVT = N->getValueType(0);
3661 EVT ElVT = VecVT.getVectorElementType();
3662
3663 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3664
3665 return Index / NumElemsPerChunk;
3666}
3667
Evan Cheng37b73872009-07-30 08:33:02 +00003668/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3669/// constant +0.0.
3670bool X86::isZeroNode(SDValue Elt) {
3671 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003672 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003673 (isa<ConstantFPSDNode>(Elt) &&
3674 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3675}
3676
Nate Begeman9008ca62009-04-27 18:41:29 +00003677/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3678/// their permute mask.
3679static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3680 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003681 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003682 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003683 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003684
Nate Begeman5a5ca152009-04-29 05:20:52 +00003685 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003686 int idx = SVOp->getMaskElt(i);
3687 if (idx < 0)
3688 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003689 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003690 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003691 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003693 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003694 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3695 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003696}
3697
Evan Cheng779ccea2007-12-07 21:30:01 +00003698/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3699/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003700static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003701 unsigned NumElems = VT.getVectorNumElements();
3702 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003703 int idx = Mask[i];
3704 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003705 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003706 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003708 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003709 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003710 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003711}
3712
Evan Cheng533a0aa2006-04-19 20:35:22 +00003713/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3714/// match movhlps. The lower half elements should come from upper half of
3715/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003716/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003717static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3718 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003719 return false;
3720 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003721 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003722 return false;
3723 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003724 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003725 return false;
3726 return true;
3727}
3728
Evan Cheng5ced1d82006-04-06 23:23:56 +00003729/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003730/// is promoted to a vector. It also returns the LoadSDNode by reference if
3731/// required.
3732static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003733 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3734 return false;
3735 N = N->getOperand(0).getNode();
3736 if (!ISD::isNON_EXTLoad(N))
3737 return false;
3738 if (LD)
3739 *LD = cast<LoadSDNode>(N);
3740 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003741}
3742
Evan Cheng533a0aa2006-04-19 20:35:22 +00003743/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3744/// match movlp{s|d}. The lower half elements should come from lower half of
3745/// V1 (and in order), and the upper half elements should come from the upper
3746/// half of V2 (and in order). And since V1 will become the source of the
3747/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003748static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3749 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003750 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003751 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003752 // Is V2 is a vector load, don't do this transformation. We will try to use
3753 // load folding shufps op.
3754 if (ISD::isNON_EXTLoad(V2))
3755 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003756
Nate Begeman5a5ca152009-04-29 05:20:52 +00003757 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003758
Evan Cheng533a0aa2006-04-19 20:35:22 +00003759 if (NumElems != 2 && NumElems != 4)
3760 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003761 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003762 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003763 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003764 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003765 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003766 return false;
3767 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003768}
3769
Evan Cheng39623da2006-04-20 08:58:49 +00003770/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3771/// all the same.
3772static bool isSplatVector(SDNode *N) {
3773 if (N->getOpcode() != ISD::BUILD_VECTOR)
3774 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003775
Dan Gohman475871a2008-07-27 21:46:04 +00003776 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003777 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3778 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003779 return false;
3780 return true;
3781}
3782
Evan Cheng213d2cf2007-05-17 18:45:50 +00003783/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003784/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003785/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003786static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003787 SDValue V1 = N->getOperand(0);
3788 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003789 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3790 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003792 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003793 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003794 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3795 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003796 if (Opc != ISD::BUILD_VECTOR ||
3797 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003798 return false;
3799 } else if (Idx >= 0) {
3800 unsigned Opc = V1.getOpcode();
3801 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3802 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003803 if (Opc != ISD::BUILD_VECTOR ||
3804 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003805 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003806 }
3807 }
3808 return true;
3809}
3810
3811/// getZeroVector - Returns a vector of specified type with all zero elements.
3812///
Owen Andersone50ed302009-08-10 22:56:29 +00003813static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003814 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003815 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003816
Dale Johannesen0488fb62010-09-30 23:57:10 +00003817 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003818 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003819 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003820 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003821 if (HasSSE2) { // SSE2
3822 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3823 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3824 } else { // SSE1
3825 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3826 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3827 }
3828 } else if (VT.getSizeInBits() == 256) { // AVX
3829 // 256-bit logic and arithmetic instructions in AVX are
3830 // all floating-point, no support for integer ops. Default
3831 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003832 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003833 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3834 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003835 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003836 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003837}
3838
Chris Lattner8a594482007-11-25 00:24:49 +00003839/// getOnesVector - Returns a vector of specified type with all bits set.
3840///
Owen Andersone50ed302009-08-10 22:56:29 +00003841static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003842 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003843
Chris Lattner8a594482007-11-25 00:24:49 +00003844 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3845 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003846 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003847 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003848 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003849 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003850}
3851
3852
Evan Cheng39623da2006-04-20 08:58:49 +00003853/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3854/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003855static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003856 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003857 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003858
Evan Cheng39623da2006-04-20 08:58:49 +00003859 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003860 SmallVector<int, 8> MaskVec;
3861 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003862
Nate Begeman5a5ca152009-04-29 05:20:52 +00003863 for (unsigned i = 0; i != NumElems; ++i) {
3864 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003865 MaskVec[i] = NumElems;
3866 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003867 }
Evan Cheng39623da2006-04-20 08:58:49 +00003868 }
Evan Cheng39623da2006-04-20 08:58:49 +00003869 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3871 SVOp->getOperand(1), &MaskVec[0]);
3872 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003873}
3874
Evan Cheng017dcc62006-04-21 01:05:10 +00003875/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3876/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003877static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003878 SDValue V2) {
3879 unsigned NumElems = VT.getVectorNumElements();
3880 SmallVector<int, 8> Mask;
3881 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003882 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003883 Mask.push_back(i);
3884 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003885}
3886
Nate Begeman9008ca62009-04-27 18:41:29 +00003887/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003888static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 SDValue V2) {
3890 unsigned NumElems = VT.getVectorNumElements();
3891 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003892 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003893 Mask.push_back(i);
3894 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003895 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003896 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003897}
3898
Nate Begeman9008ca62009-04-27 18:41:29 +00003899/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003900static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 SDValue V2) {
3902 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003903 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003905 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003906 Mask.push_back(i + Half);
3907 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003908 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003910}
3911
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003912/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3913static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003915 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003916 DebugLoc dl = SV->getDebugLoc();
3917 SDValue V1 = SV->getOperand(0);
3918 int NumElems = VT.getVectorNumElements();
3919 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003920
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 // unpack elements to the correct location
3922 while (NumElems > 4) {
3923 if (EltNo < NumElems/2) {
3924 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3925 } else {
3926 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3927 EltNo -= NumElems/2;
3928 }
3929 NumElems >>= 1;
3930 }
Eric Christopherfd179292009-08-27 18:07:15 +00003931
Nate Begeman9008ca62009-04-27 18:41:29 +00003932 // Perform the splat.
3933 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003934 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003935 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003936 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003937}
3938
Evan Chengba05f722006-04-21 23:03:30 +00003939/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003940/// vector of zero or undef vector. This produces a shuffle where the low
3941/// element of V2 is swizzled into the zero/undef vector, landing at element
3942/// 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 +00003943static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003944 bool isZero, bool HasSSE2,
3945 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003946 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003947 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3949 unsigned NumElems = VT.getVectorNumElements();
3950 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003951 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 // If this is the insertion idx, put the low elt of V2 here.
3953 MaskVec.push_back(i == Idx ? NumElems : i);
3954 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003955}
3956
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003957/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3958/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003959static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3960 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003961 if (Depth == 6)
3962 return SDValue(); // Limit search depth.
3963
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003964 SDValue V = SDValue(N, 0);
3965 EVT VT = V.getValueType();
3966 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003967
3968 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3969 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3970 Index = SV->getMaskElt(Index);
3971
3972 if (Index < 0)
3973 return DAG.getUNDEF(VT.getVectorElementType());
3974
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003975 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003976 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003977 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003978 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003979
3980 // Recurse into target specific vector shuffles to find scalars.
3981 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003982 int NumElems = VT.getVectorNumElements();
3983 SmallVector<unsigned, 16> ShuffleMask;
3984 SDValue ImmN;
3985
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003986 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003987 case X86ISD::SHUFPS:
3988 case X86ISD::SHUFPD:
3989 ImmN = N->getOperand(N->getNumOperands()-1);
3990 DecodeSHUFPSMask(NumElems,
3991 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3992 ShuffleMask);
3993 break;
3994 case X86ISD::PUNPCKHBW:
3995 case X86ISD::PUNPCKHWD:
3996 case X86ISD::PUNPCKHDQ:
3997 case X86ISD::PUNPCKHQDQ:
3998 DecodePUNPCKHMask(NumElems, ShuffleMask);
3999 break;
4000 case X86ISD::UNPCKHPS:
4001 case X86ISD::UNPCKHPD:
4002 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4003 break;
4004 case X86ISD::PUNPCKLBW:
4005 case X86ISD::PUNPCKLWD:
4006 case X86ISD::PUNPCKLDQ:
4007 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004008 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004009 break;
4010 case X86ISD::UNPCKLPS:
4011 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004012 case X86ISD::VUNPCKLPS:
4013 case X86ISD::VUNPCKLPD:
4014 case X86ISD::VUNPCKLPSY:
4015 case X86ISD::VUNPCKLPDY:
4016 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004017 break;
4018 case X86ISD::MOVHLPS:
4019 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4020 break;
4021 case X86ISD::MOVLHPS:
4022 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4023 break;
4024 case X86ISD::PSHUFD:
4025 ImmN = N->getOperand(N->getNumOperands()-1);
4026 DecodePSHUFMask(NumElems,
4027 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4028 ShuffleMask);
4029 break;
4030 case X86ISD::PSHUFHW:
4031 ImmN = N->getOperand(N->getNumOperands()-1);
4032 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4033 ShuffleMask);
4034 break;
4035 case X86ISD::PSHUFLW:
4036 ImmN = N->getOperand(N->getNumOperands()-1);
4037 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4038 ShuffleMask);
4039 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004040 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004041 case X86ISD::MOVSD: {
4042 // The index 0 always comes from the first element of the second source,
4043 // this is why MOVSS and MOVSD are used in the first place. The other
4044 // elements come from the other positions of the first source vector.
4045 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004046 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4047 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004048 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004049 default:
4050 assert("not implemented for target shuffle node");
4051 return SDValue();
4052 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004053
4054 Index = ShuffleMask[Index];
4055 if (Index < 0)
4056 return DAG.getUNDEF(VT.getVectorElementType());
4057
4058 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4059 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4060 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004061 }
4062
4063 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004064 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004065 V = V.getOperand(0);
4066 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004067 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004068
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004069 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004070 return SDValue();
4071 }
4072
4073 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4074 return (Index == 0) ? V.getOperand(0)
4075 : DAG.getUNDEF(VT.getVectorElementType());
4076
4077 if (V.getOpcode() == ISD::BUILD_VECTOR)
4078 return V.getOperand(Index);
4079
4080 return SDValue();
4081}
4082
4083/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4084/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004085/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004086static
4087unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4088 bool ZerosFromLeft, SelectionDAG &DAG) {
4089 int i = 0;
4090
4091 while (i < NumElems) {
4092 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004093 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004094 if (!(Elt.getNode() &&
4095 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4096 break;
4097 ++i;
4098 }
4099
4100 return i;
4101}
4102
4103/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4104/// MaskE correspond consecutively to elements from one of the vector operands,
4105/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4106static
4107bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4108 int OpIdx, int NumElems, unsigned &OpNum) {
4109 bool SeenV1 = false;
4110 bool SeenV2 = false;
4111
4112 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4113 int Idx = SVOp->getMaskElt(i);
4114 // Ignore undef indicies
4115 if (Idx < 0)
4116 continue;
4117
4118 if (Idx < NumElems)
4119 SeenV1 = true;
4120 else
4121 SeenV2 = true;
4122
4123 // Only accept consecutive elements from the same vector
4124 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4125 return false;
4126 }
4127
4128 OpNum = SeenV1 ? 0 : 1;
4129 return true;
4130}
4131
4132/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4133/// logical left shift of a vector.
4134static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4135 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4136 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4137 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4138 false /* check zeros from right */, DAG);
4139 unsigned OpSrc;
4140
4141 if (!NumZeros)
4142 return false;
4143
4144 // Considering the elements in the mask that are not consecutive zeros,
4145 // check if they consecutively come from only one of the source vectors.
4146 //
4147 // V1 = {X, A, B, C} 0
4148 // \ \ \ /
4149 // vector_shuffle V1, V2 <1, 2, 3, X>
4150 //
4151 if (!isShuffleMaskConsecutive(SVOp,
4152 0, // Mask Start Index
4153 NumElems-NumZeros-1, // Mask End Index
4154 NumZeros, // Where to start looking in the src vector
4155 NumElems, // Number of elements in vector
4156 OpSrc)) // Which source operand ?
4157 return false;
4158
4159 isLeft = false;
4160 ShAmt = NumZeros;
4161 ShVal = SVOp->getOperand(OpSrc);
4162 return true;
4163}
4164
4165/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4166/// logical left shift of a vector.
4167static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4168 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4169 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4170 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4171 true /* check zeros from left */, DAG);
4172 unsigned OpSrc;
4173
4174 if (!NumZeros)
4175 return false;
4176
4177 // Considering the elements in the mask that are not consecutive zeros,
4178 // check if they consecutively come from only one of the source vectors.
4179 //
4180 // 0 { A, B, X, X } = V2
4181 // / \ / /
4182 // vector_shuffle V1, V2 <X, X, 4, 5>
4183 //
4184 if (!isShuffleMaskConsecutive(SVOp,
4185 NumZeros, // Mask Start Index
4186 NumElems-1, // Mask End Index
4187 0, // Where to start looking in the src vector
4188 NumElems, // Number of elements in vector
4189 OpSrc)) // Which source operand ?
4190 return false;
4191
4192 isLeft = true;
4193 ShAmt = NumZeros;
4194 ShVal = SVOp->getOperand(OpSrc);
4195 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004196}
4197
4198/// isVectorShift - Returns true if the shuffle can be implemented as a
4199/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004200static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004201 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004202 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4203 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4204 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004205
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004206 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004207}
4208
Evan Chengc78d3b42006-04-24 18:01:45 +00004209/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4210///
Dan Gohman475871a2008-07-27 21:46:04 +00004211static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004212 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004213 SelectionDAG &DAG,
4214 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004215 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004216 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004217
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004218 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004219 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004220 bool First = true;
4221 for (unsigned i = 0; i < 16; ++i) {
4222 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4223 if (ThisIsNonZero && First) {
4224 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004226 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004228 First = false;
4229 }
4230
4231 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004232 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004233 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4234 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004235 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004237 }
4238 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4240 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4241 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004242 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004243 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004244 } else
4245 ThisElt = LastElt;
4246
Gabor Greifba36cb52008-08-28 21:40:38 +00004247 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004248 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004249 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004250 }
4251 }
4252
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004253 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004254}
4255
Bill Wendlinga348c562007-03-22 18:42:45 +00004256/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004257///
Dan Gohman475871a2008-07-27 21:46:04 +00004258static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004259 unsigned NumNonZero, unsigned NumZero,
4260 SelectionDAG &DAG,
4261 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004262 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004263 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004264
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004265 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004266 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004267 bool First = true;
4268 for (unsigned i = 0; i < 8; ++i) {
4269 bool isNonZero = (NonZeros & (1 << i)) != 0;
4270 if (isNonZero) {
4271 if (First) {
4272 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004273 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004274 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004275 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004276 First = false;
4277 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004278 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004280 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004281 }
4282 }
4283
4284 return V;
4285}
4286
Evan Chengf26ffe92008-05-29 08:22:04 +00004287/// getVShift - Return a vector logical shift node.
4288///
Owen Andersone50ed302009-08-10 22:56:29 +00004289static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004290 unsigned NumBits, SelectionDAG &DAG,
4291 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004292 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004293 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004294 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4295 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004296 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004297 DAG.getConstant(NumBits,
4298 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004299}
4300
Dan Gohman475871a2008-07-27 21:46:04 +00004301SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004302X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004303 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004304
Evan Chengc3630942009-12-09 21:00:30 +00004305 // Check if the scalar load can be widened into a vector load. And if
4306 // the address is "base + cst" see if the cst can be "absorbed" into
4307 // the shuffle mask.
4308 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4309 SDValue Ptr = LD->getBasePtr();
4310 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4311 return SDValue();
4312 EVT PVT = LD->getValueType(0);
4313 if (PVT != MVT::i32 && PVT != MVT::f32)
4314 return SDValue();
4315
4316 int FI = -1;
4317 int64_t Offset = 0;
4318 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4319 FI = FINode->getIndex();
4320 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004321 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004322 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4323 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4324 Offset = Ptr.getConstantOperandVal(1);
4325 Ptr = Ptr.getOperand(0);
4326 } else {
4327 return SDValue();
4328 }
4329
4330 SDValue Chain = LD->getChain();
4331 // Make sure the stack object alignment is at least 16.
4332 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4333 if (DAG.InferPtrAlignment(Ptr) < 16) {
4334 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004335 // Can't change the alignment. FIXME: It's possible to compute
4336 // the exact stack offset and reference FI + adjust offset instead.
4337 // If someone *really* cares about this. That's the way to implement it.
4338 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004339 } else {
4340 MFI->setObjectAlignment(FI, 16);
4341 }
4342 }
4343
4344 // (Offset % 16) must be multiple of 4. Then address is then
4345 // Ptr + (Offset & ~15).
4346 if (Offset < 0)
4347 return SDValue();
4348 if ((Offset % 16) & 3)
4349 return SDValue();
4350 int64_t StartOffset = Offset & ~15;
4351 if (StartOffset)
4352 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4353 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4354
4355 int EltNo = (Offset - StartOffset) >> 2;
4356 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4357 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004358 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4359 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004360 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004361 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004362 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4363 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004364 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004365 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004366 }
4367
4368 return SDValue();
4369}
4370
Michael J. Spencerec38de22010-10-10 22:04:20 +00004371/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4372/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004373/// load which has the same value as a build_vector whose operands are 'elts'.
4374///
4375/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004376///
Nate Begeman1449f292010-03-24 22:19:06 +00004377/// FIXME: we'd also like to handle the case where the last elements are zero
4378/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4379/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004380static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004381 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004382 EVT EltVT = VT.getVectorElementType();
4383 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004384
Nate Begemanfdea31a2010-03-24 20:49:50 +00004385 LoadSDNode *LDBase = NULL;
4386 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004387
Nate Begeman1449f292010-03-24 22:19:06 +00004388 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004389 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004390 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004391 for (unsigned i = 0; i < NumElems; ++i) {
4392 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004393
Nate Begemanfdea31a2010-03-24 20:49:50 +00004394 if (!Elt.getNode() ||
4395 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4396 return SDValue();
4397 if (!LDBase) {
4398 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4399 return SDValue();
4400 LDBase = cast<LoadSDNode>(Elt.getNode());
4401 LastLoadedElt = i;
4402 continue;
4403 }
4404 if (Elt.getOpcode() == ISD::UNDEF)
4405 continue;
4406
4407 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4408 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4409 return SDValue();
4410 LastLoadedElt = i;
4411 }
Nate Begeman1449f292010-03-24 22:19:06 +00004412
4413 // If we have found an entire vector of loads and undefs, then return a large
4414 // load of the entire vector width starting at the base pointer. If we found
4415 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004416 if (LastLoadedElt == NumElems - 1) {
4417 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004418 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004419 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004420 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004421 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004422 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004423 LDBase->isVolatile(), LDBase->isNonTemporal(),
4424 LDBase->getAlignment());
4425 } else if (NumElems == 4 && LastLoadedElt == 1) {
4426 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4427 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004428 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4429 Ops, 2, MVT::i32,
4430 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004431 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004432 }
4433 return SDValue();
4434}
4435
Evan Chengc3630942009-12-09 21:00:30 +00004436SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004437X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004438 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004439
David Greenef125a292011-02-08 19:04:41 +00004440 EVT VT = Op.getValueType();
4441 EVT ExtVT = VT.getVectorElementType();
4442
4443 unsigned NumElems = Op.getNumOperands();
4444
4445 // For AVX-length vectors, build the individual 128-bit pieces and
4446 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004447 if (VT.getSizeInBits() > 256 &&
4448 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004449 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4450 SmallVector<SDValue, 8> V;
4451 V.resize(NumElems);
4452 for (unsigned i = 0; i < NumElems; ++i) {
4453 V[i] = Op.getOperand(i);
4454 }
Owen Anderson95771af2011-02-25 21:41:48 +00004455
David Greenef125a292011-02-08 19:04:41 +00004456 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4457
4458 // Build the lower subvector.
4459 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4460 // Build the upper subvector.
4461 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4462 NumElems/2);
4463
4464 return ConcatVectors(Lower, Upper, DAG);
4465 }
4466
Chris Lattner6e80e442010-08-28 17:15:43 +00004467 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4468 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004469 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4470 // is present, so AllOnes is ignored.
4471 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4472 (Op.getValueType().getSizeInBits() != 256 &&
4473 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004474 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004475 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4476 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004477 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004478 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004479
Gabor Greifba36cb52008-08-28 21:40:38 +00004480 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004481 return getOnesVector(Op.getValueType(), DAG, dl);
4482 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004483 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004484
Owen Andersone50ed302009-08-10 22:56:29 +00004485 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004486
Evan Cheng0db9fe62006-04-25 20:13:52 +00004487 unsigned NumZero = 0;
4488 unsigned NumNonZero = 0;
4489 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004490 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004491 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004492 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004493 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004494 if (Elt.getOpcode() == ISD::UNDEF)
4495 continue;
4496 Values.insert(Elt);
4497 if (Elt.getOpcode() != ISD::Constant &&
4498 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004499 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004500 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004501 NumZero++;
4502 else {
4503 NonZeros |= (1 << i);
4504 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004505 }
4506 }
4507
Chris Lattner97a2a562010-08-26 05:24:29 +00004508 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4509 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004510 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004511
Chris Lattner67f453a2008-03-09 05:42:06 +00004512 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004513 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004514 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004515 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004516
Chris Lattner62098042008-03-09 01:05:04 +00004517 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4518 // the value are obviously zero, truncate the value to i32 and do the
4519 // insertion that way. Only do this if the value is non-constant or if the
4520 // value is a constant being inserted into element 0. It is cheaper to do
4521 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004522 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004523 (!IsAllConstants || Idx == 0)) {
4524 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004525 // Handle SSE only.
4526 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4527 EVT VecVT = MVT::v4i32;
4528 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004529
Chris Lattner62098042008-03-09 01:05:04 +00004530 // Truncate the value (which may itself be a constant) to i32, and
4531 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004532 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004533 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004534 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4535 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004536
Chris Lattner62098042008-03-09 01:05:04 +00004537 // Now we have our 32-bit value zero extended in the low element of
4538 // a vector. If Idx != 0, swizzle it into place.
4539 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004540 SmallVector<int, 4> Mask;
4541 Mask.push_back(Idx);
4542 for (unsigned i = 1; i != VecElts; ++i)
4543 Mask.push_back(i);
4544 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004545 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004547 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004548 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004549 }
4550 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004551
Chris Lattner19f79692008-03-08 22:59:52 +00004552 // If we have a constant or non-constant insertion into the low element of
4553 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4554 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004555 // depending on what the source datatype is.
4556 if (Idx == 0) {
4557 if (NumZero == 0) {
4558 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004559 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4560 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004561 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4562 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4563 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4564 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004565 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4566 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004567 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4568 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004569 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4570 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4571 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004572 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004573 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004574 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004575
4576 // Is it a vector logical left shift?
4577 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004578 X86::isZeroNode(Op.getOperand(0)) &&
4579 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004580 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004581 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004582 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004583 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004584 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004585 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004586
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004587 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004588 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004589
Chris Lattner19f79692008-03-08 22:59:52 +00004590 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4591 // is a non-constant being inserted into an element other than the low one,
4592 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4593 // movd/movss) to move this into the low element, then shuffle it into
4594 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004596 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004597
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004599 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4600 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004601 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004602 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004603 MaskVec.push_back(i == Idx ? 0 : 1);
4604 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 }
4606 }
4607
Chris Lattner67f453a2008-03-09 05:42:06 +00004608 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004609 if (Values.size() == 1) {
4610 if (EVTBits == 32) {
4611 // Instead of a shuffle like this:
4612 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4613 // Check if it's possible to issue this instead.
4614 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4615 unsigned Idx = CountTrailingZeros_32(NonZeros);
4616 SDValue Item = Op.getOperand(Idx);
4617 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4618 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4619 }
Dan Gohman475871a2008-07-27 21:46:04 +00004620 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004621 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004622
Dan Gohmana3941172007-07-24 22:55:08 +00004623 // A vector full of immediates; various special cases are already
4624 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004625 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004626 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004627
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004628 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004629 if (EVTBits == 64) {
4630 if (NumNonZero == 1) {
4631 // One half is zero or undef.
4632 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004633 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004634 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004635 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4636 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004637 }
Dan Gohman475871a2008-07-27 21:46:04 +00004638 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004639 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004640
4641 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004642 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004643 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004644 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004645 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004646 }
4647
Bill Wendling826f36f2007-03-28 00:57:11 +00004648 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004649 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004650 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004651 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004652 }
4653
4654 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004655 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004656 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004657 if (NumElems == 4 && NumZero > 0) {
4658 for (unsigned i = 0; i < 4; ++i) {
4659 bool isZero = !(NonZeros & (1 << i));
4660 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004661 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004662 else
Dale Johannesenace16102009-02-03 19:33:06 +00004663 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004664 }
4665
4666 for (unsigned i = 0; i < 2; ++i) {
4667 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4668 default: break;
4669 case 0:
4670 V[i] = V[i*2]; // Must be a zero vector.
4671 break;
4672 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004673 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004674 break;
4675 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004676 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004677 break;
4678 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004680 break;
4681 }
4682 }
4683
Nate Begeman9008ca62009-04-27 18:41:29 +00004684 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004685 bool Reverse = (NonZeros & 0x3) == 2;
4686 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004687 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004688 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4689 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4691 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004692 }
4693
Nate Begemanfdea31a2010-03-24 20:49:50 +00004694 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4695 // Check for a build vector of consecutive loads.
4696 for (unsigned i = 0; i < NumElems; ++i)
4697 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004698
Nate Begemanfdea31a2010-03-24 20:49:50 +00004699 // Check for elements which are consecutive loads.
4700 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4701 if (LD.getNode())
4702 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004703
4704 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004705 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004706 SDValue Result;
4707 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4708 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4709 else
4710 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004711
Chris Lattner24faf612010-08-28 17:59:08 +00004712 for (unsigned i = 1; i < NumElems; ++i) {
4713 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4714 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004715 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004716 }
4717 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004718 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004719
Chris Lattner6e80e442010-08-28 17:15:43 +00004720 // Otherwise, expand into a number of unpckl*, start by extending each of
4721 // our (non-undef) elements to the full vector width with the element in the
4722 // bottom slot of the vector (which generates no code for SSE).
4723 for (unsigned i = 0; i < NumElems; ++i) {
4724 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4725 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4726 else
4727 V[i] = DAG.getUNDEF(VT);
4728 }
4729
4730 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004731 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4732 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4733 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004734 unsigned EltStride = NumElems >> 1;
4735 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004736 for (unsigned i = 0; i < EltStride; ++i) {
4737 // If V[i+EltStride] is undef and this is the first round of mixing,
4738 // then it is safe to just drop this shuffle: V[i] is already in the
4739 // right place, the one element (since it's the first round) being
4740 // inserted as undef can be dropped. This isn't safe for successive
4741 // rounds because they will permute elements within both vectors.
4742 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4743 EltStride == NumElems/2)
4744 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004745
Chris Lattner6e80e442010-08-28 17:15:43 +00004746 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004747 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004748 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004749 }
4750 return V[0];
4751 }
Dan Gohman475871a2008-07-27 21:46:04 +00004752 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004753}
4754
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004755SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004756X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004757 // We support concatenate two MMX registers and place them in a MMX
4758 // register. This is better than doing a stack convert.
4759 DebugLoc dl = Op.getDebugLoc();
4760 EVT ResVT = Op.getValueType();
4761 assert(Op.getNumOperands() == 2);
4762 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4763 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4764 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004765 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004766 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4767 InVec = Op.getOperand(1);
4768 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4769 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004770 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004771 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4772 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4773 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004774 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004775 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4776 Mask[0] = 0; Mask[1] = 2;
4777 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4778 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004779 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004780}
4781
Nate Begemanb9a47b82009-02-23 08:49:38 +00004782// v8i16 shuffles - Prefer shuffles in the following order:
4783// 1. [all] pshuflw, pshufhw, optional move
4784// 2. [ssse3] 1 x pshufb
4785// 3. [ssse3] 2 x pshufb + 1 x por
4786// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004787SDValue
4788X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4789 SelectionDAG &DAG) const {
4790 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004791 SDValue V1 = SVOp->getOperand(0);
4792 SDValue V2 = SVOp->getOperand(1);
4793 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004795
Nate Begemanb9a47b82009-02-23 08:49:38 +00004796 // Determine if more than 1 of the words in each of the low and high quadwords
4797 // of the result come from the same quadword of one of the two inputs. Undef
4798 // mask values count as coming from any quadword, for better codegen.
4799 SmallVector<unsigned, 4> LoQuad(4);
4800 SmallVector<unsigned, 4> HiQuad(4);
4801 BitVector InputQuads(4);
4802 for (unsigned i = 0; i < 8; ++i) {
4803 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004804 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 MaskVals.push_back(EltIdx);
4806 if (EltIdx < 0) {
4807 ++Quad[0];
4808 ++Quad[1];
4809 ++Quad[2];
4810 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004811 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004812 }
4813 ++Quad[EltIdx / 4];
4814 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004815 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004816
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004818 unsigned MaxQuad = 1;
4819 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004820 if (LoQuad[i] > MaxQuad) {
4821 BestLoQuad = i;
4822 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004823 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004824 }
4825
Nate Begemanb9a47b82009-02-23 08:49:38 +00004826 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004827 MaxQuad = 1;
4828 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 if (HiQuad[i] > MaxQuad) {
4830 BestHiQuad = i;
4831 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004832 }
4833 }
4834
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004836 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004837 // single pshufb instruction is necessary. If There are more than 2 input
4838 // quads, disable the next transformation since it does not help SSSE3.
4839 bool V1Used = InputQuads[0] || InputQuads[1];
4840 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004841 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004842 if (InputQuads.count() == 2 && V1Used && V2Used) {
4843 BestLoQuad = InputQuads.find_first();
4844 BestHiQuad = InputQuads.find_next(BestLoQuad);
4845 }
4846 if (InputQuads.count() > 2) {
4847 BestLoQuad = -1;
4848 BestHiQuad = -1;
4849 }
4850 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004851
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4853 // the shuffle mask. If a quad is scored as -1, that means that it contains
4854 // words from all 4 input quadwords.
4855 SDValue NewV;
4856 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004857 SmallVector<int, 8> MaskV;
4858 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4859 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004860 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004861 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4862 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4863 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004864
Nate Begemanb9a47b82009-02-23 08:49:38 +00004865 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4866 // source words for the shuffle, to aid later transformations.
4867 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004868 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004869 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004870 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004871 if (idx != (int)i)
4872 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004873 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004874 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004875 AllWordsInNewV = false;
4876 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004877 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004878
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4880 if (AllWordsInNewV) {
4881 for (int i = 0; i != 8; ++i) {
4882 int idx = MaskVals[i];
4883 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004884 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004885 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004886 if ((idx != i) && idx < 4)
4887 pshufhw = false;
4888 if ((idx != i) && idx > 3)
4889 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004890 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004891 V1 = NewV;
4892 V2Used = false;
4893 BestLoQuad = 0;
4894 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004895 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004896
Nate Begemanb9a47b82009-02-23 08:49:38 +00004897 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4898 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004899 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004900 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4901 unsigned TargetMask = 0;
4902 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004903 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004904 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4905 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4906 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004907 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004908 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004909 }
Eric Christopherfd179292009-08-27 18:07:15 +00004910
Nate Begemanb9a47b82009-02-23 08:49:38 +00004911 // If we have SSSE3, and all words of the result are from 1 input vector,
4912 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4913 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004914 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004915 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004916
Nate Begemanb9a47b82009-02-23 08:49:38 +00004917 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004918 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004919 // mask, and elements that come from V1 in the V2 mask, so that the two
4920 // results can be OR'd together.
4921 bool TwoInputs = V1Used && V2Used;
4922 for (unsigned i = 0; i != 8; ++i) {
4923 int EltIdx = MaskVals[i] * 2;
4924 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004925 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4926 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004927 continue;
4928 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004929 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4930 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004931 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004932 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004933 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004934 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004936 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004937 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004938
Nate Begemanb9a47b82009-02-23 08:49:38 +00004939 // Calculate the shuffle mask for the second input, shuffle it, and
4940 // OR it with the first shuffled input.
4941 pshufbMask.clear();
4942 for (unsigned i = 0; i != 8; ++i) {
4943 int EltIdx = MaskVals[i] * 2;
4944 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004945 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4946 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004947 continue;
4948 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004949 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4950 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004951 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004952 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004953 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004954 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004955 MVT::v16i8, &pshufbMask[0], 16));
4956 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004957 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 }
4959
4960 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4961 // and update MaskVals with new element order.
4962 BitVector InOrder(8);
4963 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004964 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004965 for (int i = 0; i != 4; ++i) {
4966 int idx = MaskVals[i];
4967 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004968 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004969 InOrder.set(i);
4970 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004971 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004972 InOrder.set(i);
4973 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004975 }
4976 }
4977 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004978 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004979 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004980 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004981
4982 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4983 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4984 NewV.getOperand(0),
4985 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4986 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004987 }
Eric Christopherfd179292009-08-27 18:07:15 +00004988
Nate Begemanb9a47b82009-02-23 08:49:38 +00004989 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4990 // and update MaskVals with the new element order.
4991 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004992 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004993 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004994 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004995 for (unsigned i = 4; i != 8; ++i) {
4996 int idx = MaskVals[i];
4997 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004998 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004999 InOrder.set(i);
5000 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005001 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 InOrder.set(i);
5003 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005004 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005005 }
5006 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005007 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005008 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005009
5010 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5011 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5012 NewV.getOperand(0),
5013 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5014 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005015 }
Eric Christopherfd179292009-08-27 18:07:15 +00005016
Nate Begemanb9a47b82009-02-23 08:49:38 +00005017 // In case BestHi & BestLo were both -1, which means each quadword has a word
5018 // from each of the four input quadwords, calculate the InOrder bitvector now
5019 // before falling through to the insert/extract cleanup.
5020 if (BestLoQuad == -1 && BestHiQuad == -1) {
5021 NewV = V1;
5022 for (int i = 0; i != 8; ++i)
5023 if (MaskVals[i] < 0 || MaskVals[i] == i)
5024 InOrder.set(i);
5025 }
Eric Christopherfd179292009-08-27 18:07:15 +00005026
Nate Begemanb9a47b82009-02-23 08:49:38 +00005027 // The other elements are put in the right place using pextrw and pinsrw.
5028 for (unsigned i = 0; i != 8; ++i) {
5029 if (InOrder[i])
5030 continue;
5031 int EltIdx = MaskVals[i];
5032 if (EltIdx < 0)
5033 continue;
5034 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005035 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005036 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005037 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005038 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005039 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005040 DAG.getIntPtrConstant(i));
5041 }
5042 return NewV;
5043}
5044
5045// v16i8 shuffles - Prefer shuffles in the following order:
5046// 1. [ssse3] 1 x pshufb
5047// 2. [ssse3] 2 x pshufb + 1 x por
5048// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5049static
Nate Begeman9008ca62009-04-27 18:41:29 +00005050SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005051 SelectionDAG &DAG,
5052 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005053 SDValue V1 = SVOp->getOperand(0);
5054 SDValue V2 = SVOp->getOperand(1);
5055 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005056 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005057 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005058
Nate Begemanb9a47b82009-02-23 08:49:38 +00005059 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005060 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005061 // present, fall back to case 3.
5062 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5063 bool V1Only = true;
5064 bool V2Only = true;
5065 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005066 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005067 if (EltIdx < 0)
5068 continue;
5069 if (EltIdx < 16)
5070 V2Only = false;
5071 else
5072 V1Only = false;
5073 }
Eric Christopherfd179292009-08-27 18:07:15 +00005074
Nate Begemanb9a47b82009-02-23 08:49:38 +00005075 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5076 if (TLI.getSubtarget()->hasSSSE3()) {
5077 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005078
Nate Begemanb9a47b82009-02-23 08:49:38 +00005079 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005080 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005081 //
5082 // Otherwise, we have elements from both input vectors, and must zero out
5083 // elements that come from V2 in the first mask, and V1 in the second mask
5084 // so that we can OR them together.
5085 bool TwoInputs = !(V1Only || V2Only);
5086 for (unsigned i = 0; i != 16; ++i) {
5087 int EltIdx = MaskVals[i];
5088 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005089 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005090 continue;
5091 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005092 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005093 }
5094 // If all the elements are from V2, assign it to V1 and return after
5095 // building the first pshufb.
5096 if (V2Only)
5097 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005098 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005099 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005100 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005101 if (!TwoInputs)
5102 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005103
Nate Begemanb9a47b82009-02-23 08:49:38 +00005104 // Calculate the shuffle mask for the second input, shuffle it, and
5105 // OR it with the first shuffled input.
5106 pshufbMask.clear();
5107 for (unsigned i = 0; i != 16; ++i) {
5108 int EltIdx = MaskVals[i];
5109 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005111 continue;
5112 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005114 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005115 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005116 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005117 MVT::v16i8, &pshufbMask[0], 16));
5118 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005119 }
Eric Christopherfd179292009-08-27 18:07:15 +00005120
Nate Begemanb9a47b82009-02-23 08:49:38 +00005121 // No SSSE3 - Calculate in place words and then fix all out of place words
5122 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5123 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005124 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5125 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005126 SDValue NewV = V2Only ? V2 : V1;
5127 for (int i = 0; i != 8; ++i) {
5128 int Elt0 = MaskVals[i*2];
5129 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005130
Nate Begemanb9a47b82009-02-23 08:49:38 +00005131 // This word of the result is all undef, skip it.
5132 if (Elt0 < 0 && Elt1 < 0)
5133 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005134
Nate Begemanb9a47b82009-02-23 08:49:38 +00005135 // This word of the result is already in the correct place, skip it.
5136 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5137 continue;
5138 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5139 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005140
Nate Begemanb9a47b82009-02-23 08:49:38 +00005141 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5142 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5143 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005144
5145 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5146 // using a single extract together, load it and store it.
5147 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005148 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005149 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005150 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005151 DAG.getIntPtrConstant(i));
5152 continue;
5153 }
5154
Nate Begemanb9a47b82009-02-23 08:49:38 +00005155 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005156 // source byte is not also odd, shift the extracted word left 8 bits
5157 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005158 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005159 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005160 DAG.getIntPtrConstant(Elt1 / 2));
5161 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005162 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005163 DAG.getConstant(8,
5164 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005165 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005166 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5167 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005168 }
5169 // If Elt0 is defined, extract it from the appropriate source. If the
5170 // source byte is not also even, shift the extracted word right 8 bits. If
5171 // Elt1 was also defined, OR the extracted values together before
5172 // inserting them in the result.
5173 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005174 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005175 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5176 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005177 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005178 DAG.getConstant(8,
5179 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005180 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005181 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5182 DAG.getConstant(0x00FF, MVT::i16));
5183 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005184 : InsElt0;
5185 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005186 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005187 DAG.getIntPtrConstant(i));
5188 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005189 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005190}
5191
Evan Cheng7a831ce2007-12-15 03:00:47 +00005192/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005193/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005194/// done when every pair / quad of shuffle mask elements point to elements in
5195/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005196/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005197static
Nate Begeman9008ca62009-04-27 18:41:29 +00005198SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005199 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005200 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005201 SDValue V1 = SVOp->getOperand(0);
5202 SDValue V2 = SVOp->getOperand(1);
5203 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005204 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005205 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005206 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005207 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005208 case MVT::v4f32: NewVT = MVT::v2f64; break;
5209 case MVT::v4i32: NewVT = MVT::v2i64; break;
5210 case MVT::v8i16: NewVT = MVT::v4i32; break;
5211 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005212 }
5213
Nate Begeman9008ca62009-04-27 18:41:29 +00005214 int Scale = NumElems / NewWidth;
5215 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005216 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005217 int StartIdx = -1;
5218 for (int j = 0; j < Scale; ++j) {
5219 int EltIdx = SVOp->getMaskElt(i+j);
5220 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005221 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005222 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005223 StartIdx = EltIdx - (EltIdx % Scale);
5224 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005225 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005226 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005227 if (StartIdx == -1)
5228 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005229 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005230 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005231 }
5232
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005233 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5234 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005235 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005236}
5237
Evan Chengd880b972008-05-09 21:53:03 +00005238/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005239///
Owen Andersone50ed302009-08-10 22:56:29 +00005240static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005241 SDValue SrcOp, SelectionDAG &DAG,
5242 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005243 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005244 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005245 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005246 LD = dyn_cast<LoadSDNode>(SrcOp);
5247 if (!LD) {
5248 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5249 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005250 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005251 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005252 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005253 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005254 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005255 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005256 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005257 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005258 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5259 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5260 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005261 SrcOp.getOperand(0)
5262 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005263 }
5264 }
5265 }
5266
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005267 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005268 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005269 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005270 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005271}
5272
Evan Chengace3c172008-07-22 21:13:36 +00005273/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5274/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005275static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005276LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5277 SDValue V1 = SVOp->getOperand(0);
5278 SDValue V2 = SVOp->getOperand(1);
5279 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005280 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005281
Evan Chengace3c172008-07-22 21:13:36 +00005282 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005283 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 SmallVector<int, 8> Mask1(4U, -1);
5285 SmallVector<int, 8> PermMask;
5286 SVOp->getMask(PermMask);
5287
Evan Chengace3c172008-07-22 21:13:36 +00005288 unsigned NumHi = 0;
5289 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005290 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005291 int Idx = PermMask[i];
5292 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005293 Locs[i] = std::make_pair(-1, -1);
5294 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005295 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5296 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005297 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005299 NumLo++;
5300 } else {
5301 Locs[i] = std::make_pair(1, NumHi);
5302 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005303 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005304 NumHi++;
5305 }
5306 }
5307 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005308
Evan Chengace3c172008-07-22 21:13:36 +00005309 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005310 // If no more than two elements come from either vector. This can be
5311 // implemented with two shuffles. First shuffle gather the elements.
5312 // The second shuffle, which takes the first shuffle as both of its
5313 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005314 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005315
Nate Begeman9008ca62009-04-27 18:41:29 +00005316 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005317
Evan Chengace3c172008-07-22 21:13:36 +00005318 for (unsigned i = 0; i != 4; ++i) {
5319 if (Locs[i].first == -1)
5320 continue;
5321 else {
5322 unsigned Idx = (i < 2) ? 0 : 4;
5323 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005324 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005325 }
5326 }
5327
Nate Begeman9008ca62009-04-27 18:41:29 +00005328 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005329 } else if (NumLo == 3 || NumHi == 3) {
5330 // Otherwise, we must have three elements from one vector, call it X, and
5331 // one element from the other, call it Y. First, use a shufps to build an
5332 // intermediate vector with the one element from Y and the element from X
5333 // that will be in the same half in the final destination (the indexes don't
5334 // matter). Then, use a shufps to build the final vector, taking the half
5335 // containing the element from Y from the intermediate, and the other half
5336 // from X.
5337 if (NumHi == 3) {
5338 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005340 std::swap(V1, V2);
5341 }
5342
5343 // Find the element from V2.
5344 unsigned HiIndex;
5345 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005346 int Val = PermMask[HiIndex];
5347 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005348 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005349 if (Val >= 4)
5350 break;
5351 }
5352
Nate Begeman9008ca62009-04-27 18:41:29 +00005353 Mask1[0] = PermMask[HiIndex];
5354 Mask1[1] = -1;
5355 Mask1[2] = PermMask[HiIndex^1];
5356 Mask1[3] = -1;
5357 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005358
5359 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005360 Mask1[0] = PermMask[0];
5361 Mask1[1] = PermMask[1];
5362 Mask1[2] = HiIndex & 1 ? 6 : 4;
5363 Mask1[3] = HiIndex & 1 ? 4 : 6;
5364 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005365 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005366 Mask1[0] = HiIndex & 1 ? 2 : 0;
5367 Mask1[1] = HiIndex & 1 ? 0 : 2;
5368 Mask1[2] = PermMask[2];
5369 Mask1[3] = PermMask[3];
5370 if (Mask1[2] >= 0)
5371 Mask1[2] += 4;
5372 if (Mask1[3] >= 0)
5373 Mask1[3] += 4;
5374 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005375 }
Evan Chengace3c172008-07-22 21:13:36 +00005376 }
5377
5378 // Break it into (shuffle shuffle_hi, shuffle_lo).
5379 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005380 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005381 SmallVector<int,8> LoMask(4U, -1);
5382 SmallVector<int,8> HiMask(4U, -1);
5383
5384 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005385 unsigned MaskIdx = 0;
5386 unsigned LoIdx = 0;
5387 unsigned HiIdx = 2;
5388 for (unsigned i = 0; i != 4; ++i) {
5389 if (i == 2) {
5390 MaskPtr = &HiMask;
5391 MaskIdx = 1;
5392 LoIdx = 0;
5393 HiIdx = 2;
5394 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005395 int Idx = PermMask[i];
5396 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005397 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005398 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005399 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005400 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005401 LoIdx++;
5402 } else {
5403 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005404 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005405 HiIdx++;
5406 }
5407 }
5408
Nate Begeman9008ca62009-04-27 18:41:29 +00005409 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5410 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5411 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005412 for (unsigned i = 0; i != 4; ++i) {
5413 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005414 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005415 } else {
5416 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005417 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005418 }
5419 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005421}
5422
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005423static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005424 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005425 V = V.getOperand(0);
5426 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5427 V = V.getOperand(0);
5428 if (MayFoldLoad(V))
5429 return true;
5430 return false;
5431}
5432
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005433// FIXME: the version above should always be used. Since there's
5434// a bug where several vector shuffles can't be folded because the
5435// DAG is not updated during lowering and a node claims to have two
5436// uses while it only has one, use this version, and let isel match
5437// another instruction if the load really happens to have more than
5438// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005439// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005440static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005441 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005442 V = V.getOperand(0);
5443 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5444 V = V.getOperand(0);
5445 if (ISD::isNormalLoad(V.getNode()))
5446 return true;
5447 return false;
5448}
5449
5450/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5451/// a vector extract, and if both can be later optimized into a single load.
5452/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5453/// here because otherwise a target specific shuffle node is going to be
5454/// emitted for this shuffle, and the optimization not done.
5455/// FIXME: This is probably not the best approach, but fix the problem
5456/// until the right path is decided.
5457static
5458bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5459 const TargetLowering &TLI) {
5460 EVT VT = V.getValueType();
5461 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5462
5463 // Be sure that the vector shuffle is present in a pattern like this:
5464 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5465 if (!V.hasOneUse())
5466 return false;
5467
5468 SDNode *N = *V.getNode()->use_begin();
5469 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5470 return false;
5471
5472 SDValue EltNo = N->getOperand(1);
5473 if (!isa<ConstantSDNode>(EltNo))
5474 return false;
5475
5476 // If the bit convert changed the number of elements, it is unsafe
5477 // to examine the mask.
5478 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005479 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005480 EVT SrcVT = V.getOperand(0).getValueType();
5481 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5482 return false;
5483 V = V.getOperand(0);
5484 HasShuffleIntoBitcast = true;
5485 }
5486
5487 // Select the input vector, guarding against out of range extract vector.
5488 unsigned NumElems = VT.getVectorNumElements();
5489 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5490 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5491 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5492
5493 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005494 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005495 V = V.getOperand(0);
5496
5497 if (ISD::isNormalLoad(V.getNode())) {
5498 // Is the original load suitable?
5499 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5500
5501 // FIXME: avoid the multi-use bug that is preventing lots of
5502 // of foldings to be detected, this is still wrong of course, but
5503 // give the temporary desired behavior, and if it happens that
5504 // the load has real more uses, during isel it will not fold, and
5505 // will generate poor code.
5506 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5507 return false;
5508
5509 if (!HasShuffleIntoBitcast)
5510 return true;
5511
5512 // If there's a bitcast before the shuffle, check if the load type and
5513 // alignment is valid.
5514 unsigned Align = LN0->getAlignment();
5515 unsigned NewAlign =
5516 TLI.getTargetData()->getABITypeAlignment(
5517 VT.getTypeForEVT(*DAG.getContext()));
5518
5519 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5520 return false;
5521 }
5522
5523 return true;
5524}
5525
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005526static
Evan Cheng835580f2010-10-07 20:50:20 +00005527SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5528 EVT VT = Op.getValueType();
5529
5530 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005531 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5532 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005533 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5534 V1, DAG));
5535}
5536
5537static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005538SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5539 bool HasSSE2) {
5540 SDValue V1 = Op.getOperand(0);
5541 SDValue V2 = Op.getOperand(1);
5542 EVT VT = Op.getValueType();
5543
5544 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5545
5546 if (HasSSE2 && VT == MVT::v2f64)
5547 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5548
5549 // v4f32 or v4i32
5550 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5551}
5552
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005553static
5554SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5555 SDValue V1 = Op.getOperand(0);
5556 SDValue V2 = Op.getOperand(1);
5557 EVT VT = Op.getValueType();
5558
5559 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5560 "unsupported shuffle type");
5561
5562 if (V2.getOpcode() == ISD::UNDEF)
5563 V2 = V1;
5564
5565 // v4i32 or v4f32
5566 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5567}
5568
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005569static
5570SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5571 SDValue V1 = Op.getOperand(0);
5572 SDValue V2 = Op.getOperand(1);
5573 EVT VT = Op.getValueType();
5574 unsigned NumElems = VT.getVectorNumElements();
5575
5576 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5577 // operand of these instructions is only memory, so check if there's a
5578 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5579 // same masks.
5580 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005581
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005582 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005583 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005584 CanFoldLoad = true;
5585
5586 // When V1 is a load, it can be folded later into a store in isel, example:
5587 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5588 // turns into:
5589 // (MOVLPSmr addr:$src1, VR128:$src2)
5590 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005591 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005592 CanFoldLoad = true;
5593
Eric Christopher893a8822011-02-20 05:04:42 +00005594 // Both of them can't be memory operations though.
5595 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5596 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005597
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005598 if (CanFoldLoad) {
5599 if (HasSSE2 && NumElems == 2)
5600 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5601
5602 if (NumElems == 4)
5603 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5604 }
5605
5606 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5607 // movl and movlp will both match v2i64, but v2i64 is never matched by
5608 // movl earlier because we make it strict to avoid messing with the movlp load
5609 // folding logic (see the code above getMOVLP call). Match it here then,
5610 // this is horrible, but will stay like this until we move all shuffle
5611 // matching to x86 specific nodes. Note that for the 1st condition all
5612 // types are matched with movsd.
5613 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5614 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5615 else if (HasSSE2)
5616 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5617
5618
5619 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5620
5621 // Invert the operand order and use SHUFPS to match it.
5622 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5623 X86::getShuffleSHUFImmediate(SVOp), DAG);
5624}
5625
David Greenec4db4e52011-02-28 19:06:56 +00005626static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005627 switch(VT.getSimpleVT().SimpleTy) {
5628 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5629 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005630 case MVT::v4f32:
5631 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5632 case MVT::v2f64:
5633 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5634 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5635 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005636 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5637 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5638 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005639 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005640 }
5641 return 0;
5642}
5643
5644static inline unsigned getUNPCKHOpcode(EVT VT) {
5645 switch(VT.getSimpleVT().SimpleTy) {
5646 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5647 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5648 case MVT::v4f32: return X86ISD::UNPCKHPS;
5649 case MVT::v2f64: return X86ISD::UNPCKHPD;
5650 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5651 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5652 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005653 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005654 }
5655 return 0;
5656}
5657
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005658static
5659SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005660 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005661 const X86Subtarget *Subtarget) {
5662 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5663 EVT VT = Op.getValueType();
5664 DebugLoc dl = Op.getDebugLoc();
5665 SDValue V1 = Op.getOperand(0);
5666 SDValue V2 = Op.getOperand(1);
5667
5668 if (isZeroShuffle(SVOp))
5669 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5670
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005671 // Handle splat operations
5672 if (SVOp->isSplat()) {
5673 // Special case, this is the only place now where it's
5674 // allowed to return a vector_shuffle operation without
5675 // using a target specific node, because *hopefully* it
5676 // will be optimized away by the dag combiner.
5677 if (VT.getVectorNumElements() <= 4 &&
5678 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5679 return Op;
5680
5681 // Handle splats by matching through known masks
5682 if (VT.getVectorNumElements() <= 4)
5683 return SDValue();
5684
Evan Cheng835580f2010-10-07 20:50:20 +00005685 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005686 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005687 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005688
5689 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5690 // do it!
5691 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5692 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5693 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005694 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005695 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5696 // FIXME: Figure out a cleaner way to do this.
5697 // Try to make use of movq to zero out the top part.
5698 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5699 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5700 if (NewOp.getNode()) {
5701 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5702 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5703 DAG, Subtarget, dl);
5704 }
5705 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5706 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5707 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5708 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5709 DAG, Subtarget, dl);
5710 }
5711 }
5712 return SDValue();
5713}
5714
Dan Gohman475871a2008-07-27 21:46:04 +00005715SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005716X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005717 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005718 SDValue V1 = Op.getOperand(0);
5719 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005720 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005721 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005722 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005723 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005724 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5725 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005726 bool V1IsSplat = false;
5727 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005728 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005729 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005730 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005731 MachineFunction &MF = DAG.getMachineFunction();
5732 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005733
Dale Johannesen0488fb62010-09-30 23:57:10 +00005734 // Shuffle operations on MMX not supported.
5735 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005736 return Op;
5737
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005738 // Vector shuffle lowering takes 3 steps:
5739 //
5740 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5741 // narrowing and commutation of operands should be handled.
5742 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5743 // shuffle nodes.
5744 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5745 // so the shuffle can be broken into other shuffles and the legalizer can
5746 // try the lowering again.
5747 //
5748 // The general ideia is that no vector_shuffle operation should be left to
5749 // be matched during isel, all of them must be converted to a target specific
5750 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005751
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005752 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5753 // narrowing and commutation of operands should be handled. The actual code
5754 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005755 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005756 if (NewOp.getNode())
5757 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005758
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005759 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5760 // unpckh_undef). Only use pshufd if speed is more important than size.
5761 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5762 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005763 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005764 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5765 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5766 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005767
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005768 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005769 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005770 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005771
Dale Johannesen0488fb62010-09-30 23:57:10 +00005772 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005773 return getMOVHighToLow(Op, dl, DAG);
5774
5775 // Use to match splats
5776 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5777 (VT == MVT::v2f64 || VT == MVT::v2i64))
5778 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5779
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005780 if (X86::isPSHUFDMask(SVOp)) {
5781 // The actual implementation will match the mask in the if above and then
5782 // during isel it can match several different instructions, not only pshufd
5783 // as its name says, sad but true, emulate the behavior for now...
5784 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5785 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5786
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005787 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5788
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005789 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005790 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5791
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005792 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005793 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5794 TargetMask, DAG);
5795
5796 if (VT == MVT::v4f32)
5797 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5798 TargetMask, DAG);
5799 }
Eric Christopherfd179292009-08-27 18:07:15 +00005800
Evan Chengf26ffe92008-05-29 08:22:04 +00005801 // Check if this can be converted into a logical shift.
5802 bool isLeft = false;
5803 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005804 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005805 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005806 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005807 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005808 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005809 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005810 EVT EltVT = VT.getVectorElementType();
5811 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005812 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005813 }
Eric Christopherfd179292009-08-27 18:07:15 +00005814
Nate Begeman9008ca62009-04-27 18:41:29 +00005815 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005816 if (V1IsUndef)
5817 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005818 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005819 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005820 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005821 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005822 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5823
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005824 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005825 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5826 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005827 }
Eric Christopherfd179292009-08-27 18:07:15 +00005828
Nate Begeman9008ca62009-04-27 18:41:29 +00005829 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005830 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5831 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005832
Dale Johannesen0488fb62010-09-30 23:57:10 +00005833 if (X86::isMOVHLPSMask(SVOp))
5834 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005835
Dale Johannesen0488fb62010-09-30 23:57:10 +00005836 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5837 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005838
Dale Johannesen0488fb62010-09-30 23:57:10 +00005839 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5840 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005841
Dale Johannesen0488fb62010-09-30 23:57:10 +00005842 if (X86::isMOVLPMask(SVOp))
5843 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005844
Nate Begeman9008ca62009-04-27 18:41:29 +00005845 if (ShouldXformToMOVHLPS(SVOp) ||
5846 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5847 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005848
Evan Chengf26ffe92008-05-29 08:22:04 +00005849 if (isShift) {
5850 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005851 EVT EltVT = VT.getVectorElementType();
5852 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005853 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005854 }
Eric Christopherfd179292009-08-27 18:07:15 +00005855
Evan Cheng9eca5e82006-10-25 21:49:50 +00005856 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005857 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5858 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005859 V1IsSplat = isSplatVector(V1.getNode());
5860 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005861
Chris Lattner8a594482007-11-25 00:24:49 +00005862 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005863 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005864 Op = CommuteVectorShuffle(SVOp, DAG);
5865 SVOp = cast<ShuffleVectorSDNode>(Op);
5866 V1 = SVOp->getOperand(0);
5867 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005868 std::swap(V1IsSplat, V2IsSplat);
5869 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005870 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005871 }
5872
Nate Begeman9008ca62009-04-27 18:41:29 +00005873 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5874 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005875 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005876 return V1;
5877 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5878 // the instruction selector will not match, so get a canonical MOVL with
5879 // swapped operands to undo the commute.
5880 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005881 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005882
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005883 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005884 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5885 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005886
5887 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005888 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005889
Evan Cheng9bbbb982006-10-25 20:48:19 +00005890 if (V2IsSplat) {
5891 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005892 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005893 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005894 SDValue NewMask = NormalizeMask(SVOp, DAG);
5895 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5896 if (NSVOp != SVOp) {
5897 if (X86::isUNPCKLMask(NSVOp, true)) {
5898 return NewMask;
5899 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5900 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005901 }
5902 }
5903 }
5904
Evan Cheng9eca5e82006-10-25 21:49:50 +00005905 if (Commuted) {
5906 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005907 // FIXME: this seems wrong.
5908 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5909 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005910
5911 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005912 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5913 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005914
5915 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005916 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005917 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005918
Nate Begeman9008ca62009-04-27 18:41:29 +00005919 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005920 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005921 return CommuteVectorShuffle(SVOp, DAG);
5922
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005923 // The checks below are all present in isShuffleMaskLegal, but they are
5924 // inlined here right now to enable us to directly emit target specific
5925 // nodes, and remove one by one until they don't return Op anymore.
5926 SmallVector<int, 16> M;
5927 SVOp->getMask(M);
5928
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005929 if (isPALIGNRMask(M, VT, HasSSSE3))
5930 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5931 X86::getShufflePALIGNRImmediate(SVOp),
5932 DAG);
5933
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005934 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5935 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005936 if (VT == MVT::v2f64) {
5937 X86ISD::NodeType Opcode =
5938 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5939 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5940 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005941 if (VT == MVT::v2i64)
5942 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5943 }
5944
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005945 if (isPSHUFHWMask(M, VT))
5946 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5947 X86::getShufflePSHUFHWImmediate(SVOp),
5948 DAG);
5949
5950 if (isPSHUFLWMask(M, VT))
5951 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5952 X86::getShufflePSHUFLWImmediate(SVOp),
5953 DAG);
5954
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005955 if (isSHUFPMask(M, VT)) {
5956 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5957 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5958 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5959 TargetMask, DAG);
5960 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5961 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5962 TargetMask, DAG);
5963 }
5964
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005965 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5966 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005967 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5968 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005969 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5970 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5971 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5972
Evan Cheng14b32e12007-12-11 01:46:18 +00005973 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005974 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005975 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005976 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005977 return NewOp;
5978 }
5979
Owen Anderson825b72b2009-08-11 20:47:22 +00005980 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005981 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 if (NewOp.getNode())
5983 return NewOp;
5984 }
Eric Christopherfd179292009-08-27 18:07:15 +00005985
Dale Johannesen0488fb62010-09-30 23:57:10 +00005986 // Handle all 4 wide cases with a number of shuffles.
5987 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005988 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005989
Dan Gohman475871a2008-07-27 21:46:04 +00005990 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005991}
5992
Dan Gohman475871a2008-07-27 21:46:04 +00005993SDValue
5994X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005995 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005996 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005997 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005998 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005999 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006000 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006002 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006003 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006004 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006005 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6006 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6007 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006008 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6009 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006010 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006011 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006012 Op.getOperand(0)),
6013 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006014 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006015 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006016 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006017 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006018 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006019 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006020 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6021 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006022 // result has a single use which is a store or a bitcast to i32. And in
6023 // the case of a store, it's not worth it if the index is a constant 0,
6024 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006025 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006026 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006027 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006028 if ((User->getOpcode() != ISD::STORE ||
6029 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6030 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006031 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006033 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006035 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006036 Op.getOperand(0)),
6037 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006038 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006039 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006040 // ExtractPS works with constant index.
6041 if (isa<ConstantSDNode>(Op.getOperand(1)))
6042 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006043 }
Dan Gohman475871a2008-07-27 21:46:04 +00006044 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006045}
6046
6047
Dan Gohman475871a2008-07-27 21:46:04 +00006048SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006049X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6050 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006051 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006052 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006053
David Greene74a579d2011-02-10 16:57:36 +00006054 SDValue Vec = Op.getOperand(0);
6055 EVT VecVT = Vec.getValueType();
6056
6057 // If this is a 256-bit vector result, first extract the 128-bit
6058 // vector and then extract from the 128-bit vector.
6059 if (VecVT.getSizeInBits() > 128) {
6060 DebugLoc dl = Op.getNode()->getDebugLoc();
6061 unsigned NumElems = VecVT.getVectorNumElements();
6062 SDValue Idx = Op.getOperand(1);
6063
6064 if (!isa<ConstantSDNode>(Idx))
6065 return SDValue();
6066
6067 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6068 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6069
6070 // Get the 128-bit vector.
6071 bool Upper = IdxVal >= ExtractNumElems;
6072 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6073
6074 // Extract from it.
6075 SDValue ScaledIdx = Idx;
6076 if (Upper)
6077 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6078 DAG.getConstant(ExtractNumElems,
6079 Idx.getValueType()));
6080 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6081 ScaledIdx);
6082 }
6083
6084 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6085
Evan Cheng62a3f152008-03-24 21:52:23 +00006086 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006087 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006088 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006089 return Res;
6090 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006091
Owen Andersone50ed302009-08-10 22:56:29 +00006092 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006093 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006094 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006095 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006096 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006097 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006098 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006099 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6100 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006101 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006102 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006103 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006104 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006105 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006106 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006107 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006108 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006109 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006110 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006111 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006112 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006113 if (Idx == 0)
6114 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006115
Evan Cheng0db9fe62006-04-25 20:13:52 +00006116 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006117 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006118 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006119 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006120 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006121 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006122 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006123 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006124 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6125 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6126 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006127 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006128 if (Idx == 0)
6129 return Op;
6130
6131 // UNPCKHPD the element to the lowest double word, then movsd.
6132 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6133 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006134 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006135 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006136 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006138 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006139 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006140 }
6141
Dan Gohman475871a2008-07-27 21:46:04 +00006142 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006143}
6144
Dan Gohman475871a2008-07-27 21:46:04 +00006145SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006146X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6147 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006148 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006149 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006150 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006151
Dan Gohman475871a2008-07-27 21:46:04 +00006152 SDValue N0 = Op.getOperand(0);
6153 SDValue N1 = Op.getOperand(1);
6154 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006155
Dan Gohman8a55ce42009-09-23 21:02:20 +00006156 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006157 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006158 unsigned Opc;
6159 if (VT == MVT::v8i16)
6160 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006161 else if (VT == MVT::v16i8)
6162 Opc = X86ISD::PINSRB;
6163 else
6164 Opc = X86ISD::PINSRB;
6165
Nate Begeman14d12ca2008-02-11 04:19:36 +00006166 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6167 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006168 if (N1.getValueType() != MVT::i32)
6169 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6170 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006171 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006172 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006173 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006174 // Bits [7:6] of the constant are the source select. This will always be
6175 // zero here. The DAG Combiner may combine an extract_elt index into these
6176 // bits. For example (insert (extract, 3), 2) could be matched by putting
6177 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006178 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006179 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006180 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006181 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006182 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006183 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006184 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006185 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006186 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006187 // PINSR* works with constant index.
6188 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006189 }
Dan Gohman475871a2008-07-27 21:46:04 +00006190 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006191}
6192
Dan Gohman475871a2008-07-27 21:46:04 +00006193SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006194X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006195 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006196 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006197
David Greene6b381262011-02-09 15:32:06 +00006198 DebugLoc dl = Op.getDebugLoc();
6199 SDValue N0 = Op.getOperand(0);
6200 SDValue N1 = Op.getOperand(1);
6201 SDValue N2 = Op.getOperand(2);
6202
6203 // If this is a 256-bit vector result, first insert into a 128-bit
6204 // vector and then insert into the 256-bit vector.
6205 if (VT.getSizeInBits() > 128) {
6206 if (!isa<ConstantSDNode>(N2))
6207 return SDValue();
6208
6209 // Get the 128-bit vector.
6210 unsigned NumElems = VT.getVectorNumElements();
6211 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6212 bool Upper = IdxVal >= NumElems / 2;
6213
6214 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6215
6216 // Insert into it.
6217 SDValue ScaledN2 = N2;
6218 if (Upper)
6219 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006220 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006221 (VT.getSizeInBits() / 128),
6222 N2.getValueType()));
6223 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6224 N1, ScaledN2);
6225
6226 // Insert the 128-bit vector
6227 // FIXME: Why UNDEF?
6228 return Insert128BitVector(N0, Op, N2, DAG, dl);
6229 }
6230
Nate Begeman14d12ca2008-02-11 04:19:36 +00006231 if (Subtarget->hasSSE41())
6232 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6233
Dan Gohman8a55ce42009-09-23 21:02:20 +00006234 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006235 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006236
Dan Gohman8a55ce42009-09-23 21:02:20 +00006237 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006238 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6239 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006240 if (N1.getValueType() != MVT::i32)
6241 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6242 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006243 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006244 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006245 }
Dan Gohman475871a2008-07-27 21:46:04 +00006246 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006247}
6248
Dan Gohman475871a2008-07-27 21:46:04 +00006249SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006250X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006251 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006252 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006253 EVT OpVT = Op.getValueType();
6254
6255 // If this is a 256-bit vector result, first insert into a 128-bit
6256 // vector and then insert into the 256-bit vector.
6257 if (OpVT.getSizeInBits() > 128) {
6258 // Insert into a 128-bit vector.
6259 EVT VT128 = EVT::getVectorVT(*Context,
6260 OpVT.getVectorElementType(),
6261 OpVT.getVectorNumElements() / 2);
6262
6263 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6264
6265 // Insert the 128-bit vector.
6266 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6267 DAG.getConstant(0, MVT::i32),
6268 DAG, dl);
6269 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006270
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006271 if (Op.getValueType() == MVT::v1i64 &&
6272 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006273 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006274
Owen Anderson825b72b2009-08-11 20:47:22 +00006275 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006276 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6277 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006278 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006279 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006280}
6281
David Greene91585092011-01-26 15:38:49 +00006282// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6283// a simple subregister reference or explicit instructions to grab
6284// upper bits of a vector.
6285SDValue
6286X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6287 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006288 DebugLoc dl = Op.getNode()->getDebugLoc();
6289 SDValue Vec = Op.getNode()->getOperand(0);
6290 SDValue Idx = Op.getNode()->getOperand(1);
6291
6292 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6293 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6294 return Extract128BitVector(Vec, Idx, DAG, dl);
6295 }
David Greene91585092011-01-26 15:38:49 +00006296 }
6297 return SDValue();
6298}
6299
David Greenecfe33c42011-01-26 19:13:22 +00006300// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6301// simple superregister reference or explicit instructions to insert
6302// the upper bits of a vector.
6303SDValue
6304X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6305 if (Subtarget->hasAVX()) {
6306 DebugLoc dl = Op.getNode()->getDebugLoc();
6307 SDValue Vec = Op.getNode()->getOperand(0);
6308 SDValue SubVec = Op.getNode()->getOperand(1);
6309 SDValue Idx = Op.getNode()->getOperand(2);
6310
6311 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6312 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006313 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006314 }
6315 }
6316 return SDValue();
6317}
6318
Bill Wendling056292f2008-09-16 21:48:12 +00006319// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6320// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6321// one of the above mentioned nodes. It has to be wrapped because otherwise
6322// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6323// be used to form addressing mode. These wrapped nodes will be selected
6324// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006325SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006326X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006327 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006328
Chris Lattner41621a22009-06-26 19:22:52 +00006329 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6330 // global base reg.
6331 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006332 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006333 CodeModel::Model M = getTargetMachine().getCodeModel();
6334
Chris Lattner4f066492009-07-11 20:29:19 +00006335 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006336 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006337 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006338 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006339 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006340 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006341 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006342
Evan Cheng1606e8e2009-03-13 07:51:59 +00006343 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006344 CP->getAlignment(),
6345 CP->getOffset(), OpFlag);
6346 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006347 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006348 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006349 if (OpFlag) {
6350 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006351 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006352 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006353 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006354 }
6355
6356 return Result;
6357}
6358
Dan Gohmand858e902010-04-17 15:26:15 +00006359SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006360 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006361
Chris Lattner18c59872009-06-27 04:16:01 +00006362 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6363 // global base reg.
6364 unsigned char OpFlag = 0;
6365 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006366 CodeModel::Model M = getTargetMachine().getCodeModel();
6367
Chris Lattner4f066492009-07-11 20:29:19 +00006368 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006369 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006370 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006371 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006372 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006373 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006374 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006375
Chris Lattner18c59872009-06-27 04:16:01 +00006376 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6377 OpFlag);
6378 DebugLoc DL = JT->getDebugLoc();
6379 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006380
Chris Lattner18c59872009-06-27 04:16:01 +00006381 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006382 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006383 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6384 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006385 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006386 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006387
Chris Lattner18c59872009-06-27 04:16:01 +00006388 return Result;
6389}
6390
6391SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006392X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006393 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006394
Chris Lattner18c59872009-06-27 04:16:01 +00006395 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6396 // global base reg.
6397 unsigned char OpFlag = 0;
6398 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006399 CodeModel::Model M = getTargetMachine().getCodeModel();
6400
Chris Lattner4f066492009-07-11 20:29:19 +00006401 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006402 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006403 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006404 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006405 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006406 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006407 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006408
Chris Lattner18c59872009-06-27 04:16:01 +00006409 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006410
Chris Lattner18c59872009-06-27 04:16:01 +00006411 DebugLoc DL = Op.getDebugLoc();
6412 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006413
6414
Chris Lattner18c59872009-06-27 04:16:01 +00006415 // With PIC, the address is actually $g + Offset.
6416 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006417 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006418 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6419 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006420 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006421 Result);
6422 }
Eric Christopherfd179292009-08-27 18:07:15 +00006423
Chris Lattner18c59872009-06-27 04:16:01 +00006424 return Result;
6425}
6426
Dan Gohman475871a2008-07-27 21:46:04 +00006427SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006428X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006429 // Create the TargetBlockAddressAddress node.
6430 unsigned char OpFlags =
6431 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006432 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006433 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006434 DebugLoc dl = Op.getDebugLoc();
6435 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6436 /*isTarget=*/true, OpFlags);
6437
Dan Gohmanf705adb2009-10-30 01:28:02 +00006438 if (Subtarget->isPICStyleRIPRel() &&
6439 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006440 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6441 else
6442 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006443
Dan Gohman29cbade2009-11-20 23:18:13 +00006444 // With PIC, the address is actually $g + Offset.
6445 if (isGlobalRelativeToPICBase(OpFlags)) {
6446 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6447 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6448 Result);
6449 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006450
6451 return Result;
6452}
6453
6454SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006455X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006456 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006457 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006458 // Create the TargetGlobalAddress node, folding in the constant
6459 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006460 unsigned char OpFlags =
6461 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006462 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006463 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006464 if (OpFlags == X86II::MO_NO_FLAG &&
6465 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006466 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006467 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006468 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006469 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006470 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006471 }
Eric Christopherfd179292009-08-27 18:07:15 +00006472
Chris Lattner4f066492009-07-11 20:29:19 +00006473 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006474 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006475 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6476 else
6477 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006478
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006479 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006480 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006481 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6482 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006483 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006484 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006485
Chris Lattner36c25012009-07-10 07:34:39 +00006486 // For globals that require a load from a stub to get the address, emit the
6487 // load.
6488 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006489 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006490 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006491
Dan Gohman6520e202008-10-18 02:06:02 +00006492 // If there was a non-zero offset that we didn't fold, create an explicit
6493 // addition for it.
6494 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006495 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006496 DAG.getConstant(Offset, getPointerTy()));
6497
Evan Cheng0db9fe62006-04-25 20:13:52 +00006498 return Result;
6499}
6500
Evan Chengda43bcf2008-09-24 00:05:32 +00006501SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006502X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006503 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006504 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006505 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006506}
6507
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006508static SDValue
6509GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006510 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006511 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006512 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006513 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006514 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006515 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006516 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006517 GA->getOffset(),
6518 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006519 if (InFlag) {
6520 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006521 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006522 } else {
6523 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006524 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006525 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006526
6527 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006528 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006529
Rafael Espindola15f1b662009-04-24 12:59:40 +00006530 SDValue Flag = Chain.getValue(1);
6531 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006532}
6533
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006534// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006535static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006536LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006537 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006538 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006539 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6540 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006541 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006542 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006543 InFlag = Chain.getValue(1);
6544
Chris Lattnerb903bed2009-06-26 21:20:29 +00006545 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006546}
6547
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006548// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006549static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006550LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006551 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006552 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6553 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006554}
6555
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006556// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6557// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006558static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006559 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006560 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006561 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006562
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006563 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6564 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6565 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006566
Michael J. Spencerec38de22010-10-10 22:04:20 +00006567 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006568 DAG.getIntPtrConstant(0),
6569 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006570
Chris Lattnerb903bed2009-06-26 21:20:29 +00006571 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006572 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6573 // initialexec.
6574 unsigned WrapperKind = X86ISD::Wrapper;
6575 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006576 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006577 } else if (is64Bit) {
6578 assert(model == TLSModel::InitialExec);
6579 OperandFlags = X86II::MO_GOTTPOFF;
6580 WrapperKind = X86ISD::WrapperRIP;
6581 } else {
6582 assert(model == TLSModel::InitialExec);
6583 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006584 }
Eric Christopherfd179292009-08-27 18:07:15 +00006585
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006586 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6587 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006588 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006589 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006590 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006591 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006592
Rafael Espindola9a580232009-02-27 13:37:18 +00006593 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006594 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006595 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006596
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006597 // The address of the thread local variable is the add of the thread
6598 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006599 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006600}
6601
Dan Gohman475871a2008-07-27 21:46:04 +00006602SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006603X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006604
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006605 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006606 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006607
Eric Christopher30ef0e52010-06-03 04:07:48 +00006608 if (Subtarget->isTargetELF()) {
6609 // TODO: implement the "local dynamic" model
6610 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006611
Eric Christopher30ef0e52010-06-03 04:07:48 +00006612 // If GV is an alias then use the aliasee for determining
6613 // thread-localness.
6614 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6615 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006616
6617 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006618 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006619
Eric Christopher30ef0e52010-06-03 04:07:48 +00006620 switch (model) {
6621 case TLSModel::GeneralDynamic:
6622 case TLSModel::LocalDynamic: // not implemented
6623 if (Subtarget->is64Bit())
6624 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6625 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006626
Eric Christopher30ef0e52010-06-03 04:07:48 +00006627 case TLSModel::InitialExec:
6628 case TLSModel::LocalExec:
6629 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6630 Subtarget->is64Bit());
6631 }
6632 } else if (Subtarget->isTargetDarwin()) {
6633 // Darwin only has one model of TLS. Lower to that.
6634 unsigned char OpFlag = 0;
6635 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6636 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006637
Eric Christopher30ef0e52010-06-03 04:07:48 +00006638 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6639 // global base reg.
6640 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6641 !Subtarget->is64Bit();
6642 if (PIC32)
6643 OpFlag = X86II::MO_TLVP_PIC_BASE;
6644 else
6645 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006646 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006647 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006648 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006649 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006650 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006651
Eric Christopher30ef0e52010-06-03 04:07:48 +00006652 // With PIC32, the address is actually $g + Offset.
6653 if (PIC32)
6654 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6655 DAG.getNode(X86ISD::GlobalBaseReg,
6656 DebugLoc(), getPointerTy()),
6657 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006658
Eric Christopher30ef0e52010-06-03 04:07:48 +00006659 // Lowering the machine isd will make sure everything is in the right
6660 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006661 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006662 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006663 SDValue Args[] = { Chain, Offset };
6664 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006665
Eric Christopher30ef0e52010-06-03 04:07:48 +00006666 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6667 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6668 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006669
Eric Christopher30ef0e52010-06-03 04:07:48 +00006670 // And our return value (tls address) is in the standard call return value
6671 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006672 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6673 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006674 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006675
Eric Christopher30ef0e52010-06-03 04:07:48 +00006676 assert(false &&
6677 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006678
Torok Edwinc23197a2009-07-14 16:55:14 +00006679 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006680 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006681}
6682
Evan Cheng0db9fe62006-04-25 20:13:52 +00006683
Nadav Rotem43012222011-05-11 08:12:09 +00006684/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006685/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006686SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006687 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006688 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006689 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006690 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006691 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006692 SDValue ShOpLo = Op.getOperand(0);
6693 SDValue ShOpHi = Op.getOperand(1);
6694 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006695 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006696 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006697 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006698
Dan Gohman475871a2008-07-27 21:46:04 +00006699 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006700 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006701 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6702 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006703 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006704 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6705 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006706 }
Evan Chenge3413162006-01-09 18:33:28 +00006707
Owen Anderson825b72b2009-08-11 20:47:22 +00006708 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6709 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006710 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006711 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006712
Dan Gohman475871a2008-07-27 21:46:04 +00006713 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006714 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006715 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6716 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006717
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006718 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006719 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6720 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006721 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006722 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6723 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006724 }
6725
Dan Gohman475871a2008-07-27 21:46:04 +00006726 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006727 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006728}
Evan Chenga3195e82006-01-12 22:54:21 +00006729
Dan Gohmand858e902010-04-17 15:26:15 +00006730SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6731 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006732 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006733
Dale Johannesen0488fb62010-09-30 23:57:10 +00006734 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006735 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006736
Owen Anderson825b72b2009-08-11 20:47:22 +00006737 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006738 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006739
Eli Friedman36df4992009-05-27 00:47:34 +00006740 // These are really Legal; return the operand so the caller accepts it as
6741 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006742 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006743 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006744 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006745 Subtarget->is64Bit()) {
6746 return Op;
6747 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006748
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006749 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006750 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006751 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006752 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006753 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006754 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006755 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006756 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006757 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006758 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6759}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006760
Owen Andersone50ed302009-08-10 22:56:29 +00006761SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006762 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006763 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006764 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006765 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006766 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006767 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006768 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006769 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006770 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006771 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006772
Chris Lattner492a43e2010-09-22 01:28:21 +00006773 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006774
Stuart Hastings84be9582011-06-02 15:57:11 +00006775 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6776 MachineMemOperand *MMO;
6777 if (FI) {
6778 int SSFI = FI->getIndex();
6779 MMO =
6780 DAG.getMachineFunction()
6781 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6782 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6783 } else {
6784 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6785 StackSlot = StackSlot.getOperand(1);
6786 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006787 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006788 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6789 X86ISD::FILD, DL,
6790 Tys, Ops, array_lengthof(Ops),
6791 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006792
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006793 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006794 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006795 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796
6797 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6798 // shouldn't be necessary except that RFP cannot be live across
6799 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006800 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006801 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6802 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006803 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006804 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006805 SDValue Ops[] = {
6806 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6807 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006808 MachineMemOperand *MMO =
6809 DAG.getMachineFunction()
6810 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006811 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006812
Chris Lattner492a43e2010-09-22 01:28:21 +00006813 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6814 Ops, array_lengthof(Ops),
6815 Op.getValueType(), MMO);
6816 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006817 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006818 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006819 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006820
Evan Cheng0db9fe62006-04-25 20:13:52 +00006821 return Result;
6822}
6823
Bill Wendling8b8a6362009-01-17 03:56:04 +00006824// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006825SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6826 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006827 // This algorithm is not obvious. Here it is in C code, more or less:
6828 /*
6829 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6830 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6831 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006832
Bill Wendling8b8a6362009-01-17 03:56:04 +00006833 // Copy ints to xmm registers.
6834 __m128i xh = _mm_cvtsi32_si128( hi );
6835 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006836
Bill Wendling8b8a6362009-01-17 03:56:04 +00006837 // Combine into low half of a single xmm register.
6838 __m128i x = _mm_unpacklo_epi32( xh, xl );
6839 __m128d d;
6840 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006841
Bill Wendling8b8a6362009-01-17 03:56:04 +00006842 // Merge in appropriate exponents to give the integer bits the right
6843 // magnitude.
6844 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006845
Bill Wendling8b8a6362009-01-17 03:56:04 +00006846 // Subtract away the biases to deal with the IEEE-754 double precision
6847 // implicit 1.
6848 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006849
Bill Wendling8b8a6362009-01-17 03:56:04 +00006850 // All conversions up to here are exact. The correctly rounded result is
6851 // calculated using the current rounding mode using the following
6852 // horizontal add.
6853 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6854 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6855 // store doesn't really need to be here (except
6856 // maybe to zero the other double)
6857 return sd;
6858 }
6859 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006860
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006861 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006862 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006863
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006864 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006865 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006866 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6867 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6868 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6869 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006870 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006871 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006872
Bill Wendling8b8a6362009-01-17 03:56:04 +00006873 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006874 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006875 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006876 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006877 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006878 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006879 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006880
Owen Anderson825b72b2009-08-11 20:47:22 +00006881 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6882 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006883 Op.getOperand(0),
6884 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006885 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6886 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006887 Op.getOperand(0),
6888 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006889 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6890 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006891 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006892 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006893 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006894 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006895 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006896 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006897 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006898 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006899
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006900 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006901 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006902 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6903 DAG.getUNDEF(MVT::v2f64), ShufMask);
6904 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6905 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006906 DAG.getIntPtrConstant(0));
6907}
6908
Bill Wendling8b8a6362009-01-17 03:56:04 +00006909// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006910SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6911 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006912 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006913 // FP constant to bias correct the final result.
6914 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006915 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006916
6917 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006918 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6919 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006920 Op.getOperand(0),
6921 DAG.getIntPtrConstant(0)));
6922
Owen Anderson825b72b2009-08-11 20:47:22 +00006923 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006924 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006925 DAG.getIntPtrConstant(0));
6926
6927 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006928 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006929 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006930 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006931 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006932 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006933 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006934 MVT::v2f64, Bias)));
6935 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006936 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006937 DAG.getIntPtrConstant(0));
6938
6939 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006941
6942 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006943 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006944
Owen Anderson825b72b2009-08-11 20:47:22 +00006945 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006946 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006947 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006949 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006950 }
6951
6952 // Handle final rounding.
6953 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006954}
6955
Dan Gohmand858e902010-04-17 15:26:15 +00006956SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6957 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006958 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006959 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006960
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006961 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006962 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6963 // the optimization here.
6964 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006965 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006966
Owen Andersone50ed302009-08-10 22:56:29 +00006967 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006968 EVT DstVT = Op.getValueType();
6969 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006970 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006971 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006972 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006973
6974 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006975 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006976 if (SrcVT == MVT::i32) {
6977 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6978 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6979 getPointerTy(), StackSlot, WordOff);
6980 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006981 StackSlot, MachinePointerInfo(),
6982 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006983 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006984 OffsetSlot, MachinePointerInfo(),
6985 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006986 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6987 return Fild;
6988 }
6989
6990 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6991 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006992 StackSlot, MachinePointerInfo(),
6993 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006994 // For i64 source, we need to add the appropriate power of 2 if the input
6995 // was negative. This is the same as the optimization in
6996 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6997 // we must be careful to do the computation in x87 extended precision, not
6998 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006999 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7000 MachineMemOperand *MMO =
7001 DAG.getMachineFunction()
7002 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7003 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007004
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007005 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7006 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007007 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7008 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007009
7010 APInt FF(32, 0x5F800000ULL);
7011
7012 // Check whether the sign bit is set.
7013 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7014 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7015 ISD::SETLT);
7016
7017 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7018 SDValue FudgePtr = DAG.getConstantPool(
7019 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7020 getPointerTy());
7021
7022 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7023 SDValue Zero = DAG.getIntPtrConstant(0);
7024 SDValue Four = DAG.getIntPtrConstant(4);
7025 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7026 Zero, Four);
7027 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7028
7029 // Load the value out, extending it from f32 to f80.
7030 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007031 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007032 FudgePtr, MachinePointerInfo::getConstantPool(),
7033 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007034 // Extend everything to 80 bits to force it to be done on x87.
7035 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7036 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007037}
7038
Dan Gohman475871a2008-07-27 21:46:04 +00007039std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007040FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007041 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007042
Owen Andersone50ed302009-08-10 22:56:29 +00007043 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007044
7045 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7047 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007048 }
7049
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7051 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007052 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007053
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007054 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007055 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007056 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007057 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007058 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007059 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007060 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007061 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007062
Evan Cheng87c89352007-10-15 20:11:21 +00007063 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7064 // stack slot.
7065 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007066 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007067 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007068 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007069
Michael J. Spencerec38de22010-10-10 22:04:20 +00007070
7071
Evan Cheng0db9fe62006-04-25 20:13:52 +00007072 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007074 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007075 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7076 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7077 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007078 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007079
Dan Gohman475871a2008-07-27 21:46:04 +00007080 SDValue Chain = DAG.getEntryNode();
7081 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007082 EVT TheVT = Op.getOperand(0).getValueType();
7083 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007085 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007086 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007087 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007089 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007090 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007091 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007092
Chris Lattner492a43e2010-09-22 01:28:21 +00007093 MachineMemOperand *MMO =
7094 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7095 MachineMemOperand::MOLoad, MemSize, MemSize);
7096 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7097 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007098 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007099 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007100 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7101 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007102
Chris Lattner07290932010-09-22 01:05:16 +00007103 MachineMemOperand *MMO =
7104 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7105 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007106
Evan Cheng0db9fe62006-04-25 20:13:52 +00007107 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007108 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007109 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7110 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007111
Chris Lattner27a6c732007-11-24 07:07:01 +00007112 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007113}
7114
Dan Gohmand858e902010-04-17 15:26:15 +00007115SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7116 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007117 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007118 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007119
Eli Friedman948e95a2009-05-23 09:59:16 +00007120 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007121 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007122 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7123 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007124
Chris Lattner27a6c732007-11-24 07:07:01 +00007125 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007126 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007127 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007128}
7129
Dan Gohmand858e902010-04-17 15:26:15 +00007130SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7131 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007132 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7133 SDValue FIST = Vals.first, StackSlot = Vals.second;
7134 assert(FIST.getNode() && "Unexpected failure");
7135
7136 // Load the result.
7137 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007138 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007139}
7140
Dan Gohmand858e902010-04-17 15:26:15 +00007141SDValue X86TargetLowering::LowerFABS(SDValue Op,
7142 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007143 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007144 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007145 EVT VT = Op.getValueType();
7146 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007147 if (VT.isVector())
7148 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007150 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007151 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007152 CV.push_back(C);
7153 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007155 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007156 CV.push_back(C);
7157 CV.push_back(C);
7158 CV.push_back(C);
7159 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007161 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007162 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007163 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007164 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007165 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007166 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007167}
7168
Dan Gohmand858e902010-04-17 15:26:15 +00007169SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007170 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007171 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007172 EVT VT = Op.getValueType();
7173 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007174 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007175 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007176 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007177 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007178 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007179 CV.push_back(C);
7180 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007181 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007182 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007183 CV.push_back(C);
7184 CV.push_back(C);
7185 CV.push_back(C);
7186 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007187 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007188 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007189 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007190 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007191 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007192 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007193 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007194 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007195 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007196 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007197 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007198 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007199 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007200 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007201 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007202}
7203
Dan Gohmand858e902010-04-17 15:26:15 +00007204SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007205 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007206 SDValue Op0 = Op.getOperand(0);
7207 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007208 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007209 EVT VT = Op.getValueType();
7210 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007211
7212 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007213 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007214 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007215 SrcVT = VT;
7216 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007217 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007218 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007219 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007220 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007221 }
7222
7223 // At this point the operands and the result should have the same
7224 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007225
Evan Cheng68c47cb2007-01-05 07:55:56 +00007226 // First get the sign bit of second operand.
7227 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007228 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007229 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7230 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007231 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007232 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7233 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7234 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7235 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007236 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007237 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007238 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007239 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007240 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007241 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007242 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007243
7244 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007245 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007246 // Op0 is MVT::f32, Op1 is MVT::f64.
7247 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7248 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7249 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007250 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007251 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007252 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007253 }
7254
Evan Cheng73d6cf12007-01-05 21:37:56 +00007255 // Clear first operand sign bit.
7256 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007257 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007258 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7259 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007260 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007261 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7262 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7263 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7264 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007265 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007266 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007267 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007268 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007269 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007270 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007271 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007272
7273 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007274 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007275}
7276
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007277SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7278 SDValue N0 = Op.getOperand(0);
7279 DebugLoc dl = Op.getDebugLoc();
7280 EVT VT = Op.getValueType();
7281
7282 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7283 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7284 DAG.getConstant(1, VT));
7285 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7286}
7287
Dan Gohman076aee32009-03-04 19:44:21 +00007288/// Emit nodes that will be selected as "test Op0,Op0", or something
7289/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007290SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007291 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007292 DebugLoc dl = Op.getDebugLoc();
7293
Dan Gohman31125812009-03-07 01:58:32 +00007294 // CF and OF aren't always set the way we want. Determine which
7295 // of these we need.
7296 bool NeedCF = false;
7297 bool NeedOF = false;
7298 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007299 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007300 case X86::COND_A: case X86::COND_AE:
7301 case X86::COND_B: case X86::COND_BE:
7302 NeedCF = true;
7303 break;
7304 case X86::COND_G: case X86::COND_GE:
7305 case X86::COND_L: case X86::COND_LE:
7306 case X86::COND_O: case X86::COND_NO:
7307 NeedOF = true;
7308 break;
Dan Gohman31125812009-03-07 01:58:32 +00007309 }
7310
Dan Gohman076aee32009-03-04 19:44:21 +00007311 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007312 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7313 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007314 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7315 // Emit a CMP with 0, which is the TEST pattern.
7316 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7317 DAG.getConstant(0, Op.getValueType()));
7318
7319 unsigned Opcode = 0;
7320 unsigned NumOperands = 0;
7321 switch (Op.getNode()->getOpcode()) {
7322 case ISD::ADD:
7323 // Due to an isel shortcoming, be conservative if this add is likely to be
7324 // selected as part of a load-modify-store instruction. When the root node
7325 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7326 // uses of other nodes in the match, such as the ADD in this case. This
7327 // leads to the ADD being left around and reselected, with the result being
7328 // two adds in the output. Alas, even if none our users are stores, that
7329 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7330 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7331 // climbing the DAG back to the root, and it doesn't seem to be worth the
7332 // effort.
7333 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007334 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007335 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7336 goto default_case;
7337
7338 if (ConstantSDNode *C =
7339 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7340 // An add of one will be selected as an INC.
7341 if (C->getAPIntValue() == 1) {
7342 Opcode = X86ISD::INC;
7343 NumOperands = 1;
7344 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007345 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007346
7347 // An add of negative one (subtract of one) will be selected as a DEC.
7348 if (C->getAPIntValue().isAllOnesValue()) {
7349 Opcode = X86ISD::DEC;
7350 NumOperands = 1;
7351 break;
7352 }
Dan Gohman076aee32009-03-04 19:44:21 +00007353 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007354
7355 // Otherwise use a regular EFLAGS-setting add.
7356 Opcode = X86ISD::ADD;
7357 NumOperands = 2;
7358 break;
7359 case ISD::AND: {
7360 // If the primary and result isn't used, don't bother using X86ISD::AND,
7361 // because a TEST instruction will be better.
7362 bool NonFlagUse = false;
7363 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7364 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7365 SDNode *User = *UI;
7366 unsigned UOpNo = UI.getOperandNo();
7367 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7368 // Look pass truncate.
7369 UOpNo = User->use_begin().getOperandNo();
7370 User = *User->use_begin();
7371 }
7372
7373 if (User->getOpcode() != ISD::BRCOND &&
7374 User->getOpcode() != ISD::SETCC &&
7375 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7376 NonFlagUse = true;
7377 break;
7378 }
Dan Gohman076aee32009-03-04 19:44:21 +00007379 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007380
7381 if (!NonFlagUse)
7382 break;
7383 }
7384 // FALL THROUGH
7385 case ISD::SUB:
7386 case ISD::OR:
7387 case ISD::XOR:
7388 // Due to the ISEL shortcoming noted above, be conservative if this op is
7389 // likely to be selected as part of a load-modify-store instruction.
7390 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7391 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7392 if (UI->getOpcode() == ISD::STORE)
7393 goto default_case;
7394
7395 // Otherwise use a regular EFLAGS-setting instruction.
7396 switch (Op.getNode()->getOpcode()) {
7397 default: llvm_unreachable("unexpected operator!");
7398 case ISD::SUB: Opcode = X86ISD::SUB; break;
7399 case ISD::OR: Opcode = X86ISD::OR; break;
7400 case ISD::XOR: Opcode = X86ISD::XOR; break;
7401 case ISD::AND: Opcode = X86ISD::AND; break;
7402 }
7403
7404 NumOperands = 2;
7405 break;
7406 case X86ISD::ADD:
7407 case X86ISD::SUB:
7408 case X86ISD::INC:
7409 case X86ISD::DEC:
7410 case X86ISD::OR:
7411 case X86ISD::XOR:
7412 case X86ISD::AND:
7413 return SDValue(Op.getNode(), 1);
7414 default:
7415 default_case:
7416 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007417 }
7418
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007419 if (Opcode == 0)
7420 // Emit a CMP with 0, which is the TEST pattern.
7421 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7422 DAG.getConstant(0, Op.getValueType()));
7423
7424 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7425 SmallVector<SDValue, 4> Ops;
7426 for (unsigned i = 0; i != NumOperands; ++i)
7427 Ops.push_back(Op.getOperand(i));
7428
7429 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7430 DAG.ReplaceAllUsesWith(Op, New);
7431 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007432}
7433
7434/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7435/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007436SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007437 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007438 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7439 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007440 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007441
7442 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007443 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007444}
7445
Evan Chengd40d03e2010-01-06 19:38:29 +00007446/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7447/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007448SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7449 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007450 SDValue Op0 = And.getOperand(0);
7451 SDValue Op1 = And.getOperand(1);
7452 if (Op0.getOpcode() == ISD::TRUNCATE)
7453 Op0 = Op0.getOperand(0);
7454 if (Op1.getOpcode() == ISD::TRUNCATE)
7455 Op1 = Op1.getOperand(0);
7456
Evan Chengd40d03e2010-01-06 19:38:29 +00007457 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007458 if (Op1.getOpcode() == ISD::SHL)
7459 std::swap(Op0, Op1);
7460 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007461 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7462 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007463 // If we looked past a truncate, check that it's only truncating away
7464 // known zeros.
7465 unsigned BitWidth = Op0.getValueSizeInBits();
7466 unsigned AndBitWidth = And.getValueSizeInBits();
7467 if (BitWidth > AndBitWidth) {
7468 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7469 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7470 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7471 return SDValue();
7472 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007473 LHS = Op1;
7474 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007475 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007476 } else if (Op1.getOpcode() == ISD::Constant) {
7477 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7478 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007479 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7480 LHS = AndLHS.getOperand(0);
7481 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007482 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007483 }
Evan Cheng0488db92007-09-25 01:57:46 +00007484
Evan Chengd40d03e2010-01-06 19:38:29 +00007485 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007486 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007487 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007488 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007489 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007490 // Also promote i16 to i32 for performance / code size reason.
7491 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007492 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007493 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007494
Evan Chengd40d03e2010-01-06 19:38:29 +00007495 // If the operand types disagree, extend the shift amount to match. Since
7496 // BT ignores high bits (like shifts) we can use anyextend.
7497 if (LHS.getValueType() != RHS.getValueType())
7498 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007499
Evan Chengd40d03e2010-01-06 19:38:29 +00007500 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7501 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7502 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7503 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007504 }
7505
Evan Cheng54de3ea2010-01-05 06:52:31 +00007506 return SDValue();
7507}
7508
Dan Gohmand858e902010-04-17 15:26:15 +00007509SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007510 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7511 SDValue Op0 = Op.getOperand(0);
7512 SDValue Op1 = Op.getOperand(1);
7513 DebugLoc dl = Op.getDebugLoc();
7514 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7515
7516 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007517 // Lower (X & (1 << N)) == 0 to BT(X, N).
7518 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7519 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007520 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007521 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007522 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007523 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7524 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7525 if (NewSetCC.getNode())
7526 return NewSetCC;
7527 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007528
Chris Lattner481eebc2010-12-19 21:23:48 +00007529 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7530 // these.
7531 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007532 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007533 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7534 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007535
Chris Lattner481eebc2010-12-19 21:23:48 +00007536 // If the input is a setcc, then reuse the input setcc or use a new one with
7537 // the inverted condition.
7538 if (Op0.getOpcode() == X86ISD::SETCC) {
7539 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7540 bool Invert = (CC == ISD::SETNE) ^
7541 cast<ConstantSDNode>(Op1)->isNullValue();
7542 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007543
Evan Cheng2c755ba2010-02-27 07:36:59 +00007544 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007545 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7546 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7547 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007548 }
7549
Evan Chenge5b51ac2010-04-17 06:13:15 +00007550 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007551 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007552 if (X86CC == X86::COND_INVALID)
7553 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007554
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007555 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007556 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007557 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007558}
7559
Dan Gohmand858e902010-04-17 15:26:15 +00007560SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007561 SDValue Cond;
7562 SDValue Op0 = Op.getOperand(0);
7563 SDValue Op1 = Op.getOperand(1);
7564 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007565 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007566 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7567 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007568 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007569
7570 if (isFP) {
7571 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007572 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007573 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7574 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007575 bool Swap = false;
7576
7577 switch (SetCCOpcode) {
7578 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007579 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007580 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007581 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007582 case ISD::SETGT: Swap = true; // Fallthrough
7583 case ISD::SETLT:
7584 case ISD::SETOLT: SSECC = 1; break;
7585 case ISD::SETOGE:
7586 case ISD::SETGE: Swap = true; // Fallthrough
7587 case ISD::SETLE:
7588 case ISD::SETOLE: SSECC = 2; break;
7589 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007590 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007591 case ISD::SETNE: SSECC = 4; break;
7592 case ISD::SETULE: Swap = true;
7593 case ISD::SETUGE: SSECC = 5; break;
7594 case ISD::SETULT: Swap = true;
7595 case ISD::SETUGT: SSECC = 6; break;
7596 case ISD::SETO: SSECC = 7; break;
7597 }
7598 if (Swap)
7599 std::swap(Op0, Op1);
7600
Nate Begemanfb8ead02008-07-25 19:05:58 +00007601 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007602 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007603 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007604 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007605 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7606 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007607 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007608 }
7609 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007610 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007611 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7612 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007613 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007614 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007615 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007616 }
7617 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007618 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007619 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007620
Nate Begeman30a0de92008-07-17 16:51:19 +00007621 // We are handling one of the integer comparisons here. Since SSE only has
7622 // GT and EQ comparisons for integer, swapping operands and multiple
7623 // operations may be required for some comparisons.
7624 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7625 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007626
Owen Anderson825b72b2009-08-11 20:47:22 +00007627 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007628 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007629 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007630 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007631 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7632 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007633 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007634
Nate Begeman30a0de92008-07-17 16:51:19 +00007635 switch (SetCCOpcode) {
7636 default: break;
7637 case ISD::SETNE: Invert = true;
7638 case ISD::SETEQ: Opc = EQOpc; break;
7639 case ISD::SETLT: Swap = true;
7640 case ISD::SETGT: Opc = GTOpc; break;
7641 case ISD::SETGE: Swap = true;
7642 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7643 case ISD::SETULT: Swap = true;
7644 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7645 case ISD::SETUGE: Swap = true;
7646 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7647 }
7648 if (Swap)
7649 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007650
Nate Begeman30a0de92008-07-17 16:51:19 +00007651 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7652 // bits of the inputs before performing those operations.
7653 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007654 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007655 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7656 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007657 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007658 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7659 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007660 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7661 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007662 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007663
Dale Johannesenace16102009-02-03 19:33:06 +00007664 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007665
7666 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007667 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007668 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007669
Nate Begeman30a0de92008-07-17 16:51:19 +00007670 return Result;
7671}
Evan Cheng0488db92007-09-25 01:57:46 +00007672
Evan Cheng370e5342008-12-03 08:38:43 +00007673// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007674static bool isX86LogicalCmp(SDValue Op) {
7675 unsigned Opc = Op.getNode()->getOpcode();
7676 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7677 return true;
7678 if (Op.getResNo() == 1 &&
7679 (Opc == X86ISD::ADD ||
7680 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007681 Opc == X86ISD::ADC ||
7682 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007683 Opc == X86ISD::SMUL ||
7684 Opc == X86ISD::UMUL ||
7685 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007686 Opc == X86ISD::DEC ||
7687 Opc == X86ISD::OR ||
7688 Opc == X86ISD::XOR ||
7689 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007690 return true;
7691
Chris Lattner9637d5b2010-12-05 07:49:54 +00007692 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7693 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007694
Dan Gohman076aee32009-03-04 19:44:21 +00007695 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007696}
7697
Chris Lattnera2b56002010-12-05 01:23:24 +00007698static bool isZero(SDValue V) {
7699 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7700 return C && C->isNullValue();
7701}
7702
Chris Lattner96908b12010-12-05 02:00:51 +00007703static bool isAllOnes(SDValue V) {
7704 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7705 return C && C->isAllOnesValue();
7706}
7707
Dan Gohmand858e902010-04-17 15:26:15 +00007708SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007709 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007710 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007711 SDValue Op1 = Op.getOperand(1);
7712 SDValue Op2 = Op.getOperand(2);
7713 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007714 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007715
Dan Gohman1a492952009-10-20 16:22:37 +00007716 if (Cond.getOpcode() == ISD::SETCC) {
7717 SDValue NewCond = LowerSETCC(Cond, DAG);
7718 if (NewCond.getNode())
7719 Cond = NewCond;
7720 }
Evan Cheng734503b2006-09-11 02:19:56 +00007721
Chris Lattnera2b56002010-12-05 01:23:24 +00007722 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007723 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007724 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007725 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007726 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007727 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7728 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007729 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007730
Chris Lattnera2b56002010-12-05 01:23:24 +00007731 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007732
7733 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007734 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7735 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007736
7737 SDValue CmpOp0 = Cmp.getOperand(0);
7738 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7739 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007740
Chris Lattner96908b12010-12-05 02:00:51 +00007741 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007742 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7743 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007744
Chris Lattner96908b12010-12-05 02:00:51 +00007745 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7746 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007747
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007748 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007749 if (N2C == 0 || !N2C->isNullValue())
7750 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7751 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007752 }
7753 }
7754
Chris Lattnera2b56002010-12-05 01:23:24 +00007755 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007756 if (Cond.getOpcode() == ISD::AND &&
7757 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7758 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007759 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007760 Cond = Cond.getOperand(0);
7761 }
7762
Evan Cheng3f41d662007-10-08 22:16:29 +00007763 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7764 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007765 if (Cond.getOpcode() == X86ISD::SETCC ||
7766 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007767 CC = Cond.getOperand(0);
7768
Dan Gohman475871a2008-07-27 21:46:04 +00007769 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007770 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007771 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007772
Evan Cheng3f41d662007-10-08 22:16:29 +00007773 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007774 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007775 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007776 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007777
Chris Lattnerd1980a52009-03-12 06:52:53 +00007778 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7779 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007780 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007781 addTest = false;
7782 }
7783 }
7784
7785 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007786 // Look pass the truncate.
7787 if (Cond.getOpcode() == ISD::TRUNCATE)
7788 Cond = Cond.getOperand(0);
7789
7790 // We know the result of AND is compared against zero. Try to match
7791 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007792 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007793 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007794 if (NewSetCC.getNode()) {
7795 CC = NewSetCC.getOperand(0);
7796 Cond = NewSetCC.getOperand(1);
7797 addTest = false;
7798 }
7799 }
7800 }
7801
7802 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007803 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007804 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007805 }
7806
Benjamin Kramere915ff32010-12-22 23:09:28 +00007807 // a < b ? -1 : 0 -> RES = ~setcc_carry
7808 // a < b ? 0 : -1 -> RES = setcc_carry
7809 // a >= b ? -1 : 0 -> RES = setcc_carry
7810 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7811 if (Cond.getOpcode() == X86ISD::CMP) {
7812 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7813
7814 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7815 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7816 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7817 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7818 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7819 return DAG.getNOT(DL, Res, Res.getValueType());
7820 return Res;
7821 }
7822 }
7823
Evan Cheng0488db92007-09-25 01:57:46 +00007824 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7825 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007826 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007827 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007828 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007829}
7830
Evan Cheng370e5342008-12-03 08:38:43 +00007831// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7832// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7833// from the AND / OR.
7834static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7835 Opc = Op.getOpcode();
7836 if (Opc != ISD::OR && Opc != ISD::AND)
7837 return false;
7838 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7839 Op.getOperand(0).hasOneUse() &&
7840 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7841 Op.getOperand(1).hasOneUse());
7842}
7843
Evan Cheng961d6d42009-02-02 08:19:07 +00007844// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7845// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007846static bool isXor1OfSetCC(SDValue Op) {
7847 if (Op.getOpcode() != ISD::XOR)
7848 return false;
7849 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7850 if (N1C && N1C->getAPIntValue() == 1) {
7851 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7852 Op.getOperand(0).hasOneUse();
7853 }
7854 return false;
7855}
7856
Dan Gohmand858e902010-04-17 15:26:15 +00007857SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007858 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007859 SDValue Chain = Op.getOperand(0);
7860 SDValue Cond = Op.getOperand(1);
7861 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007862 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007863 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007864
Dan Gohman1a492952009-10-20 16:22:37 +00007865 if (Cond.getOpcode() == ISD::SETCC) {
7866 SDValue NewCond = LowerSETCC(Cond, DAG);
7867 if (NewCond.getNode())
7868 Cond = NewCond;
7869 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007870#if 0
7871 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007872 else if (Cond.getOpcode() == X86ISD::ADD ||
7873 Cond.getOpcode() == X86ISD::SUB ||
7874 Cond.getOpcode() == X86ISD::SMUL ||
7875 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007876 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007877#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007878
Evan Chengad9c0a32009-12-15 00:53:42 +00007879 // Look pass (and (setcc_carry (cmp ...)), 1).
7880 if (Cond.getOpcode() == ISD::AND &&
7881 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7882 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007883 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007884 Cond = Cond.getOperand(0);
7885 }
7886
Evan Cheng3f41d662007-10-08 22:16:29 +00007887 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7888 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007889 if (Cond.getOpcode() == X86ISD::SETCC ||
7890 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007891 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007892
Dan Gohman475871a2008-07-27 21:46:04 +00007893 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007894 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007895 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007896 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007897 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007898 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007899 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007900 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007901 default: break;
7902 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007903 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007904 // These can only come from an arithmetic instruction with overflow,
7905 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007906 Cond = Cond.getNode()->getOperand(1);
7907 addTest = false;
7908 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007909 }
Evan Cheng0488db92007-09-25 01:57:46 +00007910 }
Evan Cheng370e5342008-12-03 08:38:43 +00007911 } else {
7912 unsigned CondOpc;
7913 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7914 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007915 if (CondOpc == ISD::OR) {
7916 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7917 // two branches instead of an explicit OR instruction with a
7918 // separate test.
7919 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007920 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007921 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007922 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007923 Chain, Dest, CC, Cmp);
7924 CC = Cond.getOperand(1).getOperand(0);
7925 Cond = Cmp;
7926 addTest = false;
7927 }
7928 } else { // ISD::AND
7929 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7930 // two branches instead of an explicit AND instruction with a
7931 // separate test. However, we only do this if this block doesn't
7932 // have a fall-through edge, because this requires an explicit
7933 // jmp when the condition is false.
7934 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007935 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007936 Op.getNode()->hasOneUse()) {
7937 X86::CondCode CCode =
7938 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7939 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007940 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007941 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007942 // Look for an unconditional branch following this conditional branch.
7943 // We need this because we need to reverse the successors in order
7944 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007945 if (User->getOpcode() == ISD::BR) {
7946 SDValue FalseBB = User->getOperand(1);
7947 SDNode *NewBR =
7948 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007949 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007950 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007951 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007952
Dale Johannesene4d209d2009-02-03 20:21:25 +00007953 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007954 Chain, Dest, CC, Cmp);
7955 X86::CondCode CCode =
7956 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7957 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007958 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007959 Cond = Cmp;
7960 addTest = false;
7961 }
7962 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007963 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007964 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7965 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7966 // It should be transformed during dag combiner except when the condition
7967 // is set by a arithmetics with overflow node.
7968 X86::CondCode CCode =
7969 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7970 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007971 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007972 Cond = Cond.getOperand(0).getOperand(1);
7973 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007974 }
Evan Cheng0488db92007-09-25 01:57:46 +00007975 }
7976
7977 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007978 // Look pass the truncate.
7979 if (Cond.getOpcode() == ISD::TRUNCATE)
7980 Cond = Cond.getOperand(0);
7981
7982 // We know the result of AND is compared against zero. Try to match
7983 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007984 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007985 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7986 if (NewSetCC.getNode()) {
7987 CC = NewSetCC.getOperand(0);
7988 Cond = NewSetCC.getOperand(1);
7989 addTest = false;
7990 }
7991 }
7992 }
7993
7994 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007995 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007996 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007997 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007998 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007999 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008000}
8001
Anton Korobeynikove060b532007-04-17 19:34:00 +00008002
8003// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8004// Calls to _alloca is needed to probe the stack when allocating more than 4k
8005// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8006// that the guard pages used by the OS virtual memory manager are allocated in
8007// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008008SDValue
8009X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008010 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00008011 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008012 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008013 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008014 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008015
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008016 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008017 SDValue Chain = Op.getOperand(0);
8018 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008019 // FIXME: Ensure alignment here
8020
Dan Gohman475871a2008-07-27 21:46:04 +00008021 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008022
Owen Anderson825b72b2009-08-11 20:47:22 +00008023 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008024 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008025
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008026 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008027 Flag = Chain.getValue(1);
8028
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008029 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008030
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008031 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008032 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008033
Dale Johannesendd64c412009-02-04 00:33:20 +00008034 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008035
Dan Gohman475871a2008-07-27 21:46:04 +00008036 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008037 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008038}
8039
Dan Gohmand858e902010-04-17 15:26:15 +00008040SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008041 MachineFunction &MF = DAG.getMachineFunction();
8042 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8043
Dan Gohman69de1932008-02-06 22:27:42 +00008044 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008045 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008046
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008047 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008048 // vastart just stores the address of the VarArgsFrameIndex slot into the
8049 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008050 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8051 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008052 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8053 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008054 }
8055
8056 // __va_list_tag:
8057 // gp_offset (0 - 6 * 8)
8058 // fp_offset (48 - 48 + 8 * 16)
8059 // overflow_arg_area (point to parameters coming in memory).
8060 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008061 SmallVector<SDValue, 8> MemOps;
8062 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008063 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008064 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008065 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8066 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008067 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008068 MemOps.push_back(Store);
8069
8070 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008071 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008072 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008073 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008074 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8075 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008076 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008077 MemOps.push_back(Store);
8078
8079 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008080 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008081 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008082 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8083 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008084 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8085 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008086 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008087 MemOps.push_back(Store);
8088
8089 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008090 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008091 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008092 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8093 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008094 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8095 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008096 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008097 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008098 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008099}
8100
Dan Gohmand858e902010-04-17 15:26:15 +00008101SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008102 assert(Subtarget->is64Bit() &&
8103 "LowerVAARG only handles 64-bit va_arg!");
8104 assert((Subtarget->isTargetLinux() ||
8105 Subtarget->isTargetDarwin()) &&
8106 "Unhandled target in LowerVAARG");
8107 assert(Op.getNode()->getNumOperands() == 4);
8108 SDValue Chain = Op.getOperand(0);
8109 SDValue SrcPtr = Op.getOperand(1);
8110 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8111 unsigned Align = Op.getConstantOperandVal(3);
8112 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008113
Dan Gohman320afb82010-10-12 18:00:49 +00008114 EVT ArgVT = Op.getNode()->getValueType(0);
8115 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8116 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8117 uint8_t ArgMode;
8118
8119 // Decide which area this value should be read from.
8120 // TODO: Implement the AMD64 ABI in its entirety. This simple
8121 // selection mechanism works only for the basic types.
8122 if (ArgVT == MVT::f80) {
8123 llvm_unreachable("va_arg for f80 not yet implemented");
8124 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8125 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8126 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8127 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8128 } else {
8129 llvm_unreachable("Unhandled argument type in LowerVAARG");
8130 }
8131
8132 if (ArgMode == 2) {
8133 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008134 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008135 !(DAG.getMachineFunction()
8136 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008137 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008138 }
8139
8140 // Insert VAARG_64 node into the DAG
8141 // VAARG_64 returns two values: Variable Argument Address, Chain
8142 SmallVector<SDValue, 11> InstOps;
8143 InstOps.push_back(Chain);
8144 InstOps.push_back(SrcPtr);
8145 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8146 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8147 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8148 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8149 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8150 VTs, &InstOps[0], InstOps.size(),
8151 MVT::i64,
8152 MachinePointerInfo(SV),
8153 /*Align=*/0,
8154 /*Volatile=*/false,
8155 /*ReadMem=*/true,
8156 /*WriteMem=*/true);
8157 Chain = VAARG.getValue(1);
8158
8159 // Load the next argument and return it
8160 return DAG.getLoad(ArgVT, dl,
8161 Chain,
8162 VAARG,
8163 MachinePointerInfo(),
8164 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008165}
8166
Dan Gohmand858e902010-04-17 15:26:15 +00008167SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008168 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008169 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008170 SDValue Chain = Op.getOperand(0);
8171 SDValue DstPtr = Op.getOperand(1);
8172 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008173 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8174 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008175 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008176
Chris Lattnere72f2022010-09-21 05:40:29 +00008177 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008178 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008179 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008180 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008181}
8182
Dan Gohman475871a2008-07-27 21:46:04 +00008183SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008184X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008185 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008186 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008187 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008188 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008189 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008190 case Intrinsic::x86_sse_comieq_ss:
8191 case Intrinsic::x86_sse_comilt_ss:
8192 case Intrinsic::x86_sse_comile_ss:
8193 case Intrinsic::x86_sse_comigt_ss:
8194 case Intrinsic::x86_sse_comige_ss:
8195 case Intrinsic::x86_sse_comineq_ss:
8196 case Intrinsic::x86_sse_ucomieq_ss:
8197 case Intrinsic::x86_sse_ucomilt_ss:
8198 case Intrinsic::x86_sse_ucomile_ss:
8199 case Intrinsic::x86_sse_ucomigt_ss:
8200 case Intrinsic::x86_sse_ucomige_ss:
8201 case Intrinsic::x86_sse_ucomineq_ss:
8202 case Intrinsic::x86_sse2_comieq_sd:
8203 case Intrinsic::x86_sse2_comilt_sd:
8204 case Intrinsic::x86_sse2_comile_sd:
8205 case Intrinsic::x86_sse2_comigt_sd:
8206 case Intrinsic::x86_sse2_comige_sd:
8207 case Intrinsic::x86_sse2_comineq_sd:
8208 case Intrinsic::x86_sse2_ucomieq_sd:
8209 case Intrinsic::x86_sse2_ucomilt_sd:
8210 case Intrinsic::x86_sse2_ucomile_sd:
8211 case Intrinsic::x86_sse2_ucomigt_sd:
8212 case Intrinsic::x86_sse2_ucomige_sd:
8213 case Intrinsic::x86_sse2_ucomineq_sd: {
8214 unsigned Opc = 0;
8215 ISD::CondCode CC = ISD::SETCC_INVALID;
8216 switch (IntNo) {
8217 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008218 case Intrinsic::x86_sse_comieq_ss:
8219 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008220 Opc = X86ISD::COMI;
8221 CC = ISD::SETEQ;
8222 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008223 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008224 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008225 Opc = X86ISD::COMI;
8226 CC = ISD::SETLT;
8227 break;
8228 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008229 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008230 Opc = X86ISD::COMI;
8231 CC = ISD::SETLE;
8232 break;
8233 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008234 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008235 Opc = X86ISD::COMI;
8236 CC = ISD::SETGT;
8237 break;
8238 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008239 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008240 Opc = X86ISD::COMI;
8241 CC = ISD::SETGE;
8242 break;
8243 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008244 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008245 Opc = X86ISD::COMI;
8246 CC = ISD::SETNE;
8247 break;
8248 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008249 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008250 Opc = X86ISD::UCOMI;
8251 CC = ISD::SETEQ;
8252 break;
8253 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008254 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008255 Opc = X86ISD::UCOMI;
8256 CC = ISD::SETLT;
8257 break;
8258 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008259 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008260 Opc = X86ISD::UCOMI;
8261 CC = ISD::SETLE;
8262 break;
8263 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008264 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008265 Opc = X86ISD::UCOMI;
8266 CC = ISD::SETGT;
8267 break;
8268 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008269 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008270 Opc = X86ISD::UCOMI;
8271 CC = ISD::SETGE;
8272 break;
8273 case Intrinsic::x86_sse_ucomineq_ss:
8274 case Intrinsic::x86_sse2_ucomineq_sd:
8275 Opc = X86ISD::UCOMI;
8276 CC = ISD::SETNE;
8277 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008278 }
Evan Cheng734503b2006-09-11 02:19:56 +00008279
Dan Gohman475871a2008-07-27 21:46:04 +00008280 SDValue LHS = Op.getOperand(1);
8281 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008282 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008283 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008284 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8285 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8286 DAG.getConstant(X86CC, MVT::i8), Cond);
8287 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008288 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008289 // ptest and testp intrinsics. The intrinsic these come from are designed to
8290 // return an integer value, not just an instruction so lower it to the ptest
8291 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008292 case Intrinsic::x86_sse41_ptestz:
8293 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008294 case Intrinsic::x86_sse41_ptestnzc:
8295 case Intrinsic::x86_avx_ptestz_256:
8296 case Intrinsic::x86_avx_ptestc_256:
8297 case Intrinsic::x86_avx_ptestnzc_256:
8298 case Intrinsic::x86_avx_vtestz_ps:
8299 case Intrinsic::x86_avx_vtestc_ps:
8300 case Intrinsic::x86_avx_vtestnzc_ps:
8301 case Intrinsic::x86_avx_vtestz_pd:
8302 case Intrinsic::x86_avx_vtestc_pd:
8303 case Intrinsic::x86_avx_vtestnzc_pd:
8304 case Intrinsic::x86_avx_vtestz_ps_256:
8305 case Intrinsic::x86_avx_vtestc_ps_256:
8306 case Intrinsic::x86_avx_vtestnzc_ps_256:
8307 case Intrinsic::x86_avx_vtestz_pd_256:
8308 case Intrinsic::x86_avx_vtestc_pd_256:
8309 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8310 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008311 unsigned X86CC = 0;
8312 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008313 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008314 case Intrinsic::x86_avx_vtestz_ps:
8315 case Intrinsic::x86_avx_vtestz_pd:
8316 case Intrinsic::x86_avx_vtestz_ps_256:
8317 case Intrinsic::x86_avx_vtestz_pd_256:
8318 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008319 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008320 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008321 // ZF = 1
8322 X86CC = X86::COND_E;
8323 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008324 case Intrinsic::x86_avx_vtestc_ps:
8325 case Intrinsic::x86_avx_vtestc_pd:
8326 case Intrinsic::x86_avx_vtestc_ps_256:
8327 case Intrinsic::x86_avx_vtestc_pd_256:
8328 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008329 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008330 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008331 // CF = 1
8332 X86CC = X86::COND_B;
8333 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008334 case Intrinsic::x86_avx_vtestnzc_ps:
8335 case Intrinsic::x86_avx_vtestnzc_pd:
8336 case Intrinsic::x86_avx_vtestnzc_ps_256:
8337 case Intrinsic::x86_avx_vtestnzc_pd_256:
8338 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008339 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008340 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008341 // ZF and CF = 0
8342 X86CC = X86::COND_A;
8343 break;
8344 }
Eric Christopherfd179292009-08-27 18:07:15 +00008345
Eric Christopher71c67532009-07-29 00:28:05 +00008346 SDValue LHS = Op.getOperand(1);
8347 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008348 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8349 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008350 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8351 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8352 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008353 }
Evan Cheng5759f972008-05-04 09:15:50 +00008354
8355 // Fix vector shift instructions where the last operand is a non-immediate
8356 // i32 value.
8357 case Intrinsic::x86_sse2_pslli_w:
8358 case Intrinsic::x86_sse2_pslli_d:
8359 case Intrinsic::x86_sse2_pslli_q:
8360 case Intrinsic::x86_sse2_psrli_w:
8361 case Intrinsic::x86_sse2_psrli_d:
8362 case Intrinsic::x86_sse2_psrli_q:
8363 case Intrinsic::x86_sse2_psrai_w:
8364 case Intrinsic::x86_sse2_psrai_d:
8365 case Intrinsic::x86_mmx_pslli_w:
8366 case Intrinsic::x86_mmx_pslli_d:
8367 case Intrinsic::x86_mmx_pslli_q:
8368 case Intrinsic::x86_mmx_psrli_w:
8369 case Intrinsic::x86_mmx_psrli_d:
8370 case Intrinsic::x86_mmx_psrli_q:
8371 case Intrinsic::x86_mmx_psrai_w:
8372 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008373 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008374 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008375 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008376
8377 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008378 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008379 switch (IntNo) {
8380 case Intrinsic::x86_sse2_pslli_w:
8381 NewIntNo = Intrinsic::x86_sse2_psll_w;
8382 break;
8383 case Intrinsic::x86_sse2_pslli_d:
8384 NewIntNo = Intrinsic::x86_sse2_psll_d;
8385 break;
8386 case Intrinsic::x86_sse2_pslli_q:
8387 NewIntNo = Intrinsic::x86_sse2_psll_q;
8388 break;
8389 case Intrinsic::x86_sse2_psrli_w:
8390 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8391 break;
8392 case Intrinsic::x86_sse2_psrli_d:
8393 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8394 break;
8395 case Intrinsic::x86_sse2_psrli_q:
8396 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8397 break;
8398 case Intrinsic::x86_sse2_psrai_w:
8399 NewIntNo = Intrinsic::x86_sse2_psra_w;
8400 break;
8401 case Intrinsic::x86_sse2_psrai_d:
8402 NewIntNo = Intrinsic::x86_sse2_psra_d;
8403 break;
8404 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008405 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008406 switch (IntNo) {
8407 case Intrinsic::x86_mmx_pslli_w:
8408 NewIntNo = Intrinsic::x86_mmx_psll_w;
8409 break;
8410 case Intrinsic::x86_mmx_pslli_d:
8411 NewIntNo = Intrinsic::x86_mmx_psll_d;
8412 break;
8413 case Intrinsic::x86_mmx_pslli_q:
8414 NewIntNo = Intrinsic::x86_mmx_psll_q;
8415 break;
8416 case Intrinsic::x86_mmx_psrli_w:
8417 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8418 break;
8419 case Intrinsic::x86_mmx_psrli_d:
8420 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8421 break;
8422 case Intrinsic::x86_mmx_psrli_q:
8423 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8424 break;
8425 case Intrinsic::x86_mmx_psrai_w:
8426 NewIntNo = Intrinsic::x86_mmx_psra_w;
8427 break;
8428 case Intrinsic::x86_mmx_psrai_d:
8429 NewIntNo = Intrinsic::x86_mmx_psra_d;
8430 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008431 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008432 }
8433 break;
8434 }
8435 }
Mon P Wangefa42202009-09-03 19:56:25 +00008436
8437 // The vector shift intrinsics with scalars uses 32b shift amounts but
8438 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8439 // to be zero.
8440 SDValue ShOps[4];
8441 ShOps[0] = ShAmt;
8442 ShOps[1] = DAG.getConstant(0, MVT::i32);
8443 if (ShAmtVT == MVT::v4i32) {
8444 ShOps[2] = DAG.getUNDEF(MVT::i32);
8445 ShOps[3] = DAG.getUNDEF(MVT::i32);
8446 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8447 } else {
8448 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008449// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008450 }
8451
Owen Andersone50ed302009-08-10 22:56:29 +00008452 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008453 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008454 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008455 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008456 Op.getOperand(1), ShAmt);
8457 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008458 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008459}
Evan Cheng72261582005-12-20 06:22:03 +00008460
Dan Gohmand858e902010-04-17 15:26:15 +00008461SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8462 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008463 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8464 MFI->setReturnAddressIsTaken(true);
8465
Bill Wendling64e87322009-01-16 19:25:27 +00008466 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008467 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008468
8469 if (Depth > 0) {
8470 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8471 SDValue Offset =
8472 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008473 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008474 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008475 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008476 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008477 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008478 }
8479
8480 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008481 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008482 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008483 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008484}
8485
Dan Gohmand858e902010-04-17 15:26:15 +00008486SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008487 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8488 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008489
Owen Andersone50ed302009-08-10 22:56:29 +00008490 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008491 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008492 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8493 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008494 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008495 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008496 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8497 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008498 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008499 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008500}
8501
Dan Gohman475871a2008-07-27 21:46:04 +00008502SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008503 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008504 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008505}
8506
Dan Gohmand858e902010-04-17 15:26:15 +00008507SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008508 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008509 SDValue Chain = Op.getOperand(0);
8510 SDValue Offset = Op.getOperand(1);
8511 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008512 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008513
Dan Gohmand8816272010-08-11 18:14:00 +00008514 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8515 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8516 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008517 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008518
Dan Gohmand8816272010-08-11 18:14:00 +00008519 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8520 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008521 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008522 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8523 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008524 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008525 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008526
Dale Johannesene4d209d2009-02-03 20:21:25 +00008527 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008528 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008529 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008530}
8531
Dan Gohman475871a2008-07-27 21:46:04 +00008532SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008533 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008534 SDValue Root = Op.getOperand(0);
8535 SDValue Trmp = Op.getOperand(1); // trampoline
8536 SDValue FPtr = Op.getOperand(2); // nested function
8537 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008538 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008539
Dan Gohman69de1932008-02-06 22:27:42 +00008540 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008541
8542 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008543 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008544
8545 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008546 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8547 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008548
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008549 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8550 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008551
8552 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8553
8554 // Load the pointer to the nested function into R11.
8555 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008556 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008557 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008558 Addr, MachinePointerInfo(TrmpAddr),
8559 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008560
Owen Anderson825b72b2009-08-11 20:47:22 +00008561 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8562 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008563 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8564 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008565 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008566
8567 // Load the 'nest' parameter value into R10.
8568 // R10 is specified in X86CallingConv.td
8569 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008570 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8571 DAG.getConstant(10, MVT::i64));
8572 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008573 Addr, MachinePointerInfo(TrmpAddr, 10),
8574 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008575
Owen Anderson825b72b2009-08-11 20:47:22 +00008576 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8577 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008578 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8579 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008580 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008581
8582 // Jump to the nested function.
8583 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008584 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8585 DAG.getConstant(20, MVT::i64));
8586 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008587 Addr, MachinePointerInfo(TrmpAddr, 20),
8588 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008589
8590 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8592 DAG.getConstant(22, MVT::i64));
8593 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008594 MachinePointerInfo(TrmpAddr, 22),
8595 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008596
Dan Gohman475871a2008-07-27 21:46:04 +00008597 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008598 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008599 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008600 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008601 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008602 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008603 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008604 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008605
8606 switch (CC) {
8607 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008608 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008609 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008610 case CallingConv::X86_StdCall: {
8611 // Pass 'nest' parameter in ECX.
8612 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008613 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008614
8615 // Check that ECX wasn't needed by an 'inreg' parameter.
8616 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008617 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008618
Chris Lattner58d74912008-03-12 17:45:29 +00008619 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008620 unsigned InRegCount = 0;
8621 unsigned Idx = 1;
8622
8623 for (FunctionType::param_iterator I = FTy->param_begin(),
8624 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008625 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008626 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008627 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008628
8629 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008630 report_fatal_error("Nest register in use - reduce number of inreg"
8631 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008632 }
8633 }
8634 break;
8635 }
8636 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008637 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008638 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008639 // Pass 'nest' parameter in EAX.
8640 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008641 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008642 break;
8643 }
8644
Dan Gohman475871a2008-07-27 21:46:04 +00008645 SDValue OutChains[4];
8646 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008647
Owen Anderson825b72b2009-08-11 20:47:22 +00008648 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8649 DAG.getConstant(10, MVT::i32));
8650 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008651
Chris Lattnera62fe662010-02-05 19:20:30 +00008652 // This is storing the opcode for MOV32ri.
8653 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008654 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008655 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008656 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008657 Trmp, MachinePointerInfo(TrmpAddr),
8658 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008659
Owen Anderson825b72b2009-08-11 20:47:22 +00008660 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8661 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008662 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8663 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008664 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008665
Chris Lattnera62fe662010-02-05 19:20:30 +00008666 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008667 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8668 DAG.getConstant(5, MVT::i32));
8669 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008670 MachinePointerInfo(TrmpAddr, 5),
8671 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008672
Owen Anderson825b72b2009-08-11 20:47:22 +00008673 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8674 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008675 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8676 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008677 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008678
Dan Gohman475871a2008-07-27 21:46:04 +00008679 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008680 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008681 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008682 }
8683}
8684
Dan Gohmand858e902010-04-17 15:26:15 +00008685SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8686 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008687 /*
8688 The rounding mode is in bits 11:10 of FPSR, and has the following
8689 settings:
8690 00 Round to nearest
8691 01 Round to -inf
8692 10 Round to +inf
8693 11 Round to 0
8694
8695 FLT_ROUNDS, on the other hand, expects the following:
8696 -1 Undefined
8697 0 Round to 0
8698 1 Round to nearest
8699 2 Round to +inf
8700 3 Round to -inf
8701
8702 To perform the conversion, we do:
8703 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8704 */
8705
8706 MachineFunction &MF = DAG.getMachineFunction();
8707 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008708 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008709 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008710 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008711 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008712
8713 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008714 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008715 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008716
Michael J. Spencerec38de22010-10-10 22:04:20 +00008717
Chris Lattner2156b792010-09-22 01:11:26 +00008718 MachineMemOperand *MMO =
8719 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8720 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008721
Chris Lattner2156b792010-09-22 01:11:26 +00008722 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8723 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8724 DAG.getVTList(MVT::Other),
8725 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008726
8727 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008728 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008729 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008730
8731 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008732 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008733 DAG.getNode(ISD::SRL, DL, MVT::i16,
8734 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008735 CWD, DAG.getConstant(0x800, MVT::i16)),
8736 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008737 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008738 DAG.getNode(ISD::SRL, DL, MVT::i16,
8739 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008740 CWD, DAG.getConstant(0x400, MVT::i16)),
8741 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008742
Dan Gohman475871a2008-07-27 21:46:04 +00008743 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008744 DAG.getNode(ISD::AND, DL, MVT::i16,
8745 DAG.getNode(ISD::ADD, DL, MVT::i16,
8746 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008747 DAG.getConstant(1, MVT::i16)),
8748 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008749
8750
Duncan Sands83ec4b62008-06-06 12:08:01 +00008751 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008752 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008753}
8754
Dan Gohmand858e902010-04-17 15:26:15 +00008755SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008756 EVT VT = Op.getValueType();
8757 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008758 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008759 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008760
8761 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008762 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008763 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008764 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008765 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008766 }
Evan Cheng18efe262007-12-14 02:13:44 +00008767
Evan Cheng152804e2007-12-14 08:30:15 +00008768 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008770 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008771
8772 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008773 SDValue Ops[] = {
8774 Op,
8775 DAG.getConstant(NumBits+NumBits-1, OpVT),
8776 DAG.getConstant(X86::COND_E, MVT::i8),
8777 Op.getValue(1)
8778 };
8779 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008780
8781 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008782 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008783
Owen Anderson825b72b2009-08-11 20:47:22 +00008784 if (VT == MVT::i8)
8785 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008786 return Op;
8787}
8788
Dan Gohmand858e902010-04-17 15:26:15 +00008789SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008790 EVT VT = Op.getValueType();
8791 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008792 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008793 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008794
8795 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008796 if (VT == MVT::i8) {
8797 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008798 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008799 }
Evan Cheng152804e2007-12-14 08:30:15 +00008800
8801 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008802 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008803 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008804
8805 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008806 SDValue Ops[] = {
8807 Op,
8808 DAG.getConstant(NumBits, OpVT),
8809 DAG.getConstant(X86::COND_E, MVT::i8),
8810 Op.getValue(1)
8811 };
8812 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008813
Owen Anderson825b72b2009-08-11 20:47:22 +00008814 if (VT == MVT::i8)
8815 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008816 return Op;
8817}
8818
Dan Gohmand858e902010-04-17 15:26:15 +00008819SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008820 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008821 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008822 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008823
Mon P Wangaf9b9522008-12-18 21:42:19 +00008824 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8825 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8826 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8827 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8828 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8829 //
8830 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8831 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8832 // return AloBlo + AloBhi + AhiBlo;
8833
8834 SDValue A = Op.getOperand(0);
8835 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008836
Dale Johannesene4d209d2009-02-03 20:21:25 +00008837 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008838 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8839 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008840 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008841 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8842 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008843 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008844 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008845 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008846 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008847 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008848 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008849 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008850 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008851 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008852 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008853 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8854 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008855 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008856 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8857 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008858 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8859 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008860 return Res;
8861}
8862
Nadav Rotem43012222011-05-11 08:12:09 +00008863SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8864
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008865 EVT VT = Op.getValueType();
8866 DebugLoc dl = Op.getDebugLoc();
8867 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008868 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008869
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008870 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008871
Nadav Rotem43012222011-05-11 08:12:09 +00008872 // Must have SSE2.
8873 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008874
Nadav Rotem43012222011-05-11 08:12:09 +00008875 // Optimize shl/srl/sra with constant shift amount.
8876 if (isSplatVector(Amt.getNode())) {
8877 SDValue SclrAmt = Amt->getOperand(0);
8878 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8879 uint64_t ShiftAmt = C->getZExtValue();
8880
8881 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8882 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8883 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8884 R, DAG.getConstant(ShiftAmt, MVT::i32));
8885
8886 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8887 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8888 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8889 R, DAG.getConstant(ShiftAmt, MVT::i32));
8890
8891 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8892 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8893 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8894 R, DAG.getConstant(ShiftAmt, MVT::i32));
8895
8896 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8897 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8898 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8899 R, DAG.getConstant(ShiftAmt, MVT::i32));
8900
8901 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8902 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8903 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8904 R, DAG.getConstant(ShiftAmt, MVT::i32));
8905
8906 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8907 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8908 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8909 R, DAG.getConstant(ShiftAmt, MVT::i32));
8910
8911 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8912 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8913 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8914 R, DAG.getConstant(ShiftAmt, MVT::i32));
8915
8916 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8917 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8918 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8919 R, DAG.getConstant(ShiftAmt, MVT::i32));
8920 }
8921 }
8922
8923 // Lower SHL with variable shift amount.
8924 // Cannot lower SHL without SSE4.1 or later.
8925 if (!Subtarget->hasSSE41()) return SDValue();
8926
8927 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008928 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8929 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8930 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8931
8932 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008933
Nate Begeman51409212010-07-28 00:21:48 +00008934 std::vector<Constant*> CV(4, CI);
8935 Constant *C = ConstantVector::get(CV);
8936 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8937 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008938 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008939 false, false, 16);
8940
8941 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008942 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008943 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8944 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8945 }
Nadav Rotem43012222011-05-11 08:12:09 +00008946 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008947 // a = a << 5;
8948 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8949 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8950 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8951
8952 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8953 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8954
8955 std::vector<Constant*> CVM1(16, CM1);
8956 std::vector<Constant*> CVM2(16, CM2);
8957 Constant *C = ConstantVector::get(CVM1);
8958 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8959 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008960 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008961 false, false, 16);
8962
8963 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8964 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8965 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8966 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8967 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008968 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008969 // a += a
8970 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008971
Nate Begeman51409212010-07-28 00:21:48 +00008972 C = ConstantVector::get(CVM2);
8973 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8974 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008975 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008976 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008977
Nate Begeman51409212010-07-28 00:21:48 +00008978 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8979 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8980 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8981 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8982 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008983 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008984 // a += a
8985 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008986
Nate Begeman51409212010-07-28 00:21:48 +00008987 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008988 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008989 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8990 return R;
8991 }
8992 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008993}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008994
Dan Gohmand858e902010-04-17 15:26:15 +00008995SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008996 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8997 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008998 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8999 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00009000 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00009001 SDValue LHS = N->getOperand(0);
9002 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00009003 unsigned BaseOp = 0;
9004 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009005 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00009006 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009007 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00009008 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00009009 // A subtract of one will be selected as a INC. Note that INC doesn't
9010 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009011 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9012 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009013 BaseOp = X86ISD::INC;
9014 Cond = X86::COND_O;
9015 break;
9016 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009017 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00009018 Cond = X86::COND_O;
9019 break;
9020 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009021 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00009022 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009023 break;
9024 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009025 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9026 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009027 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9028 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009029 BaseOp = X86ISD::DEC;
9030 Cond = X86::COND_O;
9031 break;
9032 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009033 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009034 Cond = X86::COND_O;
9035 break;
9036 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009037 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009038 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009039 break;
9040 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009041 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009042 Cond = X86::COND_O;
9043 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009044 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9045 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9046 MVT::i32);
9047 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009048
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009049 SDValue SetCC =
9050 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9051 DAG.getConstant(X86::COND_O, MVT::i32),
9052 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009053
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009054 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9055 return Sum;
9056 }
Bill Wendling74c37652008-12-09 22:08:41 +00009057 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009058
Bill Wendling61edeb52008-12-02 01:06:39 +00009059 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009060 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009061 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009062
Bill Wendling61edeb52008-12-02 01:06:39 +00009063 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009064 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9065 DAG.getConstant(Cond, MVT::i32),
9066 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009067
Bill Wendling61edeb52008-12-02 01:06:39 +00009068 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9069 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009070}
9071
Eric Christopher9a9d2752010-07-22 02:48:34 +00009072SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9073 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009074
Eric Christopherb6729dc2010-08-04 23:03:04 +00009075 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009076 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009077 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00009078 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009079 SDValue Ops[] = {
9080 DAG.getRegister(X86::ESP, MVT::i32), // Base
9081 DAG.getTargetConstant(1, MVT::i8), // Scale
9082 DAG.getRegister(0, MVT::i32), // Index
9083 DAG.getTargetConstant(0, MVT::i32), // Disp
9084 DAG.getRegister(0, MVT::i32), // Segment.
9085 Zero,
9086 Chain
9087 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009088 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009089 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9090 array_lengthof(Ops));
9091 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009092 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009093
Eric Christopher9a9d2752010-07-22 02:48:34 +00009094 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009095 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009096 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009097
Chris Lattner132929a2010-08-14 17:26:09 +00009098 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9099 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9100 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9101 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009102
Chris Lattner132929a2010-08-14 17:26:09 +00009103 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9104 if (!Op1 && !Op2 && !Op3 && Op4)
9105 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009106
Chris Lattner132929a2010-08-14 17:26:09 +00009107 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9108 if (Op1 && !Op2 && !Op3 && !Op4)
9109 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009110
9111 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009112 // (MFENCE)>;
9113 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009114}
9115
Dan Gohmand858e902010-04-17 15:26:15 +00009116SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009117 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009118 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009119 unsigned Reg = 0;
9120 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009121 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009122 default:
9123 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009124 case MVT::i8: Reg = X86::AL; size = 1; break;
9125 case MVT::i16: Reg = X86::AX; size = 2; break;
9126 case MVT::i32: Reg = X86::EAX; size = 4; break;
9127 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009128 assert(Subtarget->is64Bit() && "Node not type legal!");
9129 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009130 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009131 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009132 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009133 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009134 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009135 Op.getOperand(1),
9136 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009137 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009138 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009139 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009140 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9141 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9142 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009143 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009144 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009145 return cpOut;
9146}
9147
Duncan Sands1607f052008-12-01 11:39:25 +00009148SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009149 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009150 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009151 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009152 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009153 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009154 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009155 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9156 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009157 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009158 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9159 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009160 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009161 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009162 rdx.getValue(1)
9163 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009164 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009165}
9166
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009167SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009168 SelectionDAG &DAG) const {
9169 EVT SrcVT = Op.getOperand(0).getValueType();
9170 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009171 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9172 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009173 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009174 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009175 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009176 // i64 <=> MMX conversions are Legal.
9177 if (SrcVT==MVT::i64 && DstVT.isVector())
9178 return Op;
9179 if (DstVT==MVT::i64 && SrcVT.isVector())
9180 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009181 // MMX <=> MMX conversions are Legal.
9182 if (SrcVT.isVector() && DstVT.isVector())
9183 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009184 // All other conversions need to be expanded.
9185 return SDValue();
9186}
Chris Lattner5b856542010-12-20 00:59:46 +00009187
Dan Gohmand858e902010-04-17 15:26:15 +00009188SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009189 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009190 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009191 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009192 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009193 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009194 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009195 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009196 Node->getOperand(0),
9197 Node->getOperand(1), negOp,
9198 cast<AtomicSDNode>(Node)->getSrcValue(),
9199 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009200}
9201
Chris Lattner5b856542010-12-20 00:59:46 +00009202static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9203 EVT VT = Op.getNode()->getValueType(0);
9204
9205 // Let legalize expand this if it isn't a legal type yet.
9206 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9207 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009208
Chris Lattner5b856542010-12-20 00:59:46 +00009209 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009210
Chris Lattner5b856542010-12-20 00:59:46 +00009211 unsigned Opc;
9212 bool ExtraOp = false;
9213 switch (Op.getOpcode()) {
9214 default: assert(0 && "Invalid code");
9215 case ISD::ADDC: Opc = X86ISD::ADD; break;
9216 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9217 case ISD::SUBC: Opc = X86ISD::SUB; break;
9218 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9219 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009220
Chris Lattner5b856542010-12-20 00:59:46 +00009221 if (!ExtraOp)
9222 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9223 Op.getOperand(1));
9224 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9225 Op.getOperand(1), Op.getOperand(2));
9226}
9227
Evan Cheng0db9fe62006-04-25 20:13:52 +00009228/// LowerOperation - Provide custom lowering hooks for some operations.
9229///
Dan Gohmand858e902010-04-17 15:26:15 +00009230SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009231 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009232 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009233 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009234 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9235 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009236 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009237 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009238 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9239 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9240 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009241 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009242 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009243 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9244 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9245 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009246 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009247 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009248 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009249 case ISD::SHL_PARTS:
9250 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009251 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009252 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009253 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009254 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009255 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009256 case ISD::FABS: return LowerFABS(Op, DAG);
9257 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009258 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009259 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009260 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009261 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009262 case ISD::SELECT: return LowerSELECT(Op, DAG);
9263 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009264 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009265 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009266 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009267 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009268 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009269 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9270 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009271 case ISD::FRAME_TO_ARGS_OFFSET:
9272 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009273 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009274 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009275 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009276 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009277 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9278 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009279 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009280 case ISD::SRA:
9281 case ISD::SRL:
9282 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009283 case ISD::SADDO:
9284 case ISD::UADDO:
9285 case ISD::SSUBO:
9286 case ISD::USUBO:
9287 case ISD::SMULO:
9288 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009289 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009290 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009291 case ISD::ADDC:
9292 case ISD::ADDE:
9293 case ISD::SUBC:
9294 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009295 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009296}
9297
Duncan Sands1607f052008-12-01 11:39:25 +00009298void X86TargetLowering::
9299ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009300 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009301 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009302 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009303 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009304
9305 SDValue Chain = Node->getOperand(0);
9306 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009307 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009308 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009309 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009310 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009311 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009312 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009313 SDValue Result =
9314 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9315 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009316 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009317 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009318 Results.push_back(Result.getValue(2));
9319}
9320
Duncan Sands126d9072008-07-04 11:47:58 +00009321/// ReplaceNodeResults - Replace a node with an illegal result type
9322/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009323void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9324 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009325 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009326 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009327 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009328 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009329 assert(false && "Do not know how to custom type legalize this operation!");
9330 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009331 case ISD::ADDC:
9332 case ISD::ADDE:
9333 case ISD::SUBC:
9334 case ISD::SUBE:
9335 // We don't want to expand or promote these.
9336 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009337 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009338 std::pair<SDValue,SDValue> Vals =
9339 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009340 SDValue FIST = Vals.first, StackSlot = Vals.second;
9341 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009342 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009343 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009344 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9345 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009346 }
9347 return;
9348 }
9349 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009350 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009351 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009352 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009353 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009354 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009355 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009356 eax.getValue(2));
9357 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9358 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009359 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009360 Results.push_back(edx.getValue(1));
9361 return;
9362 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009363 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009364 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009365 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009366 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009367 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9368 DAG.getConstant(0, MVT::i32));
9369 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9370 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009371 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9372 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009373 cpInL.getValue(1));
9374 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009375 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9376 DAG.getConstant(0, MVT::i32));
9377 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9378 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009379 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009380 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009381 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009382 swapInL.getValue(1));
9383 SDValue Ops[] = { swapInH.getValue(0),
9384 N->getOperand(1),
9385 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009386 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009387 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9388 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9389 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009390 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009391 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009392 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009393 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009394 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009395 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009396 Results.push_back(cpOutH.getValue(1));
9397 return;
9398 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009399 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009400 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9401 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009402 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009403 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9404 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009405 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009406 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9407 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009408 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009409 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9410 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009411 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009412 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9413 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009414 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009415 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9416 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009417 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009418 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9419 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009420 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009421}
9422
Evan Cheng72261582005-12-20 06:22:03 +00009423const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9424 switch (Opcode) {
9425 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009426 case X86ISD::BSF: return "X86ISD::BSF";
9427 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009428 case X86ISD::SHLD: return "X86ISD::SHLD";
9429 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009430 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009431 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009432 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009433 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009434 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009435 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009436 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9437 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9438 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009439 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009440 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009441 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009442 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009443 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009444 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009445 case X86ISD::COMI: return "X86ISD::COMI";
9446 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009447 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009448 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009449 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9450 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009451 case X86ISD::CMOV: return "X86ISD::CMOV";
9452 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009453 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009454 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9455 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009456 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009457 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009458 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009459 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009460 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009461 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9462 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009463 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009464 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009465 case X86ISD::PANDN: return "X86ISD::PANDN";
9466 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9467 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9468 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009469 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009470 case X86ISD::FMAX: return "X86ISD::FMAX";
9471 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009472 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9473 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009474 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009475 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009476 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009477 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009478 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009479 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9480 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009481 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9482 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9483 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9484 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9485 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9486 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009487 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9488 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009489 case X86ISD::VSHL: return "X86ISD::VSHL";
9490 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009491 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9492 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9493 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9494 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9495 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9496 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9497 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9498 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9499 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9500 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009501 case X86ISD::ADD: return "X86ISD::ADD";
9502 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009503 case X86ISD::ADC: return "X86ISD::ADC";
9504 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009505 case X86ISD::SMUL: return "X86ISD::SMUL";
9506 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009507 case X86ISD::INC: return "X86ISD::INC";
9508 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009509 case X86ISD::OR: return "X86ISD::OR";
9510 case X86ISD::XOR: return "X86ISD::XOR";
9511 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009512 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009513 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009514 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009515 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9516 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9517 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9518 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9519 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9520 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9521 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9522 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9523 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009524 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009525 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009526 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009527 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9528 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009529 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9530 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9531 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9532 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9533 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9534 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9535 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9536 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9537 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009538 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9539 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9540 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9541 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009542 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9543 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9544 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9545 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9546 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9547 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9548 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9549 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9550 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9551 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009552 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009553 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009554 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009555 }
9556}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009557
Chris Lattnerc9addb72007-03-30 23:15:24 +00009558// isLegalAddressingMode - Return true if the addressing mode represented
9559// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009560bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009561 const Type *Ty) const {
9562 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009563 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009564 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009565
Chris Lattnerc9addb72007-03-30 23:15:24 +00009566 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009567 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009568 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009569
Chris Lattnerc9addb72007-03-30 23:15:24 +00009570 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009571 unsigned GVFlags =
9572 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009573
Chris Lattnerdfed4132009-07-10 07:38:24 +00009574 // If a reference to this global requires an extra load, we can't fold it.
9575 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009576 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009577
Chris Lattnerdfed4132009-07-10 07:38:24 +00009578 // If BaseGV requires a register for the PIC base, we cannot also have a
9579 // BaseReg specified.
9580 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009581 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009582
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009583 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009584 if ((M != CodeModel::Small || R != Reloc::Static) &&
9585 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009586 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009587 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009588
Chris Lattnerc9addb72007-03-30 23:15:24 +00009589 switch (AM.Scale) {
9590 case 0:
9591 case 1:
9592 case 2:
9593 case 4:
9594 case 8:
9595 // These scales always work.
9596 break;
9597 case 3:
9598 case 5:
9599 case 9:
9600 // These scales are formed with basereg+scalereg. Only accept if there is
9601 // no basereg yet.
9602 if (AM.HasBaseReg)
9603 return false;
9604 break;
9605 default: // Other stuff never works.
9606 return false;
9607 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009608
Chris Lattnerc9addb72007-03-30 23:15:24 +00009609 return true;
9610}
9611
9612
Evan Cheng2bd122c2007-10-26 01:56:11 +00009613bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009614 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009615 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009616 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9617 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009618 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009619 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009620 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009621}
9622
Owen Andersone50ed302009-08-10 22:56:29 +00009623bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009624 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009625 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009626 unsigned NumBits1 = VT1.getSizeInBits();
9627 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009628 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009629 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009630 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009631}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009632
Dan Gohman97121ba2009-04-08 00:15:30 +00009633bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009634 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009635 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009636}
9637
Owen Andersone50ed302009-08-10 22:56:29 +00009638bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009639 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009640 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009641}
9642
Owen Andersone50ed302009-08-10 22:56:29 +00009643bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009644 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009645 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009646}
9647
Evan Cheng60c07e12006-07-05 22:17:51 +00009648/// isShuffleMaskLegal - Targets can use this to indicate that they only
9649/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9650/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9651/// are assumed to be legal.
9652bool
Eric Christopherfd179292009-08-27 18:07:15 +00009653X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009654 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009655 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009656 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009657 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009658
Nate Begemana09008b2009-10-19 02:17:23 +00009659 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009660 return (VT.getVectorNumElements() == 2 ||
9661 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9662 isMOVLMask(M, VT) ||
9663 isSHUFPMask(M, VT) ||
9664 isPSHUFDMask(M, VT) ||
9665 isPSHUFHWMask(M, VT) ||
9666 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009667 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009668 isUNPCKLMask(M, VT) ||
9669 isUNPCKHMask(M, VT) ||
9670 isUNPCKL_v_undef_Mask(M, VT) ||
9671 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009672}
9673
Dan Gohman7d8143f2008-04-09 20:09:42 +00009674bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009675X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009676 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009677 unsigned NumElts = VT.getVectorNumElements();
9678 // FIXME: This collection of masks seems suspect.
9679 if (NumElts == 2)
9680 return true;
9681 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9682 return (isMOVLMask(Mask, VT) ||
9683 isCommutedMOVLMask(Mask, VT, true) ||
9684 isSHUFPMask(Mask, VT) ||
9685 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009686 }
9687 return false;
9688}
9689
9690//===----------------------------------------------------------------------===//
9691// X86 Scheduler Hooks
9692//===----------------------------------------------------------------------===//
9693
Mon P Wang63307c32008-05-05 19:05:59 +00009694// private utility function
9695MachineBasicBlock *
9696X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9697 MachineBasicBlock *MBB,
9698 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009699 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009700 unsigned LoadOpc,
9701 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009702 unsigned notOpc,
9703 unsigned EAXreg,
9704 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009705 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009706 // For the atomic bitwise operator, we generate
9707 // thisMBB:
9708 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009709 // ld t1 = [bitinstr.addr]
9710 // op t2 = t1, [bitinstr.val]
9711 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009712 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9713 // bz newMBB
9714 // fallthrough -->nextMBB
9715 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9716 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009717 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009718 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009719
Mon P Wang63307c32008-05-05 19:05:59 +00009720 /// First build the CFG
9721 MachineFunction *F = MBB->getParent();
9722 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009723 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9724 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9725 F->insert(MBBIter, newMBB);
9726 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009727
Dan Gohman14152b42010-07-06 20:24:04 +00009728 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9729 nextMBB->splice(nextMBB->begin(), thisMBB,
9730 llvm::next(MachineBasicBlock::iterator(bInstr)),
9731 thisMBB->end());
9732 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009733
Mon P Wang63307c32008-05-05 19:05:59 +00009734 // Update thisMBB to fall through to newMBB
9735 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009736
Mon P Wang63307c32008-05-05 19:05:59 +00009737 // newMBB jumps to itself and fall through to nextMBB
9738 newMBB->addSuccessor(nextMBB);
9739 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009740
Mon P Wang63307c32008-05-05 19:05:59 +00009741 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009742 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009743 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009744 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009745 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009746 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009747 int numArgs = bInstr->getNumOperands() - 1;
9748 for (int i=0; i < numArgs; ++i)
9749 argOpers[i] = &bInstr->getOperand(i+1);
9750
9751 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009752 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009753 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009754
Dale Johannesen140be2d2008-08-19 18:47:28 +00009755 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009756 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009757 for (int i=0; i <= lastAddrIndx; ++i)
9758 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009759
Dale Johannesen140be2d2008-08-19 18:47:28 +00009760 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009761 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009762 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009763 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009764 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009765 tt = t1;
9766
Dale Johannesen140be2d2008-08-19 18:47:28 +00009767 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009768 assert((argOpers[valArgIndx]->isReg() ||
9769 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009770 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009771 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009772 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009773 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009774 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009775 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009776 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009777
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009778 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009779 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009780
Dale Johannesene4d209d2009-02-03 20:21:25 +00009781 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009782 for (int i=0; i <= lastAddrIndx; ++i)
9783 (*MIB).addOperand(*argOpers[i]);
9784 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009785 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009786 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9787 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009788
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009789 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009790 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009791
Mon P Wang63307c32008-05-05 19:05:59 +00009792 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009793 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009794
Dan Gohman14152b42010-07-06 20:24:04 +00009795 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009796 return nextMBB;
9797}
9798
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009799// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009800MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009801X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9802 MachineBasicBlock *MBB,
9803 unsigned regOpcL,
9804 unsigned regOpcH,
9805 unsigned immOpcL,
9806 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009807 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009808 // For the atomic bitwise operator, we generate
9809 // thisMBB (instructions are in pairs, except cmpxchg8b)
9810 // ld t1,t2 = [bitinstr.addr]
9811 // newMBB:
9812 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9813 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009814 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009815 // mov ECX, EBX <- t5, t6
9816 // mov EAX, EDX <- t1, t2
9817 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9818 // mov t3, t4 <- EAX, EDX
9819 // bz newMBB
9820 // result in out1, out2
9821 // fallthrough -->nextMBB
9822
9823 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9824 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009825 const unsigned NotOpc = X86::NOT32r;
9826 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9827 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9828 MachineFunction::iterator MBBIter = MBB;
9829 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009830
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009831 /// First build the CFG
9832 MachineFunction *F = MBB->getParent();
9833 MachineBasicBlock *thisMBB = MBB;
9834 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9835 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9836 F->insert(MBBIter, newMBB);
9837 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009838
Dan Gohman14152b42010-07-06 20:24:04 +00009839 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9840 nextMBB->splice(nextMBB->begin(), thisMBB,
9841 llvm::next(MachineBasicBlock::iterator(bInstr)),
9842 thisMBB->end());
9843 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009844
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009845 // Update thisMBB to fall through to newMBB
9846 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009847
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009848 // newMBB jumps to itself and fall through to nextMBB
9849 newMBB->addSuccessor(nextMBB);
9850 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009851
Dale Johannesene4d209d2009-02-03 20:21:25 +00009852 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009853 // Insert instructions into newMBB based on incoming instruction
9854 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009855 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009856 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009857 MachineOperand& dest1Oper = bInstr->getOperand(0);
9858 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009859 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9860 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009861 argOpers[i] = &bInstr->getOperand(i+2);
9862
Dan Gohman71ea4e52010-05-14 21:01:44 +00009863 // We use some of the operands multiple times, so conservatively just
9864 // clear any kill flags that might be present.
9865 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9866 argOpers[i]->setIsKill(false);
9867 }
9868
Evan Chengad5b52f2010-01-08 19:14:57 +00009869 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009870 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009871
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009872 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009873 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009874 for (int i=0; i <= lastAddrIndx; ++i)
9875 (*MIB).addOperand(*argOpers[i]);
9876 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009877 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009878 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009879 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009880 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009881 MachineOperand newOp3 = *(argOpers[3]);
9882 if (newOp3.isImm())
9883 newOp3.setImm(newOp3.getImm()+4);
9884 else
9885 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009886 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009887 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009888
9889 // t3/4 are defined later, at the bottom of the loop
9890 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9891 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009892 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009893 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009894 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009895 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9896
Evan Cheng306b4ca2010-01-08 23:41:50 +00009897 // The subsequent operations should be using the destination registers of
9898 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009899 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009900 t1 = F->getRegInfo().createVirtualRegister(RC);
9901 t2 = F->getRegInfo().createVirtualRegister(RC);
9902 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9903 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009904 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009905 t1 = dest1Oper.getReg();
9906 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009907 }
9908
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009909 int valArgIndx = lastAddrIndx + 1;
9910 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009911 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009912 "invalid operand");
9913 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9914 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009915 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009916 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009917 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009918 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009919 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009920 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009921 (*MIB).addOperand(*argOpers[valArgIndx]);
9922 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009923 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009924 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009925 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009926 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009927 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009928 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009929 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009930 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009931 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009932 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009933
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009934 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009935 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009936 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009937 MIB.addReg(t2);
9938
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009939 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009940 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009941 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009942 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009943
Dale Johannesene4d209d2009-02-03 20:21:25 +00009944 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009945 for (int i=0; i <= lastAddrIndx; ++i)
9946 (*MIB).addOperand(*argOpers[i]);
9947
9948 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009949 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9950 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009951
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009952 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009953 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009954 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009955 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009956
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009957 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009958 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009959
Dan Gohman14152b42010-07-06 20:24:04 +00009960 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009961 return nextMBB;
9962}
9963
9964// private utility function
9965MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009966X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9967 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009968 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009969 // For the atomic min/max operator, we generate
9970 // thisMBB:
9971 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009972 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009973 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009974 // cmp t1, t2
9975 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009976 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009977 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9978 // bz newMBB
9979 // fallthrough -->nextMBB
9980 //
9981 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9982 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009983 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009984 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009985
Mon P Wang63307c32008-05-05 19:05:59 +00009986 /// First build the CFG
9987 MachineFunction *F = MBB->getParent();
9988 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009989 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9990 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9991 F->insert(MBBIter, newMBB);
9992 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009993
Dan Gohman14152b42010-07-06 20:24:04 +00009994 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9995 nextMBB->splice(nextMBB->begin(), thisMBB,
9996 llvm::next(MachineBasicBlock::iterator(mInstr)),
9997 thisMBB->end());
9998 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009999
Mon P Wang63307c32008-05-05 19:05:59 +000010000 // Update thisMBB to fall through to newMBB
10001 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010002
Mon P Wang63307c32008-05-05 19:05:59 +000010003 // newMBB jumps to newMBB and fall through to nextMBB
10004 newMBB->addSuccessor(nextMBB);
10005 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010006
Dale Johannesene4d209d2009-02-03 20:21:25 +000010007 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010008 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010009 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010010 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000010011 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010012 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010013 int numArgs = mInstr->getNumOperands() - 1;
10014 for (int i=0; i < numArgs; ++i)
10015 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010016
Mon P Wang63307c32008-05-05 19:05:59 +000010017 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010018 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010019 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010020
Mon P Wangab3e7472008-05-05 22:56:23 +000010021 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010022 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010023 for (int i=0; i <= lastAddrIndx; ++i)
10024 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000010025
Mon P Wang63307c32008-05-05 19:05:59 +000010026 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010027 assert((argOpers[valArgIndx]->isReg() ||
10028 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010029 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010030
10031 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010032 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010033 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010034 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010035 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010036 (*MIB).addOperand(*argOpers[valArgIndx]);
10037
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010038 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010039 MIB.addReg(t1);
10040
Dale Johannesene4d209d2009-02-03 20:21:25 +000010041 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010042 MIB.addReg(t1);
10043 MIB.addReg(t2);
10044
10045 // Generate movc
10046 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010047 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010048 MIB.addReg(t2);
10049 MIB.addReg(t1);
10050
10051 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010052 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010053 for (int i=0; i <= lastAddrIndx; ++i)
10054 (*MIB).addOperand(*argOpers[i]);
10055 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010056 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010057 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10058 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010059
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010060 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010061 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010062
Mon P Wang63307c32008-05-05 19:05:59 +000010063 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010064 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010065
Dan Gohman14152b42010-07-06 20:24:04 +000010066 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010067 return nextMBB;
10068}
10069
Eric Christopherf83a5de2009-08-27 18:08:16 +000010070// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010071// or XMM0_V32I8 in AVX all of this code can be replaced with that
10072// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010073MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010074X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010075 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010076 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10077 "Target must have SSE4.2 or AVX features enabled");
10078
Eric Christopherb120ab42009-08-18 22:50:32 +000010079 DebugLoc dl = MI->getDebugLoc();
10080 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010081 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010082 if (!Subtarget->hasAVX()) {
10083 if (memArg)
10084 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10085 else
10086 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10087 } else {
10088 if (memArg)
10089 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10090 else
10091 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10092 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010093
Eric Christopher41c902f2010-11-30 08:20:21 +000010094 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010095 for (unsigned i = 0; i < numArgs; ++i) {
10096 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010097 if (!(Op.isReg() && Op.isImplicit()))
10098 MIB.addOperand(Op);
10099 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010100 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010101 .addReg(X86::XMM0);
10102
Dan Gohman14152b42010-07-06 20:24:04 +000010103 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010104 return BB;
10105}
10106
10107MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010108X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010109 DebugLoc dl = MI->getDebugLoc();
10110 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010111
Eric Christopher228232b2010-11-30 07:20:12 +000010112 // Address into RAX/EAX, other two args into ECX, EDX.
10113 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10114 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10115 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10116 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010117 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010118
Eric Christopher228232b2010-11-30 07:20:12 +000010119 unsigned ValOps = X86::AddrNumOperands;
10120 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10121 .addReg(MI->getOperand(ValOps).getReg());
10122 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10123 .addReg(MI->getOperand(ValOps+1).getReg());
10124
10125 // The instruction doesn't actually take any operands though.
10126 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010127
Eric Christopher228232b2010-11-30 07:20:12 +000010128 MI->eraseFromParent(); // The pseudo is gone now.
10129 return BB;
10130}
10131
10132MachineBasicBlock *
10133X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010134 DebugLoc dl = MI->getDebugLoc();
10135 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010136
Eric Christopher228232b2010-11-30 07:20:12 +000010137 // First arg in ECX, the second in EAX.
10138 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10139 .addReg(MI->getOperand(0).getReg());
10140 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10141 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010142
Eric Christopher228232b2010-11-30 07:20:12 +000010143 // The instruction doesn't actually take any operands though.
10144 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010145
Eric Christopher228232b2010-11-30 07:20:12 +000010146 MI->eraseFromParent(); // The pseudo is gone now.
10147 return BB;
10148}
10149
10150MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010151X86TargetLowering::EmitVAARG64WithCustomInserter(
10152 MachineInstr *MI,
10153 MachineBasicBlock *MBB) const {
10154 // Emit va_arg instruction on X86-64.
10155
10156 // Operands to this pseudo-instruction:
10157 // 0 ) Output : destination address (reg)
10158 // 1-5) Input : va_list address (addr, i64mem)
10159 // 6 ) ArgSize : Size (in bytes) of vararg type
10160 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10161 // 8 ) Align : Alignment of type
10162 // 9 ) EFLAGS (implicit-def)
10163
10164 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10165 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10166
10167 unsigned DestReg = MI->getOperand(0).getReg();
10168 MachineOperand &Base = MI->getOperand(1);
10169 MachineOperand &Scale = MI->getOperand(2);
10170 MachineOperand &Index = MI->getOperand(3);
10171 MachineOperand &Disp = MI->getOperand(4);
10172 MachineOperand &Segment = MI->getOperand(5);
10173 unsigned ArgSize = MI->getOperand(6).getImm();
10174 unsigned ArgMode = MI->getOperand(7).getImm();
10175 unsigned Align = MI->getOperand(8).getImm();
10176
10177 // Memory Reference
10178 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10179 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10180 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10181
10182 // Machine Information
10183 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10184 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10185 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10186 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10187 DebugLoc DL = MI->getDebugLoc();
10188
10189 // struct va_list {
10190 // i32 gp_offset
10191 // i32 fp_offset
10192 // i64 overflow_area (address)
10193 // i64 reg_save_area (address)
10194 // }
10195 // sizeof(va_list) = 24
10196 // alignment(va_list) = 8
10197
10198 unsigned TotalNumIntRegs = 6;
10199 unsigned TotalNumXMMRegs = 8;
10200 bool UseGPOffset = (ArgMode == 1);
10201 bool UseFPOffset = (ArgMode == 2);
10202 unsigned MaxOffset = TotalNumIntRegs * 8 +
10203 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10204
10205 /* Align ArgSize to a multiple of 8 */
10206 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10207 bool NeedsAlign = (Align > 8);
10208
10209 MachineBasicBlock *thisMBB = MBB;
10210 MachineBasicBlock *overflowMBB;
10211 MachineBasicBlock *offsetMBB;
10212 MachineBasicBlock *endMBB;
10213
10214 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10215 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10216 unsigned OffsetReg = 0;
10217
10218 if (!UseGPOffset && !UseFPOffset) {
10219 // If we only pull from the overflow region, we don't create a branch.
10220 // We don't need to alter control flow.
10221 OffsetDestReg = 0; // unused
10222 OverflowDestReg = DestReg;
10223
10224 offsetMBB = NULL;
10225 overflowMBB = thisMBB;
10226 endMBB = thisMBB;
10227 } else {
10228 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10229 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10230 // If not, pull from overflow_area. (branch to overflowMBB)
10231 //
10232 // thisMBB
10233 // | .
10234 // | .
10235 // offsetMBB overflowMBB
10236 // | .
10237 // | .
10238 // endMBB
10239
10240 // Registers for the PHI in endMBB
10241 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10242 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10243
10244 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10245 MachineFunction *MF = MBB->getParent();
10246 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10247 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10248 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10249
10250 MachineFunction::iterator MBBIter = MBB;
10251 ++MBBIter;
10252
10253 // Insert the new basic blocks
10254 MF->insert(MBBIter, offsetMBB);
10255 MF->insert(MBBIter, overflowMBB);
10256 MF->insert(MBBIter, endMBB);
10257
10258 // Transfer the remainder of MBB and its successor edges to endMBB.
10259 endMBB->splice(endMBB->begin(), thisMBB,
10260 llvm::next(MachineBasicBlock::iterator(MI)),
10261 thisMBB->end());
10262 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10263
10264 // Make offsetMBB and overflowMBB successors of thisMBB
10265 thisMBB->addSuccessor(offsetMBB);
10266 thisMBB->addSuccessor(overflowMBB);
10267
10268 // endMBB is a successor of both offsetMBB and overflowMBB
10269 offsetMBB->addSuccessor(endMBB);
10270 overflowMBB->addSuccessor(endMBB);
10271
10272 // Load the offset value into a register
10273 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10274 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10275 .addOperand(Base)
10276 .addOperand(Scale)
10277 .addOperand(Index)
10278 .addDisp(Disp, UseFPOffset ? 4 : 0)
10279 .addOperand(Segment)
10280 .setMemRefs(MMOBegin, MMOEnd);
10281
10282 // Check if there is enough room left to pull this argument.
10283 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10284 .addReg(OffsetReg)
10285 .addImm(MaxOffset + 8 - ArgSizeA8);
10286
10287 // Branch to "overflowMBB" if offset >= max
10288 // Fall through to "offsetMBB" otherwise
10289 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10290 .addMBB(overflowMBB);
10291 }
10292
10293 // In offsetMBB, emit code to use the reg_save_area.
10294 if (offsetMBB) {
10295 assert(OffsetReg != 0);
10296
10297 // Read the reg_save_area address.
10298 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10299 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10300 .addOperand(Base)
10301 .addOperand(Scale)
10302 .addOperand(Index)
10303 .addDisp(Disp, 16)
10304 .addOperand(Segment)
10305 .setMemRefs(MMOBegin, MMOEnd);
10306
10307 // Zero-extend the offset
10308 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10309 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10310 .addImm(0)
10311 .addReg(OffsetReg)
10312 .addImm(X86::sub_32bit);
10313
10314 // Add the offset to the reg_save_area to get the final address.
10315 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10316 .addReg(OffsetReg64)
10317 .addReg(RegSaveReg);
10318
10319 // Compute the offset for the next argument
10320 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10321 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10322 .addReg(OffsetReg)
10323 .addImm(UseFPOffset ? 16 : 8);
10324
10325 // Store it back into the va_list.
10326 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10327 .addOperand(Base)
10328 .addOperand(Scale)
10329 .addOperand(Index)
10330 .addDisp(Disp, UseFPOffset ? 4 : 0)
10331 .addOperand(Segment)
10332 .addReg(NextOffsetReg)
10333 .setMemRefs(MMOBegin, MMOEnd);
10334
10335 // Jump to endMBB
10336 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10337 .addMBB(endMBB);
10338 }
10339
10340 //
10341 // Emit code to use overflow area
10342 //
10343
10344 // Load the overflow_area address into a register.
10345 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10346 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10347 .addOperand(Base)
10348 .addOperand(Scale)
10349 .addOperand(Index)
10350 .addDisp(Disp, 8)
10351 .addOperand(Segment)
10352 .setMemRefs(MMOBegin, MMOEnd);
10353
10354 // If we need to align it, do so. Otherwise, just copy the address
10355 // to OverflowDestReg.
10356 if (NeedsAlign) {
10357 // Align the overflow address
10358 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10359 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10360
10361 // aligned_addr = (addr + (align-1)) & ~(align-1)
10362 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10363 .addReg(OverflowAddrReg)
10364 .addImm(Align-1);
10365
10366 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10367 .addReg(TmpReg)
10368 .addImm(~(uint64_t)(Align-1));
10369 } else {
10370 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10371 .addReg(OverflowAddrReg);
10372 }
10373
10374 // Compute the next overflow address after this argument.
10375 // (the overflow address should be kept 8-byte aligned)
10376 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10377 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10378 .addReg(OverflowDestReg)
10379 .addImm(ArgSizeA8);
10380
10381 // Store the new overflow address.
10382 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10383 .addOperand(Base)
10384 .addOperand(Scale)
10385 .addOperand(Index)
10386 .addDisp(Disp, 8)
10387 .addOperand(Segment)
10388 .addReg(NextAddrReg)
10389 .setMemRefs(MMOBegin, MMOEnd);
10390
10391 // If we branched, emit the PHI to the front of endMBB.
10392 if (offsetMBB) {
10393 BuildMI(*endMBB, endMBB->begin(), DL,
10394 TII->get(X86::PHI), DestReg)
10395 .addReg(OffsetDestReg).addMBB(offsetMBB)
10396 .addReg(OverflowDestReg).addMBB(overflowMBB);
10397 }
10398
10399 // Erase the pseudo instruction
10400 MI->eraseFromParent();
10401
10402 return endMBB;
10403}
10404
10405MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010406X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10407 MachineInstr *MI,
10408 MachineBasicBlock *MBB) const {
10409 // Emit code to save XMM registers to the stack. The ABI says that the
10410 // number of registers to save is given in %al, so it's theoretically
10411 // possible to do an indirect jump trick to avoid saving all of them,
10412 // however this code takes a simpler approach and just executes all
10413 // of the stores if %al is non-zero. It's less code, and it's probably
10414 // easier on the hardware branch predictor, and stores aren't all that
10415 // expensive anyway.
10416
10417 // Create the new basic blocks. One block contains all the XMM stores,
10418 // and one block is the final destination regardless of whether any
10419 // stores were performed.
10420 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10421 MachineFunction *F = MBB->getParent();
10422 MachineFunction::iterator MBBIter = MBB;
10423 ++MBBIter;
10424 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10425 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10426 F->insert(MBBIter, XMMSaveMBB);
10427 F->insert(MBBIter, EndMBB);
10428
Dan Gohman14152b42010-07-06 20:24:04 +000010429 // Transfer the remainder of MBB and its successor edges to EndMBB.
10430 EndMBB->splice(EndMBB->begin(), MBB,
10431 llvm::next(MachineBasicBlock::iterator(MI)),
10432 MBB->end());
10433 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10434
Dan Gohmand6708ea2009-08-15 01:38:56 +000010435 // The original block will now fall through to the XMM save block.
10436 MBB->addSuccessor(XMMSaveMBB);
10437 // The XMMSaveMBB will fall through to the end block.
10438 XMMSaveMBB->addSuccessor(EndMBB);
10439
10440 // Now add the instructions.
10441 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10442 DebugLoc DL = MI->getDebugLoc();
10443
10444 unsigned CountReg = MI->getOperand(0).getReg();
10445 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10446 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10447
10448 if (!Subtarget->isTargetWin64()) {
10449 // If %al is 0, branch around the XMM save block.
10450 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010451 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010452 MBB->addSuccessor(EndMBB);
10453 }
10454
10455 // In the XMM save block, save all the XMM argument registers.
10456 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10457 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010458 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010459 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010460 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010461 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010462 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010463 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10464 .addFrameIndex(RegSaveFrameIndex)
10465 .addImm(/*Scale=*/1)
10466 .addReg(/*IndexReg=*/0)
10467 .addImm(/*Disp=*/Offset)
10468 .addReg(/*Segment=*/0)
10469 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010470 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010471 }
10472
Dan Gohman14152b42010-07-06 20:24:04 +000010473 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010474
10475 return EndMBB;
10476}
Mon P Wang63307c32008-05-05 19:05:59 +000010477
Evan Cheng60c07e12006-07-05 22:17:51 +000010478MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010479X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010480 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010481 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10482 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010483
Chris Lattner52600972009-09-02 05:57:00 +000010484 // To "insert" a SELECT_CC instruction, we actually have to insert the
10485 // diamond control-flow pattern. The incoming instruction knows the
10486 // destination vreg to set, the condition code register to branch on, the
10487 // true/false values to select between, and a branch opcode to use.
10488 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10489 MachineFunction::iterator It = BB;
10490 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010491
Chris Lattner52600972009-09-02 05:57:00 +000010492 // thisMBB:
10493 // ...
10494 // TrueVal = ...
10495 // cmpTY ccX, r1, r2
10496 // bCC copy1MBB
10497 // fallthrough --> copy0MBB
10498 MachineBasicBlock *thisMBB = BB;
10499 MachineFunction *F = BB->getParent();
10500 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10501 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010502 F->insert(It, copy0MBB);
10503 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010504
Bill Wendling730c07e2010-06-25 20:48:10 +000010505 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10506 // live into the sink and copy blocks.
10507 const MachineFunction *MF = BB->getParent();
10508 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10509 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010510
Dan Gohman14152b42010-07-06 20:24:04 +000010511 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10512 const MachineOperand &MO = MI->getOperand(I);
10513 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010514 unsigned Reg = MO.getReg();
10515 if (Reg != X86::EFLAGS) continue;
10516 copy0MBB->addLiveIn(Reg);
10517 sinkMBB->addLiveIn(Reg);
10518 }
10519
Dan Gohman14152b42010-07-06 20:24:04 +000010520 // Transfer the remainder of BB and its successor edges to sinkMBB.
10521 sinkMBB->splice(sinkMBB->begin(), BB,
10522 llvm::next(MachineBasicBlock::iterator(MI)),
10523 BB->end());
10524 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10525
10526 // Add the true and fallthrough blocks as its successors.
10527 BB->addSuccessor(copy0MBB);
10528 BB->addSuccessor(sinkMBB);
10529
10530 // Create the conditional branch instruction.
10531 unsigned Opc =
10532 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10533 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10534
Chris Lattner52600972009-09-02 05:57:00 +000010535 // copy0MBB:
10536 // %FalseValue = ...
10537 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010538 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010539
Chris Lattner52600972009-09-02 05:57:00 +000010540 // sinkMBB:
10541 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10542 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010543 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10544 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010545 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10546 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10547
Dan Gohman14152b42010-07-06 20:24:04 +000010548 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010549 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010550}
10551
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010552MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010553X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010554 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010555 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10556 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010557
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010558 assert(!Subtarget->isTargetEnvMacho());
10559
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010560 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10561 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010562
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010563 if (Subtarget->isTargetWin64()) {
10564 if (Subtarget->isTargetCygMing()) {
10565 // ___chkstk(Mingw64):
10566 // Clobbers R10, R11, RAX and EFLAGS.
10567 // Updates RSP.
10568 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10569 .addExternalSymbol("___chkstk")
10570 .addReg(X86::RAX, RegState::Implicit)
10571 .addReg(X86::RSP, RegState::Implicit)
10572 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10573 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10574 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10575 } else {
10576 // __chkstk(MSVCRT): does not update stack pointer.
10577 // Clobbers R10, R11 and EFLAGS.
10578 // FIXME: RAX(allocated size) might be reused and not killed.
10579 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10580 .addExternalSymbol("__chkstk")
10581 .addReg(X86::RAX, RegState::Implicit)
10582 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10583 // RAX has the offset to subtracted from RSP.
10584 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10585 .addReg(X86::RSP)
10586 .addReg(X86::RAX);
10587 }
10588 } else {
10589 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010590 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10591
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010592 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10593 .addExternalSymbol(StackProbeSymbol)
10594 .addReg(X86::EAX, RegState::Implicit)
10595 .addReg(X86::ESP, RegState::Implicit)
10596 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10597 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10598 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10599 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010600
Dan Gohman14152b42010-07-06 20:24:04 +000010601 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010602 return BB;
10603}
Chris Lattner52600972009-09-02 05:57:00 +000010604
10605MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010606X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10607 MachineBasicBlock *BB) const {
10608 // This is pretty easy. We're taking the value that we received from
10609 // our load from the relocation, sticking it in either RDI (x86-64)
10610 // or EAX and doing an indirect call. The return value will then
10611 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010612 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010613 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010614 DebugLoc DL = MI->getDebugLoc();
10615 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010616
10617 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010618 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010619
Eric Christopher30ef0e52010-06-03 04:07:48 +000010620 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010621 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10622 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010623 .addReg(X86::RIP)
10624 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010625 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010626 MI->getOperand(3).getTargetFlags())
10627 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010628 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010629 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010630 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010631 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10632 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010633 .addReg(0)
10634 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010635 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010636 MI->getOperand(3).getTargetFlags())
10637 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010638 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010639 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010640 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010641 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10642 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010643 .addReg(TII->getGlobalBaseReg(F))
10644 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010645 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010646 MI->getOperand(3).getTargetFlags())
10647 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010648 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010649 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010650 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010651
Dan Gohman14152b42010-07-06 20:24:04 +000010652 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010653 return BB;
10654}
10655
10656MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010657X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010658 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010659 switch (MI->getOpcode()) {
10660 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010661 case X86::TAILJMPd64:
10662 case X86::TAILJMPr64:
10663 case X86::TAILJMPm64:
10664 assert(!"TAILJMP64 would not be touched here.");
10665 case X86::TCRETURNdi64:
10666 case X86::TCRETURNri64:
10667 case X86::TCRETURNmi64:
10668 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10669 // On AMD64, additional defs should be added before register allocation.
10670 if (!Subtarget->isTargetWin64()) {
10671 MI->addRegisterDefined(X86::RSI);
10672 MI->addRegisterDefined(X86::RDI);
10673 MI->addRegisterDefined(X86::XMM6);
10674 MI->addRegisterDefined(X86::XMM7);
10675 MI->addRegisterDefined(X86::XMM8);
10676 MI->addRegisterDefined(X86::XMM9);
10677 MI->addRegisterDefined(X86::XMM10);
10678 MI->addRegisterDefined(X86::XMM11);
10679 MI->addRegisterDefined(X86::XMM12);
10680 MI->addRegisterDefined(X86::XMM13);
10681 MI->addRegisterDefined(X86::XMM14);
10682 MI->addRegisterDefined(X86::XMM15);
10683 }
10684 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010685 case X86::WIN_ALLOCA:
10686 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010687 case X86::TLSCall_32:
10688 case X86::TLSCall_64:
10689 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010690 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010691 case X86::CMOV_FR32:
10692 case X86::CMOV_FR64:
10693 case X86::CMOV_V4F32:
10694 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010695 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010696 case X86::CMOV_GR16:
10697 case X86::CMOV_GR32:
10698 case X86::CMOV_RFP32:
10699 case X86::CMOV_RFP64:
10700 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010701 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010702
Dale Johannesen849f2142007-07-03 00:53:03 +000010703 case X86::FP32_TO_INT16_IN_MEM:
10704 case X86::FP32_TO_INT32_IN_MEM:
10705 case X86::FP32_TO_INT64_IN_MEM:
10706 case X86::FP64_TO_INT16_IN_MEM:
10707 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010708 case X86::FP64_TO_INT64_IN_MEM:
10709 case X86::FP80_TO_INT16_IN_MEM:
10710 case X86::FP80_TO_INT32_IN_MEM:
10711 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010712 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10713 DebugLoc DL = MI->getDebugLoc();
10714
Evan Cheng60c07e12006-07-05 22:17:51 +000010715 // Change the floating point control register to use "round towards zero"
10716 // mode when truncating to an integer value.
10717 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010718 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010719 addFrameReference(BuildMI(*BB, MI, DL,
10720 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010721
10722 // Load the old value of the high byte of the control word...
10723 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010724 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010725 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010726 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010727
10728 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010729 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010730 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010731
10732 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010733 addFrameReference(BuildMI(*BB, MI, DL,
10734 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010735
10736 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010737 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010738 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010739
10740 // Get the X86 opcode to use.
10741 unsigned Opc;
10742 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010743 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010744 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10745 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10746 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10747 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10748 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10749 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010750 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10751 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10752 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010753 }
10754
10755 X86AddressMode AM;
10756 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010757 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010758 AM.BaseType = X86AddressMode::RegBase;
10759 AM.Base.Reg = Op.getReg();
10760 } else {
10761 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010762 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010763 }
10764 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010765 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010766 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010767 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010768 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010769 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010770 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010771 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010772 AM.GV = Op.getGlobal();
10773 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010774 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010775 }
Dan Gohman14152b42010-07-06 20:24:04 +000010776 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010777 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010778
10779 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010780 addFrameReference(BuildMI(*BB, MI, DL,
10781 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010782
Dan Gohman14152b42010-07-06 20:24:04 +000010783 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010784 return BB;
10785 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010786 // String/text processing lowering.
10787 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010788 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010789 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10790 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010791 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010792 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10793 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010794 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010795 return EmitPCMP(MI, BB, 5, false /* in mem */);
10796 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010797 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010798 return EmitPCMP(MI, BB, 5, true /* in mem */);
10799
Eric Christopher228232b2010-11-30 07:20:12 +000010800 // Thread synchronization.
10801 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010802 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010803 case X86::MWAIT:
10804 return EmitMwait(MI, BB);
10805
Eric Christopherb120ab42009-08-18 22:50:32 +000010806 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010807 case X86::ATOMAND32:
10808 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010809 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010810 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010811 X86::NOT32r, X86::EAX,
10812 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010813 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010814 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10815 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010816 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010817 X86::NOT32r, X86::EAX,
10818 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010819 case X86::ATOMXOR32:
10820 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010821 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010822 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010823 X86::NOT32r, X86::EAX,
10824 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010825 case X86::ATOMNAND32:
10826 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010827 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010828 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010829 X86::NOT32r, X86::EAX,
10830 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010831 case X86::ATOMMIN32:
10832 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10833 case X86::ATOMMAX32:
10834 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10835 case X86::ATOMUMIN32:
10836 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10837 case X86::ATOMUMAX32:
10838 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010839
10840 case X86::ATOMAND16:
10841 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10842 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010843 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010844 X86::NOT16r, X86::AX,
10845 X86::GR16RegisterClass);
10846 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010847 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010848 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010849 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010850 X86::NOT16r, X86::AX,
10851 X86::GR16RegisterClass);
10852 case X86::ATOMXOR16:
10853 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10854 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010855 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010856 X86::NOT16r, X86::AX,
10857 X86::GR16RegisterClass);
10858 case X86::ATOMNAND16:
10859 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10860 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010861 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010862 X86::NOT16r, X86::AX,
10863 X86::GR16RegisterClass, true);
10864 case X86::ATOMMIN16:
10865 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10866 case X86::ATOMMAX16:
10867 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10868 case X86::ATOMUMIN16:
10869 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10870 case X86::ATOMUMAX16:
10871 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10872
10873 case X86::ATOMAND8:
10874 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10875 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010876 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010877 X86::NOT8r, X86::AL,
10878 X86::GR8RegisterClass);
10879 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010880 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010881 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010882 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010883 X86::NOT8r, X86::AL,
10884 X86::GR8RegisterClass);
10885 case X86::ATOMXOR8:
10886 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10887 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010888 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010889 X86::NOT8r, X86::AL,
10890 X86::GR8RegisterClass);
10891 case X86::ATOMNAND8:
10892 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10893 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010894 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010895 X86::NOT8r, X86::AL,
10896 X86::GR8RegisterClass, true);
10897 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010898 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010899 case X86::ATOMAND64:
10900 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010901 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010902 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010903 X86::NOT64r, X86::RAX,
10904 X86::GR64RegisterClass);
10905 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010906 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10907 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010908 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010909 X86::NOT64r, X86::RAX,
10910 X86::GR64RegisterClass);
10911 case X86::ATOMXOR64:
10912 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010913 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010914 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010915 X86::NOT64r, X86::RAX,
10916 X86::GR64RegisterClass);
10917 case X86::ATOMNAND64:
10918 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10919 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010920 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010921 X86::NOT64r, X86::RAX,
10922 X86::GR64RegisterClass, true);
10923 case X86::ATOMMIN64:
10924 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10925 case X86::ATOMMAX64:
10926 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10927 case X86::ATOMUMIN64:
10928 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10929 case X86::ATOMUMAX64:
10930 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010931
10932 // This group does 64-bit operations on a 32-bit host.
10933 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010934 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010935 X86::AND32rr, X86::AND32rr,
10936 X86::AND32ri, X86::AND32ri,
10937 false);
10938 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010939 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010940 X86::OR32rr, X86::OR32rr,
10941 X86::OR32ri, X86::OR32ri,
10942 false);
10943 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010944 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010945 X86::XOR32rr, X86::XOR32rr,
10946 X86::XOR32ri, X86::XOR32ri,
10947 false);
10948 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010949 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010950 X86::AND32rr, X86::AND32rr,
10951 X86::AND32ri, X86::AND32ri,
10952 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010953 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010954 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010955 X86::ADD32rr, X86::ADC32rr,
10956 X86::ADD32ri, X86::ADC32ri,
10957 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010958 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010959 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010960 X86::SUB32rr, X86::SBB32rr,
10961 X86::SUB32ri, X86::SBB32ri,
10962 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010963 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010964 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010965 X86::MOV32rr, X86::MOV32rr,
10966 X86::MOV32ri, X86::MOV32ri,
10967 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010968 case X86::VASTART_SAVE_XMM_REGS:
10969 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010970
10971 case X86::VAARG_64:
10972 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010973 }
10974}
10975
10976//===----------------------------------------------------------------------===//
10977// X86 Optimization Hooks
10978//===----------------------------------------------------------------------===//
10979
Dan Gohman475871a2008-07-27 21:46:04 +000010980void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010981 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010982 APInt &KnownZero,
10983 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010984 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010985 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010986 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010987 assert((Opc >= ISD::BUILTIN_OP_END ||
10988 Opc == ISD::INTRINSIC_WO_CHAIN ||
10989 Opc == ISD::INTRINSIC_W_CHAIN ||
10990 Opc == ISD::INTRINSIC_VOID) &&
10991 "Should use MaskedValueIsZero if you don't know whether Op"
10992 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010993
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010994 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010995 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010996 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010997 case X86ISD::ADD:
10998 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010999 case X86ISD::ADC:
11000 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011001 case X86ISD::SMUL:
11002 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000011003 case X86ISD::INC:
11004 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000011005 case X86ISD::OR:
11006 case X86ISD::XOR:
11007 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000011008 // These nodes' second result is a boolean.
11009 if (Op.getResNo() == 0)
11010 break;
11011 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011012 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000011013 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
11014 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000011015 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011016 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011017}
Chris Lattner259e97c2006-01-31 19:43:35 +000011018
Owen Andersonbc146b02010-09-21 20:42:50 +000011019unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
11020 unsigned Depth) const {
11021 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
11022 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
11023 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011024
Owen Andersonbc146b02010-09-21 20:42:50 +000011025 // Fallback case.
11026 return 1;
11027}
11028
Evan Cheng206ee9d2006-07-07 08:33:52 +000011029/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011030/// node is a GlobalAddress + offset.
11031bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011032 const GlobalValue* &GA,
11033 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011034 if (N->getOpcode() == X86ISD::Wrapper) {
11035 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011036 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011037 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011038 return true;
11039 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011040 }
Evan Chengad4196b2008-05-12 19:56:52 +000011041 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011042}
11043
Evan Cheng206ee9d2006-07-07 08:33:52 +000011044/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11045/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11046/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011047/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011048static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011049 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011050 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011051 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011052
Eli Friedman7a5e5552009-06-07 06:52:44 +000011053 if (VT.getSizeInBits() != 128)
11054 return SDValue();
11055
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011056 // Don't create instructions with illegal types after legalize types has run.
11057 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11058 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11059 return SDValue();
11060
Nate Begemanfdea31a2010-03-24 20:49:50 +000011061 SmallVector<SDValue, 16> Elts;
11062 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011063 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011064
Nate Begemanfdea31a2010-03-24 20:49:50 +000011065 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011066}
Evan Chengd880b972008-05-09 21:53:03 +000011067
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011068/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11069/// generation and convert it from being a bunch of shuffles and extracts
11070/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011071static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11072 const TargetLowering &TLI) {
11073 SDValue InputVector = N->getOperand(0);
11074
11075 // Only operate on vectors of 4 elements, where the alternative shuffling
11076 // gets to be more expensive.
11077 if (InputVector.getValueType() != MVT::v4i32)
11078 return SDValue();
11079
11080 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11081 // single use which is a sign-extend or zero-extend, and all elements are
11082 // used.
11083 SmallVector<SDNode *, 4> Uses;
11084 unsigned ExtractedElements = 0;
11085 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11086 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11087 if (UI.getUse().getResNo() != InputVector.getResNo())
11088 return SDValue();
11089
11090 SDNode *Extract = *UI;
11091 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11092 return SDValue();
11093
11094 if (Extract->getValueType(0) != MVT::i32)
11095 return SDValue();
11096 if (!Extract->hasOneUse())
11097 return SDValue();
11098 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11099 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11100 return SDValue();
11101 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11102 return SDValue();
11103
11104 // Record which element was extracted.
11105 ExtractedElements |=
11106 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11107
11108 Uses.push_back(Extract);
11109 }
11110
11111 // If not all the elements were used, this may not be worthwhile.
11112 if (ExtractedElements != 15)
11113 return SDValue();
11114
11115 // Ok, we've now decided to do the transformation.
11116 DebugLoc dl = InputVector.getDebugLoc();
11117
11118 // Store the value to a temporary stack slot.
11119 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011120 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11121 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011122
11123 // Replace each use (extract) with a load of the appropriate element.
11124 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11125 UE = Uses.end(); UI != UE; ++UI) {
11126 SDNode *Extract = *UI;
11127
Nadav Rotem86694292011-05-17 08:31:57 +000011128 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011129 SDValue Idx = Extract->getOperand(1);
11130 unsigned EltSize =
11131 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11132 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11133 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11134
Nadav Rotem86694292011-05-17 08:31:57 +000011135 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011136 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011137
11138 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011139 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011140 ScalarAddr, MachinePointerInfo(),
11141 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011142
11143 // Replace the exact with the load.
11144 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11145 }
11146
11147 // The replacement was made in place; don't return anything.
11148 return SDValue();
11149}
11150
Chris Lattner83e6c992006-10-04 06:57:07 +000011151/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011152static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011153 const X86Subtarget *Subtarget) {
11154 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011155 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011156 // Get the LHS/RHS of the select.
11157 SDValue LHS = N->getOperand(1);
11158 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011159
Dan Gohman670e5392009-09-21 18:03:22 +000011160 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011161 // instructions match the semantics of the common C idiom x<y?x:y but not
11162 // x<=y?x:y, because of how they handle negative zero (which can be
11163 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011164 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011165 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011166 Cond.getOpcode() == ISD::SETCC) {
11167 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011168
Chris Lattner47b4ce82009-03-11 05:48:52 +000011169 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011170 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011171 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11172 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011173 switch (CC) {
11174 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011175 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011176 // Converting this to a min would handle NaNs incorrectly, and swapping
11177 // the operands would cause it to handle comparisons between positive
11178 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011179 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011180 if (!UnsafeFPMath &&
11181 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11182 break;
11183 std::swap(LHS, RHS);
11184 }
Dan Gohman670e5392009-09-21 18:03:22 +000011185 Opcode = X86ISD::FMIN;
11186 break;
11187 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011188 // Converting this to a min would handle comparisons between positive
11189 // and negative zero incorrectly.
11190 if (!UnsafeFPMath &&
11191 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11192 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011193 Opcode = X86ISD::FMIN;
11194 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011195 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011196 // Converting this to a min would handle both negative zeros and NaNs
11197 // incorrectly, but we can swap the operands to fix both.
11198 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011199 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011200 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011201 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011202 Opcode = X86ISD::FMIN;
11203 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011204
Dan Gohman670e5392009-09-21 18:03:22 +000011205 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011206 // Converting this to a max would handle comparisons between positive
11207 // and negative zero incorrectly.
11208 if (!UnsafeFPMath &&
11209 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11210 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011211 Opcode = X86ISD::FMAX;
11212 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011213 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011214 // Converting this to a max would handle NaNs incorrectly, and swapping
11215 // the operands would cause it to handle comparisons between positive
11216 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011217 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011218 if (!UnsafeFPMath &&
11219 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11220 break;
11221 std::swap(LHS, RHS);
11222 }
Dan Gohman670e5392009-09-21 18:03:22 +000011223 Opcode = X86ISD::FMAX;
11224 break;
11225 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011226 // Converting this to a max would handle both negative zeros and NaNs
11227 // incorrectly, but we can swap the operands to fix both.
11228 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011229 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011230 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011231 case ISD::SETGE:
11232 Opcode = X86ISD::FMAX;
11233 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011234 }
Dan Gohman670e5392009-09-21 18:03:22 +000011235 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011236 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11237 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011238 switch (CC) {
11239 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011240 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011241 // Converting this to a min would handle comparisons between positive
11242 // and negative zero incorrectly, and swapping the operands would
11243 // cause it to handle NaNs incorrectly.
11244 if (!UnsafeFPMath &&
11245 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011246 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011247 break;
11248 std::swap(LHS, RHS);
11249 }
Dan Gohman670e5392009-09-21 18:03:22 +000011250 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011251 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011252 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011253 // Converting this to a min would handle NaNs incorrectly.
11254 if (!UnsafeFPMath &&
11255 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11256 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011257 Opcode = X86ISD::FMIN;
11258 break;
11259 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011260 // Converting this to a min would handle both negative zeros and NaNs
11261 // incorrectly, but we can swap the operands to fix both.
11262 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011263 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011264 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011265 case ISD::SETGE:
11266 Opcode = X86ISD::FMIN;
11267 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011268
Dan Gohman670e5392009-09-21 18:03:22 +000011269 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011270 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011271 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011272 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011273 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011274 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011275 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011276 // Converting this to a max would handle comparisons between positive
11277 // and negative zero incorrectly, and swapping the operands would
11278 // cause it to handle NaNs incorrectly.
11279 if (!UnsafeFPMath &&
11280 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011281 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011282 break;
11283 std::swap(LHS, RHS);
11284 }
Dan Gohman670e5392009-09-21 18:03:22 +000011285 Opcode = X86ISD::FMAX;
11286 break;
11287 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011288 // Converting this to a max would handle both negative zeros and NaNs
11289 // incorrectly, but we can swap the operands to fix both.
11290 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011291 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011292 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011293 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011294 Opcode = X86ISD::FMAX;
11295 break;
11296 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011297 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011298
Chris Lattner47b4ce82009-03-11 05:48:52 +000011299 if (Opcode)
11300 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011301 }
Eric Christopherfd179292009-08-27 18:07:15 +000011302
Chris Lattnerd1980a52009-03-12 06:52:53 +000011303 // If this is a select between two integer constants, try to do some
11304 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011305 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11306 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011307 // Don't do this for crazy integer types.
11308 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11309 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011310 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011311 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011312
Chris Lattnercee56e72009-03-13 05:53:31 +000011313 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011314 // Efficiently invertible.
11315 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11316 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11317 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11318 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011319 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011320 }
Eric Christopherfd179292009-08-27 18:07:15 +000011321
Chris Lattnerd1980a52009-03-12 06:52:53 +000011322 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011323 if (FalseC->getAPIntValue() == 0 &&
11324 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011325 if (NeedsCondInvert) // Invert the condition if needed.
11326 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11327 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011328
Chris Lattnerd1980a52009-03-12 06:52:53 +000011329 // Zero extend the condition if needed.
11330 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011331
Chris Lattnercee56e72009-03-13 05:53:31 +000011332 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011333 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011334 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011335 }
Eric Christopherfd179292009-08-27 18:07:15 +000011336
Chris Lattner97a29a52009-03-13 05:22:11 +000011337 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011338 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011339 if (NeedsCondInvert) // Invert the condition if needed.
11340 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11341 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011342
Chris Lattner97a29a52009-03-13 05:22:11 +000011343 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011344 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11345 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011346 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011347 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011348 }
Eric Christopherfd179292009-08-27 18:07:15 +000011349
Chris Lattnercee56e72009-03-13 05:53:31 +000011350 // Optimize cases that will turn into an LEA instruction. This requires
11351 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011352 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011353 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011354 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011355
Chris Lattnercee56e72009-03-13 05:53:31 +000011356 bool isFastMultiplier = false;
11357 if (Diff < 10) {
11358 switch ((unsigned char)Diff) {
11359 default: break;
11360 case 1: // result = add base, cond
11361 case 2: // result = lea base( , cond*2)
11362 case 3: // result = lea base(cond, cond*2)
11363 case 4: // result = lea base( , cond*4)
11364 case 5: // result = lea base(cond, cond*4)
11365 case 8: // result = lea base( , cond*8)
11366 case 9: // result = lea base(cond, cond*8)
11367 isFastMultiplier = true;
11368 break;
11369 }
11370 }
Eric Christopherfd179292009-08-27 18:07:15 +000011371
Chris Lattnercee56e72009-03-13 05:53:31 +000011372 if (isFastMultiplier) {
11373 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11374 if (NeedsCondInvert) // Invert the condition if needed.
11375 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11376 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011377
Chris Lattnercee56e72009-03-13 05:53:31 +000011378 // Zero extend the condition if needed.
11379 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11380 Cond);
11381 // Scale the condition by the difference.
11382 if (Diff != 1)
11383 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11384 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011385
Chris Lattnercee56e72009-03-13 05:53:31 +000011386 // Add the base if non-zero.
11387 if (FalseC->getAPIntValue() != 0)
11388 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11389 SDValue(FalseC, 0));
11390 return Cond;
11391 }
Eric Christopherfd179292009-08-27 18:07:15 +000011392 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011393 }
11394 }
Eric Christopherfd179292009-08-27 18:07:15 +000011395
Dan Gohman475871a2008-07-27 21:46:04 +000011396 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011397}
11398
Chris Lattnerd1980a52009-03-12 06:52:53 +000011399/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11400static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11401 TargetLowering::DAGCombinerInfo &DCI) {
11402 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011403
Chris Lattnerd1980a52009-03-12 06:52:53 +000011404 // If the flag operand isn't dead, don't touch this CMOV.
11405 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11406 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011407
Evan Chengb5a55d92011-05-24 01:48:22 +000011408 SDValue FalseOp = N->getOperand(0);
11409 SDValue TrueOp = N->getOperand(1);
11410 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11411 SDValue Cond = N->getOperand(3);
11412 if (CC == X86::COND_E || CC == X86::COND_NE) {
11413 switch (Cond.getOpcode()) {
11414 default: break;
11415 case X86ISD::BSR:
11416 case X86ISD::BSF:
11417 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11418 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11419 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11420 }
11421 }
11422
Chris Lattnerd1980a52009-03-12 06:52:53 +000011423 // If this is a select between two integer constants, try to do some
11424 // optimizations. Note that the operands are ordered the opposite of SELECT
11425 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011426 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11427 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011428 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11429 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011430 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11431 CC = X86::GetOppositeBranchCondition(CC);
11432 std::swap(TrueC, FalseC);
11433 }
Eric Christopherfd179292009-08-27 18:07:15 +000011434
Chris Lattnerd1980a52009-03-12 06:52:53 +000011435 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011436 // This is efficient for any integer data type (including i8/i16) and
11437 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011438 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011439 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11440 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011441
Chris Lattnerd1980a52009-03-12 06:52:53 +000011442 // Zero extend the condition if needed.
11443 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011444
Chris Lattnerd1980a52009-03-12 06:52:53 +000011445 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11446 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011447 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011448 if (N->getNumValues() == 2) // Dead flag value?
11449 return DCI.CombineTo(N, Cond, SDValue());
11450 return Cond;
11451 }
Eric Christopherfd179292009-08-27 18:07:15 +000011452
Chris Lattnercee56e72009-03-13 05:53:31 +000011453 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11454 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011455 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011456 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11457 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011458
Chris Lattner97a29a52009-03-13 05:22:11 +000011459 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011460 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11461 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011462 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11463 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011464
Chris Lattner97a29a52009-03-13 05:22:11 +000011465 if (N->getNumValues() == 2) // Dead flag value?
11466 return DCI.CombineTo(N, Cond, SDValue());
11467 return Cond;
11468 }
Eric Christopherfd179292009-08-27 18:07:15 +000011469
Chris Lattnercee56e72009-03-13 05:53:31 +000011470 // Optimize cases that will turn into an LEA instruction. This requires
11471 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011472 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011473 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011474 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011475
Chris Lattnercee56e72009-03-13 05:53:31 +000011476 bool isFastMultiplier = false;
11477 if (Diff < 10) {
11478 switch ((unsigned char)Diff) {
11479 default: break;
11480 case 1: // result = add base, cond
11481 case 2: // result = lea base( , cond*2)
11482 case 3: // result = lea base(cond, cond*2)
11483 case 4: // result = lea base( , cond*4)
11484 case 5: // result = lea base(cond, cond*4)
11485 case 8: // result = lea base( , cond*8)
11486 case 9: // result = lea base(cond, cond*8)
11487 isFastMultiplier = true;
11488 break;
11489 }
11490 }
Eric Christopherfd179292009-08-27 18:07:15 +000011491
Chris Lattnercee56e72009-03-13 05:53:31 +000011492 if (isFastMultiplier) {
11493 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011494 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11495 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011496 // Zero extend the condition if needed.
11497 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11498 Cond);
11499 // Scale the condition by the difference.
11500 if (Diff != 1)
11501 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11502 DAG.getConstant(Diff, Cond.getValueType()));
11503
11504 // Add the base if non-zero.
11505 if (FalseC->getAPIntValue() != 0)
11506 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11507 SDValue(FalseC, 0));
11508 if (N->getNumValues() == 2) // Dead flag value?
11509 return DCI.CombineTo(N, Cond, SDValue());
11510 return Cond;
11511 }
Eric Christopherfd179292009-08-27 18:07:15 +000011512 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011513 }
11514 }
11515 return SDValue();
11516}
11517
11518
Evan Cheng0b0cd912009-03-28 05:57:29 +000011519/// PerformMulCombine - Optimize a single multiply with constant into two
11520/// in order to implement it with two cheaper instructions, e.g.
11521/// LEA + SHL, LEA + LEA.
11522static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11523 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011524 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11525 return SDValue();
11526
Owen Andersone50ed302009-08-10 22:56:29 +000011527 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011528 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011529 return SDValue();
11530
11531 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11532 if (!C)
11533 return SDValue();
11534 uint64_t MulAmt = C->getZExtValue();
11535 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11536 return SDValue();
11537
11538 uint64_t MulAmt1 = 0;
11539 uint64_t MulAmt2 = 0;
11540 if ((MulAmt % 9) == 0) {
11541 MulAmt1 = 9;
11542 MulAmt2 = MulAmt / 9;
11543 } else if ((MulAmt % 5) == 0) {
11544 MulAmt1 = 5;
11545 MulAmt2 = MulAmt / 5;
11546 } else if ((MulAmt % 3) == 0) {
11547 MulAmt1 = 3;
11548 MulAmt2 = MulAmt / 3;
11549 }
11550 if (MulAmt2 &&
11551 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11552 DebugLoc DL = N->getDebugLoc();
11553
11554 if (isPowerOf2_64(MulAmt2) &&
11555 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11556 // If second multiplifer is pow2, issue it first. We want the multiply by
11557 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11558 // is an add.
11559 std::swap(MulAmt1, MulAmt2);
11560
11561 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011562 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011563 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011564 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011565 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011566 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011567 DAG.getConstant(MulAmt1, VT));
11568
Eric Christopherfd179292009-08-27 18:07:15 +000011569 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011570 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011571 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011572 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011573 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011574 DAG.getConstant(MulAmt2, VT));
11575
11576 // Do not add new nodes to DAG combiner worklist.
11577 DCI.CombineTo(N, NewMul, false);
11578 }
11579 return SDValue();
11580}
11581
Evan Chengad9c0a32009-12-15 00:53:42 +000011582static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11583 SDValue N0 = N->getOperand(0);
11584 SDValue N1 = N->getOperand(1);
11585 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11586 EVT VT = N0.getValueType();
11587
11588 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11589 // since the result of setcc_c is all zero's or all ones.
11590 if (N1C && N0.getOpcode() == ISD::AND &&
11591 N0.getOperand(1).getOpcode() == ISD::Constant) {
11592 SDValue N00 = N0.getOperand(0);
11593 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11594 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11595 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11596 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11597 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11598 APInt ShAmt = N1C->getAPIntValue();
11599 Mask = Mask.shl(ShAmt);
11600 if (Mask != 0)
11601 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11602 N00, DAG.getConstant(Mask, VT));
11603 }
11604 }
11605
11606 return SDValue();
11607}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011608
Nate Begeman740ab032009-01-26 00:52:55 +000011609/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11610/// when possible.
11611static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11612 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011613 EVT VT = N->getValueType(0);
11614 if (!VT.isVector() && VT.isInteger() &&
11615 N->getOpcode() == ISD::SHL)
11616 return PerformSHLCombine(N, DAG);
11617
Nate Begeman740ab032009-01-26 00:52:55 +000011618 // On X86 with SSE2 support, we can transform this to a vector shift if
11619 // all elements are shifted by the same amount. We can't do this in legalize
11620 // because the a constant vector is typically transformed to a constant pool
11621 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011622 if (!Subtarget->hasSSE2())
11623 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011624
Owen Anderson825b72b2009-08-11 20:47:22 +000011625 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011626 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011627
Mon P Wang3becd092009-01-28 08:12:05 +000011628 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011629 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011630 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011631 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011632 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11633 unsigned NumElts = VT.getVectorNumElements();
11634 unsigned i = 0;
11635 for (; i != NumElts; ++i) {
11636 SDValue Arg = ShAmtOp.getOperand(i);
11637 if (Arg.getOpcode() == ISD::UNDEF) continue;
11638 BaseShAmt = Arg;
11639 break;
11640 }
11641 for (; i != NumElts; ++i) {
11642 SDValue Arg = ShAmtOp.getOperand(i);
11643 if (Arg.getOpcode() == ISD::UNDEF) continue;
11644 if (Arg != BaseShAmt) {
11645 return SDValue();
11646 }
11647 }
11648 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011649 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011650 SDValue InVec = ShAmtOp.getOperand(0);
11651 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11652 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11653 unsigned i = 0;
11654 for (; i != NumElts; ++i) {
11655 SDValue Arg = InVec.getOperand(i);
11656 if (Arg.getOpcode() == ISD::UNDEF) continue;
11657 BaseShAmt = Arg;
11658 break;
11659 }
11660 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11661 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011662 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011663 if (C->getZExtValue() == SplatIdx)
11664 BaseShAmt = InVec.getOperand(1);
11665 }
11666 }
11667 if (BaseShAmt.getNode() == 0)
11668 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11669 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011670 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011671 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011672
Mon P Wangefa42202009-09-03 19:56:25 +000011673 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011674 if (EltVT.bitsGT(MVT::i32))
11675 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11676 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011677 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011678
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011679 // The shift amount is identical so we can do a vector shift.
11680 SDValue ValOp = N->getOperand(0);
11681 switch (N->getOpcode()) {
11682 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011683 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011684 break;
11685 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011686 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011687 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011688 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011689 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011690 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011691 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011692 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011693 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011694 if (VT == MVT::v8i16)
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_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011697 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011698 break;
11699 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011700 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011701 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011702 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011703 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011704 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011705 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011706 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011707 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011708 break;
11709 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011710 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011711 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011712 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011713 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011714 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011715 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011716 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011717 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011718 if (VT == MVT::v8i16)
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_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011721 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011722 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011723 }
11724 return SDValue();
11725}
11726
Nate Begemanb65c1752010-12-17 22:55:37 +000011727
Stuart Hastings865f0932011-06-03 23:53:54 +000011728// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11729// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11730// and friends. Likewise for OR -> CMPNEQSS.
11731static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11732 TargetLowering::DAGCombinerInfo &DCI,
11733 const X86Subtarget *Subtarget) {
11734 unsigned opcode;
11735
11736 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11737 // we're requiring SSE2 for both.
11738 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11739 SDValue N0 = N->getOperand(0);
11740 SDValue N1 = N->getOperand(1);
11741 SDValue CMP0 = N0->getOperand(1);
11742 SDValue CMP1 = N1->getOperand(1);
11743 DebugLoc DL = N->getDebugLoc();
11744
11745 // The SETCCs should both refer to the same CMP.
11746 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11747 return SDValue();
11748
11749 SDValue CMP00 = CMP0->getOperand(0);
11750 SDValue CMP01 = CMP0->getOperand(1);
11751 EVT VT = CMP00.getValueType();
11752
11753 if (VT == MVT::f32 || VT == MVT::f64) {
11754 bool ExpectingFlags = false;
11755 // Check for any users that want flags:
11756 for (SDNode::use_iterator UI = N->use_begin(),
11757 UE = N->use_end();
11758 !ExpectingFlags && UI != UE; ++UI)
11759 switch (UI->getOpcode()) {
11760 default:
11761 case ISD::BR_CC:
11762 case ISD::BRCOND:
11763 case ISD::SELECT:
11764 ExpectingFlags = true;
11765 break;
11766 case ISD::CopyToReg:
11767 case ISD::SIGN_EXTEND:
11768 case ISD::ZERO_EXTEND:
11769 case ISD::ANY_EXTEND:
11770 break;
11771 }
11772
11773 if (!ExpectingFlags) {
11774 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11775 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11776
11777 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11778 X86::CondCode tmp = cc0;
11779 cc0 = cc1;
11780 cc1 = tmp;
11781 }
11782
11783 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11784 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11785 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11786 X86ISD::NodeType NTOperator = is64BitFP ?
11787 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11788 // FIXME: need symbolic constants for these magic numbers.
11789 // See X86ATTInstPrinter.cpp:printSSECC().
11790 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11791 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11792 DAG.getConstant(x86cc, MVT::i8));
11793 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11794 OnesOrZeroesF);
11795 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11796 DAG.getConstant(1, MVT::i32));
11797 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11798 return OneBitOfTruth;
11799 }
11800 }
11801 }
11802 }
11803 return SDValue();
11804}
11805
Nate Begemanb65c1752010-12-17 22:55:37 +000011806static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11807 TargetLowering::DAGCombinerInfo &DCI,
11808 const X86Subtarget *Subtarget) {
11809 if (DCI.isBeforeLegalizeOps())
11810 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011811
Stuart Hastings865f0932011-06-03 23:53:54 +000011812 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11813 if (R.getNode())
11814 return R;
11815
Nate Begemanb65c1752010-12-17 22:55:37 +000011816 // Want to form PANDN nodes, in the hopes of then easily combining them with
11817 // OR and AND nodes to form PBLEND/PSIGN.
11818 EVT VT = N->getValueType(0);
11819 if (VT != MVT::v2i64)
11820 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011821
Nate Begemanb65c1752010-12-17 22:55:37 +000011822 SDValue N0 = N->getOperand(0);
11823 SDValue N1 = N->getOperand(1);
11824 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011825
Nate Begemanb65c1752010-12-17 22:55:37 +000011826 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011827 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011828 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11829 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11830
11831 // Check RHS for vnot
11832 if (N1.getOpcode() == ISD::XOR &&
11833 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11834 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011835
Nate Begemanb65c1752010-12-17 22:55:37 +000011836 return SDValue();
11837}
11838
Evan Cheng760d1942010-01-04 21:22:48 +000011839static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011840 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011841 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011842 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011843 return SDValue();
11844
Stuart Hastings865f0932011-06-03 23:53:54 +000011845 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11846 if (R.getNode())
11847 return R;
11848
Evan Cheng760d1942010-01-04 21:22:48 +000011849 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011850 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011851 return SDValue();
11852
Evan Cheng760d1942010-01-04 21:22:48 +000011853 SDValue N0 = N->getOperand(0);
11854 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011855
Nate Begemanb65c1752010-12-17 22:55:37 +000011856 // look for psign/blend
11857 if (Subtarget->hasSSSE3()) {
11858 if (VT == MVT::v2i64) {
11859 // Canonicalize pandn to RHS
11860 if (N0.getOpcode() == X86ISD::PANDN)
11861 std::swap(N0, N1);
11862 // or (and (m, x), (pandn m, y))
11863 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11864 SDValue Mask = N1.getOperand(0);
11865 SDValue X = N1.getOperand(1);
11866 SDValue Y;
11867 if (N0.getOperand(0) == Mask)
11868 Y = N0.getOperand(1);
11869 if (N0.getOperand(1) == Mask)
11870 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011871
Nate Begemanb65c1752010-12-17 22:55:37 +000011872 // Check to see if the mask appeared in both the AND and PANDN and
11873 if (!Y.getNode())
11874 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011875
Nate Begemanb65c1752010-12-17 22:55:37 +000011876 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11877 if (Mask.getOpcode() != ISD::BITCAST ||
11878 X.getOpcode() != ISD::BITCAST ||
11879 Y.getOpcode() != ISD::BITCAST)
11880 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011881
Nate Begemanb65c1752010-12-17 22:55:37 +000011882 // Look through mask bitcast.
11883 Mask = Mask.getOperand(0);
11884 EVT MaskVT = Mask.getValueType();
11885
11886 // Validate that the Mask operand is a vector sra node. The sra node
11887 // will be an intrinsic.
11888 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11889 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011890
Nate Begemanb65c1752010-12-17 22:55:37 +000011891 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11892 // there is no psrai.b
11893 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11894 case Intrinsic::x86_sse2_psrai_w:
11895 case Intrinsic::x86_sse2_psrai_d:
11896 break;
11897 default: return SDValue();
11898 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011899
Nate Begemanb65c1752010-12-17 22:55:37 +000011900 // Check that the SRA is all signbits.
11901 SDValue SraC = Mask.getOperand(2);
11902 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11903 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11904 if ((SraAmt + 1) != EltBits)
11905 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011906
Nate Begemanb65c1752010-12-17 22:55:37 +000011907 DebugLoc DL = N->getDebugLoc();
11908
11909 // Now we know we at least have a plendvb with the mask val. See if
11910 // we can form a psignb/w/d.
11911 // psign = x.type == y.type == mask.type && y = sub(0, x);
11912 X = X.getOperand(0);
11913 Y = Y.getOperand(0);
11914 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11915 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11916 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11917 unsigned Opc = 0;
11918 switch (EltBits) {
11919 case 8: Opc = X86ISD::PSIGNB; break;
11920 case 16: Opc = X86ISD::PSIGNW; break;
11921 case 32: Opc = X86ISD::PSIGND; break;
11922 default: break;
11923 }
11924 if (Opc) {
11925 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11926 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11927 }
11928 }
11929 // PBLENDVB only available on SSE 4.1
11930 if (!Subtarget->hasSSE41())
11931 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011932
Nate Begemanb65c1752010-12-17 22:55:37 +000011933 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11934 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11935 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011936 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011937 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11938 }
11939 }
11940 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011941
Nate Begemanb65c1752010-12-17 22:55:37 +000011942 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011943 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11944 std::swap(N0, N1);
11945 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11946 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011947 if (!N0.hasOneUse() || !N1.hasOneUse())
11948 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011949
11950 SDValue ShAmt0 = N0.getOperand(1);
11951 if (ShAmt0.getValueType() != MVT::i8)
11952 return SDValue();
11953 SDValue ShAmt1 = N1.getOperand(1);
11954 if (ShAmt1.getValueType() != MVT::i8)
11955 return SDValue();
11956 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11957 ShAmt0 = ShAmt0.getOperand(0);
11958 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11959 ShAmt1 = ShAmt1.getOperand(0);
11960
11961 DebugLoc DL = N->getDebugLoc();
11962 unsigned Opc = X86ISD::SHLD;
11963 SDValue Op0 = N0.getOperand(0);
11964 SDValue Op1 = N1.getOperand(0);
11965 if (ShAmt0.getOpcode() == ISD::SUB) {
11966 Opc = X86ISD::SHRD;
11967 std::swap(Op0, Op1);
11968 std::swap(ShAmt0, ShAmt1);
11969 }
11970
Evan Cheng8b1190a2010-04-28 01:18:01 +000011971 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011972 if (ShAmt1.getOpcode() == ISD::SUB) {
11973 SDValue Sum = ShAmt1.getOperand(0);
11974 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011975 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11976 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11977 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11978 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011979 return DAG.getNode(Opc, DL, VT,
11980 Op0, Op1,
11981 DAG.getNode(ISD::TRUNCATE, DL,
11982 MVT::i8, ShAmt0));
11983 }
11984 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11985 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11986 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011987 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011988 return DAG.getNode(Opc, DL, VT,
11989 N0.getOperand(0), N1.getOperand(0),
11990 DAG.getNode(ISD::TRUNCATE, DL,
11991 MVT::i8, ShAmt0));
11992 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011993
Evan Cheng760d1942010-01-04 21:22:48 +000011994 return SDValue();
11995}
11996
Chris Lattner149a4e52008-02-22 02:09:43 +000011997/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011998static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011999 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000012000 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
12001 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000012002 // A preferable solution to the general problem is to figure out the right
12003 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000012004
12005 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000012006 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000012007 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000012008 if (VT.getSizeInBits() != 64)
12009 return SDValue();
12010
Devang Patel578efa92009-06-05 21:57:13 +000012011 const Function *F = DAG.getMachineFunction().getFunction();
12012 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000012013 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000012014 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000012015 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000012016 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000012017 isa<LoadSDNode>(St->getValue()) &&
12018 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
12019 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012020 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012021 LoadSDNode *Ld = 0;
12022 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000012023 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000012024 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012025 // Must be a store of a load. We currently handle two cases: the load
12026 // is a direct child, and it's under an intervening TokenFactor. It is
12027 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000012028 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000012029 Ld = cast<LoadSDNode>(St->getChain());
12030 else if (St->getValue().hasOneUse() &&
12031 ChainVal->getOpcode() == ISD::TokenFactor) {
12032 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000012033 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000012034 TokenFactorIndex = i;
12035 Ld = cast<LoadSDNode>(St->getValue());
12036 } else
12037 Ops.push_back(ChainVal->getOperand(i));
12038 }
12039 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000012040
Evan Cheng536e6672009-03-12 05:59:15 +000012041 if (!Ld || !ISD::isNormalLoad(Ld))
12042 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012043
Evan Cheng536e6672009-03-12 05:59:15 +000012044 // If this is not the MMX case, i.e. we are just turning i64 load/store
12045 // into f64 load/store, avoid the transformation if there are multiple
12046 // uses of the loaded value.
12047 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12048 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012049
Evan Cheng536e6672009-03-12 05:59:15 +000012050 DebugLoc LdDL = Ld->getDebugLoc();
12051 DebugLoc StDL = N->getDebugLoc();
12052 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12053 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12054 // pair instead.
12055 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012056 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012057 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12058 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012059 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012060 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012061 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012062 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012063 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012064 Ops.size());
12065 }
Evan Cheng536e6672009-03-12 05:59:15 +000012066 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012067 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012068 St->isVolatile(), St->isNonTemporal(),
12069 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012070 }
Evan Cheng536e6672009-03-12 05:59:15 +000012071
12072 // Otherwise, lower to two pairs of 32-bit loads / stores.
12073 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012074 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12075 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012076
Owen Anderson825b72b2009-08-11 20:47:22 +000012077 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012078 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012079 Ld->isVolatile(), Ld->isNonTemporal(),
12080 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012081 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012082 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012083 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012084 MinAlign(Ld->getAlignment(), 4));
12085
12086 SDValue NewChain = LoLd.getValue(1);
12087 if (TokenFactorIndex != -1) {
12088 Ops.push_back(LoLd);
12089 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012090 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012091 Ops.size());
12092 }
12093
12094 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012095 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12096 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012097
12098 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012099 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012100 St->isVolatile(), St->isNonTemporal(),
12101 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012102 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012103 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012104 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012105 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012106 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012107 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012108 }
Dan Gohman475871a2008-07-27 21:46:04 +000012109 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012110}
12111
Chris Lattner6cf73262008-01-25 06:14:17 +000012112/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12113/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012114static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012115 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12116 // F[X]OR(0.0, x) -> x
12117 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012118 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12119 if (C->getValueAPF().isPosZero())
12120 return N->getOperand(1);
12121 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12122 if (C->getValueAPF().isPosZero())
12123 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012124 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012125}
12126
12127/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012128static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012129 // FAND(0.0, x) -> 0.0
12130 // FAND(x, 0.0) -> 0.0
12131 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12132 if (C->getValueAPF().isPosZero())
12133 return N->getOperand(0);
12134 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12135 if (C->getValueAPF().isPosZero())
12136 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012137 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012138}
12139
Dan Gohmane5af2d32009-01-29 01:59:02 +000012140static SDValue PerformBTCombine(SDNode *N,
12141 SelectionDAG &DAG,
12142 TargetLowering::DAGCombinerInfo &DCI) {
12143 // BT ignores high bits in the bit index operand.
12144 SDValue Op1 = N->getOperand(1);
12145 if (Op1.hasOneUse()) {
12146 unsigned BitWidth = Op1.getValueSizeInBits();
12147 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12148 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012149 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12150 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012151 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012152 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12153 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12154 DCI.CommitTargetLoweringOpt(TLO);
12155 }
12156 return SDValue();
12157}
Chris Lattner83e6c992006-10-04 06:57:07 +000012158
Eli Friedman7a5e5552009-06-07 06:52:44 +000012159static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12160 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012161 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012162 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012163 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012164 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012165 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012166 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012167 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012168 }
12169 return SDValue();
12170}
12171
Evan Cheng2e489c42009-12-16 00:53:11 +000012172static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12173 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12174 // (and (i32 x86isd::setcc_carry), 1)
12175 // This eliminates the zext. This transformation is necessary because
12176 // ISD::SETCC is always legalized to i8.
12177 DebugLoc dl = N->getDebugLoc();
12178 SDValue N0 = N->getOperand(0);
12179 EVT VT = N->getValueType(0);
12180 if (N0.getOpcode() == ISD::AND &&
12181 N0.hasOneUse() &&
12182 N0.getOperand(0).hasOneUse()) {
12183 SDValue N00 = N0.getOperand(0);
12184 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12185 return SDValue();
12186 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12187 if (!C || C->getZExtValue() != 1)
12188 return SDValue();
12189 return DAG.getNode(ISD::AND, dl, VT,
12190 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12191 N00.getOperand(0), N00.getOperand(1)),
12192 DAG.getConstant(1, VT));
12193 }
12194
12195 return SDValue();
12196}
12197
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012198// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12199static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12200 unsigned X86CC = N->getConstantOperandVal(0);
12201 SDValue EFLAG = N->getOperand(1);
12202 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012203
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012204 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12205 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12206 // cases.
12207 if (X86CC == X86::COND_B)
12208 return DAG.getNode(ISD::AND, DL, MVT::i8,
12209 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12210 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12211 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012212
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012213 return SDValue();
12214}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012215
Benjamin Kramer1396c402011-06-18 11:09:41 +000012216static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
12217 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012218 SDValue Op0 = N->getOperand(0);
12219 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12220 // a 32-bit target where SSE doesn't support i64->FP operations.
12221 if (Op0.getOpcode() == ISD::LOAD) {
12222 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12223 EVT VT = Ld->getValueType(0);
12224 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12225 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12226 !XTLI->getSubtarget()->is64Bit() &&
12227 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000012228 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
12229 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012230 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12231 return FILDChain;
12232 }
12233 }
12234 return SDValue();
12235}
12236
Chris Lattner23a01992010-12-20 01:37:09 +000012237// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12238static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12239 X86TargetLowering::DAGCombinerInfo &DCI) {
12240 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12241 // the result is either zero or one (depending on the input carry bit).
12242 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12243 if (X86::isZeroNode(N->getOperand(0)) &&
12244 X86::isZeroNode(N->getOperand(1)) &&
12245 // We don't have a good way to replace an EFLAGS use, so only do this when
12246 // dead right now.
12247 SDValue(N, 1).use_empty()) {
12248 DebugLoc DL = N->getDebugLoc();
12249 EVT VT = N->getValueType(0);
12250 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12251 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12252 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12253 DAG.getConstant(X86::COND_B,MVT::i8),
12254 N->getOperand(2)),
12255 DAG.getConstant(1, VT));
12256 return DCI.CombineTo(N, Res1, CarryOut);
12257 }
12258
12259 return SDValue();
12260}
12261
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012262// fold (add Y, (sete X, 0)) -> adc 0, Y
12263// (add Y, (setne X, 0)) -> sbb -1, Y
12264// (sub (sete X, 0), Y) -> sbb 0, Y
12265// (sub (setne X, 0), Y) -> adc -1, Y
12266static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12267 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012268
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012269 // Look through ZExts.
12270 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12271 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12272 return SDValue();
12273
12274 SDValue SetCC = Ext.getOperand(0);
12275 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12276 return SDValue();
12277
12278 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12279 if (CC != X86::COND_E && CC != X86::COND_NE)
12280 return SDValue();
12281
12282 SDValue Cmp = SetCC.getOperand(1);
12283 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012284 !X86::isZeroNode(Cmp.getOperand(1)) ||
12285 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012286 return SDValue();
12287
12288 SDValue CmpOp0 = Cmp.getOperand(0);
12289 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12290 DAG.getConstant(1, CmpOp0.getValueType()));
12291
12292 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12293 if (CC == X86::COND_NE)
12294 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12295 DL, OtherVal.getValueType(), OtherVal,
12296 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12297 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12298 DL, OtherVal.getValueType(), OtherVal,
12299 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12300}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012301
Dan Gohman475871a2008-07-27 21:46:04 +000012302SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012303 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012304 SelectionDAG &DAG = DCI.DAG;
12305 switch (N->getOpcode()) {
12306 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012307 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012308 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012309 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012310 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012311 case ISD::ADD:
12312 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012313 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012314 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012315 case ISD::SHL:
12316 case ISD::SRA:
12317 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012318 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012319 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012320 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012321 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012322 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012323 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12324 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012325 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012326 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012327 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012328 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012329 case X86ISD::SHUFPS: // Handle all target specific shuffles
12330 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012331 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012332 case X86ISD::PUNPCKHBW:
12333 case X86ISD::PUNPCKHWD:
12334 case X86ISD::PUNPCKHDQ:
12335 case X86ISD::PUNPCKHQDQ:
12336 case X86ISD::UNPCKHPS:
12337 case X86ISD::UNPCKHPD:
12338 case X86ISD::PUNPCKLBW:
12339 case X86ISD::PUNPCKLWD:
12340 case X86ISD::PUNPCKLDQ:
12341 case X86ISD::PUNPCKLQDQ:
12342 case X86ISD::UNPCKLPS:
12343 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012344 case X86ISD::VUNPCKLPS:
12345 case X86ISD::VUNPCKLPD:
12346 case X86ISD::VUNPCKLPSY:
12347 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012348 case X86ISD::MOVHLPS:
12349 case X86ISD::MOVLHPS:
12350 case X86ISD::PSHUFD:
12351 case X86ISD::PSHUFHW:
12352 case X86ISD::PSHUFLW:
12353 case X86ISD::MOVSS:
12354 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012355 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012356 }
12357
Dan Gohman475871a2008-07-27 21:46:04 +000012358 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012359}
12360
Evan Chenge5b51ac2010-04-17 06:13:15 +000012361/// isTypeDesirableForOp - Return true if the target has native support for
12362/// the specified value type and it is 'desirable' to use the type for the
12363/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12364/// instruction encodings are longer and some i16 instructions are slow.
12365bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12366 if (!isTypeLegal(VT))
12367 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012368 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012369 return true;
12370
12371 switch (Opc) {
12372 default:
12373 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012374 case ISD::LOAD:
12375 case ISD::SIGN_EXTEND:
12376 case ISD::ZERO_EXTEND:
12377 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012378 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012379 case ISD::SRL:
12380 case ISD::SUB:
12381 case ISD::ADD:
12382 case ISD::MUL:
12383 case ISD::AND:
12384 case ISD::OR:
12385 case ISD::XOR:
12386 return false;
12387 }
12388}
12389
12390/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012391/// beneficial for dag combiner to promote the specified node. If true, it
12392/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012393bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012394 EVT VT = Op.getValueType();
12395 if (VT != MVT::i16)
12396 return false;
12397
Evan Cheng4c26e932010-04-19 19:29:22 +000012398 bool Promote = false;
12399 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012400 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012401 default: break;
12402 case ISD::LOAD: {
12403 LoadSDNode *LD = cast<LoadSDNode>(Op);
12404 // If the non-extending load has a single use and it's not live out, then it
12405 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012406 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12407 Op.hasOneUse()*/) {
12408 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12409 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12410 // The only case where we'd want to promote LOAD (rather then it being
12411 // promoted as an operand is when it's only use is liveout.
12412 if (UI->getOpcode() != ISD::CopyToReg)
12413 return false;
12414 }
12415 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012416 Promote = true;
12417 break;
12418 }
12419 case ISD::SIGN_EXTEND:
12420 case ISD::ZERO_EXTEND:
12421 case ISD::ANY_EXTEND:
12422 Promote = true;
12423 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012424 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012425 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012426 SDValue N0 = Op.getOperand(0);
12427 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012428 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012429 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012430 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012431 break;
12432 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012433 case ISD::ADD:
12434 case ISD::MUL:
12435 case ISD::AND:
12436 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012437 case ISD::XOR:
12438 Commute = true;
12439 // fallthrough
12440 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012441 SDValue N0 = Op.getOperand(0);
12442 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012443 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012444 return false;
12445 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012446 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012447 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012448 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012449 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012450 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012451 }
12452 }
12453
12454 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012455 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012456}
12457
Evan Cheng60c07e12006-07-05 22:17:51 +000012458//===----------------------------------------------------------------------===//
12459// X86 Inline Assembly Support
12460//===----------------------------------------------------------------------===//
12461
Chris Lattnerb8105652009-07-20 17:51:36 +000012462bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12463 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012464
12465 std::string AsmStr = IA->getAsmString();
12466
12467 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012468 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012469 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012470
12471 switch (AsmPieces.size()) {
12472 default: return false;
12473 case 1:
12474 AsmStr = AsmPieces[0];
12475 AsmPieces.clear();
12476 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12477
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012478 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012479 // we will turn this bswap into something that will be lowered to logical ops
12480 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12481 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012482 // bswap $0
12483 if (AsmPieces.size() == 2 &&
12484 (AsmPieces[0] == "bswap" ||
12485 AsmPieces[0] == "bswapq" ||
12486 AsmPieces[0] == "bswapl") &&
12487 (AsmPieces[1] == "$0" ||
12488 AsmPieces[1] == "${0:q}")) {
12489 // No need to check constraints, nothing other than the equivalent of
12490 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012491 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12492 if (!Ty || Ty->getBitWidth() % 16 != 0)
12493 return false;
12494 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012495 }
12496 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012497 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012498 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012499 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012500 AsmPieces[1] == "$$8," &&
12501 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012502 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12503 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012504 const std::string &ConstraintsStr = IA->getConstraintString();
12505 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012506 std::sort(AsmPieces.begin(), AsmPieces.end());
12507 if (AsmPieces.size() == 4 &&
12508 AsmPieces[0] == "~{cc}" &&
12509 AsmPieces[1] == "~{dirflag}" &&
12510 AsmPieces[2] == "~{flags}" &&
12511 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012512 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12513 if (!Ty || Ty->getBitWidth() % 16 != 0)
12514 return false;
12515 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012516 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012517 }
12518 break;
12519 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012520 if (CI->getType()->isIntegerTy(32) &&
12521 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12522 SmallVector<StringRef, 4> Words;
12523 SplitString(AsmPieces[0], Words, " \t,");
12524 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12525 Words[2] == "${0:w}") {
12526 Words.clear();
12527 SplitString(AsmPieces[1], Words, " \t,");
12528 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12529 Words[2] == "$0") {
12530 Words.clear();
12531 SplitString(AsmPieces[2], Words, " \t,");
12532 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12533 Words[2] == "${0:w}") {
12534 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012535 const std::string &ConstraintsStr = IA->getConstraintString();
12536 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012537 std::sort(AsmPieces.begin(), AsmPieces.end());
12538 if (AsmPieces.size() == 4 &&
12539 AsmPieces[0] == "~{cc}" &&
12540 AsmPieces[1] == "~{dirflag}" &&
12541 AsmPieces[2] == "~{flags}" &&
12542 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012543 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12544 if (!Ty || Ty->getBitWidth() % 16 != 0)
12545 return false;
12546 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012547 }
12548 }
12549 }
12550 }
12551 }
Evan Cheng55d42002011-01-08 01:24:27 +000012552
12553 if (CI->getType()->isIntegerTy(64)) {
12554 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12555 if (Constraints.size() >= 2 &&
12556 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12557 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12558 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12559 SmallVector<StringRef, 4> Words;
12560 SplitString(AsmPieces[0], Words, " \t");
12561 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012562 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012563 SplitString(AsmPieces[1], Words, " \t");
12564 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12565 Words.clear();
12566 SplitString(AsmPieces[2], Words, " \t,");
12567 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12568 Words[2] == "%edx") {
12569 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12570 if (!Ty || Ty->getBitWidth() % 16 != 0)
12571 return false;
12572 return IntrinsicLowering::LowerToByteSwap(CI);
12573 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012574 }
12575 }
12576 }
12577 }
12578 break;
12579 }
12580 return false;
12581}
12582
12583
12584
Chris Lattnerf4dff842006-07-11 02:54:03 +000012585/// getConstraintType - Given a constraint letter, return the type of
12586/// constraint it is for this target.
12587X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012588X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12589 if (Constraint.size() == 1) {
12590 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012591 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012592 case 'q':
12593 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012594 case 'f':
12595 case 't':
12596 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012597 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012598 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012599 case 'Y':
12600 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012601 case 'a':
12602 case 'b':
12603 case 'c':
12604 case 'd':
12605 case 'S':
12606 case 'D':
12607 case 'A':
12608 return C_Register;
12609 case 'I':
12610 case 'J':
12611 case 'K':
12612 case 'L':
12613 case 'M':
12614 case 'N':
12615 case 'G':
12616 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012617 case 'e':
12618 case 'Z':
12619 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012620 default:
12621 break;
12622 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012623 }
Chris Lattner4234f572007-03-25 02:14:49 +000012624 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012625}
12626
John Thompson44ab89e2010-10-29 17:29:13 +000012627/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012628/// This object must already have been set up with the operand type
12629/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012630TargetLowering::ConstraintWeight
12631 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012632 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012633 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012634 Value *CallOperandVal = info.CallOperandVal;
12635 // If we don't have a value, we can't do a match,
12636 // but allow it at the lowest weight.
12637 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012638 return CW_Default;
12639 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012640 // Look at the constraint type.
12641 switch (*constraint) {
12642 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012643 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12644 case 'R':
12645 case 'q':
12646 case 'Q':
12647 case 'a':
12648 case 'b':
12649 case 'c':
12650 case 'd':
12651 case 'S':
12652 case 'D':
12653 case 'A':
12654 if (CallOperandVal->getType()->isIntegerTy())
12655 weight = CW_SpecificReg;
12656 break;
12657 case 'f':
12658 case 't':
12659 case 'u':
12660 if (type->isFloatingPointTy())
12661 weight = CW_SpecificReg;
12662 break;
12663 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012664 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012665 weight = CW_SpecificReg;
12666 break;
12667 case 'x':
12668 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012669 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012670 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012671 break;
12672 case 'I':
12673 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12674 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012675 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012676 }
12677 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012678 case 'J':
12679 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12680 if (C->getZExtValue() <= 63)
12681 weight = CW_Constant;
12682 }
12683 break;
12684 case 'K':
12685 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12686 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12687 weight = CW_Constant;
12688 }
12689 break;
12690 case 'L':
12691 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12692 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12693 weight = CW_Constant;
12694 }
12695 break;
12696 case 'M':
12697 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12698 if (C->getZExtValue() <= 3)
12699 weight = CW_Constant;
12700 }
12701 break;
12702 case 'N':
12703 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12704 if (C->getZExtValue() <= 0xff)
12705 weight = CW_Constant;
12706 }
12707 break;
12708 case 'G':
12709 case 'C':
12710 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12711 weight = CW_Constant;
12712 }
12713 break;
12714 case 'e':
12715 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12716 if ((C->getSExtValue() >= -0x80000000LL) &&
12717 (C->getSExtValue() <= 0x7fffffffLL))
12718 weight = CW_Constant;
12719 }
12720 break;
12721 case 'Z':
12722 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12723 if (C->getZExtValue() <= 0xffffffff)
12724 weight = CW_Constant;
12725 }
12726 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012727 }
12728 return weight;
12729}
12730
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012731/// LowerXConstraint - try to replace an X constraint, which matches anything,
12732/// with another that has more specific requirements based on the type of the
12733/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012734const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012735LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012736 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12737 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012738 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012739 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012740 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012741 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012742 return "x";
12743 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012744
Chris Lattner5e764232008-04-26 23:02:14 +000012745 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012746}
12747
Chris Lattner48884cd2007-08-25 00:47:38 +000012748/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12749/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012750void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012751 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012752 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012753 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012754 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012755
Eric Christopher100c8332011-06-02 23:16:42 +000012756 // Only support length 1 constraints for now.
12757 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000012758
Eric Christopher100c8332011-06-02 23:16:42 +000012759 char ConstraintLetter = Constraint[0];
12760 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012761 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012762 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012763 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012764 if (C->getZExtValue() <= 31) {
12765 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012766 break;
12767 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012768 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012769 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012770 case 'J':
12771 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012772 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012773 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12774 break;
12775 }
12776 }
12777 return;
12778 case 'K':
12779 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012780 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012781 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12782 break;
12783 }
12784 }
12785 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012786 case 'N':
12787 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012788 if (C->getZExtValue() <= 255) {
12789 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012790 break;
12791 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012792 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012793 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012794 case 'e': {
12795 // 32-bit signed value
12796 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012797 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12798 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012799 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012800 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012801 break;
12802 }
12803 // FIXME gcc accepts some relocatable values here too, but only in certain
12804 // memory models; it's complicated.
12805 }
12806 return;
12807 }
12808 case 'Z': {
12809 // 32-bit unsigned value
12810 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012811 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12812 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012813 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12814 break;
12815 }
12816 }
12817 // FIXME gcc accepts some relocatable values here too, but only in certain
12818 // memory models; it's complicated.
12819 return;
12820 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012821 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012822 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012823 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012824 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012825 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012826 break;
12827 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012828
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012829 // In any sort of PIC mode addresses need to be computed at runtime by
12830 // adding in a register or some sort of table lookup. These can't
12831 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012832 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012833 return;
12834
Chris Lattnerdc43a882007-05-03 16:52:29 +000012835 // If we are in non-pic codegen mode, we allow the address of a global (with
12836 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012837 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012838 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012839
Chris Lattner49921962009-05-08 18:23:14 +000012840 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12841 while (1) {
12842 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12843 Offset += GA->getOffset();
12844 break;
12845 } else if (Op.getOpcode() == ISD::ADD) {
12846 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12847 Offset += C->getZExtValue();
12848 Op = Op.getOperand(0);
12849 continue;
12850 }
12851 } else if (Op.getOpcode() == ISD::SUB) {
12852 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12853 Offset += -C->getZExtValue();
12854 Op = Op.getOperand(0);
12855 continue;
12856 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012857 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012858
Chris Lattner49921962009-05-08 18:23:14 +000012859 // Otherwise, this isn't something we can handle, reject it.
12860 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012861 }
Eric Christopherfd179292009-08-27 18:07:15 +000012862
Dan Gohman46510a72010-04-15 01:51:59 +000012863 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012864 // If we require an extra load to get this address, as in PIC mode, we
12865 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012866 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12867 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012868 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012869
Devang Patel0d881da2010-07-06 22:08:15 +000012870 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12871 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012872 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012873 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012874 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012875
Gabor Greifba36cb52008-08-28 21:40:38 +000012876 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012877 Ops.push_back(Result);
12878 return;
12879 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012880 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012881}
12882
Chris Lattner259e97c2006-01-31 19:43:35 +000012883std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012884getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012885 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012886 if (Constraint.size() == 1) {
12887 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012888 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012889 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012890 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12891 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012892 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012893 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12894 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12895 X86::R10D,X86::R11D,X86::R12D,
12896 X86::R13D,X86::R14D,X86::R15D,
12897 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012898 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012899 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12900 X86::SI, X86::DI, X86::R8W,X86::R9W,
12901 X86::R10W,X86::R11W,X86::R12W,
12902 X86::R13W,X86::R14W,X86::R15W,
12903 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012904 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012905 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12906 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12907 X86::R10B,X86::R11B,X86::R12B,
12908 X86::R13B,X86::R14B,X86::R15B,
12909 X86::BPL, X86::SPL, 0);
12910
Owen Anderson825b72b2009-08-11 20:47:22 +000012911 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012912 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12913 X86::RSI, X86::RDI, X86::R8, X86::R9,
12914 X86::R10, X86::R11, X86::R12,
12915 X86::R13, X86::R14, X86::R15,
12916 X86::RBP, X86::RSP, 0);
12917
12918 break;
12919 }
Eric Christopherfd179292009-08-27 18:07:15 +000012920 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012921 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012922 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012923 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012924 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012925 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012926 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012927 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012928 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012929 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12930 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012931 }
12932 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012933
Chris Lattner1efa40f2006-02-22 00:56:39 +000012934 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012935}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012936
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012937std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012938X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012939 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012940 // First, see if this is a constraint that directly corresponds to an LLVM
12941 // register class.
12942 if (Constraint.size() == 1) {
12943 // GCC Constraint Letters
12944 switch (Constraint[0]) {
12945 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012946 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012947 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012948 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012949 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012950 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012951 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012952 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012953 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012954 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012955 case 'R': // LEGACY_REGS
12956 if (VT == MVT::i8)
12957 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12958 if (VT == MVT::i16)
12959 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12960 if (VT == MVT::i32 || !Subtarget->is64Bit())
12961 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12962 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012963 case 'f': // FP Stack registers.
12964 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12965 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012966 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012967 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012968 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012969 return std::make_pair(0U, X86::RFP64RegisterClass);
12970 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012971 case 'y': // MMX_REGS if MMX allowed.
12972 if (!Subtarget->hasMMX()) break;
12973 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012974 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012975 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012976 // FALL THROUGH.
12977 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012978 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012979
Owen Anderson825b72b2009-08-11 20:47:22 +000012980 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012981 default: break;
12982 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012983 case MVT::f32:
12984 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012985 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012986 case MVT::f64:
12987 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012988 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012989 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012990 case MVT::v16i8:
12991 case MVT::v8i16:
12992 case MVT::v4i32:
12993 case MVT::v2i64:
12994 case MVT::v4f32:
12995 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012996 return std::make_pair(0U, X86::VR128RegisterClass);
12997 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012998 break;
12999 }
13000 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013001
Chris Lattnerf76d1802006-07-31 23:26:50 +000013002 // Use the default implementation in TargetLowering to convert the register
13003 // constraint into a member of a register class.
13004 std::pair<unsigned, const TargetRegisterClass*> Res;
13005 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000013006
13007 // Not found as a standard register?
13008 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013009 // Map st(0) -> st(7) -> ST0
13010 if (Constraint.size() == 7 && Constraint[0] == '{' &&
13011 tolower(Constraint[1]) == 's' &&
13012 tolower(Constraint[2]) == 't' &&
13013 Constraint[3] == '(' &&
13014 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
13015 Constraint[5] == ')' &&
13016 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000013017
Chris Lattner56d77c72009-09-13 22:41:48 +000013018 Res.first = X86::ST0+Constraint[4]-'0';
13019 Res.second = X86::RFP80RegisterClass;
13020 return Res;
13021 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013022
Chris Lattner56d77c72009-09-13 22:41:48 +000013023 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013024 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000013025 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000013026 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013027 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000013028 }
Chris Lattner56d77c72009-09-13 22:41:48 +000013029
13030 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000013031 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000013032 Res.first = X86::EFLAGS;
13033 Res.second = X86::CCRRegisterClass;
13034 return Res;
13035 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000013036
Dale Johannesen330169f2008-11-13 21:52:36 +000013037 // 'A' means EAX + EDX.
13038 if (Constraint == "A") {
13039 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000013040 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000013041 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000013042 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000013043 return Res;
13044 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013045
Chris Lattnerf76d1802006-07-31 23:26:50 +000013046 // Otherwise, check to see if this is a register class of the wrong value
13047 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
13048 // turn into {ax},{dx}.
13049 if (Res.second->hasType(VT))
13050 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013051
Chris Lattnerf76d1802006-07-31 23:26:50 +000013052 // All of the single-register GCC register classes map their values onto
13053 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13054 // really want an 8-bit or 32-bit register, map to the appropriate register
13055 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013056 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013057 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013058 unsigned DestReg = 0;
13059 switch (Res.first) {
13060 default: break;
13061 case X86::AX: DestReg = X86::AL; break;
13062 case X86::DX: DestReg = X86::DL; break;
13063 case X86::CX: DestReg = X86::CL; break;
13064 case X86::BX: DestReg = X86::BL; break;
13065 }
13066 if (DestReg) {
13067 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013068 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013069 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013070 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013071 unsigned DestReg = 0;
13072 switch (Res.first) {
13073 default: break;
13074 case X86::AX: DestReg = X86::EAX; break;
13075 case X86::DX: DestReg = X86::EDX; break;
13076 case X86::CX: DestReg = X86::ECX; break;
13077 case X86::BX: DestReg = X86::EBX; break;
13078 case X86::SI: DestReg = X86::ESI; break;
13079 case X86::DI: DestReg = X86::EDI; break;
13080 case X86::BP: DestReg = X86::EBP; break;
13081 case X86::SP: DestReg = X86::ESP; break;
13082 }
13083 if (DestReg) {
13084 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013085 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013086 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013087 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013088 unsigned DestReg = 0;
13089 switch (Res.first) {
13090 default: break;
13091 case X86::AX: DestReg = X86::RAX; break;
13092 case X86::DX: DestReg = X86::RDX; break;
13093 case X86::CX: DestReg = X86::RCX; break;
13094 case X86::BX: DestReg = X86::RBX; break;
13095 case X86::SI: DestReg = X86::RSI; break;
13096 case X86::DI: DestReg = X86::RDI; break;
13097 case X86::BP: DestReg = X86::RBP; break;
13098 case X86::SP: DestReg = X86::RSP; break;
13099 }
13100 if (DestReg) {
13101 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013102 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013103 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013104 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013105 } else if (Res.second == X86::FR32RegisterClass ||
13106 Res.second == X86::FR64RegisterClass ||
13107 Res.second == X86::VR128RegisterClass) {
13108 // Handle references to XMM physical registers that got mapped into the
13109 // wrong class. This can happen with constraints like {xmm0} where the
13110 // target independent register mapper will just pick the first match it can
13111 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013112 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013113 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013114 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013115 Res.second = X86::FR64RegisterClass;
13116 else if (X86::VR128RegisterClass->hasType(VT))
13117 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013118 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013119
Chris Lattnerf76d1802006-07-31 23:26:50 +000013120 return Res;
13121}