blob: a8bb4ef27827be8f2b4aa0f445bc2f9c50054279 [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"
Mon P Wang3c81d352008-11-23 04:37:22 +000048#include "llvm/Support/CommandLine.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
David Greenef125a292011-02-08 19:04:41 +000059static cl::opt<bool>
60Disable256Bit("disable-256bit", cl::Hidden,
61 cl::desc("Disable use of 256-bit vectors"));
62
Evan Cheng10e86422008-04-25 19:11:04 +000063// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000064static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000065 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000066
David Greenea5f26012011-02-07 19:36:54 +000067static SDValue Insert128BitVector(SDValue Result,
68 SDValue Vec,
69 SDValue Idx,
70 SelectionDAG &DAG,
71 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000072
David Greenea5f26012011-02-07 19:36:54 +000073static SDValue Extract128BitVector(SDValue Vec,
74 SDValue Idx,
75 SelectionDAG &DAG,
76 DebugLoc dl);
77
David Greenef125a292011-02-08 19:04:41 +000078static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
79
80
David Greenea5f26012011-02-07 19:36:54 +000081/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
82/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000083/// simple subregister reference. Idx is an index in the 128 bits we
84/// want. It need not be aligned to a 128-bit bounday. That makes
85/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000086static SDValue Extract128BitVector(SDValue Vec,
87 SDValue Idx,
88 SelectionDAG &DAG,
89 DebugLoc dl) {
90 EVT VT = Vec.getValueType();
91 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
92
93 EVT ElVT = VT.getVectorElementType();
94
95 int Factor = VT.getSizeInBits() / 128;
96
97 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
98 ElVT,
99 VT.getVectorNumElements() / Factor);
100
101 // Extract from UNDEF is UNDEF.
102 if (Vec.getOpcode() == ISD::UNDEF)
103 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
104
105 if (isa<ConstantSDNode>(Idx)) {
106 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
107
108 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
109 // we can match to VEXTRACTF128.
110 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
111
112 // This is the index of the first element of the 128-bit chunk
113 // we want.
114 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
115 * ElemsPerChunk);
116
117 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
118
119 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
120 VecIdx);
121
122 return Result;
123 }
124
125 return SDValue();
126}
127
128/// Generate a DAG to put 128-bits into a vector > 128 bits. This
129/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000130/// simple superregister reference. Idx is an index in the 128 bits
131/// we want. It need not be aligned to a 128-bit bounday. That makes
132/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000133static SDValue Insert128BitVector(SDValue Result,
134 SDValue Vec,
135 SDValue Idx,
136 SelectionDAG &DAG,
137 DebugLoc dl) {
138 if (isa<ConstantSDNode>(Idx)) {
139 EVT VT = Vec.getValueType();
140 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
141
142 EVT ElVT = VT.getVectorElementType();
143
144 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
145
146 EVT ResultVT = Result.getValueType();
147
148 // Insert the relevant 128 bits.
149 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
150
151 // This is the index of the first element of the 128-bit chunk
152 // we want.
153 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
154 * ElemsPerChunk);
155
156 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
157
158 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
159 VecIdx);
160 return Result;
161 }
162
163 return SDValue();
164}
165
David Greenef125a292011-02-08 19:04:41 +0000166/// Given two vectors, concat them.
167static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
168 DebugLoc dl = Lower.getDebugLoc();
169
170 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
171
172 EVT VT = EVT::getVectorVT(*DAG.getContext(),
173 Lower.getValueType().getVectorElementType(),
174 Lower.getValueType().getVectorNumElements() * 2);
175
176 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
177 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
178
179 // Insert the upper subvector.
180 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
181 DAG.getConstant(
182 // This is half the length of the result
183 // vector. Start inserting the upper 128
184 // bits here.
185 Lower.getValueType().getVectorNumElements(),
186 MVT::i32),
187 DAG, dl);
188
189 // Insert the lower subvector.
190 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
191 return Vec;
192}
193
Chris Lattnerf0144122009-07-28 03:13:23 +0000194static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000195 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
196 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000197
Evan Cheng2bffee22011-02-01 01:14:13 +0000198 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000199 if (is64Bit)
200 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000201 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000202 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000203
Evan Cheng2bffee22011-02-01 01:14:13 +0000204 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000205 if (is64Bit)
206 return new X8664_ELFTargetObjectFile(TM);
207 return new X8632_ELFTargetObjectFile(TM);
208 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000209 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000210 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000211 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000212}
213
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000214X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000215 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000216 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000217 X86ScalarSSEf64 = Subtarget->hasXMMInt();
218 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000220
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000221 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000222 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000223
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000224 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000225 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226
227 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000229 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +0000230 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000232
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000233 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000234 // Setup Windows compiler runtime calls.
235 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000236 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
237 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000238 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000239 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000240 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000241 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
242 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000243 }
244
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000245 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000246 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000247 setUseUnderscoreSetJmp(false);
248 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000249 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000250 // MS runtime is weird: it exports _setjmp, but longjmp!
251 setUseUnderscoreSetJmp(true);
252 setUseUnderscoreLongJmp(false);
253 } else {
254 setUseUnderscoreSetJmp(true);
255 setUseUnderscoreLongJmp(true);
256 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000257
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000258 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000260 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000262 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000266
Scott Michelfdc40a02009-02-17 22:15:04 +0000267 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000269 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000271 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
273 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000274
275 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
277 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
278 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
279 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
280 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
281 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000282
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000283 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
284 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
286 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000288
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
291 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000292 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000293 // We have an algorithm for SSE2->double, and we turn this into a
294 // 64-bit FILD followed by conditional FADD for other targets.
295 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000296 // We have an algorithm for SSE2, and we turn this into a 64-bit
297 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000298 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000299 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000300
301 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
302 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
304 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000305
Devang Patel6a784892009-06-05 18:48:29 +0000306 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000307 // SSE has no i16 to fp conversion, only i32
308 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000310 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000312 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
314 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000315 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000316 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
318 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000319 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000320
Dale Johannesen73328d12007-09-19 23:55:34 +0000321 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
322 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
324 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000325
Evan Cheng02568ff2006-01-30 22:13:22 +0000326 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
327 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
329 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000330
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000331 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000333 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000335 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
337 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000338 }
339
340 // Handle FP_TO_UINT by promoting the destination to a larger signed
341 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
343 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000345
Evan Cheng25ab6902006-09-08 06:48:29 +0000346 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
348 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000349 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000350 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000351 // Expand FP_TO_UINT into a select.
352 // FIXME: We would like to use a Custom expander here eventually to do
353 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000355 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000356 // With SSE3 we can use fisttpll to convert to a signed i64; without
357 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000359 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000360
Chris Lattner399610a2006-12-05 18:22:22 +0000361 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000362 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000363 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
364 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000365 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000367 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000368 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000369 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000370 }
Chris Lattner21f66852005-12-23 05:15:23 +0000371
Dan Gohmanb00ee212008-02-18 19:34:53 +0000372 // Scalar integer divide and remainder are lowered to use operations that
373 // produce two results, to match the available instructions. This exposes
374 // the two-result form to trivial CSE, which is able to combine x/y and x%y
375 // into a single instruction.
376 //
377 // Scalar integer multiply-high is also lowered to use two-result
378 // operations, to match the available instructions. However, plain multiply
379 // (low) operations are left as Legal, as there are single-result
380 // instructions for this in x86. Using the two-result multiply instructions
381 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000382 for (unsigned i = 0, e = 4; i != e; ++i) {
383 MVT VT = IntVTs[i];
384 setOperationAction(ISD::MULHS, VT, Expand);
385 setOperationAction(ISD::MULHU, VT, Expand);
386 setOperationAction(ISD::SDIV, VT, Expand);
387 setOperationAction(ISD::UDIV, VT, Expand);
388 setOperationAction(ISD::SREM, VT, Expand);
389 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000390
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000391 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000392 setOperationAction(ISD::ADDC, VT, Custom);
393 setOperationAction(ISD::ADDE, VT, Custom);
394 setOperationAction(ISD::SUBC, VT, Custom);
395 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000396 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
399 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
400 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
401 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000402 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
407 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
408 setOperationAction(ISD::FREM , MVT::f32 , Expand);
409 setOperationAction(ISD::FREM , MVT::f64 , Expand);
410 setOperationAction(ISD::FREM , MVT::f80 , Expand);
411 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000412
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
414 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000415 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
418 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000419 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
421 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000422 }
423
Benjamin Kramer1292c222010-12-04 20:32:23 +0000424 if (Subtarget->hasPOPCNT()) {
425 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
426 } else {
427 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
428 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
429 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
430 if (Subtarget->is64Bit())
431 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
432 }
433
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
435 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000436
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000438 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000439 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000440 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000441 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
443 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
446 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000447 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
449 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000452 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
454 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000455 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000457
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000458 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
460 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
461 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
462 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000463 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
465 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000466 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000467 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
469 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
470 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
471 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000472 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000473 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000474 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
476 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
477 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000478 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
480 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
481 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000482 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000483
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000484 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000486
Eric Christopher9a9d2752010-07-22 02:48:34 +0000487 // We may not have a libcall for MEMBARRIER so we should lower this.
488 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000489
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000490 // On X86 and X86-64, atomic operations are lowered to locked instructions.
491 // Locked instructions, in turn, have implicit fence semantics (all memory
492 // operations are flushed before issuing the locked instruction, and they
493 // are not buffered), so we can fold away the common pattern of
494 // fence-atomic-fence.
495 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000496
Mon P Wang63307c32008-05-05 19:05:59 +0000497 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000498 for (unsigned i = 0, e = 4; i != e; ++i) {
499 MVT VT = IntVTs[i];
500 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
501 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
502 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000503
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000504 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000512 }
513
Evan Cheng3c992d22006-03-07 02:02:57 +0000514 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000515 if (!Subtarget->isTargetDarwin() &&
516 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000517 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000519 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
522 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
523 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
524 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000525 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000526 setExceptionPointerRegister(X86::RAX);
527 setExceptionSelectorRegister(X86::RDX);
528 } else {
529 setExceptionPointerRegister(X86::EAX);
530 setExceptionSelectorRegister(X86::EDX);
531 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
533 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000536
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000538
Nate Begemanacc398c2006-01-25 18:21:52 +0000539 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::VASTART , MVT::Other, Custom);
541 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000542 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::VAARG , MVT::Other, Custom);
544 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000545 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::VAARG , MVT::Other, Expand);
547 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000548 }
Evan Chengae642192007-03-02 23:16:35 +0000549
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
551 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000552 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000554 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000556 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, 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
Evan Chengd25e9e82006-02-02 00:28:23 +0000577 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FSIN , MVT::f64, Expand);
579 setOperationAction(ISD::FCOS , MVT::f64, Expand);
580 setOperationAction(ISD::FSIN , MVT::f32, Expand);
581 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582
Chris Lattnera54aa942006-01-29 06:26:08 +0000583 // Expand FP immediates into loads from the stack, except for the special
584 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585 addLegalFPImmediate(APFloat(+0.0)); // xorpd
586 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000587 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588 // Use SSE for f32, x87 for f64.
589 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
591 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595
596 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
601 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
603 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FSIN , MVT::f32, Expand);
607 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
Nate Begemane1795842008-02-14 08:57:00 +0000609 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 addLegalFPImmediate(APFloat(+0.0f)); // xorps
611 addLegalFPImmediate(APFloat(+0.0)); // FLD0
612 addLegalFPImmediate(APFloat(+1.0)); // FLD1
613 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
614 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
615
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
618 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000620 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000621 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000622 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
624 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
627 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
628 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
629 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000630
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000631 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
633 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000634 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000635 addLegalFPImmediate(APFloat(+0.0)); // FLD0
636 addLegalFPImmediate(APFloat(+1.0)); // FLD1
637 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
638 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000643 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000644
Dale Johannesen59a58732007-08-05 18:49:15 +0000645 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000646 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
648 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000650 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000651 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 addLegalFPImmediate(TmpFlt); // FLD0
653 TmpFlt.changeSign();
654 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000655
656 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000657 APFloat TmpFlt2(+1.0);
658 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
659 &ignored);
660 addLegalFPImmediate(TmpFlt2); // FLD1
661 TmpFlt2.changeSign();
662 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
663 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
667 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000669 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000670
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000671 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
674 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FLOG, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
678 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP, MVT::f80, Expand);
680 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000681
Mon P Wangf007a8b2008-11-06 05:31:54 +0000682 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000683 // (for widening) or expand (for scalarization). Then we will selectively
684 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
686 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
687 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000703 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
704 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000736 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000737 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
803 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
834 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Nate Begeman14d12ca2008-02-11 04:19:36 +0000912 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000927 // Can turn SHL into an integer multiply.
928 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000929 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000930
Nate Begeman14d12ca2008-02-11 04:19:36 +0000931 // i8 and i16 vectors are custom , because the source register and source
932 // source memory operand types are not the same width. f32 vectors are
933 // custom since the immediate controlling the insert encodes additional
934 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944
945 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 }
949 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000950
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000951 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000953
David Greene9b9838d2009-06-29 16:47:10 +0000954 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
956 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
957 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
958 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000959 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000960
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
962 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
963 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
964 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000965
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
967 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
968 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
969 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
970 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
971 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
974 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
975 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
976 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
977 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
978 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000979
David Greene54d8eba2011-01-27 22:38:56 +0000980 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
981 // insert_vector_elt extract_subvector and extract_vector_elt for
982 // 256-bit types.
983 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
984 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
985 ++i) {
986 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
987 // Do not attempt to custom lower non-256-bit vectors
988 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
989 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000990 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000991 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
992 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000993 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000994 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000995 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000996 }
David Greene54d8eba2011-01-27 22:38:56 +0000997 // Custom-lower insert_subvector and extract_subvector based on
998 // the result type.
999 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1000 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1001 ++i) {
1002 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1003 // Do not attempt to custom lower non-256-bit vectors
1004 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001005 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001006
1007 if (MVT(VT).getSizeInBits() == 128) {
1008 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001009 }
David Greene54d8eba2011-01-27 22:38:56 +00001010 else if (MVT(VT).getSizeInBits() == 256) {
1011 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1012 }
David Greene9b9838d2009-06-29 16:47:10 +00001013 }
1014
David Greene54d8eba2011-01-27 22:38:56 +00001015 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1016 // Don't promote loads because we need them for VPERM vector index versions.
1017
1018 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1019 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1020 VT++) {
1021 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1022 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1023 continue;
1024 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1025 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1026 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1027 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1028 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1029 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1030 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1031 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1032 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1033 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1034 }
David Greene9b9838d2009-06-29 16:47:10 +00001035 }
1036
Evan Cheng6be2c582006-04-05 23:38:46 +00001037 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001039
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001040
Eli Friedman962f5492010-06-02 19:35:46 +00001041 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1042 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001043 //
Eli Friedman962f5492010-06-02 19:35:46 +00001044 // FIXME: We really should do custom legalization for addition and
1045 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1046 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001047 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1048 // Add/Sub/Mul with overflow operations are custom lowered.
1049 MVT VT = IntVTs[i];
1050 setOperationAction(ISD::SADDO, VT, Custom);
1051 setOperationAction(ISD::UADDO, VT, Custom);
1052 setOperationAction(ISD::SSUBO, VT, Custom);
1053 setOperationAction(ISD::USUBO, VT, Custom);
1054 setOperationAction(ISD::SMULO, VT, Custom);
1055 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001056 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001057
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001058 // There are no 8-bit 3-address imul/mul instructions
1059 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1060 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001061
Evan Chengd54f2d52009-03-31 19:38:51 +00001062 if (!Subtarget->is64Bit()) {
1063 // These libcalls are not available in 32-bit.
1064 setLibcallName(RTLIB::SHL_I128, 0);
1065 setLibcallName(RTLIB::SRL_I128, 0);
1066 setLibcallName(RTLIB::SRA_I128, 0);
1067 }
1068
Evan Cheng206ee9d2006-07-07 08:33:52 +00001069 // We have target-specific dag combine patterns for the following nodes:
1070 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001071 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001072 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001073 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001074 setTargetDAGCombine(ISD::SHL);
1075 setTargetDAGCombine(ISD::SRA);
1076 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001077 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001078 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001079 setTargetDAGCombine(ISD::ADD);
1080 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001081 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001082 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001083 if (Subtarget->is64Bit())
1084 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001085
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001086 computeRegisterProperties();
1087
Evan Cheng05219282011-01-06 06:52:41 +00001088 // On Darwin, -Os means optimize for size without hurting performance,
1089 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001090 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001091 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001092 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001093 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1094 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1095 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001096 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001097 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001098}
1099
Scott Michel5b8f82e2008-03-10 15:42:14 +00001100
Owen Anderson825b72b2009-08-11 20:47:22 +00001101MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1102 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001103}
1104
1105
Evan Cheng29286502008-01-23 23:17:41 +00001106/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1107/// the desired ByVal argument alignment.
1108static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1109 if (MaxAlign == 16)
1110 return;
1111 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1112 if (VTy->getBitWidth() == 128)
1113 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001114 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1115 unsigned EltAlign = 0;
1116 getMaxByValAlign(ATy->getElementType(), EltAlign);
1117 if (EltAlign > MaxAlign)
1118 MaxAlign = EltAlign;
1119 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1120 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1121 unsigned EltAlign = 0;
1122 getMaxByValAlign(STy->getElementType(i), EltAlign);
1123 if (EltAlign > MaxAlign)
1124 MaxAlign = EltAlign;
1125 if (MaxAlign == 16)
1126 break;
1127 }
1128 }
1129 return;
1130}
1131
1132/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1133/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001134/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1135/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001136unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001137 if (Subtarget->is64Bit()) {
1138 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001139 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001140 if (TyAlign > 8)
1141 return TyAlign;
1142 return 8;
1143 }
1144
Evan Cheng29286502008-01-23 23:17:41 +00001145 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001146 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001147 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001148 return Align;
1149}
Chris Lattner2b02a442007-02-25 08:29:00 +00001150
Evan Chengf0df0312008-05-15 08:39:06 +00001151/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001152/// and store operations as a result of memset, memcpy, and memmove
1153/// lowering. If DstAlign is zero that means it's safe to destination
1154/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1155/// means there isn't a need to check it against alignment requirement,
1156/// probably because the source does not need to be loaded. If
1157/// 'NonScalarIntSafe' is true, that means it's safe to return a
1158/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1159/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1160/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001161/// It returns EVT::Other if the type should be determined using generic
1162/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001163EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001164X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1165 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001166 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001167 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001168 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001169 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1170 // linux. This is because the stack realignment code can't handle certain
1171 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001172 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001173 if (NonScalarIntSafe &&
1174 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001175 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001176 (Subtarget->isUnalignedMemAccessFast() ||
1177 ((DstAlign == 0 || DstAlign >= 16) &&
1178 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001179 Subtarget->getStackAlignment() >= 16) {
1180 if (Subtarget->hasSSE2())
1181 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001182 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001183 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001184 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001185 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001186 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001187 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001188 // Do not use f64 to lower memcpy if source is string constant. It's
1189 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001190 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001191 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001192 }
Evan Chengf0df0312008-05-15 08:39:06 +00001193 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 return MVT::i64;
1195 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001196}
1197
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001198/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1199/// current function. The returned value is a member of the
1200/// MachineJumpTableInfo::JTEntryKind enum.
1201unsigned X86TargetLowering::getJumpTableEncoding() const {
1202 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1203 // symbol.
1204 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1205 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001206 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001207
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001208 // Otherwise, use the normal jump table encoding heuristics.
1209 return TargetLowering::getJumpTableEncoding();
1210}
1211
Chris Lattnerc64daab2010-01-26 05:02:42 +00001212const MCExpr *
1213X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1214 const MachineBasicBlock *MBB,
1215 unsigned uid,MCContext &Ctx) const{
1216 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1217 Subtarget->isPICStyleGOT());
1218 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1219 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001220 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1221 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001222}
1223
Evan Chengcc415862007-11-09 01:32:10 +00001224/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1225/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001226SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001227 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001228 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001229 // This doesn't have DebugLoc associated with it, but is not really the
1230 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001231 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001232 return Table;
1233}
1234
Chris Lattner589c6f62010-01-26 06:28:43 +00001235/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1236/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1237/// MCExpr.
1238const MCExpr *X86TargetLowering::
1239getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1240 MCContext &Ctx) const {
1241 // X86-64 uses RIP relative addressing based on the jump table label.
1242 if (Subtarget->isPICStyleRIPRel())
1243 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1244
1245 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001246 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001247}
1248
Bill Wendlingb4202b82009-07-01 18:50:55 +00001249/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001250unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001251 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001252}
1253
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001254// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001255std::pair<const TargetRegisterClass*, uint8_t>
1256X86TargetLowering::findRepresentativeClass(EVT VT) const{
1257 const TargetRegisterClass *RRC = 0;
1258 uint8_t Cost = 1;
1259 switch (VT.getSimpleVT().SimpleTy) {
1260 default:
1261 return TargetLowering::findRepresentativeClass(VT);
1262 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1263 RRC = (Subtarget->is64Bit()
1264 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1265 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001266 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001267 RRC = X86::VR64RegisterClass;
1268 break;
1269 case MVT::f32: case MVT::f64:
1270 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1271 case MVT::v4f32: case MVT::v2f64:
1272 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1273 case MVT::v4f64:
1274 RRC = X86::VR128RegisterClass;
1275 break;
1276 }
1277 return std::make_pair(RRC, Cost);
1278}
1279
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001280// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001281unsigned
1282X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1283 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001284 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001285
1286 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001287 switch (RC->getID()) {
1288 default:
1289 return 0;
1290 case X86::GR32RegClassID:
1291 return 4 - FPDiff;
1292 case X86::GR64RegClassID:
1293 return 8 - FPDiff;
1294 case X86::VR128RegClassID:
1295 return Subtarget->is64Bit() ? 10 : 4;
1296 case X86::VR64RegClassID:
1297 return 4;
1298 }
1299}
1300
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001301bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1302 unsigned &Offset) const {
1303 if (!Subtarget->isTargetLinux())
1304 return false;
1305
1306 if (Subtarget->is64Bit()) {
1307 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1308 Offset = 0x28;
1309 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1310 AddressSpace = 256;
1311 else
1312 AddressSpace = 257;
1313 } else {
1314 // %gs:0x14 on i386
1315 Offset = 0x14;
1316 AddressSpace = 256;
1317 }
1318 return true;
1319}
1320
1321
Chris Lattner2b02a442007-02-25 08:29:00 +00001322//===----------------------------------------------------------------------===//
1323// Return Value Calling Convention Implementation
1324//===----------------------------------------------------------------------===//
1325
Chris Lattner59ed56b2007-02-28 04:55:35 +00001326#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001327
Michael J. Spencerec38de22010-10-10 22:04:20 +00001328bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001329X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001330 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001331 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001332 SmallVector<CCValAssign, 16> RVLocs;
1333 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001334 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001335 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001336}
1337
Dan Gohman98ca4f22009-08-05 01:29:28 +00001338SDValue
1339X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001340 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001341 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001342 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001343 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001344 MachineFunction &MF = DAG.getMachineFunction();
1345 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001346
Chris Lattner9774c912007-02-27 05:28:59 +00001347 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001348 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1349 RVLocs, *DAG.getContext());
1350 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001351
Evan Chengdcea1632010-02-04 02:40:39 +00001352 // Add the regs to the liveout set for the function.
1353 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1354 for (unsigned i = 0; i != RVLocs.size(); ++i)
1355 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1356 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001357
Dan Gohman475871a2008-07-27 21:46:04 +00001358 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001359
Dan Gohman475871a2008-07-27 21:46:04 +00001360 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001361 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1362 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001363 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1364 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001365
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001366 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001367 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1368 CCValAssign &VA = RVLocs[i];
1369 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001370 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001371 EVT ValVT = ValToCopy.getValueType();
1372
Dale Johannesenc4510512010-09-24 19:05:48 +00001373 // If this is x86-64, and we disabled SSE, we can't return FP values,
1374 // or SSE or MMX vectors.
1375 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1376 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001377 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001378 report_fatal_error("SSE register return with SSE disabled");
1379 }
1380 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1381 // llvm-gcc has never done it right and no one has noticed, so this
1382 // should be OK for now.
1383 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001384 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001385 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001386
Chris Lattner447ff682008-03-11 03:23:40 +00001387 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1388 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001389 if (VA.getLocReg() == X86::ST0 ||
1390 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001391 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1392 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001393 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001394 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001395 RetOps.push_back(ValToCopy);
1396 // Don't emit a copytoreg.
1397 continue;
1398 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001399
Evan Cheng242b38b2009-02-23 09:03:22 +00001400 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1401 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001402 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001403 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001404 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001405 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001406 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1407 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001408 // If we don't have SSE2 available, convert to v4f32 so the generated
1409 // register is legal.
1410 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001411 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001412 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001413 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001414 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001415
Dale Johannesendd64c412009-02-04 00:33:20 +00001416 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001417 Flag = Chain.getValue(1);
1418 }
Dan Gohman61a92132008-04-21 23:59:07 +00001419
1420 // The x86-64 ABI for returning structs by value requires that we copy
1421 // the sret argument into %rax for the return. We saved the argument into
1422 // a virtual register in the entry block, so now we copy the value out
1423 // and into %rax.
1424 if (Subtarget->is64Bit() &&
1425 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1426 MachineFunction &MF = DAG.getMachineFunction();
1427 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1428 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001429 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001430 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001431 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001432
Dale Johannesendd64c412009-02-04 00:33:20 +00001433 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001434 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001435
1436 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001437 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001438 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001439
Chris Lattner447ff682008-03-11 03:23:40 +00001440 RetOps[0] = Chain; // Update chain.
1441
1442 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001443 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001444 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001445
1446 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001447 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001448}
1449
Evan Cheng3d2125c2010-11-30 23:55:39 +00001450bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1451 if (N->getNumValues() != 1)
1452 return false;
1453 if (!N->hasNUsesOfValue(1, 0))
1454 return false;
1455
1456 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001457 if (Copy->getOpcode() != ISD::CopyToReg &&
1458 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001459 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001460
1461 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001462 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001463 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001464 if (UI->getOpcode() != X86ISD::RET_FLAG)
1465 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001466 HasRet = true;
1467 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001468
Evan Cheng1bf891a2010-12-01 22:59:46 +00001469 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001470}
1471
Dan Gohman98ca4f22009-08-05 01:29:28 +00001472/// LowerCallResult - Lower the result values of a call into the
1473/// appropriate copies out of appropriate physical registers.
1474///
1475SDValue
1476X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001477 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001478 const SmallVectorImpl<ISD::InputArg> &Ins,
1479 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001480 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001481
Chris Lattnere32bbf62007-02-28 07:09:55 +00001482 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001483 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001484 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001485 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001486 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001488
Chris Lattner3085e152007-02-25 08:59:22 +00001489 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001490 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001491 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001492 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001493
Torok Edwin3f142c32009-02-01 18:15:56 +00001494 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001495 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001496 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001497 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001498 }
1499
Evan Cheng79fb3b42009-02-20 20:43:02 +00001500 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001501
1502 // If this is a call to a function that returns an fp value on the floating
1503 // point stack, we must guarantee the the value is popped from the stack, so
1504 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1505 // if the return value is not used. We use the FpGET_ST0 instructions
1506 // instead.
1507 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1508 // If we prefer to use the value in xmm registers, copy it out as f80 and
1509 // use a truncate to move it from fp stack reg to xmm reg.
1510 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1511 bool isST0 = VA.getLocReg() == X86::ST0;
1512 unsigned Opc = 0;
1513 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1514 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1515 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1516 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001517 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001518 Ops, 2), 1);
1519 Val = Chain.getValue(0);
1520
1521 // Round the f80 to the right size, which also moves it to the appropriate
1522 // xmm register.
1523 if (CopyVT != VA.getValVT())
1524 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1525 // This truncation won't change the value.
1526 DAG.getIntPtrConstant(1));
1527 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001528 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1529 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1530 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001532 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1534 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001535 } else {
1536 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001537 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001538 Val = Chain.getValue(0);
1539 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001540 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001541 } else {
1542 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1543 CopyVT, InFlag).getValue(1);
1544 Val = Chain.getValue(0);
1545 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001546 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001547 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001548 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001549
Dan Gohman98ca4f22009-08-05 01:29:28 +00001550 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001551}
1552
1553
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001554//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001555// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001556//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001557// StdCall calling convention seems to be standard for many Windows' API
1558// routines and around. It differs from C calling convention just a little:
1559// callee should clean up the stack, not caller. Symbols should be also
1560// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001561// For info on fast calling convention see Fast Calling Convention (tail call)
1562// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001563
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001565/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1567 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001568 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001569
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001571}
1572
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001573/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001574/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575static bool
1576ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1577 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001578 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001579
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001581}
1582
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001583/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1584/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001585/// the specific parameter attribute. The copy will be passed as a byval
1586/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001587static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001588CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001589 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1590 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001591 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001592
Dale Johannesendd64c412009-02-04 00:33:20 +00001593 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001594 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001595 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001596}
1597
Chris Lattner29689432010-03-11 00:22:57 +00001598/// IsTailCallConvention - Return true if the calling convention is one that
1599/// supports tail call optimization.
1600static bool IsTailCallConvention(CallingConv::ID CC) {
1601 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1602}
1603
Evan Cheng0c439eb2010-01-27 00:07:07 +00001604/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1605/// a tailcall target by changing its ABI.
1606static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001607 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001608}
1609
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610SDValue
1611X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001612 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613 const SmallVectorImpl<ISD::InputArg> &Ins,
1614 DebugLoc dl, SelectionDAG &DAG,
1615 const CCValAssign &VA,
1616 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001617 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001618 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001619 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001620 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001621 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001622 EVT ValVT;
1623
1624 // If value is passed by pointer we have address passed instead of the value
1625 // itself.
1626 if (VA.getLocInfo() == CCValAssign::Indirect)
1627 ValVT = VA.getLocVT();
1628 else
1629 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001630
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001631 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001632 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001633 // In case of tail call optimization mark all arguments mutable. Since they
1634 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001635 if (Flags.isByVal()) {
1636 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001637 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001638 return DAG.getFrameIndex(FI, getPointerTy());
1639 } else {
1640 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001641 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001642 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1643 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001644 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001645 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001646 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001647}
1648
Dan Gohman475871a2008-07-27 21:46:04 +00001649SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001650X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001651 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001652 bool isVarArg,
1653 const SmallVectorImpl<ISD::InputArg> &Ins,
1654 DebugLoc dl,
1655 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001656 SmallVectorImpl<SDValue> &InVals)
1657 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001658 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001659 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
Gordon Henriksen86737662008-01-05 16:56:59 +00001661 const Function* Fn = MF.getFunction();
1662 if (Fn->hasExternalLinkage() &&
1663 Subtarget->isTargetCygMing() &&
1664 Fn->getName() == "main")
1665 FuncInfo->setForceFramePointer(true);
1666
Evan Cheng1bc78042006-04-26 01:20:17 +00001667 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001668 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001669 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001670
Chris Lattner29689432010-03-11 00:22:57 +00001671 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1672 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001673
Chris Lattner638402b2007-02-28 07:00:42 +00001674 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001675 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001676 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1677 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001678
1679 // Allocate shadow area for Win64
1680 if (IsWin64) {
1681 CCInfo.AllocateStack(32, 8);
1682 }
1683
Duncan Sands45907662010-10-31 13:21:44 +00001684 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001685
Chris Lattnerf39f7712007-02-28 05:46:49 +00001686 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001687 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001688 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1689 CCValAssign &VA = ArgLocs[i];
1690 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1691 // places.
1692 assert(VA.getValNo() != LastVal &&
1693 "Don't support value assigned to multiple locs yet");
1694 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001695
Chris Lattnerf39f7712007-02-28 05:46:49 +00001696 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001697 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001698 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001700 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001704 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001707 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1708 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001709 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001710 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001711 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001712 RC = X86::VR64RegisterClass;
1713 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001714 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001715
Devang Patel68e6bee2011-02-21 23:21:26 +00001716 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Chris Lattnerf39f7712007-02-28 05:46:49 +00001719 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1720 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1721 // right size.
1722 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001723 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001724 DAG.getValueType(VA.getValVT()));
1725 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001726 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001727 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001728 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001729 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001730
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001731 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001732 // Handle MMX values passed in XMM regs.
1733 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001734 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1735 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001736 } else
1737 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001738 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001739 } else {
1740 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001742 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001743
1744 // If value is passed via pointer - do a load.
1745 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001746 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1747 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001748
Dan Gohman98ca4f22009-08-05 01:29:28 +00001749 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001750 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001751
Dan Gohman61a92132008-04-21 23:59:07 +00001752 // The x86-64 ABI for returning structs by value requires that we copy
1753 // the sret argument into %rax for the return. Save the argument into
1754 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001755 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001756 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1757 unsigned Reg = FuncInfo->getSRetReturnReg();
1758 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001760 FuncInfo->setSRetReturnReg(Reg);
1761 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001762 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001764 }
1765
Chris Lattnerf39f7712007-02-28 05:46:49 +00001766 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001767 // Align stack specially for tail calls.
1768 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001769 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001770
Evan Cheng1bc78042006-04-26 01:20:17 +00001771 // If the function takes variable number of arguments, make a frame index for
1772 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001773 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001774 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1775 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001776 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001777 }
1778 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001779 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1780
1781 // FIXME: We should really autogenerate these arrays
1782 static const unsigned GPR64ArgRegsWin64[] = {
1783 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001784 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001785 static const unsigned GPR64ArgRegs64Bit[] = {
1786 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1787 };
1788 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001789 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1790 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1791 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001792 const unsigned *GPR64ArgRegs;
1793 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001794
1795 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001796 // The XMM registers which might contain var arg parameters are shadowed
1797 // in their paired GPR. So we only need to save the GPR to their home
1798 // slots.
1799 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001800 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001801 } else {
1802 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1803 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001804
1805 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001806 }
1807 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1808 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001809
Devang Patel578efa92009-06-05 21:57:13 +00001810 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001811 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001812 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001813 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001814 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001815 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001816 // Kernel mode asks for SSE to be disabled, so don't push them
1817 // on the stack.
1818 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001819
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001820 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001821 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001822 // Get to the caller-allocated home save location. Add 8 to account
1823 // for the return address.
1824 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001825 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001826 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001827 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1828 } else {
1829 // For X86-64, if there are vararg parameters that are passed via
1830 // registers, then we must store them to their spots on the stack so they
1831 // may be loaded by deferencing the result of va_next.
1832 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1833 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1834 FuncInfo->setRegSaveFrameIndex(
1835 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001836 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001837 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001838
Gordon Henriksen86737662008-01-05 16:56:59 +00001839 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001841 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1842 getPointerTy());
1843 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001844 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001845 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1846 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001847 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001848 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001851 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001852 MachinePointerInfo::getFixedStack(
1853 FuncInfo->getRegSaveFrameIndex(), Offset),
1854 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001855 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001856 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001857 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001858
Dan Gohmanface41a2009-08-16 21:24:25 +00001859 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1860 // Now store the XMM (fp + vector) parameter registers.
1861 SmallVector<SDValue, 11> SaveXMMOps;
1862 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001863
Devang Patel68e6bee2011-02-21 23:21:26 +00001864 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001865 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1866 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001867
Dan Gohman1e93df62010-04-17 14:41:14 +00001868 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1869 FuncInfo->getRegSaveFrameIndex()));
1870 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1871 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001872
Dan Gohmanface41a2009-08-16 21:24:25 +00001873 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001874 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001875 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001876 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1877 SaveXMMOps.push_back(Val);
1878 }
1879 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1880 MVT::Other,
1881 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001883
1884 if (!MemOps.empty())
1885 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1886 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001887 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001888 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001889
Gordon Henriksen86737662008-01-05 16:56:59 +00001890 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001891 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001892 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001893 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001894 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001895 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001896 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001897 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001898 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001899
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001901 // RegSaveFrameIndex is X86-64 only.
1902 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001903 if (CallConv == CallingConv::X86_FastCall ||
1904 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001905 // fastcc functions can't have varargs.
1906 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001907 }
Evan Cheng25caf632006-05-23 21:06:34 +00001908
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001910}
1911
Dan Gohman475871a2008-07-27 21:46:04 +00001912SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001913X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1914 SDValue StackPtr, SDValue Arg,
1915 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001916 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001917 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001918 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001919 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001920 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001921 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001922 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001923
1924 return DAG.getStore(Chain, dl, Arg, PtrOff,
1925 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001926 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001927}
1928
Bill Wendling64e87322009-01-16 19:25:27 +00001929/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001930/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001931SDValue
1932X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001933 SDValue &OutRetAddr, SDValue Chain,
1934 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001935 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001936 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001937 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001938 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001939
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001940 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001941 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1942 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001943 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001944}
1945
1946/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1947/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001948static SDValue
1949EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001950 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001951 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001952 // Store the return address to the appropriate stack slot.
1953 if (!FPDiff) return Chain;
1954 // Calculate the new stack slot for the return address.
1955 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001956 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001957 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001958 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001960 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001961 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001962 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001963 return Chain;
1964}
1965
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001967X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001968 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001969 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001971 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001972 const SmallVectorImpl<ISD::InputArg> &Ins,
1973 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975 MachineFunction &MF = DAG.getMachineFunction();
1976 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001977 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001979 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980
Evan Cheng5f941932010-02-05 02:21:12 +00001981 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001982 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001983 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1984 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001985 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001986
1987 // Sibcalls are automatically detected tailcalls which do not require
1988 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001989 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001990 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001991
1992 if (isTailCall)
1993 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001994 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001995
Chris Lattner29689432010-03-11 00:22:57 +00001996 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1997 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001998
Chris Lattner638402b2007-02-28 07:00:42 +00001999 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002000 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002001 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2002 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002003
2004 // Allocate shadow area for Win64
2005 if (IsWin64) {
2006 CCInfo.AllocateStack(32, 8);
2007 }
2008
Duncan Sands45907662010-10-31 13:21:44 +00002009 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002010
Chris Lattner423c5f42007-02-28 05:31:48 +00002011 // Get a count of how many bytes are to be pushed on the stack.
2012 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002013 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002014 // This is a sibcall. The memory operands are available in caller's
2015 // own caller's stack.
2016 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002017 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002018 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002019
Gordon Henriksen86737662008-01-05 16:56:59 +00002020 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002021 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002022 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002023 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002024 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2025 FPDiff = NumBytesCallerPushed - NumBytes;
2026
2027 // Set the delta of movement of the returnaddr stackslot.
2028 // But only set if delta is greater than previous delta.
2029 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2030 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2031 }
2032
Evan Chengf22f9b32010-02-06 03:28:46 +00002033 if (!IsSibcall)
2034 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002035
Dan Gohman475871a2008-07-27 21:46:04 +00002036 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002037 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002038 if (isTailCall && FPDiff)
2039 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2040 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002041
Dan Gohman475871a2008-07-27 21:46:04 +00002042 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2043 SmallVector<SDValue, 8> MemOpChains;
2044 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002045
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002046 // Walk the register/memloc assignments, inserting copies/loads. In the case
2047 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002048 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2049 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002050 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002051 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002052 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002053 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002054
Chris Lattner423c5f42007-02-28 05:31:48 +00002055 // Promote the value if needed.
2056 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002057 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002058 case CCValAssign::Full: break;
2059 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002060 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002061 break;
2062 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002063 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002064 break;
2065 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002066 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2067 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002068 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002069 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2070 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002071 } else
2072 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2073 break;
2074 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002075 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002076 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002077 case CCValAssign::Indirect: {
2078 // Store the argument.
2079 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002080 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002081 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002082 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002083 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002084 Arg = SpillSlot;
2085 break;
2086 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002087 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Chris Lattner423c5f42007-02-28 05:31:48 +00002089 if (VA.isRegLoc()) {
2090 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002091 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002092 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2093 // shadow reg if callee is a varargs function.
2094 unsigned ShadowReg = 0;
2095 switch (VA.getLocReg()) {
2096 case X86::XMM0: ShadowReg = X86::RCX; break;
2097 case X86::XMM1: ShadowReg = X86::RDX; break;
2098 case X86::XMM2: ShadowReg = X86::R8; break;
2099 case X86::XMM3: ShadowReg = X86::R9; break;
2100 }
2101 if (ShadowReg)
2102 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2103 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002104 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002105 assert(VA.isMemLoc());
2106 if (StackPtr.getNode() == 0)
2107 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2108 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2109 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002110 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002112
Evan Cheng32fe1032006-05-25 00:59:30 +00002113 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002114 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002115 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002116
Evan Cheng347d5f72006-04-28 21:29:37 +00002117 // Build a sequence of copy-to-reg nodes chained together with token chain
2118 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002119 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002120 // Tail call byval lowering might overwrite argument registers so in case of
2121 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002122 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002123 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002124 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002125 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002126 InFlag = Chain.getValue(1);
2127 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002128
Chris Lattner88e1fd52009-07-09 04:24:46 +00002129 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002130 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2131 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002132 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002133 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2134 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002135 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002136 InFlag);
2137 InFlag = Chain.getValue(1);
2138 } else {
2139 // If we are tail calling and generating PIC/GOT style code load the
2140 // address of the callee into ECX. The value in ecx is used as target of
2141 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2142 // for tail calls on PIC/GOT architectures. Normally we would just put the
2143 // address of GOT into ebx and then call target@PLT. But for tail calls
2144 // ebx would be restored (since ebx is callee saved) before jumping to the
2145 // target@PLT.
2146
2147 // Note: The actual moving to ECX is done further down.
2148 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2149 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2150 !G->getGlobal()->hasProtectedVisibility())
2151 Callee = LowerGlobalAddress(Callee, DAG);
2152 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002153 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002154 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002155 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002156
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002157 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002158 // From AMD64 ABI document:
2159 // For calls that may call functions that use varargs or stdargs
2160 // (prototype-less calls or calls to functions containing ellipsis (...) in
2161 // the declaration) %al is used as hidden argument to specify the number
2162 // of SSE registers used. The contents of %al do not need to match exactly
2163 // the number of registers, but must be an ubound on the number of SSE
2164 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002165
Gordon Henriksen86737662008-01-05 16:56:59 +00002166 // Count the number of XMM registers allocated.
2167 static const unsigned XMMArgRegs[] = {
2168 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2169 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2170 };
2171 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002172 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002173 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002174
Dale Johannesendd64c412009-02-04 00:33:20 +00002175 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002177 InFlag = Chain.getValue(1);
2178 }
2179
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002180
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002181 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002182 if (isTailCall) {
2183 // Force all the incoming stack arguments to be loaded from the stack
2184 // before any new outgoing arguments are stored to the stack, because the
2185 // outgoing stack slots may alias the incoming argument stack slots, and
2186 // the alias isn't otherwise explicit. This is slightly more conservative
2187 // than necessary, because it means that each store effectively depends
2188 // on every argument instead of just those arguments it would clobber.
2189 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2190
Dan Gohman475871a2008-07-27 21:46:04 +00002191 SmallVector<SDValue, 8> MemOpChains2;
2192 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002193 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002194 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002195 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002196 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002197 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2198 CCValAssign &VA = ArgLocs[i];
2199 if (VA.isRegLoc())
2200 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002201 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002202 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002204 // Create frame index.
2205 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002206 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002207 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002208 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002209
Duncan Sands276dcbd2008-03-21 09:14:45 +00002210 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002211 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002212 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002213 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002214 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002215 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002216 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002217
Dan Gohman98ca4f22009-08-05 01:29:28 +00002218 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2219 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002220 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002222 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002223 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002225 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002226 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002227 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002228 }
2229 }
2230
2231 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002232 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002233 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002234
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002235 // Copy arguments to their registers.
2236 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002237 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002238 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002239 InFlag = Chain.getValue(1);
2240 }
Dan Gohman475871a2008-07-27 21:46:04 +00002241 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002242
Gordon Henriksen86737662008-01-05 16:56:59 +00002243 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002244 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002245 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002246 }
2247
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002248 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2249 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2250 // In the 64-bit large code model, we have to make all calls
2251 // through a register, since the call instruction's 32-bit
2252 // pc-relative offset may not be large enough to hold the whole
2253 // address.
2254 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002255 // If the callee is a GlobalAddress node (quite common, every direct call
2256 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2257 // it.
2258
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002259 // We should use extra load for direct calls to dllimported functions in
2260 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002261 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002262 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002263 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002264
Chris Lattner48a7d022009-07-09 05:02:21 +00002265 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2266 // external symbols most go through the PLT in PIC mode. If the symbol
2267 // has hidden or protected visibility, or if it is static or local, then
2268 // we don't need to use the PLT - we can directly call it.
2269 if (Subtarget->isTargetELF() &&
2270 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002271 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002272 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002273 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002274 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2275 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002276 // PC-relative references to external symbols should go through $stub,
2277 // unless we're building with the leopard linker or later, which
2278 // automatically synthesizes these stubs.
2279 OpFlags = X86II::MO_DARWIN_STUB;
2280 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002281
Devang Patel0d881da2010-07-06 22:08:15 +00002282 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002283 G->getOffset(), OpFlags);
2284 }
Bill Wendling056292f2008-09-16 21:48:12 +00002285 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002286 unsigned char OpFlags = 0;
2287
Evan Cheng1bf891a2010-12-01 22:59:46 +00002288 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2289 // external symbols should go through the PLT.
2290 if (Subtarget->isTargetELF() &&
2291 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2292 OpFlags = X86II::MO_PLT;
2293 } else if (Subtarget->isPICStyleStubAny() &&
2294 Subtarget->getDarwinVers() < 9) {
2295 // PC-relative references to external symbols should go through $stub,
2296 // unless we're building with the leopard linker or later, which
2297 // automatically synthesizes these stubs.
2298 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002299 }
Eric Christopherfd179292009-08-27 18:07:15 +00002300
Chris Lattner48a7d022009-07-09 05:02:21 +00002301 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2302 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002303 }
2304
Chris Lattnerd96d0722007-02-25 06:40:16 +00002305 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002306 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002307 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002308
Evan Chengf22f9b32010-02-06 03:28:46 +00002309 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002310 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2311 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002312 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002314
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002315 Ops.push_back(Chain);
2316 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002317
Dan Gohman98ca4f22009-08-05 01:29:28 +00002318 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002319 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002320
Gordon Henriksen86737662008-01-05 16:56:59 +00002321 // Add argument registers to the end of the list so that they are known live
2322 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002323 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2324 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2325 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002326
Evan Cheng586ccac2008-03-18 23:36:35 +00002327 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002328 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002329 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2330
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002331 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002332 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002333 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002334
Gabor Greifba36cb52008-08-28 21:40:38 +00002335 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002336 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002337
Dan Gohman98ca4f22009-08-05 01:29:28 +00002338 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002339 // We used to do:
2340 //// If this is the first return lowered for this function, add the regs
2341 //// to the liveout set for the function.
2342 // This isn't right, although it's probably harmless on x86; liveouts
2343 // should be computed from returns not tail calls. Consider a void
2344 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002345 return DAG.getNode(X86ISD::TC_RETURN, dl,
2346 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002347 }
2348
Dale Johannesenace16102009-02-03 19:33:06 +00002349 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002350 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002351
Chris Lattner2d297092006-05-23 18:50:38 +00002352 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002353 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002354 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002356 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002357 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002358 // pops the hidden struct pointer, so we have to push it back.
2359 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002360 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002361 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002362 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002363
Gordon Henriksenae636f82008-01-03 16:47:34 +00002364 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002365 if (!IsSibcall) {
2366 Chain = DAG.getCALLSEQ_END(Chain,
2367 DAG.getIntPtrConstant(NumBytes, true),
2368 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2369 true),
2370 InFlag);
2371 InFlag = Chain.getValue(1);
2372 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002373
Chris Lattner3085e152007-02-25 08:59:22 +00002374 // Handle result values, copying them out of physregs into vregs that we
2375 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002376 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2377 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002378}
2379
Evan Cheng25ab6902006-09-08 06:48:29 +00002380
2381//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002382// Fast Calling Convention (tail call) implementation
2383//===----------------------------------------------------------------------===//
2384
2385// Like std call, callee cleans arguments, convention except that ECX is
2386// reserved for storing the tail called function address. Only 2 registers are
2387// free for argument passing (inreg). Tail call optimization is performed
2388// provided:
2389// * tailcallopt is enabled
2390// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002391// On X86_64 architecture with GOT-style position independent code only local
2392// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002393// To keep the stack aligned according to platform abi the function
2394// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2395// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002396// If a tail called function callee has more arguments than the caller the
2397// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002398// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002399// original REtADDR, but before the saved framepointer or the spilled registers
2400// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2401// stack layout:
2402// arg1
2403// arg2
2404// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002405// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002406// move area ]
2407// (possible EBP)
2408// ESI
2409// EDI
2410// local1 ..
2411
2412/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2413/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002414unsigned
2415X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2416 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002417 MachineFunction &MF = DAG.getMachineFunction();
2418 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002419 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002420 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002421 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002422 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002423 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002424 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2425 // Number smaller than 12 so just add the difference.
2426 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2427 } else {
2428 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002429 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002430 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002431 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002432 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002433}
2434
Evan Cheng5f941932010-02-05 02:21:12 +00002435/// MatchingStackOffset - Return true if the given stack call argument is
2436/// already available in the same position (relatively) of the caller's
2437/// incoming argument stack.
2438static
2439bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2440 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2441 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002442 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2443 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002444 if (Arg.getOpcode() == ISD::CopyFromReg) {
2445 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002446 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002447 return false;
2448 MachineInstr *Def = MRI->getVRegDef(VR);
2449 if (!Def)
2450 return false;
2451 if (!Flags.isByVal()) {
2452 if (!TII->isLoadFromStackSlot(Def, FI))
2453 return false;
2454 } else {
2455 unsigned Opcode = Def->getOpcode();
2456 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2457 Def->getOperand(1).isFI()) {
2458 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002459 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002460 } else
2461 return false;
2462 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002463 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2464 if (Flags.isByVal())
2465 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002466 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002467 // define @foo(%struct.X* %A) {
2468 // tail call @bar(%struct.X* byval %A)
2469 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002470 return false;
2471 SDValue Ptr = Ld->getBasePtr();
2472 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2473 if (!FINode)
2474 return false;
2475 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002476 } else
2477 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002478
Evan Cheng4cae1332010-03-05 08:38:04 +00002479 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002480 if (!MFI->isFixedObjectIndex(FI))
2481 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002482 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002483}
2484
Dan Gohman98ca4f22009-08-05 01:29:28 +00002485/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2486/// for tail call optimization. Targets which want to do tail call
2487/// optimization should implement this function.
2488bool
2489X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002490 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002492 bool isCalleeStructRet,
2493 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002494 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002495 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002496 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002497 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002498 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002499 CalleeCC != CallingConv::C)
2500 return false;
2501
Evan Cheng7096ae42010-01-29 06:45:59 +00002502 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002503 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002504 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002505 CallingConv::ID CallerCC = CallerF->getCallingConv();
2506 bool CCMatch = CallerCC == CalleeCC;
2507
Dan Gohman1797ed52010-02-08 20:27:50 +00002508 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002509 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002510 return true;
2511 return false;
2512 }
2513
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002514 // Look for obvious safe cases to perform tail call optimization that do not
2515 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002516
Evan Cheng2c12cb42010-03-26 16:26:03 +00002517 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2518 // emit a special epilogue.
2519 if (RegInfo->needsStackRealignment(MF))
2520 return false;
2521
Eric Christopher90eb4022010-07-22 00:26:08 +00002522 // Do not sibcall optimize vararg calls unless the call site is not passing
2523 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002524 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002525 return false;
2526
Evan Chenga375d472010-03-15 18:54:48 +00002527 // Also avoid sibcall optimization if either caller or callee uses struct
2528 // return semantics.
2529 if (isCalleeStructRet || isCallerStructRet)
2530 return false;
2531
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002532 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2533 // Therefore if it's not used by the call it is not safe to optimize this into
2534 // a sibcall.
2535 bool Unused = false;
2536 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2537 if (!Ins[i].Used) {
2538 Unused = true;
2539 break;
2540 }
2541 }
2542 if (Unused) {
2543 SmallVector<CCValAssign, 16> RVLocs;
2544 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2545 RVLocs, *DAG.getContext());
2546 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002547 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002548 CCValAssign &VA = RVLocs[i];
2549 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2550 return false;
2551 }
2552 }
2553
Evan Cheng13617962010-04-30 01:12:32 +00002554 // If the calling conventions do not match, then we'd better make sure the
2555 // results are returned in the same way as what the caller expects.
2556 if (!CCMatch) {
2557 SmallVector<CCValAssign, 16> RVLocs1;
2558 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2559 RVLocs1, *DAG.getContext());
2560 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2561
2562 SmallVector<CCValAssign, 16> RVLocs2;
2563 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2564 RVLocs2, *DAG.getContext());
2565 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2566
2567 if (RVLocs1.size() != RVLocs2.size())
2568 return false;
2569 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2570 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2571 return false;
2572 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2573 return false;
2574 if (RVLocs1[i].isRegLoc()) {
2575 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2576 return false;
2577 } else {
2578 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2579 return false;
2580 }
2581 }
2582 }
2583
Evan Chenga6bff982010-01-30 01:22:00 +00002584 // If the callee takes no arguments then go on to check the results of the
2585 // call.
2586 if (!Outs.empty()) {
2587 // Check if stack adjustment is needed. For now, do not do this if any
2588 // argument is passed on the stack.
2589 SmallVector<CCValAssign, 16> ArgLocs;
2590 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2591 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002592
2593 // Allocate shadow area for Win64
2594 if (Subtarget->isTargetWin64()) {
2595 CCInfo.AllocateStack(32, 8);
2596 }
2597
Duncan Sands45907662010-10-31 13:21:44 +00002598 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002599 if (CCInfo.getNextStackOffset()) {
2600 MachineFunction &MF = DAG.getMachineFunction();
2601 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2602 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002603
2604 // Check if the arguments are already laid out in the right way as
2605 // the caller's fixed stack objects.
2606 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002607 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2608 const X86InstrInfo *TII =
2609 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002610 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2611 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002612 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002613 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002614 if (VA.getLocInfo() == CCValAssign::Indirect)
2615 return false;
2616 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002617 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2618 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002619 return false;
2620 }
2621 }
2622 }
Evan Cheng9c044672010-05-29 01:35:22 +00002623
2624 // If the tailcall address may be in a register, then make sure it's
2625 // possible to register allocate for it. In 32-bit, the call address can
2626 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002627 // callee-saved registers are restored. These happen to be the same
2628 // registers used to pass 'inreg' arguments so watch out for those.
2629 if (!Subtarget->is64Bit() &&
2630 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002631 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002632 unsigned NumInRegs = 0;
2633 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2634 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002635 if (!VA.isRegLoc())
2636 continue;
2637 unsigned Reg = VA.getLocReg();
2638 switch (Reg) {
2639 default: break;
2640 case X86::EAX: case X86::EDX: case X86::ECX:
2641 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002642 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002643 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002644 }
2645 }
2646 }
Evan Chenga6bff982010-01-30 01:22:00 +00002647 }
Evan Chengb1712452010-01-27 06:25:16 +00002648
Dale Johannesend155d7e2010-10-25 22:17:05 +00002649 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002650 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002651 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2652 return false;
2653
Evan Cheng86809cc2010-02-03 03:28:02 +00002654 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002655}
2656
Dan Gohman3df24e62008-09-03 23:12:08 +00002657FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002658X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2659 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002660}
2661
2662
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002663//===----------------------------------------------------------------------===//
2664// Other Lowering Hooks
2665//===----------------------------------------------------------------------===//
2666
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002667static bool MayFoldLoad(SDValue Op) {
2668 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2669}
2670
2671static bool MayFoldIntoStore(SDValue Op) {
2672 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2673}
2674
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002675static bool isTargetShuffle(unsigned Opcode) {
2676 switch(Opcode) {
2677 default: return false;
2678 case X86ISD::PSHUFD:
2679 case X86ISD::PSHUFHW:
2680 case X86ISD::PSHUFLW:
2681 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002682 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002683 case X86ISD::SHUFPS:
2684 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002685 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002686 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002687 case X86ISD::MOVLPS:
2688 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002689 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002690 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002691 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002692 case X86ISD::MOVSS:
2693 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002694 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002695 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002696 case X86ISD::VUNPCKLPS:
2697 case X86ISD::VUNPCKLPD:
2698 case X86ISD::VUNPCKLPSY:
2699 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002700 case X86ISD::PUNPCKLWD:
2701 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002702 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002703 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002704 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002705 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002706 case X86ISD::PUNPCKHWD:
2707 case X86ISD::PUNPCKHBW:
2708 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002709 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002710 return true;
2711 }
2712 return false;
2713}
2714
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002715static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002716 SDValue V1, SelectionDAG &DAG) {
2717 switch(Opc) {
2718 default: llvm_unreachable("Unknown x86 shuffle node");
2719 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002720 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002721 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002722 return DAG.getNode(Opc, dl, VT, V1);
2723 }
2724
2725 return SDValue();
2726}
2727
2728static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002729 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002730 switch(Opc) {
2731 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002732 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002733 case X86ISD::PSHUFHW:
2734 case X86ISD::PSHUFLW:
2735 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2736 }
2737
2738 return SDValue();
2739}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002740
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002741static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2742 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2743 switch(Opc) {
2744 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002745 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002746 case X86ISD::SHUFPD:
2747 case X86ISD::SHUFPS:
2748 return DAG.getNode(Opc, dl, VT, V1, V2,
2749 DAG.getConstant(TargetMask, MVT::i8));
2750 }
2751 return SDValue();
2752}
2753
2754static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2755 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2756 switch(Opc) {
2757 default: llvm_unreachable("Unknown x86 shuffle node");
2758 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002759 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002760 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002761 case X86ISD::MOVLPS:
2762 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002763 case X86ISD::MOVSS:
2764 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002765 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002766 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002767 case X86ISD::VUNPCKLPS:
2768 case X86ISD::VUNPCKLPD:
2769 case X86ISD::VUNPCKLPSY:
2770 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002771 case X86ISD::PUNPCKLWD:
2772 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002773 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002774 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002775 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002776 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002777 case X86ISD::PUNPCKHWD:
2778 case X86ISD::PUNPCKHBW:
2779 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002780 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002781 return DAG.getNode(Opc, dl, VT, V1, V2);
2782 }
2783 return SDValue();
2784}
2785
Dan Gohmand858e902010-04-17 15:26:15 +00002786SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002787 MachineFunction &MF = DAG.getMachineFunction();
2788 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2789 int ReturnAddrIndex = FuncInfo->getRAIndex();
2790
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002791 if (ReturnAddrIndex == 0) {
2792 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002793 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002794 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002795 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002796 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002797 }
2798
Evan Cheng25ab6902006-09-08 06:48:29 +00002799 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002800}
2801
2802
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002803bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2804 bool hasSymbolicDisplacement) {
2805 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002806 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002807 return false;
2808
2809 // If we don't have a symbolic displacement - we don't have any extra
2810 // restrictions.
2811 if (!hasSymbolicDisplacement)
2812 return true;
2813
2814 // FIXME: Some tweaks might be needed for medium code model.
2815 if (M != CodeModel::Small && M != CodeModel::Kernel)
2816 return false;
2817
2818 // For small code model we assume that latest object is 16MB before end of 31
2819 // bits boundary. We may also accept pretty large negative constants knowing
2820 // that all objects are in the positive half of address space.
2821 if (M == CodeModel::Small && Offset < 16*1024*1024)
2822 return true;
2823
2824 // For kernel code model we know that all object resist in the negative half
2825 // of 32bits address space. We may not accept negative offsets, since they may
2826 // be just off and we may accept pretty large positive ones.
2827 if (M == CodeModel::Kernel && Offset > 0)
2828 return true;
2829
2830 return false;
2831}
2832
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002833/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2834/// specific condition code, returning the condition code and the LHS/RHS of the
2835/// comparison to make.
2836static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2837 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002838 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002839 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2840 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2841 // X > -1 -> X == 0, jump !sign.
2842 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002843 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002844 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2845 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002846 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002847 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002848 // X < 1 -> X <= 0
2849 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002850 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002851 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002852 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002853
Evan Chengd9558e02006-01-06 00:43:03 +00002854 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002855 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002856 case ISD::SETEQ: return X86::COND_E;
2857 case ISD::SETGT: return X86::COND_G;
2858 case ISD::SETGE: return X86::COND_GE;
2859 case ISD::SETLT: return X86::COND_L;
2860 case ISD::SETLE: return X86::COND_LE;
2861 case ISD::SETNE: return X86::COND_NE;
2862 case ISD::SETULT: return X86::COND_B;
2863 case ISD::SETUGT: return X86::COND_A;
2864 case ISD::SETULE: return X86::COND_BE;
2865 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002866 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002867 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002868
Chris Lattner4c78e022008-12-23 23:42:27 +00002869 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002870
Chris Lattner4c78e022008-12-23 23:42:27 +00002871 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002872 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2873 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002874 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2875 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002876 }
2877
Chris Lattner4c78e022008-12-23 23:42:27 +00002878 switch (SetCCOpcode) {
2879 default: break;
2880 case ISD::SETOLT:
2881 case ISD::SETOLE:
2882 case ISD::SETUGT:
2883 case ISD::SETUGE:
2884 std::swap(LHS, RHS);
2885 break;
2886 }
2887
2888 // On a floating point condition, the flags are set as follows:
2889 // ZF PF CF op
2890 // 0 | 0 | 0 | X > Y
2891 // 0 | 0 | 1 | X < Y
2892 // 1 | 0 | 0 | X == Y
2893 // 1 | 1 | 1 | unordered
2894 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002895 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002896 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002897 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002898 case ISD::SETOLT: // flipped
2899 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002900 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002901 case ISD::SETOLE: // flipped
2902 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002903 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002904 case ISD::SETUGT: // flipped
2905 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002906 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002907 case ISD::SETUGE: // flipped
2908 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002909 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002910 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002911 case ISD::SETNE: return X86::COND_NE;
2912 case ISD::SETUO: return X86::COND_P;
2913 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002914 case ISD::SETOEQ:
2915 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002916 }
Evan Chengd9558e02006-01-06 00:43:03 +00002917}
2918
Evan Cheng4a460802006-01-11 00:33:36 +00002919/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2920/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002921/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002922static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002923 switch (X86CC) {
2924 default:
2925 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002926 case X86::COND_B:
2927 case X86::COND_BE:
2928 case X86::COND_E:
2929 case X86::COND_P:
2930 case X86::COND_A:
2931 case X86::COND_AE:
2932 case X86::COND_NE:
2933 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002934 return true;
2935 }
2936}
2937
Evan Chengeb2f9692009-10-27 19:56:55 +00002938/// isFPImmLegal - Returns true if the target can instruction select the
2939/// specified FP immediate natively. If false, the legalizer will
2940/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002941bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002942 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2943 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2944 return true;
2945 }
2946 return false;
2947}
2948
Nate Begeman9008ca62009-04-27 18:41:29 +00002949/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2950/// the specified range (L, H].
2951static bool isUndefOrInRange(int Val, int Low, int Hi) {
2952 return (Val < 0) || (Val >= Low && Val < Hi);
2953}
2954
2955/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2956/// specified value.
2957static bool isUndefOrEqual(int Val, int CmpVal) {
2958 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002959 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002960 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002961}
2962
Nate Begeman9008ca62009-04-27 18:41:29 +00002963/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2964/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2965/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002966static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002967 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002968 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002969 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002970 return (Mask[0] < 2 && Mask[1] < 2);
2971 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002972}
2973
Nate Begeman9008ca62009-04-27 18:41:29 +00002974bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002975 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 N->getMask(M);
2977 return ::isPSHUFDMask(M, N->getValueType(0));
2978}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002979
Nate Begeman9008ca62009-04-27 18:41:29 +00002980/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2981/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002982static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002983 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002984 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002985
Nate Begeman9008ca62009-04-27 18:41:29 +00002986 // Lower quadword copied in order or undef.
2987 for (int i = 0; i != 4; ++i)
2988 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002989 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002990
Evan Cheng506d3df2006-03-29 23:07:14 +00002991 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002992 for (int i = 4; i != 8; ++i)
2993 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002994 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002995
Evan Cheng506d3df2006-03-29 23:07:14 +00002996 return true;
2997}
2998
Nate Begeman9008ca62009-04-27 18:41:29 +00002999bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003000 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003001 N->getMask(M);
3002 return ::isPSHUFHWMask(M, N->getValueType(0));
3003}
Evan Cheng506d3df2006-03-29 23:07:14 +00003004
Nate Begeman9008ca62009-04-27 18:41:29 +00003005/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3006/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003007static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003008 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003009 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003010
Rafael Espindola15684b22009-04-24 12:40:33 +00003011 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003012 for (int i = 4; i != 8; ++i)
3013 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003014 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003015
Rafael Espindola15684b22009-04-24 12:40:33 +00003016 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003017 for (int i = 0; i != 4; ++i)
3018 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003019 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003020
Rafael Espindola15684b22009-04-24 12:40:33 +00003021 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003022}
3023
Nate Begeman9008ca62009-04-27 18:41:29 +00003024bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003025 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003026 N->getMask(M);
3027 return ::isPSHUFLWMask(M, N->getValueType(0));
3028}
3029
Nate Begemana09008b2009-10-19 02:17:23 +00003030/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3031/// is suitable for input to PALIGNR.
3032static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3033 bool hasSSSE3) {
3034 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003035
Nate Begemana09008b2009-10-19 02:17:23 +00003036 // Do not handle v2i64 / v2f64 shuffles with palignr.
3037 if (e < 4 || !hasSSSE3)
3038 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003039
Nate Begemana09008b2009-10-19 02:17:23 +00003040 for (i = 0; i != e; ++i)
3041 if (Mask[i] >= 0)
3042 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003043
Nate Begemana09008b2009-10-19 02:17:23 +00003044 // All undef, not a palignr.
3045 if (i == e)
3046 return false;
3047
3048 // Determine if it's ok to perform a palignr with only the LHS, since we
3049 // don't have access to the actual shuffle elements to see if RHS is undef.
3050 bool Unary = Mask[i] < (int)e;
3051 bool NeedsUnary = false;
3052
3053 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003054
Nate Begemana09008b2009-10-19 02:17:23 +00003055 // Check the rest of the elements to see if they are consecutive.
3056 for (++i; i != e; ++i) {
3057 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003058 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003059 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003060
Nate Begemana09008b2009-10-19 02:17:23 +00003061 Unary = Unary && (m < (int)e);
3062 NeedsUnary = NeedsUnary || (m < s);
3063
3064 if (NeedsUnary && !Unary)
3065 return false;
3066 if (Unary && m != ((s+i) & (e-1)))
3067 return false;
3068 if (!Unary && m != (s+i))
3069 return false;
3070 }
3071 return true;
3072}
3073
3074bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3075 SmallVector<int, 8> M;
3076 N->getMask(M);
3077 return ::isPALIGNRMask(M, N->getValueType(0), true);
3078}
3079
Evan Cheng14aed5e2006-03-24 01:18:28 +00003080/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3081/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003082static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 int NumElems = VT.getVectorNumElements();
3084 if (NumElems != 2 && NumElems != 4)
3085 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003086
Nate Begeman9008ca62009-04-27 18:41:29 +00003087 int Half = NumElems / 2;
3088 for (int i = 0; i < Half; ++i)
3089 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003090 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 for (int i = Half; i < NumElems; ++i)
3092 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003093 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003094
Evan Cheng14aed5e2006-03-24 01:18:28 +00003095 return true;
3096}
3097
Nate Begeman9008ca62009-04-27 18:41:29 +00003098bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3099 SmallVector<int, 8> M;
3100 N->getMask(M);
3101 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003102}
3103
Evan Cheng213d2cf2007-05-17 18:45:50 +00003104/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003105/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3106/// half elements to come from vector 1 (which would equal the dest.) and
3107/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003108static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003109 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003110
3111 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003113
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 int Half = NumElems / 2;
3115 for (int i = 0; i < Half; ++i)
3116 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003117 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 for (int i = Half; i < NumElems; ++i)
3119 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003120 return false;
3121 return true;
3122}
3123
Nate Begeman9008ca62009-04-27 18:41:29 +00003124static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3125 SmallVector<int, 8> M;
3126 N->getMask(M);
3127 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003128}
3129
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003130/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3131/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003132bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3133 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003134 return false;
3135
Evan Cheng2064a2b2006-03-28 06:50:32 +00003136 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003137 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3138 isUndefOrEqual(N->getMaskElt(1), 7) &&
3139 isUndefOrEqual(N->getMaskElt(2), 2) &&
3140 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003141}
3142
Nate Begeman0b10b912009-11-07 23:17:15 +00003143/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3144/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3145/// <2, 3, 2, 3>
3146bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3147 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003148
Nate Begeman0b10b912009-11-07 23:17:15 +00003149 if (NumElems != 4)
3150 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003151
Nate Begeman0b10b912009-11-07 23:17:15 +00003152 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3153 isUndefOrEqual(N->getMaskElt(1), 3) &&
3154 isUndefOrEqual(N->getMaskElt(2), 2) &&
3155 isUndefOrEqual(N->getMaskElt(3), 3);
3156}
3157
Evan Cheng5ced1d82006-04-06 23:23:56 +00003158/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3159/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003160bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3161 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003162
Evan Cheng5ced1d82006-04-06 23:23:56 +00003163 if (NumElems != 2 && NumElems != 4)
3164 return false;
3165
Evan Chengc5cdff22006-04-07 21:53:05 +00003166 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003168 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003169
Evan Chengc5cdff22006-04-07 21:53:05 +00003170 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003172 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003173
3174 return true;
3175}
3176
Nate Begeman0b10b912009-11-07 23:17:15 +00003177/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3178/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3179bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003181
Evan Cheng5ced1d82006-04-06 23:23:56 +00003182 if (NumElems != 2 && NumElems != 4)
3183 return false;
3184
Evan Chengc5cdff22006-04-07 21:53:05 +00003185 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003187 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003188
Nate Begeman9008ca62009-04-27 18:41:29 +00003189 for (unsigned i = 0; i < NumElems/2; ++i)
3190 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003191 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003192
3193 return true;
3194}
3195
Evan Cheng0038e592006-03-28 00:39:58 +00003196/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3197/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003198static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003199 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003201 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003202 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003203
Nate Begeman9008ca62009-04-27 18:41:29 +00003204 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3205 int BitI = Mask[i];
3206 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003207 if (!isUndefOrEqual(BitI, j))
3208 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003209 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003210 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003211 return false;
3212 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003213 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003214 return false;
3215 }
Evan Cheng0038e592006-03-28 00:39:58 +00003216 }
Evan Cheng0038e592006-03-28 00:39:58 +00003217 return true;
3218}
3219
Nate Begeman9008ca62009-04-27 18:41:29 +00003220bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3221 SmallVector<int, 8> M;
3222 N->getMask(M);
3223 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003224}
3225
Evan Cheng4fcb9222006-03-28 02:43:26 +00003226/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3227/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003228static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003229 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003230 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003231 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003232 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003233
Nate Begeman9008ca62009-04-27 18:41:29 +00003234 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3235 int BitI = Mask[i];
3236 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003237 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003238 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003239 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003240 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003241 return false;
3242 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003243 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003244 return false;
3245 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003246 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003247 return true;
3248}
3249
Nate Begeman9008ca62009-04-27 18:41:29 +00003250bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3251 SmallVector<int, 8> M;
3252 N->getMask(M);
3253 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003254}
3255
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003256/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3257/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3258/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003259static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003261 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003262 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003263
Nate Begeman9008ca62009-04-27 18:41:29 +00003264 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3265 int BitI = Mask[i];
3266 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003267 if (!isUndefOrEqual(BitI, j))
3268 return false;
3269 if (!isUndefOrEqual(BitI1, j))
3270 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003271 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003272 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003273}
3274
Nate Begeman9008ca62009-04-27 18:41:29 +00003275bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3276 SmallVector<int, 8> M;
3277 N->getMask(M);
3278 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3279}
3280
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003281/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3282/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3283/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003284static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003285 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003286 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3287 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003288
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3290 int BitI = Mask[i];
3291 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003292 if (!isUndefOrEqual(BitI, j))
3293 return false;
3294 if (!isUndefOrEqual(BitI1, j))
3295 return false;
3296 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003297 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003298}
3299
Nate Begeman9008ca62009-04-27 18:41:29 +00003300bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3301 SmallVector<int, 8> M;
3302 N->getMask(M);
3303 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3304}
3305
Evan Cheng017dcc62006-04-21 01:05:10 +00003306/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3307/// specifies a shuffle of elements that is suitable for input to MOVSS,
3308/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003309static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003310 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003311 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003312
3313 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003314
Nate Begeman9008ca62009-04-27 18:41:29 +00003315 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003316 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003317
Nate Begeman9008ca62009-04-27 18:41:29 +00003318 for (int i = 1; i < NumElts; ++i)
3319 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003320 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003321
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003322 return true;
3323}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003324
Nate Begeman9008ca62009-04-27 18:41:29 +00003325bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3326 SmallVector<int, 8> M;
3327 N->getMask(M);
3328 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003329}
3330
Evan Cheng017dcc62006-04-21 01:05:10 +00003331/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3332/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003333/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003334static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003335 bool V2IsSplat = false, bool V2IsUndef = false) {
3336 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003337 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003338 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003339
Nate Begeman9008ca62009-04-27 18:41:29 +00003340 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003341 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003342
Nate Begeman9008ca62009-04-27 18:41:29 +00003343 for (int i = 1; i < NumOps; ++i)
3344 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3345 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3346 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003347 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003348
Evan Cheng39623da2006-04-20 08:58:49 +00003349 return true;
3350}
3351
Nate Begeman9008ca62009-04-27 18:41:29 +00003352static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003353 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003354 SmallVector<int, 8> M;
3355 N->getMask(M);
3356 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003357}
3358
Evan Chengd9539472006-04-14 21:59:03 +00003359/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3360/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003361bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3362 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003363 return false;
3364
3365 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003366 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 int Elt = N->getMaskElt(i);
3368 if (Elt >= 0 && Elt != 1)
3369 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003370 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003371
3372 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003373 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 int Elt = N->getMaskElt(i);
3375 if (Elt >= 0 && Elt != 3)
3376 return false;
3377 if (Elt == 3)
3378 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003379 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003380 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003381 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003382 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003383}
3384
3385/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3386/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003387bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3388 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003389 return false;
3390
3391 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 for (unsigned i = 0; i < 2; ++i)
3393 if (N->getMaskElt(i) > 0)
3394 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003395
3396 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003397 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 int Elt = N->getMaskElt(i);
3399 if (Elt >= 0 && Elt != 2)
3400 return false;
3401 if (Elt == 2)
3402 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003403 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003405 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003406}
3407
Evan Cheng0b457f02008-09-25 20:50:48 +00003408/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3409/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003410bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3411 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003412
Nate Begeman9008ca62009-04-27 18:41:29 +00003413 for (int i = 0; i < e; ++i)
3414 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003415 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003416 for (int i = 0; i < e; ++i)
3417 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003418 return false;
3419 return true;
3420}
3421
David Greenec38a03e2011-02-03 15:50:00 +00003422/// isVEXTRACTF128Index - Return true if the specified
3423/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3424/// suitable for input to VEXTRACTF128.
3425bool X86::isVEXTRACTF128Index(SDNode *N) {
3426 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3427 return false;
3428
3429 // The index should be aligned on a 128-bit boundary.
3430 uint64_t Index =
3431 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3432
3433 unsigned VL = N->getValueType(0).getVectorNumElements();
3434 unsigned VBits = N->getValueType(0).getSizeInBits();
3435 unsigned ElSize = VBits / VL;
3436 bool Result = (Index * ElSize) % 128 == 0;
3437
3438 return Result;
3439}
3440
David Greeneccacdc12011-02-04 16:08:29 +00003441/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3442/// operand specifies a subvector insert that is suitable for input to
3443/// VINSERTF128.
3444bool X86::isVINSERTF128Index(SDNode *N) {
3445 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3446 return false;
3447
3448 // The index should be aligned on a 128-bit boundary.
3449 uint64_t Index =
3450 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3451
3452 unsigned VL = N->getValueType(0).getVectorNumElements();
3453 unsigned VBits = N->getValueType(0).getSizeInBits();
3454 unsigned ElSize = VBits / VL;
3455 bool Result = (Index * ElSize) % 128 == 0;
3456
3457 return Result;
3458}
3459
Evan Cheng63d33002006-03-22 08:01:21 +00003460/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003461/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003462unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3464 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3465
Evan Chengb9df0ca2006-03-22 02:53:00 +00003466 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3467 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003468 for (int i = 0; i < NumOperands; ++i) {
3469 int Val = SVOp->getMaskElt(NumOperands-i-1);
3470 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003471 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003472 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003473 if (i != NumOperands - 1)
3474 Mask <<= Shift;
3475 }
Evan Cheng63d33002006-03-22 08:01:21 +00003476 return Mask;
3477}
3478
Evan Cheng506d3df2006-03-29 23:07:14 +00003479/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003480/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003481unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003483 unsigned Mask = 0;
3484 // 8 nodes, but we only care about the last 4.
3485 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 int Val = SVOp->getMaskElt(i);
3487 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003488 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003489 if (i != 4)
3490 Mask <<= 2;
3491 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003492 return Mask;
3493}
3494
3495/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003496/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003497unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003499 unsigned Mask = 0;
3500 // 8 nodes, but we only care about the first 4.
3501 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 int Val = SVOp->getMaskElt(i);
3503 if (Val >= 0)
3504 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003505 if (i != 0)
3506 Mask <<= 2;
3507 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003508 return Mask;
3509}
3510
Nate Begemana09008b2009-10-19 02:17:23 +00003511/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3512/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3513unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3514 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3515 EVT VVT = N->getValueType(0);
3516 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3517 int Val = 0;
3518
3519 unsigned i, e;
3520 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3521 Val = SVOp->getMaskElt(i);
3522 if (Val >= 0)
3523 break;
3524 }
3525 return (Val - i) * EltSize;
3526}
3527
David Greenec38a03e2011-02-03 15:50:00 +00003528/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3529/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3530/// instructions.
3531unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3532 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3533 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3534
3535 uint64_t Index =
3536 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3537
3538 EVT VecVT = N->getOperand(0).getValueType();
3539 EVT ElVT = VecVT.getVectorElementType();
3540
3541 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3542
3543 return Index / NumElemsPerChunk;
3544}
3545
David Greeneccacdc12011-02-04 16:08:29 +00003546/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3547/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3548/// instructions.
3549unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3550 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3551 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3552
3553 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003554 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003555
3556 EVT VecVT = N->getValueType(0);
3557 EVT ElVT = VecVT.getVectorElementType();
3558
3559 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3560
3561 return Index / NumElemsPerChunk;
3562}
3563
Evan Cheng37b73872009-07-30 08:33:02 +00003564/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3565/// constant +0.0.
3566bool X86::isZeroNode(SDValue Elt) {
3567 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003568 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003569 (isa<ConstantFPSDNode>(Elt) &&
3570 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3571}
3572
Nate Begeman9008ca62009-04-27 18:41:29 +00003573/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3574/// their permute mask.
3575static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3576 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003577 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003578 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003579 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003580
Nate Begeman5a5ca152009-04-29 05:20:52 +00003581 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003582 int idx = SVOp->getMaskElt(i);
3583 if (idx < 0)
3584 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003585 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003586 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003587 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003589 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003590 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3591 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003592}
3593
Evan Cheng779ccea2007-12-07 21:30:01 +00003594/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3595/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003596static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003597 unsigned NumElems = VT.getVectorNumElements();
3598 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003599 int idx = Mask[i];
3600 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003601 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003602 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003603 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003604 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003606 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003607}
3608
Evan Cheng533a0aa2006-04-19 20:35:22 +00003609/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3610/// match movhlps. The lower half elements should come from upper half of
3611/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003612/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003613static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3614 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003615 return false;
3616 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003618 return false;
3619 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003620 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003621 return false;
3622 return true;
3623}
3624
Evan Cheng5ced1d82006-04-06 23:23:56 +00003625/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003626/// is promoted to a vector. It also returns the LoadSDNode by reference if
3627/// required.
3628static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003629 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3630 return false;
3631 N = N->getOperand(0).getNode();
3632 if (!ISD::isNON_EXTLoad(N))
3633 return false;
3634 if (LD)
3635 *LD = cast<LoadSDNode>(N);
3636 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003637}
3638
Evan Cheng533a0aa2006-04-19 20:35:22 +00003639/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3640/// match movlp{s|d}. The lower half elements should come from lower half of
3641/// V1 (and in order), and the upper half elements should come from the upper
3642/// half of V2 (and in order). And since V1 will become the source of the
3643/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003644static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3645 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003646 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003647 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003648 // Is V2 is a vector load, don't do this transformation. We will try to use
3649 // load folding shufps op.
3650 if (ISD::isNON_EXTLoad(V2))
3651 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003652
Nate Begeman5a5ca152009-04-29 05:20:52 +00003653 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003654
Evan Cheng533a0aa2006-04-19 20:35:22 +00003655 if (NumElems != 2 && NumElems != 4)
3656 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003657 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003658 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003659 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003660 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003661 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003662 return false;
3663 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003664}
3665
Evan Cheng39623da2006-04-20 08:58:49 +00003666/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3667/// all the same.
3668static bool isSplatVector(SDNode *N) {
3669 if (N->getOpcode() != ISD::BUILD_VECTOR)
3670 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003671
Dan Gohman475871a2008-07-27 21:46:04 +00003672 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003673 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3674 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003675 return false;
3676 return true;
3677}
3678
Evan Cheng213d2cf2007-05-17 18:45:50 +00003679/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003680/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003681/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003682static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003683 SDValue V1 = N->getOperand(0);
3684 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003685 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3686 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003687 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003688 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003689 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003690 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3691 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003692 if (Opc != ISD::BUILD_VECTOR ||
3693 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003694 return false;
3695 } else if (Idx >= 0) {
3696 unsigned Opc = V1.getOpcode();
3697 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3698 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003699 if (Opc != ISD::BUILD_VECTOR ||
3700 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003701 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003702 }
3703 }
3704 return true;
3705}
3706
3707/// getZeroVector - Returns a vector of specified type with all zero elements.
3708///
Owen Andersone50ed302009-08-10 22:56:29 +00003709static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003710 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003711 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003712
Dale Johannesen0488fb62010-09-30 23:57:10 +00003713 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003714 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003715 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003716 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003717 if (HasSSE2) { // SSE2
3718 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3719 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3720 } else { // SSE1
3721 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3722 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3723 }
3724 } else if (VT.getSizeInBits() == 256) { // AVX
3725 // 256-bit logic and arithmetic instructions in AVX are
3726 // all floating-point, no support for integer ops. Default
3727 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003728 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003729 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3730 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003731 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003732 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003733}
3734
Chris Lattner8a594482007-11-25 00:24:49 +00003735/// getOnesVector - Returns a vector of specified type with all bits set.
3736///
Owen Andersone50ed302009-08-10 22:56:29 +00003737static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003738 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003739
Chris Lattner8a594482007-11-25 00:24:49 +00003740 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3741 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003742 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003743 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003744 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003745 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003746}
3747
3748
Evan Cheng39623da2006-04-20 08:58:49 +00003749/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3750/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003751static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003752 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003753 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003754
Evan Cheng39623da2006-04-20 08:58:49 +00003755 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003756 SmallVector<int, 8> MaskVec;
3757 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003758
Nate Begeman5a5ca152009-04-29 05:20:52 +00003759 for (unsigned i = 0; i != NumElems; ++i) {
3760 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003761 MaskVec[i] = NumElems;
3762 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003763 }
Evan Cheng39623da2006-04-20 08:58:49 +00003764 }
Evan Cheng39623da2006-04-20 08:58:49 +00003765 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003766 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3767 SVOp->getOperand(1), &MaskVec[0]);
3768 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003769}
3770
Evan Cheng017dcc62006-04-21 01:05:10 +00003771/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3772/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003773static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003774 SDValue V2) {
3775 unsigned NumElems = VT.getVectorNumElements();
3776 SmallVector<int, 8> Mask;
3777 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003778 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003779 Mask.push_back(i);
3780 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003781}
3782
Nate Begeman9008ca62009-04-27 18:41:29 +00003783/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003784static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003785 SDValue V2) {
3786 unsigned NumElems = VT.getVectorNumElements();
3787 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003788 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003789 Mask.push_back(i);
3790 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003791 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003792 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003793}
3794
Nate Begeman9008ca62009-04-27 18:41:29 +00003795/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003796static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003797 SDValue V2) {
3798 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003799 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003801 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003802 Mask.push_back(i + Half);
3803 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003804 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003805 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003806}
3807
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003808/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3809static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003810 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003811 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003812 DebugLoc dl = SV->getDebugLoc();
3813 SDValue V1 = SV->getOperand(0);
3814 int NumElems = VT.getVectorNumElements();
3815 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003816
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 // unpack elements to the correct location
3818 while (NumElems > 4) {
3819 if (EltNo < NumElems/2) {
3820 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3821 } else {
3822 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3823 EltNo -= NumElems/2;
3824 }
3825 NumElems >>= 1;
3826 }
Eric Christopherfd179292009-08-27 18:07:15 +00003827
Nate Begeman9008ca62009-04-27 18:41:29 +00003828 // Perform the splat.
3829 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003830 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003832 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003833}
3834
Evan Chengba05f722006-04-21 23:03:30 +00003835/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003836/// vector of zero or undef vector. This produces a shuffle where the low
3837/// element of V2 is swizzled into the zero/undef vector, landing at element
3838/// 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 +00003839static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003840 bool isZero, bool HasSSE2,
3841 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003842 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003843 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003844 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3845 unsigned NumElems = VT.getVectorNumElements();
3846 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003847 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 // If this is the insertion idx, put the low elt of V2 here.
3849 MaskVec.push_back(i == Idx ? NumElems : i);
3850 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003851}
3852
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003853/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3854/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003855SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3856 unsigned Depth) {
3857 if (Depth == 6)
3858 return SDValue(); // Limit search depth.
3859
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003860 SDValue V = SDValue(N, 0);
3861 EVT VT = V.getValueType();
3862 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003863
3864 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3865 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3866 Index = SV->getMaskElt(Index);
3867
3868 if (Index < 0)
3869 return DAG.getUNDEF(VT.getVectorElementType());
3870
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003871 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003872 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003873 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003874 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003875
3876 // Recurse into target specific vector shuffles to find scalars.
3877 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003878 int NumElems = VT.getVectorNumElements();
3879 SmallVector<unsigned, 16> ShuffleMask;
3880 SDValue ImmN;
3881
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003882 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003883 case X86ISD::SHUFPS:
3884 case X86ISD::SHUFPD:
3885 ImmN = N->getOperand(N->getNumOperands()-1);
3886 DecodeSHUFPSMask(NumElems,
3887 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3888 ShuffleMask);
3889 break;
3890 case X86ISD::PUNPCKHBW:
3891 case X86ISD::PUNPCKHWD:
3892 case X86ISD::PUNPCKHDQ:
3893 case X86ISD::PUNPCKHQDQ:
3894 DecodePUNPCKHMask(NumElems, ShuffleMask);
3895 break;
3896 case X86ISD::UNPCKHPS:
3897 case X86ISD::UNPCKHPD:
3898 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3899 break;
3900 case X86ISD::PUNPCKLBW:
3901 case X86ISD::PUNPCKLWD:
3902 case X86ISD::PUNPCKLDQ:
3903 case X86ISD::PUNPCKLQDQ:
3904 DecodePUNPCKLMask(NumElems, ShuffleMask);
3905 break;
3906 case X86ISD::UNPCKLPS:
3907 case X86ISD::UNPCKLPD:
3908 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3909 break;
3910 case X86ISD::MOVHLPS:
3911 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3912 break;
3913 case X86ISD::MOVLHPS:
3914 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3915 break;
3916 case X86ISD::PSHUFD:
3917 ImmN = N->getOperand(N->getNumOperands()-1);
3918 DecodePSHUFMask(NumElems,
3919 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3920 ShuffleMask);
3921 break;
3922 case X86ISD::PSHUFHW:
3923 ImmN = N->getOperand(N->getNumOperands()-1);
3924 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3925 ShuffleMask);
3926 break;
3927 case X86ISD::PSHUFLW:
3928 ImmN = N->getOperand(N->getNumOperands()-1);
3929 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3930 ShuffleMask);
3931 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003932 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003933 case X86ISD::MOVSD: {
3934 // The index 0 always comes from the first element of the second source,
3935 // this is why MOVSS and MOVSD are used in the first place. The other
3936 // elements come from the other positions of the first source vector.
3937 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003938 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3939 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003940 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003941 default:
3942 assert("not implemented for target shuffle node");
3943 return SDValue();
3944 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003945
3946 Index = ShuffleMask[Index];
3947 if (Index < 0)
3948 return DAG.getUNDEF(VT.getVectorElementType());
3949
3950 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3951 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3952 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003953 }
3954
3955 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003956 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003957 V = V.getOperand(0);
3958 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003959 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003960
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003961 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003962 return SDValue();
3963 }
3964
3965 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3966 return (Index == 0) ? V.getOperand(0)
3967 : DAG.getUNDEF(VT.getVectorElementType());
3968
3969 if (V.getOpcode() == ISD::BUILD_VECTOR)
3970 return V.getOperand(Index);
3971
3972 return SDValue();
3973}
3974
3975/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3976/// shuffle operation which come from a consecutively from a zero. The
3977/// search can start in two diferent directions, from left or right.
3978static
3979unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3980 bool ZerosFromLeft, SelectionDAG &DAG) {
3981 int i = 0;
3982
3983 while (i < NumElems) {
3984 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003985 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003986 if (!(Elt.getNode() &&
3987 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3988 break;
3989 ++i;
3990 }
3991
3992 return i;
3993}
3994
3995/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3996/// MaskE correspond consecutively to elements from one of the vector operands,
3997/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3998static
3999bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4000 int OpIdx, int NumElems, unsigned &OpNum) {
4001 bool SeenV1 = false;
4002 bool SeenV2 = false;
4003
4004 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4005 int Idx = SVOp->getMaskElt(i);
4006 // Ignore undef indicies
4007 if (Idx < 0)
4008 continue;
4009
4010 if (Idx < NumElems)
4011 SeenV1 = true;
4012 else
4013 SeenV2 = true;
4014
4015 // Only accept consecutive elements from the same vector
4016 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4017 return false;
4018 }
4019
4020 OpNum = SeenV1 ? 0 : 1;
4021 return true;
4022}
4023
4024/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4025/// logical left shift of a vector.
4026static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4027 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4028 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4029 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4030 false /* check zeros from right */, DAG);
4031 unsigned OpSrc;
4032
4033 if (!NumZeros)
4034 return false;
4035
4036 // Considering the elements in the mask that are not consecutive zeros,
4037 // check if they consecutively come from only one of the source vectors.
4038 //
4039 // V1 = {X, A, B, C} 0
4040 // \ \ \ /
4041 // vector_shuffle V1, V2 <1, 2, 3, X>
4042 //
4043 if (!isShuffleMaskConsecutive(SVOp,
4044 0, // Mask Start Index
4045 NumElems-NumZeros-1, // Mask End Index
4046 NumZeros, // Where to start looking in the src vector
4047 NumElems, // Number of elements in vector
4048 OpSrc)) // Which source operand ?
4049 return false;
4050
4051 isLeft = false;
4052 ShAmt = NumZeros;
4053 ShVal = SVOp->getOperand(OpSrc);
4054 return true;
4055}
4056
4057/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4058/// logical left shift of a vector.
4059static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4060 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4061 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4062 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4063 true /* check zeros from left */, DAG);
4064 unsigned OpSrc;
4065
4066 if (!NumZeros)
4067 return false;
4068
4069 // Considering the elements in the mask that are not consecutive zeros,
4070 // check if they consecutively come from only one of the source vectors.
4071 //
4072 // 0 { A, B, X, X } = V2
4073 // / \ / /
4074 // vector_shuffle V1, V2 <X, X, 4, 5>
4075 //
4076 if (!isShuffleMaskConsecutive(SVOp,
4077 NumZeros, // Mask Start Index
4078 NumElems-1, // Mask End Index
4079 0, // Where to start looking in the src vector
4080 NumElems, // Number of elements in vector
4081 OpSrc)) // Which source operand ?
4082 return false;
4083
4084 isLeft = true;
4085 ShAmt = NumZeros;
4086 ShVal = SVOp->getOperand(OpSrc);
4087 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004088}
4089
4090/// isVectorShift - Returns true if the shuffle can be implemented as a
4091/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004092static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004093 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004094 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4095 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4096 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004097
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004098 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004099}
4100
Evan Chengc78d3b42006-04-24 18:01:45 +00004101/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4102///
Dan Gohman475871a2008-07-27 21:46:04 +00004103static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004104 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004105 SelectionDAG &DAG,
4106 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004107 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004108 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004109
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004110 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004111 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004112 bool First = true;
4113 for (unsigned i = 0; i < 16; ++i) {
4114 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4115 if (ThisIsNonZero && First) {
4116 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004117 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004118 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004119 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004120 First = false;
4121 }
4122
4123 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004124 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004125 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4126 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004127 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004128 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004129 }
4130 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4132 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4133 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004134 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004136 } else
4137 ThisElt = LastElt;
4138
Gabor Greifba36cb52008-08-28 21:40:38 +00004139 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004140 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004141 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004142 }
4143 }
4144
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004145 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004146}
4147
Bill Wendlinga348c562007-03-22 18:42:45 +00004148/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004149///
Dan Gohman475871a2008-07-27 21:46:04 +00004150static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004151 unsigned NumNonZero, unsigned NumZero,
4152 SelectionDAG &DAG,
4153 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004154 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004155 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004156
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004157 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004158 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004159 bool First = true;
4160 for (unsigned i = 0; i < 8; ++i) {
4161 bool isNonZero = (NonZeros & (1 << i)) != 0;
4162 if (isNonZero) {
4163 if (First) {
4164 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004166 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004167 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004168 First = false;
4169 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004170 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004171 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004172 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004173 }
4174 }
4175
4176 return V;
4177}
4178
Evan Chengf26ffe92008-05-29 08:22:04 +00004179/// getVShift - Return a vector logical shift node.
4180///
Owen Andersone50ed302009-08-10 22:56:29 +00004181static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004182 unsigned NumBits, SelectionDAG &DAG,
4183 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004184 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004185 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004186 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4187 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004188 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004189 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004190}
4191
Dan Gohman475871a2008-07-27 21:46:04 +00004192SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004193X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004194 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004195
Evan Chengc3630942009-12-09 21:00:30 +00004196 // Check if the scalar load can be widened into a vector load. And if
4197 // the address is "base + cst" see if the cst can be "absorbed" into
4198 // the shuffle mask.
4199 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4200 SDValue Ptr = LD->getBasePtr();
4201 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4202 return SDValue();
4203 EVT PVT = LD->getValueType(0);
4204 if (PVT != MVT::i32 && PVT != MVT::f32)
4205 return SDValue();
4206
4207 int FI = -1;
4208 int64_t Offset = 0;
4209 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4210 FI = FINode->getIndex();
4211 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004212 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004213 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4214 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4215 Offset = Ptr.getConstantOperandVal(1);
4216 Ptr = Ptr.getOperand(0);
4217 } else {
4218 return SDValue();
4219 }
4220
4221 SDValue Chain = LD->getChain();
4222 // Make sure the stack object alignment is at least 16.
4223 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4224 if (DAG.InferPtrAlignment(Ptr) < 16) {
4225 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004226 // Can't change the alignment. FIXME: It's possible to compute
4227 // the exact stack offset and reference FI + adjust offset instead.
4228 // If someone *really* cares about this. That's the way to implement it.
4229 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004230 } else {
4231 MFI->setObjectAlignment(FI, 16);
4232 }
4233 }
4234
4235 // (Offset % 16) must be multiple of 4. Then address is then
4236 // Ptr + (Offset & ~15).
4237 if (Offset < 0)
4238 return SDValue();
4239 if ((Offset % 16) & 3)
4240 return SDValue();
4241 int64_t StartOffset = Offset & ~15;
4242 if (StartOffset)
4243 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4244 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4245
4246 int EltNo = (Offset - StartOffset) >> 2;
4247 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4248 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004249 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4250 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004251 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004252 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004253 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4254 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004255 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004256 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004257 }
4258
4259 return SDValue();
4260}
4261
Michael J. Spencerec38de22010-10-10 22:04:20 +00004262/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4263/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004264/// load which has the same value as a build_vector whose operands are 'elts'.
4265///
4266/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004267///
Nate Begeman1449f292010-03-24 22:19:06 +00004268/// FIXME: we'd also like to handle the case where the last elements are zero
4269/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4270/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004271static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004272 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004273 EVT EltVT = VT.getVectorElementType();
4274 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004275
Nate Begemanfdea31a2010-03-24 20:49:50 +00004276 LoadSDNode *LDBase = NULL;
4277 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004278
Nate Begeman1449f292010-03-24 22:19:06 +00004279 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004280 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004281 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004282 for (unsigned i = 0; i < NumElems; ++i) {
4283 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004284
Nate Begemanfdea31a2010-03-24 20:49:50 +00004285 if (!Elt.getNode() ||
4286 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4287 return SDValue();
4288 if (!LDBase) {
4289 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4290 return SDValue();
4291 LDBase = cast<LoadSDNode>(Elt.getNode());
4292 LastLoadedElt = i;
4293 continue;
4294 }
4295 if (Elt.getOpcode() == ISD::UNDEF)
4296 continue;
4297
4298 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4299 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4300 return SDValue();
4301 LastLoadedElt = i;
4302 }
Nate Begeman1449f292010-03-24 22:19:06 +00004303
4304 // If we have found an entire vector of loads and undefs, then return a large
4305 // load of the entire vector width starting at the base pointer. If we found
4306 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004307 if (LastLoadedElt == NumElems - 1) {
4308 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004309 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004310 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004311 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004312 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004313 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004314 LDBase->isVolatile(), LDBase->isNonTemporal(),
4315 LDBase->getAlignment());
4316 } else if (NumElems == 4 && LastLoadedElt == 1) {
4317 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4318 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004319 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4320 Ops, 2, MVT::i32,
4321 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004322 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004323 }
4324 return SDValue();
4325}
4326
Evan Chengc3630942009-12-09 21:00:30 +00004327SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004328X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004329 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004330
David Greenef125a292011-02-08 19:04:41 +00004331 EVT VT = Op.getValueType();
4332 EVT ExtVT = VT.getVectorElementType();
4333
4334 unsigned NumElems = Op.getNumOperands();
4335
4336 // For AVX-length vectors, build the individual 128-bit pieces and
4337 // use shuffles to put them in place.
4338 if (VT.getSizeInBits() > 256 &&
4339 Subtarget->hasAVX() &&
4340 !Disable256Bit &&
4341 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4342 SmallVector<SDValue, 8> V;
4343 V.resize(NumElems);
4344 for (unsigned i = 0; i < NumElems; ++i) {
4345 V[i] = Op.getOperand(i);
4346 }
4347
4348 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4349
4350 // Build the lower subvector.
4351 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4352 // Build the upper subvector.
4353 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4354 NumElems/2);
4355
4356 return ConcatVectors(Lower, Upper, DAG);
4357 }
4358
Chris Lattner6e80e442010-08-28 17:15:43 +00004359 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4360 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004361 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4362 // is present, so AllOnes is ignored.
4363 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4364 (Op.getValueType().getSizeInBits() != 256 &&
4365 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004366 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004367 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4368 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004369 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004370 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004371
Gabor Greifba36cb52008-08-28 21:40:38 +00004372 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004373 return getOnesVector(Op.getValueType(), DAG, dl);
4374 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004375 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004376
Owen Andersone50ed302009-08-10 22:56:29 +00004377 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004378
Evan Cheng0db9fe62006-04-25 20:13:52 +00004379 unsigned NumZero = 0;
4380 unsigned NumNonZero = 0;
4381 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004382 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004383 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004384 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004385 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004386 if (Elt.getOpcode() == ISD::UNDEF)
4387 continue;
4388 Values.insert(Elt);
4389 if (Elt.getOpcode() != ISD::Constant &&
4390 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004391 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004392 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004393 NumZero++;
4394 else {
4395 NonZeros |= (1 << i);
4396 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004397 }
4398 }
4399
Chris Lattner97a2a562010-08-26 05:24:29 +00004400 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4401 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004402 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004403
Chris Lattner67f453a2008-03-09 05:42:06 +00004404 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004405 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004406 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004407 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004408
Chris Lattner62098042008-03-09 01:05:04 +00004409 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4410 // the value are obviously zero, truncate the value to i32 and do the
4411 // insertion that way. Only do this if the value is non-constant or if the
4412 // value is a constant being inserted into element 0. It is cheaper to do
4413 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004414 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004415 (!IsAllConstants || Idx == 0)) {
4416 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004417 // Handle SSE only.
4418 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4419 EVT VecVT = MVT::v4i32;
4420 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004421
Chris Lattner62098042008-03-09 01:05:04 +00004422 // Truncate the value (which may itself be a constant) to i32, and
4423 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004425 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004426 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4427 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004428
Chris Lattner62098042008-03-09 01:05:04 +00004429 // Now we have our 32-bit value zero extended in the low element of
4430 // a vector. If Idx != 0, swizzle it into place.
4431 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 SmallVector<int, 4> Mask;
4433 Mask.push_back(Idx);
4434 for (unsigned i = 1; i != VecElts; ++i)
4435 Mask.push_back(i);
4436 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004437 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004438 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004439 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004440 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004441 }
4442 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004443
Chris Lattner19f79692008-03-08 22:59:52 +00004444 // If we have a constant or non-constant insertion into the low element of
4445 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4446 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004447 // depending on what the source datatype is.
4448 if (Idx == 0) {
4449 if (NumZero == 0) {
4450 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004451 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4452 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004453 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4454 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4455 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4456 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004457 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4458 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004459 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4460 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004461 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4462 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4463 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004464 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004465 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004466 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004467
4468 // Is it a vector logical left shift?
4469 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004470 X86::isZeroNode(Op.getOperand(0)) &&
4471 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004472 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004473 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004474 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004475 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004476 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004477 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004478
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004479 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004480 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481
Chris Lattner19f79692008-03-08 22:59:52 +00004482 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4483 // is a non-constant being inserted into an element other than the low one,
4484 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4485 // movd/movss) to move this into the low element, then shuffle it into
4486 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004487 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004488 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004489
Evan Cheng0db9fe62006-04-25 20:13:52 +00004490 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004491 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4492 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004494 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004495 MaskVec.push_back(i == Idx ? 0 : 1);
4496 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004497 }
4498 }
4499
Chris Lattner67f453a2008-03-09 05:42:06 +00004500 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004501 if (Values.size() == 1) {
4502 if (EVTBits == 32) {
4503 // Instead of a shuffle like this:
4504 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4505 // Check if it's possible to issue this instead.
4506 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4507 unsigned Idx = CountTrailingZeros_32(NonZeros);
4508 SDValue Item = Op.getOperand(Idx);
4509 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4510 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4511 }
Dan Gohman475871a2008-07-27 21:46:04 +00004512 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004513 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004514
Dan Gohmana3941172007-07-24 22:55:08 +00004515 // A vector full of immediates; various special cases are already
4516 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004517 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004518 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004519
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004520 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004521 if (EVTBits == 64) {
4522 if (NumNonZero == 1) {
4523 // One half is zero or undef.
4524 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004525 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004526 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004527 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4528 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004529 }
Dan Gohman475871a2008-07-27 21:46:04 +00004530 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004531 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004532
4533 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004534 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004535 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004536 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004537 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004538 }
4539
Bill Wendling826f36f2007-03-28 00:57:11 +00004540 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004541 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004542 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004543 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004544 }
4545
4546 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004547 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004548 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004549 if (NumElems == 4 && NumZero > 0) {
4550 for (unsigned i = 0; i < 4; ++i) {
4551 bool isZero = !(NonZeros & (1 << i));
4552 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004553 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004554 else
Dale Johannesenace16102009-02-03 19:33:06 +00004555 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004556 }
4557
4558 for (unsigned i = 0; i < 2; ++i) {
4559 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4560 default: break;
4561 case 0:
4562 V[i] = V[i*2]; // Must be a zero vector.
4563 break;
4564 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004565 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004566 break;
4567 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004568 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004569 break;
4570 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004571 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004572 break;
4573 }
4574 }
4575
Nate Begeman9008ca62009-04-27 18:41:29 +00004576 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004577 bool Reverse = (NonZeros & 0x3) == 2;
4578 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004579 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004580 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4581 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004582 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4583 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004584 }
4585
Nate Begemanfdea31a2010-03-24 20:49:50 +00004586 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4587 // Check for a build vector of consecutive loads.
4588 for (unsigned i = 0; i < NumElems; ++i)
4589 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004590
Nate Begemanfdea31a2010-03-24 20:49:50 +00004591 // Check for elements which are consecutive loads.
4592 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4593 if (LD.getNode())
4594 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004595
4596 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004597 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004598 SDValue Result;
4599 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4600 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4601 else
4602 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004603
Chris Lattner24faf612010-08-28 17:59:08 +00004604 for (unsigned i = 1; i < NumElems; ++i) {
4605 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4606 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004607 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004608 }
4609 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004610 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004611
Chris Lattner6e80e442010-08-28 17:15:43 +00004612 // Otherwise, expand into a number of unpckl*, start by extending each of
4613 // our (non-undef) elements to the full vector width with the element in the
4614 // bottom slot of the vector (which generates no code for SSE).
4615 for (unsigned i = 0; i < NumElems; ++i) {
4616 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4617 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4618 else
4619 V[i] = DAG.getUNDEF(VT);
4620 }
4621
4622 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004623 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4624 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4625 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004626 unsigned EltStride = NumElems >> 1;
4627 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004628 for (unsigned i = 0; i < EltStride; ++i) {
4629 // If V[i+EltStride] is undef and this is the first round of mixing,
4630 // then it is safe to just drop this shuffle: V[i] is already in the
4631 // right place, the one element (since it's the first round) being
4632 // inserted as undef can be dropped. This isn't safe for successive
4633 // rounds because they will permute elements within both vectors.
4634 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4635 EltStride == NumElems/2)
4636 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004637
Chris Lattner6e80e442010-08-28 17:15:43 +00004638 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004639 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004640 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004641 }
4642 return V[0];
4643 }
Dan Gohman475871a2008-07-27 21:46:04 +00004644 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004645}
4646
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004647SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004648X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004649 // We support concatenate two MMX registers and place them in a MMX
4650 // register. This is better than doing a stack convert.
4651 DebugLoc dl = Op.getDebugLoc();
4652 EVT ResVT = Op.getValueType();
4653 assert(Op.getNumOperands() == 2);
4654 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4655 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4656 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004657 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004658 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4659 InVec = Op.getOperand(1);
4660 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4661 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004662 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004663 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4664 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4665 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004666 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004667 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4668 Mask[0] = 0; Mask[1] = 2;
4669 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4670 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004671 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004672}
4673
Nate Begemanb9a47b82009-02-23 08:49:38 +00004674// v8i16 shuffles - Prefer shuffles in the following order:
4675// 1. [all] pshuflw, pshufhw, optional move
4676// 2. [ssse3] 1 x pshufb
4677// 3. [ssse3] 2 x pshufb + 1 x por
4678// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004679SDValue
4680X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4681 SelectionDAG &DAG) const {
4682 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004683 SDValue V1 = SVOp->getOperand(0);
4684 SDValue V2 = SVOp->getOperand(1);
4685 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004686 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004687
Nate Begemanb9a47b82009-02-23 08:49:38 +00004688 // Determine if more than 1 of the words in each of the low and high quadwords
4689 // of the result come from the same quadword of one of the two inputs. Undef
4690 // mask values count as coming from any quadword, for better codegen.
4691 SmallVector<unsigned, 4> LoQuad(4);
4692 SmallVector<unsigned, 4> HiQuad(4);
4693 BitVector InputQuads(4);
4694 for (unsigned i = 0; i < 8; ++i) {
4695 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004696 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004697 MaskVals.push_back(EltIdx);
4698 if (EltIdx < 0) {
4699 ++Quad[0];
4700 ++Quad[1];
4701 ++Quad[2];
4702 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004703 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004704 }
4705 ++Quad[EltIdx / 4];
4706 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004707 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004708
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004710 unsigned MaxQuad = 1;
4711 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 if (LoQuad[i] > MaxQuad) {
4713 BestLoQuad = i;
4714 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004715 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004716 }
4717
Nate Begemanb9a47b82009-02-23 08:49:38 +00004718 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004719 MaxQuad = 1;
4720 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004721 if (HiQuad[i] > MaxQuad) {
4722 BestHiQuad = i;
4723 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004724 }
4725 }
4726
Nate Begemanb9a47b82009-02-23 08:49:38 +00004727 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004728 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 // single pshufb instruction is necessary. If There are more than 2 input
4730 // quads, disable the next transformation since it does not help SSSE3.
4731 bool V1Used = InputQuads[0] || InputQuads[1];
4732 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004733 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004734 if (InputQuads.count() == 2 && V1Used && V2Used) {
4735 BestLoQuad = InputQuads.find_first();
4736 BestHiQuad = InputQuads.find_next(BestLoQuad);
4737 }
4738 if (InputQuads.count() > 2) {
4739 BestLoQuad = -1;
4740 BestHiQuad = -1;
4741 }
4742 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004743
Nate Begemanb9a47b82009-02-23 08:49:38 +00004744 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4745 // the shuffle mask. If a quad is scored as -1, that means that it contains
4746 // words from all 4 input quadwords.
4747 SDValue NewV;
4748 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004749 SmallVector<int, 8> MaskV;
4750 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4751 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004752 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004753 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4754 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4755 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004756
Nate Begemanb9a47b82009-02-23 08:49:38 +00004757 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4758 // source words for the shuffle, to aid later transformations.
4759 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004760 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004761 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004762 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004763 if (idx != (int)i)
4764 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004765 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004766 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 AllWordsInNewV = false;
4768 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004769 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004770
Nate Begemanb9a47b82009-02-23 08:49:38 +00004771 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4772 if (AllWordsInNewV) {
4773 for (int i = 0; i != 8; ++i) {
4774 int idx = MaskVals[i];
4775 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004776 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004777 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004778 if ((idx != i) && idx < 4)
4779 pshufhw = false;
4780 if ((idx != i) && idx > 3)
4781 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004782 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004783 V1 = NewV;
4784 V2Used = false;
4785 BestLoQuad = 0;
4786 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004787 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004788
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4790 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004791 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004792 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4793 unsigned TargetMask = 0;
4794 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004795 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004796 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4797 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4798 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004799 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004800 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004801 }
Eric Christopherfd179292009-08-27 18:07:15 +00004802
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 // If we have SSSE3, and all words of the result are from 1 input vector,
4804 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4805 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004806 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004807 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004808
Nate Begemanb9a47b82009-02-23 08:49:38 +00004809 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004810 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004811 // mask, and elements that come from V1 in the V2 mask, so that the two
4812 // results can be OR'd together.
4813 bool TwoInputs = V1Used && V2Used;
4814 for (unsigned i = 0; i != 8; ++i) {
4815 int EltIdx = MaskVals[i] * 2;
4816 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4818 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 continue;
4820 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4822 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004823 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004824 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004825 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004826 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004828 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004829 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004830
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 // Calculate the shuffle mask for the second input, shuffle it, and
4832 // OR it with the first shuffled input.
4833 pshufbMask.clear();
4834 for (unsigned i = 0; i != 8; ++i) {
4835 int EltIdx = MaskVals[i] * 2;
4836 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4838 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004839 continue;
4840 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004841 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4842 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004843 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004844 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004845 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004846 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 MVT::v16i8, &pshufbMask[0], 16));
4848 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004849 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004850 }
4851
4852 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4853 // and update MaskVals with new element order.
4854 BitVector InOrder(8);
4855 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004856 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004857 for (int i = 0; i != 4; ++i) {
4858 int idx = MaskVals[i];
4859 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004860 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004861 InOrder.set(i);
4862 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004863 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004864 InOrder.set(i);
4865 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004866 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004867 }
4868 }
4869 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004870 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004872 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004873
4874 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4875 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4876 NewV.getOperand(0),
4877 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4878 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 }
Eric Christopherfd179292009-08-27 18:07:15 +00004880
Nate Begemanb9a47b82009-02-23 08:49:38 +00004881 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4882 // and update MaskVals with the new element order.
4883 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004884 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004885 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004886 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004887 for (unsigned i = 4; i != 8; ++i) {
4888 int idx = MaskVals[i];
4889 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004890 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004891 InOrder.set(i);
4892 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004893 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004894 InOrder.set(i);
4895 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004896 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004897 }
4898 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004899 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004900 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004901
4902 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4903 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4904 NewV.getOperand(0),
4905 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4906 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004907 }
Eric Christopherfd179292009-08-27 18:07:15 +00004908
Nate Begemanb9a47b82009-02-23 08:49:38 +00004909 // In case BestHi & BestLo were both -1, which means each quadword has a word
4910 // from each of the four input quadwords, calculate the InOrder bitvector now
4911 // before falling through to the insert/extract cleanup.
4912 if (BestLoQuad == -1 && BestHiQuad == -1) {
4913 NewV = V1;
4914 for (int i = 0; i != 8; ++i)
4915 if (MaskVals[i] < 0 || MaskVals[i] == i)
4916 InOrder.set(i);
4917 }
Eric Christopherfd179292009-08-27 18:07:15 +00004918
Nate Begemanb9a47b82009-02-23 08:49:38 +00004919 // The other elements are put in the right place using pextrw and pinsrw.
4920 for (unsigned i = 0; i != 8; ++i) {
4921 if (InOrder[i])
4922 continue;
4923 int EltIdx = MaskVals[i];
4924 if (EltIdx < 0)
4925 continue;
4926 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004927 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004928 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004929 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004930 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004931 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004932 DAG.getIntPtrConstant(i));
4933 }
4934 return NewV;
4935}
4936
4937// v16i8 shuffles - Prefer shuffles in the following order:
4938// 1. [ssse3] 1 x pshufb
4939// 2. [ssse3] 2 x pshufb + 1 x por
4940// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4941static
Nate Begeman9008ca62009-04-27 18:41:29 +00004942SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004943 SelectionDAG &DAG,
4944 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004945 SDValue V1 = SVOp->getOperand(0);
4946 SDValue V2 = SVOp->getOperand(1);
4947 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004948 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004949 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004950
Nate Begemanb9a47b82009-02-23 08:49:38 +00004951 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004952 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004953 // present, fall back to case 3.
4954 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4955 bool V1Only = true;
4956 bool V2Only = true;
4957 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004958 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004959 if (EltIdx < 0)
4960 continue;
4961 if (EltIdx < 16)
4962 V2Only = false;
4963 else
4964 V1Only = false;
4965 }
Eric Christopherfd179292009-08-27 18:07:15 +00004966
Nate Begemanb9a47b82009-02-23 08:49:38 +00004967 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4968 if (TLI.getSubtarget()->hasSSSE3()) {
4969 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004970
Nate Begemanb9a47b82009-02-23 08:49:38 +00004971 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004972 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004973 //
4974 // Otherwise, we have elements from both input vectors, and must zero out
4975 // elements that come from V2 in the first mask, and V1 in the second mask
4976 // so that we can OR them together.
4977 bool TwoInputs = !(V1Only || V2Only);
4978 for (unsigned i = 0; i != 16; ++i) {
4979 int EltIdx = MaskVals[i];
4980 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004981 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004982 continue;
4983 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004984 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004985 }
4986 // If all the elements are from V2, assign it to V1 and return after
4987 // building the first pshufb.
4988 if (V2Only)
4989 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004990 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004991 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004992 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004993 if (!TwoInputs)
4994 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004995
Nate Begemanb9a47b82009-02-23 08:49:38 +00004996 // Calculate the shuffle mask for the second input, shuffle it, and
4997 // OR it with the first shuffled input.
4998 pshufbMask.clear();
4999 for (unsigned i = 0; i != 16; ++i) {
5000 int EltIdx = MaskVals[i];
5001 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005002 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005003 continue;
5004 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005005 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005006 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005007 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005008 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005009 MVT::v16i8, &pshufbMask[0], 16));
5010 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005011 }
Eric Christopherfd179292009-08-27 18:07:15 +00005012
Nate Begemanb9a47b82009-02-23 08:49:38 +00005013 // No SSSE3 - Calculate in place words and then fix all out of place words
5014 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5015 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005016 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5017 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005018 SDValue NewV = V2Only ? V2 : V1;
5019 for (int i = 0; i != 8; ++i) {
5020 int Elt0 = MaskVals[i*2];
5021 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005022
Nate Begemanb9a47b82009-02-23 08:49:38 +00005023 // This word of the result is all undef, skip it.
5024 if (Elt0 < 0 && Elt1 < 0)
5025 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005026
Nate Begemanb9a47b82009-02-23 08:49:38 +00005027 // This word of the result is already in the correct place, skip it.
5028 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5029 continue;
5030 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5031 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005032
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5034 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5035 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005036
5037 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5038 // using a single extract together, load it and store it.
5039 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005040 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005041 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005042 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005043 DAG.getIntPtrConstant(i));
5044 continue;
5045 }
5046
Nate Begemanb9a47b82009-02-23 08:49:38 +00005047 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005048 // source byte is not also odd, shift the extracted word left 8 bits
5049 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005050 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005052 DAG.getIntPtrConstant(Elt1 / 2));
5053 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005054 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005055 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005056 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005057 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5058 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005059 }
5060 // If Elt0 is defined, extract it from the appropriate source. If the
5061 // source byte is not also even, shift the extracted word right 8 bits. If
5062 // Elt1 was also defined, OR the extracted values together before
5063 // inserting them in the result.
5064 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005066 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5067 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005068 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005069 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005070 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005071 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5072 DAG.getConstant(0x00FF, MVT::i16));
5073 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005074 : InsElt0;
5075 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005076 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005077 DAG.getIntPtrConstant(i));
5078 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005079 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005080}
5081
Evan Cheng7a831ce2007-12-15 03:00:47 +00005082/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005083/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005084/// done when every pair / quad of shuffle mask elements point to elements in
5085/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005086/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005087static
Nate Begeman9008ca62009-04-27 18:41:29 +00005088SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005089 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005090 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005091 SDValue V1 = SVOp->getOperand(0);
5092 SDValue V2 = SVOp->getOperand(1);
5093 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005094 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005095 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005097 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005098 case MVT::v4f32: NewVT = MVT::v2f64; break;
5099 case MVT::v4i32: NewVT = MVT::v2i64; break;
5100 case MVT::v8i16: NewVT = MVT::v4i32; break;
5101 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005102 }
5103
Nate Begeman9008ca62009-04-27 18:41:29 +00005104 int Scale = NumElems / NewWidth;
5105 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005106 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005107 int StartIdx = -1;
5108 for (int j = 0; j < Scale; ++j) {
5109 int EltIdx = SVOp->getMaskElt(i+j);
5110 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005111 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005112 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005113 StartIdx = EltIdx - (EltIdx % Scale);
5114 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005115 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005116 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005117 if (StartIdx == -1)
5118 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005119 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005120 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005121 }
5122
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005123 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5124 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005125 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005126}
5127
Evan Chengd880b972008-05-09 21:53:03 +00005128/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005129///
Owen Andersone50ed302009-08-10 22:56:29 +00005130static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005131 SDValue SrcOp, SelectionDAG &DAG,
5132 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005133 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005134 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005135 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005136 LD = dyn_cast<LoadSDNode>(SrcOp);
5137 if (!LD) {
5138 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5139 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005140 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005141 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005142 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005143 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005144 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005145 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005146 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005147 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005148 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5149 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5150 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005151 SrcOp.getOperand(0)
5152 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005153 }
5154 }
5155 }
5156
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005157 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005158 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005159 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005160 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005161}
5162
Evan Chengace3c172008-07-22 21:13:36 +00005163/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5164/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005165static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005166LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5167 SDValue V1 = SVOp->getOperand(0);
5168 SDValue V2 = SVOp->getOperand(1);
5169 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005170 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005171
Evan Chengace3c172008-07-22 21:13:36 +00005172 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005173 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005174 SmallVector<int, 8> Mask1(4U, -1);
5175 SmallVector<int, 8> PermMask;
5176 SVOp->getMask(PermMask);
5177
Evan Chengace3c172008-07-22 21:13:36 +00005178 unsigned NumHi = 0;
5179 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005180 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005181 int Idx = PermMask[i];
5182 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005183 Locs[i] = std::make_pair(-1, -1);
5184 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005185 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5186 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005187 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005188 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005189 NumLo++;
5190 } else {
5191 Locs[i] = std::make_pair(1, NumHi);
5192 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005193 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005194 NumHi++;
5195 }
5196 }
5197 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005198
Evan Chengace3c172008-07-22 21:13:36 +00005199 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005200 // If no more than two elements come from either vector. This can be
5201 // implemented with two shuffles. First shuffle gather the elements.
5202 // The second shuffle, which takes the first shuffle as both of its
5203 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005204 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005205
Nate Begeman9008ca62009-04-27 18:41:29 +00005206 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005207
Evan Chengace3c172008-07-22 21:13:36 +00005208 for (unsigned i = 0; i != 4; ++i) {
5209 if (Locs[i].first == -1)
5210 continue;
5211 else {
5212 unsigned Idx = (i < 2) ? 0 : 4;
5213 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005214 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005215 }
5216 }
5217
Nate Begeman9008ca62009-04-27 18:41:29 +00005218 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005219 } else if (NumLo == 3 || NumHi == 3) {
5220 // Otherwise, we must have three elements from one vector, call it X, and
5221 // one element from the other, call it Y. First, use a shufps to build an
5222 // intermediate vector with the one element from Y and the element from X
5223 // that will be in the same half in the final destination (the indexes don't
5224 // matter). Then, use a shufps to build the final vector, taking the half
5225 // containing the element from Y from the intermediate, and the other half
5226 // from X.
5227 if (NumHi == 3) {
5228 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005229 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005230 std::swap(V1, V2);
5231 }
5232
5233 // Find the element from V2.
5234 unsigned HiIndex;
5235 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005236 int Val = PermMask[HiIndex];
5237 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005238 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005239 if (Val >= 4)
5240 break;
5241 }
5242
Nate Begeman9008ca62009-04-27 18:41:29 +00005243 Mask1[0] = PermMask[HiIndex];
5244 Mask1[1] = -1;
5245 Mask1[2] = PermMask[HiIndex^1];
5246 Mask1[3] = -1;
5247 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005248
5249 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 Mask1[0] = PermMask[0];
5251 Mask1[1] = PermMask[1];
5252 Mask1[2] = HiIndex & 1 ? 6 : 4;
5253 Mask1[3] = HiIndex & 1 ? 4 : 6;
5254 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005255 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 Mask1[0] = HiIndex & 1 ? 2 : 0;
5257 Mask1[1] = HiIndex & 1 ? 0 : 2;
5258 Mask1[2] = PermMask[2];
5259 Mask1[3] = PermMask[3];
5260 if (Mask1[2] >= 0)
5261 Mask1[2] += 4;
5262 if (Mask1[3] >= 0)
5263 Mask1[3] += 4;
5264 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005265 }
Evan Chengace3c172008-07-22 21:13:36 +00005266 }
5267
5268 // Break it into (shuffle shuffle_hi, shuffle_lo).
5269 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005270 SmallVector<int,8> LoMask(4U, -1);
5271 SmallVector<int,8> HiMask(4U, -1);
5272
5273 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005274 unsigned MaskIdx = 0;
5275 unsigned LoIdx = 0;
5276 unsigned HiIdx = 2;
5277 for (unsigned i = 0; i != 4; ++i) {
5278 if (i == 2) {
5279 MaskPtr = &HiMask;
5280 MaskIdx = 1;
5281 LoIdx = 0;
5282 HiIdx = 2;
5283 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 int Idx = PermMask[i];
5285 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005286 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005287 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005288 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005289 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005290 LoIdx++;
5291 } else {
5292 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005293 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005294 HiIdx++;
5295 }
5296 }
5297
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5299 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5300 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005301 for (unsigned i = 0; i != 4; ++i) {
5302 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005303 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005304 } else {
5305 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005306 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005307 }
5308 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005310}
5311
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005312static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005313 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005314 V = V.getOperand(0);
5315 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5316 V = V.getOperand(0);
5317 if (MayFoldLoad(V))
5318 return true;
5319 return false;
5320}
5321
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005322// FIXME: the version above should always be used. Since there's
5323// a bug where several vector shuffles can't be folded because the
5324// DAG is not updated during lowering and a node claims to have two
5325// uses while it only has one, use this version, and let isel match
5326// another instruction if the load really happens to have more than
5327// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005328// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005329static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005330 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005331 V = V.getOperand(0);
5332 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5333 V = V.getOperand(0);
5334 if (ISD::isNormalLoad(V.getNode()))
5335 return true;
5336 return false;
5337}
5338
5339/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5340/// a vector extract, and if both can be later optimized into a single load.
5341/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5342/// here because otherwise a target specific shuffle node is going to be
5343/// emitted for this shuffle, and the optimization not done.
5344/// FIXME: This is probably not the best approach, but fix the problem
5345/// until the right path is decided.
5346static
5347bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5348 const TargetLowering &TLI) {
5349 EVT VT = V.getValueType();
5350 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5351
5352 // Be sure that the vector shuffle is present in a pattern like this:
5353 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5354 if (!V.hasOneUse())
5355 return false;
5356
5357 SDNode *N = *V.getNode()->use_begin();
5358 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5359 return false;
5360
5361 SDValue EltNo = N->getOperand(1);
5362 if (!isa<ConstantSDNode>(EltNo))
5363 return false;
5364
5365 // If the bit convert changed the number of elements, it is unsafe
5366 // to examine the mask.
5367 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005368 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005369 EVT SrcVT = V.getOperand(0).getValueType();
5370 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5371 return false;
5372 V = V.getOperand(0);
5373 HasShuffleIntoBitcast = true;
5374 }
5375
5376 // Select the input vector, guarding against out of range extract vector.
5377 unsigned NumElems = VT.getVectorNumElements();
5378 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5379 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5380 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5381
5382 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005383 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005384 V = V.getOperand(0);
5385
5386 if (ISD::isNormalLoad(V.getNode())) {
5387 // Is the original load suitable?
5388 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5389
5390 // FIXME: avoid the multi-use bug that is preventing lots of
5391 // of foldings to be detected, this is still wrong of course, but
5392 // give the temporary desired behavior, and if it happens that
5393 // the load has real more uses, during isel it will not fold, and
5394 // will generate poor code.
5395 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5396 return false;
5397
5398 if (!HasShuffleIntoBitcast)
5399 return true;
5400
5401 // If there's a bitcast before the shuffle, check if the load type and
5402 // alignment is valid.
5403 unsigned Align = LN0->getAlignment();
5404 unsigned NewAlign =
5405 TLI.getTargetData()->getABITypeAlignment(
5406 VT.getTypeForEVT(*DAG.getContext()));
5407
5408 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5409 return false;
5410 }
5411
5412 return true;
5413}
5414
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005415static
Evan Cheng835580f2010-10-07 20:50:20 +00005416SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5417 EVT VT = Op.getValueType();
5418
5419 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005420 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5421 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005422 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5423 V1, DAG));
5424}
5425
5426static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005427SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5428 bool HasSSE2) {
5429 SDValue V1 = Op.getOperand(0);
5430 SDValue V2 = Op.getOperand(1);
5431 EVT VT = Op.getValueType();
5432
5433 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5434
5435 if (HasSSE2 && VT == MVT::v2f64)
5436 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5437
5438 // v4f32 or v4i32
5439 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5440}
5441
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005442static
5443SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5444 SDValue V1 = Op.getOperand(0);
5445 SDValue V2 = Op.getOperand(1);
5446 EVT VT = Op.getValueType();
5447
5448 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5449 "unsupported shuffle type");
5450
5451 if (V2.getOpcode() == ISD::UNDEF)
5452 V2 = V1;
5453
5454 // v4i32 or v4f32
5455 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5456}
5457
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005458static
5459SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5460 SDValue V1 = Op.getOperand(0);
5461 SDValue V2 = Op.getOperand(1);
5462 EVT VT = Op.getValueType();
5463 unsigned NumElems = VT.getVectorNumElements();
5464
5465 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5466 // operand of these instructions is only memory, so check if there's a
5467 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5468 // same masks.
5469 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005470
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005471 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005472 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005473 CanFoldLoad = true;
5474
5475 // When V1 is a load, it can be folded later into a store in isel, example:
5476 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5477 // turns into:
5478 // (MOVLPSmr addr:$src1, VR128:$src2)
5479 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005480 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005481 CanFoldLoad = true;
5482
Eric Christopher893a8822011-02-20 05:04:42 +00005483 // Both of them can't be memory operations though.
5484 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5485 CanFoldLoad = false;
5486
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005487 if (CanFoldLoad) {
5488 if (HasSSE2 && NumElems == 2)
5489 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5490
5491 if (NumElems == 4)
5492 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5493 }
5494
5495 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5496 // movl and movlp will both match v2i64, but v2i64 is never matched by
5497 // movl earlier because we make it strict to avoid messing with the movlp load
5498 // folding logic (see the code above getMOVLP call). Match it here then,
5499 // this is horrible, but will stay like this until we move all shuffle
5500 // matching to x86 specific nodes. Note that for the 1st condition all
5501 // types are matched with movsd.
5502 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5503 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5504 else if (HasSSE2)
5505 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5506
5507
5508 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5509
5510 // Invert the operand order and use SHUFPS to match it.
5511 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5512 X86::getShuffleSHUFImmediate(SVOp), DAG);
5513}
5514
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005515static inline unsigned getUNPCKLOpcode(EVT VT) {
5516 switch(VT.getSimpleVT().SimpleTy) {
5517 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5518 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5519 case MVT::v4f32: return X86ISD::UNPCKLPS;
5520 case MVT::v2f64: return X86ISD::UNPCKLPD;
5521 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5522 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5523 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005524 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005525 }
5526 return 0;
5527}
5528
5529static inline unsigned getUNPCKHOpcode(EVT VT) {
5530 switch(VT.getSimpleVT().SimpleTy) {
5531 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5532 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5533 case MVT::v4f32: return X86ISD::UNPCKHPS;
5534 case MVT::v2f64: return X86ISD::UNPCKHPD;
5535 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5536 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5537 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005538 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005539 }
5540 return 0;
5541}
5542
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005543static
5544SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005545 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005546 const X86Subtarget *Subtarget) {
5547 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5548 EVT VT = Op.getValueType();
5549 DebugLoc dl = Op.getDebugLoc();
5550 SDValue V1 = Op.getOperand(0);
5551 SDValue V2 = Op.getOperand(1);
5552
5553 if (isZeroShuffle(SVOp))
5554 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5555
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005556 // Handle splat operations
5557 if (SVOp->isSplat()) {
5558 // Special case, this is the only place now where it's
5559 // allowed to return a vector_shuffle operation without
5560 // using a target specific node, because *hopefully* it
5561 // will be optimized away by the dag combiner.
5562 if (VT.getVectorNumElements() <= 4 &&
5563 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5564 return Op;
5565
5566 // Handle splats by matching through known masks
5567 if (VT.getVectorNumElements() <= 4)
5568 return SDValue();
5569
Evan Cheng835580f2010-10-07 20:50:20 +00005570 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005571 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005572 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005573
5574 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5575 // do it!
5576 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5577 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5578 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005579 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005580 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5581 // FIXME: Figure out a cleaner way to do this.
5582 // Try to make use of movq to zero out the top part.
5583 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5584 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5585 if (NewOp.getNode()) {
5586 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5587 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5588 DAG, Subtarget, dl);
5589 }
5590 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5591 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5592 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5593 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5594 DAG, Subtarget, dl);
5595 }
5596 }
5597 return SDValue();
5598}
5599
Dan Gohman475871a2008-07-27 21:46:04 +00005600SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005601X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005602 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005603 SDValue V1 = Op.getOperand(0);
5604 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005605 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005606 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005607 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005608 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005609 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5610 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005611 bool V1IsSplat = false;
5612 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005613 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005614 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005615 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005616 MachineFunction &MF = DAG.getMachineFunction();
5617 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005618
Dale Johannesen0488fb62010-09-30 23:57:10 +00005619 // Shuffle operations on MMX not supported.
5620 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005621 return Op;
5622
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005623 // Vector shuffle lowering takes 3 steps:
5624 //
5625 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5626 // narrowing and commutation of operands should be handled.
5627 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5628 // shuffle nodes.
5629 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5630 // so the shuffle can be broken into other shuffles and the legalizer can
5631 // try the lowering again.
5632 //
5633 // The general ideia is that no vector_shuffle operation should be left to
5634 // be matched during isel, all of them must be converted to a target specific
5635 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005636
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005637 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5638 // narrowing and commutation of operands should be handled. The actual code
5639 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005640 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005641 if (NewOp.getNode())
5642 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005643
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005644 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5645 // unpckh_undef). Only use pshufd if speed is more important than size.
5646 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5647 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5648 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5649 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5650 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5651 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005652
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005653 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005654 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005655 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005656
Dale Johannesen0488fb62010-09-30 23:57:10 +00005657 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005658 return getMOVHighToLow(Op, dl, DAG);
5659
5660 // Use to match splats
5661 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5662 (VT == MVT::v2f64 || VT == MVT::v2i64))
5663 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5664
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005665 if (X86::isPSHUFDMask(SVOp)) {
5666 // The actual implementation will match the mask in the if above and then
5667 // during isel it can match several different instructions, not only pshufd
5668 // as its name says, sad but true, emulate the behavior for now...
5669 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5670 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5671
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005672 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5673
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005674 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005675 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5676
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005677 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005678 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5679 TargetMask, DAG);
5680
5681 if (VT == MVT::v4f32)
5682 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5683 TargetMask, DAG);
5684 }
Eric Christopherfd179292009-08-27 18:07:15 +00005685
Evan Chengf26ffe92008-05-29 08:22:04 +00005686 // Check if this can be converted into a logical shift.
5687 bool isLeft = false;
5688 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005689 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005690 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005691 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005692 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005693 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005694 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005695 EVT EltVT = VT.getVectorElementType();
5696 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005697 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005698 }
Eric Christopherfd179292009-08-27 18:07:15 +00005699
Nate Begeman9008ca62009-04-27 18:41:29 +00005700 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005701 if (V1IsUndef)
5702 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005703 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005704 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005705 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005706 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005707 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5708
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005709 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005710 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5711 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005712 }
Eric Christopherfd179292009-08-27 18:07:15 +00005713
Nate Begeman9008ca62009-04-27 18:41:29 +00005714 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005715 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5716 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005717
Dale Johannesen0488fb62010-09-30 23:57:10 +00005718 if (X86::isMOVHLPSMask(SVOp))
5719 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005720
Dale Johannesen0488fb62010-09-30 23:57:10 +00005721 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5722 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005723
Dale Johannesen0488fb62010-09-30 23:57:10 +00005724 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5725 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005726
Dale Johannesen0488fb62010-09-30 23:57:10 +00005727 if (X86::isMOVLPMask(SVOp))
5728 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005729
Nate Begeman9008ca62009-04-27 18:41:29 +00005730 if (ShouldXformToMOVHLPS(SVOp) ||
5731 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5732 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005733
Evan Chengf26ffe92008-05-29 08:22:04 +00005734 if (isShift) {
5735 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005736 EVT EltVT = VT.getVectorElementType();
5737 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005738 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005739 }
Eric Christopherfd179292009-08-27 18:07:15 +00005740
Evan Cheng9eca5e82006-10-25 21:49:50 +00005741 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005742 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5743 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005744 V1IsSplat = isSplatVector(V1.getNode());
5745 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005746
Chris Lattner8a594482007-11-25 00:24:49 +00005747 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005748 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005749 Op = CommuteVectorShuffle(SVOp, DAG);
5750 SVOp = cast<ShuffleVectorSDNode>(Op);
5751 V1 = SVOp->getOperand(0);
5752 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005753 std::swap(V1IsSplat, V2IsSplat);
5754 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005755 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005756 }
5757
Nate Begeman9008ca62009-04-27 18:41:29 +00005758 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5759 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005760 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005761 return V1;
5762 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5763 // the instruction selector will not match, so get a canonical MOVL with
5764 // swapped operands to undo the commute.
5765 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005766 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005767
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005768 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005769 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005770
5771 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005772 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005773
Evan Cheng9bbbb982006-10-25 20:48:19 +00005774 if (V2IsSplat) {
5775 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005776 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005777 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005778 SDValue NewMask = NormalizeMask(SVOp, DAG);
5779 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5780 if (NSVOp != SVOp) {
5781 if (X86::isUNPCKLMask(NSVOp, true)) {
5782 return NewMask;
5783 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5784 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005785 }
5786 }
5787 }
5788
Evan Cheng9eca5e82006-10-25 21:49:50 +00005789 if (Commuted) {
5790 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005791 // FIXME: this seems wrong.
5792 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5793 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005794
5795 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005796 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005797
5798 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005799 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005800 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005801
Nate Begeman9008ca62009-04-27 18:41:29 +00005802 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005803 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005804 return CommuteVectorShuffle(SVOp, DAG);
5805
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005806 // The checks below are all present in isShuffleMaskLegal, but they are
5807 // inlined here right now to enable us to directly emit target specific
5808 // nodes, and remove one by one until they don't return Op anymore.
5809 SmallVector<int, 16> M;
5810 SVOp->getMask(M);
5811
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005812 if (isPALIGNRMask(M, VT, HasSSSE3))
5813 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5814 X86::getShufflePALIGNRImmediate(SVOp),
5815 DAG);
5816
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005817 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5818 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5819 if (VT == MVT::v2f64)
5820 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5821 if (VT == MVT::v2i64)
5822 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5823 }
5824
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005825 if (isPSHUFHWMask(M, VT))
5826 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5827 X86::getShufflePSHUFHWImmediate(SVOp),
5828 DAG);
5829
5830 if (isPSHUFLWMask(M, VT))
5831 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5832 X86::getShufflePSHUFLWImmediate(SVOp),
5833 DAG);
5834
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005835 if (isSHUFPMask(M, VT)) {
5836 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5837 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5838 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5839 TargetMask, DAG);
5840 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5841 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5842 TargetMask, DAG);
5843 }
5844
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005845 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5846 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5847 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5848 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5849 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5850 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5851
Evan Cheng14b32e12007-12-11 01:46:18 +00005852 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005854 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005855 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005856 return NewOp;
5857 }
5858
Owen Anderson825b72b2009-08-11 20:47:22 +00005859 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005860 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 if (NewOp.getNode())
5862 return NewOp;
5863 }
Eric Christopherfd179292009-08-27 18:07:15 +00005864
Dale Johannesen0488fb62010-09-30 23:57:10 +00005865 // Handle all 4 wide cases with a number of shuffles.
5866 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005867 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005868
Dan Gohman475871a2008-07-27 21:46:04 +00005869 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005870}
5871
Dan Gohman475871a2008-07-27 21:46:04 +00005872SDValue
5873X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005874 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005875 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005876 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005877 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005878 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005879 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005881 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005882 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005883 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005884 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5885 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5886 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5888 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005889 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005891 Op.getOperand(0)),
5892 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005893 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005894 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005895 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005896 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005897 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005899 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5900 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005901 // result has a single use which is a store or a bitcast to i32. And in
5902 // the case of a store, it's not worth it if the index is a constant 0,
5903 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005904 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005905 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005906 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005907 if ((User->getOpcode() != ISD::STORE ||
5908 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5909 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005910 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005911 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005912 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005913 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005914 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005915 Op.getOperand(0)),
5916 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005917 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005919 // ExtractPS works with constant index.
5920 if (isa<ConstantSDNode>(Op.getOperand(1)))
5921 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005922 }
Dan Gohman475871a2008-07-27 21:46:04 +00005923 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005924}
5925
5926
Dan Gohman475871a2008-07-27 21:46:04 +00005927SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005928X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5929 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005930 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005931 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005932
David Greene74a579d2011-02-10 16:57:36 +00005933 SDValue Vec = Op.getOperand(0);
5934 EVT VecVT = Vec.getValueType();
5935
5936 // If this is a 256-bit vector result, first extract the 128-bit
5937 // vector and then extract from the 128-bit vector.
5938 if (VecVT.getSizeInBits() > 128) {
5939 DebugLoc dl = Op.getNode()->getDebugLoc();
5940 unsigned NumElems = VecVT.getVectorNumElements();
5941 SDValue Idx = Op.getOperand(1);
5942
5943 if (!isa<ConstantSDNode>(Idx))
5944 return SDValue();
5945
5946 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5947 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5948
5949 // Get the 128-bit vector.
5950 bool Upper = IdxVal >= ExtractNumElems;
5951 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5952
5953 // Extract from it.
5954 SDValue ScaledIdx = Idx;
5955 if (Upper)
5956 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
5957 DAG.getConstant(ExtractNumElems,
5958 Idx.getValueType()));
5959 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
5960 ScaledIdx);
5961 }
5962
5963 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
5964
Evan Cheng62a3f152008-03-24 21:52:23 +00005965 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005966 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005967 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005968 return Res;
5969 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005970
Owen Andersone50ed302009-08-10 22:56:29 +00005971 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005972 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005973 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005974 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005975 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005976 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005977 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005978 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5979 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005980 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005981 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005982 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005983 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005984 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005985 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005986 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005987 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005988 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005989 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005990 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005991 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005992 if (Idx == 0)
5993 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005994
Evan Cheng0db9fe62006-04-25 20:13:52 +00005995 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005996 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005997 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005998 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005999 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006000 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006001 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006002 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006003 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6004 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6005 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006006 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006007 if (Idx == 0)
6008 return Op;
6009
6010 // UNPCKHPD the element to the lowest double word, then movsd.
6011 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6012 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006013 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006014 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006015 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006016 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006017 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006018 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006019 }
6020
Dan Gohman475871a2008-07-27 21:46:04 +00006021 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006022}
6023
Dan Gohman475871a2008-07-27 21:46:04 +00006024SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006025X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6026 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006027 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006028 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006029 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006030
Dan Gohman475871a2008-07-27 21:46:04 +00006031 SDValue N0 = Op.getOperand(0);
6032 SDValue N1 = Op.getOperand(1);
6033 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006034
Dan Gohman8a55ce42009-09-23 21:02:20 +00006035 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006036 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006037 unsigned Opc;
6038 if (VT == MVT::v8i16)
6039 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006040 else if (VT == MVT::v16i8)
6041 Opc = X86ISD::PINSRB;
6042 else
6043 Opc = X86ISD::PINSRB;
6044
Nate Begeman14d12ca2008-02-11 04:19:36 +00006045 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6046 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006047 if (N1.getValueType() != MVT::i32)
6048 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6049 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006050 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006051 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006052 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006053 // Bits [7:6] of the constant are the source select. This will always be
6054 // zero here. The DAG Combiner may combine an extract_elt index into these
6055 // bits. For example (insert (extract, 3), 2) could be matched by putting
6056 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006057 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006058 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006059 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006060 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006061 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006062 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006063 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006064 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006065 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006066 // PINSR* works with constant index.
6067 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006068 }
Dan Gohman475871a2008-07-27 21:46:04 +00006069 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006070}
6071
Dan Gohman475871a2008-07-27 21:46:04 +00006072SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006073X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006074 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006075 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006076
David Greene6b381262011-02-09 15:32:06 +00006077 DebugLoc dl = Op.getDebugLoc();
6078 SDValue N0 = Op.getOperand(0);
6079 SDValue N1 = Op.getOperand(1);
6080 SDValue N2 = Op.getOperand(2);
6081
6082 // If this is a 256-bit vector result, first insert into a 128-bit
6083 // vector and then insert into the 256-bit vector.
6084 if (VT.getSizeInBits() > 128) {
6085 if (!isa<ConstantSDNode>(N2))
6086 return SDValue();
6087
6088 // Get the 128-bit vector.
6089 unsigned NumElems = VT.getVectorNumElements();
6090 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6091 bool Upper = IdxVal >= NumElems / 2;
6092
6093 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6094
6095 // Insert into it.
6096 SDValue ScaledN2 = N2;
6097 if (Upper)
6098 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
6099 DAG.getConstant(NumElems /
6100 (VT.getSizeInBits() / 128),
6101 N2.getValueType()));
6102 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6103 N1, ScaledN2);
6104
6105 // Insert the 128-bit vector
6106 // FIXME: Why UNDEF?
6107 return Insert128BitVector(N0, Op, N2, DAG, dl);
6108 }
6109
Nate Begeman14d12ca2008-02-11 04:19:36 +00006110 if (Subtarget->hasSSE41())
6111 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6112
Dan Gohman8a55ce42009-09-23 21:02:20 +00006113 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006114 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006115
Dan Gohman8a55ce42009-09-23 21:02:20 +00006116 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006117 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6118 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006119 if (N1.getValueType() != MVT::i32)
6120 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6121 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006122 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006123 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006124 }
Dan Gohman475871a2008-07-27 21:46:04 +00006125 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006126}
6127
Dan Gohman475871a2008-07-27 21:46:04 +00006128SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006129X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006130 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006131 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006132 EVT OpVT = Op.getValueType();
6133
6134 // If this is a 256-bit vector result, first insert into a 128-bit
6135 // vector and then insert into the 256-bit vector.
6136 if (OpVT.getSizeInBits() > 128) {
6137 // Insert into a 128-bit vector.
6138 EVT VT128 = EVT::getVectorVT(*Context,
6139 OpVT.getVectorElementType(),
6140 OpVT.getVectorNumElements() / 2);
6141
6142 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6143
6144 // Insert the 128-bit vector.
6145 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6146 DAG.getConstant(0, MVT::i32),
6147 DAG, dl);
6148 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006149
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006150 if (Op.getValueType() == MVT::v1i64 &&
6151 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006152 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006153
Owen Anderson825b72b2009-08-11 20:47:22 +00006154 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006155 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6156 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006157 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006158 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006159}
6160
David Greene91585092011-01-26 15:38:49 +00006161// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6162// a simple subregister reference or explicit instructions to grab
6163// upper bits of a vector.
6164SDValue
6165X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6166 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006167 DebugLoc dl = Op.getNode()->getDebugLoc();
6168 SDValue Vec = Op.getNode()->getOperand(0);
6169 SDValue Idx = Op.getNode()->getOperand(1);
6170
6171 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6172 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6173 return Extract128BitVector(Vec, Idx, DAG, dl);
6174 }
David Greene91585092011-01-26 15:38:49 +00006175 }
6176 return SDValue();
6177}
6178
David Greenecfe33c42011-01-26 19:13:22 +00006179// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6180// simple superregister reference or explicit instructions to insert
6181// the upper bits of a vector.
6182SDValue
6183X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6184 if (Subtarget->hasAVX()) {
6185 DebugLoc dl = Op.getNode()->getDebugLoc();
6186 SDValue Vec = Op.getNode()->getOperand(0);
6187 SDValue SubVec = Op.getNode()->getOperand(1);
6188 SDValue Idx = Op.getNode()->getOperand(2);
6189
6190 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6191 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006192 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006193 }
6194 }
6195 return SDValue();
6196}
6197
Bill Wendling056292f2008-09-16 21:48:12 +00006198// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6199// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6200// one of the above mentioned nodes. It has to be wrapped because otherwise
6201// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6202// be used to form addressing mode. These wrapped nodes will be selected
6203// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006204SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006205X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006206 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006207
Chris Lattner41621a22009-06-26 19:22:52 +00006208 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6209 // global base reg.
6210 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006211 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006212 CodeModel::Model M = getTargetMachine().getCodeModel();
6213
Chris Lattner4f066492009-07-11 20:29:19 +00006214 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006215 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006216 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006217 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006218 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006219 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006220 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006221
Evan Cheng1606e8e2009-03-13 07:51:59 +00006222 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006223 CP->getAlignment(),
6224 CP->getOffset(), OpFlag);
6225 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006226 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006227 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006228 if (OpFlag) {
6229 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006230 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006231 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006232 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006233 }
6234
6235 return Result;
6236}
6237
Dan Gohmand858e902010-04-17 15:26:15 +00006238SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006239 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006240
Chris Lattner18c59872009-06-27 04:16:01 +00006241 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6242 // global base reg.
6243 unsigned char OpFlag = 0;
6244 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006245 CodeModel::Model M = getTargetMachine().getCodeModel();
6246
Chris Lattner4f066492009-07-11 20:29:19 +00006247 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006248 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006249 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006250 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006251 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006252 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006253 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006254
Chris Lattner18c59872009-06-27 04:16:01 +00006255 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6256 OpFlag);
6257 DebugLoc DL = JT->getDebugLoc();
6258 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006259
Chris Lattner18c59872009-06-27 04:16:01 +00006260 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006261 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006262 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6263 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006264 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006265 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006266
Chris Lattner18c59872009-06-27 04:16:01 +00006267 return Result;
6268}
6269
6270SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006271X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006272 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006273
Chris Lattner18c59872009-06-27 04:16:01 +00006274 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6275 // global base reg.
6276 unsigned char OpFlag = 0;
6277 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006278 CodeModel::Model M = getTargetMachine().getCodeModel();
6279
Chris Lattner4f066492009-07-11 20:29:19 +00006280 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006281 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006282 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006283 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006284 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006285 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006286 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006287
Chris Lattner18c59872009-06-27 04:16:01 +00006288 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006289
Chris Lattner18c59872009-06-27 04:16:01 +00006290 DebugLoc DL = Op.getDebugLoc();
6291 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006292
6293
Chris Lattner18c59872009-06-27 04:16:01 +00006294 // With PIC, the address is actually $g + Offset.
6295 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006296 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006297 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6298 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006299 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006300 Result);
6301 }
Eric Christopherfd179292009-08-27 18:07:15 +00006302
Chris Lattner18c59872009-06-27 04:16:01 +00006303 return Result;
6304}
6305
Dan Gohman475871a2008-07-27 21:46:04 +00006306SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006307X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006308 // Create the TargetBlockAddressAddress node.
6309 unsigned char OpFlags =
6310 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006311 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006312 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006313 DebugLoc dl = Op.getDebugLoc();
6314 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6315 /*isTarget=*/true, OpFlags);
6316
Dan Gohmanf705adb2009-10-30 01:28:02 +00006317 if (Subtarget->isPICStyleRIPRel() &&
6318 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006319 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6320 else
6321 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006322
Dan Gohman29cbade2009-11-20 23:18:13 +00006323 // With PIC, the address is actually $g + Offset.
6324 if (isGlobalRelativeToPICBase(OpFlags)) {
6325 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6326 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6327 Result);
6328 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006329
6330 return Result;
6331}
6332
6333SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006334X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006335 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006336 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006337 // Create the TargetGlobalAddress node, folding in the constant
6338 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006339 unsigned char OpFlags =
6340 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006341 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006342 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006343 if (OpFlags == X86II::MO_NO_FLAG &&
6344 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006345 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006346 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006347 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006348 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006349 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006350 }
Eric Christopherfd179292009-08-27 18:07:15 +00006351
Chris Lattner4f066492009-07-11 20:29:19 +00006352 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006353 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006354 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6355 else
6356 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006357
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006358 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006359 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006360 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6361 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006362 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006363 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006364
Chris Lattner36c25012009-07-10 07:34:39 +00006365 // For globals that require a load from a stub to get the address, emit the
6366 // load.
6367 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006368 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006369 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006370
Dan Gohman6520e202008-10-18 02:06:02 +00006371 // If there was a non-zero offset that we didn't fold, create an explicit
6372 // addition for it.
6373 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006374 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006375 DAG.getConstant(Offset, getPointerTy()));
6376
Evan Cheng0db9fe62006-04-25 20:13:52 +00006377 return Result;
6378}
6379
Evan Chengda43bcf2008-09-24 00:05:32 +00006380SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006381X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006382 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006383 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006384 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006385}
6386
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006387static SDValue
6388GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006389 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006390 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006391 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006392 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006393 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006394 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006395 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006396 GA->getOffset(),
6397 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006398 if (InFlag) {
6399 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006400 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006401 } else {
6402 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006403 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006404 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006405
6406 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006407 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006408
Rafael Espindola15f1b662009-04-24 12:59:40 +00006409 SDValue Flag = Chain.getValue(1);
6410 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006411}
6412
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006413// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006414static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006415LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006416 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006417 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006418 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6419 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006420 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006421 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006422 InFlag = Chain.getValue(1);
6423
Chris Lattnerb903bed2009-06-26 21:20:29 +00006424 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006425}
6426
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006427// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006428static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006429LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006430 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006431 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6432 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006433}
6434
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006435// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6436// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006437static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006438 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006439 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006440 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006441
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006442 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6443 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6444 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006445
Michael J. Spencerec38de22010-10-10 22:04:20 +00006446 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006447 DAG.getIntPtrConstant(0),
6448 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006449
Chris Lattnerb903bed2009-06-26 21:20:29 +00006450 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006451 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6452 // initialexec.
6453 unsigned WrapperKind = X86ISD::Wrapper;
6454 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006455 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006456 } else if (is64Bit) {
6457 assert(model == TLSModel::InitialExec);
6458 OperandFlags = X86II::MO_GOTTPOFF;
6459 WrapperKind = X86ISD::WrapperRIP;
6460 } else {
6461 assert(model == TLSModel::InitialExec);
6462 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006463 }
Eric Christopherfd179292009-08-27 18:07:15 +00006464
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006465 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6466 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006467 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006468 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006469 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006470 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006471
Rafael Espindola9a580232009-02-27 13:37:18 +00006472 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006473 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006474 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006475
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006476 // The address of the thread local variable is the add of the thread
6477 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006478 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006479}
6480
Dan Gohman475871a2008-07-27 21:46:04 +00006481SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006482X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006483
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006484 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006485 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006486
Eric Christopher30ef0e52010-06-03 04:07:48 +00006487 if (Subtarget->isTargetELF()) {
6488 // TODO: implement the "local dynamic" model
6489 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006490
Eric Christopher30ef0e52010-06-03 04:07:48 +00006491 // If GV is an alias then use the aliasee for determining
6492 // thread-localness.
6493 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6494 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006495
6496 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006497 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006498
Eric Christopher30ef0e52010-06-03 04:07:48 +00006499 switch (model) {
6500 case TLSModel::GeneralDynamic:
6501 case TLSModel::LocalDynamic: // not implemented
6502 if (Subtarget->is64Bit())
6503 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6504 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006505
Eric Christopher30ef0e52010-06-03 04:07:48 +00006506 case TLSModel::InitialExec:
6507 case TLSModel::LocalExec:
6508 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6509 Subtarget->is64Bit());
6510 }
6511 } else if (Subtarget->isTargetDarwin()) {
6512 // Darwin only has one model of TLS. Lower to that.
6513 unsigned char OpFlag = 0;
6514 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6515 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006516
Eric Christopher30ef0e52010-06-03 04:07:48 +00006517 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6518 // global base reg.
6519 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6520 !Subtarget->is64Bit();
6521 if (PIC32)
6522 OpFlag = X86II::MO_TLVP_PIC_BASE;
6523 else
6524 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006525 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006526 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006527 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006528 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006529 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006530
Eric Christopher30ef0e52010-06-03 04:07:48 +00006531 // With PIC32, the address is actually $g + Offset.
6532 if (PIC32)
6533 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6534 DAG.getNode(X86ISD::GlobalBaseReg,
6535 DebugLoc(), getPointerTy()),
6536 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006537
Eric Christopher30ef0e52010-06-03 04:07:48 +00006538 // Lowering the machine isd will make sure everything is in the right
6539 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006540 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006541 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006542 SDValue Args[] = { Chain, Offset };
6543 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006544
Eric Christopher30ef0e52010-06-03 04:07:48 +00006545 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6546 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6547 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006548
Eric Christopher30ef0e52010-06-03 04:07:48 +00006549 // And our return value (tls address) is in the standard call return value
6550 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006551 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6552 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006553 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006554
Eric Christopher30ef0e52010-06-03 04:07:48 +00006555 assert(false &&
6556 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006557
Torok Edwinc23197a2009-07-14 16:55:14 +00006558 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006559 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006560}
6561
Evan Cheng0db9fe62006-04-25 20:13:52 +00006562
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006563/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006564/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006565SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006566 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006567 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006568 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006569 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006570 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006571 SDValue ShOpLo = Op.getOperand(0);
6572 SDValue ShOpHi = Op.getOperand(1);
6573 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006574 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006575 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006576 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006577
Dan Gohman475871a2008-07-27 21:46:04 +00006578 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006579 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006580 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6581 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006582 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006583 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6584 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006585 }
Evan Chenge3413162006-01-09 18:33:28 +00006586
Owen Anderson825b72b2009-08-11 20:47:22 +00006587 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6588 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006589 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006590 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006591
Dan Gohman475871a2008-07-27 21:46:04 +00006592 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006593 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006594 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6595 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006596
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006597 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006598 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6599 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006600 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006601 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6602 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006603 }
6604
Dan Gohman475871a2008-07-27 21:46:04 +00006605 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006606 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006607}
Evan Chenga3195e82006-01-12 22:54:21 +00006608
Dan Gohmand858e902010-04-17 15:26:15 +00006609SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6610 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006611 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006612
Dale Johannesen0488fb62010-09-30 23:57:10 +00006613 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006614 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006615
Owen Anderson825b72b2009-08-11 20:47:22 +00006616 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006617 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006618
Eli Friedman36df4992009-05-27 00:47:34 +00006619 // These are really Legal; return the operand so the caller accepts it as
6620 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006621 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006622 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006623 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006624 Subtarget->is64Bit()) {
6625 return Op;
6626 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006627
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006628 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006629 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006630 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006631 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006632 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006633 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006634 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006635 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006636 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006637 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6638}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006639
Owen Andersone50ed302009-08-10 22:56:29 +00006640SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006641 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006642 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006643 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006644 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006645 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006646 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006647 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006648 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006649 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006650 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006651
Chris Lattner492a43e2010-09-22 01:28:21 +00006652 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006653
Chris Lattner492a43e2010-09-22 01:28:21 +00006654 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6655 MachineMemOperand *MMO =
6656 DAG.getMachineFunction()
6657 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6658 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006659
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006660 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006661 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6662 X86ISD::FILD, DL,
6663 Tys, Ops, array_lengthof(Ops),
6664 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006665
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006666 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006667 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006668 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006669
6670 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6671 // shouldn't be necessary except that RFP cannot be live across
6672 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006673 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006674 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6675 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006676 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006677 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006678 SDValue Ops[] = {
6679 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6680 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006681 MachineMemOperand *MMO =
6682 DAG.getMachineFunction()
6683 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006684 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006685
Chris Lattner492a43e2010-09-22 01:28:21 +00006686 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6687 Ops, array_lengthof(Ops),
6688 Op.getValueType(), MMO);
6689 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006690 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006691 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006692 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006693
Evan Cheng0db9fe62006-04-25 20:13:52 +00006694 return Result;
6695}
6696
Bill Wendling8b8a6362009-01-17 03:56:04 +00006697// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006698SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6699 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006700 // This algorithm is not obvious. Here it is in C code, more or less:
6701 /*
6702 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6703 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6704 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006705
Bill Wendling8b8a6362009-01-17 03:56:04 +00006706 // Copy ints to xmm registers.
6707 __m128i xh = _mm_cvtsi32_si128( hi );
6708 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006709
Bill Wendling8b8a6362009-01-17 03:56:04 +00006710 // Combine into low half of a single xmm register.
6711 __m128i x = _mm_unpacklo_epi32( xh, xl );
6712 __m128d d;
6713 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006714
Bill Wendling8b8a6362009-01-17 03:56:04 +00006715 // Merge in appropriate exponents to give the integer bits the right
6716 // magnitude.
6717 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006718
Bill Wendling8b8a6362009-01-17 03:56:04 +00006719 // Subtract away the biases to deal with the IEEE-754 double precision
6720 // implicit 1.
6721 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006722
Bill Wendling8b8a6362009-01-17 03:56:04 +00006723 // All conversions up to here are exact. The correctly rounded result is
6724 // calculated using the current rounding mode using the following
6725 // horizontal add.
6726 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6727 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6728 // store doesn't really need to be here (except
6729 // maybe to zero the other double)
6730 return sd;
6731 }
6732 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006733
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006734 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006735 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006736
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006737 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006738 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006739 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6740 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6741 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6742 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006743 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006744 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006745
Bill Wendling8b8a6362009-01-17 03:56:04 +00006746 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006747 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006748 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006749 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006750 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006751 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006752 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006753
Owen Anderson825b72b2009-08-11 20:47:22 +00006754 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6755 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006756 Op.getOperand(0),
6757 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006758 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6759 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006760 Op.getOperand(0),
6761 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006762 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6763 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006764 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006765 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006766 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006767 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006768 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006769 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006770 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006771 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006772
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006773 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006774 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006775 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6776 DAG.getUNDEF(MVT::v2f64), ShufMask);
6777 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6778 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006779 DAG.getIntPtrConstant(0));
6780}
6781
Bill Wendling8b8a6362009-01-17 03:56:04 +00006782// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006783SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6784 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006785 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006786 // FP constant to bias correct the final result.
6787 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006788 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006789
6790 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006791 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6792 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006793 Op.getOperand(0),
6794 DAG.getIntPtrConstant(0)));
6795
Owen Anderson825b72b2009-08-11 20:47:22 +00006796 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006797 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006798 DAG.getIntPtrConstant(0));
6799
6800 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006801 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006802 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006803 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006804 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006805 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006806 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006807 MVT::v2f64, Bias)));
6808 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006809 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006810 DAG.getIntPtrConstant(0));
6811
6812 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006813 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006814
6815 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006816 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006817
Owen Anderson825b72b2009-08-11 20:47:22 +00006818 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006819 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006820 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006821 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006822 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006823 }
6824
6825 // Handle final rounding.
6826 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006827}
6828
Dan Gohmand858e902010-04-17 15:26:15 +00006829SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6830 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006831 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006832 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006833
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006834 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006835 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6836 // the optimization here.
6837 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006838 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006839
Owen Andersone50ed302009-08-10 22:56:29 +00006840 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006841 EVT DstVT = Op.getValueType();
6842 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006843 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006844 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006845 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006846
6847 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006848 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006849 if (SrcVT == MVT::i32) {
6850 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6851 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6852 getPointerTy(), StackSlot, WordOff);
6853 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006854 StackSlot, MachinePointerInfo(),
6855 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006856 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006857 OffsetSlot, MachinePointerInfo(),
6858 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006859 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6860 return Fild;
6861 }
6862
6863 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6864 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006865 StackSlot, MachinePointerInfo(),
6866 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006867 // For i64 source, we need to add the appropriate power of 2 if the input
6868 // was negative. This is the same as the optimization in
6869 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6870 // we must be careful to do the computation in x87 extended precision, not
6871 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006872 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6873 MachineMemOperand *MMO =
6874 DAG.getMachineFunction()
6875 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6876 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006877
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006878 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6879 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006880 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6881 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006882
6883 APInt FF(32, 0x5F800000ULL);
6884
6885 // Check whether the sign bit is set.
6886 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6887 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6888 ISD::SETLT);
6889
6890 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6891 SDValue FudgePtr = DAG.getConstantPool(
6892 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6893 getPointerTy());
6894
6895 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6896 SDValue Zero = DAG.getIntPtrConstant(0);
6897 SDValue Four = DAG.getIntPtrConstant(4);
6898 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6899 Zero, Four);
6900 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6901
6902 // Load the value out, extending it from f32 to f80.
6903 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006904 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006905 FudgePtr, MachinePointerInfo::getConstantPool(),
6906 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006907 // Extend everything to 80 bits to force it to be done on x87.
6908 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6909 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006910}
6911
Dan Gohman475871a2008-07-27 21:46:04 +00006912std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006913FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006914 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006915
Owen Andersone50ed302009-08-10 22:56:29 +00006916 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006917
6918 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006919 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6920 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006921 }
6922
Owen Anderson825b72b2009-08-11 20:47:22 +00006923 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6924 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006925 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006926
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006927 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006928 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006929 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006930 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006931 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006932 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006933 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006934 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006935
Evan Cheng87c89352007-10-15 20:11:21 +00006936 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6937 // stack slot.
6938 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006939 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006940 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006941 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006942
Michael J. Spencerec38de22010-10-10 22:04:20 +00006943
6944
Evan Cheng0db9fe62006-04-25 20:13:52 +00006945 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006946 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006947 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6949 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6950 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006951 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006952
Dan Gohman475871a2008-07-27 21:46:04 +00006953 SDValue Chain = DAG.getEntryNode();
6954 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006955 EVT TheVT = Op.getOperand(0).getValueType();
6956 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006958 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006959 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006960 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006961 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006962 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006963 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006964 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006965
Chris Lattner492a43e2010-09-22 01:28:21 +00006966 MachineMemOperand *MMO =
6967 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6968 MachineMemOperand::MOLoad, MemSize, MemSize);
6969 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6970 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006971 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006972 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006973 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6974 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006975
Chris Lattner07290932010-09-22 01:05:16 +00006976 MachineMemOperand *MMO =
6977 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6978 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006979
Evan Cheng0db9fe62006-04-25 20:13:52 +00006980 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006981 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006982 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6983 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006984
Chris Lattner27a6c732007-11-24 07:07:01 +00006985 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006986}
6987
Dan Gohmand858e902010-04-17 15:26:15 +00006988SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6989 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006990 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006991 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006992
Eli Friedman948e95a2009-05-23 09:59:16 +00006993 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006994 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006995 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6996 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006997
Chris Lattner27a6c732007-11-24 07:07:01 +00006998 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006999 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007000 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007001}
7002
Dan Gohmand858e902010-04-17 15:26:15 +00007003SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7004 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007005 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7006 SDValue FIST = Vals.first, StackSlot = Vals.second;
7007 assert(FIST.getNode() && "Unexpected failure");
7008
7009 // Load the result.
7010 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007011 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007012}
7013
Dan Gohmand858e902010-04-17 15:26:15 +00007014SDValue X86TargetLowering::LowerFABS(SDValue Op,
7015 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007016 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007017 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007018 EVT VT = Op.getValueType();
7019 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007020 if (VT.isVector())
7021 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007022 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007023 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007024 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007025 CV.push_back(C);
7026 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007027 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007028 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007029 CV.push_back(C);
7030 CV.push_back(C);
7031 CV.push_back(C);
7032 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007033 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007034 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007035 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007036 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007037 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007038 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007039 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007040}
7041
Dan Gohmand858e902010-04-17 15:26:15 +00007042SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007043 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007044 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007045 EVT VT = Op.getValueType();
7046 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007047 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007048 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007049 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007051 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007052 CV.push_back(C);
7053 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007054 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007055 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007056 CV.push_back(C);
7057 CV.push_back(C);
7058 CV.push_back(C);
7059 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007060 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007061 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007062 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007063 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007064 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007065 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007066 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007067 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007069 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007070 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007071 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007072 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007073 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007074 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007075}
7076
Dan Gohmand858e902010-04-17 15:26:15 +00007077SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007078 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007079 SDValue Op0 = Op.getOperand(0);
7080 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007081 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007082 EVT VT = Op.getValueType();
7083 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007084
7085 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007086 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007087 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007088 SrcVT = VT;
7089 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007090 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007091 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007092 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007093 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007094 }
7095
7096 // At this point the operands and the result should have the same
7097 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007098
Evan Cheng68c47cb2007-01-05 07:55:56 +00007099 // First get the sign bit of second operand.
7100 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007101 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007102 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7103 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007104 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007105 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7106 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7107 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7108 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007109 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007110 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007111 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007112 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007113 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007114 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007115 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007116
7117 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007118 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 // Op0 is MVT::f32, Op1 is MVT::f64.
7120 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7121 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7122 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007123 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007124 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007125 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007126 }
7127
Evan Cheng73d6cf12007-01-05 21:37:56 +00007128 // Clear first operand sign bit.
7129 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007131 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7132 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007133 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007134 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7135 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7136 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7137 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007138 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007139 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007140 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007141 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007142 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007143 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007144 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007145
7146 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007147 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007148}
7149
Dan Gohman076aee32009-03-04 19:44:21 +00007150/// Emit nodes that will be selected as "test Op0,Op0", or something
7151/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007152SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007153 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007154 DebugLoc dl = Op.getDebugLoc();
7155
Dan Gohman31125812009-03-07 01:58:32 +00007156 // CF and OF aren't always set the way we want. Determine which
7157 // of these we need.
7158 bool NeedCF = false;
7159 bool NeedOF = false;
7160 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007161 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007162 case X86::COND_A: case X86::COND_AE:
7163 case X86::COND_B: case X86::COND_BE:
7164 NeedCF = true;
7165 break;
7166 case X86::COND_G: case X86::COND_GE:
7167 case X86::COND_L: case X86::COND_LE:
7168 case X86::COND_O: case X86::COND_NO:
7169 NeedOF = true;
7170 break;
Dan Gohman31125812009-03-07 01:58:32 +00007171 }
7172
Dan Gohman076aee32009-03-04 19:44:21 +00007173 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007174 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7175 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007176 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7177 // Emit a CMP with 0, which is the TEST pattern.
7178 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7179 DAG.getConstant(0, Op.getValueType()));
7180
7181 unsigned Opcode = 0;
7182 unsigned NumOperands = 0;
7183 switch (Op.getNode()->getOpcode()) {
7184 case ISD::ADD:
7185 // Due to an isel shortcoming, be conservative if this add is likely to be
7186 // selected as part of a load-modify-store instruction. When the root node
7187 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7188 // uses of other nodes in the match, such as the ADD in this case. This
7189 // leads to the ADD being left around and reselected, with the result being
7190 // two adds in the output. Alas, even if none our users are stores, that
7191 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7192 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7193 // climbing the DAG back to the root, and it doesn't seem to be worth the
7194 // effort.
7195 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007196 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007197 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7198 goto default_case;
7199
7200 if (ConstantSDNode *C =
7201 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7202 // An add of one will be selected as an INC.
7203 if (C->getAPIntValue() == 1) {
7204 Opcode = X86ISD::INC;
7205 NumOperands = 1;
7206 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007207 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007208
7209 // An add of negative one (subtract of one) will be selected as a DEC.
7210 if (C->getAPIntValue().isAllOnesValue()) {
7211 Opcode = X86ISD::DEC;
7212 NumOperands = 1;
7213 break;
7214 }
Dan Gohman076aee32009-03-04 19:44:21 +00007215 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007216
7217 // Otherwise use a regular EFLAGS-setting add.
7218 Opcode = X86ISD::ADD;
7219 NumOperands = 2;
7220 break;
7221 case ISD::AND: {
7222 // If the primary and result isn't used, don't bother using X86ISD::AND,
7223 // because a TEST instruction will be better.
7224 bool NonFlagUse = false;
7225 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7226 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7227 SDNode *User = *UI;
7228 unsigned UOpNo = UI.getOperandNo();
7229 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7230 // Look pass truncate.
7231 UOpNo = User->use_begin().getOperandNo();
7232 User = *User->use_begin();
7233 }
7234
7235 if (User->getOpcode() != ISD::BRCOND &&
7236 User->getOpcode() != ISD::SETCC &&
7237 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7238 NonFlagUse = true;
7239 break;
7240 }
Dan Gohman076aee32009-03-04 19:44:21 +00007241 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007242
7243 if (!NonFlagUse)
7244 break;
7245 }
7246 // FALL THROUGH
7247 case ISD::SUB:
7248 case ISD::OR:
7249 case ISD::XOR:
7250 // Due to the ISEL shortcoming noted above, be conservative if this op is
7251 // likely to be selected as part of a load-modify-store instruction.
7252 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7253 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7254 if (UI->getOpcode() == ISD::STORE)
7255 goto default_case;
7256
7257 // Otherwise use a regular EFLAGS-setting instruction.
7258 switch (Op.getNode()->getOpcode()) {
7259 default: llvm_unreachable("unexpected operator!");
7260 case ISD::SUB: Opcode = X86ISD::SUB; break;
7261 case ISD::OR: Opcode = X86ISD::OR; break;
7262 case ISD::XOR: Opcode = X86ISD::XOR; break;
7263 case ISD::AND: Opcode = X86ISD::AND; break;
7264 }
7265
7266 NumOperands = 2;
7267 break;
7268 case X86ISD::ADD:
7269 case X86ISD::SUB:
7270 case X86ISD::INC:
7271 case X86ISD::DEC:
7272 case X86ISD::OR:
7273 case X86ISD::XOR:
7274 case X86ISD::AND:
7275 return SDValue(Op.getNode(), 1);
7276 default:
7277 default_case:
7278 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007279 }
7280
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007281 if (Opcode == 0)
7282 // Emit a CMP with 0, which is the TEST pattern.
7283 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7284 DAG.getConstant(0, Op.getValueType()));
7285
7286 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7287 SmallVector<SDValue, 4> Ops;
7288 for (unsigned i = 0; i != NumOperands; ++i)
7289 Ops.push_back(Op.getOperand(i));
7290
7291 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7292 DAG.ReplaceAllUsesWith(Op, New);
7293 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007294}
7295
7296/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7297/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007298SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007299 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007300 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7301 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007302 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007303
7304 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007305 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007306}
7307
Evan Chengd40d03e2010-01-06 19:38:29 +00007308/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7309/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007310SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7311 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007312 SDValue Op0 = And.getOperand(0);
7313 SDValue Op1 = And.getOperand(1);
7314 if (Op0.getOpcode() == ISD::TRUNCATE)
7315 Op0 = Op0.getOperand(0);
7316 if (Op1.getOpcode() == ISD::TRUNCATE)
7317 Op1 = Op1.getOperand(0);
7318
Evan Chengd40d03e2010-01-06 19:38:29 +00007319 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007320 if (Op1.getOpcode() == ISD::SHL)
7321 std::swap(Op0, Op1);
7322 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007323 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7324 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007325 // If we looked past a truncate, check that it's only truncating away
7326 // known zeros.
7327 unsigned BitWidth = Op0.getValueSizeInBits();
7328 unsigned AndBitWidth = And.getValueSizeInBits();
7329 if (BitWidth > AndBitWidth) {
7330 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7331 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7332 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7333 return SDValue();
7334 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007335 LHS = Op1;
7336 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007337 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007338 } else if (Op1.getOpcode() == ISD::Constant) {
7339 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7340 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007341 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7342 LHS = AndLHS.getOperand(0);
7343 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007344 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007345 }
Evan Cheng0488db92007-09-25 01:57:46 +00007346
Evan Chengd40d03e2010-01-06 19:38:29 +00007347 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007348 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007349 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007350 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007351 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007352 // Also promote i16 to i32 for performance / code size reason.
7353 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007354 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007355 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007356
Evan Chengd40d03e2010-01-06 19:38:29 +00007357 // If the operand types disagree, extend the shift amount to match. Since
7358 // BT ignores high bits (like shifts) we can use anyextend.
7359 if (LHS.getValueType() != RHS.getValueType())
7360 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007361
Evan Chengd40d03e2010-01-06 19:38:29 +00007362 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7363 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7364 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7365 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007366 }
7367
Evan Cheng54de3ea2010-01-05 06:52:31 +00007368 return SDValue();
7369}
7370
Dan Gohmand858e902010-04-17 15:26:15 +00007371SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007372 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7373 SDValue Op0 = Op.getOperand(0);
7374 SDValue Op1 = Op.getOperand(1);
7375 DebugLoc dl = Op.getDebugLoc();
7376 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7377
7378 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007379 // Lower (X & (1 << N)) == 0 to BT(X, N).
7380 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7381 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007382 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007383 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007384 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007385 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7386 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7387 if (NewSetCC.getNode())
7388 return NewSetCC;
7389 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007390
Chris Lattner481eebc2010-12-19 21:23:48 +00007391 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7392 // these.
7393 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007394 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7395 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7396 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007397
Chris Lattner481eebc2010-12-19 21:23:48 +00007398 // If the input is a setcc, then reuse the input setcc or use a new one with
7399 // the inverted condition.
7400 if (Op0.getOpcode() == X86ISD::SETCC) {
7401 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7402 bool Invert = (CC == ISD::SETNE) ^
7403 cast<ConstantSDNode>(Op1)->isNullValue();
7404 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007405
Evan Cheng2c755ba2010-02-27 07:36:59 +00007406 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007407 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7408 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7409 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007410 }
7411
Evan Chenge5b51ac2010-04-17 06:13:15 +00007412 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007413 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007414 if (X86CC == X86::COND_INVALID)
7415 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007416
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007417 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007418 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007419 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007420}
7421
Dan Gohmand858e902010-04-17 15:26:15 +00007422SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007423 SDValue Cond;
7424 SDValue Op0 = Op.getOperand(0);
7425 SDValue Op1 = Op.getOperand(1);
7426 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007427 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007428 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7429 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007430 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007431
7432 if (isFP) {
7433 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007434 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007435 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7436 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007437 bool Swap = false;
7438
7439 switch (SetCCOpcode) {
7440 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007441 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007442 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007443 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007444 case ISD::SETGT: Swap = true; // Fallthrough
7445 case ISD::SETLT:
7446 case ISD::SETOLT: SSECC = 1; break;
7447 case ISD::SETOGE:
7448 case ISD::SETGE: Swap = true; // Fallthrough
7449 case ISD::SETLE:
7450 case ISD::SETOLE: SSECC = 2; break;
7451 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007452 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007453 case ISD::SETNE: SSECC = 4; break;
7454 case ISD::SETULE: Swap = true;
7455 case ISD::SETUGE: SSECC = 5; break;
7456 case ISD::SETULT: Swap = true;
7457 case ISD::SETUGT: SSECC = 6; break;
7458 case ISD::SETO: SSECC = 7; break;
7459 }
7460 if (Swap)
7461 std::swap(Op0, Op1);
7462
Nate Begemanfb8ead02008-07-25 19:05:58 +00007463 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007464 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007465 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007466 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007467 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7468 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007469 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007470 }
7471 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007472 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007473 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7474 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007475 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007476 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007477 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007478 }
7479 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007480 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007481 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007482
Nate Begeman30a0de92008-07-17 16:51:19 +00007483 // We are handling one of the integer comparisons here. Since SSE only has
7484 // GT and EQ comparisons for integer, swapping operands and multiple
7485 // operations may be required for some comparisons.
7486 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7487 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007488
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007490 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007492 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007493 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7494 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007495 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007496
Nate Begeman30a0de92008-07-17 16:51:19 +00007497 switch (SetCCOpcode) {
7498 default: break;
7499 case ISD::SETNE: Invert = true;
7500 case ISD::SETEQ: Opc = EQOpc; break;
7501 case ISD::SETLT: Swap = true;
7502 case ISD::SETGT: Opc = GTOpc; break;
7503 case ISD::SETGE: Swap = true;
7504 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7505 case ISD::SETULT: Swap = true;
7506 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7507 case ISD::SETUGE: Swap = true;
7508 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7509 }
7510 if (Swap)
7511 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007512
Nate Begeman30a0de92008-07-17 16:51:19 +00007513 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7514 // bits of the inputs before performing those operations.
7515 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007516 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007517 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7518 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007519 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007520 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7521 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007522 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7523 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007524 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007525
Dale Johannesenace16102009-02-03 19:33:06 +00007526 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007527
7528 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007529 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007530 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007531
Nate Begeman30a0de92008-07-17 16:51:19 +00007532 return Result;
7533}
Evan Cheng0488db92007-09-25 01:57:46 +00007534
Evan Cheng370e5342008-12-03 08:38:43 +00007535// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007536static bool isX86LogicalCmp(SDValue Op) {
7537 unsigned Opc = Op.getNode()->getOpcode();
7538 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7539 return true;
7540 if (Op.getResNo() == 1 &&
7541 (Opc == X86ISD::ADD ||
7542 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007543 Opc == X86ISD::ADC ||
7544 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007545 Opc == X86ISD::SMUL ||
7546 Opc == X86ISD::UMUL ||
7547 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007548 Opc == X86ISD::DEC ||
7549 Opc == X86ISD::OR ||
7550 Opc == X86ISD::XOR ||
7551 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007552 return true;
7553
Chris Lattner9637d5b2010-12-05 07:49:54 +00007554 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7555 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007556
Dan Gohman076aee32009-03-04 19:44:21 +00007557 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007558}
7559
Chris Lattnera2b56002010-12-05 01:23:24 +00007560static bool isZero(SDValue V) {
7561 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7562 return C && C->isNullValue();
7563}
7564
Chris Lattner96908b12010-12-05 02:00:51 +00007565static bool isAllOnes(SDValue V) {
7566 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7567 return C && C->isAllOnesValue();
7568}
7569
Dan Gohmand858e902010-04-17 15:26:15 +00007570SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007571 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007572 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007573 SDValue Op1 = Op.getOperand(1);
7574 SDValue Op2 = Op.getOperand(2);
7575 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007576 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007577
Dan Gohman1a492952009-10-20 16:22:37 +00007578 if (Cond.getOpcode() == ISD::SETCC) {
7579 SDValue NewCond = LowerSETCC(Cond, DAG);
7580 if (NewCond.getNode())
7581 Cond = NewCond;
7582 }
Evan Cheng734503b2006-09-11 02:19:56 +00007583
Chris Lattnera2b56002010-12-05 01:23:24 +00007584 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007585 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007586 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007587 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007588 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007589 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7590 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007591 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007592
Chris Lattnera2b56002010-12-05 01:23:24 +00007593 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007594
7595 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007596 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7597 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007598
7599 SDValue CmpOp0 = Cmp.getOperand(0);
7600 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7601 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007602
Chris Lattner96908b12010-12-05 02:00:51 +00007603 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007604 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7605 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007606
Chris Lattner96908b12010-12-05 02:00:51 +00007607 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7608 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007609
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007610 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007611 if (N2C == 0 || !N2C->isNullValue())
7612 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7613 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007614 }
7615 }
7616
Chris Lattnera2b56002010-12-05 01:23:24 +00007617 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007618 if (Cond.getOpcode() == ISD::AND &&
7619 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7620 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007621 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007622 Cond = Cond.getOperand(0);
7623 }
7624
Evan Cheng3f41d662007-10-08 22:16:29 +00007625 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7626 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007627 if (Cond.getOpcode() == X86ISD::SETCC ||
7628 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007629 CC = Cond.getOperand(0);
7630
Dan Gohman475871a2008-07-27 21:46:04 +00007631 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007632 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007633 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007634
Evan Cheng3f41d662007-10-08 22:16:29 +00007635 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007636 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007637 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007638 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007639
Chris Lattnerd1980a52009-03-12 06:52:53 +00007640 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7641 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007642 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007643 addTest = false;
7644 }
7645 }
7646
7647 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007648 // Look pass the truncate.
7649 if (Cond.getOpcode() == ISD::TRUNCATE)
7650 Cond = Cond.getOperand(0);
7651
7652 // We know the result of AND is compared against zero. Try to match
7653 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007654 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007655 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007656 if (NewSetCC.getNode()) {
7657 CC = NewSetCC.getOperand(0);
7658 Cond = NewSetCC.getOperand(1);
7659 addTest = false;
7660 }
7661 }
7662 }
7663
7664 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007665 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007666 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007667 }
7668
Benjamin Kramere915ff32010-12-22 23:09:28 +00007669 // a < b ? -1 : 0 -> RES = ~setcc_carry
7670 // a < b ? 0 : -1 -> RES = setcc_carry
7671 // a >= b ? -1 : 0 -> RES = setcc_carry
7672 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7673 if (Cond.getOpcode() == X86ISD::CMP) {
7674 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7675
7676 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7677 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7678 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7679 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7680 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7681 return DAG.getNOT(DL, Res, Res.getValueType());
7682 return Res;
7683 }
7684 }
7685
Evan Cheng0488db92007-09-25 01:57:46 +00007686 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7687 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007688 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007689 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007690 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007691}
7692
Evan Cheng370e5342008-12-03 08:38:43 +00007693// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7694// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7695// from the AND / OR.
7696static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7697 Opc = Op.getOpcode();
7698 if (Opc != ISD::OR && Opc != ISD::AND)
7699 return false;
7700 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7701 Op.getOperand(0).hasOneUse() &&
7702 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7703 Op.getOperand(1).hasOneUse());
7704}
7705
Evan Cheng961d6d42009-02-02 08:19:07 +00007706// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7707// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007708static bool isXor1OfSetCC(SDValue Op) {
7709 if (Op.getOpcode() != ISD::XOR)
7710 return false;
7711 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7712 if (N1C && N1C->getAPIntValue() == 1) {
7713 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7714 Op.getOperand(0).hasOneUse();
7715 }
7716 return false;
7717}
7718
Dan Gohmand858e902010-04-17 15:26:15 +00007719SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007720 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007721 SDValue Chain = Op.getOperand(0);
7722 SDValue Cond = Op.getOperand(1);
7723 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007724 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007725 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007726
Dan Gohman1a492952009-10-20 16:22:37 +00007727 if (Cond.getOpcode() == ISD::SETCC) {
7728 SDValue NewCond = LowerSETCC(Cond, DAG);
7729 if (NewCond.getNode())
7730 Cond = NewCond;
7731 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007732#if 0
7733 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007734 else if (Cond.getOpcode() == X86ISD::ADD ||
7735 Cond.getOpcode() == X86ISD::SUB ||
7736 Cond.getOpcode() == X86ISD::SMUL ||
7737 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007738 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007739#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007740
Evan Chengad9c0a32009-12-15 00:53:42 +00007741 // Look pass (and (setcc_carry (cmp ...)), 1).
7742 if (Cond.getOpcode() == ISD::AND &&
7743 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7744 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007745 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007746 Cond = Cond.getOperand(0);
7747 }
7748
Evan Cheng3f41d662007-10-08 22:16:29 +00007749 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7750 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007751 if (Cond.getOpcode() == X86ISD::SETCC ||
7752 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007753 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007754
Dan Gohman475871a2008-07-27 21:46:04 +00007755 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007756 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007757 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007758 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007759 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007760 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007761 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007762 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007763 default: break;
7764 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007765 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007766 // These can only come from an arithmetic instruction with overflow,
7767 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007768 Cond = Cond.getNode()->getOperand(1);
7769 addTest = false;
7770 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007771 }
Evan Cheng0488db92007-09-25 01:57:46 +00007772 }
Evan Cheng370e5342008-12-03 08:38:43 +00007773 } else {
7774 unsigned CondOpc;
7775 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7776 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007777 if (CondOpc == ISD::OR) {
7778 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7779 // two branches instead of an explicit OR instruction with a
7780 // separate test.
7781 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007782 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007783 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007784 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007785 Chain, Dest, CC, Cmp);
7786 CC = Cond.getOperand(1).getOperand(0);
7787 Cond = Cmp;
7788 addTest = false;
7789 }
7790 } else { // ISD::AND
7791 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7792 // two branches instead of an explicit AND instruction with a
7793 // separate test. However, we only do this if this block doesn't
7794 // have a fall-through edge, because this requires an explicit
7795 // jmp when the condition is false.
7796 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007797 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007798 Op.getNode()->hasOneUse()) {
7799 X86::CondCode CCode =
7800 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7801 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007802 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007803 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007804 // Look for an unconditional branch following this conditional branch.
7805 // We need this because we need to reverse the successors in order
7806 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007807 if (User->getOpcode() == ISD::BR) {
7808 SDValue FalseBB = User->getOperand(1);
7809 SDNode *NewBR =
7810 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007811 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007812 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007813 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007814
Dale Johannesene4d209d2009-02-03 20:21:25 +00007815 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007816 Chain, Dest, CC, Cmp);
7817 X86::CondCode CCode =
7818 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7819 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007820 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007821 Cond = Cmp;
7822 addTest = false;
7823 }
7824 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007825 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007826 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7827 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7828 // It should be transformed during dag combiner except when the condition
7829 // is set by a arithmetics with overflow node.
7830 X86::CondCode CCode =
7831 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7832 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007833 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007834 Cond = Cond.getOperand(0).getOperand(1);
7835 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007836 }
Evan Cheng0488db92007-09-25 01:57:46 +00007837 }
7838
7839 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007840 // Look pass the truncate.
7841 if (Cond.getOpcode() == ISD::TRUNCATE)
7842 Cond = Cond.getOperand(0);
7843
7844 // We know the result of AND is compared against zero. Try to match
7845 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007846 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007847 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7848 if (NewSetCC.getNode()) {
7849 CC = NewSetCC.getOperand(0);
7850 Cond = NewSetCC.getOperand(1);
7851 addTest = false;
7852 }
7853 }
7854 }
7855
7856 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007857 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007858 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007859 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007860 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007861 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007862}
7863
Anton Korobeynikove060b532007-04-17 19:34:00 +00007864
7865// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7866// Calls to _alloca is needed to probe the stack when allocating more than 4k
7867// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7868// that the guard pages used by the OS virtual memory manager are allocated in
7869// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007870SDValue
7871X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007872 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007873 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007874 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007875 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007876
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007877 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007878 SDValue Chain = Op.getOperand(0);
7879 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007880 // FIXME: Ensure alignment here
7881
Dan Gohman475871a2008-07-27 21:46:04 +00007882 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007883
Owen Anderson825b72b2009-08-11 20:47:22 +00007884 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007885
Dale Johannesendd64c412009-02-04 00:33:20 +00007886 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007887 Flag = Chain.getValue(1);
7888
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007889 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007890
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007891 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007892 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007893
Dale Johannesendd64c412009-02-04 00:33:20 +00007894 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007895
Dan Gohman475871a2008-07-27 21:46:04 +00007896 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007897 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007898}
7899
Dan Gohmand858e902010-04-17 15:26:15 +00007900SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007901 MachineFunction &MF = DAG.getMachineFunction();
7902 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7903
Dan Gohman69de1932008-02-06 22:27:42 +00007904 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007905 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007906
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007907 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007908 // vastart just stores the address of the VarArgsFrameIndex slot into the
7909 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007910 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7911 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007912 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7913 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007914 }
7915
7916 // __va_list_tag:
7917 // gp_offset (0 - 6 * 8)
7918 // fp_offset (48 - 48 + 8 * 16)
7919 // overflow_arg_area (point to parameters coming in memory).
7920 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007921 SmallVector<SDValue, 8> MemOps;
7922 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007923 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007924 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007925 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7926 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007927 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007928 MemOps.push_back(Store);
7929
7930 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007931 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007932 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007933 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007934 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7935 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007936 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007937 MemOps.push_back(Store);
7938
7939 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007940 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007941 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007942 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7943 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007944 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7945 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007946 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007947 MemOps.push_back(Store);
7948
7949 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007950 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007951 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007952 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7953 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007954 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7955 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007956 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007957 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007958 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007959}
7960
Dan Gohmand858e902010-04-17 15:26:15 +00007961SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007962 assert(Subtarget->is64Bit() &&
7963 "LowerVAARG only handles 64-bit va_arg!");
7964 assert((Subtarget->isTargetLinux() ||
7965 Subtarget->isTargetDarwin()) &&
7966 "Unhandled target in LowerVAARG");
7967 assert(Op.getNode()->getNumOperands() == 4);
7968 SDValue Chain = Op.getOperand(0);
7969 SDValue SrcPtr = Op.getOperand(1);
7970 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7971 unsigned Align = Op.getConstantOperandVal(3);
7972 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007973
Dan Gohman320afb82010-10-12 18:00:49 +00007974 EVT ArgVT = Op.getNode()->getValueType(0);
7975 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7976 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7977 uint8_t ArgMode;
7978
7979 // Decide which area this value should be read from.
7980 // TODO: Implement the AMD64 ABI in its entirety. This simple
7981 // selection mechanism works only for the basic types.
7982 if (ArgVT == MVT::f80) {
7983 llvm_unreachable("va_arg for f80 not yet implemented");
7984 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7985 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7986 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7987 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7988 } else {
7989 llvm_unreachable("Unhandled argument type in LowerVAARG");
7990 }
7991
7992 if (ArgMode == 2) {
7993 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007994 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007995 !(DAG.getMachineFunction()
7996 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007997 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007998 }
7999
8000 // Insert VAARG_64 node into the DAG
8001 // VAARG_64 returns two values: Variable Argument Address, Chain
8002 SmallVector<SDValue, 11> InstOps;
8003 InstOps.push_back(Chain);
8004 InstOps.push_back(SrcPtr);
8005 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8006 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8007 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8008 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8009 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8010 VTs, &InstOps[0], InstOps.size(),
8011 MVT::i64,
8012 MachinePointerInfo(SV),
8013 /*Align=*/0,
8014 /*Volatile=*/false,
8015 /*ReadMem=*/true,
8016 /*WriteMem=*/true);
8017 Chain = VAARG.getValue(1);
8018
8019 // Load the next argument and return it
8020 return DAG.getLoad(ArgVT, dl,
8021 Chain,
8022 VAARG,
8023 MachinePointerInfo(),
8024 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008025}
8026
Dan Gohmand858e902010-04-17 15:26:15 +00008027SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008028 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008029 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008030 SDValue Chain = Op.getOperand(0);
8031 SDValue DstPtr = Op.getOperand(1);
8032 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008033 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8034 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008035 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008036
Chris Lattnere72f2022010-09-21 05:40:29 +00008037 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008038 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008039 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008040 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008041}
8042
Dan Gohman475871a2008-07-27 21:46:04 +00008043SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008044X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008045 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008046 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008047 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008048 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008049 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008050 case Intrinsic::x86_sse_comieq_ss:
8051 case Intrinsic::x86_sse_comilt_ss:
8052 case Intrinsic::x86_sse_comile_ss:
8053 case Intrinsic::x86_sse_comigt_ss:
8054 case Intrinsic::x86_sse_comige_ss:
8055 case Intrinsic::x86_sse_comineq_ss:
8056 case Intrinsic::x86_sse_ucomieq_ss:
8057 case Intrinsic::x86_sse_ucomilt_ss:
8058 case Intrinsic::x86_sse_ucomile_ss:
8059 case Intrinsic::x86_sse_ucomigt_ss:
8060 case Intrinsic::x86_sse_ucomige_ss:
8061 case Intrinsic::x86_sse_ucomineq_ss:
8062 case Intrinsic::x86_sse2_comieq_sd:
8063 case Intrinsic::x86_sse2_comilt_sd:
8064 case Intrinsic::x86_sse2_comile_sd:
8065 case Intrinsic::x86_sse2_comigt_sd:
8066 case Intrinsic::x86_sse2_comige_sd:
8067 case Intrinsic::x86_sse2_comineq_sd:
8068 case Intrinsic::x86_sse2_ucomieq_sd:
8069 case Intrinsic::x86_sse2_ucomilt_sd:
8070 case Intrinsic::x86_sse2_ucomile_sd:
8071 case Intrinsic::x86_sse2_ucomigt_sd:
8072 case Intrinsic::x86_sse2_ucomige_sd:
8073 case Intrinsic::x86_sse2_ucomineq_sd: {
8074 unsigned Opc = 0;
8075 ISD::CondCode CC = ISD::SETCC_INVALID;
8076 switch (IntNo) {
8077 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008078 case Intrinsic::x86_sse_comieq_ss:
8079 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008080 Opc = X86ISD::COMI;
8081 CC = ISD::SETEQ;
8082 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008083 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008084 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008085 Opc = X86ISD::COMI;
8086 CC = ISD::SETLT;
8087 break;
8088 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008089 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008090 Opc = X86ISD::COMI;
8091 CC = ISD::SETLE;
8092 break;
8093 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008094 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008095 Opc = X86ISD::COMI;
8096 CC = ISD::SETGT;
8097 break;
8098 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008099 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008100 Opc = X86ISD::COMI;
8101 CC = ISD::SETGE;
8102 break;
8103 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008104 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008105 Opc = X86ISD::COMI;
8106 CC = ISD::SETNE;
8107 break;
8108 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008109 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008110 Opc = X86ISD::UCOMI;
8111 CC = ISD::SETEQ;
8112 break;
8113 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008114 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008115 Opc = X86ISD::UCOMI;
8116 CC = ISD::SETLT;
8117 break;
8118 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008119 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008120 Opc = X86ISD::UCOMI;
8121 CC = ISD::SETLE;
8122 break;
8123 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008124 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008125 Opc = X86ISD::UCOMI;
8126 CC = ISD::SETGT;
8127 break;
8128 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008129 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008130 Opc = X86ISD::UCOMI;
8131 CC = ISD::SETGE;
8132 break;
8133 case Intrinsic::x86_sse_ucomineq_ss:
8134 case Intrinsic::x86_sse2_ucomineq_sd:
8135 Opc = X86ISD::UCOMI;
8136 CC = ISD::SETNE;
8137 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008138 }
Evan Cheng734503b2006-09-11 02:19:56 +00008139
Dan Gohman475871a2008-07-27 21:46:04 +00008140 SDValue LHS = Op.getOperand(1);
8141 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008142 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008143 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008144 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8145 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8146 DAG.getConstant(X86CC, MVT::i8), Cond);
8147 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008148 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008149 // ptest and testp intrinsics. The intrinsic these come from are designed to
8150 // return an integer value, not just an instruction so lower it to the ptest
8151 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008152 case Intrinsic::x86_sse41_ptestz:
8153 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008154 case Intrinsic::x86_sse41_ptestnzc:
8155 case Intrinsic::x86_avx_ptestz_256:
8156 case Intrinsic::x86_avx_ptestc_256:
8157 case Intrinsic::x86_avx_ptestnzc_256:
8158 case Intrinsic::x86_avx_vtestz_ps:
8159 case Intrinsic::x86_avx_vtestc_ps:
8160 case Intrinsic::x86_avx_vtestnzc_ps:
8161 case Intrinsic::x86_avx_vtestz_pd:
8162 case Intrinsic::x86_avx_vtestc_pd:
8163 case Intrinsic::x86_avx_vtestnzc_pd:
8164 case Intrinsic::x86_avx_vtestz_ps_256:
8165 case Intrinsic::x86_avx_vtestc_ps_256:
8166 case Intrinsic::x86_avx_vtestnzc_ps_256:
8167 case Intrinsic::x86_avx_vtestz_pd_256:
8168 case Intrinsic::x86_avx_vtestc_pd_256:
8169 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8170 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008171 unsigned X86CC = 0;
8172 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008173 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008174 case Intrinsic::x86_avx_vtestz_ps:
8175 case Intrinsic::x86_avx_vtestz_pd:
8176 case Intrinsic::x86_avx_vtestz_ps_256:
8177 case Intrinsic::x86_avx_vtestz_pd_256:
8178 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008179 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008180 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008181 // ZF = 1
8182 X86CC = X86::COND_E;
8183 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008184 case Intrinsic::x86_avx_vtestc_ps:
8185 case Intrinsic::x86_avx_vtestc_pd:
8186 case Intrinsic::x86_avx_vtestc_ps_256:
8187 case Intrinsic::x86_avx_vtestc_pd_256:
8188 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008189 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008190 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008191 // CF = 1
8192 X86CC = X86::COND_B;
8193 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008194 case Intrinsic::x86_avx_vtestnzc_ps:
8195 case Intrinsic::x86_avx_vtestnzc_pd:
8196 case Intrinsic::x86_avx_vtestnzc_ps_256:
8197 case Intrinsic::x86_avx_vtestnzc_pd_256:
8198 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008199 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008200 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008201 // ZF and CF = 0
8202 X86CC = X86::COND_A;
8203 break;
8204 }
Eric Christopherfd179292009-08-27 18:07:15 +00008205
Eric Christopher71c67532009-07-29 00:28:05 +00008206 SDValue LHS = Op.getOperand(1);
8207 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008208 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8209 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008210 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8211 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8212 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008213 }
Evan Cheng5759f972008-05-04 09:15:50 +00008214
8215 // Fix vector shift instructions where the last operand is a non-immediate
8216 // i32 value.
8217 case Intrinsic::x86_sse2_pslli_w:
8218 case Intrinsic::x86_sse2_pslli_d:
8219 case Intrinsic::x86_sse2_pslli_q:
8220 case Intrinsic::x86_sse2_psrli_w:
8221 case Intrinsic::x86_sse2_psrli_d:
8222 case Intrinsic::x86_sse2_psrli_q:
8223 case Intrinsic::x86_sse2_psrai_w:
8224 case Intrinsic::x86_sse2_psrai_d:
8225 case Intrinsic::x86_mmx_pslli_w:
8226 case Intrinsic::x86_mmx_pslli_d:
8227 case Intrinsic::x86_mmx_pslli_q:
8228 case Intrinsic::x86_mmx_psrli_w:
8229 case Intrinsic::x86_mmx_psrli_d:
8230 case Intrinsic::x86_mmx_psrli_q:
8231 case Intrinsic::x86_mmx_psrai_w:
8232 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008233 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008234 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008235 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008236
8237 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008238 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008239 switch (IntNo) {
8240 case Intrinsic::x86_sse2_pslli_w:
8241 NewIntNo = Intrinsic::x86_sse2_psll_w;
8242 break;
8243 case Intrinsic::x86_sse2_pslli_d:
8244 NewIntNo = Intrinsic::x86_sse2_psll_d;
8245 break;
8246 case Intrinsic::x86_sse2_pslli_q:
8247 NewIntNo = Intrinsic::x86_sse2_psll_q;
8248 break;
8249 case Intrinsic::x86_sse2_psrli_w:
8250 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8251 break;
8252 case Intrinsic::x86_sse2_psrli_d:
8253 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8254 break;
8255 case Intrinsic::x86_sse2_psrli_q:
8256 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8257 break;
8258 case Intrinsic::x86_sse2_psrai_w:
8259 NewIntNo = Intrinsic::x86_sse2_psra_w;
8260 break;
8261 case Intrinsic::x86_sse2_psrai_d:
8262 NewIntNo = Intrinsic::x86_sse2_psra_d;
8263 break;
8264 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008265 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008266 switch (IntNo) {
8267 case Intrinsic::x86_mmx_pslli_w:
8268 NewIntNo = Intrinsic::x86_mmx_psll_w;
8269 break;
8270 case Intrinsic::x86_mmx_pslli_d:
8271 NewIntNo = Intrinsic::x86_mmx_psll_d;
8272 break;
8273 case Intrinsic::x86_mmx_pslli_q:
8274 NewIntNo = Intrinsic::x86_mmx_psll_q;
8275 break;
8276 case Intrinsic::x86_mmx_psrli_w:
8277 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8278 break;
8279 case Intrinsic::x86_mmx_psrli_d:
8280 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8281 break;
8282 case Intrinsic::x86_mmx_psrli_q:
8283 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8284 break;
8285 case Intrinsic::x86_mmx_psrai_w:
8286 NewIntNo = Intrinsic::x86_mmx_psra_w;
8287 break;
8288 case Intrinsic::x86_mmx_psrai_d:
8289 NewIntNo = Intrinsic::x86_mmx_psra_d;
8290 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008291 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008292 }
8293 break;
8294 }
8295 }
Mon P Wangefa42202009-09-03 19:56:25 +00008296
8297 // The vector shift intrinsics with scalars uses 32b shift amounts but
8298 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8299 // to be zero.
8300 SDValue ShOps[4];
8301 ShOps[0] = ShAmt;
8302 ShOps[1] = DAG.getConstant(0, MVT::i32);
8303 if (ShAmtVT == MVT::v4i32) {
8304 ShOps[2] = DAG.getUNDEF(MVT::i32);
8305 ShOps[3] = DAG.getUNDEF(MVT::i32);
8306 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8307 } else {
8308 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008309// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008310 }
8311
Owen Andersone50ed302009-08-10 22:56:29 +00008312 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008313 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008314 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008315 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008316 Op.getOperand(1), ShAmt);
8317 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008318 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008319}
Evan Cheng72261582005-12-20 06:22:03 +00008320
Dan Gohmand858e902010-04-17 15:26:15 +00008321SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8322 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008323 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8324 MFI->setReturnAddressIsTaken(true);
8325
Bill Wendling64e87322009-01-16 19:25:27 +00008326 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008327 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008328
8329 if (Depth > 0) {
8330 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8331 SDValue Offset =
8332 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008333 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008334 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008335 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008336 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008337 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008338 }
8339
8340 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008341 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008342 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008343 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008344}
8345
Dan Gohmand858e902010-04-17 15:26:15 +00008346SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008347 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8348 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008349
Owen Andersone50ed302009-08-10 22:56:29 +00008350 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008351 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008352 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8353 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008354 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008355 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008356 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8357 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008358 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008359 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008360}
8361
Dan Gohman475871a2008-07-27 21:46:04 +00008362SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008363 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008364 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008365}
8366
Dan Gohmand858e902010-04-17 15:26:15 +00008367SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008368 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008369 SDValue Chain = Op.getOperand(0);
8370 SDValue Offset = Op.getOperand(1);
8371 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008372 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008373
Dan Gohmand8816272010-08-11 18:14:00 +00008374 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8375 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8376 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008377 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008378
Dan Gohmand8816272010-08-11 18:14:00 +00008379 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8380 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008381 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008382 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8383 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008384 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008385 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008386
Dale Johannesene4d209d2009-02-03 20:21:25 +00008387 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008388 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008389 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008390}
8391
Dan Gohman475871a2008-07-27 21:46:04 +00008392SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008393 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008394 SDValue Root = Op.getOperand(0);
8395 SDValue Trmp = Op.getOperand(1); // trampoline
8396 SDValue FPtr = Op.getOperand(2); // nested function
8397 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008398 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008399
Dan Gohman69de1932008-02-06 22:27:42 +00008400 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008401
8402 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008403 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008404
8405 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008406 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8407 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008408
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008409 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8410 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008411
8412 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8413
8414 // Load the pointer to the nested function into R11.
8415 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008416 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008417 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008418 Addr, MachinePointerInfo(TrmpAddr),
8419 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008420
Owen Anderson825b72b2009-08-11 20:47:22 +00008421 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8422 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008423 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8424 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008425 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008426
8427 // Load the 'nest' parameter value into R10.
8428 // R10 is specified in X86CallingConv.td
8429 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008430 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8431 DAG.getConstant(10, MVT::i64));
8432 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008433 Addr, MachinePointerInfo(TrmpAddr, 10),
8434 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008435
Owen Anderson825b72b2009-08-11 20:47:22 +00008436 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8437 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008438 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8439 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008440 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008441
8442 // Jump to the nested function.
8443 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008444 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8445 DAG.getConstant(20, MVT::i64));
8446 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008447 Addr, MachinePointerInfo(TrmpAddr, 20),
8448 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008449
8450 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008451 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8452 DAG.getConstant(22, MVT::i64));
8453 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008454 MachinePointerInfo(TrmpAddr, 22),
8455 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008456
Dan Gohman475871a2008-07-27 21:46:04 +00008457 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008458 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008459 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008460 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008461 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008462 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008463 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008464 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008465
8466 switch (CC) {
8467 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008468 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008469 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008470 case CallingConv::X86_StdCall: {
8471 // Pass 'nest' parameter in ECX.
8472 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008473 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008474
8475 // Check that ECX wasn't needed by an 'inreg' parameter.
8476 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008477 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008478
Chris Lattner58d74912008-03-12 17:45:29 +00008479 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008480 unsigned InRegCount = 0;
8481 unsigned Idx = 1;
8482
8483 for (FunctionType::param_iterator I = FTy->param_begin(),
8484 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008485 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008486 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008487 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008488
8489 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008490 report_fatal_error("Nest register in use - reduce number of inreg"
8491 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008492 }
8493 }
8494 break;
8495 }
8496 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008497 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008498 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008499 // Pass 'nest' parameter in EAX.
8500 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008501 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008502 break;
8503 }
8504
Dan Gohman475871a2008-07-27 21:46:04 +00008505 SDValue OutChains[4];
8506 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008507
Owen Anderson825b72b2009-08-11 20:47:22 +00008508 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8509 DAG.getConstant(10, MVT::i32));
8510 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008511
Chris Lattnera62fe662010-02-05 19:20:30 +00008512 // This is storing the opcode for MOV32ri.
8513 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008514 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008515 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008516 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008517 Trmp, MachinePointerInfo(TrmpAddr),
8518 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008519
Owen Anderson825b72b2009-08-11 20:47:22 +00008520 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8521 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008522 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8523 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008524 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008525
Chris Lattnera62fe662010-02-05 19:20:30 +00008526 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008527 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8528 DAG.getConstant(5, MVT::i32));
8529 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008530 MachinePointerInfo(TrmpAddr, 5),
8531 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008532
Owen Anderson825b72b2009-08-11 20:47:22 +00008533 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8534 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008535 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8536 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008537 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008538
Dan Gohman475871a2008-07-27 21:46:04 +00008539 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008540 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008541 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008542 }
8543}
8544
Dan Gohmand858e902010-04-17 15:26:15 +00008545SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8546 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008547 /*
8548 The rounding mode is in bits 11:10 of FPSR, and has the following
8549 settings:
8550 00 Round to nearest
8551 01 Round to -inf
8552 10 Round to +inf
8553 11 Round to 0
8554
8555 FLT_ROUNDS, on the other hand, expects the following:
8556 -1 Undefined
8557 0 Round to 0
8558 1 Round to nearest
8559 2 Round to +inf
8560 3 Round to -inf
8561
8562 To perform the conversion, we do:
8563 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8564 */
8565
8566 MachineFunction &MF = DAG.getMachineFunction();
8567 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008568 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008569 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008570 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008571 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008572
8573 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008574 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008575 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008576
Michael J. Spencerec38de22010-10-10 22:04:20 +00008577
Chris Lattner2156b792010-09-22 01:11:26 +00008578 MachineMemOperand *MMO =
8579 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8580 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008581
Chris Lattner2156b792010-09-22 01:11:26 +00008582 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8583 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8584 DAG.getVTList(MVT::Other),
8585 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008586
8587 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008588 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008589 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008590
8591 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008592 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008593 DAG.getNode(ISD::SRL, DL, MVT::i16,
8594 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008595 CWD, DAG.getConstant(0x800, MVT::i16)),
8596 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008597 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008598 DAG.getNode(ISD::SRL, DL, MVT::i16,
8599 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008600 CWD, DAG.getConstant(0x400, MVT::i16)),
8601 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008602
Dan Gohman475871a2008-07-27 21:46:04 +00008603 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008604 DAG.getNode(ISD::AND, DL, MVT::i16,
8605 DAG.getNode(ISD::ADD, DL, MVT::i16,
8606 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008607 DAG.getConstant(1, MVT::i16)),
8608 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008609
8610
Duncan Sands83ec4b62008-06-06 12:08:01 +00008611 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008612 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008613}
8614
Dan Gohmand858e902010-04-17 15:26:15 +00008615SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008616 EVT VT = Op.getValueType();
8617 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008618 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008619 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008620
8621 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008622 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008623 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008624 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008625 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008626 }
Evan Cheng18efe262007-12-14 02:13:44 +00008627
Evan Cheng152804e2007-12-14 08:30:15 +00008628 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008629 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008630 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008631
8632 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008633 SDValue Ops[] = {
8634 Op,
8635 DAG.getConstant(NumBits+NumBits-1, OpVT),
8636 DAG.getConstant(X86::COND_E, MVT::i8),
8637 Op.getValue(1)
8638 };
8639 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008640
8641 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008642 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008643
Owen Anderson825b72b2009-08-11 20:47:22 +00008644 if (VT == MVT::i8)
8645 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008646 return Op;
8647}
8648
Dan Gohmand858e902010-04-17 15:26:15 +00008649SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008650 EVT VT = Op.getValueType();
8651 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008652 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008653 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008654
8655 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008656 if (VT == MVT::i8) {
8657 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008658 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008659 }
Evan Cheng152804e2007-12-14 08:30:15 +00008660
8661 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008662 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008663 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008664
8665 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008666 SDValue Ops[] = {
8667 Op,
8668 DAG.getConstant(NumBits, OpVT),
8669 DAG.getConstant(X86::COND_E, MVT::i8),
8670 Op.getValue(1)
8671 };
8672 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008673
Owen Anderson825b72b2009-08-11 20:47:22 +00008674 if (VT == MVT::i8)
8675 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008676 return Op;
8677}
8678
Dan Gohmand858e902010-04-17 15:26:15 +00008679SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008680 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008681 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008682 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008683
Mon P Wangaf9b9522008-12-18 21:42:19 +00008684 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8685 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8686 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8687 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8688 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8689 //
8690 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8691 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8692 // return AloBlo + AloBhi + AhiBlo;
8693
8694 SDValue A = Op.getOperand(0);
8695 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008696
Dale Johannesene4d209d2009-02-03 20:21:25 +00008697 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008698 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8699 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008700 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008701 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8702 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008703 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008704 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008705 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008706 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008707 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008708 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008709 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008710 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008711 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008712 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008713 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8714 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008715 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008716 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8717 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008718 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8719 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008720 return Res;
8721}
8722
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008723SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8724 EVT VT = Op.getValueType();
8725 DebugLoc dl = Op.getDebugLoc();
8726 SDValue R = Op.getOperand(0);
8727
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008728 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008729
Nate Begeman51409212010-07-28 00:21:48 +00008730 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8731
8732 if (VT == MVT::v4i32) {
8733 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8734 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8735 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8736
8737 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008738
Nate Begeman51409212010-07-28 00:21:48 +00008739 std::vector<Constant*> CV(4, CI);
8740 Constant *C = ConstantVector::get(CV);
8741 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8742 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008743 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008744 false, false, 16);
8745
8746 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008747 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008748 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8749 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8750 }
8751 if (VT == MVT::v16i8) {
8752 // a = a << 5;
8753 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8754 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8755 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8756
8757 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8758 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8759
8760 std::vector<Constant*> CVM1(16, CM1);
8761 std::vector<Constant*> CVM2(16, CM2);
8762 Constant *C = ConstantVector::get(CVM1);
8763 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8764 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008765 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008766 false, false, 16);
8767
8768 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8769 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8770 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8771 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8772 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008773 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008774 // a += a
8775 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008776
Nate Begeman51409212010-07-28 00:21:48 +00008777 C = ConstantVector::get(CVM2);
8778 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8779 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008780 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008781 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008782
Nate Begeman51409212010-07-28 00:21:48 +00008783 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8784 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8785 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8786 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8787 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008788 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008789 // a += a
8790 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008791
Nate Begeman51409212010-07-28 00:21:48 +00008792 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008793 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008794 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8795 return R;
8796 }
8797 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008798}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008799
Dan Gohmand858e902010-04-17 15:26:15 +00008800SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008801 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8802 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008803 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8804 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008805 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008806 SDValue LHS = N->getOperand(0);
8807 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008808 unsigned BaseOp = 0;
8809 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008810 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008811 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008812 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008813 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008814 // A subtract of one will be selected as a INC. Note that INC doesn't
8815 // set CF, so we can't do this for UADDO.
8816 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8817 if (C->getAPIntValue() == 1) {
8818 BaseOp = X86ISD::INC;
8819 Cond = X86::COND_O;
8820 break;
8821 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008822 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008823 Cond = X86::COND_O;
8824 break;
8825 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008826 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008827 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008828 break;
8829 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008830 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8831 // set CF, so we can't do this for USUBO.
8832 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8833 if (C->getAPIntValue() == 1) {
8834 BaseOp = X86ISD::DEC;
8835 Cond = X86::COND_O;
8836 break;
8837 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008838 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008839 Cond = X86::COND_O;
8840 break;
8841 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008842 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008843 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008844 break;
8845 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008846 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008847 Cond = X86::COND_O;
8848 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008849 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8850 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8851 MVT::i32);
8852 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008853
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008854 SDValue SetCC =
8855 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8856 DAG.getConstant(X86::COND_O, MVT::i32),
8857 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008858
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008859 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8860 return Sum;
8861 }
Bill Wendling74c37652008-12-09 22:08:41 +00008862 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008863
Bill Wendling61edeb52008-12-02 01:06:39 +00008864 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008865 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008866 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008867
Bill Wendling61edeb52008-12-02 01:06:39 +00008868 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008869 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8870 DAG.getConstant(Cond, MVT::i32),
8871 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008872
Bill Wendling61edeb52008-12-02 01:06:39 +00008873 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8874 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008875}
8876
Eric Christopher9a9d2752010-07-22 02:48:34 +00008877SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8878 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008879
Eric Christopherb6729dc2010-08-04 23:03:04 +00008880 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008881 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008882 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008883 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008884 SDValue Ops[] = {
8885 DAG.getRegister(X86::ESP, MVT::i32), // Base
8886 DAG.getTargetConstant(1, MVT::i8), // Scale
8887 DAG.getRegister(0, MVT::i32), // Index
8888 DAG.getTargetConstant(0, MVT::i32), // Disp
8889 DAG.getRegister(0, MVT::i32), // Segment.
8890 Zero,
8891 Chain
8892 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008893 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008894 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8895 array_lengthof(Ops));
8896 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008897 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008898
Eric Christopher9a9d2752010-07-22 02:48:34 +00008899 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008900 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008901 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008902
Chris Lattner132929a2010-08-14 17:26:09 +00008903 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8904 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8905 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8906 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008907
Chris Lattner132929a2010-08-14 17:26:09 +00008908 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8909 if (!Op1 && !Op2 && !Op3 && Op4)
8910 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008911
Chris Lattner132929a2010-08-14 17:26:09 +00008912 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8913 if (Op1 && !Op2 && !Op3 && !Op4)
8914 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008915
8916 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008917 // (MFENCE)>;
8918 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008919}
8920
Dan Gohmand858e902010-04-17 15:26:15 +00008921SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008922 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008923 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008924 unsigned Reg = 0;
8925 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008926 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008927 default:
8928 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008929 case MVT::i8: Reg = X86::AL; size = 1; break;
8930 case MVT::i16: Reg = X86::AX; size = 2; break;
8931 case MVT::i32: Reg = X86::EAX; size = 4; break;
8932 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008933 assert(Subtarget->is64Bit() && "Node not type legal!");
8934 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008935 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008936 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008937 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008938 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008939 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008940 Op.getOperand(1),
8941 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008942 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008943 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008944 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008945 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8946 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8947 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008948 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008949 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008950 return cpOut;
8951}
8952
Duncan Sands1607f052008-12-01 11:39:25 +00008953SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008954 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008955 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008956 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008957 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008958 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008959 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008960 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8961 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008962 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008963 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8964 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008965 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008966 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008967 rdx.getValue(1)
8968 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008969 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008970}
8971
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008972SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008973 SelectionDAG &DAG) const {
8974 EVT SrcVT = Op.getOperand(0).getValueType();
8975 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008976 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8977 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008978 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008979 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008980 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008981 // i64 <=> MMX conversions are Legal.
8982 if (SrcVT==MVT::i64 && DstVT.isVector())
8983 return Op;
8984 if (DstVT==MVT::i64 && SrcVT.isVector())
8985 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008986 // MMX <=> MMX conversions are Legal.
8987 if (SrcVT.isVector() && DstVT.isVector())
8988 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008989 // All other conversions need to be expanded.
8990 return SDValue();
8991}
Chris Lattner5b856542010-12-20 00:59:46 +00008992
Dan Gohmand858e902010-04-17 15:26:15 +00008993SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008994 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008995 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008996 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008997 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008998 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008999 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009000 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009001 Node->getOperand(0),
9002 Node->getOperand(1), negOp,
9003 cast<AtomicSDNode>(Node)->getSrcValue(),
9004 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009005}
9006
Chris Lattner5b856542010-12-20 00:59:46 +00009007static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9008 EVT VT = Op.getNode()->getValueType(0);
9009
9010 // Let legalize expand this if it isn't a legal type yet.
9011 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9012 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009013
Chris Lattner5b856542010-12-20 00:59:46 +00009014 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009015
Chris Lattner5b856542010-12-20 00:59:46 +00009016 unsigned Opc;
9017 bool ExtraOp = false;
9018 switch (Op.getOpcode()) {
9019 default: assert(0 && "Invalid code");
9020 case ISD::ADDC: Opc = X86ISD::ADD; break;
9021 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9022 case ISD::SUBC: Opc = X86ISD::SUB; break;
9023 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9024 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009025
Chris Lattner5b856542010-12-20 00:59:46 +00009026 if (!ExtraOp)
9027 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9028 Op.getOperand(1));
9029 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9030 Op.getOperand(1), Op.getOperand(2));
9031}
9032
Evan Cheng0db9fe62006-04-25 20:13:52 +00009033/// LowerOperation - Provide custom lowering hooks for some operations.
9034///
Dan Gohmand858e902010-04-17 15:26:15 +00009035SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009036 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009037 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009038 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009039 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9040 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009041 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009042 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009043 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9044 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9045 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009046 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009047 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009048 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9049 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9050 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009051 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009052 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009053 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009054 case ISD::SHL_PARTS:
9055 case ISD::SRA_PARTS:
9056 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9057 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009058 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009059 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009060 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009061 case ISD::FABS: return LowerFABS(Op, DAG);
9062 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009063 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009064 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009065 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009066 case ISD::SELECT: return LowerSELECT(Op, DAG);
9067 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009068 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009069 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009070 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009071 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009072 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009073 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9074 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009075 case ISD::FRAME_TO_ARGS_OFFSET:
9076 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009077 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009078 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009079 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009080 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009081 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9082 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009083 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009084 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009085 case ISD::SADDO:
9086 case ISD::UADDO:
9087 case ISD::SSUBO:
9088 case ISD::USUBO:
9089 case ISD::SMULO:
9090 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009091 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009092 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009093 case ISD::ADDC:
9094 case ISD::ADDE:
9095 case ISD::SUBC:
9096 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009097 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009098}
9099
Duncan Sands1607f052008-12-01 11:39:25 +00009100void X86TargetLowering::
9101ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009102 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009103 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009104 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009105 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009106
9107 SDValue Chain = Node->getOperand(0);
9108 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009109 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009110 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009111 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009112 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009113 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009114 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009115 SDValue Result =
9116 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9117 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009118 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009119 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009120 Results.push_back(Result.getValue(2));
9121}
9122
Duncan Sands126d9072008-07-04 11:47:58 +00009123/// ReplaceNodeResults - Replace a node with an illegal result type
9124/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009125void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9126 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009127 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009128 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009129 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009130 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009131 assert(false && "Do not know how to custom type legalize this operation!");
9132 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009133 case ISD::ADDC:
9134 case ISD::ADDE:
9135 case ISD::SUBC:
9136 case ISD::SUBE:
9137 // We don't want to expand or promote these.
9138 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009139 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009140 std::pair<SDValue,SDValue> Vals =
9141 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009142 SDValue FIST = Vals.first, StackSlot = Vals.second;
9143 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009144 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009145 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009146 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9147 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009148 }
9149 return;
9150 }
9151 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009152 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009153 SDValue TheChain = N->getOperand(0);
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 eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009156 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009157 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009158 eax.getValue(2));
9159 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9160 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009161 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009162 Results.push_back(edx.getValue(1));
9163 return;
9164 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009165 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009166 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009167 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009168 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009169 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9170 DAG.getConstant(0, MVT::i32));
9171 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9172 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009173 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9174 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009175 cpInL.getValue(1));
9176 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009177 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9178 DAG.getConstant(0, MVT::i32));
9179 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9180 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009181 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009182 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009183 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009184 swapInL.getValue(1));
9185 SDValue Ops[] = { swapInH.getValue(0),
9186 N->getOperand(1),
9187 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009188 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009189 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9190 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9191 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009192 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009193 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009194 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009195 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009196 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009197 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009198 Results.push_back(cpOutH.getValue(1));
9199 return;
9200 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009201 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009202 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9203 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009204 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009205 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9206 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009207 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009208 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9209 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009210 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009211 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9212 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009213 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009214 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9215 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009216 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009217 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9218 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009219 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009220 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9221 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009222 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009223}
9224
Evan Cheng72261582005-12-20 06:22:03 +00009225const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9226 switch (Opcode) {
9227 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009228 case X86ISD::BSF: return "X86ISD::BSF";
9229 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009230 case X86ISD::SHLD: return "X86ISD::SHLD";
9231 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009232 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009233 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009234 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009235 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009236 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009237 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009238 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9239 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9240 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009241 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009242 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009243 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009244 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009245 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009246 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009247 case X86ISD::COMI: return "X86ISD::COMI";
9248 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009249 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009250 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009251 case X86ISD::CMOV: return "X86ISD::CMOV";
9252 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009253 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009254 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9255 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009256 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009257 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009258 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009259 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009260 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009261 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9262 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009263 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009264 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009265 case X86ISD::PANDN: return "X86ISD::PANDN";
9266 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9267 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9268 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009269 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009270 case X86ISD::FMAX: return "X86ISD::FMAX";
9271 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009272 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9273 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009274 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009275 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009276 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009277 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009278 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009279 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9280 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009281 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9282 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9283 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9284 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9285 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9286 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009287 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9288 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009289 case X86ISD::VSHL: return "X86ISD::VSHL";
9290 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009291 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9292 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9293 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9294 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9295 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9296 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9297 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9298 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9299 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9300 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009301 case X86ISD::ADD: return "X86ISD::ADD";
9302 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009303 case X86ISD::ADC: return "X86ISD::ADC";
9304 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009305 case X86ISD::SMUL: return "X86ISD::SMUL";
9306 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009307 case X86ISD::INC: return "X86ISD::INC";
9308 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009309 case X86ISD::OR: return "X86ISD::OR";
9310 case X86ISD::XOR: return "X86ISD::XOR";
9311 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009312 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009313 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009314 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009315 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9316 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9317 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9318 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9319 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9320 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9321 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9322 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9323 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009324 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009325 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009326 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009327 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9328 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009329 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9330 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9331 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9332 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9333 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9334 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9335 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9336 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9337 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009338 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9339 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9340 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9341 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009342 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9343 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9344 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9345 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9346 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9347 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9348 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9349 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9350 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9351 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009352 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009353 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009354 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009355 }
9356}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009357
Chris Lattnerc9addb72007-03-30 23:15:24 +00009358// isLegalAddressingMode - Return true if the addressing mode represented
9359// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009360bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009361 const Type *Ty) const {
9362 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009363 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009364 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009365
Chris Lattnerc9addb72007-03-30 23:15:24 +00009366 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009367 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009368 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009369
Chris Lattnerc9addb72007-03-30 23:15:24 +00009370 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009371 unsigned GVFlags =
9372 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009373
Chris Lattnerdfed4132009-07-10 07:38:24 +00009374 // If a reference to this global requires an extra load, we can't fold it.
9375 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009376 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009377
Chris Lattnerdfed4132009-07-10 07:38:24 +00009378 // If BaseGV requires a register for the PIC base, we cannot also have a
9379 // BaseReg specified.
9380 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009381 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009382
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009383 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009384 if ((M != CodeModel::Small || R != Reloc::Static) &&
9385 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009386 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009388
Chris Lattnerc9addb72007-03-30 23:15:24 +00009389 switch (AM.Scale) {
9390 case 0:
9391 case 1:
9392 case 2:
9393 case 4:
9394 case 8:
9395 // These scales always work.
9396 break;
9397 case 3:
9398 case 5:
9399 case 9:
9400 // These scales are formed with basereg+scalereg. Only accept if there is
9401 // no basereg yet.
9402 if (AM.HasBaseReg)
9403 return false;
9404 break;
9405 default: // Other stuff never works.
9406 return false;
9407 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009408
Chris Lattnerc9addb72007-03-30 23:15:24 +00009409 return true;
9410}
9411
9412
Evan Cheng2bd122c2007-10-26 01:56:11 +00009413bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009414 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009415 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009416 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9417 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009418 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009419 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009420 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009421}
9422
Owen Andersone50ed302009-08-10 22:56:29 +00009423bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009424 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009425 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009426 unsigned NumBits1 = VT1.getSizeInBits();
9427 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009428 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009429 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009430 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009431}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009432
Dan Gohman97121ba2009-04-08 00:15:30 +00009433bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009434 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009435 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009436}
9437
Owen Andersone50ed302009-08-10 22:56:29 +00009438bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009439 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009440 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009441}
9442
Owen Andersone50ed302009-08-10 22:56:29 +00009443bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009444 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009445 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009446}
9447
Evan Cheng60c07e12006-07-05 22:17:51 +00009448/// isShuffleMaskLegal - Targets can use this to indicate that they only
9449/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9450/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9451/// are assumed to be legal.
9452bool
Eric Christopherfd179292009-08-27 18:07:15 +00009453X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009454 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009455 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009456 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009457 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009458
Nate Begemana09008b2009-10-19 02:17:23 +00009459 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009460 return (VT.getVectorNumElements() == 2 ||
9461 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9462 isMOVLMask(M, VT) ||
9463 isSHUFPMask(M, VT) ||
9464 isPSHUFDMask(M, VT) ||
9465 isPSHUFHWMask(M, VT) ||
9466 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009467 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009468 isUNPCKLMask(M, VT) ||
9469 isUNPCKHMask(M, VT) ||
9470 isUNPCKL_v_undef_Mask(M, VT) ||
9471 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009472}
9473
Dan Gohman7d8143f2008-04-09 20:09:42 +00009474bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009475X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009476 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009477 unsigned NumElts = VT.getVectorNumElements();
9478 // FIXME: This collection of masks seems suspect.
9479 if (NumElts == 2)
9480 return true;
9481 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9482 return (isMOVLMask(Mask, VT) ||
9483 isCommutedMOVLMask(Mask, VT, true) ||
9484 isSHUFPMask(Mask, VT) ||
9485 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009486 }
9487 return false;
9488}
9489
9490//===----------------------------------------------------------------------===//
9491// X86 Scheduler Hooks
9492//===----------------------------------------------------------------------===//
9493
Mon P Wang63307c32008-05-05 19:05:59 +00009494// private utility function
9495MachineBasicBlock *
9496X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9497 MachineBasicBlock *MBB,
9498 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009499 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009500 unsigned LoadOpc,
9501 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009502 unsigned notOpc,
9503 unsigned EAXreg,
9504 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009505 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009506 // For the atomic bitwise operator, we generate
9507 // thisMBB:
9508 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009509 // ld t1 = [bitinstr.addr]
9510 // op t2 = t1, [bitinstr.val]
9511 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009512 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9513 // bz newMBB
9514 // fallthrough -->nextMBB
9515 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9516 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009517 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009518 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009519
Mon P Wang63307c32008-05-05 19:05:59 +00009520 /// First build the CFG
9521 MachineFunction *F = MBB->getParent();
9522 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009523 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9524 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9525 F->insert(MBBIter, newMBB);
9526 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009527
Dan Gohman14152b42010-07-06 20:24:04 +00009528 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9529 nextMBB->splice(nextMBB->begin(), thisMBB,
9530 llvm::next(MachineBasicBlock::iterator(bInstr)),
9531 thisMBB->end());
9532 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009533
Mon P Wang63307c32008-05-05 19:05:59 +00009534 // Update thisMBB to fall through to newMBB
9535 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009536
Mon P Wang63307c32008-05-05 19:05:59 +00009537 // newMBB jumps to itself and fall through to nextMBB
9538 newMBB->addSuccessor(nextMBB);
9539 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009540
Mon P Wang63307c32008-05-05 19:05:59 +00009541 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009542 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009543 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009544 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009545 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009546 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009547 int numArgs = bInstr->getNumOperands() - 1;
9548 for (int i=0; i < numArgs; ++i)
9549 argOpers[i] = &bInstr->getOperand(i+1);
9550
9551 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009552 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009553 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009554
Dale Johannesen140be2d2008-08-19 18:47:28 +00009555 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009556 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009557 for (int i=0; i <= lastAddrIndx; ++i)
9558 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009559
Dale Johannesen140be2d2008-08-19 18:47:28 +00009560 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009561 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009562 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009563 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009564 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009565 tt = t1;
9566
Dale Johannesen140be2d2008-08-19 18:47:28 +00009567 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009568 assert((argOpers[valArgIndx]->isReg() ||
9569 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009570 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009571 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009572 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009573 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009574 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009575 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009576 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009577
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009578 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009579 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009580
Dale Johannesene4d209d2009-02-03 20:21:25 +00009581 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009582 for (int i=0; i <= lastAddrIndx; ++i)
9583 (*MIB).addOperand(*argOpers[i]);
9584 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009585 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009586 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9587 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009588
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009589 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009590 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009591
Mon P Wang63307c32008-05-05 19:05:59 +00009592 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009593 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009594
Dan Gohman14152b42010-07-06 20:24:04 +00009595 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009596 return nextMBB;
9597}
9598
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009599// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009600MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009601X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9602 MachineBasicBlock *MBB,
9603 unsigned regOpcL,
9604 unsigned regOpcH,
9605 unsigned immOpcL,
9606 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009607 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009608 // For the atomic bitwise operator, we generate
9609 // thisMBB (instructions are in pairs, except cmpxchg8b)
9610 // ld t1,t2 = [bitinstr.addr]
9611 // newMBB:
9612 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9613 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009614 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009615 // mov ECX, EBX <- t5, t6
9616 // mov EAX, EDX <- t1, t2
9617 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9618 // mov t3, t4 <- EAX, EDX
9619 // bz newMBB
9620 // result in out1, out2
9621 // fallthrough -->nextMBB
9622
9623 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9624 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009625 const unsigned NotOpc = X86::NOT32r;
9626 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9627 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9628 MachineFunction::iterator MBBIter = MBB;
9629 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009630
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009631 /// First build the CFG
9632 MachineFunction *F = MBB->getParent();
9633 MachineBasicBlock *thisMBB = MBB;
9634 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9635 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9636 F->insert(MBBIter, newMBB);
9637 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009638
Dan Gohman14152b42010-07-06 20:24:04 +00009639 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9640 nextMBB->splice(nextMBB->begin(), thisMBB,
9641 llvm::next(MachineBasicBlock::iterator(bInstr)),
9642 thisMBB->end());
9643 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009644
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009645 // Update thisMBB to fall through to newMBB
9646 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009647
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009648 // newMBB jumps to itself and fall through to nextMBB
9649 newMBB->addSuccessor(nextMBB);
9650 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009651
Dale Johannesene4d209d2009-02-03 20:21:25 +00009652 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009653 // Insert instructions into newMBB based on incoming instruction
9654 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009655 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009656 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009657 MachineOperand& dest1Oper = bInstr->getOperand(0);
9658 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009659 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9660 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009661 argOpers[i] = &bInstr->getOperand(i+2);
9662
Dan Gohman71ea4e52010-05-14 21:01:44 +00009663 // We use some of the operands multiple times, so conservatively just
9664 // clear any kill flags that might be present.
9665 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9666 argOpers[i]->setIsKill(false);
9667 }
9668
Evan Chengad5b52f2010-01-08 19:14:57 +00009669 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009670 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009671
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009672 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009673 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009674 for (int i=0; i <= lastAddrIndx; ++i)
9675 (*MIB).addOperand(*argOpers[i]);
9676 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009677 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009678 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009679 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009680 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009681 MachineOperand newOp3 = *(argOpers[3]);
9682 if (newOp3.isImm())
9683 newOp3.setImm(newOp3.getImm()+4);
9684 else
9685 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009686 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009687 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009688
9689 // t3/4 are defined later, at the bottom of the loop
9690 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9691 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009692 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009693 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009694 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009695 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9696
Evan Cheng306b4ca2010-01-08 23:41:50 +00009697 // The subsequent operations should be using the destination registers of
9698 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009699 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009700 t1 = F->getRegInfo().createVirtualRegister(RC);
9701 t2 = F->getRegInfo().createVirtualRegister(RC);
9702 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9703 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009704 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009705 t1 = dest1Oper.getReg();
9706 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009707 }
9708
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009709 int valArgIndx = lastAddrIndx + 1;
9710 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009711 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009712 "invalid operand");
9713 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9714 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009715 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009716 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009717 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009718 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009719 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009720 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009721 (*MIB).addOperand(*argOpers[valArgIndx]);
9722 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009723 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009724 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009725 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009726 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009727 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009728 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009729 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009730 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009731 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009732 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009733
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009734 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009735 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009736 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009737 MIB.addReg(t2);
9738
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009739 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009740 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009741 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009742 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009743
Dale Johannesene4d209d2009-02-03 20:21:25 +00009744 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009745 for (int i=0; i <= lastAddrIndx; ++i)
9746 (*MIB).addOperand(*argOpers[i]);
9747
9748 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009749 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9750 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009751
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009752 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009753 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009754 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009755 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009756
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009757 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009758 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009759
Dan Gohman14152b42010-07-06 20:24:04 +00009760 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009761 return nextMBB;
9762}
9763
9764// private utility function
9765MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009766X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9767 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009768 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009769 // For the atomic min/max operator, we generate
9770 // thisMBB:
9771 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009772 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009773 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009774 // cmp t1, t2
9775 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009776 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009777 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9778 // bz newMBB
9779 // fallthrough -->nextMBB
9780 //
9781 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9782 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009783 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009784 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009785
Mon P Wang63307c32008-05-05 19:05:59 +00009786 /// First build the CFG
9787 MachineFunction *F = MBB->getParent();
9788 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009789 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9790 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9791 F->insert(MBBIter, newMBB);
9792 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009793
Dan Gohman14152b42010-07-06 20:24:04 +00009794 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9795 nextMBB->splice(nextMBB->begin(), thisMBB,
9796 llvm::next(MachineBasicBlock::iterator(mInstr)),
9797 thisMBB->end());
9798 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009799
Mon P Wang63307c32008-05-05 19:05:59 +00009800 // Update thisMBB to fall through to newMBB
9801 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009802
Mon P Wang63307c32008-05-05 19:05:59 +00009803 // newMBB jumps to newMBB and fall through to nextMBB
9804 newMBB->addSuccessor(nextMBB);
9805 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009806
Dale Johannesene4d209d2009-02-03 20:21:25 +00009807 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009808 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009809 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009810 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009811 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009812 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009813 int numArgs = mInstr->getNumOperands() - 1;
9814 for (int i=0; i < numArgs; ++i)
9815 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009816
Mon P Wang63307c32008-05-05 19:05:59 +00009817 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009818 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009819 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009820
Mon P Wangab3e7472008-05-05 22:56:23 +00009821 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009822 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009823 for (int i=0; i <= lastAddrIndx; ++i)
9824 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009825
Mon P Wang63307c32008-05-05 19:05:59 +00009826 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009827 assert((argOpers[valArgIndx]->isReg() ||
9828 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009829 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009830
9831 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009832 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009833 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009834 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009835 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009836 (*MIB).addOperand(*argOpers[valArgIndx]);
9837
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009838 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009839 MIB.addReg(t1);
9840
Dale Johannesene4d209d2009-02-03 20:21:25 +00009841 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009842 MIB.addReg(t1);
9843 MIB.addReg(t2);
9844
9845 // Generate movc
9846 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009847 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009848 MIB.addReg(t2);
9849 MIB.addReg(t1);
9850
9851 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009852 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009853 for (int i=0; i <= lastAddrIndx; ++i)
9854 (*MIB).addOperand(*argOpers[i]);
9855 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009856 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009857 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9858 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009859
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009860 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009861 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009862
Mon P Wang63307c32008-05-05 19:05:59 +00009863 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009864 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009865
Dan Gohman14152b42010-07-06 20:24:04 +00009866 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009867 return nextMBB;
9868}
9869
Eric Christopherf83a5de2009-08-27 18:08:16 +00009870// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009871// or XMM0_V32I8 in AVX all of this code can be replaced with that
9872// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009873MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009874X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009875 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009876 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9877 "Target must have SSE4.2 or AVX features enabled");
9878
Eric Christopherb120ab42009-08-18 22:50:32 +00009879 DebugLoc dl = MI->getDebugLoc();
9880 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009881 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009882 if (!Subtarget->hasAVX()) {
9883 if (memArg)
9884 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9885 else
9886 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9887 } else {
9888 if (memArg)
9889 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9890 else
9891 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9892 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009893
Eric Christopher41c902f2010-11-30 08:20:21 +00009894 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009895 for (unsigned i = 0; i < numArgs; ++i) {
9896 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009897 if (!(Op.isReg() && Op.isImplicit()))
9898 MIB.addOperand(Op);
9899 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009900 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009901 .addReg(X86::XMM0);
9902
Dan Gohman14152b42010-07-06 20:24:04 +00009903 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009904 return BB;
9905}
9906
9907MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009908X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009909 DebugLoc dl = MI->getDebugLoc();
9910 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009911
Eric Christopher228232b2010-11-30 07:20:12 +00009912 // Address into RAX/EAX, other two args into ECX, EDX.
9913 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9914 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9915 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9916 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009917 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009918
Eric Christopher228232b2010-11-30 07:20:12 +00009919 unsigned ValOps = X86::AddrNumOperands;
9920 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9921 .addReg(MI->getOperand(ValOps).getReg());
9922 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9923 .addReg(MI->getOperand(ValOps+1).getReg());
9924
9925 // The instruction doesn't actually take any operands though.
9926 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009927
Eric Christopher228232b2010-11-30 07:20:12 +00009928 MI->eraseFromParent(); // The pseudo is gone now.
9929 return BB;
9930}
9931
9932MachineBasicBlock *
9933X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009934 DebugLoc dl = MI->getDebugLoc();
9935 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009936
Eric Christopher228232b2010-11-30 07:20:12 +00009937 // First arg in ECX, the second in EAX.
9938 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9939 .addReg(MI->getOperand(0).getReg());
9940 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9941 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009942
Eric Christopher228232b2010-11-30 07:20:12 +00009943 // The instruction doesn't actually take any operands though.
9944 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009945
Eric Christopher228232b2010-11-30 07:20:12 +00009946 MI->eraseFromParent(); // The pseudo is gone now.
9947 return BB;
9948}
9949
9950MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009951X86TargetLowering::EmitVAARG64WithCustomInserter(
9952 MachineInstr *MI,
9953 MachineBasicBlock *MBB) const {
9954 // Emit va_arg instruction on X86-64.
9955
9956 // Operands to this pseudo-instruction:
9957 // 0 ) Output : destination address (reg)
9958 // 1-5) Input : va_list address (addr, i64mem)
9959 // 6 ) ArgSize : Size (in bytes) of vararg type
9960 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9961 // 8 ) Align : Alignment of type
9962 // 9 ) EFLAGS (implicit-def)
9963
9964 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9965 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9966
9967 unsigned DestReg = MI->getOperand(0).getReg();
9968 MachineOperand &Base = MI->getOperand(1);
9969 MachineOperand &Scale = MI->getOperand(2);
9970 MachineOperand &Index = MI->getOperand(3);
9971 MachineOperand &Disp = MI->getOperand(4);
9972 MachineOperand &Segment = MI->getOperand(5);
9973 unsigned ArgSize = MI->getOperand(6).getImm();
9974 unsigned ArgMode = MI->getOperand(7).getImm();
9975 unsigned Align = MI->getOperand(8).getImm();
9976
9977 // Memory Reference
9978 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9979 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9980 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9981
9982 // Machine Information
9983 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9984 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9985 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9986 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9987 DebugLoc DL = MI->getDebugLoc();
9988
9989 // struct va_list {
9990 // i32 gp_offset
9991 // i32 fp_offset
9992 // i64 overflow_area (address)
9993 // i64 reg_save_area (address)
9994 // }
9995 // sizeof(va_list) = 24
9996 // alignment(va_list) = 8
9997
9998 unsigned TotalNumIntRegs = 6;
9999 unsigned TotalNumXMMRegs = 8;
10000 bool UseGPOffset = (ArgMode == 1);
10001 bool UseFPOffset = (ArgMode == 2);
10002 unsigned MaxOffset = TotalNumIntRegs * 8 +
10003 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10004
10005 /* Align ArgSize to a multiple of 8 */
10006 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10007 bool NeedsAlign = (Align > 8);
10008
10009 MachineBasicBlock *thisMBB = MBB;
10010 MachineBasicBlock *overflowMBB;
10011 MachineBasicBlock *offsetMBB;
10012 MachineBasicBlock *endMBB;
10013
10014 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10015 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10016 unsigned OffsetReg = 0;
10017
10018 if (!UseGPOffset && !UseFPOffset) {
10019 // If we only pull from the overflow region, we don't create a branch.
10020 // We don't need to alter control flow.
10021 OffsetDestReg = 0; // unused
10022 OverflowDestReg = DestReg;
10023
10024 offsetMBB = NULL;
10025 overflowMBB = thisMBB;
10026 endMBB = thisMBB;
10027 } else {
10028 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10029 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10030 // If not, pull from overflow_area. (branch to overflowMBB)
10031 //
10032 // thisMBB
10033 // | .
10034 // | .
10035 // offsetMBB overflowMBB
10036 // | .
10037 // | .
10038 // endMBB
10039
10040 // Registers for the PHI in endMBB
10041 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10042 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10043
10044 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10045 MachineFunction *MF = MBB->getParent();
10046 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10047 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10048 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10049
10050 MachineFunction::iterator MBBIter = MBB;
10051 ++MBBIter;
10052
10053 // Insert the new basic blocks
10054 MF->insert(MBBIter, offsetMBB);
10055 MF->insert(MBBIter, overflowMBB);
10056 MF->insert(MBBIter, endMBB);
10057
10058 // Transfer the remainder of MBB and its successor edges to endMBB.
10059 endMBB->splice(endMBB->begin(), thisMBB,
10060 llvm::next(MachineBasicBlock::iterator(MI)),
10061 thisMBB->end());
10062 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10063
10064 // Make offsetMBB and overflowMBB successors of thisMBB
10065 thisMBB->addSuccessor(offsetMBB);
10066 thisMBB->addSuccessor(overflowMBB);
10067
10068 // endMBB is a successor of both offsetMBB and overflowMBB
10069 offsetMBB->addSuccessor(endMBB);
10070 overflowMBB->addSuccessor(endMBB);
10071
10072 // Load the offset value into a register
10073 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10074 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10075 .addOperand(Base)
10076 .addOperand(Scale)
10077 .addOperand(Index)
10078 .addDisp(Disp, UseFPOffset ? 4 : 0)
10079 .addOperand(Segment)
10080 .setMemRefs(MMOBegin, MMOEnd);
10081
10082 // Check if there is enough room left to pull this argument.
10083 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10084 .addReg(OffsetReg)
10085 .addImm(MaxOffset + 8 - ArgSizeA8);
10086
10087 // Branch to "overflowMBB" if offset >= max
10088 // Fall through to "offsetMBB" otherwise
10089 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10090 .addMBB(overflowMBB);
10091 }
10092
10093 // In offsetMBB, emit code to use the reg_save_area.
10094 if (offsetMBB) {
10095 assert(OffsetReg != 0);
10096
10097 // Read the reg_save_area address.
10098 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10099 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10100 .addOperand(Base)
10101 .addOperand(Scale)
10102 .addOperand(Index)
10103 .addDisp(Disp, 16)
10104 .addOperand(Segment)
10105 .setMemRefs(MMOBegin, MMOEnd);
10106
10107 // Zero-extend the offset
10108 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10109 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10110 .addImm(0)
10111 .addReg(OffsetReg)
10112 .addImm(X86::sub_32bit);
10113
10114 // Add the offset to the reg_save_area to get the final address.
10115 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10116 .addReg(OffsetReg64)
10117 .addReg(RegSaveReg);
10118
10119 // Compute the offset for the next argument
10120 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10121 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10122 .addReg(OffsetReg)
10123 .addImm(UseFPOffset ? 16 : 8);
10124
10125 // Store it back into the va_list.
10126 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10127 .addOperand(Base)
10128 .addOperand(Scale)
10129 .addOperand(Index)
10130 .addDisp(Disp, UseFPOffset ? 4 : 0)
10131 .addOperand(Segment)
10132 .addReg(NextOffsetReg)
10133 .setMemRefs(MMOBegin, MMOEnd);
10134
10135 // Jump to endMBB
10136 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10137 .addMBB(endMBB);
10138 }
10139
10140 //
10141 // Emit code to use overflow area
10142 //
10143
10144 // Load the overflow_area address into a register.
10145 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10146 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10147 .addOperand(Base)
10148 .addOperand(Scale)
10149 .addOperand(Index)
10150 .addDisp(Disp, 8)
10151 .addOperand(Segment)
10152 .setMemRefs(MMOBegin, MMOEnd);
10153
10154 // If we need to align it, do so. Otherwise, just copy the address
10155 // to OverflowDestReg.
10156 if (NeedsAlign) {
10157 // Align the overflow address
10158 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10159 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10160
10161 // aligned_addr = (addr + (align-1)) & ~(align-1)
10162 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10163 .addReg(OverflowAddrReg)
10164 .addImm(Align-1);
10165
10166 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10167 .addReg(TmpReg)
10168 .addImm(~(uint64_t)(Align-1));
10169 } else {
10170 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10171 .addReg(OverflowAddrReg);
10172 }
10173
10174 // Compute the next overflow address after this argument.
10175 // (the overflow address should be kept 8-byte aligned)
10176 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10177 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10178 .addReg(OverflowDestReg)
10179 .addImm(ArgSizeA8);
10180
10181 // Store the new overflow address.
10182 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10183 .addOperand(Base)
10184 .addOperand(Scale)
10185 .addOperand(Index)
10186 .addDisp(Disp, 8)
10187 .addOperand(Segment)
10188 .addReg(NextAddrReg)
10189 .setMemRefs(MMOBegin, MMOEnd);
10190
10191 // If we branched, emit the PHI to the front of endMBB.
10192 if (offsetMBB) {
10193 BuildMI(*endMBB, endMBB->begin(), DL,
10194 TII->get(X86::PHI), DestReg)
10195 .addReg(OffsetDestReg).addMBB(offsetMBB)
10196 .addReg(OverflowDestReg).addMBB(overflowMBB);
10197 }
10198
10199 // Erase the pseudo instruction
10200 MI->eraseFromParent();
10201
10202 return endMBB;
10203}
10204
10205MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010206X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10207 MachineInstr *MI,
10208 MachineBasicBlock *MBB) const {
10209 // Emit code to save XMM registers to the stack. The ABI says that the
10210 // number of registers to save is given in %al, so it's theoretically
10211 // possible to do an indirect jump trick to avoid saving all of them,
10212 // however this code takes a simpler approach and just executes all
10213 // of the stores if %al is non-zero. It's less code, and it's probably
10214 // easier on the hardware branch predictor, and stores aren't all that
10215 // expensive anyway.
10216
10217 // Create the new basic blocks. One block contains all the XMM stores,
10218 // and one block is the final destination regardless of whether any
10219 // stores were performed.
10220 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10221 MachineFunction *F = MBB->getParent();
10222 MachineFunction::iterator MBBIter = MBB;
10223 ++MBBIter;
10224 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10225 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10226 F->insert(MBBIter, XMMSaveMBB);
10227 F->insert(MBBIter, EndMBB);
10228
Dan Gohman14152b42010-07-06 20:24:04 +000010229 // Transfer the remainder of MBB and its successor edges to EndMBB.
10230 EndMBB->splice(EndMBB->begin(), MBB,
10231 llvm::next(MachineBasicBlock::iterator(MI)),
10232 MBB->end());
10233 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10234
Dan Gohmand6708ea2009-08-15 01:38:56 +000010235 // The original block will now fall through to the XMM save block.
10236 MBB->addSuccessor(XMMSaveMBB);
10237 // The XMMSaveMBB will fall through to the end block.
10238 XMMSaveMBB->addSuccessor(EndMBB);
10239
10240 // Now add the instructions.
10241 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10242 DebugLoc DL = MI->getDebugLoc();
10243
10244 unsigned CountReg = MI->getOperand(0).getReg();
10245 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10246 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10247
10248 if (!Subtarget->isTargetWin64()) {
10249 // If %al is 0, branch around the XMM save block.
10250 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010251 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010252 MBB->addSuccessor(EndMBB);
10253 }
10254
10255 // In the XMM save block, save all the XMM argument registers.
10256 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10257 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010258 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010259 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010260 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010261 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010262 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010263 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10264 .addFrameIndex(RegSaveFrameIndex)
10265 .addImm(/*Scale=*/1)
10266 .addReg(/*IndexReg=*/0)
10267 .addImm(/*Disp=*/Offset)
10268 .addReg(/*Segment=*/0)
10269 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010270 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010271 }
10272
Dan Gohman14152b42010-07-06 20:24:04 +000010273 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010274
10275 return EndMBB;
10276}
Mon P Wang63307c32008-05-05 19:05:59 +000010277
Evan Cheng60c07e12006-07-05 22:17:51 +000010278MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010279X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010280 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010281 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10282 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010283
Chris Lattner52600972009-09-02 05:57:00 +000010284 // To "insert" a SELECT_CC instruction, we actually have to insert the
10285 // diamond control-flow pattern. The incoming instruction knows the
10286 // destination vreg to set, the condition code register to branch on, the
10287 // true/false values to select between, and a branch opcode to use.
10288 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10289 MachineFunction::iterator It = BB;
10290 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010291
Chris Lattner52600972009-09-02 05:57:00 +000010292 // thisMBB:
10293 // ...
10294 // TrueVal = ...
10295 // cmpTY ccX, r1, r2
10296 // bCC copy1MBB
10297 // fallthrough --> copy0MBB
10298 MachineBasicBlock *thisMBB = BB;
10299 MachineFunction *F = BB->getParent();
10300 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10301 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010302 F->insert(It, copy0MBB);
10303 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010304
Bill Wendling730c07e2010-06-25 20:48:10 +000010305 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10306 // live into the sink and copy blocks.
10307 const MachineFunction *MF = BB->getParent();
10308 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10309 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010310
Dan Gohman14152b42010-07-06 20:24:04 +000010311 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10312 const MachineOperand &MO = MI->getOperand(I);
10313 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010314 unsigned Reg = MO.getReg();
10315 if (Reg != X86::EFLAGS) continue;
10316 copy0MBB->addLiveIn(Reg);
10317 sinkMBB->addLiveIn(Reg);
10318 }
10319
Dan Gohman14152b42010-07-06 20:24:04 +000010320 // Transfer the remainder of BB and its successor edges to sinkMBB.
10321 sinkMBB->splice(sinkMBB->begin(), BB,
10322 llvm::next(MachineBasicBlock::iterator(MI)),
10323 BB->end());
10324 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10325
10326 // Add the true and fallthrough blocks as its successors.
10327 BB->addSuccessor(copy0MBB);
10328 BB->addSuccessor(sinkMBB);
10329
10330 // Create the conditional branch instruction.
10331 unsigned Opc =
10332 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10333 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10334
Chris Lattner52600972009-09-02 05:57:00 +000010335 // copy0MBB:
10336 // %FalseValue = ...
10337 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010338 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010339
Chris Lattner52600972009-09-02 05:57:00 +000010340 // sinkMBB:
10341 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10342 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010343 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10344 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010345 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10346 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10347
Dan Gohman14152b42010-07-06 20:24:04 +000010348 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010349 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010350}
10351
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010352MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010353X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010354 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010355 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10356 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010357
10358 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10359 // non-trivial part is impdef of ESP.
10360 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10361 // mingw-w64.
10362
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010363 const char *StackProbeSymbol =
10364 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10365
Dan Gohman14152b42010-07-06 20:24:04 +000010366 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010367 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010368 .addReg(X86::EAX, RegState::Implicit)
10369 .addReg(X86::ESP, RegState::Implicit)
10370 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010371 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10372 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010373
Dan Gohman14152b42010-07-06 20:24:04 +000010374 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010375 return BB;
10376}
Chris Lattner52600972009-09-02 05:57:00 +000010377
10378MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010379X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10380 MachineBasicBlock *BB) const {
10381 // This is pretty easy. We're taking the value that we received from
10382 // our load from the relocation, sticking it in either RDI (x86-64)
10383 // or EAX and doing an indirect call. The return value will then
10384 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010385 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010386 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010387 DebugLoc DL = MI->getDebugLoc();
10388 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010389
10390 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010391 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010392
Eric Christopher30ef0e52010-06-03 04:07:48 +000010393 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010394 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10395 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010396 .addReg(X86::RIP)
10397 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010398 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010399 MI->getOperand(3).getTargetFlags())
10400 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010401 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010402 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010403 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010404 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10405 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010406 .addReg(0)
10407 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010408 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010409 MI->getOperand(3).getTargetFlags())
10410 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010411 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010412 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010413 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010414 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10415 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010416 .addReg(TII->getGlobalBaseReg(F))
10417 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010418 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010419 MI->getOperand(3).getTargetFlags())
10420 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010421 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010422 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010423 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010424
Dan Gohman14152b42010-07-06 20:24:04 +000010425 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010426 return BB;
10427}
10428
10429MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010430X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010431 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010432 switch (MI->getOpcode()) {
10433 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010434 case X86::TAILJMPd64:
10435 case X86::TAILJMPr64:
10436 case X86::TAILJMPm64:
10437 assert(!"TAILJMP64 would not be touched here.");
10438 case X86::TCRETURNdi64:
10439 case X86::TCRETURNri64:
10440 case X86::TCRETURNmi64:
10441 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10442 // On AMD64, additional defs should be added before register allocation.
10443 if (!Subtarget->isTargetWin64()) {
10444 MI->addRegisterDefined(X86::RSI);
10445 MI->addRegisterDefined(X86::RDI);
10446 MI->addRegisterDefined(X86::XMM6);
10447 MI->addRegisterDefined(X86::XMM7);
10448 MI->addRegisterDefined(X86::XMM8);
10449 MI->addRegisterDefined(X86::XMM9);
10450 MI->addRegisterDefined(X86::XMM10);
10451 MI->addRegisterDefined(X86::XMM11);
10452 MI->addRegisterDefined(X86::XMM12);
10453 MI->addRegisterDefined(X86::XMM13);
10454 MI->addRegisterDefined(X86::XMM14);
10455 MI->addRegisterDefined(X86::XMM15);
10456 }
10457 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010458 case X86::WIN_ALLOCA:
10459 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010460 case X86::TLSCall_32:
10461 case X86::TLSCall_64:
10462 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010463 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010464 case X86::CMOV_FR32:
10465 case X86::CMOV_FR64:
10466 case X86::CMOV_V4F32:
10467 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010468 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010469 case X86::CMOV_GR16:
10470 case X86::CMOV_GR32:
10471 case X86::CMOV_RFP32:
10472 case X86::CMOV_RFP64:
10473 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010474 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010475
Dale Johannesen849f2142007-07-03 00:53:03 +000010476 case X86::FP32_TO_INT16_IN_MEM:
10477 case X86::FP32_TO_INT32_IN_MEM:
10478 case X86::FP32_TO_INT64_IN_MEM:
10479 case X86::FP64_TO_INT16_IN_MEM:
10480 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010481 case X86::FP64_TO_INT64_IN_MEM:
10482 case X86::FP80_TO_INT16_IN_MEM:
10483 case X86::FP80_TO_INT32_IN_MEM:
10484 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010485 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10486 DebugLoc DL = MI->getDebugLoc();
10487
Evan Cheng60c07e12006-07-05 22:17:51 +000010488 // Change the floating point control register to use "round towards zero"
10489 // mode when truncating to an integer value.
10490 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010491 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010492 addFrameReference(BuildMI(*BB, MI, DL,
10493 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010494
10495 // Load the old value of the high byte of the control word...
10496 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010497 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010498 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010499 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010500
10501 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010502 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010503 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010504
10505 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010506 addFrameReference(BuildMI(*BB, MI, DL,
10507 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010508
10509 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010510 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010511 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010512
10513 // Get the X86 opcode to use.
10514 unsigned Opc;
10515 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010516 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010517 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10518 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10519 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10520 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10521 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10522 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010523 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10524 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10525 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010526 }
10527
10528 X86AddressMode AM;
10529 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010530 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010531 AM.BaseType = X86AddressMode::RegBase;
10532 AM.Base.Reg = Op.getReg();
10533 } else {
10534 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010535 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010536 }
10537 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010538 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010539 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010540 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010541 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010542 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010543 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010544 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010545 AM.GV = Op.getGlobal();
10546 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010547 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010548 }
Dan Gohman14152b42010-07-06 20:24:04 +000010549 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010550 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010551
10552 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010553 addFrameReference(BuildMI(*BB, MI, DL,
10554 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010555
Dan Gohman14152b42010-07-06 20:24:04 +000010556 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010557 return BB;
10558 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010559 // String/text processing lowering.
10560 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010561 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010562 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10563 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010564 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010565 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10566 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010567 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010568 return EmitPCMP(MI, BB, 5, false /* in mem */);
10569 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010570 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010571 return EmitPCMP(MI, BB, 5, true /* in mem */);
10572
Eric Christopher228232b2010-11-30 07:20:12 +000010573 // Thread synchronization.
10574 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010575 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010576 case X86::MWAIT:
10577 return EmitMwait(MI, BB);
10578
Eric Christopherb120ab42009-08-18 22:50:32 +000010579 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010580 case X86::ATOMAND32:
10581 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010582 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010583 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010584 X86::NOT32r, X86::EAX,
10585 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010586 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010587 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10588 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010589 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010590 X86::NOT32r, X86::EAX,
10591 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010592 case X86::ATOMXOR32:
10593 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010594 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010595 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010596 X86::NOT32r, X86::EAX,
10597 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010598 case X86::ATOMNAND32:
10599 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010600 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010601 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010602 X86::NOT32r, X86::EAX,
10603 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010604 case X86::ATOMMIN32:
10605 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10606 case X86::ATOMMAX32:
10607 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10608 case X86::ATOMUMIN32:
10609 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10610 case X86::ATOMUMAX32:
10611 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010612
10613 case X86::ATOMAND16:
10614 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10615 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010616 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010617 X86::NOT16r, X86::AX,
10618 X86::GR16RegisterClass);
10619 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010620 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010621 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010622 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010623 X86::NOT16r, X86::AX,
10624 X86::GR16RegisterClass);
10625 case X86::ATOMXOR16:
10626 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10627 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010628 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010629 X86::NOT16r, X86::AX,
10630 X86::GR16RegisterClass);
10631 case X86::ATOMNAND16:
10632 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10633 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010634 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010635 X86::NOT16r, X86::AX,
10636 X86::GR16RegisterClass, true);
10637 case X86::ATOMMIN16:
10638 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10639 case X86::ATOMMAX16:
10640 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10641 case X86::ATOMUMIN16:
10642 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10643 case X86::ATOMUMAX16:
10644 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10645
10646 case X86::ATOMAND8:
10647 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10648 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010649 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010650 X86::NOT8r, X86::AL,
10651 X86::GR8RegisterClass);
10652 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010653 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010654 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010655 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010656 X86::NOT8r, X86::AL,
10657 X86::GR8RegisterClass);
10658 case X86::ATOMXOR8:
10659 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10660 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010661 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010662 X86::NOT8r, X86::AL,
10663 X86::GR8RegisterClass);
10664 case X86::ATOMNAND8:
10665 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10666 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010667 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010668 X86::NOT8r, X86::AL,
10669 X86::GR8RegisterClass, true);
10670 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010671 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010672 case X86::ATOMAND64:
10673 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010674 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010675 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010676 X86::NOT64r, X86::RAX,
10677 X86::GR64RegisterClass);
10678 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010679 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10680 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010681 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010682 X86::NOT64r, X86::RAX,
10683 X86::GR64RegisterClass);
10684 case X86::ATOMXOR64:
10685 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010686 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010687 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010688 X86::NOT64r, X86::RAX,
10689 X86::GR64RegisterClass);
10690 case X86::ATOMNAND64:
10691 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10692 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010693 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010694 X86::NOT64r, X86::RAX,
10695 X86::GR64RegisterClass, true);
10696 case X86::ATOMMIN64:
10697 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10698 case X86::ATOMMAX64:
10699 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10700 case X86::ATOMUMIN64:
10701 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10702 case X86::ATOMUMAX64:
10703 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010704
10705 // This group does 64-bit operations on a 32-bit host.
10706 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010707 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010708 X86::AND32rr, X86::AND32rr,
10709 X86::AND32ri, X86::AND32ri,
10710 false);
10711 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010712 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010713 X86::OR32rr, X86::OR32rr,
10714 X86::OR32ri, X86::OR32ri,
10715 false);
10716 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010717 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010718 X86::XOR32rr, X86::XOR32rr,
10719 X86::XOR32ri, X86::XOR32ri,
10720 false);
10721 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010722 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010723 X86::AND32rr, X86::AND32rr,
10724 X86::AND32ri, X86::AND32ri,
10725 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010726 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010727 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010728 X86::ADD32rr, X86::ADC32rr,
10729 X86::ADD32ri, X86::ADC32ri,
10730 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010731 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010732 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010733 X86::SUB32rr, X86::SBB32rr,
10734 X86::SUB32ri, X86::SBB32ri,
10735 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010736 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010737 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010738 X86::MOV32rr, X86::MOV32rr,
10739 X86::MOV32ri, X86::MOV32ri,
10740 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010741 case X86::VASTART_SAVE_XMM_REGS:
10742 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010743
10744 case X86::VAARG_64:
10745 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010746 }
10747}
10748
10749//===----------------------------------------------------------------------===//
10750// X86 Optimization Hooks
10751//===----------------------------------------------------------------------===//
10752
Dan Gohman475871a2008-07-27 21:46:04 +000010753void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010754 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010755 APInt &KnownZero,
10756 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010757 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010758 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010759 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010760 assert((Opc >= ISD::BUILTIN_OP_END ||
10761 Opc == ISD::INTRINSIC_WO_CHAIN ||
10762 Opc == ISD::INTRINSIC_W_CHAIN ||
10763 Opc == ISD::INTRINSIC_VOID) &&
10764 "Should use MaskedValueIsZero if you don't know whether Op"
10765 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010766
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010767 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010768 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010769 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010770 case X86ISD::ADD:
10771 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010772 case X86ISD::ADC:
10773 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010774 case X86ISD::SMUL:
10775 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010776 case X86ISD::INC:
10777 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010778 case X86ISD::OR:
10779 case X86ISD::XOR:
10780 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010781 // These nodes' second result is a boolean.
10782 if (Op.getResNo() == 0)
10783 break;
10784 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010785 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010786 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10787 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010788 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010789 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010790}
Chris Lattner259e97c2006-01-31 19:43:35 +000010791
Owen Andersonbc146b02010-09-21 20:42:50 +000010792unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10793 unsigned Depth) const {
10794 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10795 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10796 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010797
Owen Andersonbc146b02010-09-21 20:42:50 +000010798 // Fallback case.
10799 return 1;
10800}
10801
Evan Cheng206ee9d2006-07-07 08:33:52 +000010802/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010803/// node is a GlobalAddress + offset.
10804bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010805 const GlobalValue* &GA,
10806 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010807 if (N->getOpcode() == X86ISD::Wrapper) {
10808 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010809 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010810 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010811 return true;
10812 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010813 }
Evan Chengad4196b2008-05-12 19:56:52 +000010814 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010815}
10816
Evan Cheng206ee9d2006-07-07 08:33:52 +000010817/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10818/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10819/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010820/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010821static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010822 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010823 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010824 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010825
Eli Friedman7a5e5552009-06-07 06:52:44 +000010826 if (VT.getSizeInBits() != 128)
10827 return SDValue();
10828
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010829 // Don't create instructions with illegal types after legalize types has run.
10830 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10831 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10832 return SDValue();
10833
Nate Begemanfdea31a2010-03-24 20:49:50 +000010834 SmallVector<SDValue, 16> Elts;
10835 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010836 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010837
Nate Begemanfdea31a2010-03-24 20:49:50 +000010838 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010839}
Evan Chengd880b972008-05-09 21:53:03 +000010840
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010841/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10842/// generation and convert it from being a bunch of shuffles and extracts
10843/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010844static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10845 const TargetLowering &TLI) {
10846 SDValue InputVector = N->getOperand(0);
10847
10848 // Only operate on vectors of 4 elements, where the alternative shuffling
10849 // gets to be more expensive.
10850 if (InputVector.getValueType() != MVT::v4i32)
10851 return SDValue();
10852
10853 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10854 // single use which is a sign-extend or zero-extend, and all elements are
10855 // used.
10856 SmallVector<SDNode *, 4> Uses;
10857 unsigned ExtractedElements = 0;
10858 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10859 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10860 if (UI.getUse().getResNo() != InputVector.getResNo())
10861 return SDValue();
10862
10863 SDNode *Extract = *UI;
10864 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10865 return SDValue();
10866
10867 if (Extract->getValueType(0) != MVT::i32)
10868 return SDValue();
10869 if (!Extract->hasOneUse())
10870 return SDValue();
10871 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10872 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10873 return SDValue();
10874 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10875 return SDValue();
10876
10877 // Record which element was extracted.
10878 ExtractedElements |=
10879 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10880
10881 Uses.push_back(Extract);
10882 }
10883
10884 // If not all the elements were used, this may not be worthwhile.
10885 if (ExtractedElements != 15)
10886 return SDValue();
10887
10888 // Ok, we've now decided to do the transformation.
10889 DebugLoc dl = InputVector.getDebugLoc();
10890
10891 // Store the value to a temporary stack slot.
10892 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010893 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10894 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010895
10896 // Replace each use (extract) with a load of the appropriate element.
10897 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10898 UE = Uses.end(); UI != UE; ++UI) {
10899 SDNode *Extract = *UI;
10900
10901 // Compute the element's address.
10902 SDValue Idx = Extract->getOperand(1);
10903 unsigned EltSize =
10904 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10905 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10906 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10907
Eric Christopher90eb4022010-07-22 00:26:08 +000010908 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010909 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010910
10911 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010912 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010913 ScalarAddr, MachinePointerInfo(),
10914 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010915
10916 // Replace the exact with the load.
10917 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10918 }
10919
10920 // The replacement was made in place; don't return anything.
10921 return SDValue();
10922}
10923
Chris Lattner83e6c992006-10-04 06:57:07 +000010924/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010925static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010926 const X86Subtarget *Subtarget) {
10927 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010928 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010929 // Get the LHS/RHS of the select.
10930 SDValue LHS = N->getOperand(1);
10931 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010932
Dan Gohman670e5392009-09-21 18:03:22 +000010933 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010934 // instructions match the semantics of the common C idiom x<y?x:y but not
10935 // x<=y?x:y, because of how they handle negative zero (which can be
10936 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010937 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010938 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010939 Cond.getOpcode() == ISD::SETCC) {
10940 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010941
Chris Lattner47b4ce82009-03-11 05:48:52 +000010942 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010943 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010944 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10945 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010946 switch (CC) {
10947 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010948 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010949 // Converting this to a min would handle NaNs incorrectly, and swapping
10950 // the operands would cause it to handle comparisons between positive
10951 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010952 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010953 if (!UnsafeFPMath &&
10954 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10955 break;
10956 std::swap(LHS, RHS);
10957 }
Dan Gohman670e5392009-09-21 18:03:22 +000010958 Opcode = X86ISD::FMIN;
10959 break;
10960 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010961 // Converting this to a min would handle comparisons between positive
10962 // and negative zero incorrectly.
10963 if (!UnsafeFPMath &&
10964 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10965 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010966 Opcode = X86ISD::FMIN;
10967 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010968 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010969 // Converting this to a min would handle both negative zeros and NaNs
10970 // incorrectly, but we can swap the operands to fix both.
10971 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010972 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010973 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010974 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010975 Opcode = X86ISD::FMIN;
10976 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010977
Dan Gohman670e5392009-09-21 18:03:22 +000010978 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010979 // Converting this to a max would handle comparisons between positive
10980 // and negative zero incorrectly.
10981 if (!UnsafeFPMath &&
10982 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10983 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010984 Opcode = X86ISD::FMAX;
10985 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010986 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010987 // Converting this to a max would handle NaNs incorrectly, and swapping
10988 // the operands would cause it to handle comparisons between positive
10989 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010990 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010991 if (!UnsafeFPMath &&
10992 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10993 break;
10994 std::swap(LHS, RHS);
10995 }
Dan Gohman670e5392009-09-21 18:03:22 +000010996 Opcode = X86ISD::FMAX;
10997 break;
10998 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010999 // Converting this to a max would handle both negative zeros and NaNs
11000 // incorrectly, but we can swap the operands to fix both.
11001 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011002 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011003 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011004 case ISD::SETGE:
11005 Opcode = X86ISD::FMAX;
11006 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011007 }
Dan Gohman670e5392009-09-21 18:03:22 +000011008 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011009 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11010 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011011 switch (CC) {
11012 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011013 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011014 // Converting this to a min would handle comparisons between positive
11015 // and negative zero incorrectly, and swapping the operands would
11016 // cause it to handle NaNs incorrectly.
11017 if (!UnsafeFPMath &&
11018 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011019 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011020 break;
11021 std::swap(LHS, RHS);
11022 }
Dan Gohman670e5392009-09-21 18:03:22 +000011023 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011024 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011025 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011026 // Converting this to a min would handle NaNs incorrectly.
11027 if (!UnsafeFPMath &&
11028 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11029 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011030 Opcode = X86ISD::FMIN;
11031 break;
11032 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011033 // Converting this to a min would handle both negative zeros and NaNs
11034 // incorrectly, but we can swap the operands to fix both.
11035 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011036 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011037 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011038 case ISD::SETGE:
11039 Opcode = X86ISD::FMIN;
11040 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011041
Dan Gohman670e5392009-09-21 18:03:22 +000011042 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011043 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011044 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011045 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011046 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011047 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011048 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011049 // Converting this to a max would handle comparisons between positive
11050 // and negative zero incorrectly, and swapping the operands would
11051 // cause it to handle NaNs incorrectly.
11052 if (!UnsafeFPMath &&
11053 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011054 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011055 break;
11056 std::swap(LHS, RHS);
11057 }
Dan Gohman670e5392009-09-21 18:03:22 +000011058 Opcode = X86ISD::FMAX;
11059 break;
11060 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011061 // Converting this to a max would handle both negative zeros and NaNs
11062 // incorrectly, but we can swap the operands to fix both.
11063 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011064 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011065 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011066 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011067 Opcode = X86ISD::FMAX;
11068 break;
11069 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011070 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011071
Chris Lattner47b4ce82009-03-11 05:48:52 +000011072 if (Opcode)
11073 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011074 }
Eric Christopherfd179292009-08-27 18:07:15 +000011075
Chris Lattnerd1980a52009-03-12 06:52:53 +000011076 // If this is a select between two integer constants, try to do some
11077 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011078 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11079 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011080 // Don't do this for crazy integer types.
11081 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11082 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011083 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011084 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011085
Chris Lattnercee56e72009-03-13 05:53:31 +000011086 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011087 // Efficiently invertible.
11088 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11089 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11090 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11091 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011092 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011093 }
Eric Christopherfd179292009-08-27 18:07:15 +000011094
Chris Lattnerd1980a52009-03-12 06:52:53 +000011095 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011096 if (FalseC->getAPIntValue() == 0 &&
11097 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011098 if (NeedsCondInvert) // Invert the condition if needed.
11099 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11100 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011101
Chris Lattnerd1980a52009-03-12 06:52:53 +000011102 // Zero extend the condition if needed.
11103 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011104
Chris Lattnercee56e72009-03-13 05:53:31 +000011105 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011106 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011107 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011108 }
Eric Christopherfd179292009-08-27 18:07:15 +000011109
Chris Lattner97a29a52009-03-13 05:22:11 +000011110 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011111 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011112 if (NeedsCondInvert) // Invert the condition if needed.
11113 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11114 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011115
Chris Lattner97a29a52009-03-13 05:22:11 +000011116 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011117 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11118 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011119 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011120 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011121 }
Eric Christopherfd179292009-08-27 18:07:15 +000011122
Chris Lattnercee56e72009-03-13 05:53:31 +000011123 // Optimize cases that will turn into an LEA instruction. This requires
11124 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011125 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011126 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011127 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011128
Chris Lattnercee56e72009-03-13 05:53:31 +000011129 bool isFastMultiplier = false;
11130 if (Diff < 10) {
11131 switch ((unsigned char)Diff) {
11132 default: break;
11133 case 1: // result = add base, cond
11134 case 2: // result = lea base( , cond*2)
11135 case 3: // result = lea base(cond, cond*2)
11136 case 4: // result = lea base( , cond*4)
11137 case 5: // result = lea base(cond, cond*4)
11138 case 8: // result = lea base( , cond*8)
11139 case 9: // result = lea base(cond, cond*8)
11140 isFastMultiplier = true;
11141 break;
11142 }
11143 }
Eric Christopherfd179292009-08-27 18:07:15 +000011144
Chris Lattnercee56e72009-03-13 05:53:31 +000011145 if (isFastMultiplier) {
11146 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11147 if (NeedsCondInvert) // Invert the condition if needed.
11148 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11149 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011150
Chris Lattnercee56e72009-03-13 05:53:31 +000011151 // Zero extend the condition if needed.
11152 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11153 Cond);
11154 // Scale the condition by the difference.
11155 if (Diff != 1)
11156 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11157 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011158
Chris Lattnercee56e72009-03-13 05:53:31 +000011159 // Add the base if non-zero.
11160 if (FalseC->getAPIntValue() != 0)
11161 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11162 SDValue(FalseC, 0));
11163 return Cond;
11164 }
Eric Christopherfd179292009-08-27 18:07:15 +000011165 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011166 }
11167 }
Eric Christopherfd179292009-08-27 18:07:15 +000011168
Dan Gohman475871a2008-07-27 21:46:04 +000011169 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011170}
11171
Chris Lattnerd1980a52009-03-12 06:52:53 +000011172/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11173static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11174 TargetLowering::DAGCombinerInfo &DCI) {
11175 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011176
Chris Lattnerd1980a52009-03-12 06:52:53 +000011177 // If the flag operand isn't dead, don't touch this CMOV.
11178 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11179 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011180
Chris Lattnerd1980a52009-03-12 06:52:53 +000011181 // If this is a select between two integer constants, try to do some
11182 // optimizations. Note that the operands are ordered the opposite of SELECT
11183 // operands.
11184 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11185 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11186 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11187 // larger than FalseC (the false value).
11188 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011189
Chris Lattnerd1980a52009-03-12 06:52:53 +000011190 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11191 CC = X86::GetOppositeBranchCondition(CC);
11192 std::swap(TrueC, FalseC);
11193 }
Eric Christopherfd179292009-08-27 18:07:15 +000011194
Chris Lattnerd1980a52009-03-12 06:52:53 +000011195 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011196 // This is efficient for any integer data type (including i8/i16) and
11197 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011198 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11199 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011200 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11201 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011202
Chris Lattnerd1980a52009-03-12 06:52:53 +000011203 // Zero extend the condition if needed.
11204 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011205
Chris Lattnerd1980a52009-03-12 06:52:53 +000011206 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11207 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011208 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011209 if (N->getNumValues() == 2) // Dead flag value?
11210 return DCI.CombineTo(N, Cond, SDValue());
11211 return Cond;
11212 }
Eric Christopherfd179292009-08-27 18:07:15 +000011213
Chris Lattnercee56e72009-03-13 05:53:31 +000011214 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11215 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011216 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11217 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011218 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11219 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011220
Chris Lattner97a29a52009-03-13 05:22:11 +000011221 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011222 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11223 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011224 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11225 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011226
Chris Lattner97a29a52009-03-13 05:22:11 +000011227 if (N->getNumValues() == 2) // Dead flag value?
11228 return DCI.CombineTo(N, Cond, SDValue());
11229 return Cond;
11230 }
Eric Christopherfd179292009-08-27 18:07:15 +000011231
Chris Lattnercee56e72009-03-13 05:53:31 +000011232 // Optimize cases that will turn into an LEA instruction. This requires
11233 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011234 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011235 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011236 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011237
Chris Lattnercee56e72009-03-13 05:53:31 +000011238 bool isFastMultiplier = false;
11239 if (Diff < 10) {
11240 switch ((unsigned char)Diff) {
11241 default: break;
11242 case 1: // result = add base, cond
11243 case 2: // result = lea base( , cond*2)
11244 case 3: // result = lea base(cond, cond*2)
11245 case 4: // result = lea base( , cond*4)
11246 case 5: // result = lea base(cond, cond*4)
11247 case 8: // result = lea base( , cond*8)
11248 case 9: // result = lea base(cond, cond*8)
11249 isFastMultiplier = true;
11250 break;
11251 }
11252 }
Eric Christopherfd179292009-08-27 18:07:15 +000011253
Chris Lattnercee56e72009-03-13 05:53:31 +000011254 if (isFastMultiplier) {
11255 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11256 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011257 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11258 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011259 // Zero extend the condition if needed.
11260 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11261 Cond);
11262 // Scale the condition by the difference.
11263 if (Diff != 1)
11264 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11265 DAG.getConstant(Diff, Cond.getValueType()));
11266
11267 // Add the base if non-zero.
11268 if (FalseC->getAPIntValue() != 0)
11269 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11270 SDValue(FalseC, 0));
11271 if (N->getNumValues() == 2) // Dead flag value?
11272 return DCI.CombineTo(N, Cond, SDValue());
11273 return Cond;
11274 }
Eric Christopherfd179292009-08-27 18:07:15 +000011275 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011276 }
11277 }
11278 return SDValue();
11279}
11280
11281
Evan Cheng0b0cd912009-03-28 05:57:29 +000011282/// PerformMulCombine - Optimize a single multiply with constant into two
11283/// in order to implement it with two cheaper instructions, e.g.
11284/// LEA + SHL, LEA + LEA.
11285static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11286 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011287 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11288 return SDValue();
11289
Owen Andersone50ed302009-08-10 22:56:29 +000011290 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011291 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011292 return SDValue();
11293
11294 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11295 if (!C)
11296 return SDValue();
11297 uint64_t MulAmt = C->getZExtValue();
11298 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11299 return SDValue();
11300
11301 uint64_t MulAmt1 = 0;
11302 uint64_t MulAmt2 = 0;
11303 if ((MulAmt % 9) == 0) {
11304 MulAmt1 = 9;
11305 MulAmt2 = MulAmt / 9;
11306 } else if ((MulAmt % 5) == 0) {
11307 MulAmt1 = 5;
11308 MulAmt2 = MulAmt / 5;
11309 } else if ((MulAmt % 3) == 0) {
11310 MulAmt1 = 3;
11311 MulAmt2 = MulAmt / 3;
11312 }
11313 if (MulAmt2 &&
11314 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11315 DebugLoc DL = N->getDebugLoc();
11316
11317 if (isPowerOf2_64(MulAmt2) &&
11318 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11319 // If second multiplifer is pow2, issue it first. We want the multiply by
11320 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11321 // is an add.
11322 std::swap(MulAmt1, MulAmt2);
11323
11324 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011325 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011326 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011327 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011328 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011329 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011330 DAG.getConstant(MulAmt1, VT));
11331
Eric Christopherfd179292009-08-27 18:07:15 +000011332 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011333 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011334 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011335 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011336 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011337 DAG.getConstant(MulAmt2, VT));
11338
11339 // Do not add new nodes to DAG combiner worklist.
11340 DCI.CombineTo(N, NewMul, false);
11341 }
11342 return SDValue();
11343}
11344
Evan Chengad9c0a32009-12-15 00:53:42 +000011345static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11346 SDValue N0 = N->getOperand(0);
11347 SDValue N1 = N->getOperand(1);
11348 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11349 EVT VT = N0.getValueType();
11350
11351 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11352 // since the result of setcc_c is all zero's or all ones.
11353 if (N1C && N0.getOpcode() == ISD::AND &&
11354 N0.getOperand(1).getOpcode() == ISD::Constant) {
11355 SDValue N00 = N0.getOperand(0);
11356 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11357 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11358 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11359 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11360 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11361 APInt ShAmt = N1C->getAPIntValue();
11362 Mask = Mask.shl(ShAmt);
11363 if (Mask != 0)
11364 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11365 N00, DAG.getConstant(Mask, VT));
11366 }
11367 }
11368
11369 return SDValue();
11370}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011371
Nate Begeman740ab032009-01-26 00:52:55 +000011372/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11373/// when possible.
11374static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11375 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011376 EVT VT = N->getValueType(0);
11377 if (!VT.isVector() && VT.isInteger() &&
11378 N->getOpcode() == ISD::SHL)
11379 return PerformSHLCombine(N, DAG);
11380
Nate Begeman740ab032009-01-26 00:52:55 +000011381 // On X86 with SSE2 support, we can transform this to a vector shift if
11382 // all elements are shifted by the same amount. We can't do this in legalize
11383 // because the a constant vector is typically transformed to a constant pool
11384 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011385 if (!Subtarget->hasSSE2())
11386 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011387
Owen Anderson825b72b2009-08-11 20:47:22 +000011388 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011389 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011390
Mon P Wang3becd092009-01-28 08:12:05 +000011391 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011392 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011393 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011394 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011395 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11396 unsigned NumElts = VT.getVectorNumElements();
11397 unsigned i = 0;
11398 for (; i != NumElts; ++i) {
11399 SDValue Arg = ShAmtOp.getOperand(i);
11400 if (Arg.getOpcode() == ISD::UNDEF) continue;
11401 BaseShAmt = Arg;
11402 break;
11403 }
11404 for (; i != NumElts; ++i) {
11405 SDValue Arg = ShAmtOp.getOperand(i);
11406 if (Arg.getOpcode() == ISD::UNDEF) continue;
11407 if (Arg != BaseShAmt) {
11408 return SDValue();
11409 }
11410 }
11411 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011412 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011413 SDValue InVec = ShAmtOp.getOperand(0);
11414 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11415 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11416 unsigned i = 0;
11417 for (; i != NumElts; ++i) {
11418 SDValue Arg = InVec.getOperand(i);
11419 if (Arg.getOpcode() == ISD::UNDEF) continue;
11420 BaseShAmt = Arg;
11421 break;
11422 }
11423 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011425 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011426 if (C->getZExtValue() == SplatIdx)
11427 BaseShAmt = InVec.getOperand(1);
11428 }
11429 }
11430 if (BaseShAmt.getNode() == 0)
11431 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11432 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011433 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011434 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011435
Mon P Wangefa42202009-09-03 19:56:25 +000011436 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011437 if (EltVT.bitsGT(MVT::i32))
11438 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11439 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011440 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011441
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011442 // The shift amount is identical so we can do a vector shift.
11443 SDValue ValOp = N->getOperand(0);
11444 switch (N->getOpcode()) {
11445 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011446 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011447 break;
11448 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011449 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011450 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011451 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011452 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011453 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011454 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011455 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011456 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011457 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011458 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011459 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011460 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011461 break;
11462 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011463 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011464 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011465 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011466 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011467 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011468 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011469 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011470 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011471 break;
11472 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011473 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011474 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011475 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011476 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011477 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011478 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011479 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011480 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011481 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011482 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011483 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011484 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011485 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011486 }
11487 return SDValue();
11488}
11489
Nate Begemanb65c1752010-12-17 22:55:37 +000011490
11491static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11492 TargetLowering::DAGCombinerInfo &DCI,
11493 const X86Subtarget *Subtarget) {
11494 if (DCI.isBeforeLegalizeOps())
11495 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011496
Nate Begemanb65c1752010-12-17 22:55:37 +000011497 // Want to form PANDN nodes, in the hopes of then easily combining them with
11498 // OR and AND nodes to form PBLEND/PSIGN.
11499 EVT VT = N->getValueType(0);
11500 if (VT != MVT::v2i64)
11501 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011502
Nate Begemanb65c1752010-12-17 22:55:37 +000011503 SDValue N0 = N->getOperand(0);
11504 SDValue N1 = N->getOperand(1);
11505 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011506
Nate Begemanb65c1752010-12-17 22:55:37 +000011507 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011508 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011509 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11510 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11511
11512 // Check RHS for vnot
11513 if (N1.getOpcode() == ISD::XOR &&
11514 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11515 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011516
Nate Begemanb65c1752010-12-17 22:55:37 +000011517 return SDValue();
11518}
11519
Evan Cheng760d1942010-01-04 21:22:48 +000011520static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011521 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011522 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011523 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011524 return SDValue();
11525
Evan Cheng760d1942010-01-04 21:22:48 +000011526 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011527 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011528 return SDValue();
11529
Evan Cheng760d1942010-01-04 21:22:48 +000011530 SDValue N0 = N->getOperand(0);
11531 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011532
Nate Begemanb65c1752010-12-17 22:55:37 +000011533 // look for psign/blend
11534 if (Subtarget->hasSSSE3()) {
11535 if (VT == MVT::v2i64) {
11536 // Canonicalize pandn to RHS
11537 if (N0.getOpcode() == X86ISD::PANDN)
11538 std::swap(N0, N1);
11539 // or (and (m, x), (pandn m, y))
11540 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11541 SDValue Mask = N1.getOperand(0);
11542 SDValue X = N1.getOperand(1);
11543 SDValue Y;
11544 if (N0.getOperand(0) == Mask)
11545 Y = N0.getOperand(1);
11546 if (N0.getOperand(1) == Mask)
11547 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011548
Nate Begemanb65c1752010-12-17 22:55:37 +000011549 // Check to see if the mask appeared in both the AND and PANDN and
11550 if (!Y.getNode())
11551 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011552
Nate Begemanb65c1752010-12-17 22:55:37 +000011553 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11554 if (Mask.getOpcode() != ISD::BITCAST ||
11555 X.getOpcode() != ISD::BITCAST ||
11556 Y.getOpcode() != ISD::BITCAST)
11557 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011558
Nate Begemanb65c1752010-12-17 22:55:37 +000011559 // Look through mask bitcast.
11560 Mask = Mask.getOperand(0);
11561 EVT MaskVT = Mask.getValueType();
11562
11563 // Validate that the Mask operand is a vector sra node. The sra node
11564 // will be an intrinsic.
11565 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11566 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011567
Nate Begemanb65c1752010-12-17 22:55:37 +000011568 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11569 // there is no psrai.b
11570 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11571 case Intrinsic::x86_sse2_psrai_w:
11572 case Intrinsic::x86_sse2_psrai_d:
11573 break;
11574 default: return SDValue();
11575 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011576
Nate Begemanb65c1752010-12-17 22:55:37 +000011577 // Check that the SRA is all signbits.
11578 SDValue SraC = Mask.getOperand(2);
11579 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11580 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11581 if ((SraAmt + 1) != EltBits)
11582 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011583
Nate Begemanb65c1752010-12-17 22:55:37 +000011584 DebugLoc DL = N->getDebugLoc();
11585
11586 // Now we know we at least have a plendvb with the mask val. See if
11587 // we can form a psignb/w/d.
11588 // psign = x.type == y.type == mask.type && y = sub(0, x);
11589 X = X.getOperand(0);
11590 Y = Y.getOperand(0);
11591 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11592 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11593 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11594 unsigned Opc = 0;
11595 switch (EltBits) {
11596 case 8: Opc = X86ISD::PSIGNB; break;
11597 case 16: Opc = X86ISD::PSIGNW; break;
11598 case 32: Opc = X86ISD::PSIGND; break;
11599 default: break;
11600 }
11601 if (Opc) {
11602 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11603 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11604 }
11605 }
11606 // PBLENDVB only available on SSE 4.1
11607 if (!Subtarget->hasSSE41())
11608 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011609
Nate Begemanb65c1752010-12-17 22:55:37 +000011610 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11611 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11612 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011613 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011614 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11615 }
11616 }
11617 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011618
Nate Begemanb65c1752010-12-17 22:55:37 +000011619 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011620 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11621 std::swap(N0, N1);
11622 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11623 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011624 if (!N0.hasOneUse() || !N1.hasOneUse())
11625 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011626
11627 SDValue ShAmt0 = N0.getOperand(1);
11628 if (ShAmt0.getValueType() != MVT::i8)
11629 return SDValue();
11630 SDValue ShAmt1 = N1.getOperand(1);
11631 if (ShAmt1.getValueType() != MVT::i8)
11632 return SDValue();
11633 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11634 ShAmt0 = ShAmt0.getOperand(0);
11635 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11636 ShAmt1 = ShAmt1.getOperand(0);
11637
11638 DebugLoc DL = N->getDebugLoc();
11639 unsigned Opc = X86ISD::SHLD;
11640 SDValue Op0 = N0.getOperand(0);
11641 SDValue Op1 = N1.getOperand(0);
11642 if (ShAmt0.getOpcode() == ISD::SUB) {
11643 Opc = X86ISD::SHRD;
11644 std::swap(Op0, Op1);
11645 std::swap(ShAmt0, ShAmt1);
11646 }
11647
Evan Cheng8b1190a2010-04-28 01:18:01 +000011648 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011649 if (ShAmt1.getOpcode() == ISD::SUB) {
11650 SDValue Sum = ShAmt1.getOperand(0);
11651 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011652 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11653 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11654 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11655 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011656 return DAG.getNode(Opc, DL, VT,
11657 Op0, Op1,
11658 DAG.getNode(ISD::TRUNCATE, DL,
11659 MVT::i8, ShAmt0));
11660 }
11661 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11662 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11663 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011664 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011665 return DAG.getNode(Opc, DL, VT,
11666 N0.getOperand(0), N1.getOperand(0),
11667 DAG.getNode(ISD::TRUNCATE, DL,
11668 MVT::i8, ShAmt0));
11669 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011670
Evan Cheng760d1942010-01-04 21:22:48 +000011671 return SDValue();
11672}
11673
Chris Lattner149a4e52008-02-22 02:09:43 +000011674/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011675static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011676 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011677 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11678 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011679 // A preferable solution to the general problem is to figure out the right
11680 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011681
11682 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011683 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011684 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011685 if (VT.getSizeInBits() != 64)
11686 return SDValue();
11687
Devang Patel578efa92009-06-05 21:57:13 +000011688 const Function *F = DAG.getMachineFunction().getFunction();
11689 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011690 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011691 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011692 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011693 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011694 isa<LoadSDNode>(St->getValue()) &&
11695 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11696 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011697 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011698 LoadSDNode *Ld = 0;
11699 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011700 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011701 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011702 // Must be a store of a load. We currently handle two cases: the load
11703 // is a direct child, and it's under an intervening TokenFactor. It is
11704 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011705 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011706 Ld = cast<LoadSDNode>(St->getChain());
11707 else if (St->getValue().hasOneUse() &&
11708 ChainVal->getOpcode() == ISD::TokenFactor) {
11709 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011710 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011711 TokenFactorIndex = i;
11712 Ld = cast<LoadSDNode>(St->getValue());
11713 } else
11714 Ops.push_back(ChainVal->getOperand(i));
11715 }
11716 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011717
Evan Cheng536e6672009-03-12 05:59:15 +000011718 if (!Ld || !ISD::isNormalLoad(Ld))
11719 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011720
Evan Cheng536e6672009-03-12 05:59:15 +000011721 // If this is not the MMX case, i.e. we are just turning i64 load/store
11722 // into f64 load/store, avoid the transformation if there are multiple
11723 // uses of the loaded value.
11724 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11725 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011726
Evan Cheng536e6672009-03-12 05:59:15 +000011727 DebugLoc LdDL = Ld->getDebugLoc();
11728 DebugLoc StDL = N->getDebugLoc();
11729 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11730 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11731 // pair instead.
11732 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011733 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011734 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11735 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011736 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011737 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011738 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011739 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011740 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011741 Ops.size());
11742 }
Evan Cheng536e6672009-03-12 05:59:15 +000011743 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011744 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011745 St->isVolatile(), St->isNonTemporal(),
11746 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011747 }
Evan Cheng536e6672009-03-12 05:59:15 +000011748
11749 // Otherwise, lower to two pairs of 32-bit loads / stores.
11750 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011751 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11752 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011753
Owen Anderson825b72b2009-08-11 20:47:22 +000011754 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011755 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011756 Ld->isVolatile(), Ld->isNonTemporal(),
11757 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011758 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011759 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011760 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011761 MinAlign(Ld->getAlignment(), 4));
11762
11763 SDValue NewChain = LoLd.getValue(1);
11764 if (TokenFactorIndex != -1) {
11765 Ops.push_back(LoLd);
11766 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011767 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011768 Ops.size());
11769 }
11770
11771 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011772 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11773 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011774
11775 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011776 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011777 St->isVolatile(), St->isNonTemporal(),
11778 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011779 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011780 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011781 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011782 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011783 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011784 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011785 }
Dan Gohman475871a2008-07-27 21:46:04 +000011786 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011787}
11788
Chris Lattner6cf73262008-01-25 06:14:17 +000011789/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11790/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011791static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011792 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11793 // F[X]OR(0.0, x) -> x
11794 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011795 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11796 if (C->getValueAPF().isPosZero())
11797 return N->getOperand(1);
11798 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11799 if (C->getValueAPF().isPosZero())
11800 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011801 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011802}
11803
11804/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011805static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011806 // FAND(0.0, x) -> 0.0
11807 // FAND(x, 0.0) -> 0.0
11808 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11809 if (C->getValueAPF().isPosZero())
11810 return N->getOperand(0);
11811 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11812 if (C->getValueAPF().isPosZero())
11813 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011814 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011815}
11816
Dan Gohmane5af2d32009-01-29 01:59:02 +000011817static SDValue PerformBTCombine(SDNode *N,
11818 SelectionDAG &DAG,
11819 TargetLowering::DAGCombinerInfo &DCI) {
11820 // BT ignores high bits in the bit index operand.
11821 SDValue Op1 = N->getOperand(1);
11822 if (Op1.hasOneUse()) {
11823 unsigned BitWidth = Op1.getValueSizeInBits();
11824 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11825 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011826 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11827 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011828 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011829 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11830 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11831 DCI.CommitTargetLoweringOpt(TLO);
11832 }
11833 return SDValue();
11834}
Chris Lattner83e6c992006-10-04 06:57:07 +000011835
Eli Friedman7a5e5552009-06-07 06:52:44 +000011836static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11837 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011838 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011839 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011840 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011841 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011842 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011843 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011844 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011845 }
11846 return SDValue();
11847}
11848
Evan Cheng2e489c42009-12-16 00:53:11 +000011849static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11850 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11851 // (and (i32 x86isd::setcc_carry), 1)
11852 // This eliminates the zext. This transformation is necessary because
11853 // ISD::SETCC is always legalized to i8.
11854 DebugLoc dl = N->getDebugLoc();
11855 SDValue N0 = N->getOperand(0);
11856 EVT VT = N->getValueType(0);
11857 if (N0.getOpcode() == ISD::AND &&
11858 N0.hasOneUse() &&
11859 N0.getOperand(0).hasOneUse()) {
11860 SDValue N00 = N0.getOperand(0);
11861 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11862 return SDValue();
11863 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11864 if (!C || C->getZExtValue() != 1)
11865 return SDValue();
11866 return DAG.getNode(ISD::AND, dl, VT,
11867 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11868 N00.getOperand(0), N00.getOperand(1)),
11869 DAG.getConstant(1, VT));
11870 }
11871
11872 return SDValue();
11873}
11874
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011875// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11876static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11877 unsigned X86CC = N->getConstantOperandVal(0);
11878 SDValue EFLAG = N->getOperand(1);
11879 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011880
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011881 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11882 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11883 // cases.
11884 if (X86CC == X86::COND_B)
11885 return DAG.getNode(ISD::AND, DL, MVT::i8,
11886 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11887 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11888 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011889
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011890 return SDValue();
11891}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011892
Chris Lattner23a01992010-12-20 01:37:09 +000011893// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11894static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11895 X86TargetLowering::DAGCombinerInfo &DCI) {
11896 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11897 // the result is either zero or one (depending on the input carry bit).
11898 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11899 if (X86::isZeroNode(N->getOperand(0)) &&
11900 X86::isZeroNode(N->getOperand(1)) &&
11901 // We don't have a good way to replace an EFLAGS use, so only do this when
11902 // dead right now.
11903 SDValue(N, 1).use_empty()) {
11904 DebugLoc DL = N->getDebugLoc();
11905 EVT VT = N->getValueType(0);
11906 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11907 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11908 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11909 DAG.getConstant(X86::COND_B,MVT::i8),
11910 N->getOperand(2)),
11911 DAG.getConstant(1, VT));
11912 return DCI.CombineTo(N, Res1, CarryOut);
11913 }
11914
11915 return SDValue();
11916}
11917
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011918// fold (add Y, (sete X, 0)) -> adc 0, Y
11919// (add Y, (setne X, 0)) -> sbb -1, Y
11920// (sub (sete X, 0), Y) -> sbb 0, Y
11921// (sub (setne X, 0), Y) -> adc -1, Y
11922static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11923 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011924
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011925 // Look through ZExts.
11926 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11927 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11928 return SDValue();
11929
11930 SDValue SetCC = Ext.getOperand(0);
11931 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11932 return SDValue();
11933
11934 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11935 if (CC != X86::COND_E && CC != X86::COND_NE)
11936 return SDValue();
11937
11938 SDValue Cmp = SetCC.getOperand(1);
11939 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011940 !X86::isZeroNode(Cmp.getOperand(1)) ||
11941 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011942 return SDValue();
11943
11944 SDValue CmpOp0 = Cmp.getOperand(0);
11945 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11946 DAG.getConstant(1, CmpOp0.getValueType()));
11947
11948 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11949 if (CC == X86::COND_NE)
11950 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11951 DL, OtherVal.getValueType(), OtherVal,
11952 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11953 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11954 DL, OtherVal.getValueType(), OtherVal,
11955 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11956}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011957
Dan Gohman475871a2008-07-27 21:46:04 +000011958SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011959 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011960 SelectionDAG &DAG = DCI.DAG;
11961 switch (N->getOpcode()) {
11962 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011963 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011964 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011965 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011966 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011967 case ISD::ADD:
11968 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011969 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011970 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011971 case ISD::SHL:
11972 case ISD::SRA:
11973 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011974 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011975 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011976 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011977 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011978 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11979 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011980 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011981 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011982 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011983 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011984 case X86ISD::SHUFPS: // Handle all target specific shuffles
11985 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011986 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011987 case X86ISD::PUNPCKHBW:
11988 case X86ISD::PUNPCKHWD:
11989 case X86ISD::PUNPCKHDQ:
11990 case X86ISD::PUNPCKHQDQ:
11991 case X86ISD::UNPCKHPS:
11992 case X86ISD::UNPCKHPD:
11993 case X86ISD::PUNPCKLBW:
11994 case X86ISD::PUNPCKLWD:
11995 case X86ISD::PUNPCKLDQ:
11996 case X86ISD::PUNPCKLQDQ:
11997 case X86ISD::UNPCKLPS:
11998 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000011999 case X86ISD::VUNPCKLPS:
12000 case X86ISD::VUNPCKLPD:
12001 case X86ISD::VUNPCKLPSY:
12002 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012003 case X86ISD::MOVHLPS:
12004 case X86ISD::MOVLHPS:
12005 case X86ISD::PSHUFD:
12006 case X86ISD::PSHUFHW:
12007 case X86ISD::PSHUFLW:
12008 case X86ISD::MOVSS:
12009 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012010 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012011 }
12012
Dan Gohman475871a2008-07-27 21:46:04 +000012013 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012014}
12015
Evan Chenge5b51ac2010-04-17 06:13:15 +000012016/// isTypeDesirableForOp - Return true if the target has native support for
12017/// the specified value type and it is 'desirable' to use the type for the
12018/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12019/// instruction encodings are longer and some i16 instructions are slow.
12020bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12021 if (!isTypeLegal(VT))
12022 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012023 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012024 return true;
12025
12026 switch (Opc) {
12027 default:
12028 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012029 case ISD::LOAD:
12030 case ISD::SIGN_EXTEND:
12031 case ISD::ZERO_EXTEND:
12032 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012033 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012034 case ISD::SRL:
12035 case ISD::SUB:
12036 case ISD::ADD:
12037 case ISD::MUL:
12038 case ISD::AND:
12039 case ISD::OR:
12040 case ISD::XOR:
12041 return false;
12042 }
12043}
12044
12045/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012046/// beneficial for dag combiner to promote the specified node. If true, it
12047/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012048bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012049 EVT VT = Op.getValueType();
12050 if (VT != MVT::i16)
12051 return false;
12052
Evan Cheng4c26e932010-04-19 19:29:22 +000012053 bool Promote = false;
12054 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012055 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012056 default: break;
12057 case ISD::LOAD: {
12058 LoadSDNode *LD = cast<LoadSDNode>(Op);
12059 // If the non-extending load has a single use and it's not live out, then it
12060 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012061 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12062 Op.hasOneUse()*/) {
12063 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12064 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12065 // The only case where we'd want to promote LOAD (rather then it being
12066 // promoted as an operand is when it's only use is liveout.
12067 if (UI->getOpcode() != ISD::CopyToReg)
12068 return false;
12069 }
12070 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012071 Promote = true;
12072 break;
12073 }
12074 case ISD::SIGN_EXTEND:
12075 case ISD::ZERO_EXTEND:
12076 case ISD::ANY_EXTEND:
12077 Promote = true;
12078 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012079 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012080 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012081 SDValue N0 = Op.getOperand(0);
12082 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012083 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012084 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012085 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012086 break;
12087 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012088 case ISD::ADD:
12089 case ISD::MUL:
12090 case ISD::AND:
12091 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012092 case ISD::XOR:
12093 Commute = true;
12094 // fallthrough
12095 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012096 SDValue N0 = Op.getOperand(0);
12097 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012098 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012099 return false;
12100 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012101 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012102 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012103 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012104 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012105 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012106 }
12107 }
12108
12109 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012110 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012111}
12112
Evan Cheng60c07e12006-07-05 22:17:51 +000012113//===----------------------------------------------------------------------===//
12114// X86 Inline Assembly Support
12115//===----------------------------------------------------------------------===//
12116
Chris Lattnerb8105652009-07-20 17:51:36 +000012117bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12118 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012119
12120 std::string AsmStr = IA->getAsmString();
12121
12122 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012123 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012124 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012125
12126 switch (AsmPieces.size()) {
12127 default: return false;
12128 case 1:
12129 AsmStr = AsmPieces[0];
12130 AsmPieces.clear();
12131 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12132
Evan Cheng55d42002011-01-08 01:24:27 +000012133 // FIXME: this should verify that we are targetting a 486 or better. If not,
12134 // we will turn this bswap into something that will be lowered to logical ops
12135 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12136 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012137 // bswap $0
12138 if (AsmPieces.size() == 2 &&
12139 (AsmPieces[0] == "bswap" ||
12140 AsmPieces[0] == "bswapq" ||
12141 AsmPieces[0] == "bswapl") &&
12142 (AsmPieces[1] == "$0" ||
12143 AsmPieces[1] == "${0:q}")) {
12144 // No need to check constraints, nothing other than the equivalent of
12145 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012146 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12147 if (!Ty || Ty->getBitWidth() % 16 != 0)
12148 return false;
12149 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012150 }
12151 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012152 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012153 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012154 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012155 AsmPieces[1] == "$$8," &&
12156 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012157 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12158 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012159 const std::string &ConstraintsStr = IA->getConstraintString();
12160 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012161 std::sort(AsmPieces.begin(), AsmPieces.end());
12162 if (AsmPieces.size() == 4 &&
12163 AsmPieces[0] == "~{cc}" &&
12164 AsmPieces[1] == "~{dirflag}" &&
12165 AsmPieces[2] == "~{flags}" &&
12166 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012167 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12168 if (!Ty || Ty->getBitWidth() % 16 != 0)
12169 return false;
12170 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012171 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012172 }
12173 break;
12174 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012175 if (CI->getType()->isIntegerTy(32) &&
12176 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12177 SmallVector<StringRef, 4> Words;
12178 SplitString(AsmPieces[0], Words, " \t,");
12179 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12180 Words[2] == "${0:w}") {
12181 Words.clear();
12182 SplitString(AsmPieces[1], Words, " \t,");
12183 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12184 Words[2] == "$0") {
12185 Words.clear();
12186 SplitString(AsmPieces[2], Words, " \t,");
12187 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12188 Words[2] == "${0:w}") {
12189 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012190 const std::string &ConstraintsStr = IA->getConstraintString();
12191 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012192 std::sort(AsmPieces.begin(), AsmPieces.end());
12193 if (AsmPieces.size() == 4 &&
12194 AsmPieces[0] == "~{cc}" &&
12195 AsmPieces[1] == "~{dirflag}" &&
12196 AsmPieces[2] == "~{flags}" &&
12197 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012198 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12199 if (!Ty || Ty->getBitWidth() % 16 != 0)
12200 return false;
12201 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012202 }
12203 }
12204 }
12205 }
12206 }
Evan Cheng55d42002011-01-08 01:24:27 +000012207
12208 if (CI->getType()->isIntegerTy(64)) {
12209 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12210 if (Constraints.size() >= 2 &&
12211 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12212 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12213 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12214 SmallVector<StringRef, 4> Words;
12215 SplitString(AsmPieces[0], Words, " \t");
12216 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012217 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012218 SplitString(AsmPieces[1], Words, " \t");
12219 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12220 Words.clear();
12221 SplitString(AsmPieces[2], Words, " \t,");
12222 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12223 Words[2] == "%edx") {
12224 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12225 if (!Ty || Ty->getBitWidth() % 16 != 0)
12226 return false;
12227 return IntrinsicLowering::LowerToByteSwap(CI);
12228 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012229 }
12230 }
12231 }
12232 }
12233 break;
12234 }
12235 return false;
12236}
12237
12238
12239
Chris Lattnerf4dff842006-07-11 02:54:03 +000012240/// getConstraintType - Given a constraint letter, return the type of
12241/// constraint it is for this target.
12242X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012243X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12244 if (Constraint.size() == 1) {
12245 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012246 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012247 case 'q':
12248 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012249 case 'f':
12250 case 't':
12251 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012252 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012253 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012254 case 'Y':
12255 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012256 case 'a':
12257 case 'b':
12258 case 'c':
12259 case 'd':
12260 case 'S':
12261 case 'D':
12262 case 'A':
12263 return C_Register;
12264 case 'I':
12265 case 'J':
12266 case 'K':
12267 case 'L':
12268 case 'M':
12269 case 'N':
12270 case 'G':
12271 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012272 case 'e':
12273 case 'Z':
12274 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012275 default:
12276 break;
12277 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012278 }
Chris Lattner4234f572007-03-25 02:14:49 +000012279 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012280}
12281
John Thompson44ab89e2010-10-29 17:29:13 +000012282/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012283/// This object must already have been set up with the operand type
12284/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012285TargetLowering::ConstraintWeight
12286 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012287 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012288 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012289 Value *CallOperandVal = info.CallOperandVal;
12290 // If we don't have a value, we can't do a match,
12291 // but allow it at the lowest weight.
12292 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012293 return CW_Default;
12294 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012295 // Look at the constraint type.
12296 switch (*constraint) {
12297 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012298 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12299 case 'R':
12300 case 'q':
12301 case 'Q':
12302 case 'a':
12303 case 'b':
12304 case 'c':
12305 case 'd':
12306 case 'S':
12307 case 'D':
12308 case 'A':
12309 if (CallOperandVal->getType()->isIntegerTy())
12310 weight = CW_SpecificReg;
12311 break;
12312 case 'f':
12313 case 't':
12314 case 'u':
12315 if (type->isFloatingPointTy())
12316 weight = CW_SpecificReg;
12317 break;
12318 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012319 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012320 weight = CW_SpecificReg;
12321 break;
12322 case 'x':
12323 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012324 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012325 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012326 break;
12327 case 'I':
12328 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12329 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012330 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012331 }
12332 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012333 case 'J':
12334 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12335 if (C->getZExtValue() <= 63)
12336 weight = CW_Constant;
12337 }
12338 break;
12339 case 'K':
12340 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12341 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12342 weight = CW_Constant;
12343 }
12344 break;
12345 case 'L':
12346 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12347 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12348 weight = CW_Constant;
12349 }
12350 break;
12351 case 'M':
12352 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12353 if (C->getZExtValue() <= 3)
12354 weight = CW_Constant;
12355 }
12356 break;
12357 case 'N':
12358 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12359 if (C->getZExtValue() <= 0xff)
12360 weight = CW_Constant;
12361 }
12362 break;
12363 case 'G':
12364 case 'C':
12365 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12366 weight = CW_Constant;
12367 }
12368 break;
12369 case 'e':
12370 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12371 if ((C->getSExtValue() >= -0x80000000LL) &&
12372 (C->getSExtValue() <= 0x7fffffffLL))
12373 weight = CW_Constant;
12374 }
12375 break;
12376 case 'Z':
12377 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12378 if (C->getZExtValue() <= 0xffffffff)
12379 weight = CW_Constant;
12380 }
12381 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012382 }
12383 return weight;
12384}
12385
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012386/// LowerXConstraint - try to replace an X constraint, which matches anything,
12387/// with another that has more specific requirements based on the type of the
12388/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012389const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012390LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012391 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12392 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012393 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012394 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012395 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012396 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012397 return "x";
12398 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012399
Chris Lattner5e764232008-04-26 23:02:14 +000012400 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012401}
12402
Chris Lattner48884cd2007-08-25 00:47:38 +000012403/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12404/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012405void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012406 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012407 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012408 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012409 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012410
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012411 switch (Constraint) {
12412 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012413 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012414 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012415 if (C->getZExtValue() <= 31) {
12416 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012417 break;
12418 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012419 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012420 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012421 case 'J':
12422 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012423 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012424 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12425 break;
12426 }
12427 }
12428 return;
12429 case 'K':
12430 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012431 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012432 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12433 break;
12434 }
12435 }
12436 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012437 case 'N':
12438 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012439 if (C->getZExtValue() <= 255) {
12440 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012441 break;
12442 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012443 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012444 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012445 case 'e': {
12446 // 32-bit signed value
12447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012448 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12449 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012450 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012451 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012452 break;
12453 }
12454 // FIXME gcc accepts some relocatable values here too, but only in certain
12455 // memory models; it's complicated.
12456 }
12457 return;
12458 }
12459 case 'Z': {
12460 // 32-bit unsigned value
12461 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012462 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12463 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012464 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12465 break;
12466 }
12467 }
12468 // FIXME gcc accepts some relocatable values here too, but only in certain
12469 // memory models; it's complicated.
12470 return;
12471 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012472 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012473 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012474 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012475 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012476 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012477 break;
12478 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012479
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012480 // In any sort of PIC mode addresses need to be computed at runtime by
12481 // adding in a register or some sort of table lookup. These can't
12482 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012483 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012484 return;
12485
Chris Lattnerdc43a882007-05-03 16:52:29 +000012486 // If we are in non-pic codegen mode, we allow the address of a global (with
12487 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012488 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012489 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012490
Chris Lattner49921962009-05-08 18:23:14 +000012491 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12492 while (1) {
12493 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12494 Offset += GA->getOffset();
12495 break;
12496 } else if (Op.getOpcode() == ISD::ADD) {
12497 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12498 Offset += C->getZExtValue();
12499 Op = Op.getOperand(0);
12500 continue;
12501 }
12502 } else if (Op.getOpcode() == ISD::SUB) {
12503 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12504 Offset += -C->getZExtValue();
12505 Op = Op.getOperand(0);
12506 continue;
12507 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012508 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012509
Chris Lattner49921962009-05-08 18:23:14 +000012510 // Otherwise, this isn't something we can handle, reject it.
12511 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012512 }
Eric Christopherfd179292009-08-27 18:07:15 +000012513
Dan Gohman46510a72010-04-15 01:51:59 +000012514 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012515 // If we require an extra load to get this address, as in PIC mode, we
12516 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012517 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12518 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012519 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012520
Devang Patel0d881da2010-07-06 22:08:15 +000012521 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12522 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012523 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012524 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012525 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012526
Gabor Greifba36cb52008-08-28 21:40:38 +000012527 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012528 Ops.push_back(Result);
12529 return;
12530 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012531 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012532}
12533
Chris Lattner259e97c2006-01-31 19:43:35 +000012534std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012535getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012536 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012537 if (Constraint.size() == 1) {
12538 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012539 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012540 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012541 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12542 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012543 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012544 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12545 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12546 X86::R10D,X86::R11D,X86::R12D,
12547 X86::R13D,X86::R14D,X86::R15D,
12548 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012549 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012550 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12551 X86::SI, X86::DI, X86::R8W,X86::R9W,
12552 X86::R10W,X86::R11W,X86::R12W,
12553 X86::R13W,X86::R14W,X86::R15W,
12554 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012555 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012556 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12557 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12558 X86::R10B,X86::R11B,X86::R12B,
12559 X86::R13B,X86::R14B,X86::R15B,
12560 X86::BPL, X86::SPL, 0);
12561
Owen Anderson825b72b2009-08-11 20:47:22 +000012562 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012563 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12564 X86::RSI, X86::RDI, X86::R8, X86::R9,
12565 X86::R10, X86::R11, X86::R12,
12566 X86::R13, X86::R14, X86::R15,
12567 X86::RBP, X86::RSP, 0);
12568
12569 break;
12570 }
Eric Christopherfd179292009-08-27 18:07:15 +000012571 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012572 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012573 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012574 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012575 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012576 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012577 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012578 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012579 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012580 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12581 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012582 }
12583 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012584
Chris Lattner1efa40f2006-02-22 00:56:39 +000012585 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012586}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012587
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012588std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012589X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012590 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012591 // First, see if this is a constraint that directly corresponds to an LLVM
12592 // register class.
12593 if (Constraint.size() == 1) {
12594 // GCC Constraint Letters
12595 switch (Constraint[0]) {
12596 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012597 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012598 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012599 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012600 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012601 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012602 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012603 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012604 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012605 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012606 case 'R': // LEGACY_REGS
12607 if (VT == MVT::i8)
12608 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12609 if (VT == MVT::i16)
12610 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12611 if (VT == MVT::i32 || !Subtarget->is64Bit())
12612 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12613 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012614 case 'f': // FP Stack registers.
12615 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12616 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012617 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012618 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012619 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012620 return std::make_pair(0U, X86::RFP64RegisterClass);
12621 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012622 case 'y': // MMX_REGS if MMX allowed.
12623 if (!Subtarget->hasMMX()) break;
12624 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012625 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012626 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012627 // FALL THROUGH.
12628 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012629 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012630
Owen Anderson825b72b2009-08-11 20:47:22 +000012631 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012632 default: break;
12633 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012634 case MVT::f32:
12635 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012636 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012637 case MVT::f64:
12638 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012639 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012640 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012641 case MVT::v16i8:
12642 case MVT::v8i16:
12643 case MVT::v4i32:
12644 case MVT::v2i64:
12645 case MVT::v4f32:
12646 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012647 return std::make_pair(0U, X86::VR128RegisterClass);
12648 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012649 break;
12650 }
12651 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012652
Chris Lattnerf76d1802006-07-31 23:26:50 +000012653 // Use the default implementation in TargetLowering to convert the register
12654 // constraint into a member of a register class.
12655 std::pair<unsigned, const TargetRegisterClass*> Res;
12656 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012657
12658 // Not found as a standard register?
12659 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012660 // Map st(0) -> st(7) -> ST0
12661 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12662 tolower(Constraint[1]) == 's' &&
12663 tolower(Constraint[2]) == 't' &&
12664 Constraint[3] == '(' &&
12665 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12666 Constraint[5] == ')' &&
12667 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012668
Chris Lattner56d77c72009-09-13 22:41:48 +000012669 Res.first = X86::ST0+Constraint[4]-'0';
12670 Res.second = X86::RFP80RegisterClass;
12671 return Res;
12672 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012673
Chris Lattner56d77c72009-09-13 22:41:48 +000012674 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012675 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012676 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012677 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012678 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012679 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012680
12681 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012682 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012683 Res.first = X86::EFLAGS;
12684 Res.second = X86::CCRRegisterClass;
12685 return Res;
12686 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012687
Dale Johannesen330169f2008-11-13 21:52:36 +000012688 // 'A' means EAX + EDX.
12689 if (Constraint == "A") {
12690 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012691 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012692 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012693 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012694 return Res;
12695 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012696
Chris Lattnerf76d1802006-07-31 23:26:50 +000012697 // Otherwise, check to see if this is a register class of the wrong value
12698 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12699 // turn into {ax},{dx}.
12700 if (Res.second->hasType(VT))
12701 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012702
Chris Lattnerf76d1802006-07-31 23:26:50 +000012703 // All of the single-register GCC register classes map their values onto
12704 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12705 // really want an 8-bit or 32-bit register, map to the appropriate register
12706 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012707 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012708 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012709 unsigned DestReg = 0;
12710 switch (Res.first) {
12711 default: break;
12712 case X86::AX: DestReg = X86::AL; break;
12713 case X86::DX: DestReg = X86::DL; break;
12714 case X86::CX: DestReg = X86::CL; break;
12715 case X86::BX: DestReg = X86::BL; break;
12716 }
12717 if (DestReg) {
12718 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012719 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012720 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012721 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012722 unsigned DestReg = 0;
12723 switch (Res.first) {
12724 default: break;
12725 case X86::AX: DestReg = X86::EAX; break;
12726 case X86::DX: DestReg = X86::EDX; break;
12727 case X86::CX: DestReg = X86::ECX; break;
12728 case X86::BX: DestReg = X86::EBX; break;
12729 case X86::SI: DestReg = X86::ESI; break;
12730 case X86::DI: DestReg = X86::EDI; break;
12731 case X86::BP: DestReg = X86::EBP; break;
12732 case X86::SP: DestReg = X86::ESP; break;
12733 }
12734 if (DestReg) {
12735 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012736 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012737 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012738 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012739 unsigned DestReg = 0;
12740 switch (Res.first) {
12741 default: break;
12742 case X86::AX: DestReg = X86::RAX; break;
12743 case X86::DX: DestReg = X86::RDX; break;
12744 case X86::CX: DestReg = X86::RCX; break;
12745 case X86::BX: DestReg = X86::RBX; break;
12746 case X86::SI: DestReg = X86::RSI; break;
12747 case X86::DI: DestReg = X86::RDI; break;
12748 case X86::BP: DestReg = X86::RBP; break;
12749 case X86::SP: DestReg = X86::RSP; break;
12750 }
12751 if (DestReg) {
12752 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012753 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012754 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012755 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012756 } else if (Res.second == X86::FR32RegisterClass ||
12757 Res.second == X86::FR64RegisterClass ||
12758 Res.second == X86::VR128RegisterClass) {
12759 // Handle references to XMM physical registers that got mapped into the
12760 // wrong class. This can happen with constraints like {xmm0} where the
12761 // target independent register mapper will just pick the first match it can
12762 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012763 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012764 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012765 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012766 Res.second = X86::FR64RegisterClass;
12767 else if (X86::VR128RegisterClass->hasType(VT))
12768 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012769 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012770
Chris Lattnerf76d1802006-07-31 23:26:50 +000012771 return Res;
12772}