blob: b7599f097fb0796526912709f29e25902578ab94 [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"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.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
83/// simple subregister reference.
84static SDValue Extract128BitVector(SDValue Vec,
85 SDValue Idx,
86 SelectionDAG &DAG,
87 DebugLoc dl) {
88 EVT VT = Vec.getValueType();
89 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
90
91 EVT ElVT = VT.getVectorElementType();
92
93 int Factor = VT.getSizeInBits() / 128;
94
95 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
96 ElVT,
97 VT.getVectorNumElements() / Factor);
98
99 // Extract from UNDEF is UNDEF.
100 if (Vec.getOpcode() == ISD::UNDEF)
101 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
102
103 if (isa<ConstantSDNode>(Idx)) {
104 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
105
106 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
107 // we can match to VEXTRACTF128.
108 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
109
110 // This is the index of the first element of the 128-bit chunk
111 // we want.
112 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
113 * ElemsPerChunk);
114
115 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
116
117 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
118 VecIdx);
119
120 return Result;
121 }
122
123 return SDValue();
124}
125
126/// Generate a DAG to put 128-bits into a vector > 128 bits. This
127/// sets things up to match to an AVX VINSERTF128 instruction or a
128/// simple superregister reference.
129static SDValue Insert128BitVector(SDValue Result,
130 SDValue Vec,
131 SDValue Idx,
132 SelectionDAG &DAG,
133 DebugLoc dl) {
134 if (isa<ConstantSDNode>(Idx)) {
135 EVT VT = Vec.getValueType();
136 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
137
138 EVT ElVT = VT.getVectorElementType();
139
140 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
141
142 EVT ResultVT = Result.getValueType();
143
144 // Insert the relevant 128 bits.
145 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
146
147 // This is the index of the first element of the 128-bit chunk
148 // we want.
149 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
150 * ElemsPerChunk);
151
152 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
153
154 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
155 VecIdx);
156 return Result;
157 }
158
159 return SDValue();
160}
161
David Greenef125a292011-02-08 19:04:41 +0000162/// Given two vectors, concat them.
163static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
164 DebugLoc dl = Lower.getDebugLoc();
165
166 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
167
168 EVT VT = EVT::getVectorVT(*DAG.getContext(),
169 Lower.getValueType().getVectorElementType(),
170 Lower.getValueType().getVectorNumElements() * 2);
171
172 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
173 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
174
175 // Insert the upper subvector.
176 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
177 DAG.getConstant(
178 // This is half the length of the result
179 // vector. Start inserting the upper 128
180 // bits here.
181 Lower.getValueType().getVectorNumElements(),
182 MVT::i32),
183 DAG, dl);
184
185 // Insert the lower subvector.
186 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
187 return Vec;
188}
189
Chris Lattnerf0144122009-07-28 03:13:23 +0000190static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000191 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
192 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000193
Evan Cheng2bffee22011-02-01 01:14:13 +0000194 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000195 if (is64Bit)
196 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000197 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000198 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000199
Evan Cheng2bffee22011-02-01 01:14:13 +0000200 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000201 if (is64Bit)
202 return new X8664_ELFTargetObjectFile(TM);
203 return new X8632_ELFTargetObjectFile(TM);
204 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000205 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000206 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000207 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000208}
209
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000210X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000211 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000212 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000213 X86ScalarSSEf64 = Subtarget->hasXMMInt();
214 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000216
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000217 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000218 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000219
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000221 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222
223 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000224 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000225 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +0000226 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000228
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000229 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000230 // Setup Windows compiler runtime calls.
231 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000232 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
233 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000234 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000235 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000236 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000237 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
238 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000239 }
240
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000241 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000242 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000243 setUseUnderscoreSetJmp(false);
244 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000245 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000246 // MS runtime is weird: it exports _setjmp, but longjmp!
247 setUseUnderscoreSetJmp(true);
248 setUseUnderscoreLongJmp(false);
249 } else {
250 setUseUnderscoreSetJmp(true);
251 setUseUnderscoreLongJmp(true);
252 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000253
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000254 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000256 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000258 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000260
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000262
Scott Michelfdc40a02009-02-17 22:15:04 +0000263 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000265 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000267 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
269 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000270
271 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
273 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
274 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
275 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
276 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
277 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000278
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000279 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
280 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
282 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
283 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000284
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000288 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000289 // We have an algorithm for SSE2->double, and we turn this into a
290 // 64-bit FILD followed by conditional FADD for other targets.
291 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000292 // We have an algorithm for SSE2, and we turn this into a 64-bit
293 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000294 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000295 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000296
297 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
298 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
300 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000301
Devang Patel6a784892009-06-05 18:48:29 +0000302 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000303 // SSE has no i16 to fp conversion, only i32
304 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000308 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
310 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000311 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000312 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
314 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000315 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000316
Dale Johannesen73328d12007-09-19 23:55:34 +0000317 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
318 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
320 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000321
Evan Cheng02568ff2006-01-30 22:13:22 +0000322 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
323 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
325 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000326
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000327 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000329 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000331 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
333 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000334 }
335
336 // Handle FP_TO_UINT by promoting the destination to a larger signed
337 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
339 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
340 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000341
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000345 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000346 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 // Expand FP_TO_UINT into a select.
348 // FIXME: We would like to use a Custom expander here eventually to do
349 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000351 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000352 // With SSE3 we can use fisttpll to convert to a signed i64; without
353 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000355 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000356
Chris Lattner399610a2006-12-05 18:22:22 +0000357 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000358 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000359 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
360 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000361 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000362 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000363 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000365 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000366 }
Chris Lattner21f66852005-12-23 05:15:23 +0000367
Dan Gohmanb00ee212008-02-18 19:34:53 +0000368 // Scalar integer divide and remainder are lowered to use operations that
369 // produce two results, to match the available instructions. This exposes
370 // the two-result form to trivial CSE, which is able to combine x/y and x%y
371 // into a single instruction.
372 //
373 // Scalar integer multiply-high is also lowered to use two-result
374 // operations, to match the available instructions. However, plain multiply
375 // (low) operations are left as Legal, as there are single-result
376 // instructions for this in x86. Using the two-result multiply instructions
377 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000378 for (unsigned i = 0, e = 4; i != e; ++i) {
379 MVT VT = IntVTs[i];
380 setOperationAction(ISD::MULHS, VT, Expand);
381 setOperationAction(ISD::MULHU, VT, Expand);
382 setOperationAction(ISD::SDIV, VT, Expand);
383 setOperationAction(ISD::UDIV, VT, Expand);
384 setOperationAction(ISD::SREM, VT, Expand);
385 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000386
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000387 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000388 setOperationAction(ISD::ADDC, VT, Custom);
389 setOperationAction(ISD::ADDE, VT, Custom);
390 setOperationAction(ISD::SUBC, VT, Custom);
391 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000392 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000393
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
395 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
396 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
397 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000398 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
400 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
401 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
402 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
403 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
404 setOperationAction(ISD::FREM , MVT::f32 , Expand);
405 setOperationAction(ISD::FREM , MVT::f64 , Expand);
406 setOperationAction(ISD::FREM , MVT::f80 , Expand);
407 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000408
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
410 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000411 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
412 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
414 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000415 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000418 }
419
Benjamin Kramer1292c222010-12-04 20:32:23 +0000420 if (Subtarget->hasPOPCNT()) {
421 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
422 } else {
423 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
424 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
425 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
426 if (Subtarget->is64Bit())
427 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
428 }
429
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
431 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000432
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000434 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000436 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000437 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
439 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
440 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
441 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
442 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000443 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
445 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
446 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
447 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000448 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
450 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000453
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000454 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
456 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
457 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
458 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000459 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
461 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000462 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000463 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
465 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
466 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
467 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000468 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000469 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000470 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
472 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
473 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000474 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
476 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
477 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000478 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000479
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000480 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000482
Eric Christopher9a9d2752010-07-22 02:48:34 +0000483 // We may not have a libcall for MEMBARRIER so we should lower this.
484 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000485
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000486 // On X86 and X86-64, atomic operations are lowered to locked instructions.
487 // Locked instructions, in turn, have implicit fence semantics (all memory
488 // operations are flushed before issuing the locked instruction, and they
489 // are not buffered), so we can fold away the common pattern of
490 // fence-atomic-fence.
491 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000492
Mon P Wang63307c32008-05-05 19:05:59 +0000493 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000494 for (unsigned i = 0, e = 4; i != e; ++i) {
495 MVT VT = IntVTs[i];
496 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
497 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
498 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000499
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000500 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
503 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
505 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 }
509
Evan Cheng3c992d22006-03-07 02:02:57 +0000510 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000511 if (!Subtarget->isTargetDarwin() &&
512 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000513 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000515 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000516
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
518 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
519 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
520 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000521 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000522 setExceptionPointerRegister(X86::RAX);
523 setExceptionSelectorRegister(X86::RDX);
524 } else {
525 setExceptionPointerRegister(X86::EAX);
526 setExceptionSelectorRegister(X86::EDX);
527 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
529 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000530
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000532
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000534
Nate Begemanacc398c2006-01-25 18:21:52 +0000535 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::VASTART , MVT::Other, Custom);
537 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000538 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::VAARG , MVT::Other, Custom);
540 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000541 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::VAARG , MVT::Other, Expand);
543 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000544 }
Evan Chengae642192007-03-02 23:16:35 +0000545
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
547 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000548 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000550 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000552 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000554
Evan Chengc7ce29b2009-02-13 22:36:38 +0000555 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000556 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000557 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
559 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000560
Evan Cheng223547a2006-01-31 22:28:30 +0000561 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FABS , MVT::f64, Custom);
563 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000564
565 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FNEG , MVT::f64, Custom);
567 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000568
Evan Cheng68c47cb2007-01-05 07:55:56 +0000569 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
571 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000572
Evan Chengd25e9e82006-02-02 00:28:23 +0000573 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FSIN , MVT::f64, Expand);
575 setOperationAction(ISD::FCOS , MVT::f64, Expand);
576 setOperationAction(ISD::FSIN , MVT::f32, Expand);
577 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000578
Chris Lattnera54aa942006-01-29 06:26:08 +0000579 // Expand FP immediates into loads from the stack, except for the special
580 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581 addLegalFPImmediate(APFloat(+0.0)); // xorpd
582 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000583 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584 // Use SSE for f32, x87 for f64.
585 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
587 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
592 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000594
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
599 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
601 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::FSIN , MVT::f32, Expand);
603 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
Nate Begemane1795842008-02-14 08:57:00 +0000605 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000606 addLegalFPImmediate(APFloat(+0.0f)); // xorps
607 addLegalFPImmediate(APFloat(+0.0)); // FLD0
608 addLegalFPImmediate(APFloat(+1.0)); // FLD1
609 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
610 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
611
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
614 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000616 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000617 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000618 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
620 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000621
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
623 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
624 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
625 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000626
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
629 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000630 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000631 addLegalFPImmediate(APFloat(+0.0)); // FLD0
632 addLegalFPImmediate(APFloat(+1.0)); // FLD1
633 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
634 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000635 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
636 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
637 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
638 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000639 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000640
Dale Johannesen59a58732007-08-05 18:49:15 +0000641 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000642 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
644 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
645 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000646 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000647 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000648 addLegalFPImmediate(TmpFlt); // FLD0
649 TmpFlt.changeSign();
650 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000651
652 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 APFloat TmpFlt2(+1.0);
654 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
655 &ignored);
656 addLegalFPImmediate(TmpFlt2); // FLD1
657 TmpFlt2.changeSign();
658 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
659 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
663 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000665 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000666
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000667 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
669 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
670 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FLOG, MVT::f80, Expand);
673 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
674 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
675 setOperationAction(ISD::FEXP, MVT::f80, Expand);
676 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000677
Mon P Wangf007a8b2008-11-06 05:31:54 +0000678 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000679 // (for widening) or expand (for scalarization). Then we will selectively
680 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
682 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
683 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000699 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
700 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000732 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000733 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
737 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
738 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
739 setTruncStoreAction((MVT::SimpleValueType)VT,
740 (MVT::SimpleValueType)InnerVT, Expand);
741 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
742 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
743 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000744 }
745
Evan Chengc7ce29b2009-02-13 22:36:38 +0000746 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
747 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000748 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000749 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000750 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000751 }
752
Dale Johannesen0488fb62010-09-30 23:57:10 +0000753 // MMX-sized vectors (other than x86mmx) are expected to be expanded
754 // into smaller operations.
755 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
756 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
757 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
758 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
759 setOperationAction(ISD::AND, MVT::v8i8, Expand);
760 setOperationAction(ISD::AND, MVT::v4i16, Expand);
761 setOperationAction(ISD::AND, MVT::v2i32, Expand);
762 setOperationAction(ISD::AND, MVT::v1i64, Expand);
763 setOperationAction(ISD::OR, MVT::v8i8, Expand);
764 setOperationAction(ISD::OR, MVT::v4i16, Expand);
765 setOperationAction(ISD::OR, MVT::v2i32, Expand);
766 setOperationAction(ISD::OR, MVT::v1i64, Expand);
767 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
768 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
769 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
770 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
771 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
776 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
777 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
778 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
779 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000780 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
781 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
782 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
783 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000784
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000785 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000787
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
789 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
790 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
791 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
792 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
793 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
794 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
795 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
796 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
797 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
798 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
799 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000800 }
801
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000802 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000804
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000805 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
806 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
808 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
809 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
810 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000811
Owen Anderson825b72b2009-08-11 20:47:22 +0000812 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
813 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
814 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
815 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
816 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
817 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
818 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
819 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
820 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
821 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
822 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
823 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
824 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
825 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
826 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
827 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
830 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
831 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
832 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000833
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
835 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
836 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
837 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
838 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000839
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000840 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
841 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
842 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
843 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
845
Evan Cheng2c3ae372006-04-12 21:21:57 +0000846 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000847 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
848 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000849 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000850 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000851 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000852 // Do not attempt to custom lower non-128-bit vectors
853 if (!VT.is128BitVector())
854 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 setOperationAction(ISD::BUILD_VECTOR,
856 VT.getSimpleVT().SimpleTy, Custom);
857 setOperationAction(ISD::VECTOR_SHUFFLE,
858 VT.getSimpleVT().SimpleTy, Custom);
859 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
860 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000861 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000862
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
864 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
865 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
866 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
867 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
868 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000869
Nate Begemancdd1eec2008-02-12 22:51:28 +0000870 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000873 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000874
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000875 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000876 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
877 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000878 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000879
880 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000881 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000882 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000883
Owen Andersond6662ad2009-08-10 20:46:15 +0000884 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000886 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000894 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000895
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000897
Evan Cheng2c3ae372006-04-12 21:21:57 +0000898 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
900 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
901 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
902 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
905 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000906 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000907
Nate Begeman14d12ca2008-02-11 04:19:36 +0000908 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000909 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
910 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
911 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
912 setOperationAction(ISD::FRINT, MVT::f32, Legal);
913 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
914 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
915 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
916 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
917 setOperationAction(ISD::FRINT, MVT::f64, Legal);
918 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
919
Nate Begeman14d12ca2008-02-11 04:19:36 +0000920 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000922
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000923 // Can turn SHL into an integer multiply.
924 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000925 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000926
Nate Begeman14d12ca2008-02-11 04:19:36 +0000927 // i8 and i16 vectors are custom , because the source register and source
928 // source memory operand types are not the same width. f32 vectors are
929 // custom since the immediate controlling the insert encodes additional
930 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
934 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000935
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
939 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
941 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944 }
945 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000946
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000947 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000949
David Greene9b9838d2009-06-29 16:47:10 +0000950 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
952 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
953 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
954 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000955 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000956
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
958 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
959 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
960 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000961
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
963 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
964 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
965 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
966 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
967 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000968
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
970 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
971 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
972 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
973 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
974 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000975
David Greene54d8eba2011-01-27 22:38:56 +0000976 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
977 // insert_vector_elt extract_subvector and extract_vector_elt for
978 // 256-bit types.
979 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
980 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
981 ++i) {
982 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
983 // Do not attempt to custom lower non-256-bit vectors
984 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
985 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000986 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000987 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
988 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000989 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000990 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000991 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000992 }
David Greene54d8eba2011-01-27 22:38:56 +0000993 // Custom-lower insert_subvector and extract_subvector based on
994 // the result type.
995 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
996 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
997 ++i) {
998 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
999 // Do not attempt to custom lower non-256-bit vectors
1000 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001001 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001002
1003 if (MVT(VT).getSizeInBits() == 128) {
1004 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001005 }
David Greene54d8eba2011-01-27 22:38:56 +00001006 else if (MVT(VT).getSizeInBits() == 256) {
1007 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1008 }
David Greene9b9838d2009-06-29 16:47:10 +00001009 }
1010
David Greene54d8eba2011-01-27 22:38:56 +00001011 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1012 // Don't promote loads because we need them for VPERM vector index versions.
1013
1014 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1015 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1016 VT++) {
1017 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1018 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1019 continue;
1020 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1021 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1022 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1023 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1024 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1025 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1026 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1027 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1028 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1029 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1030 }
David Greene9b9838d2009-06-29 16:47:10 +00001031 }
1032
Evan Cheng6be2c582006-04-05 23:38:46 +00001033 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001034 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001035
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001036
Eli Friedman962f5492010-06-02 19:35:46 +00001037 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1038 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001039 //
Eli Friedman962f5492010-06-02 19:35:46 +00001040 // FIXME: We really should do custom legalization for addition and
1041 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1042 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001043 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1044 // Add/Sub/Mul with overflow operations are custom lowered.
1045 MVT VT = IntVTs[i];
1046 setOperationAction(ISD::SADDO, VT, Custom);
1047 setOperationAction(ISD::UADDO, VT, Custom);
1048 setOperationAction(ISD::SSUBO, VT, Custom);
1049 setOperationAction(ISD::USUBO, VT, Custom);
1050 setOperationAction(ISD::SMULO, VT, Custom);
1051 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001052 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001053
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001054 // There are no 8-bit 3-address imul/mul instructions
1055 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1056 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001057
Evan Chengd54f2d52009-03-31 19:38:51 +00001058 if (!Subtarget->is64Bit()) {
1059 // These libcalls are not available in 32-bit.
1060 setLibcallName(RTLIB::SHL_I128, 0);
1061 setLibcallName(RTLIB::SRL_I128, 0);
1062 setLibcallName(RTLIB::SRA_I128, 0);
1063 }
1064
Evan Cheng206ee9d2006-07-07 08:33:52 +00001065 // We have target-specific dag combine patterns for the following nodes:
1066 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001067 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001068 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001069 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001070 setTargetDAGCombine(ISD::SHL);
1071 setTargetDAGCombine(ISD::SRA);
1072 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001073 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001074 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001075 setTargetDAGCombine(ISD::ADD);
1076 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001077 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001078 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001079 if (Subtarget->is64Bit())
1080 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001081
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001082 computeRegisterProperties();
1083
Evan Cheng05219282011-01-06 06:52:41 +00001084 // On Darwin, -Os means optimize for size without hurting performance,
1085 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001086 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001087 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001088 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001089 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1090 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1091 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001092 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001093 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001094}
1095
Scott Michel5b8f82e2008-03-10 15:42:14 +00001096
Owen Anderson825b72b2009-08-11 20:47:22 +00001097MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1098 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001099}
1100
1101
Evan Cheng29286502008-01-23 23:17:41 +00001102/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1103/// the desired ByVal argument alignment.
1104static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1105 if (MaxAlign == 16)
1106 return;
1107 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1108 if (VTy->getBitWidth() == 128)
1109 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001110 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1111 unsigned EltAlign = 0;
1112 getMaxByValAlign(ATy->getElementType(), EltAlign);
1113 if (EltAlign > MaxAlign)
1114 MaxAlign = EltAlign;
1115 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1116 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1117 unsigned EltAlign = 0;
1118 getMaxByValAlign(STy->getElementType(i), EltAlign);
1119 if (EltAlign > MaxAlign)
1120 MaxAlign = EltAlign;
1121 if (MaxAlign == 16)
1122 break;
1123 }
1124 }
1125 return;
1126}
1127
1128/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1129/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001130/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1131/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001132unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001133 if (Subtarget->is64Bit()) {
1134 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001135 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001136 if (TyAlign > 8)
1137 return TyAlign;
1138 return 8;
1139 }
1140
Evan Cheng29286502008-01-23 23:17:41 +00001141 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001142 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001143 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001144 return Align;
1145}
Chris Lattner2b02a442007-02-25 08:29:00 +00001146
Evan Chengf0df0312008-05-15 08:39:06 +00001147/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001148/// and store operations as a result of memset, memcpy, and memmove
1149/// lowering. If DstAlign is zero that means it's safe to destination
1150/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1151/// means there isn't a need to check it against alignment requirement,
1152/// probably because the source does not need to be loaded. If
1153/// 'NonScalarIntSafe' is true, that means it's safe to return a
1154/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1155/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1156/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001157/// It returns EVT::Other if the type should be determined using generic
1158/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001159EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001160X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1161 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001162 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001163 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001164 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001165 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1166 // linux. This is because the stack realignment code can't handle certain
1167 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001168 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001169 if (NonScalarIntSafe &&
1170 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001171 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001172 (Subtarget->isUnalignedMemAccessFast() ||
1173 ((DstAlign == 0 || DstAlign >= 16) &&
1174 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001175 Subtarget->getStackAlignment() >= 16) {
1176 if (Subtarget->hasSSE2())
1177 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001178 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001179 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001180 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001181 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001182 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001183 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001184 // Do not use f64 to lower memcpy if source is string constant. It's
1185 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001186 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001187 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001188 }
Evan Chengf0df0312008-05-15 08:39:06 +00001189 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001190 return MVT::i64;
1191 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001192}
1193
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001194/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1195/// current function. The returned value is a member of the
1196/// MachineJumpTableInfo::JTEntryKind enum.
1197unsigned X86TargetLowering::getJumpTableEncoding() const {
1198 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1199 // symbol.
1200 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1201 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001202 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001203
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001204 // Otherwise, use the normal jump table encoding heuristics.
1205 return TargetLowering::getJumpTableEncoding();
1206}
1207
Chris Lattnerc64daab2010-01-26 05:02:42 +00001208const MCExpr *
1209X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1210 const MachineBasicBlock *MBB,
1211 unsigned uid,MCContext &Ctx) const{
1212 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1213 Subtarget->isPICStyleGOT());
1214 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1215 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001216 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1217 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001218}
1219
Evan Chengcc415862007-11-09 01:32:10 +00001220/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1221/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001222SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001223 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001224 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001225 // This doesn't have DebugLoc associated with it, but is not really the
1226 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001227 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001228 return Table;
1229}
1230
Chris Lattner589c6f62010-01-26 06:28:43 +00001231/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1232/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1233/// MCExpr.
1234const MCExpr *X86TargetLowering::
1235getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1236 MCContext &Ctx) const {
1237 // X86-64 uses RIP relative addressing based on the jump table label.
1238 if (Subtarget->isPICStyleRIPRel())
1239 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1240
1241 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001242 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001243}
1244
Bill Wendlingb4202b82009-07-01 18:50:55 +00001245/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001246unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001247 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001248}
1249
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001250// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001251std::pair<const TargetRegisterClass*, uint8_t>
1252X86TargetLowering::findRepresentativeClass(EVT VT) const{
1253 const TargetRegisterClass *RRC = 0;
1254 uint8_t Cost = 1;
1255 switch (VT.getSimpleVT().SimpleTy) {
1256 default:
1257 return TargetLowering::findRepresentativeClass(VT);
1258 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1259 RRC = (Subtarget->is64Bit()
1260 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1261 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001262 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001263 RRC = X86::VR64RegisterClass;
1264 break;
1265 case MVT::f32: case MVT::f64:
1266 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1267 case MVT::v4f32: case MVT::v2f64:
1268 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1269 case MVT::v4f64:
1270 RRC = X86::VR128RegisterClass;
1271 break;
1272 }
1273 return std::make_pair(RRC, Cost);
1274}
1275
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001276// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001277unsigned
1278X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1279 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001280 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001281
1282 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001283 switch (RC->getID()) {
1284 default:
1285 return 0;
1286 case X86::GR32RegClassID:
1287 return 4 - FPDiff;
1288 case X86::GR64RegClassID:
1289 return 8 - FPDiff;
1290 case X86::VR128RegClassID:
1291 return Subtarget->is64Bit() ? 10 : 4;
1292 case X86::VR64RegClassID:
1293 return 4;
1294 }
1295}
1296
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001297bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1298 unsigned &Offset) const {
1299 if (!Subtarget->isTargetLinux())
1300 return false;
1301
1302 if (Subtarget->is64Bit()) {
1303 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1304 Offset = 0x28;
1305 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1306 AddressSpace = 256;
1307 else
1308 AddressSpace = 257;
1309 } else {
1310 // %gs:0x14 on i386
1311 Offset = 0x14;
1312 AddressSpace = 256;
1313 }
1314 return true;
1315}
1316
1317
Chris Lattner2b02a442007-02-25 08:29:00 +00001318//===----------------------------------------------------------------------===//
1319// Return Value Calling Convention Implementation
1320//===----------------------------------------------------------------------===//
1321
Chris Lattner59ed56b2007-02-28 04:55:35 +00001322#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001323
Michael J. Spencerec38de22010-10-10 22:04:20 +00001324bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001325X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001326 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001327 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001328 SmallVector<CCValAssign, 16> RVLocs;
1329 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001330 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001331 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001332}
1333
Dan Gohman98ca4f22009-08-05 01:29:28 +00001334SDValue
1335X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001336 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001338 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001339 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001340 MachineFunction &MF = DAG.getMachineFunction();
1341 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001342
Chris Lattner9774c912007-02-27 05:28:59 +00001343 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001344 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1345 RVLocs, *DAG.getContext());
1346 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001347
Evan Chengdcea1632010-02-04 02:40:39 +00001348 // Add the regs to the liveout set for the function.
1349 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1350 for (unsigned i = 0; i != RVLocs.size(); ++i)
1351 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1352 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001353
Dan Gohman475871a2008-07-27 21:46:04 +00001354 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001355
Dan Gohman475871a2008-07-27 21:46:04 +00001356 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001357 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1358 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001359 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1360 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001361
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001362 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001363 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1364 CCValAssign &VA = RVLocs[i];
1365 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001366 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001367 EVT ValVT = ValToCopy.getValueType();
1368
Dale Johannesenc4510512010-09-24 19:05:48 +00001369 // If this is x86-64, and we disabled SSE, we can't return FP values,
1370 // or SSE or MMX vectors.
1371 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1372 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001373 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001374 report_fatal_error("SSE register return with SSE disabled");
1375 }
1376 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1377 // llvm-gcc has never done it right and no one has noticed, so this
1378 // should be OK for now.
1379 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001380 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001381 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001382
Chris Lattner447ff682008-03-11 03:23:40 +00001383 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1384 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001385 if (VA.getLocReg() == X86::ST0 ||
1386 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001387 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1388 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001389 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001391 RetOps.push_back(ValToCopy);
1392 // Don't emit a copytoreg.
1393 continue;
1394 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001395
Evan Cheng242b38b2009-02-23 09:03:22 +00001396 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1397 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001398 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001399 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001400 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001401 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001402 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1403 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001404 // If we don't have SSE2 available, convert to v4f32 so the generated
1405 // register is legal.
1406 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001408 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001409 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001410 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001411
Dale Johannesendd64c412009-02-04 00:33:20 +00001412 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001413 Flag = Chain.getValue(1);
1414 }
Dan Gohman61a92132008-04-21 23:59:07 +00001415
1416 // The x86-64 ABI for returning structs by value requires that we copy
1417 // the sret argument into %rax for the return. We saved the argument into
1418 // a virtual register in the entry block, so now we copy the value out
1419 // and into %rax.
1420 if (Subtarget->is64Bit() &&
1421 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1422 MachineFunction &MF = DAG.getMachineFunction();
1423 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1424 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001425 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001426 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001427 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001428
Dale Johannesendd64c412009-02-04 00:33:20 +00001429 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001430 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001431
1432 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001433 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001434 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001435
Chris Lattner447ff682008-03-11 03:23:40 +00001436 RetOps[0] = Chain; // Update chain.
1437
1438 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001439 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001440 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001441
1442 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001443 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001444}
1445
Evan Cheng3d2125c2010-11-30 23:55:39 +00001446bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1447 if (N->getNumValues() != 1)
1448 return false;
1449 if (!N->hasNUsesOfValue(1, 0))
1450 return false;
1451
1452 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001453 if (Copy->getOpcode() != ISD::CopyToReg &&
1454 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001455 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001456
1457 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001458 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001459 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001460 if (UI->getOpcode() != X86ISD::RET_FLAG)
1461 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001462 HasRet = true;
1463 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001464
Evan Cheng1bf891a2010-12-01 22:59:46 +00001465 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001466}
1467
Dan Gohman98ca4f22009-08-05 01:29:28 +00001468/// LowerCallResult - Lower the result values of a call into the
1469/// appropriate copies out of appropriate physical registers.
1470///
1471SDValue
1472X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001473 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001474 const SmallVectorImpl<ISD::InputArg> &Ins,
1475 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001476 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001477
Chris Lattnere32bbf62007-02-28 07:09:55 +00001478 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001479 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001480 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001482 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Chris Lattner3085e152007-02-25 08:59:22 +00001485 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001486 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001487 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001488 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001489
Torok Edwin3f142c32009-02-01 18:15:56 +00001490 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001491 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001492 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001493 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001494 }
1495
Evan Cheng79fb3b42009-02-20 20:43:02 +00001496 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001497
1498 // If this is a call to a function that returns an fp value on the floating
1499 // point stack, we must guarantee the the value is popped from the stack, so
1500 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1501 // if the return value is not used. We use the FpGET_ST0 instructions
1502 // instead.
1503 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1504 // If we prefer to use the value in xmm registers, copy it out as f80 and
1505 // use a truncate to move it from fp stack reg to xmm reg.
1506 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1507 bool isST0 = VA.getLocReg() == X86::ST0;
1508 unsigned Opc = 0;
1509 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1510 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1511 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1512 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001513 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001514 Ops, 2), 1);
1515 Val = Chain.getValue(0);
1516
1517 // Round the f80 to the right size, which also moves it to the appropriate
1518 // xmm register.
1519 if (CopyVT != VA.getValVT())
1520 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1521 // This truncation won't change the value.
1522 DAG.getIntPtrConstant(1));
1523 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001524 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1525 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1526 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001527 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001528 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1530 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001531 } else {
1532 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001534 Val = Chain.getValue(0);
1535 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001536 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001537 } else {
1538 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1539 CopyVT, InFlag).getValue(1);
1540 Val = Chain.getValue(0);
1541 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001542 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001544 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001545
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001547}
1548
1549
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001550//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001551// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001552//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001553// StdCall calling convention seems to be standard for many Windows' API
1554// routines and around. It differs from C calling convention just a little:
1555// callee should clean up the stack, not caller. Symbols should be also
1556// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001557// For info on fast calling convention see Fast Calling Convention (tail call)
1558// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001559
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001561/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1563 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001564 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001565
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001567}
1568
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001569/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001570/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001571static bool
1572ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1573 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001574 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001575
Dan Gohman98ca4f22009-08-05 01:29:28 +00001576 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001577}
1578
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001579/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1580/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001581/// the specific parameter attribute. The copy will be passed as a byval
1582/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001583static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001584CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001585 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1586 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001587 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001588
Dale Johannesendd64c412009-02-04 00:33:20 +00001589 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001590 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001591 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001592}
1593
Chris Lattner29689432010-03-11 00:22:57 +00001594/// IsTailCallConvention - Return true if the calling convention is one that
1595/// supports tail call optimization.
1596static bool IsTailCallConvention(CallingConv::ID CC) {
1597 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1598}
1599
Evan Cheng0c439eb2010-01-27 00:07:07 +00001600/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1601/// a tailcall target by changing its ABI.
1602static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001603 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001604}
1605
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606SDValue
1607X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001608 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001609 const SmallVectorImpl<ISD::InputArg> &Ins,
1610 DebugLoc dl, SelectionDAG &DAG,
1611 const CCValAssign &VA,
1612 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001613 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001614 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001616 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001617 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001618 EVT ValVT;
1619
1620 // If value is passed by pointer we have address passed instead of the value
1621 // itself.
1622 if (VA.getLocInfo() == CCValAssign::Indirect)
1623 ValVT = VA.getLocVT();
1624 else
1625 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001626
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001627 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001628 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001629 // In case of tail call optimization mark all arguments mutable. Since they
1630 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001631 if (Flags.isByVal()) {
1632 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001633 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001634 return DAG.getFrameIndex(FI, getPointerTy());
1635 } else {
1636 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001637 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001638 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1639 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001640 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001641 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001642 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001643}
1644
Dan Gohman475871a2008-07-27 21:46:04 +00001645SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001647 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001648 bool isVarArg,
1649 const SmallVectorImpl<ISD::InputArg> &Ins,
1650 DebugLoc dl,
1651 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001652 SmallVectorImpl<SDValue> &InVals)
1653 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001654 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001655 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001656
Gordon Henriksen86737662008-01-05 16:56:59 +00001657 const Function* Fn = MF.getFunction();
1658 if (Fn->hasExternalLinkage() &&
1659 Subtarget->isTargetCygMing() &&
1660 Fn->getName() == "main")
1661 FuncInfo->setForceFramePointer(true);
1662
Evan Cheng1bc78042006-04-26 01:20:17 +00001663 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001665 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001666
Chris Lattner29689432010-03-11 00:22:57 +00001667 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1668 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001669
Chris Lattner638402b2007-02-28 07:00:42 +00001670 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001671 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001672 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1673 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001674
1675 // Allocate shadow area for Win64
1676 if (IsWin64) {
1677 CCInfo.AllocateStack(32, 8);
1678 }
1679
Duncan Sands45907662010-10-31 13:21:44 +00001680 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001681
Chris Lattnerf39f7712007-02-28 05:46:49 +00001682 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001683 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001684 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1685 CCValAssign &VA = ArgLocs[i];
1686 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1687 // places.
1688 assert(VA.getValNo() != LastVal &&
1689 "Don't support value assigned to multiple locs yet");
1690 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001691
Chris Lattnerf39f7712007-02-28 05:46:49 +00001692 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001693 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001694 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001696 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001700 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001703 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1704 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001705 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001706 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001707 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001708 RC = X86::VR64RegisterClass;
1709 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001710 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001711
Devang Patele9a7ea62011-01-31 21:38:14 +00001712 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001713 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001714
Chris Lattnerf39f7712007-02-28 05:46:49 +00001715 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1716 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1717 // right size.
1718 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001719 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001720 DAG.getValueType(VA.getValVT()));
1721 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001722 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001723 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001724 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001725 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001726
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001727 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001728 // Handle MMX values passed in XMM regs.
1729 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001730 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1731 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001732 } else
1733 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001734 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001735 } else {
1736 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001737 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001738 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001739
1740 // If value is passed via pointer - do a load.
1741 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001742 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1743 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001744
Dan Gohman98ca4f22009-08-05 01:29:28 +00001745 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001746 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001747
Dan Gohman61a92132008-04-21 23:59:07 +00001748 // The x86-64 ABI for returning structs by value requires that we copy
1749 // the sret argument into %rax for the return. Save the argument into
1750 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001751 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001752 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1753 unsigned Reg = FuncInfo->getSRetReturnReg();
1754 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001755 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001756 FuncInfo->setSRetReturnReg(Reg);
1757 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001758 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001760 }
1761
Chris Lattnerf39f7712007-02-28 05:46:49 +00001762 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001763 // Align stack specially for tail calls.
1764 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001765 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001766
Evan Cheng1bc78042006-04-26 01:20:17 +00001767 // If the function takes variable number of arguments, make a frame index for
1768 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001769 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001770 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1771 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001772 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001773 }
1774 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001775 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1776
1777 // FIXME: We should really autogenerate these arrays
1778 static const unsigned GPR64ArgRegsWin64[] = {
1779 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001780 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001781 static const unsigned GPR64ArgRegs64Bit[] = {
1782 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1783 };
1784 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001785 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1786 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1787 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001788 const unsigned *GPR64ArgRegs;
1789 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001790
1791 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001792 // The XMM registers which might contain var arg parameters are shadowed
1793 // in their paired GPR. So we only need to save the GPR to their home
1794 // slots.
1795 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001796 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001797 } else {
1798 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1799 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001800
1801 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001802 }
1803 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1804 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001805
Devang Patel578efa92009-06-05 21:57:13 +00001806 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001807 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001808 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001809 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001810 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001811 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001812 // Kernel mode asks for SSE to be disabled, so don't push them
1813 // on the stack.
1814 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001815
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001816 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001817 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001818 // Get to the caller-allocated home save location. Add 8 to account
1819 // for the return address.
1820 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001821 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001822 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001823 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1824 } else {
1825 // For X86-64, if there are vararg parameters that are passed via
1826 // registers, then we must store them to their spots on the stack so they
1827 // may be loaded by deferencing the result of va_next.
1828 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1829 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1830 FuncInfo->setRegSaveFrameIndex(
1831 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001832 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001833 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001834
Gordon Henriksen86737662008-01-05 16:56:59 +00001835 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001837 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1838 getPointerTy());
1839 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001840 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001841 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1842 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001843 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001844 X86::GR64RegisterClass, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001846 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001847 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001848 MachinePointerInfo::getFixedStack(
1849 FuncInfo->getRegSaveFrameIndex(), Offset),
1850 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001851 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001852 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001853 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001854
Dan Gohmanface41a2009-08-16 21:24:25 +00001855 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1856 // Now store the XMM (fp + vector) parameter registers.
1857 SmallVector<SDValue, 11> SaveXMMOps;
1858 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001859
Devang Patele9a7ea62011-01-31 21:38:14 +00001860 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001861 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1862 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001863
Dan Gohman1e93df62010-04-17 14:41:14 +00001864 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1865 FuncInfo->getRegSaveFrameIndex()));
1866 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1867 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001868
Dan Gohmanface41a2009-08-16 21:24:25 +00001869 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001870 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001871 X86::VR128RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001872 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1873 SaveXMMOps.push_back(Val);
1874 }
1875 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1876 MVT::Other,
1877 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001878 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001879
1880 if (!MemOps.empty())
1881 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1882 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001883 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001884 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001885
Gordon Henriksen86737662008-01-05 16:56:59 +00001886 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001887 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001888 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001889 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001890 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001891 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001892 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001893 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001894 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001895
Gordon Henriksen86737662008-01-05 16:56:59 +00001896 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001897 // RegSaveFrameIndex is X86-64 only.
1898 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001899 if (CallConv == CallingConv::X86_FastCall ||
1900 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001901 // fastcc functions can't have varargs.
1902 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001903 }
Evan Cheng25caf632006-05-23 21:06:34 +00001904
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001906}
1907
Dan Gohman475871a2008-07-27 21:46:04 +00001908SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1910 SDValue StackPtr, SDValue Arg,
1911 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001912 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001913 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001914 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001915 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001916 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001917 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001918 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001919
1920 return DAG.getStore(Chain, dl, Arg, PtrOff,
1921 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001922 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001923}
1924
Bill Wendling64e87322009-01-16 19:25:27 +00001925/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001926/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001927SDValue
1928X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001929 SDValue &OutRetAddr, SDValue Chain,
1930 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001931 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001932 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001933 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001934 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001935
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001936 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001937 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1938 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001939 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001940}
1941
1942/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1943/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001944static SDValue
1945EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001946 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001947 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001948 // Store the return address to the appropriate stack slot.
1949 if (!FPDiff) return Chain;
1950 // Calculate the new stack slot for the return address.
1951 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001952 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001953 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001956 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001957 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001958 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001959 return Chain;
1960}
1961
Dan Gohman98ca4f22009-08-05 01:29:28 +00001962SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001963X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001964 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001965 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001967 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968 const SmallVectorImpl<ISD::InputArg> &Ins,
1969 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001970 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001971 MachineFunction &MF = DAG.getMachineFunction();
1972 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001973 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001974 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001975 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976
Evan Cheng5f941932010-02-05 02:21:12 +00001977 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001978 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001979 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1980 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001981 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001982
1983 // Sibcalls are automatically detected tailcalls which do not require
1984 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001985 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001986 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001987
1988 if (isTailCall)
1989 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001990 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001991
Chris Lattner29689432010-03-11 00:22:57 +00001992 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1993 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001994
Chris Lattner638402b2007-02-28 07:00:42 +00001995 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001996 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001997 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1998 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001999
2000 // Allocate shadow area for Win64
2001 if (IsWin64) {
2002 CCInfo.AllocateStack(32, 8);
2003 }
2004
Duncan Sands45907662010-10-31 13:21:44 +00002005 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002006
Chris Lattner423c5f42007-02-28 05:31:48 +00002007 // Get a count of how many bytes are to be pushed on the stack.
2008 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002009 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002010 // This is a sibcall. The memory operands are available in caller's
2011 // own caller's stack.
2012 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002013 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002014 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002015
Gordon Henriksen86737662008-01-05 16:56:59 +00002016 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002017 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002018 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002019 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002020 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2021 FPDiff = NumBytesCallerPushed - NumBytes;
2022
2023 // Set the delta of movement of the returnaddr stackslot.
2024 // But only set if delta is greater than previous delta.
2025 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2026 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2027 }
2028
Evan Chengf22f9b32010-02-06 03:28:46 +00002029 if (!IsSibcall)
2030 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002031
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002033 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002034 if (isTailCall && FPDiff)
2035 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2036 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002037
Dan Gohman475871a2008-07-27 21:46:04 +00002038 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2039 SmallVector<SDValue, 8> MemOpChains;
2040 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002041
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002042 // Walk the register/memloc assignments, inserting copies/loads. In the case
2043 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002044 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2045 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002046 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002047 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002048 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002049 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002050
Chris Lattner423c5f42007-02-28 05:31:48 +00002051 // Promote the value if needed.
2052 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002053 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002054 case CCValAssign::Full: break;
2055 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002056 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002057 break;
2058 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002059 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002060 break;
2061 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002062 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2063 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002064 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2066 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002067 } else
2068 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2069 break;
2070 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002071 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002072 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002073 case CCValAssign::Indirect: {
2074 // Store the argument.
2075 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002076 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002077 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002078 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002079 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002080 Arg = SpillSlot;
2081 break;
2082 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002083 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002084
Chris Lattner423c5f42007-02-28 05:31:48 +00002085 if (VA.isRegLoc()) {
2086 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002087 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002088 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2089 // shadow reg if callee is a varargs function.
2090 unsigned ShadowReg = 0;
2091 switch (VA.getLocReg()) {
2092 case X86::XMM0: ShadowReg = X86::RCX; break;
2093 case X86::XMM1: ShadowReg = X86::RDX; break;
2094 case X86::XMM2: ShadowReg = X86::R8; break;
2095 case X86::XMM3: ShadowReg = X86::R9; break;
2096 }
2097 if (ShadowReg)
2098 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2099 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002100 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002101 assert(VA.isMemLoc());
2102 if (StackPtr.getNode() == 0)
2103 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2104 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2105 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002106 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002107 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002108
Evan Cheng32fe1032006-05-25 00:59:30 +00002109 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002110 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002111 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002112
Evan Cheng347d5f72006-04-28 21:29:37 +00002113 // Build a sequence of copy-to-reg nodes chained together with token chain
2114 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002115 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002116 // Tail call byval lowering might overwrite argument registers so in case of
2117 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002118 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002119 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002120 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002121 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002122 InFlag = Chain.getValue(1);
2123 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002124
Chris Lattner88e1fd52009-07-09 04:24:46 +00002125 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002126 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2127 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002128 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002129 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2130 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002131 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002132 InFlag);
2133 InFlag = Chain.getValue(1);
2134 } else {
2135 // If we are tail calling and generating PIC/GOT style code load the
2136 // address of the callee into ECX. The value in ecx is used as target of
2137 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2138 // for tail calls on PIC/GOT architectures. Normally we would just put the
2139 // address of GOT into ebx and then call target@PLT. But for tail calls
2140 // ebx would be restored (since ebx is callee saved) before jumping to the
2141 // target@PLT.
2142
2143 // Note: The actual moving to ECX is done further down.
2144 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2145 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2146 !G->getGlobal()->hasProtectedVisibility())
2147 Callee = LowerGlobalAddress(Callee, DAG);
2148 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002149 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002150 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002151 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002152
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002153 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002154 // From AMD64 ABI document:
2155 // For calls that may call functions that use varargs or stdargs
2156 // (prototype-less calls or calls to functions containing ellipsis (...) in
2157 // the declaration) %al is used as hidden argument to specify the number
2158 // of SSE registers used. The contents of %al do not need to match exactly
2159 // the number of registers, but must be an ubound on the number of SSE
2160 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002161
Gordon Henriksen86737662008-01-05 16:56:59 +00002162 // Count the number of XMM registers allocated.
2163 static const unsigned XMMArgRegs[] = {
2164 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2165 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2166 };
2167 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002168 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002169 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002170
Dale Johannesendd64c412009-02-04 00:33:20 +00002171 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002172 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002173 InFlag = Chain.getValue(1);
2174 }
2175
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002176
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002177 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178 if (isTailCall) {
2179 // Force all the incoming stack arguments to be loaded from the stack
2180 // before any new outgoing arguments are stored to the stack, because the
2181 // outgoing stack slots may alias the incoming argument stack slots, and
2182 // the alias isn't otherwise explicit. This is slightly more conservative
2183 // than necessary, because it means that each store effectively depends
2184 // on every argument instead of just those arguments it would clobber.
2185 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2186
Dan Gohman475871a2008-07-27 21:46:04 +00002187 SmallVector<SDValue, 8> MemOpChains2;
2188 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002189 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002190 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002191 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002192 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002193 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2194 CCValAssign &VA = ArgLocs[i];
2195 if (VA.isRegLoc())
2196 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002197 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002198 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002199 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002200 // Create frame index.
2201 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002202 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002203 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002204 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002205
Duncan Sands276dcbd2008-03-21 09:14:45 +00002206 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002207 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002208 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002209 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002210 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002211 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002212 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002213
Dan Gohman98ca4f22009-08-05 01:29:28 +00002214 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2215 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002216 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002217 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002218 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002219 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002220 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002221 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002222 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002223 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002224 }
2225 }
2226
2227 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002228 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002229 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002230
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002231 // Copy arguments to their registers.
2232 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002233 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002234 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002235 InFlag = Chain.getValue(1);
2236 }
Dan Gohman475871a2008-07-27 21:46:04 +00002237 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238
Gordon Henriksen86737662008-01-05 16:56:59 +00002239 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002240 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002241 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002242 }
2243
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002244 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2245 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2246 // In the 64-bit large code model, we have to make all calls
2247 // through a register, since the call instruction's 32-bit
2248 // pc-relative offset may not be large enough to hold the whole
2249 // address.
2250 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002251 // If the callee is a GlobalAddress node (quite common, every direct call
2252 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2253 // it.
2254
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002255 // We should use extra load for direct calls to dllimported functions in
2256 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002257 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002258 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002259 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002260
Chris Lattner48a7d022009-07-09 05:02:21 +00002261 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2262 // external symbols most go through the PLT in PIC mode. If the symbol
2263 // has hidden or protected visibility, or if it is static or local, then
2264 // we don't need to use the PLT - we can directly call it.
2265 if (Subtarget->isTargetELF() &&
2266 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002267 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002268 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002269 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002270 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2271 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002272 // PC-relative references to external symbols should go through $stub,
2273 // unless we're building with the leopard linker or later, which
2274 // automatically synthesizes these stubs.
2275 OpFlags = X86II::MO_DARWIN_STUB;
2276 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002277
Devang Patel0d881da2010-07-06 22:08:15 +00002278 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002279 G->getOffset(), OpFlags);
2280 }
Bill Wendling056292f2008-09-16 21:48:12 +00002281 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002282 unsigned char OpFlags = 0;
2283
Evan Cheng1bf891a2010-12-01 22:59:46 +00002284 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2285 // external symbols should go through the PLT.
2286 if (Subtarget->isTargetELF() &&
2287 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2288 OpFlags = X86II::MO_PLT;
2289 } else if (Subtarget->isPICStyleStubAny() &&
2290 Subtarget->getDarwinVers() < 9) {
2291 // PC-relative references to external symbols should go through $stub,
2292 // unless we're building with the leopard linker or later, which
2293 // automatically synthesizes these stubs.
2294 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002295 }
Eric Christopherfd179292009-08-27 18:07:15 +00002296
Chris Lattner48a7d022009-07-09 05:02:21 +00002297 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2298 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002299 }
2300
Chris Lattnerd96d0722007-02-25 06:40:16 +00002301 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002302 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002303 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002304
Evan Chengf22f9b32010-02-06 03:28:46 +00002305 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002306 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2307 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002308 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002309 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002310
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002311 Ops.push_back(Chain);
2312 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002313
Dan Gohman98ca4f22009-08-05 01:29:28 +00002314 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002315 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002316
Gordon Henriksen86737662008-01-05 16:56:59 +00002317 // Add argument registers to the end of the list so that they are known live
2318 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002319 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2320 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2321 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002322
Evan Cheng586ccac2008-03-18 23:36:35 +00002323 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002324 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002325 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2326
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002327 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002328 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002329 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002330
Gabor Greifba36cb52008-08-28 21:40:38 +00002331 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002332 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002333
Dan Gohman98ca4f22009-08-05 01:29:28 +00002334 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002335 // We used to do:
2336 //// If this is the first return lowered for this function, add the regs
2337 //// to the liveout set for the function.
2338 // This isn't right, although it's probably harmless on x86; liveouts
2339 // should be computed from returns not tail calls. Consider a void
2340 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002341 return DAG.getNode(X86ISD::TC_RETURN, dl,
2342 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002343 }
2344
Dale Johannesenace16102009-02-03 19:33:06 +00002345 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002346 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002347
Chris Lattner2d297092006-05-23 18:50:38 +00002348 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002349 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002350 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002351 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002352 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002353 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002354 // pops the hidden struct pointer, so we have to push it back.
2355 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002356 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002357 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002358 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002359
Gordon Henriksenae636f82008-01-03 16:47:34 +00002360 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002361 if (!IsSibcall) {
2362 Chain = DAG.getCALLSEQ_END(Chain,
2363 DAG.getIntPtrConstant(NumBytes, true),
2364 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2365 true),
2366 InFlag);
2367 InFlag = Chain.getValue(1);
2368 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002369
Chris Lattner3085e152007-02-25 08:59:22 +00002370 // Handle result values, copying them out of physregs into vregs that we
2371 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002372 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2373 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002374}
2375
Evan Cheng25ab6902006-09-08 06:48:29 +00002376
2377//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002378// Fast Calling Convention (tail call) implementation
2379//===----------------------------------------------------------------------===//
2380
2381// Like std call, callee cleans arguments, convention except that ECX is
2382// reserved for storing the tail called function address. Only 2 registers are
2383// free for argument passing (inreg). Tail call optimization is performed
2384// provided:
2385// * tailcallopt is enabled
2386// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002387// On X86_64 architecture with GOT-style position independent code only local
2388// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002389// To keep the stack aligned according to platform abi the function
2390// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2391// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002392// If a tail called function callee has more arguments than the caller the
2393// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002394// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002395// original REtADDR, but before the saved framepointer or the spilled registers
2396// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2397// stack layout:
2398// arg1
2399// arg2
2400// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002401// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002402// move area ]
2403// (possible EBP)
2404// ESI
2405// EDI
2406// local1 ..
2407
2408/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2409/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002410unsigned
2411X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2412 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002413 MachineFunction &MF = DAG.getMachineFunction();
2414 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002415 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002416 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002417 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002418 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002419 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002420 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2421 // Number smaller than 12 so just add the difference.
2422 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2423 } else {
2424 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002425 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002426 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002427 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002428 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002429}
2430
Evan Cheng5f941932010-02-05 02:21:12 +00002431/// MatchingStackOffset - Return true if the given stack call argument is
2432/// already available in the same position (relatively) of the caller's
2433/// incoming argument stack.
2434static
2435bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2436 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2437 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002438 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2439 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002440 if (Arg.getOpcode() == ISD::CopyFromReg) {
2441 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002442 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002443 return false;
2444 MachineInstr *Def = MRI->getVRegDef(VR);
2445 if (!Def)
2446 return false;
2447 if (!Flags.isByVal()) {
2448 if (!TII->isLoadFromStackSlot(Def, FI))
2449 return false;
2450 } else {
2451 unsigned Opcode = Def->getOpcode();
2452 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2453 Def->getOperand(1).isFI()) {
2454 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002455 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002456 } else
2457 return false;
2458 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002459 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2460 if (Flags.isByVal())
2461 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002462 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002463 // define @foo(%struct.X* %A) {
2464 // tail call @bar(%struct.X* byval %A)
2465 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002466 return false;
2467 SDValue Ptr = Ld->getBasePtr();
2468 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2469 if (!FINode)
2470 return false;
2471 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002472 } else
2473 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002474
Evan Cheng4cae1332010-03-05 08:38:04 +00002475 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002476 if (!MFI->isFixedObjectIndex(FI))
2477 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002478 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002479}
2480
Dan Gohman98ca4f22009-08-05 01:29:28 +00002481/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2482/// for tail call optimization. Targets which want to do tail call
2483/// optimization should implement this function.
2484bool
2485X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002486 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002487 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002488 bool isCalleeStructRet,
2489 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002490 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002491 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002492 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002493 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002494 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002495 CalleeCC != CallingConv::C)
2496 return false;
2497
Evan Cheng7096ae42010-01-29 06:45:59 +00002498 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002499 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002500 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002501 CallingConv::ID CallerCC = CallerF->getCallingConv();
2502 bool CCMatch = CallerCC == CalleeCC;
2503
Dan Gohman1797ed52010-02-08 20:27:50 +00002504 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002505 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002506 return true;
2507 return false;
2508 }
2509
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002510 // Look for obvious safe cases to perform tail call optimization that do not
2511 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002512
Evan Cheng2c12cb42010-03-26 16:26:03 +00002513 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2514 // emit a special epilogue.
2515 if (RegInfo->needsStackRealignment(MF))
2516 return false;
2517
Eric Christopher90eb4022010-07-22 00:26:08 +00002518 // Do not sibcall optimize vararg calls unless the call site is not passing
2519 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002520 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002521 return false;
2522
Evan Chenga375d472010-03-15 18:54:48 +00002523 // Also avoid sibcall optimization if either caller or callee uses struct
2524 // return semantics.
2525 if (isCalleeStructRet || isCallerStructRet)
2526 return false;
2527
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002528 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2529 // Therefore if it's not used by the call it is not safe to optimize this into
2530 // a sibcall.
2531 bool Unused = false;
2532 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2533 if (!Ins[i].Used) {
2534 Unused = true;
2535 break;
2536 }
2537 }
2538 if (Unused) {
2539 SmallVector<CCValAssign, 16> RVLocs;
2540 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2541 RVLocs, *DAG.getContext());
2542 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002543 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002544 CCValAssign &VA = RVLocs[i];
2545 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2546 return false;
2547 }
2548 }
2549
Evan Cheng13617962010-04-30 01:12:32 +00002550 // If the calling conventions do not match, then we'd better make sure the
2551 // results are returned in the same way as what the caller expects.
2552 if (!CCMatch) {
2553 SmallVector<CCValAssign, 16> RVLocs1;
2554 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2555 RVLocs1, *DAG.getContext());
2556 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2557
2558 SmallVector<CCValAssign, 16> RVLocs2;
2559 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2560 RVLocs2, *DAG.getContext());
2561 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2562
2563 if (RVLocs1.size() != RVLocs2.size())
2564 return false;
2565 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2566 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2567 return false;
2568 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2569 return false;
2570 if (RVLocs1[i].isRegLoc()) {
2571 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2572 return false;
2573 } else {
2574 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2575 return false;
2576 }
2577 }
2578 }
2579
Evan Chenga6bff982010-01-30 01:22:00 +00002580 // If the callee takes no arguments then go on to check the results of the
2581 // call.
2582 if (!Outs.empty()) {
2583 // Check if stack adjustment is needed. For now, do not do this if any
2584 // argument is passed on the stack.
2585 SmallVector<CCValAssign, 16> ArgLocs;
2586 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2587 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002588
2589 // Allocate shadow area for Win64
2590 if (Subtarget->isTargetWin64()) {
2591 CCInfo.AllocateStack(32, 8);
2592 }
2593
Duncan Sands45907662010-10-31 13:21:44 +00002594 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002595 if (CCInfo.getNextStackOffset()) {
2596 MachineFunction &MF = DAG.getMachineFunction();
2597 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2598 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002599
2600 // Check if the arguments are already laid out in the right way as
2601 // the caller's fixed stack objects.
2602 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002603 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2604 const X86InstrInfo *TII =
2605 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002606 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2607 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002608 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002609 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002610 if (VA.getLocInfo() == CCValAssign::Indirect)
2611 return false;
2612 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002613 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2614 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002615 return false;
2616 }
2617 }
2618 }
Evan Cheng9c044672010-05-29 01:35:22 +00002619
2620 // If the tailcall address may be in a register, then make sure it's
2621 // possible to register allocate for it. In 32-bit, the call address can
2622 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002623 // callee-saved registers are restored. These happen to be the same
2624 // registers used to pass 'inreg' arguments so watch out for those.
2625 if (!Subtarget->is64Bit() &&
2626 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002627 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002628 unsigned NumInRegs = 0;
2629 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2630 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002631 if (!VA.isRegLoc())
2632 continue;
2633 unsigned Reg = VA.getLocReg();
2634 switch (Reg) {
2635 default: break;
2636 case X86::EAX: case X86::EDX: case X86::ECX:
2637 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002638 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002639 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002640 }
2641 }
2642 }
Evan Chenga6bff982010-01-30 01:22:00 +00002643 }
Evan Chengb1712452010-01-27 06:25:16 +00002644
Dale Johannesend155d7e2010-10-25 22:17:05 +00002645 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002646 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002647 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2648 return false;
2649
Evan Cheng86809cc2010-02-03 03:28:02 +00002650 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002651}
2652
Dan Gohman3df24e62008-09-03 23:12:08 +00002653FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002654X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2655 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002656}
2657
2658
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002659//===----------------------------------------------------------------------===//
2660// Other Lowering Hooks
2661//===----------------------------------------------------------------------===//
2662
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002663static bool MayFoldLoad(SDValue Op) {
2664 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2665}
2666
2667static bool MayFoldIntoStore(SDValue Op) {
2668 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2669}
2670
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002671static bool isTargetShuffle(unsigned Opcode) {
2672 switch(Opcode) {
2673 default: return false;
2674 case X86ISD::PSHUFD:
2675 case X86ISD::PSHUFHW:
2676 case X86ISD::PSHUFLW:
2677 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002678 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002679 case X86ISD::SHUFPS:
2680 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002681 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002682 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002683 case X86ISD::MOVLPS:
2684 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002685 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002686 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002687 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002688 case X86ISD::MOVSS:
2689 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002690 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002691 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002692 case X86ISD::PUNPCKLWD:
2693 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002694 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002695 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002696 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002697 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002698 case X86ISD::PUNPCKHWD:
2699 case X86ISD::PUNPCKHBW:
2700 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002701 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002702 return true;
2703 }
2704 return false;
2705}
2706
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002707static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002708 SDValue V1, SelectionDAG &DAG) {
2709 switch(Opc) {
2710 default: llvm_unreachable("Unknown x86 shuffle node");
2711 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002712 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002713 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002714 return DAG.getNode(Opc, dl, VT, V1);
2715 }
2716
2717 return SDValue();
2718}
2719
2720static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002721 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002722 switch(Opc) {
2723 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002724 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002725 case X86ISD::PSHUFHW:
2726 case X86ISD::PSHUFLW:
2727 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2728 }
2729
2730 return SDValue();
2731}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002732
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002733static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2734 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2735 switch(Opc) {
2736 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002737 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002738 case X86ISD::SHUFPD:
2739 case X86ISD::SHUFPS:
2740 return DAG.getNode(Opc, dl, VT, V1, V2,
2741 DAG.getConstant(TargetMask, MVT::i8));
2742 }
2743 return SDValue();
2744}
2745
2746static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2747 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2748 switch(Opc) {
2749 default: llvm_unreachable("Unknown x86 shuffle node");
2750 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002751 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002752 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002753 case X86ISD::MOVLPS:
2754 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002755 case X86ISD::MOVSS:
2756 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002757 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002758 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002759 case X86ISD::PUNPCKLWD:
2760 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002761 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002762 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002763 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002764 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002765 case X86ISD::PUNPCKHWD:
2766 case X86ISD::PUNPCKHBW:
2767 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002768 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002769 return DAG.getNode(Opc, dl, VT, V1, V2);
2770 }
2771 return SDValue();
2772}
2773
Dan Gohmand858e902010-04-17 15:26:15 +00002774SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002775 MachineFunction &MF = DAG.getMachineFunction();
2776 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2777 int ReturnAddrIndex = FuncInfo->getRAIndex();
2778
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002779 if (ReturnAddrIndex == 0) {
2780 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002781 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002782 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002783 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002784 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002785 }
2786
Evan Cheng25ab6902006-09-08 06:48:29 +00002787 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002788}
2789
2790
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002791bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2792 bool hasSymbolicDisplacement) {
2793 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002794 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002795 return false;
2796
2797 // If we don't have a symbolic displacement - we don't have any extra
2798 // restrictions.
2799 if (!hasSymbolicDisplacement)
2800 return true;
2801
2802 // FIXME: Some tweaks might be needed for medium code model.
2803 if (M != CodeModel::Small && M != CodeModel::Kernel)
2804 return false;
2805
2806 // For small code model we assume that latest object is 16MB before end of 31
2807 // bits boundary. We may also accept pretty large negative constants knowing
2808 // that all objects are in the positive half of address space.
2809 if (M == CodeModel::Small && Offset < 16*1024*1024)
2810 return true;
2811
2812 // For kernel code model we know that all object resist in the negative half
2813 // of 32bits address space. We may not accept negative offsets, since they may
2814 // be just off and we may accept pretty large positive ones.
2815 if (M == CodeModel::Kernel && Offset > 0)
2816 return true;
2817
2818 return false;
2819}
2820
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002821/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2822/// specific condition code, returning the condition code and the LHS/RHS of the
2823/// comparison to make.
2824static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2825 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002826 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002827 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2828 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2829 // X > -1 -> X == 0, jump !sign.
2830 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002831 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002832 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2833 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002834 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002835 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002836 // X < 1 -> X <= 0
2837 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002838 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002839 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002840 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002841
Evan Chengd9558e02006-01-06 00:43:03 +00002842 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002843 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002844 case ISD::SETEQ: return X86::COND_E;
2845 case ISD::SETGT: return X86::COND_G;
2846 case ISD::SETGE: return X86::COND_GE;
2847 case ISD::SETLT: return X86::COND_L;
2848 case ISD::SETLE: return X86::COND_LE;
2849 case ISD::SETNE: return X86::COND_NE;
2850 case ISD::SETULT: return X86::COND_B;
2851 case ISD::SETUGT: return X86::COND_A;
2852 case ISD::SETULE: return X86::COND_BE;
2853 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002854 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002855 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002856
Chris Lattner4c78e022008-12-23 23:42:27 +00002857 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002858
Chris Lattner4c78e022008-12-23 23:42:27 +00002859 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002860 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2861 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002862 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2863 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002864 }
2865
Chris Lattner4c78e022008-12-23 23:42:27 +00002866 switch (SetCCOpcode) {
2867 default: break;
2868 case ISD::SETOLT:
2869 case ISD::SETOLE:
2870 case ISD::SETUGT:
2871 case ISD::SETUGE:
2872 std::swap(LHS, RHS);
2873 break;
2874 }
2875
2876 // On a floating point condition, the flags are set as follows:
2877 // ZF PF CF op
2878 // 0 | 0 | 0 | X > Y
2879 // 0 | 0 | 1 | X < Y
2880 // 1 | 0 | 0 | X == Y
2881 // 1 | 1 | 1 | unordered
2882 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002883 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002884 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002885 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002886 case ISD::SETOLT: // flipped
2887 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002888 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002889 case ISD::SETOLE: // flipped
2890 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002891 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002892 case ISD::SETUGT: // flipped
2893 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002894 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002895 case ISD::SETUGE: // flipped
2896 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002897 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002898 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002899 case ISD::SETNE: return X86::COND_NE;
2900 case ISD::SETUO: return X86::COND_P;
2901 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002902 case ISD::SETOEQ:
2903 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002904 }
Evan Chengd9558e02006-01-06 00:43:03 +00002905}
2906
Evan Cheng4a460802006-01-11 00:33:36 +00002907/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2908/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002909/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002910static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002911 switch (X86CC) {
2912 default:
2913 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002914 case X86::COND_B:
2915 case X86::COND_BE:
2916 case X86::COND_E:
2917 case X86::COND_P:
2918 case X86::COND_A:
2919 case X86::COND_AE:
2920 case X86::COND_NE:
2921 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002922 return true;
2923 }
2924}
2925
Evan Chengeb2f9692009-10-27 19:56:55 +00002926/// isFPImmLegal - Returns true if the target can instruction select the
2927/// specified FP immediate natively. If false, the legalizer will
2928/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002929bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002930 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2931 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2932 return true;
2933 }
2934 return false;
2935}
2936
Nate Begeman9008ca62009-04-27 18:41:29 +00002937/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2938/// the specified range (L, H].
2939static bool isUndefOrInRange(int Val, int Low, int Hi) {
2940 return (Val < 0) || (Val >= Low && Val < Hi);
2941}
2942
2943/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2944/// specified value.
2945static bool isUndefOrEqual(int Val, int CmpVal) {
2946 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002947 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002949}
2950
Nate Begeman9008ca62009-04-27 18:41:29 +00002951/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2952/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2953/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002954static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002955 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002956 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002958 return (Mask[0] < 2 && Mask[1] < 2);
2959 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002960}
2961
Nate Begeman9008ca62009-04-27 18:41:29 +00002962bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002963 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 N->getMask(M);
2965 return ::isPSHUFDMask(M, N->getValueType(0));
2966}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002967
Nate Begeman9008ca62009-04-27 18:41:29 +00002968/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2969/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002970static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002971 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002972 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002973
Nate Begeman9008ca62009-04-27 18:41:29 +00002974 // Lower quadword copied in order or undef.
2975 for (int i = 0; i != 4; ++i)
2976 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002977 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002978
Evan Cheng506d3df2006-03-29 23:07:14 +00002979 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002980 for (int i = 4; i != 8; ++i)
2981 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002982 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002983
Evan Cheng506d3df2006-03-29 23:07:14 +00002984 return true;
2985}
2986
Nate Begeman9008ca62009-04-27 18:41:29 +00002987bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002988 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002989 N->getMask(M);
2990 return ::isPSHUFHWMask(M, N->getValueType(0));
2991}
Evan Cheng506d3df2006-03-29 23:07:14 +00002992
Nate Begeman9008ca62009-04-27 18:41:29 +00002993/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2994/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002995static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002996 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002997 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002998
Rafael Espindola15684b22009-04-24 12:40:33 +00002999 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003000 for (int i = 4; i != 8; ++i)
3001 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003002 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003003
Rafael Espindola15684b22009-04-24 12:40:33 +00003004 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003005 for (int i = 0; i != 4; ++i)
3006 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003007 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003008
Rafael Espindola15684b22009-04-24 12:40:33 +00003009 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003010}
3011
Nate Begeman9008ca62009-04-27 18:41:29 +00003012bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003013 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 N->getMask(M);
3015 return ::isPSHUFLWMask(M, N->getValueType(0));
3016}
3017
Nate Begemana09008b2009-10-19 02:17:23 +00003018/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3019/// is suitable for input to PALIGNR.
3020static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3021 bool hasSSSE3) {
3022 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003023
Nate Begemana09008b2009-10-19 02:17:23 +00003024 // Do not handle v2i64 / v2f64 shuffles with palignr.
3025 if (e < 4 || !hasSSSE3)
3026 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003027
Nate Begemana09008b2009-10-19 02:17:23 +00003028 for (i = 0; i != e; ++i)
3029 if (Mask[i] >= 0)
3030 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003031
Nate Begemana09008b2009-10-19 02:17:23 +00003032 // All undef, not a palignr.
3033 if (i == e)
3034 return false;
3035
3036 // Determine if it's ok to perform a palignr with only the LHS, since we
3037 // don't have access to the actual shuffle elements to see if RHS is undef.
3038 bool Unary = Mask[i] < (int)e;
3039 bool NeedsUnary = false;
3040
3041 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003042
Nate Begemana09008b2009-10-19 02:17:23 +00003043 // Check the rest of the elements to see if they are consecutive.
3044 for (++i; i != e; ++i) {
3045 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003046 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003047 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003048
Nate Begemana09008b2009-10-19 02:17:23 +00003049 Unary = Unary && (m < (int)e);
3050 NeedsUnary = NeedsUnary || (m < s);
3051
3052 if (NeedsUnary && !Unary)
3053 return false;
3054 if (Unary && m != ((s+i) & (e-1)))
3055 return false;
3056 if (!Unary && m != (s+i))
3057 return false;
3058 }
3059 return true;
3060}
3061
3062bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3063 SmallVector<int, 8> M;
3064 N->getMask(M);
3065 return ::isPALIGNRMask(M, N->getValueType(0), true);
3066}
3067
Evan Cheng14aed5e2006-03-24 01:18:28 +00003068/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3069/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003070static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003071 int NumElems = VT.getVectorNumElements();
3072 if (NumElems != 2 && NumElems != 4)
3073 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003074
Nate Begeman9008ca62009-04-27 18:41:29 +00003075 int Half = NumElems / 2;
3076 for (int i = 0; i < Half; ++i)
3077 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003078 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003079 for (int i = Half; i < NumElems; ++i)
3080 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003081 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003082
Evan Cheng14aed5e2006-03-24 01:18:28 +00003083 return true;
3084}
3085
Nate Begeman9008ca62009-04-27 18:41:29 +00003086bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3087 SmallVector<int, 8> M;
3088 N->getMask(M);
3089 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003090}
3091
Evan Cheng213d2cf2007-05-17 18:45:50 +00003092/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003093/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3094/// half elements to come from vector 1 (which would equal the dest.) and
3095/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003096static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003098
3099 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003101
Nate Begeman9008ca62009-04-27 18:41:29 +00003102 int Half = NumElems / 2;
3103 for (int i = 0; i < Half; ++i)
3104 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003105 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003106 for (int i = Half; i < NumElems; ++i)
3107 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003108 return false;
3109 return true;
3110}
3111
Nate Begeman9008ca62009-04-27 18:41:29 +00003112static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3113 SmallVector<int, 8> M;
3114 N->getMask(M);
3115 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003116}
3117
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003118/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3119/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003120bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3121 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003122 return false;
3123
Evan Cheng2064a2b2006-03-28 06:50:32 +00003124 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003125 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3126 isUndefOrEqual(N->getMaskElt(1), 7) &&
3127 isUndefOrEqual(N->getMaskElt(2), 2) &&
3128 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003129}
3130
Nate Begeman0b10b912009-11-07 23:17:15 +00003131/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3132/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3133/// <2, 3, 2, 3>
3134bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3135 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003136
Nate Begeman0b10b912009-11-07 23:17:15 +00003137 if (NumElems != 4)
3138 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003139
Nate Begeman0b10b912009-11-07 23:17:15 +00003140 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3141 isUndefOrEqual(N->getMaskElt(1), 3) &&
3142 isUndefOrEqual(N->getMaskElt(2), 2) &&
3143 isUndefOrEqual(N->getMaskElt(3), 3);
3144}
3145
Evan Cheng5ced1d82006-04-06 23:23:56 +00003146/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3147/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003148bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3149 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003150
Evan Cheng5ced1d82006-04-06 23:23:56 +00003151 if (NumElems != 2 && NumElems != 4)
3152 return false;
3153
Evan Chengc5cdff22006-04-07 21:53:05 +00003154 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003156 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003157
Evan Chengc5cdff22006-04-07 21:53:05 +00003158 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003159 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003160 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003161
3162 return true;
3163}
3164
Nate Begeman0b10b912009-11-07 23:17:15 +00003165/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3166/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3167bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003168 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003169
Evan Cheng5ced1d82006-04-06 23:23:56 +00003170 if (NumElems != 2 && NumElems != 4)
3171 return false;
3172
Evan Chengc5cdff22006-04-07 21:53:05 +00003173 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003175 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003176
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 for (unsigned i = 0; i < NumElems/2; ++i)
3178 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003179 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003180
3181 return true;
3182}
3183
Evan Cheng0038e592006-03-28 00:39:58 +00003184/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3185/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003186static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003187 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003188 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003189 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3193 int BitI = Mask[i];
3194 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003195 if (!isUndefOrEqual(BitI, j))
3196 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003197 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003198 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003199 return false;
3200 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003201 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003202 return false;
3203 }
Evan Cheng0038e592006-03-28 00:39:58 +00003204 }
Evan Cheng0038e592006-03-28 00:39:58 +00003205 return true;
3206}
3207
Nate Begeman9008ca62009-04-27 18:41:29 +00003208bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3209 SmallVector<int, 8> M;
3210 N->getMask(M);
3211 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003212}
3213
Evan Cheng4fcb9222006-03-28 02:43:26 +00003214/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3215/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003216static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003217 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003219 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003220 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003221
Nate Begeman9008ca62009-04-27 18:41:29 +00003222 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3223 int BitI = Mask[i];
3224 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003225 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003226 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003227 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003228 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003229 return false;
3230 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003231 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003232 return false;
3233 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003234 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003235 return true;
3236}
3237
Nate Begeman9008ca62009-04-27 18:41:29 +00003238bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3239 SmallVector<int, 8> M;
3240 N->getMask(M);
3241 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003242}
3243
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003244/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3245/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3246/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003247static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003248 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003249 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003250 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003251
Nate Begeman9008ca62009-04-27 18:41:29 +00003252 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3253 int BitI = Mask[i];
3254 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003255 if (!isUndefOrEqual(BitI, j))
3256 return false;
3257 if (!isUndefOrEqual(BitI1, j))
3258 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003259 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003260 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003261}
3262
Nate Begeman9008ca62009-04-27 18:41:29 +00003263bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3264 SmallVector<int, 8> M;
3265 N->getMask(M);
3266 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3267}
3268
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003269/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3270/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3271/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003272static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003274 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3275 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003276
Nate Begeman9008ca62009-04-27 18:41:29 +00003277 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3278 int BitI = Mask[i];
3279 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003280 if (!isUndefOrEqual(BitI, j))
3281 return false;
3282 if (!isUndefOrEqual(BitI1, j))
3283 return false;
3284 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003285 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003286}
3287
Nate Begeman9008ca62009-04-27 18:41:29 +00003288bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3289 SmallVector<int, 8> M;
3290 N->getMask(M);
3291 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3292}
3293
Evan Cheng017dcc62006-04-21 01:05:10 +00003294/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3295/// specifies a shuffle of elements that is suitable for input to MOVSS,
3296/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003297static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003298 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003299 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003300
3301 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003302
Nate Begeman9008ca62009-04-27 18:41:29 +00003303 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003304 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003305
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 for (int i = 1; i < NumElts; ++i)
3307 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003308 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003309
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003310 return true;
3311}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003312
Nate Begeman9008ca62009-04-27 18:41:29 +00003313bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3314 SmallVector<int, 8> M;
3315 N->getMask(M);
3316 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003317}
3318
Evan Cheng017dcc62006-04-21 01:05:10 +00003319/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3320/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003321/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003322static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003323 bool V2IsSplat = false, bool V2IsUndef = false) {
3324 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003325 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003326 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003327
Nate Begeman9008ca62009-04-27 18:41:29 +00003328 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003329 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003330
Nate Begeman9008ca62009-04-27 18:41:29 +00003331 for (int i = 1; i < NumOps; ++i)
3332 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3333 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3334 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003335 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003336
Evan Cheng39623da2006-04-20 08:58:49 +00003337 return true;
3338}
3339
Nate Begeman9008ca62009-04-27 18:41:29 +00003340static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003341 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003342 SmallVector<int, 8> M;
3343 N->getMask(M);
3344 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003345}
3346
Evan Chengd9539472006-04-14 21:59:03 +00003347/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3348/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003349bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3350 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003351 return false;
3352
3353 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003354 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003355 int Elt = N->getMaskElt(i);
3356 if (Elt >= 0 && Elt != 1)
3357 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003358 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003359
3360 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003361 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 int Elt = N->getMaskElt(i);
3363 if (Elt >= 0 && Elt != 3)
3364 return false;
3365 if (Elt == 3)
3366 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003367 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003368 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003370 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003371}
3372
3373/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3374/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003375bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3376 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003377 return false;
3378
3379 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003380 for (unsigned i = 0; i < 2; ++i)
3381 if (N->getMaskElt(i) > 0)
3382 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003383
3384 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003385 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 int Elt = N->getMaskElt(i);
3387 if (Elt >= 0 && Elt != 2)
3388 return false;
3389 if (Elt == 2)
3390 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003391 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003393 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003394}
3395
Evan Cheng0b457f02008-09-25 20:50:48 +00003396/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3397/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003398bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3399 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003400
Nate Begeman9008ca62009-04-27 18:41:29 +00003401 for (int i = 0; i < e; ++i)
3402 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003403 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 for (int i = 0; i < e; ++i)
3405 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003406 return false;
3407 return true;
3408}
3409
David Greenec38a03e2011-02-03 15:50:00 +00003410/// isVEXTRACTF128Index - Return true if the specified
3411/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3412/// suitable for input to VEXTRACTF128.
3413bool X86::isVEXTRACTF128Index(SDNode *N) {
3414 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3415 return false;
3416
3417 // The index should be aligned on a 128-bit boundary.
3418 uint64_t Index =
3419 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3420
3421 unsigned VL = N->getValueType(0).getVectorNumElements();
3422 unsigned VBits = N->getValueType(0).getSizeInBits();
3423 unsigned ElSize = VBits / VL;
3424 bool Result = (Index * ElSize) % 128 == 0;
3425
3426 return Result;
3427}
3428
David Greeneccacdc12011-02-04 16:08:29 +00003429/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3430/// operand specifies a subvector insert that is suitable for input to
3431/// VINSERTF128.
3432bool X86::isVINSERTF128Index(SDNode *N) {
3433 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3434 return false;
3435
3436 // The index should be aligned on a 128-bit boundary.
3437 uint64_t Index =
3438 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3439
3440 unsigned VL = N->getValueType(0).getVectorNumElements();
3441 unsigned VBits = N->getValueType(0).getSizeInBits();
3442 unsigned ElSize = VBits / VL;
3443 bool Result = (Index * ElSize) % 128 == 0;
3444
3445 return Result;
3446}
3447
Evan Cheng63d33002006-03-22 08:01:21 +00003448/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003449/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003450unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3452 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3453
Evan Chengb9df0ca2006-03-22 02:53:00 +00003454 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3455 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 for (int i = 0; i < NumOperands; ++i) {
3457 int Val = SVOp->getMaskElt(NumOperands-i-1);
3458 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003459 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003460 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003461 if (i != NumOperands - 1)
3462 Mask <<= Shift;
3463 }
Evan Cheng63d33002006-03-22 08:01:21 +00003464 return Mask;
3465}
3466
Evan Cheng506d3df2006-03-29 23:07:14 +00003467/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003468/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003469unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003470 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003471 unsigned Mask = 0;
3472 // 8 nodes, but we only care about the last 4.
3473 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 int Val = SVOp->getMaskElt(i);
3475 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003476 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003477 if (i != 4)
3478 Mask <<= 2;
3479 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003480 return Mask;
3481}
3482
3483/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003484/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003485unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003487 unsigned Mask = 0;
3488 // 8 nodes, but we only care about the first 4.
3489 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003490 int Val = SVOp->getMaskElt(i);
3491 if (Val >= 0)
3492 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003493 if (i != 0)
3494 Mask <<= 2;
3495 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003496 return Mask;
3497}
3498
Nate Begemana09008b2009-10-19 02:17:23 +00003499/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3500/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3501unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3502 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3503 EVT VVT = N->getValueType(0);
3504 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3505 int Val = 0;
3506
3507 unsigned i, e;
3508 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3509 Val = SVOp->getMaskElt(i);
3510 if (Val >= 0)
3511 break;
3512 }
3513 return (Val - i) * EltSize;
3514}
3515
David Greenec38a03e2011-02-03 15:50:00 +00003516/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3517/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3518/// instructions.
3519unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3520 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3521 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3522
3523 uint64_t Index =
3524 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3525
3526 EVT VecVT = N->getOperand(0).getValueType();
3527 EVT ElVT = VecVT.getVectorElementType();
3528
3529 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3530
3531 return Index / NumElemsPerChunk;
3532}
3533
David Greeneccacdc12011-02-04 16:08:29 +00003534/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3535/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3536/// instructions.
3537unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3538 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3539 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3540
3541 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003542 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003543
3544 EVT VecVT = N->getValueType(0);
3545 EVT ElVT = VecVT.getVectorElementType();
3546
3547 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3548
3549 return Index / NumElemsPerChunk;
3550}
3551
Evan Cheng37b73872009-07-30 08:33:02 +00003552/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3553/// constant +0.0.
3554bool X86::isZeroNode(SDValue Elt) {
3555 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003556 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003557 (isa<ConstantFPSDNode>(Elt) &&
3558 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3559}
3560
Nate Begeman9008ca62009-04-27 18:41:29 +00003561/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3562/// their permute mask.
3563static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3564 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003565 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003566 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003567 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003568
Nate Begeman5a5ca152009-04-29 05:20:52 +00003569 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003570 int idx = SVOp->getMaskElt(i);
3571 if (idx < 0)
3572 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003573 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003574 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003575 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003576 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003577 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003578 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3579 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003580}
3581
Evan Cheng779ccea2007-12-07 21:30:01 +00003582/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3583/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003584static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003585 unsigned NumElems = VT.getVectorNumElements();
3586 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003587 int idx = Mask[i];
3588 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003589 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003590 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003591 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003592 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003593 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003594 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003595}
3596
Evan Cheng533a0aa2006-04-19 20:35:22 +00003597/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3598/// match movhlps. The lower half elements should come from upper half of
3599/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003600/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003601static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3602 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003603 return false;
3604 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003606 return false;
3607 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003608 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003609 return false;
3610 return true;
3611}
3612
Evan Cheng5ced1d82006-04-06 23:23:56 +00003613/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003614/// is promoted to a vector. It also returns the LoadSDNode by reference if
3615/// required.
3616static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003617 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3618 return false;
3619 N = N->getOperand(0).getNode();
3620 if (!ISD::isNON_EXTLoad(N))
3621 return false;
3622 if (LD)
3623 *LD = cast<LoadSDNode>(N);
3624 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003625}
3626
Evan Cheng533a0aa2006-04-19 20:35:22 +00003627/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3628/// match movlp{s|d}. The lower half elements should come from lower half of
3629/// V1 (and in order), and the upper half elements should come from the upper
3630/// half of V2 (and in order). And since V1 will become the source of the
3631/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003632static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3633 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003634 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003635 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003636 // Is V2 is a vector load, don't do this transformation. We will try to use
3637 // load folding shufps op.
3638 if (ISD::isNON_EXTLoad(V2))
3639 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003640
Nate Begeman5a5ca152009-04-29 05:20:52 +00003641 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003642
Evan Cheng533a0aa2006-04-19 20:35:22 +00003643 if (NumElems != 2 && NumElems != 4)
3644 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003645 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003647 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003648 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003649 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003650 return false;
3651 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003652}
3653
Evan Cheng39623da2006-04-20 08:58:49 +00003654/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3655/// all the same.
3656static bool isSplatVector(SDNode *N) {
3657 if (N->getOpcode() != ISD::BUILD_VECTOR)
3658 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003659
Dan Gohman475871a2008-07-27 21:46:04 +00003660 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003661 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3662 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003663 return false;
3664 return true;
3665}
3666
Evan Cheng213d2cf2007-05-17 18:45:50 +00003667/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003668/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003669/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003670static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003671 SDValue V1 = N->getOperand(0);
3672 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003673 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3674 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003675 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003676 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003677 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003678 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3679 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003680 if (Opc != ISD::BUILD_VECTOR ||
3681 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003682 return false;
3683 } else if (Idx >= 0) {
3684 unsigned Opc = V1.getOpcode();
3685 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3686 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003687 if (Opc != ISD::BUILD_VECTOR ||
3688 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003689 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003690 }
3691 }
3692 return true;
3693}
3694
3695/// getZeroVector - Returns a vector of specified type with all zero elements.
3696///
Owen Andersone50ed302009-08-10 22:56:29 +00003697static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003698 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003699 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003700
Dale Johannesen0488fb62010-09-30 23:57:10 +00003701 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003702 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003703 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003704 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003705 if (HasSSE2) { // SSE2
3706 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3707 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3708 } else { // SSE1
3709 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3710 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3711 }
3712 } else if (VT.getSizeInBits() == 256) { // AVX
3713 // 256-bit logic and arithmetic instructions in AVX are
3714 // all floating-point, no support for integer ops. Default
3715 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003717 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3718 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003719 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003720 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003721}
3722
Chris Lattner8a594482007-11-25 00:24:49 +00003723/// getOnesVector - Returns a vector of specified type with all bits set.
3724///
Owen Andersone50ed302009-08-10 22:56:29 +00003725static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003726 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003727
Chris Lattner8a594482007-11-25 00:24:49 +00003728 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3729 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003730 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003731 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003732 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003733 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003734}
3735
3736
Evan Cheng39623da2006-04-20 08:58:49 +00003737/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3738/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003739static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003740 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003741 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003742
Evan Cheng39623da2006-04-20 08:58:49 +00003743 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003744 SmallVector<int, 8> MaskVec;
3745 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003746
Nate Begeman5a5ca152009-04-29 05:20:52 +00003747 for (unsigned i = 0; i != NumElems; ++i) {
3748 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003749 MaskVec[i] = NumElems;
3750 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003751 }
Evan Cheng39623da2006-04-20 08:58:49 +00003752 }
Evan Cheng39623da2006-04-20 08:58:49 +00003753 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003754 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3755 SVOp->getOperand(1), &MaskVec[0]);
3756 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003757}
3758
Evan Cheng017dcc62006-04-21 01:05:10 +00003759/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3760/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003761static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003762 SDValue V2) {
3763 unsigned NumElems = VT.getVectorNumElements();
3764 SmallVector<int, 8> Mask;
3765 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003766 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003767 Mask.push_back(i);
3768 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003769}
3770
Nate Begeman9008ca62009-04-27 18:41:29 +00003771/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003772static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003773 SDValue V2) {
3774 unsigned NumElems = VT.getVectorNumElements();
3775 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003776 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003777 Mask.push_back(i);
3778 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003779 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003780 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003781}
3782
Nate Begeman9008ca62009-04-27 18:41:29 +00003783/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003784static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003785 SDValue V2) {
3786 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003787 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003789 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003790 Mask.push_back(i + Half);
3791 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003792 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003793 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003794}
3795
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003796/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3797static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003799 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 DebugLoc dl = SV->getDebugLoc();
3801 SDValue V1 = SV->getOperand(0);
3802 int NumElems = VT.getVectorNumElements();
3803 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003804
Nate Begeman9008ca62009-04-27 18:41:29 +00003805 // unpack elements to the correct location
3806 while (NumElems > 4) {
3807 if (EltNo < NumElems/2) {
3808 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3809 } else {
3810 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3811 EltNo -= NumElems/2;
3812 }
3813 NumElems >>= 1;
3814 }
Eric Christopherfd179292009-08-27 18:07:15 +00003815
Nate Begeman9008ca62009-04-27 18:41:29 +00003816 // Perform the splat.
3817 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003818 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003819 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003820 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003821}
3822
Evan Chengba05f722006-04-21 23:03:30 +00003823/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003824/// vector of zero or undef vector. This produces a shuffle where the low
3825/// element of V2 is swizzled into the zero/undef vector, landing at element
3826/// 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 +00003827static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003828 bool isZero, bool HasSSE2,
3829 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003830 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003831 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3833 unsigned NumElems = VT.getVectorNumElements();
3834 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003835 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003836 // If this is the insertion idx, put the low elt of V2 here.
3837 MaskVec.push_back(i == Idx ? NumElems : i);
3838 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003839}
3840
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003841/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3842/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003843SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3844 unsigned Depth) {
3845 if (Depth == 6)
3846 return SDValue(); // Limit search depth.
3847
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003848 SDValue V = SDValue(N, 0);
3849 EVT VT = V.getValueType();
3850 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003851
3852 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3853 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3854 Index = SV->getMaskElt(Index);
3855
3856 if (Index < 0)
3857 return DAG.getUNDEF(VT.getVectorElementType());
3858
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003859 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003860 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003861 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003862 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003863
3864 // Recurse into target specific vector shuffles to find scalars.
3865 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003866 int NumElems = VT.getVectorNumElements();
3867 SmallVector<unsigned, 16> ShuffleMask;
3868 SDValue ImmN;
3869
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003870 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003871 case X86ISD::SHUFPS:
3872 case X86ISD::SHUFPD:
3873 ImmN = N->getOperand(N->getNumOperands()-1);
3874 DecodeSHUFPSMask(NumElems,
3875 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3876 ShuffleMask);
3877 break;
3878 case X86ISD::PUNPCKHBW:
3879 case X86ISD::PUNPCKHWD:
3880 case X86ISD::PUNPCKHDQ:
3881 case X86ISD::PUNPCKHQDQ:
3882 DecodePUNPCKHMask(NumElems, ShuffleMask);
3883 break;
3884 case X86ISD::UNPCKHPS:
3885 case X86ISD::UNPCKHPD:
3886 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3887 break;
3888 case X86ISD::PUNPCKLBW:
3889 case X86ISD::PUNPCKLWD:
3890 case X86ISD::PUNPCKLDQ:
3891 case X86ISD::PUNPCKLQDQ:
3892 DecodePUNPCKLMask(NumElems, ShuffleMask);
3893 break;
3894 case X86ISD::UNPCKLPS:
3895 case X86ISD::UNPCKLPD:
3896 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3897 break;
3898 case X86ISD::MOVHLPS:
3899 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3900 break;
3901 case X86ISD::MOVLHPS:
3902 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3903 break;
3904 case X86ISD::PSHUFD:
3905 ImmN = N->getOperand(N->getNumOperands()-1);
3906 DecodePSHUFMask(NumElems,
3907 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3908 ShuffleMask);
3909 break;
3910 case X86ISD::PSHUFHW:
3911 ImmN = N->getOperand(N->getNumOperands()-1);
3912 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3913 ShuffleMask);
3914 break;
3915 case X86ISD::PSHUFLW:
3916 ImmN = N->getOperand(N->getNumOperands()-1);
3917 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3918 ShuffleMask);
3919 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003920 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003921 case X86ISD::MOVSD: {
3922 // The index 0 always comes from the first element of the second source,
3923 // this is why MOVSS and MOVSD are used in the first place. The other
3924 // elements come from the other positions of the first source vector.
3925 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003926 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3927 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003928 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003929 default:
3930 assert("not implemented for target shuffle node");
3931 return SDValue();
3932 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003933
3934 Index = ShuffleMask[Index];
3935 if (Index < 0)
3936 return DAG.getUNDEF(VT.getVectorElementType());
3937
3938 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3939 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3940 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003941 }
3942
3943 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003944 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003945 V = V.getOperand(0);
3946 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003947 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003948
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003949 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003950 return SDValue();
3951 }
3952
3953 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3954 return (Index == 0) ? V.getOperand(0)
3955 : DAG.getUNDEF(VT.getVectorElementType());
3956
3957 if (V.getOpcode() == ISD::BUILD_VECTOR)
3958 return V.getOperand(Index);
3959
3960 return SDValue();
3961}
3962
3963/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3964/// shuffle operation which come from a consecutively from a zero. The
3965/// search can start in two diferent directions, from left or right.
3966static
3967unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3968 bool ZerosFromLeft, SelectionDAG &DAG) {
3969 int i = 0;
3970
3971 while (i < NumElems) {
3972 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003973 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003974 if (!(Elt.getNode() &&
3975 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3976 break;
3977 ++i;
3978 }
3979
3980 return i;
3981}
3982
3983/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3984/// MaskE correspond consecutively to elements from one of the vector operands,
3985/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3986static
3987bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3988 int OpIdx, int NumElems, unsigned &OpNum) {
3989 bool SeenV1 = false;
3990 bool SeenV2 = false;
3991
3992 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3993 int Idx = SVOp->getMaskElt(i);
3994 // Ignore undef indicies
3995 if (Idx < 0)
3996 continue;
3997
3998 if (Idx < NumElems)
3999 SeenV1 = true;
4000 else
4001 SeenV2 = true;
4002
4003 // Only accept consecutive elements from the same vector
4004 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4005 return false;
4006 }
4007
4008 OpNum = SeenV1 ? 0 : 1;
4009 return true;
4010}
4011
4012/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4013/// logical left shift of a vector.
4014static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4015 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4016 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4017 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4018 false /* check zeros from right */, DAG);
4019 unsigned OpSrc;
4020
4021 if (!NumZeros)
4022 return false;
4023
4024 // Considering the elements in the mask that are not consecutive zeros,
4025 // check if they consecutively come from only one of the source vectors.
4026 //
4027 // V1 = {X, A, B, C} 0
4028 // \ \ \ /
4029 // vector_shuffle V1, V2 <1, 2, 3, X>
4030 //
4031 if (!isShuffleMaskConsecutive(SVOp,
4032 0, // Mask Start Index
4033 NumElems-NumZeros-1, // Mask End Index
4034 NumZeros, // Where to start looking in the src vector
4035 NumElems, // Number of elements in vector
4036 OpSrc)) // Which source operand ?
4037 return false;
4038
4039 isLeft = false;
4040 ShAmt = NumZeros;
4041 ShVal = SVOp->getOperand(OpSrc);
4042 return true;
4043}
4044
4045/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4046/// logical left shift of a vector.
4047static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4048 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4049 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4050 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4051 true /* check zeros from left */, DAG);
4052 unsigned OpSrc;
4053
4054 if (!NumZeros)
4055 return false;
4056
4057 // Considering the elements in the mask that are not consecutive zeros,
4058 // check if they consecutively come from only one of the source vectors.
4059 //
4060 // 0 { A, B, X, X } = V2
4061 // / \ / /
4062 // vector_shuffle V1, V2 <X, X, 4, 5>
4063 //
4064 if (!isShuffleMaskConsecutive(SVOp,
4065 NumZeros, // Mask Start Index
4066 NumElems-1, // Mask End Index
4067 0, // Where to start looking in the src vector
4068 NumElems, // Number of elements in vector
4069 OpSrc)) // Which source operand ?
4070 return false;
4071
4072 isLeft = true;
4073 ShAmt = NumZeros;
4074 ShVal = SVOp->getOperand(OpSrc);
4075 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004076}
4077
4078/// isVectorShift - Returns true if the shuffle can be implemented as a
4079/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004080static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004081 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004082 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4083 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4084 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004085
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004086 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004087}
4088
Evan Chengc78d3b42006-04-24 18:01:45 +00004089/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4090///
Dan Gohman475871a2008-07-27 21:46:04 +00004091static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004092 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004093 SelectionDAG &DAG,
4094 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004095 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004096 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004097
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004098 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004099 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004100 bool First = true;
4101 for (unsigned i = 0; i < 16; ++i) {
4102 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4103 if (ThisIsNonZero && First) {
4104 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004105 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004106 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004107 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004108 First = false;
4109 }
4110
4111 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004112 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004113 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4114 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004115 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004116 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004117 }
4118 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004119 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4120 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4121 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004122 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004124 } else
4125 ThisElt = LastElt;
4126
Gabor Greifba36cb52008-08-28 21:40:38 +00004127 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004128 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004129 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004130 }
4131 }
4132
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004133 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004134}
4135
Bill Wendlinga348c562007-03-22 18:42:45 +00004136/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004137///
Dan Gohman475871a2008-07-27 21:46:04 +00004138static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004139 unsigned NumNonZero, unsigned NumZero,
4140 SelectionDAG &DAG,
4141 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004142 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004143 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004144
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004145 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004146 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004147 bool First = true;
4148 for (unsigned i = 0; i < 8; ++i) {
4149 bool isNonZero = (NonZeros & (1 << i)) != 0;
4150 if (isNonZero) {
4151 if (First) {
4152 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004154 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004155 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004156 First = false;
4157 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004158 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004160 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004161 }
4162 }
4163
4164 return V;
4165}
4166
Evan Chengf26ffe92008-05-29 08:22:04 +00004167/// getVShift - Return a vector logical shift node.
4168///
Owen Andersone50ed302009-08-10 22:56:29 +00004169static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004170 unsigned NumBits, SelectionDAG &DAG,
4171 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004172 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004173 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004174 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4175 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004176 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004177 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004178}
4179
Dan Gohman475871a2008-07-27 21:46:04 +00004180SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004181X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004182 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004183
Evan Chengc3630942009-12-09 21:00:30 +00004184 // Check if the scalar load can be widened into a vector load. And if
4185 // the address is "base + cst" see if the cst can be "absorbed" into
4186 // the shuffle mask.
4187 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4188 SDValue Ptr = LD->getBasePtr();
4189 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4190 return SDValue();
4191 EVT PVT = LD->getValueType(0);
4192 if (PVT != MVT::i32 && PVT != MVT::f32)
4193 return SDValue();
4194
4195 int FI = -1;
4196 int64_t Offset = 0;
4197 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4198 FI = FINode->getIndex();
4199 Offset = 0;
4200 } else if (Ptr.getOpcode() == ISD::ADD &&
4201 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4202 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4203 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4204 Offset = Ptr.getConstantOperandVal(1);
4205 Ptr = Ptr.getOperand(0);
4206 } else {
4207 return SDValue();
4208 }
4209
4210 SDValue Chain = LD->getChain();
4211 // Make sure the stack object alignment is at least 16.
4212 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4213 if (DAG.InferPtrAlignment(Ptr) < 16) {
4214 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004215 // Can't change the alignment. FIXME: It's possible to compute
4216 // the exact stack offset and reference FI + adjust offset instead.
4217 // If someone *really* cares about this. That's the way to implement it.
4218 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004219 } else {
4220 MFI->setObjectAlignment(FI, 16);
4221 }
4222 }
4223
4224 // (Offset % 16) must be multiple of 4. Then address is then
4225 // Ptr + (Offset & ~15).
4226 if (Offset < 0)
4227 return SDValue();
4228 if ((Offset % 16) & 3)
4229 return SDValue();
4230 int64_t StartOffset = Offset & ~15;
4231 if (StartOffset)
4232 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4233 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4234
4235 int EltNo = (Offset - StartOffset) >> 2;
4236 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4237 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004238 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4239 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004240 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004241 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004242 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4243 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004244 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004245 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004246 }
4247
4248 return SDValue();
4249}
4250
Michael J. Spencerec38de22010-10-10 22:04:20 +00004251/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4252/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004253/// load which has the same value as a build_vector whose operands are 'elts'.
4254///
4255/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004256///
Nate Begeman1449f292010-03-24 22:19:06 +00004257/// FIXME: we'd also like to handle the case where the last elements are zero
4258/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4259/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004260static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004261 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004262 EVT EltVT = VT.getVectorElementType();
4263 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004264
Nate Begemanfdea31a2010-03-24 20:49:50 +00004265 LoadSDNode *LDBase = NULL;
4266 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004267
Nate Begeman1449f292010-03-24 22:19:06 +00004268 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004269 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004270 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004271 for (unsigned i = 0; i < NumElems; ++i) {
4272 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004273
Nate Begemanfdea31a2010-03-24 20:49:50 +00004274 if (!Elt.getNode() ||
4275 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4276 return SDValue();
4277 if (!LDBase) {
4278 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4279 return SDValue();
4280 LDBase = cast<LoadSDNode>(Elt.getNode());
4281 LastLoadedElt = i;
4282 continue;
4283 }
4284 if (Elt.getOpcode() == ISD::UNDEF)
4285 continue;
4286
4287 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4288 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4289 return SDValue();
4290 LastLoadedElt = i;
4291 }
Nate Begeman1449f292010-03-24 22:19:06 +00004292
4293 // If we have found an entire vector of loads and undefs, then return a large
4294 // load of the entire vector width starting at the base pointer. If we found
4295 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004296 if (LastLoadedElt == NumElems - 1) {
4297 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004298 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004299 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004300 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004301 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004302 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004303 LDBase->isVolatile(), LDBase->isNonTemporal(),
4304 LDBase->getAlignment());
4305 } else if (NumElems == 4 && LastLoadedElt == 1) {
4306 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4307 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004308 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4309 Ops, 2, MVT::i32,
4310 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004311 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004312 }
4313 return SDValue();
4314}
4315
Evan Chengc3630942009-12-09 21:00:30 +00004316SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004317X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004318 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004319
David Greenef125a292011-02-08 19:04:41 +00004320 EVT VT = Op.getValueType();
4321 EVT ExtVT = VT.getVectorElementType();
4322
4323 unsigned NumElems = Op.getNumOperands();
4324
4325 // For AVX-length vectors, build the individual 128-bit pieces and
4326 // use shuffles to put them in place.
4327 if (VT.getSizeInBits() > 256 &&
4328 Subtarget->hasAVX() &&
4329 !Disable256Bit &&
4330 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4331 SmallVector<SDValue, 8> V;
4332 V.resize(NumElems);
4333 for (unsigned i = 0; i < NumElems; ++i) {
4334 V[i] = Op.getOperand(i);
4335 }
4336
4337 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4338
4339 // Build the lower subvector.
4340 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4341 // Build the upper subvector.
4342 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4343 NumElems/2);
4344
4345 return ConcatVectors(Lower, Upper, DAG);
4346 }
4347
Chris Lattner6e80e442010-08-28 17:15:43 +00004348 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4349 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004350 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4351 // is present, so AllOnes is ignored.
4352 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4353 (Op.getValueType().getSizeInBits() != 256 &&
4354 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004355 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004356 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4357 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004358 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004359 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004360
Gabor Greifba36cb52008-08-28 21:40:38 +00004361 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004362 return getOnesVector(Op.getValueType(), DAG, dl);
4363 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004364 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004365
Owen Andersone50ed302009-08-10 22:56:29 +00004366 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004367
Evan Cheng0db9fe62006-04-25 20:13:52 +00004368 unsigned NumZero = 0;
4369 unsigned NumNonZero = 0;
4370 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004371 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004372 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004373 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004374 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004375 if (Elt.getOpcode() == ISD::UNDEF)
4376 continue;
4377 Values.insert(Elt);
4378 if (Elt.getOpcode() != ISD::Constant &&
4379 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004380 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004381 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004382 NumZero++;
4383 else {
4384 NonZeros |= (1 << i);
4385 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004386 }
4387 }
4388
Chris Lattner97a2a562010-08-26 05:24:29 +00004389 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4390 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004391 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004392
Chris Lattner67f453a2008-03-09 05:42:06 +00004393 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004394 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004395 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004396 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004397
Chris Lattner62098042008-03-09 01:05:04 +00004398 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4399 // the value are obviously zero, truncate the value to i32 and do the
4400 // insertion that way. Only do this if the value is non-constant or if the
4401 // value is a constant being inserted into element 0. It is cheaper to do
4402 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004403 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004404 (!IsAllConstants || Idx == 0)) {
4405 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004406 // Handle SSE only.
4407 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4408 EVT VecVT = MVT::v4i32;
4409 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004410
Chris Lattner62098042008-03-09 01:05:04 +00004411 // Truncate the value (which may itself be a constant) to i32, and
4412 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004413 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004414 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004415 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4416 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004417
Chris Lattner62098042008-03-09 01:05:04 +00004418 // Now we have our 32-bit value zero extended in the low element of
4419 // a vector. If Idx != 0, swizzle it into place.
4420 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004421 SmallVector<int, 4> Mask;
4422 Mask.push_back(Idx);
4423 for (unsigned i = 1; i != VecElts; ++i)
4424 Mask.push_back(i);
4425 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004426 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004428 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004429 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004430 }
4431 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004432
Chris Lattner19f79692008-03-08 22:59:52 +00004433 // If we have a constant or non-constant insertion into the low element of
4434 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4435 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004436 // depending on what the source datatype is.
4437 if (Idx == 0) {
4438 if (NumZero == 0) {
4439 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004440 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4441 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004442 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4443 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4444 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4445 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004446 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4447 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004448 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4449 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004450 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4451 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4452 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004453 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004454 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004455 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004456
4457 // Is it a vector logical left shift?
4458 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004459 X86::isZeroNode(Op.getOperand(0)) &&
4460 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004461 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004462 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004463 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004464 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004465 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004467
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004468 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004469 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004470
Chris Lattner19f79692008-03-08 22:59:52 +00004471 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4472 // is a non-constant being inserted into an element other than the low one,
4473 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4474 // movd/movss) to move this into the low element, then shuffle it into
4475 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004476 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004477 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004478
Evan Cheng0db9fe62006-04-25 20:13:52 +00004479 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004480 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4481 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004483 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 MaskVec.push_back(i == Idx ? 0 : 1);
4485 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004486 }
4487 }
4488
Chris Lattner67f453a2008-03-09 05:42:06 +00004489 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004490 if (Values.size() == 1) {
4491 if (EVTBits == 32) {
4492 // Instead of a shuffle like this:
4493 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4494 // Check if it's possible to issue this instead.
4495 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4496 unsigned Idx = CountTrailingZeros_32(NonZeros);
4497 SDValue Item = Op.getOperand(Idx);
4498 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4499 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4500 }
Dan Gohman475871a2008-07-27 21:46:04 +00004501 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004502 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004503
Dan Gohmana3941172007-07-24 22:55:08 +00004504 // A vector full of immediates; various special cases are already
4505 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004506 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004507 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004508
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004509 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004510 if (EVTBits == 64) {
4511 if (NumNonZero == 1) {
4512 // One half is zero or undef.
4513 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004514 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004515 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004516 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4517 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004518 }
Dan Gohman475871a2008-07-27 21:46:04 +00004519 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004520 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004521
4522 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004523 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004524 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004525 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004526 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004527 }
4528
Bill Wendling826f36f2007-03-28 00:57:11 +00004529 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004530 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004531 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004532 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004533 }
4534
4535 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004536 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004537 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004538 if (NumElems == 4 && NumZero > 0) {
4539 for (unsigned i = 0; i < 4; ++i) {
4540 bool isZero = !(NonZeros & (1 << i));
4541 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004542 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004543 else
Dale Johannesenace16102009-02-03 19:33:06 +00004544 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004545 }
4546
4547 for (unsigned i = 0; i < 2; ++i) {
4548 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4549 default: break;
4550 case 0:
4551 V[i] = V[i*2]; // Must be a zero vector.
4552 break;
4553 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004554 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004555 break;
4556 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004557 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004558 break;
4559 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004560 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004561 break;
4562 }
4563 }
4564
Nate Begeman9008ca62009-04-27 18:41:29 +00004565 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004566 bool Reverse = (NonZeros & 0x3) == 2;
4567 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004568 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004569 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4570 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004571 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4572 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004573 }
4574
Nate Begemanfdea31a2010-03-24 20:49:50 +00004575 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4576 // Check for a build vector of consecutive loads.
4577 for (unsigned i = 0; i < NumElems; ++i)
4578 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004579
Nate Begemanfdea31a2010-03-24 20:49:50 +00004580 // Check for elements which are consecutive loads.
4581 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4582 if (LD.getNode())
4583 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004584
4585 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004586 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004587 SDValue Result;
4588 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4589 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4590 else
4591 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004592
Chris Lattner24faf612010-08-28 17:59:08 +00004593 for (unsigned i = 1; i < NumElems; ++i) {
4594 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4595 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004596 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004597 }
4598 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004599 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004600
Chris Lattner6e80e442010-08-28 17:15:43 +00004601 // Otherwise, expand into a number of unpckl*, start by extending each of
4602 // our (non-undef) elements to the full vector width with the element in the
4603 // bottom slot of the vector (which generates no code for SSE).
4604 for (unsigned i = 0; i < NumElems; ++i) {
4605 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4606 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4607 else
4608 V[i] = DAG.getUNDEF(VT);
4609 }
4610
4611 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004612 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4613 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4614 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004615 unsigned EltStride = NumElems >> 1;
4616 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004617 for (unsigned i = 0; i < EltStride; ++i) {
4618 // If V[i+EltStride] is undef and this is the first round of mixing,
4619 // then it is safe to just drop this shuffle: V[i] is already in the
4620 // right place, the one element (since it's the first round) being
4621 // inserted as undef can be dropped. This isn't safe for successive
4622 // rounds because they will permute elements within both vectors.
4623 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4624 EltStride == NumElems/2)
4625 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004626
Chris Lattner6e80e442010-08-28 17:15:43 +00004627 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004628 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004629 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004630 }
4631 return V[0];
4632 }
Dan Gohman475871a2008-07-27 21:46:04 +00004633 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004634}
4635
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004636SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004637X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004638 // We support concatenate two MMX registers and place them in a MMX
4639 // register. This is better than doing a stack convert.
4640 DebugLoc dl = Op.getDebugLoc();
4641 EVT ResVT = Op.getValueType();
4642 assert(Op.getNumOperands() == 2);
4643 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4644 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4645 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004646 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004647 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4648 InVec = Op.getOperand(1);
4649 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4650 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004651 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004652 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4653 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4654 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004655 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004656 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4657 Mask[0] = 0; Mask[1] = 2;
4658 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4659 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004660 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004661}
4662
Nate Begemanb9a47b82009-02-23 08:49:38 +00004663// v8i16 shuffles - Prefer shuffles in the following order:
4664// 1. [all] pshuflw, pshufhw, optional move
4665// 2. [ssse3] 1 x pshufb
4666// 3. [ssse3] 2 x pshufb + 1 x por
4667// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004668SDValue
4669X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4670 SelectionDAG &DAG) const {
4671 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004672 SDValue V1 = SVOp->getOperand(0);
4673 SDValue V2 = SVOp->getOperand(1);
4674 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004675 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004676
Nate Begemanb9a47b82009-02-23 08:49:38 +00004677 // Determine if more than 1 of the words in each of the low and high quadwords
4678 // of the result come from the same quadword of one of the two inputs. Undef
4679 // mask values count as coming from any quadword, for better codegen.
4680 SmallVector<unsigned, 4> LoQuad(4);
4681 SmallVector<unsigned, 4> HiQuad(4);
4682 BitVector InputQuads(4);
4683 for (unsigned i = 0; i < 8; ++i) {
4684 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004685 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004686 MaskVals.push_back(EltIdx);
4687 if (EltIdx < 0) {
4688 ++Quad[0];
4689 ++Quad[1];
4690 ++Quad[2];
4691 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004692 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004693 }
4694 ++Quad[EltIdx / 4];
4695 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004696 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004697
Nate Begemanb9a47b82009-02-23 08:49:38 +00004698 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004699 unsigned MaxQuad = 1;
4700 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004701 if (LoQuad[i] > MaxQuad) {
4702 BestLoQuad = i;
4703 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004704 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004705 }
4706
Nate Begemanb9a47b82009-02-23 08:49:38 +00004707 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004708 MaxQuad = 1;
4709 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004710 if (HiQuad[i] > MaxQuad) {
4711 BestHiQuad = i;
4712 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004713 }
4714 }
4715
Nate Begemanb9a47b82009-02-23 08:49:38 +00004716 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004717 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004718 // single pshufb instruction is necessary. If There are more than 2 input
4719 // quads, disable the next transformation since it does not help SSSE3.
4720 bool V1Used = InputQuads[0] || InputQuads[1];
4721 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004722 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004723 if (InputQuads.count() == 2 && V1Used && V2Used) {
4724 BestLoQuad = InputQuads.find_first();
4725 BestHiQuad = InputQuads.find_next(BestLoQuad);
4726 }
4727 if (InputQuads.count() > 2) {
4728 BestLoQuad = -1;
4729 BestHiQuad = -1;
4730 }
4731 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004732
Nate Begemanb9a47b82009-02-23 08:49:38 +00004733 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4734 // the shuffle mask. If a quad is scored as -1, that means that it contains
4735 // words from all 4 input quadwords.
4736 SDValue NewV;
4737 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004738 SmallVector<int, 8> MaskV;
4739 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4740 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004741 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004742 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4743 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4744 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004745
Nate Begemanb9a47b82009-02-23 08:49:38 +00004746 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4747 // source words for the shuffle, to aid later transformations.
4748 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004749 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004750 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004751 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004752 if (idx != (int)i)
4753 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004754 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004755 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004756 AllWordsInNewV = false;
4757 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004758 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004759
Nate Begemanb9a47b82009-02-23 08:49:38 +00004760 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4761 if (AllWordsInNewV) {
4762 for (int i = 0; i != 8; ++i) {
4763 int idx = MaskVals[i];
4764 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004765 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004766 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 if ((idx != i) && idx < 4)
4768 pshufhw = false;
4769 if ((idx != i) && idx > 3)
4770 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004771 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004772 V1 = NewV;
4773 V2Used = false;
4774 BestLoQuad = 0;
4775 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004776 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004777
Nate Begemanb9a47b82009-02-23 08:49:38 +00004778 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4779 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004780 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004781 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4782 unsigned TargetMask = 0;
4783 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004785 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4786 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4787 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004788 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004789 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004790 }
Eric Christopherfd179292009-08-27 18:07:15 +00004791
Nate Begemanb9a47b82009-02-23 08:49:38 +00004792 // If we have SSSE3, and all words of the result are from 1 input vector,
4793 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4794 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004795 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004796 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004797
Nate Begemanb9a47b82009-02-23 08:49:38 +00004798 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004799 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 // mask, and elements that come from V1 in the V2 mask, so that the two
4801 // results can be OR'd together.
4802 bool TwoInputs = V1Used && V2Used;
4803 for (unsigned i = 0; i != 8; ++i) {
4804 int EltIdx = MaskVals[i] * 2;
4805 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004806 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4807 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 continue;
4809 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004810 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4811 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004812 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004813 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004814 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004815 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004818 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004819
Nate Begemanb9a47b82009-02-23 08:49:38 +00004820 // Calculate the shuffle mask for the second input, shuffle it, and
4821 // OR it with the first shuffled input.
4822 pshufbMask.clear();
4823 for (unsigned i = 0; i != 8; ++i) {
4824 int EltIdx = MaskVals[i] * 2;
4825 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4827 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004828 continue;
4829 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004830 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4831 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004833 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004834 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004835 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004836 MVT::v16i8, &pshufbMask[0], 16));
4837 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004838 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004839 }
4840
4841 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4842 // and update MaskVals with new element order.
4843 BitVector InOrder(8);
4844 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004845 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004846 for (int i = 0; i != 4; ++i) {
4847 int idx = MaskVals[i];
4848 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004849 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004850 InOrder.set(i);
4851 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004852 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004853 InOrder.set(i);
4854 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004855 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004856 }
4857 }
4858 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004859 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004861 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004862
4863 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4864 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4865 NewV.getOperand(0),
4866 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4867 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 }
Eric Christopherfd179292009-08-27 18:07:15 +00004869
Nate Begemanb9a47b82009-02-23 08:49:38 +00004870 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4871 // and update MaskVals with the new element order.
4872 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004873 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004874 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004875 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004876 for (unsigned i = 4; i != 8; ++i) {
4877 int idx = MaskVals[i];
4878 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004879 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004880 InOrder.set(i);
4881 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004882 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004883 InOrder.set(i);
4884 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004885 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004886 }
4887 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004888 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004889 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004890
4891 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4892 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4893 NewV.getOperand(0),
4894 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4895 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004896 }
Eric Christopherfd179292009-08-27 18:07:15 +00004897
Nate Begemanb9a47b82009-02-23 08:49:38 +00004898 // In case BestHi & BestLo were both -1, which means each quadword has a word
4899 // from each of the four input quadwords, calculate the InOrder bitvector now
4900 // before falling through to the insert/extract cleanup.
4901 if (BestLoQuad == -1 && BestHiQuad == -1) {
4902 NewV = V1;
4903 for (int i = 0; i != 8; ++i)
4904 if (MaskVals[i] < 0 || MaskVals[i] == i)
4905 InOrder.set(i);
4906 }
Eric Christopherfd179292009-08-27 18:07:15 +00004907
Nate Begemanb9a47b82009-02-23 08:49:38 +00004908 // The other elements are put in the right place using pextrw and pinsrw.
4909 for (unsigned i = 0; i != 8; ++i) {
4910 if (InOrder[i])
4911 continue;
4912 int EltIdx = MaskVals[i];
4913 if (EltIdx < 0)
4914 continue;
4915 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004916 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004917 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004918 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004919 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004920 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004921 DAG.getIntPtrConstant(i));
4922 }
4923 return NewV;
4924}
4925
4926// v16i8 shuffles - Prefer shuffles in the following order:
4927// 1. [ssse3] 1 x pshufb
4928// 2. [ssse3] 2 x pshufb + 1 x por
4929// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4930static
Nate Begeman9008ca62009-04-27 18:41:29 +00004931SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004932 SelectionDAG &DAG,
4933 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004934 SDValue V1 = SVOp->getOperand(0);
4935 SDValue V2 = SVOp->getOperand(1);
4936 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004937 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004938 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004939
Nate Begemanb9a47b82009-02-23 08:49:38 +00004940 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004941 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 // present, fall back to case 3.
4943 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4944 bool V1Only = true;
4945 bool V2Only = true;
4946 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004947 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004948 if (EltIdx < 0)
4949 continue;
4950 if (EltIdx < 16)
4951 V2Only = false;
4952 else
4953 V1Only = false;
4954 }
Eric Christopherfd179292009-08-27 18:07:15 +00004955
Nate Begemanb9a47b82009-02-23 08:49:38 +00004956 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4957 if (TLI.getSubtarget()->hasSSSE3()) {
4958 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004959
Nate Begemanb9a47b82009-02-23 08:49:38 +00004960 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004961 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004962 //
4963 // Otherwise, we have elements from both input vectors, and must zero out
4964 // elements that come from V2 in the first mask, and V1 in the second mask
4965 // so that we can OR them together.
4966 bool TwoInputs = !(V1Only || V2Only);
4967 for (unsigned i = 0; i != 16; ++i) {
4968 int EltIdx = MaskVals[i];
4969 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004970 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004971 continue;
4972 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004973 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004974 }
4975 // If all the elements are from V2, assign it to V1 and return after
4976 // building the first pshufb.
4977 if (V2Only)
4978 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004979 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004980 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004981 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004982 if (!TwoInputs)
4983 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004984
Nate Begemanb9a47b82009-02-23 08:49:38 +00004985 // Calculate the shuffle mask for the second input, shuffle it, and
4986 // OR it with the first shuffled input.
4987 pshufbMask.clear();
4988 for (unsigned i = 0; i != 16; ++i) {
4989 int EltIdx = MaskVals[i];
4990 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004991 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 continue;
4993 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004994 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004995 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004996 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004997 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004998 MVT::v16i8, &pshufbMask[0], 16));
4999 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005000 }
Eric Christopherfd179292009-08-27 18:07:15 +00005001
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 // No SSSE3 - Calculate in place words and then fix all out of place words
5003 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5004 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005005 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5006 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005007 SDValue NewV = V2Only ? V2 : V1;
5008 for (int i = 0; i != 8; ++i) {
5009 int Elt0 = MaskVals[i*2];
5010 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005011
Nate Begemanb9a47b82009-02-23 08:49:38 +00005012 // This word of the result is all undef, skip it.
5013 if (Elt0 < 0 && Elt1 < 0)
5014 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005015
Nate Begemanb9a47b82009-02-23 08:49:38 +00005016 // This word of the result is already in the correct place, skip it.
5017 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5018 continue;
5019 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5020 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005021
Nate Begemanb9a47b82009-02-23 08:49:38 +00005022 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5023 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5024 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005025
5026 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5027 // using a single extract together, load it and store it.
5028 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005029 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005030 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005031 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005032 DAG.getIntPtrConstant(i));
5033 continue;
5034 }
5035
Nate Begemanb9a47b82009-02-23 08:49:38 +00005036 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005037 // source byte is not also odd, shift the extracted word left 8 bits
5038 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005039 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005040 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005041 DAG.getIntPtrConstant(Elt1 / 2));
5042 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005043 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005044 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005045 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005046 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5047 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005048 }
5049 // If Elt0 is defined, extract it from the appropriate source. If the
5050 // source byte is not also even, shift the extracted word right 8 bits. If
5051 // Elt1 was also defined, OR the extracted values together before
5052 // inserting them in the result.
5053 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005054 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005055 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5056 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005057 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005058 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005059 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005060 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5061 DAG.getConstant(0x00FF, MVT::i16));
5062 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005063 : InsElt0;
5064 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005066 DAG.getIntPtrConstant(i));
5067 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005068 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005069}
5070
Evan Cheng7a831ce2007-12-15 03:00:47 +00005071/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005072/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005073/// done when every pair / quad of shuffle mask elements point to elements in
5074/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005075/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005076static
Nate Begeman9008ca62009-04-27 18:41:29 +00005077SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005078 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005079 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005080 SDValue V1 = SVOp->getOperand(0);
5081 SDValue V2 = SVOp->getOperand(1);
5082 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005083 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005084 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005085 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005086 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005087 case MVT::v4f32: NewVT = MVT::v2f64; break;
5088 case MVT::v4i32: NewVT = MVT::v2i64; break;
5089 case MVT::v8i16: NewVT = MVT::v4i32; break;
5090 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005091 }
5092
Nate Begeman9008ca62009-04-27 18:41:29 +00005093 int Scale = NumElems / NewWidth;
5094 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005095 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005096 int StartIdx = -1;
5097 for (int j = 0; j < Scale; ++j) {
5098 int EltIdx = SVOp->getMaskElt(i+j);
5099 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005100 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005101 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005102 StartIdx = EltIdx - (EltIdx % Scale);
5103 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005104 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005105 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005106 if (StartIdx == -1)
5107 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005108 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005109 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005110 }
5111
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005112 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5113 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005114 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005115}
5116
Evan Chengd880b972008-05-09 21:53:03 +00005117/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005118///
Owen Andersone50ed302009-08-10 22:56:29 +00005119static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005120 SDValue SrcOp, SelectionDAG &DAG,
5121 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005122 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005123 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005124 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005125 LD = dyn_cast<LoadSDNode>(SrcOp);
5126 if (!LD) {
5127 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5128 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005129 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005130 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005131 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005132 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005133 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005134 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005135 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005136 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005137 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5138 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5139 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005140 SrcOp.getOperand(0)
5141 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005142 }
5143 }
5144 }
5145
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005146 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005147 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005148 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005149 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005150}
5151
Evan Chengace3c172008-07-22 21:13:36 +00005152/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5153/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005154static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005155LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5156 SDValue V1 = SVOp->getOperand(0);
5157 SDValue V2 = SVOp->getOperand(1);
5158 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005159 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005160
Evan Chengace3c172008-07-22 21:13:36 +00005161 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005162 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005163 SmallVector<int, 8> Mask1(4U, -1);
5164 SmallVector<int, 8> PermMask;
5165 SVOp->getMask(PermMask);
5166
Evan Chengace3c172008-07-22 21:13:36 +00005167 unsigned NumHi = 0;
5168 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005169 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005170 int Idx = PermMask[i];
5171 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005172 Locs[i] = std::make_pair(-1, -1);
5173 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005174 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5175 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005176 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005177 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005178 NumLo++;
5179 } else {
5180 Locs[i] = std::make_pair(1, NumHi);
5181 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005182 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005183 NumHi++;
5184 }
5185 }
5186 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005187
Evan Chengace3c172008-07-22 21:13:36 +00005188 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005189 // If no more than two elements come from either vector. This can be
5190 // implemented with two shuffles. First shuffle gather the elements.
5191 // The second shuffle, which takes the first shuffle as both of its
5192 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005193 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005194
Nate Begeman9008ca62009-04-27 18:41:29 +00005195 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005196
Evan Chengace3c172008-07-22 21:13:36 +00005197 for (unsigned i = 0; i != 4; ++i) {
5198 if (Locs[i].first == -1)
5199 continue;
5200 else {
5201 unsigned Idx = (i < 2) ? 0 : 4;
5202 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005203 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005204 }
5205 }
5206
Nate Begeman9008ca62009-04-27 18:41:29 +00005207 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005208 } else if (NumLo == 3 || NumHi == 3) {
5209 // Otherwise, we must have three elements from one vector, call it X, and
5210 // one element from the other, call it Y. First, use a shufps to build an
5211 // intermediate vector with the one element from Y and the element from X
5212 // that will be in the same half in the final destination (the indexes don't
5213 // matter). Then, use a shufps to build the final vector, taking the half
5214 // containing the element from Y from the intermediate, and the other half
5215 // from X.
5216 if (NumHi == 3) {
5217 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005218 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005219 std::swap(V1, V2);
5220 }
5221
5222 // Find the element from V2.
5223 unsigned HiIndex;
5224 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005225 int Val = PermMask[HiIndex];
5226 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005227 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005228 if (Val >= 4)
5229 break;
5230 }
5231
Nate Begeman9008ca62009-04-27 18:41:29 +00005232 Mask1[0] = PermMask[HiIndex];
5233 Mask1[1] = -1;
5234 Mask1[2] = PermMask[HiIndex^1];
5235 Mask1[3] = -1;
5236 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005237
5238 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005239 Mask1[0] = PermMask[0];
5240 Mask1[1] = PermMask[1];
5241 Mask1[2] = HiIndex & 1 ? 6 : 4;
5242 Mask1[3] = HiIndex & 1 ? 4 : 6;
5243 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005244 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005245 Mask1[0] = HiIndex & 1 ? 2 : 0;
5246 Mask1[1] = HiIndex & 1 ? 0 : 2;
5247 Mask1[2] = PermMask[2];
5248 Mask1[3] = PermMask[3];
5249 if (Mask1[2] >= 0)
5250 Mask1[2] += 4;
5251 if (Mask1[3] >= 0)
5252 Mask1[3] += 4;
5253 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005254 }
Evan Chengace3c172008-07-22 21:13:36 +00005255 }
5256
5257 // Break it into (shuffle shuffle_hi, shuffle_lo).
5258 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005259 SmallVector<int,8> LoMask(4U, -1);
5260 SmallVector<int,8> HiMask(4U, -1);
5261
5262 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005263 unsigned MaskIdx = 0;
5264 unsigned LoIdx = 0;
5265 unsigned HiIdx = 2;
5266 for (unsigned i = 0; i != 4; ++i) {
5267 if (i == 2) {
5268 MaskPtr = &HiMask;
5269 MaskIdx = 1;
5270 LoIdx = 0;
5271 HiIdx = 2;
5272 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005273 int Idx = PermMask[i];
5274 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005275 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005276 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005277 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005278 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005279 LoIdx++;
5280 } else {
5281 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005282 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005283 HiIdx++;
5284 }
5285 }
5286
Nate Begeman9008ca62009-04-27 18:41:29 +00005287 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5288 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5289 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005290 for (unsigned i = 0; i != 4; ++i) {
5291 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005292 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005293 } else {
5294 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005295 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005296 }
5297 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005299}
5300
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005301static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005302 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005303 V = V.getOperand(0);
5304 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5305 V = V.getOperand(0);
5306 if (MayFoldLoad(V))
5307 return true;
5308 return false;
5309}
5310
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005311// FIXME: the version above should always be used. Since there's
5312// a bug where several vector shuffles can't be folded because the
5313// DAG is not updated during lowering and a node claims to have two
5314// uses while it only has one, use this version, and let isel match
5315// another instruction if the load really happens to have more than
5316// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005317// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005318static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005319 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005320 V = V.getOperand(0);
5321 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5322 V = V.getOperand(0);
5323 if (ISD::isNormalLoad(V.getNode()))
5324 return true;
5325 return false;
5326}
5327
5328/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5329/// a vector extract, and if both can be later optimized into a single load.
5330/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5331/// here because otherwise a target specific shuffle node is going to be
5332/// emitted for this shuffle, and the optimization not done.
5333/// FIXME: This is probably not the best approach, but fix the problem
5334/// until the right path is decided.
5335static
5336bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5337 const TargetLowering &TLI) {
5338 EVT VT = V.getValueType();
5339 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5340
5341 // Be sure that the vector shuffle is present in a pattern like this:
5342 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5343 if (!V.hasOneUse())
5344 return false;
5345
5346 SDNode *N = *V.getNode()->use_begin();
5347 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5348 return false;
5349
5350 SDValue EltNo = N->getOperand(1);
5351 if (!isa<ConstantSDNode>(EltNo))
5352 return false;
5353
5354 // If the bit convert changed the number of elements, it is unsafe
5355 // to examine the mask.
5356 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005357 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005358 EVT SrcVT = V.getOperand(0).getValueType();
5359 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5360 return false;
5361 V = V.getOperand(0);
5362 HasShuffleIntoBitcast = true;
5363 }
5364
5365 // Select the input vector, guarding against out of range extract vector.
5366 unsigned NumElems = VT.getVectorNumElements();
5367 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5368 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5369 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5370
5371 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005372 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005373 V = V.getOperand(0);
5374
5375 if (ISD::isNormalLoad(V.getNode())) {
5376 // Is the original load suitable?
5377 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5378
5379 // FIXME: avoid the multi-use bug that is preventing lots of
5380 // of foldings to be detected, this is still wrong of course, but
5381 // give the temporary desired behavior, and if it happens that
5382 // the load has real more uses, during isel it will not fold, and
5383 // will generate poor code.
5384 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5385 return false;
5386
5387 if (!HasShuffleIntoBitcast)
5388 return true;
5389
5390 // If there's a bitcast before the shuffle, check if the load type and
5391 // alignment is valid.
5392 unsigned Align = LN0->getAlignment();
5393 unsigned NewAlign =
5394 TLI.getTargetData()->getABITypeAlignment(
5395 VT.getTypeForEVT(*DAG.getContext()));
5396
5397 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5398 return false;
5399 }
5400
5401 return true;
5402}
5403
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005404static
Evan Cheng835580f2010-10-07 20:50:20 +00005405SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5406 EVT VT = Op.getValueType();
5407
5408 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005409 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5410 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005411 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5412 V1, DAG));
5413}
5414
5415static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005416SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5417 bool HasSSE2) {
5418 SDValue V1 = Op.getOperand(0);
5419 SDValue V2 = Op.getOperand(1);
5420 EVT VT = Op.getValueType();
5421
5422 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5423
5424 if (HasSSE2 && VT == MVT::v2f64)
5425 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5426
5427 // v4f32 or v4i32
5428 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5429}
5430
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005431static
5432SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5433 SDValue V1 = Op.getOperand(0);
5434 SDValue V2 = Op.getOperand(1);
5435 EVT VT = Op.getValueType();
5436
5437 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5438 "unsupported shuffle type");
5439
5440 if (V2.getOpcode() == ISD::UNDEF)
5441 V2 = V1;
5442
5443 // v4i32 or v4f32
5444 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5445}
5446
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005447static
5448SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5449 SDValue V1 = Op.getOperand(0);
5450 SDValue V2 = Op.getOperand(1);
5451 EVT VT = Op.getValueType();
5452 unsigned NumElems = VT.getVectorNumElements();
5453
5454 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5455 // operand of these instructions is only memory, so check if there's a
5456 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5457 // same masks.
5458 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005459
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005460 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005461 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005462 CanFoldLoad = true;
5463
5464 // When V1 is a load, it can be folded later into a store in isel, example:
5465 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5466 // turns into:
5467 // (MOVLPSmr addr:$src1, VR128:$src2)
5468 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005469 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005470 CanFoldLoad = true;
5471
5472 if (CanFoldLoad) {
5473 if (HasSSE2 && NumElems == 2)
5474 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5475
5476 if (NumElems == 4)
5477 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5478 }
5479
5480 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5481 // movl and movlp will both match v2i64, but v2i64 is never matched by
5482 // movl earlier because we make it strict to avoid messing with the movlp load
5483 // folding logic (see the code above getMOVLP call). Match it here then,
5484 // this is horrible, but will stay like this until we move all shuffle
5485 // matching to x86 specific nodes. Note that for the 1st condition all
5486 // types are matched with movsd.
5487 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5488 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5489 else if (HasSSE2)
5490 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5491
5492
5493 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5494
5495 // Invert the operand order and use SHUFPS to match it.
5496 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5497 X86::getShuffleSHUFImmediate(SVOp), DAG);
5498}
5499
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005500static inline unsigned getUNPCKLOpcode(EVT VT) {
5501 switch(VT.getSimpleVT().SimpleTy) {
5502 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5503 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5504 case MVT::v4f32: return X86ISD::UNPCKLPS;
5505 case MVT::v2f64: return X86ISD::UNPCKLPD;
5506 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5507 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5508 default:
5509 llvm_unreachable("Unknow type for unpckl");
5510 }
5511 return 0;
5512}
5513
5514static inline unsigned getUNPCKHOpcode(EVT VT) {
5515 switch(VT.getSimpleVT().SimpleTy) {
5516 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5517 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5518 case MVT::v4f32: return X86ISD::UNPCKHPS;
5519 case MVT::v2f64: return X86ISD::UNPCKHPD;
5520 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5521 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5522 default:
5523 llvm_unreachable("Unknow type for unpckh");
5524 }
5525 return 0;
5526}
5527
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005528static
5529SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005530 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005531 const X86Subtarget *Subtarget) {
5532 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5533 EVT VT = Op.getValueType();
5534 DebugLoc dl = Op.getDebugLoc();
5535 SDValue V1 = Op.getOperand(0);
5536 SDValue V2 = Op.getOperand(1);
5537
5538 if (isZeroShuffle(SVOp))
5539 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5540
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005541 // Handle splat operations
5542 if (SVOp->isSplat()) {
5543 // Special case, this is the only place now where it's
5544 // allowed to return a vector_shuffle operation without
5545 // using a target specific node, because *hopefully* it
5546 // will be optimized away by the dag combiner.
5547 if (VT.getVectorNumElements() <= 4 &&
5548 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5549 return Op;
5550
5551 // Handle splats by matching through known masks
5552 if (VT.getVectorNumElements() <= 4)
5553 return SDValue();
5554
Evan Cheng835580f2010-10-07 20:50:20 +00005555 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005556 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005557 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005558
5559 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5560 // do it!
5561 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5562 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5563 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005564 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005565 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5566 // FIXME: Figure out a cleaner way to do this.
5567 // Try to make use of movq to zero out the top part.
5568 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5569 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5570 if (NewOp.getNode()) {
5571 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5572 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5573 DAG, Subtarget, dl);
5574 }
5575 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5576 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5577 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5578 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5579 DAG, Subtarget, dl);
5580 }
5581 }
5582 return SDValue();
5583}
5584
Dan Gohman475871a2008-07-27 21:46:04 +00005585SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005586X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005587 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005588 SDValue V1 = Op.getOperand(0);
5589 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005590 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005591 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005593 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005594 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5595 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005596 bool V1IsSplat = false;
5597 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005598 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005599 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005600 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005601 MachineFunction &MF = DAG.getMachineFunction();
5602 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005603
Dale Johannesen0488fb62010-09-30 23:57:10 +00005604 // Shuffle operations on MMX not supported.
5605 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005606 return Op;
5607
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005608 // Vector shuffle lowering takes 3 steps:
5609 //
5610 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5611 // narrowing and commutation of operands should be handled.
5612 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5613 // shuffle nodes.
5614 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5615 // so the shuffle can be broken into other shuffles and the legalizer can
5616 // try the lowering again.
5617 //
5618 // The general ideia is that no vector_shuffle operation should be left to
5619 // be matched during isel, all of them must be converted to a target specific
5620 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005621
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005622 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5623 // narrowing and commutation of operands should be handled. The actual code
5624 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005625 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005626 if (NewOp.getNode())
5627 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005628
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005629 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5630 // unpckh_undef). Only use pshufd if speed is more important than size.
5631 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5632 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5633 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5634 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5635 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5636 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005637
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005638 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005639 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005640 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005641
Dale Johannesen0488fb62010-09-30 23:57:10 +00005642 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005643 return getMOVHighToLow(Op, dl, DAG);
5644
5645 // Use to match splats
5646 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5647 (VT == MVT::v2f64 || VT == MVT::v2i64))
5648 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5649
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005650 if (X86::isPSHUFDMask(SVOp)) {
5651 // The actual implementation will match the mask in the if above and then
5652 // during isel it can match several different instructions, not only pshufd
5653 // as its name says, sad but true, emulate the behavior for now...
5654 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5655 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5656
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005657 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5658
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005659 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005660 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5661
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005662 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005663 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5664 TargetMask, DAG);
5665
5666 if (VT == MVT::v4f32)
5667 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5668 TargetMask, DAG);
5669 }
Eric Christopherfd179292009-08-27 18:07:15 +00005670
Evan Chengf26ffe92008-05-29 08:22:04 +00005671 // Check if this can be converted into a logical shift.
5672 bool isLeft = false;
5673 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005674 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005675 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005676 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005677 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005678 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005679 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005680 EVT EltVT = VT.getVectorElementType();
5681 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005682 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005683 }
Eric Christopherfd179292009-08-27 18:07:15 +00005684
Nate Begeman9008ca62009-04-27 18:41:29 +00005685 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005686 if (V1IsUndef)
5687 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005688 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005689 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005690 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005691 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005692 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5693
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005694 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005695 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5696 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005697 }
Eric Christopherfd179292009-08-27 18:07:15 +00005698
Nate Begeman9008ca62009-04-27 18:41:29 +00005699 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005700 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5701 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005702
Dale Johannesen0488fb62010-09-30 23:57:10 +00005703 if (X86::isMOVHLPSMask(SVOp))
5704 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005705
Dale Johannesen0488fb62010-09-30 23:57:10 +00005706 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5707 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005708
Dale Johannesen0488fb62010-09-30 23:57:10 +00005709 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5710 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005711
Dale Johannesen0488fb62010-09-30 23:57:10 +00005712 if (X86::isMOVLPMask(SVOp))
5713 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005714
Nate Begeman9008ca62009-04-27 18:41:29 +00005715 if (ShouldXformToMOVHLPS(SVOp) ||
5716 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5717 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005718
Evan Chengf26ffe92008-05-29 08:22:04 +00005719 if (isShift) {
5720 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005721 EVT EltVT = VT.getVectorElementType();
5722 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005723 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005724 }
Eric Christopherfd179292009-08-27 18:07:15 +00005725
Evan Cheng9eca5e82006-10-25 21:49:50 +00005726 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005727 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5728 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005729 V1IsSplat = isSplatVector(V1.getNode());
5730 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005731
Chris Lattner8a594482007-11-25 00:24:49 +00005732 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005733 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005734 Op = CommuteVectorShuffle(SVOp, DAG);
5735 SVOp = cast<ShuffleVectorSDNode>(Op);
5736 V1 = SVOp->getOperand(0);
5737 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005738 std::swap(V1IsSplat, V2IsSplat);
5739 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005740 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005741 }
5742
Nate Begeman9008ca62009-04-27 18:41:29 +00005743 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5744 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005745 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005746 return V1;
5747 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5748 // the instruction selector will not match, so get a canonical MOVL with
5749 // swapped operands to undo the commute.
5750 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005751 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005752
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005753 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005754 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005755
5756 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005757 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005758
Evan Cheng9bbbb982006-10-25 20:48:19 +00005759 if (V2IsSplat) {
5760 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005761 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005762 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005763 SDValue NewMask = NormalizeMask(SVOp, DAG);
5764 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5765 if (NSVOp != SVOp) {
5766 if (X86::isUNPCKLMask(NSVOp, true)) {
5767 return NewMask;
5768 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5769 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005770 }
5771 }
5772 }
5773
Evan Cheng9eca5e82006-10-25 21:49:50 +00005774 if (Commuted) {
5775 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005776 // FIXME: this seems wrong.
5777 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5778 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005779
5780 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005781 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005782
5783 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005784 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005785 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005786
Nate Begeman9008ca62009-04-27 18:41:29 +00005787 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005788 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005789 return CommuteVectorShuffle(SVOp, DAG);
5790
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005791 // The checks below are all present in isShuffleMaskLegal, but they are
5792 // inlined here right now to enable us to directly emit target specific
5793 // nodes, and remove one by one until they don't return Op anymore.
5794 SmallVector<int, 16> M;
5795 SVOp->getMask(M);
5796
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005797 if (isPALIGNRMask(M, VT, HasSSSE3))
5798 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5799 X86::getShufflePALIGNRImmediate(SVOp),
5800 DAG);
5801
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005802 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5803 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5804 if (VT == MVT::v2f64)
5805 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5806 if (VT == MVT::v2i64)
5807 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5808 }
5809
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005810 if (isPSHUFHWMask(M, VT))
5811 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5812 X86::getShufflePSHUFHWImmediate(SVOp),
5813 DAG);
5814
5815 if (isPSHUFLWMask(M, VT))
5816 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5817 X86::getShufflePSHUFLWImmediate(SVOp),
5818 DAG);
5819
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005820 if (isSHUFPMask(M, VT)) {
5821 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5822 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5823 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5824 TargetMask, DAG);
5825 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5826 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5827 TargetMask, DAG);
5828 }
5829
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005830 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5831 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5832 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5833 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5834 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5835 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5836
Evan Cheng14b32e12007-12-11 01:46:18 +00005837 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005838 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005839 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005840 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005841 return NewOp;
5842 }
5843
Owen Anderson825b72b2009-08-11 20:47:22 +00005844 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005845 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 if (NewOp.getNode())
5847 return NewOp;
5848 }
Eric Christopherfd179292009-08-27 18:07:15 +00005849
Dale Johannesen0488fb62010-09-30 23:57:10 +00005850 // Handle all 4 wide cases with a number of shuffles.
5851 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005852 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005853
Dan Gohman475871a2008-07-27 21:46:04 +00005854 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005855}
5856
Dan Gohman475871a2008-07-27 21:46:04 +00005857SDValue
5858X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005859 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005860 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005861 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005862 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005864 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005865 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005866 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005867 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005868 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005869 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5870 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5871 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005872 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5873 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005874 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005875 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005876 Op.getOperand(0)),
5877 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005878 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, 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);
Owen Anderson825b72b2009-08-11 20:47:22 +00005883 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005884 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5885 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005886 // result has a single use which is a store or a bitcast to i32. And in
5887 // the case of a store, it's not worth it if the index is a constant 0,
5888 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005889 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005890 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005891 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005892 if ((User->getOpcode() != ISD::STORE ||
5893 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5894 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005895 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005896 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005897 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005899 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005900 Op.getOperand(0)),
5901 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005902 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005903 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005904 // ExtractPS works with constant index.
5905 if (isa<ConstantSDNode>(Op.getOperand(1)))
5906 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005907 }
Dan Gohman475871a2008-07-27 21:46:04 +00005908 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005909}
5910
5911
Dan Gohman475871a2008-07-27 21:46:04 +00005912SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005913X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5914 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005915 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005916 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005917
Evan Cheng62a3f152008-03-24 21:52:23 +00005918 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005919 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005920 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005921 return Res;
5922 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005923
Owen Andersone50ed302009-08-10 22:56:29 +00005924 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005925 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005926 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005927 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005928 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005929 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005930 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5932 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005933 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005934 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005935 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005936 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005937 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005938 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005939 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005940 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005941 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005942 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005943 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005944 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005945 if (Idx == 0)
5946 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005947
Evan Cheng0db9fe62006-04-25 20:13:52 +00005948 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005949 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005950 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005951 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005952 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005953 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005954 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005955 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005956 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5957 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5958 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005959 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005960 if (Idx == 0)
5961 return Op;
5962
5963 // UNPCKHPD the element to the lowest double word, then movsd.
5964 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5965 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005966 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005967 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005968 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005969 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005970 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005971 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005972 }
5973
Dan Gohman475871a2008-07-27 21:46:04 +00005974 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005975}
5976
Dan Gohman475871a2008-07-27 21:46:04 +00005977SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005978X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5979 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005980 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005981 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005982 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005983
Dan Gohman475871a2008-07-27 21:46:04 +00005984 SDValue N0 = Op.getOperand(0);
5985 SDValue N1 = Op.getOperand(1);
5986 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005987
Dan Gohman8a55ce42009-09-23 21:02:20 +00005988 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005989 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005990 unsigned Opc;
5991 if (VT == MVT::v8i16)
5992 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005993 else if (VT == MVT::v16i8)
5994 Opc = X86ISD::PINSRB;
5995 else
5996 Opc = X86ISD::PINSRB;
5997
Nate Begeman14d12ca2008-02-11 04:19:36 +00005998 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5999 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006000 if (N1.getValueType() != MVT::i32)
6001 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6002 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006003 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006004 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006005 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006006 // Bits [7:6] of the constant are the source select. This will always be
6007 // zero here. The DAG Combiner may combine an extract_elt index into these
6008 // bits. For example (insert (extract, 3), 2) could be matched by putting
6009 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006010 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006011 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006012 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006013 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006014 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006015 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006016 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006017 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006018 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006019 // PINSR* works with constant index.
6020 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006021 }
Dan Gohman475871a2008-07-27 21:46:04 +00006022 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006023}
6024
Dan Gohman475871a2008-07-27 21:46:04 +00006025SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006026X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, 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();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006029
6030 if (Subtarget->hasSSE41())
6031 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6032
Dan Gohman8a55ce42009-09-23 21:02:20 +00006033 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006034 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006035
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006036 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006037 SDValue N0 = Op.getOperand(0);
6038 SDValue N1 = Op.getOperand(1);
6039 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00006040
Dan Gohman8a55ce42009-09-23 21:02:20 +00006041 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006042 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6043 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006044 if (N1.getValueType() != MVT::i32)
6045 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6046 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006047 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006048 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006049 }
Dan Gohman475871a2008-07-27 21:46:04 +00006050 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006051}
6052
Dan Gohman475871a2008-07-27 21:46:04 +00006053SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006054X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006055 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006056
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006057 if (Op.getValueType() == MVT::v1i64 &&
6058 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006059 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006060
Owen Anderson825b72b2009-08-11 20:47:22 +00006061 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006062 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6063 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006064 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006065 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006066}
6067
David Greene91585092011-01-26 15:38:49 +00006068// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6069// a simple subregister reference or explicit instructions to grab
6070// upper bits of a vector.
6071SDValue
6072X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6073 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006074 DebugLoc dl = Op.getNode()->getDebugLoc();
6075 SDValue Vec = Op.getNode()->getOperand(0);
6076 SDValue Idx = Op.getNode()->getOperand(1);
6077
6078 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6079 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6080 return Extract128BitVector(Vec, Idx, DAG, dl);
6081 }
David Greene91585092011-01-26 15:38:49 +00006082 }
6083 return SDValue();
6084}
6085
David Greenecfe33c42011-01-26 19:13:22 +00006086// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6087// simple superregister reference or explicit instructions to insert
6088// the upper bits of a vector.
6089SDValue
6090X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6091 if (Subtarget->hasAVX()) {
6092 DebugLoc dl = Op.getNode()->getDebugLoc();
6093 SDValue Vec = Op.getNode()->getOperand(0);
6094 SDValue SubVec = Op.getNode()->getOperand(1);
6095 SDValue Idx = Op.getNode()->getOperand(2);
6096
6097 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6098 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006099 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006100 }
6101 }
6102 return SDValue();
6103}
6104
Bill Wendling056292f2008-09-16 21:48:12 +00006105// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6106// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6107// one of the above mentioned nodes. It has to be wrapped because otherwise
6108// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6109// be used to form addressing mode. These wrapped nodes will be selected
6110// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006111SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006112X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006113 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006114
Chris Lattner41621a22009-06-26 19:22:52 +00006115 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6116 // global base reg.
6117 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006118 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006119 CodeModel::Model M = getTargetMachine().getCodeModel();
6120
Chris Lattner4f066492009-07-11 20:29:19 +00006121 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006122 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006123 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006124 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006125 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006126 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006127 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006128
Evan Cheng1606e8e2009-03-13 07:51:59 +00006129 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006130 CP->getAlignment(),
6131 CP->getOffset(), OpFlag);
6132 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006133 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006134 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006135 if (OpFlag) {
6136 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006137 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006138 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006139 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006140 }
6141
6142 return Result;
6143}
6144
Dan Gohmand858e902010-04-17 15:26:15 +00006145SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006146 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006147
Chris Lattner18c59872009-06-27 04:16:01 +00006148 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6149 // global base reg.
6150 unsigned char OpFlag = 0;
6151 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006152 CodeModel::Model M = getTargetMachine().getCodeModel();
6153
Chris Lattner4f066492009-07-11 20:29:19 +00006154 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006155 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006156 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006157 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006158 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006159 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006160 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006161
Chris Lattner18c59872009-06-27 04:16:01 +00006162 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6163 OpFlag);
6164 DebugLoc DL = JT->getDebugLoc();
6165 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006166
Chris Lattner18c59872009-06-27 04:16:01 +00006167 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006168 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006169 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6170 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006171 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006172 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006173
Chris Lattner18c59872009-06-27 04:16:01 +00006174 return Result;
6175}
6176
6177SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006178X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006179 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006180
Chris Lattner18c59872009-06-27 04:16:01 +00006181 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6182 // global base reg.
6183 unsigned char OpFlag = 0;
6184 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006185 CodeModel::Model M = getTargetMachine().getCodeModel();
6186
Chris Lattner4f066492009-07-11 20:29:19 +00006187 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006188 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006189 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006190 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006191 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006192 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006193 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006194
Chris Lattner18c59872009-06-27 04:16:01 +00006195 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006196
Chris Lattner18c59872009-06-27 04:16:01 +00006197 DebugLoc DL = Op.getDebugLoc();
6198 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006199
6200
Chris Lattner18c59872009-06-27 04:16:01 +00006201 // With PIC, the address is actually $g + Offset.
6202 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006203 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006204 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6205 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006206 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006207 Result);
6208 }
Eric Christopherfd179292009-08-27 18:07:15 +00006209
Chris Lattner18c59872009-06-27 04:16:01 +00006210 return Result;
6211}
6212
Dan Gohman475871a2008-07-27 21:46:04 +00006213SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006214X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006215 // Create the TargetBlockAddressAddress node.
6216 unsigned char OpFlags =
6217 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006218 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006219 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006220 DebugLoc dl = Op.getDebugLoc();
6221 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6222 /*isTarget=*/true, OpFlags);
6223
Dan Gohmanf705adb2009-10-30 01:28:02 +00006224 if (Subtarget->isPICStyleRIPRel() &&
6225 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006226 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6227 else
6228 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006229
Dan Gohman29cbade2009-11-20 23:18:13 +00006230 // With PIC, the address is actually $g + Offset.
6231 if (isGlobalRelativeToPICBase(OpFlags)) {
6232 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6233 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6234 Result);
6235 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006236
6237 return Result;
6238}
6239
6240SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006241X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006242 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006243 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006244 // Create the TargetGlobalAddress node, folding in the constant
6245 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006246 unsigned char OpFlags =
6247 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006248 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006249 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006250 if (OpFlags == X86II::MO_NO_FLAG &&
6251 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006252 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006253 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006254 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006255 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006256 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006257 }
Eric Christopherfd179292009-08-27 18:07:15 +00006258
Chris Lattner4f066492009-07-11 20:29:19 +00006259 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006260 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006261 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6262 else
6263 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006264
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006265 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006266 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006267 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6268 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006269 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006270 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006271
Chris Lattner36c25012009-07-10 07:34:39 +00006272 // For globals that require a load from a stub to get the address, emit the
6273 // load.
6274 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006275 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006276 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006277
Dan Gohman6520e202008-10-18 02:06:02 +00006278 // If there was a non-zero offset that we didn't fold, create an explicit
6279 // addition for it.
6280 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006281 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006282 DAG.getConstant(Offset, getPointerTy()));
6283
Evan Cheng0db9fe62006-04-25 20:13:52 +00006284 return Result;
6285}
6286
Evan Chengda43bcf2008-09-24 00:05:32 +00006287SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006288X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006289 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006290 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006291 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006292}
6293
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006294static SDValue
6295GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006296 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006297 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006298 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006299 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006300 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006301 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006302 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006303 GA->getOffset(),
6304 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006305 if (InFlag) {
6306 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006307 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006308 } else {
6309 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006310 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006311 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006312
6313 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006314 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006315
Rafael Espindola15f1b662009-04-24 12:59:40 +00006316 SDValue Flag = Chain.getValue(1);
6317 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006318}
6319
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006320// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006321static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006322LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006323 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006324 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006325 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6326 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006327 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006328 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006329 InFlag = Chain.getValue(1);
6330
Chris Lattnerb903bed2009-06-26 21:20:29 +00006331 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006332}
6333
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006334// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006335static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006336LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006337 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006338 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6339 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006340}
6341
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006342// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6343// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006344static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006345 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006346 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006347 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006348
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006349 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6350 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6351 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006352
Michael J. Spencerec38de22010-10-10 22:04:20 +00006353 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006354 DAG.getIntPtrConstant(0),
6355 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006356
Chris Lattnerb903bed2009-06-26 21:20:29 +00006357 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006358 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6359 // initialexec.
6360 unsigned WrapperKind = X86ISD::Wrapper;
6361 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006362 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006363 } else if (is64Bit) {
6364 assert(model == TLSModel::InitialExec);
6365 OperandFlags = X86II::MO_GOTTPOFF;
6366 WrapperKind = X86ISD::WrapperRIP;
6367 } else {
6368 assert(model == TLSModel::InitialExec);
6369 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006370 }
Eric Christopherfd179292009-08-27 18:07:15 +00006371
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006372 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6373 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006374 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006375 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006376 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006377 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006378
Rafael Espindola9a580232009-02-27 13:37:18 +00006379 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006380 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006381 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006382
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006383 // The address of the thread local variable is the add of the thread
6384 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006385 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006386}
6387
Dan Gohman475871a2008-07-27 21:46:04 +00006388SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006389X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006390
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006391 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006392 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006393
Eric Christopher30ef0e52010-06-03 04:07:48 +00006394 if (Subtarget->isTargetELF()) {
6395 // TODO: implement the "local dynamic" model
6396 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006397
Eric Christopher30ef0e52010-06-03 04:07:48 +00006398 // If GV is an alias then use the aliasee for determining
6399 // thread-localness.
6400 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6401 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006402
6403 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006404 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006405
Eric Christopher30ef0e52010-06-03 04:07:48 +00006406 switch (model) {
6407 case TLSModel::GeneralDynamic:
6408 case TLSModel::LocalDynamic: // not implemented
6409 if (Subtarget->is64Bit())
6410 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6411 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006412
Eric Christopher30ef0e52010-06-03 04:07:48 +00006413 case TLSModel::InitialExec:
6414 case TLSModel::LocalExec:
6415 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6416 Subtarget->is64Bit());
6417 }
6418 } else if (Subtarget->isTargetDarwin()) {
6419 // Darwin only has one model of TLS. Lower to that.
6420 unsigned char OpFlag = 0;
6421 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6422 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006423
Eric Christopher30ef0e52010-06-03 04:07:48 +00006424 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6425 // global base reg.
6426 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6427 !Subtarget->is64Bit();
6428 if (PIC32)
6429 OpFlag = X86II::MO_TLVP_PIC_BASE;
6430 else
6431 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006432 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006433 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006434 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006435 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006436 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006437
Eric Christopher30ef0e52010-06-03 04:07:48 +00006438 // With PIC32, the address is actually $g + Offset.
6439 if (PIC32)
6440 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6441 DAG.getNode(X86ISD::GlobalBaseReg,
6442 DebugLoc(), getPointerTy()),
6443 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006444
Eric Christopher30ef0e52010-06-03 04:07:48 +00006445 // Lowering the machine isd will make sure everything is in the right
6446 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006447 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006448 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006449 SDValue Args[] = { Chain, Offset };
6450 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006451
Eric Christopher30ef0e52010-06-03 04:07:48 +00006452 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6453 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6454 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006455
Eric Christopher30ef0e52010-06-03 04:07:48 +00006456 // And our return value (tls address) is in the standard call return value
6457 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006458 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6459 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006460 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006461
Eric Christopher30ef0e52010-06-03 04:07:48 +00006462 assert(false &&
6463 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006464
Torok Edwinc23197a2009-07-14 16:55:14 +00006465 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006466 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006467}
6468
Evan Cheng0db9fe62006-04-25 20:13:52 +00006469
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006470/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006471/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006472SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006473 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006474 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006475 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006476 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006477 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006478 SDValue ShOpLo = Op.getOperand(0);
6479 SDValue ShOpHi = Op.getOperand(1);
6480 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006481 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006482 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006483 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006484
Dan Gohman475871a2008-07-27 21:46:04 +00006485 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006486 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006487 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6488 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006489 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006490 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6491 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006492 }
Evan Chenge3413162006-01-09 18:33:28 +00006493
Owen Anderson825b72b2009-08-11 20:47:22 +00006494 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6495 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006496 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006497 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006498
Dan Gohman475871a2008-07-27 21:46:04 +00006499 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006500 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006501 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6502 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006503
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006504 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006505 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6506 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006507 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006508 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6509 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006510 }
6511
Dan Gohman475871a2008-07-27 21:46:04 +00006512 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006513 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006514}
Evan Chenga3195e82006-01-12 22:54:21 +00006515
Dan Gohmand858e902010-04-17 15:26:15 +00006516SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6517 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006518 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006519
Dale Johannesen0488fb62010-09-30 23:57:10 +00006520 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006521 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006522
Owen Anderson825b72b2009-08-11 20:47:22 +00006523 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006524 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006525
Eli Friedman36df4992009-05-27 00:47:34 +00006526 // These are really Legal; return the operand so the caller accepts it as
6527 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006528 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006529 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006530 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006531 Subtarget->is64Bit()) {
6532 return Op;
6533 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006534
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006535 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006536 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006537 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006538 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006539 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006540 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006541 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006542 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006543 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006544 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6545}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006546
Owen Andersone50ed302009-08-10 22:56:29 +00006547SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006548 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006549 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006550 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006551 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006552 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006553 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006554 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006555 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006556 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006557 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006558
Chris Lattner492a43e2010-09-22 01:28:21 +00006559 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006560
Chris Lattner492a43e2010-09-22 01:28:21 +00006561 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6562 MachineMemOperand *MMO =
6563 DAG.getMachineFunction()
6564 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6565 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006566
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006567 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006568 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6569 X86ISD::FILD, DL,
6570 Tys, Ops, array_lengthof(Ops),
6571 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006572
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006573 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006574 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006575 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006576
6577 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6578 // shouldn't be necessary except that RFP cannot be live across
6579 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006580 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006581 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6582 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006583 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006584 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006585 SDValue Ops[] = {
6586 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6587 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006588 MachineMemOperand *MMO =
6589 DAG.getMachineFunction()
6590 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006591 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006592
Chris Lattner492a43e2010-09-22 01:28:21 +00006593 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6594 Ops, array_lengthof(Ops),
6595 Op.getValueType(), MMO);
6596 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006597 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006598 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006599 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006600
Evan Cheng0db9fe62006-04-25 20:13:52 +00006601 return Result;
6602}
6603
Bill Wendling8b8a6362009-01-17 03:56:04 +00006604// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006605SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6606 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006607 // This algorithm is not obvious. Here it is in C code, more or less:
6608 /*
6609 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6610 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6611 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006612
Bill Wendling8b8a6362009-01-17 03:56:04 +00006613 // Copy ints to xmm registers.
6614 __m128i xh = _mm_cvtsi32_si128( hi );
6615 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006616
Bill Wendling8b8a6362009-01-17 03:56:04 +00006617 // Combine into low half of a single xmm register.
6618 __m128i x = _mm_unpacklo_epi32( xh, xl );
6619 __m128d d;
6620 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006621
Bill Wendling8b8a6362009-01-17 03:56:04 +00006622 // Merge in appropriate exponents to give the integer bits the right
6623 // magnitude.
6624 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006625
Bill Wendling8b8a6362009-01-17 03:56:04 +00006626 // Subtract away the biases to deal with the IEEE-754 double precision
6627 // implicit 1.
6628 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006629
Bill Wendling8b8a6362009-01-17 03:56:04 +00006630 // All conversions up to here are exact. The correctly rounded result is
6631 // calculated using the current rounding mode using the following
6632 // horizontal add.
6633 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6634 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6635 // store doesn't really need to be here (except
6636 // maybe to zero the other double)
6637 return sd;
6638 }
6639 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006640
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006641 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006642 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006643
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006644 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006645 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006646 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6647 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6648 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6649 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006650 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006651 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006652
Bill Wendling8b8a6362009-01-17 03:56:04 +00006653 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006654 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006655 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006656 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006657 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006658 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006659 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006660
Owen Anderson825b72b2009-08-11 20:47:22 +00006661 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6662 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006663 Op.getOperand(0),
6664 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006665 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6666 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006667 Op.getOperand(0),
6668 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006669 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6670 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006671 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006672 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006673 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006674 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006675 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006676 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006677 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006678 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006679
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006680 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006681 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006682 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6683 DAG.getUNDEF(MVT::v2f64), ShufMask);
6684 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6685 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006686 DAG.getIntPtrConstant(0));
6687}
6688
Bill Wendling8b8a6362009-01-17 03:56:04 +00006689// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006690SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6691 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006692 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006693 // FP constant to bias correct the final result.
6694 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006695 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006696
6697 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006698 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6699 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006700 Op.getOperand(0),
6701 DAG.getIntPtrConstant(0)));
6702
Owen Anderson825b72b2009-08-11 20:47:22 +00006703 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006704 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006705 DAG.getIntPtrConstant(0));
6706
6707 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006708 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006709 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006710 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006711 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006712 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006713 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006714 MVT::v2f64, Bias)));
6715 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006716 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006717 DAG.getIntPtrConstant(0));
6718
6719 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006720 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006721
6722 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006723 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006724
Owen Anderson825b72b2009-08-11 20:47:22 +00006725 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006726 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006727 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006728 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006729 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006730 }
6731
6732 // Handle final rounding.
6733 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006734}
6735
Dan Gohmand858e902010-04-17 15:26:15 +00006736SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6737 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006738 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006739 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006740
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006741 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006742 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6743 // the optimization here.
6744 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006745 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006746
Owen Andersone50ed302009-08-10 22:56:29 +00006747 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006748 EVT DstVT = Op.getValueType();
6749 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006750 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006751 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006752 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006753
6754 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006755 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006756 if (SrcVT == MVT::i32) {
6757 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6758 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6759 getPointerTy(), StackSlot, WordOff);
6760 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006761 StackSlot, MachinePointerInfo(),
6762 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006763 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006764 OffsetSlot, MachinePointerInfo(),
6765 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006766 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6767 return Fild;
6768 }
6769
6770 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6771 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006772 StackSlot, MachinePointerInfo(),
6773 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006774 // For i64 source, we need to add the appropriate power of 2 if the input
6775 // was negative. This is the same as the optimization in
6776 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6777 // we must be careful to do the computation in x87 extended precision, not
6778 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006779 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6780 MachineMemOperand *MMO =
6781 DAG.getMachineFunction()
6782 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6783 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006784
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006785 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6786 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006787 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6788 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006789
6790 APInt FF(32, 0x5F800000ULL);
6791
6792 // Check whether the sign bit is set.
6793 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6794 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6795 ISD::SETLT);
6796
6797 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6798 SDValue FudgePtr = DAG.getConstantPool(
6799 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6800 getPointerTy());
6801
6802 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6803 SDValue Zero = DAG.getIntPtrConstant(0);
6804 SDValue Four = DAG.getIntPtrConstant(4);
6805 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6806 Zero, Four);
6807 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6808
6809 // Load the value out, extending it from f32 to f80.
6810 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006811 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006812 FudgePtr, MachinePointerInfo::getConstantPool(),
6813 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006814 // Extend everything to 80 bits to force it to be done on x87.
6815 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6816 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006817}
6818
Dan Gohman475871a2008-07-27 21:46:04 +00006819std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006820FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006821 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006822
Owen Andersone50ed302009-08-10 22:56:29 +00006823 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006824
6825 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006826 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6827 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006828 }
6829
Owen Anderson825b72b2009-08-11 20:47:22 +00006830 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6831 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006832 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006833
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006834 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006835 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006836 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006837 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006838 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006840 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006841 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006842
Evan Cheng87c89352007-10-15 20:11:21 +00006843 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6844 // stack slot.
6845 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006846 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006847 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006848 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006849
Michael J. Spencerec38de22010-10-10 22:04:20 +00006850
6851
Evan Cheng0db9fe62006-04-25 20:13:52 +00006852 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006853 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006854 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6856 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6857 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006858 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006859
Dan Gohman475871a2008-07-27 21:46:04 +00006860 SDValue Chain = DAG.getEntryNode();
6861 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006862 EVT TheVT = Op.getOperand(0).getValueType();
6863 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006864 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006865 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006866 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006867 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006868 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006869 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006870 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006871 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006872
Chris Lattner492a43e2010-09-22 01:28:21 +00006873 MachineMemOperand *MMO =
6874 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6875 MachineMemOperand::MOLoad, MemSize, MemSize);
6876 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6877 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006878 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006879 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006880 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6881 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006882
Chris Lattner07290932010-09-22 01:05:16 +00006883 MachineMemOperand *MMO =
6884 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6885 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006886
Evan Cheng0db9fe62006-04-25 20:13:52 +00006887 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006888 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006889 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6890 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006891
Chris Lattner27a6c732007-11-24 07:07:01 +00006892 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006893}
6894
Dan Gohmand858e902010-04-17 15:26:15 +00006895SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6896 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006897 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006898 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006899
Eli Friedman948e95a2009-05-23 09:59:16 +00006900 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006901 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006902 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6903 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006904
Chris Lattner27a6c732007-11-24 07:07:01 +00006905 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006906 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006907 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006908}
6909
Dan Gohmand858e902010-04-17 15:26:15 +00006910SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6911 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006912 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6913 SDValue FIST = Vals.first, StackSlot = Vals.second;
6914 assert(FIST.getNode() && "Unexpected failure");
6915
6916 // Load the result.
6917 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006918 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006919}
6920
Dan Gohmand858e902010-04-17 15:26:15 +00006921SDValue X86TargetLowering::LowerFABS(SDValue Op,
6922 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006923 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006924 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006925 EVT VT = Op.getValueType();
6926 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006927 if (VT.isVector())
6928 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006929 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006931 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006932 CV.push_back(C);
6933 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006934 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006935 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006936 CV.push_back(C);
6937 CV.push_back(C);
6938 CV.push_back(C);
6939 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006940 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006941 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006942 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006943 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006944 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006945 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006946 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006947}
6948
Dan Gohmand858e902010-04-17 15:26:15 +00006949SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006950 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006951 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006952 EVT VT = Op.getValueType();
6953 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006954 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006955 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006956 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006958 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006959 CV.push_back(C);
6960 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006961 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006962 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006963 CV.push_back(C);
6964 CV.push_back(C);
6965 CV.push_back(C);
6966 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006967 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006968 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006969 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006970 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006971 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006972 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006973 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006974 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006975 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006976 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006977 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006978 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006979 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006980 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006981 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006982}
6983
Dan Gohmand858e902010-04-17 15:26:15 +00006984SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006985 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006986 SDValue Op0 = Op.getOperand(0);
6987 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006988 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006989 EVT VT = Op.getValueType();
6990 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006991
6992 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006993 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006994 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006995 SrcVT = VT;
6996 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006997 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006998 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006999 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007000 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007001 }
7002
7003 // At this point the operands and the result should have the same
7004 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007005
Evan Cheng68c47cb2007-01-05 07:55:56 +00007006 // First get the sign bit of second operand.
7007 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007008 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007009 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7010 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007011 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007012 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7013 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7014 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7015 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007016 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007017 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007018 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007019 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007020 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007021 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007022 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007023
7024 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007025 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007026 // Op0 is MVT::f32, Op1 is MVT::f64.
7027 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7028 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7029 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007030 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007031 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007032 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007033 }
7034
Evan Cheng73d6cf12007-01-05 21:37:56 +00007035 // Clear first operand sign bit.
7036 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007037 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007038 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7039 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007040 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007041 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7042 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7043 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7044 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007045 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007046 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007047 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007048 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007049 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007050 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007051 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007052
7053 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007054 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007055}
7056
Dan Gohman076aee32009-03-04 19:44:21 +00007057/// Emit nodes that will be selected as "test Op0,Op0", or something
7058/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007059SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007060 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007061 DebugLoc dl = Op.getDebugLoc();
7062
Dan Gohman31125812009-03-07 01:58:32 +00007063 // CF and OF aren't always set the way we want. Determine which
7064 // of these we need.
7065 bool NeedCF = false;
7066 bool NeedOF = false;
7067 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007068 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007069 case X86::COND_A: case X86::COND_AE:
7070 case X86::COND_B: case X86::COND_BE:
7071 NeedCF = true;
7072 break;
7073 case X86::COND_G: case X86::COND_GE:
7074 case X86::COND_L: case X86::COND_LE:
7075 case X86::COND_O: case X86::COND_NO:
7076 NeedOF = true;
7077 break;
Dan Gohman31125812009-03-07 01:58:32 +00007078 }
7079
Dan Gohman076aee32009-03-04 19:44:21 +00007080 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007081 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7082 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007083 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7084 // Emit a CMP with 0, which is the TEST pattern.
7085 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7086 DAG.getConstant(0, Op.getValueType()));
7087
7088 unsigned Opcode = 0;
7089 unsigned NumOperands = 0;
7090 switch (Op.getNode()->getOpcode()) {
7091 case ISD::ADD:
7092 // Due to an isel shortcoming, be conservative if this add is likely to be
7093 // selected as part of a load-modify-store instruction. When the root node
7094 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7095 // uses of other nodes in the match, such as the ADD in this case. This
7096 // leads to the ADD being left around and reselected, with the result being
7097 // two adds in the output. Alas, even if none our users are stores, that
7098 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7099 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7100 // climbing the DAG back to the root, and it doesn't seem to be worth the
7101 // effort.
7102 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007103 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007104 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7105 goto default_case;
7106
7107 if (ConstantSDNode *C =
7108 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7109 // An add of one will be selected as an INC.
7110 if (C->getAPIntValue() == 1) {
7111 Opcode = X86ISD::INC;
7112 NumOperands = 1;
7113 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007114 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007115
7116 // An add of negative one (subtract of one) will be selected as a DEC.
7117 if (C->getAPIntValue().isAllOnesValue()) {
7118 Opcode = X86ISD::DEC;
7119 NumOperands = 1;
7120 break;
7121 }
Dan Gohman076aee32009-03-04 19:44:21 +00007122 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007123
7124 // Otherwise use a regular EFLAGS-setting add.
7125 Opcode = X86ISD::ADD;
7126 NumOperands = 2;
7127 break;
7128 case ISD::AND: {
7129 // If the primary and result isn't used, don't bother using X86ISD::AND,
7130 // because a TEST instruction will be better.
7131 bool NonFlagUse = false;
7132 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7133 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7134 SDNode *User = *UI;
7135 unsigned UOpNo = UI.getOperandNo();
7136 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7137 // Look pass truncate.
7138 UOpNo = User->use_begin().getOperandNo();
7139 User = *User->use_begin();
7140 }
7141
7142 if (User->getOpcode() != ISD::BRCOND &&
7143 User->getOpcode() != ISD::SETCC &&
7144 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7145 NonFlagUse = true;
7146 break;
7147 }
Dan Gohman076aee32009-03-04 19:44:21 +00007148 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007149
7150 if (!NonFlagUse)
7151 break;
7152 }
7153 // FALL THROUGH
7154 case ISD::SUB:
7155 case ISD::OR:
7156 case ISD::XOR:
7157 // Due to the ISEL shortcoming noted above, be conservative if this op is
7158 // likely to be selected as part of a load-modify-store instruction.
7159 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7160 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7161 if (UI->getOpcode() == ISD::STORE)
7162 goto default_case;
7163
7164 // Otherwise use a regular EFLAGS-setting instruction.
7165 switch (Op.getNode()->getOpcode()) {
7166 default: llvm_unreachable("unexpected operator!");
7167 case ISD::SUB: Opcode = X86ISD::SUB; break;
7168 case ISD::OR: Opcode = X86ISD::OR; break;
7169 case ISD::XOR: Opcode = X86ISD::XOR; break;
7170 case ISD::AND: Opcode = X86ISD::AND; break;
7171 }
7172
7173 NumOperands = 2;
7174 break;
7175 case X86ISD::ADD:
7176 case X86ISD::SUB:
7177 case X86ISD::INC:
7178 case X86ISD::DEC:
7179 case X86ISD::OR:
7180 case X86ISD::XOR:
7181 case X86ISD::AND:
7182 return SDValue(Op.getNode(), 1);
7183 default:
7184 default_case:
7185 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007186 }
7187
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007188 if (Opcode == 0)
7189 // Emit a CMP with 0, which is the TEST pattern.
7190 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7191 DAG.getConstant(0, Op.getValueType()));
7192
7193 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7194 SmallVector<SDValue, 4> Ops;
7195 for (unsigned i = 0; i != NumOperands; ++i)
7196 Ops.push_back(Op.getOperand(i));
7197
7198 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7199 DAG.ReplaceAllUsesWith(Op, New);
7200 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007201}
7202
7203/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7204/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007205SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007206 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007207 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7208 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007209 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007210
7211 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007212 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007213}
7214
Evan Chengd40d03e2010-01-06 19:38:29 +00007215/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7216/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007217SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7218 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007219 SDValue Op0 = And.getOperand(0);
7220 SDValue Op1 = And.getOperand(1);
7221 if (Op0.getOpcode() == ISD::TRUNCATE)
7222 Op0 = Op0.getOperand(0);
7223 if (Op1.getOpcode() == ISD::TRUNCATE)
7224 Op1 = Op1.getOperand(0);
7225
Evan Chengd40d03e2010-01-06 19:38:29 +00007226 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007227 if (Op1.getOpcode() == ISD::SHL)
7228 std::swap(Op0, Op1);
7229 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007230 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7231 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007232 // If we looked past a truncate, check that it's only truncating away
7233 // known zeros.
7234 unsigned BitWidth = Op0.getValueSizeInBits();
7235 unsigned AndBitWidth = And.getValueSizeInBits();
7236 if (BitWidth > AndBitWidth) {
7237 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7238 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7239 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7240 return SDValue();
7241 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007242 LHS = Op1;
7243 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007244 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007245 } else if (Op1.getOpcode() == ISD::Constant) {
7246 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7247 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007248 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7249 LHS = AndLHS.getOperand(0);
7250 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007251 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007252 }
Evan Cheng0488db92007-09-25 01:57:46 +00007253
Evan Chengd40d03e2010-01-06 19:38:29 +00007254 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007255 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007256 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007257 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007258 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007259 // Also promote i16 to i32 for performance / code size reason.
7260 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007261 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007262 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007263
Evan Chengd40d03e2010-01-06 19:38:29 +00007264 // If the operand types disagree, extend the shift amount to match. Since
7265 // BT ignores high bits (like shifts) we can use anyextend.
7266 if (LHS.getValueType() != RHS.getValueType())
7267 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007268
Evan Chengd40d03e2010-01-06 19:38:29 +00007269 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7270 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7271 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7272 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007273 }
7274
Evan Cheng54de3ea2010-01-05 06:52:31 +00007275 return SDValue();
7276}
7277
Dan Gohmand858e902010-04-17 15:26:15 +00007278SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007279 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7280 SDValue Op0 = Op.getOperand(0);
7281 SDValue Op1 = Op.getOperand(1);
7282 DebugLoc dl = Op.getDebugLoc();
7283 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7284
7285 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007286 // Lower (X & (1 << N)) == 0 to BT(X, N).
7287 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7288 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007289 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007290 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007291 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007292 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7293 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7294 if (NewSetCC.getNode())
7295 return NewSetCC;
7296 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007297
Chris Lattner481eebc2010-12-19 21:23:48 +00007298 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7299 // these.
7300 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007301 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7302 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7303 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007304
Chris Lattner481eebc2010-12-19 21:23:48 +00007305 // If the input is a setcc, then reuse the input setcc or use a new one with
7306 // the inverted condition.
7307 if (Op0.getOpcode() == X86ISD::SETCC) {
7308 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7309 bool Invert = (CC == ISD::SETNE) ^
7310 cast<ConstantSDNode>(Op1)->isNullValue();
7311 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007312
Evan Cheng2c755ba2010-02-27 07:36:59 +00007313 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007314 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7315 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7316 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007317 }
7318
Evan Chenge5b51ac2010-04-17 06:13:15 +00007319 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007320 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007321 if (X86CC == X86::COND_INVALID)
7322 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007323
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007324 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007325 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007326 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007327}
7328
Dan Gohmand858e902010-04-17 15:26:15 +00007329SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007330 SDValue Cond;
7331 SDValue Op0 = Op.getOperand(0);
7332 SDValue Op1 = Op.getOperand(1);
7333 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007334 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007335 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7336 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007337 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007338
7339 if (isFP) {
7340 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007341 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007342 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7343 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007344 bool Swap = false;
7345
7346 switch (SetCCOpcode) {
7347 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007348 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007349 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007350 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007351 case ISD::SETGT: Swap = true; // Fallthrough
7352 case ISD::SETLT:
7353 case ISD::SETOLT: SSECC = 1; break;
7354 case ISD::SETOGE:
7355 case ISD::SETGE: Swap = true; // Fallthrough
7356 case ISD::SETLE:
7357 case ISD::SETOLE: SSECC = 2; break;
7358 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007359 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007360 case ISD::SETNE: SSECC = 4; break;
7361 case ISD::SETULE: Swap = true;
7362 case ISD::SETUGE: SSECC = 5; break;
7363 case ISD::SETULT: Swap = true;
7364 case ISD::SETUGT: SSECC = 6; break;
7365 case ISD::SETO: SSECC = 7; break;
7366 }
7367 if (Swap)
7368 std::swap(Op0, Op1);
7369
Nate Begemanfb8ead02008-07-25 19:05:58 +00007370 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007371 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007372 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007373 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007374 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7375 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007376 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007377 }
7378 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007379 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007380 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7381 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007382 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007383 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007384 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007385 }
7386 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007387 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007388 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007389
Nate Begeman30a0de92008-07-17 16:51:19 +00007390 // We are handling one of the integer comparisons here. Since SSE only has
7391 // GT and EQ comparisons for integer, swapping operands and multiple
7392 // operations may be required for some comparisons.
7393 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7394 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007395
Owen Anderson825b72b2009-08-11 20:47:22 +00007396 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007397 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007398 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007399 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007400 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7401 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007402 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007403
Nate Begeman30a0de92008-07-17 16:51:19 +00007404 switch (SetCCOpcode) {
7405 default: break;
7406 case ISD::SETNE: Invert = true;
7407 case ISD::SETEQ: Opc = EQOpc; break;
7408 case ISD::SETLT: Swap = true;
7409 case ISD::SETGT: Opc = GTOpc; break;
7410 case ISD::SETGE: Swap = true;
7411 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7412 case ISD::SETULT: Swap = true;
7413 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7414 case ISD::SETUGE: Swap = true;
7415 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7416 }
7417 if (Swap)
7418 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007419
Nate Begeman30a0de92008-07-17 16:51:19 +00007420 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7421 // bits of the inputs before performing those operations.
7422 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007423 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007424 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7425 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007426 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007427 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7428 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007429 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7430 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007431 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007432
Dale Johannesenace16102009-02-03 19:33:06 +00007433 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007434
7435 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007436 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007437 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007438
Nate Begeman30a0de92008-07-17 16:51:19 +00007439 return Result;
7440}
Evan Cheng0488db92007-09-25 01:57:46 +00007441
Evan Cheng370e5342008-12-03 08:38:43 +00007442// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007443static bool isX86LogicalCmp(SDValue Op) {
7444 unsigned Opc = Op.getNode()->getOpcode();
7445 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7446 return true;
7447 if (Op.getResNo() == 1 &&
7448 (Opc == X86ISD::ADD ||
7449 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007450 Opc == X86ISD::ADC ||
7451 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007452 Opc == X86ISD::SMUL ||
7453 Opc == X86ISD::UMUL ||
7454 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007455 Opc == X86ISD::DEC ||
7456 Opc == X86ISD::OR ||
7457 Opc == X86ISD::XOR ||
7458 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007459 return true;
7460
Chris Lattner9637d5b2010-12-05 07:49:54 +00007461 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7462 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007463
Dan Gohman076aee32009-03-04 19:44:21 +00007464 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007465}
7466
Chris Lattnera2b56002010-12-05 01:23:24 +00007467static bool isZero(SDValue V) {
7468 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7469 return C && C->isNullValue();
7470}
7471
Chris Lattner96908b12010-12-05 02:00:51 +00007472static bool isAllOnes(SDValue V) {
7473 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7474 return C && C->isAllOnesValue();
7475}
7476
Dan Gohmand858e902010-04-17 15:26:15 +00007477SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007478 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007479 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007480 SDValue Op1 = Op.getOperand(1);
7481 SDValue Op2 = Op.getOperand(2);
7482 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007483 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007484
Dan Gohman1a492952009-10-20 16:22:37 +00007485 if (Cond.getOpcode() == ISD::SETCC) {
7486 SDValue NewCond = LowerSETCC(Cond, DAG);
7487 if (NewCond.getNode())
7488 Cond = NewCond;
7489 }
Evan Cheng734503b2006-09-11 02:19:56 +00007490
Chris Lattnera2b56002010-12-05 01:23:24 +00007491 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007492 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007493 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007494 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007495 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007496 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7497 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007498 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007499
Chris Lattnera2b56002010-12-05 01:23:24 +00007500 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007501
7502 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007503 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7504 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007505
7506 SDValue CmpOp0 = Cmp.getOperand(0);
7507 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7508 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007509
Chris Lattner96908b12010-12-05 02:00:51 +00007510 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007511 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7512 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007513
Chris Lattner96908b12010-12-05 02:00:51 +00007514 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7515 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007516
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007517 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007518 if (N2C == 0 || !N2C->isNullValue())
7519 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7520 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007521 }
7522 }
7523
Chris Lattnera2b56002010-12-05 01:23:24 +00007524 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007525 if (Cond.getOpcode() == ISD::AND &&
7526 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7527 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007528 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007529 Cond = Cond.getOperand(0);
7530 }
7531
Evan Cheng3f41d662007-10-08 22:16:29 +00007532 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7533 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007534 if (Cond.getOpcode() == X86ISD::SETCC ||
7535 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007536 CC = Cond.getOperand(0);
7537
Dan Gohman475871a2008-07-27 21:46:04 +00007538 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007539 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007540 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007541
Evan Cheng3f41d662007-10-08 22:16:29 +00007542 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007543 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007544 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007545 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007546
Chris Lattnerd1980a52009-03-12 06:52:53 +00007547 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7548 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007549 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007550 addTest = false;
7551 }
7552 }
7553
7554 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007555 // Look pass the truncate.
7556 if (Cond.getOpcode() == ISD::TRUNCATE)
7557 Cond = Cond.getOperand(0);
7558
7559 // We know the result of AND is compared against zero. Try to match
7560 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007561 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007562 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007563 if (NewSetCC.getNode()) {
7564 CC = NewSetCC.getOperand(0);
7565 Cond = NewSetCC.getOperand(1);
7566 addTest = false;
7567 }
7568 }
7569 }
7570
7571 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007572 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007573 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007574 }
7575
Benjamin Kramere915ff32010-12-22 23:09:28 +00007576 // a < b ? -1 : 0 -> RES = ~setcc_carry
7577 // a < b ? 0 : -1 -> RES = setcc_carry
7578 // a >= b ? -1 : 0 -> RES = setcc_carry
7579 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7580 if (Cond.getOpcode() == X86ISD::CMP) {
7581 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7582
7583 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7584 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7585 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7586 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7587 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7588 return DAG.getNOT(DL, Res, Res.getValueType());
7589 return Res;
7590 }
7591 }
7592
Evan Cheng0488db92007-09-25 01:57:46 +00007593 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7594 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007595 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007596 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007597 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007598}
7599
Evan Cheng370e5342008-12-03 08:38:43 +00007600// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7601// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7602// from the AND / OR.
7603static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7604 Opc = Op.getOpcode();
7605 if (Opc != ISD::OR && Opc != ISD::AND)
7606 return false;
7607 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7608 Op.getOperand(0).hasOneUse() &&
7609 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7610 Op.getOperand(1).hasOneUse());
7611}
7612
Evan Cheng961d6d42009-02-02 08:19:07 +00007613// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7614// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007615static bool isXor1OfSetCC(SDValue Op) {
7616 if (Op.getOpcode() != ISD::XOR)
7617 return false;
7618 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7619 if (N1C && N1C->getAPIntValue() == 1) {
7620 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7621 Op.getOperand(0).hasOneUse();
7622 }
7623 return false;
7624}
7625
Dan Gohmand858e902010-04-17 15:26:15 +00007626SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007627 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007628 SDValue Chain = Op.getOperand(0);
7629 SDValue Cond = Op.getOperand(1);
7630 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007631 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007632 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007633
Dan Gohman1a492952009-10-20 16:22:37 +00007634 if (Cond.getOpcode() == ISD::SETCC) {
7635 SDValue NewCond = LowerSETCC(Cond, DAG);
7636 if (NewCond.getNode())
7637 Cond = NewCond;
7638 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007639#if 0
7640 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007641 else if (Cond.getOpcode() == X86ISD::ADD ||
7642 Cond.getOpcode() == X86ISD::SUB ||
7643 Cond.getOpcode() == X86ISD::SMUL ||
7644 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007645 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007646#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007647
Evan Chengad9c0a32009-12-15 00:53:42 +00007648 // Look pass (and (setcc_carry (cmp ...)), 1).
7649 if (Cond.getOpcode() == ISD::AND &&
7650 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7651 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007652 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007653 Cond = Cond.getOperand(0);
7654 }
7655
Evan Cheng3f41d662007-10-08 22:16:29 +00007656 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7657 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007658 if (Cond.getOpcode() == X86ISD::SETCC ||
7659 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007660 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007661
Dan Gohman475871a2008-07-27 21:46:04 +00007662 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007663 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007664 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007665 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007666 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007667 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007668 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007669 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007670 default: break;
7671 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007672 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007673 // These can only come from an arithmetic instruction with overflow,
7674 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007675 Cond = Cond.getNode()->getOperand(1);
7676 addTest = false;
7677 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007678 }
Evan Cheng0488db92007-09-25 01:57:46 +00007679 }
Evan Cheng370e5342008-12-03 08:38:43 +00007680 } else {
7681 unsigned CondOpc;
7682 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7683 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007684 if (CondOpc == ISD::OR) {
7685 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7686 // two branches instead of an explicit OR instruction with a
7687 // separate test.
7688 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007689 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007690 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007691 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007692 Chain, Dest, CC, Cmp);
7693 CC = Cond.getOperand(1).getOperand(0);
7694 Cond = Cmp;
7695 addTest = false;
7696 }
7697 } else { // ISD::AND
7698 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7699 // two branches instead of an explicit AND instruction with a
7700 // separate test. However, we only do this if this block doesn't
7701 // have a fall-through edge, because this requires an explicit
7702 // jmp when the condition is false.
7703 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007704 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007705 Op.getNode()->hasOneUse()) {
7706 X86::CondCode CCode =
7707 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7708 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007709 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007710 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007711 // Look for an unconditional branch following this conditional branch.
7712 // We need this because we need to reverse the successors in order
7713 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007714 if (User->getOpcode() == ISD::BR) {
7715 SDValue FalseBB = User->getOperand(1);
7716 SDNode *NewBR =
7717 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007718 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007719 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007720 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007721
Dale Johannesene4d209d2009-02-03 20:21:25 +00007722 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007723 Chain, Dest, CC, Cmp);
7724 X86::CondCode CCode =
7725 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7726 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007727 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007728 Cond = Cmp;
7729 addTest = false;
7730 }
7731 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007732 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007733 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7734 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7735 // It should be transformed during dag combiner except when the condition
7736 // is set by a arithmetics with overflow node.
7737 X86::CondCode CCode =
7738 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7739 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007741 Cond = Cond.getOperand(0).getOperand(1);
7742 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007743 }
Evan Cheng0488db92007-09-25 01:57:46 +00007744 }
7745
7746 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007747 // Look pass the truncate.
7748 if (Cond.getOpcode() == ISD::TRUNCATE)
7749 Cond = Cond.getOperand(0);
7750
7751 // We know the result of AND is compared against zero. Try to match
7752 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007753 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007754 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7755 if (NewSetCC.getNode()) {
7756 CC = NewSetCC.getOperand(0);
7757 Cond = NewSetCC.getOperand(1);
7758 addTest = false;
7759 }
7760 }
7761 }
7762
7763 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007764 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007765 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007766 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007767 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007768 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007769}
7770
Anton Korobeynikove060b532007-04-17 19:34:00 +00007771
7772// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7773// Calls to _alloca is needed to probe the stack when allocating more than 4k
7774// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7775// that the guard pages used by the OS virtual memory manager are allocated in
7776// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007777SDValue
7778X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007779 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007780 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007781 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007782 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007783
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007784 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007785 SDValue Chain = Op.getOperand(0);
7786 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007787 // FIXME: Ensure alignment here
7788
Dan Gohman475871a2008-07-27 21:46:04 +00007789 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007790
Owen Anderson825b72b2009-08-11 20:47:22 +00007791 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007792
Dale Johannesendd64c412009-02-04 00:33:20 +00007793 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007794 Flag = Chain.getValue(1);
7795
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007796 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007797
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007798 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007799 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007800
Dale Johannesendd64c412009-02-04 00:33:20 +00007801 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007802
Dan Gohman475871a2008-07-27 21:46:04 +00007803 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007804 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007805}
7806
Dan Gohmand858e902010-04-17 15:26:15 +00007807SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007808 MachineFunction &MF = DAG.getMachineFunction();
7809 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7810
Dan Gohman69de1932008-02-06 22:27:42 +00007811 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007812 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007813
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007814 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007815 // vastart just stores the address of the VarArgsFrameIndex slot into the
7816 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007817 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7818 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007819 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7820 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007821 }
7822
7823 // __va_list_tag:
7824 // gp_offset (0 - 6 * 8)
7825 // fp_offset (48 - 48 + 8 * 16)
7826 // overflow_arg_area (point to parameters coming in memory).
7827 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007828 SmallVector<SDValue, 8> MemOps;
7829 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007830 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007831 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007832 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7833 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007834 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007835 MemOps.push_back(Store);
7836
7837 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007838 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007839 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007840 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007841 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7842 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007843 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007844 MemOps.push_back(Store);
7845
7846 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007847 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007848 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007849 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7850 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007851 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7852 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007853 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007854 MemOps.push_back(Store);
7855
7856 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007857 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007858 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007859 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7860 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007861 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7862 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007863 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007864 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007865 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007866}
7867
Dan Gohmand858e902010-04-17 15:26:15 +00007868SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007869 assert(Subtarget->is64Bit() &&
7870 "LowerVAARG only handles 64-bit va_arg!");
7871 assert((Subtarget->isTargetLinux() ||
7872 Subtarget->isTargetDarwin()) &&
7873 "Unhandled target in LowerVAARG");
7874 assert(Op.getNode()->getNumOperands() == 4);
7875 SDValue Chain = Op.getOperand(0);
7876 SDValue SrcPtr = Op.getOperand(1);
7877 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7878 unsigned Align = Op.getConstantOperandVal(3);
7879 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007880
Dan Gohman320afb82010-10-12 18:00:49 +00007881 EVT ArgVT = Op.getNode()->getValueType(0);
7882 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7883 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7884 uint8_t ArgMode;
7885
7886 // Decide which area this value should be read from.
7887 // TODO: Implement the AMD64 ABI in its entirety. This simple
7888 // selection mechanism works only for the basic types.
7889 if (ArgVT == MVT::f80) {
7890 llvm_unreachable("va_arg for f80 not yet implemented");
7891 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7892 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7893 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7894 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7895 } else {
7896 llvm_unreachable("Unhandled argument type in LowerVAARG");
7897 }
7898
7899 if (ArgMode == 2) {
7900 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007901 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007902 !(DAG.getMachineFunction()
7903 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007904 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007905 }
7906
7907 // Insert VAARG_64 node into the DAG
7908 // VAARG_64 returns two values: Variable Argument Address, Chain
7909 SmallVector<SDValue, 11> InstOps;
7910 InstOps.push_back(Chain);
7911 InstOps.push_back(SrcPtr);
7912 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7913 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7914 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7915 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7916 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7917 VTs, &InstOps[0], InstOps.size(),
7918 MVT::i64,
7919 MachinePointerInfo(SV),
7920 /*Align=*/0,
7921 /*Volatile=*/false,
7922 /*ReadMem=*/true,
7923 /*WriteMem=*/true);
7924 Chain = VAARG.getValue(1);
7925
7926 // Load the next argument and return it
7927 return DAG.getLoad(ArgVT, dl,
7928 Chain,
7929 VAARG,
7930 MachinePointerInfo(),
7931 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007932}
7933
Dan Gohmand858e902010-04-17 15:26:15 +00007934SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007935 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007936 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007937 SDValue Chain = Op.getOperand(0);
7938 SDValue DstPtr = Op.getOperand(1);
7939 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007940 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7941 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007942 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007943
Chris Lattnere72f2022010-09-21 05:40:29 +00007944 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007945 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007946 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007947 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007948}
7949
Dan Gohman475871a2008-07-27 21:46:04 +00007950SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007951X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007952 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007953 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007954 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007955 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007956 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007957 case Intrinsic::x86_sse_comieq_ss:
7958 case Intrinsic::x86_sse_comilt_ss:
7959 case Intrinsic::x86_sse_comile_ss:
7960 case Intrinsic::x86_sse_comigt_ss:
7961 case Intrinsic::x86_sse_comige_ss:
7962 case Intrinsic::x86_sse_comineq_ss:
7963 case Intrinsic::x86_sse_ucomieq_ss:
7964 case Intrinsic::x86_sse_ucomilt_ss:
7965 case Intrinsic::x86_sse_ucomile_ss:
7966 case Intrinsic::x86_sse_ucomigt_ss:
7967 case Intrinsic::x86_sse_ucomige_ss:
7968 case Intrinsic::x86_sse_ucomineq_ss:
7969 case Intrinsic::x86_sse2_comieq_sd:
7970 case Intrinsic::x86_sse2_comilt_sd:
7971 case Intrinsic::x86_sse2_comile_sd:
7972 case Intrinsic::x86_sse2_comigt_sd:
7973 case Intrinsic::x86_sse2_comige_sd:
7974 case Intrinsic::x86_sse2_comineq_sd:
7975 case Intrinsic::x86_sse2_ucomieq_sd:
7976 case Intrinsic::x86_sse2_ucomilt_sd:
7977 case Intrinsic::x86_sse2_ucomile_sd:
7978 case Intrinsic::x86_sse2_ucomigt_sd:
7979 case Intrinsic::x86_sse2_ucomige_sd:
7980 case Intrinsic::x86_sse2_ucomineq_sd: {
7981 unsigned Opc = 0;
7982 ISD::CondCode CC = ISD::SETCC_INVALID;
7983 switch (IntNo) {
7984 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007985 case Intrinsic::x86_sse_comieq_ss:
7986 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007987 Opc = X86ISD::COMI;
7988 CC = ISD::SETEQ;
7989 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007990 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007991 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007992 Opc = X86ISD::COMI;
7993 CC = ISD::SETLT;
7994 break;
7995 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007996 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007997 Opc = X86ISD::COMI;
7998 CC = ISD::SETLE;
7999 break;
8000 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008001 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008002 Opc = X86ISD::COMI;
8003 CC = ISD::SETGT;
8004 break;
8005 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008006 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008007 Opc = X86ISD::COMI;
8008 CC = ISD::SETGE;
8009 break;
8010 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008011 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008012 Opc = X86ISD::COMI;
8013 CC = ISD::SETNE;
8014 break;
8015 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008016 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008017 Opc = X86ISD::UCOMI;
8018 CC = ISD::SETEQ;
8019 break;
8020 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008021 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008022 Opc = X86ISD::UCOMI;
8023 CC = ISD::SETLT;
8024 break;
8025 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008026 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008027 Opc = X86ISD::UCOMI;
8028 CC = ISD::SETLE;
8029 break;
8030 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008031 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008032 Opc = X86ISD::UCOMI;
8033 CC = ISD::SETGT;
8034 break;
8035 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008036 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008037 Opc = X86ISD::UCOMI;
8038 CC = ISD::SETGE;
8039 break;
8040 case Intrinsic::x86_sse_ucomineq_ss:
8041 case Intrinsic::x86_sse2_ucomineq_sd:
8042 Opc = X86ISD::UCOMI;
8043 CC = ISD::SETNE;
8044 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008045 }
Evan Cheng734503b2006-09-11 02:19:56 +00008046
Dan Gohman475871a2008-07-27 21:46:04 +00008047 SDValue LHS = Op.getOperand(1);
8048 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008049 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008050 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008051 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8052 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8053 DAG.getConstant(X86CC, MVT::i8), Cond);
8054 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008055 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008056 // ptest and testp intrinsics. The intrinsic these come from are designed to
8057 // return an integer value, not just an instruction so lower it to the ptest
8058 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008059 case Intrinsic::x86_sse41_ptestz:
8060 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008061 case Intrinsic::x86_sse41_ptestnzc:
8062 case Intrinsic::x86_avx_ptestz_256:
8063 case Intrinsic::x86_avx_ptestc_256:
8064 case Intrinsic::x86_avx_ptestnzc_256:
8065 case Intrinsic::x86_avx_vtestz_ps:
8066 case Intrinsic::x86_avx_vtestc_ps:
8067 case Intrinsic::x86_avx_vtestnzc_ps:
8068 case Intrinsic::x86_avx_vtestz_pd:
8069 case Intrinsic::x86_avx_vtestc_pd:
8070 case Intrinsic::x86_avx_vtestnzc_pd:
8071 case Intrinsic::x86_avx_vtestz_ps_256:
8072 case Intrinsic::x86_avx_vtestc_ps_256:
8073 case Intrinsic::x86_avx_vtestnzc_ps_256:
8074 case Intrinsic::x86_avx_vtestz_pd_256:
8075 case Intrinsic::x86_avx_vtestc_pd_256:
8076 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8077 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008078 unsigned X86CC = 0;
8079 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008080 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008081 case Intrinsic::x86_avx_vtestz_ps:
8082 case Intrinsic::x86_avx_vtestz_pd:
8083 case Intrinsic::x86_avx_vtestz_ps_256:
8084 case Intrinsic::x86_avx_vtestz_pd_256:
8085 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008086 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008087 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008088 // ZF = 1
8089 X86CC = X86::COND_E;
8090 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008091 case Intrinsic::x86_avx_vtestc_ps:
8092 case Intrinsic::x86_avx_vtestc_pd:
8093 case Intrinsic::x86_avx_vtestc_ps_256:
8094 case Intrinsic::x86_avx_vtestc_pd_256:
8095 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008096 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008097 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008098 // CF = 1
8099 X86CC = X86::COND_B;
8100 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008101 case Intrinsic::x86_avx_vtestnzc_ps:
8102 case Intrinsic::x86_avx_vtestnzc_pd:
8103 case Intrinsic::x86_avx_vtestnzc_ps_256:
8104 case Intrinsic::x86_avx_vtestnzc_pd_256:
8105 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008106 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008107 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008108 // ZF and CF = 0
8109 X86CC = X86::COND_A;
8110 break;
8111 }
Eric Christopherfd179292009-08-27 18:07:15 +00008112
Eric Christopher71c67532009-07-29 00:28:05 +00008113 SDValue LHS = Op.getOperand(1);
8114 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008115 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8116 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008117 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8118 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8119 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008120 }
Evan Cheng5759f972008-05-04 09:15:50 +00008121
8122 // Fix vector shift instructions where the last operand is a non-immediate
8123 // i32 value.
8124 case Intrinsic::x86_sse2_pslli_w:
8125 case Intrinsic::x86_sse2_pslli_d:
8126 case Intrinsic::x86_sse2_pslli_q:
8127 case Intrinsic::x86_sse2_psrli_w:
8128 case Intrinsic::x86_sse2_psrli_d:
8129 case Intrinsic::x86_sse2_psrli_q:
8130 case Intrinsic::x86_sse2_psrai_w:
8131 case Intrinsic::x86_sse2_psrai_d:
8132 case Intrinsic::x86_mmx_pslli_w:
8133 case Intrinsic::x86_mmx_pslli_d:
8134 case Intrinsic::x86_mmx_pslli_q:
8135 case Intrinsic::x86_mmx_psrli_w:
8136 case Intrinsic::x86_mmx_psrli_d:
8137 case Intrinsic::x86_mmx_psrli_q:
8138 case Intrinsic::x86_mmx_psrai_w:
8139 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008140 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008141 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008142 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008143
8144 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008145 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008146 switch (IntNo) {
8147 case Intrinsic::x86_sse2_pslli_w:
8148 NewIntNo = Intrinsic::x86_sse2_psll_w;
8149 break;
8150 case Intrinsic::x86_sse2_pslli_d:
8151 NewIntNo = Intrinsic::x86_sse2_psll_d;
8152 break;
8153 case Intrinsic::x86_sse2_pslli_q:
8154 NewIntNo = Intrinsic::x86_sse2_psll_q;
8155 break;
8156 case Intrinsic::x86_sse2_psrli_w:
8157 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8158 break;
8159 case Intrinsic::x86_sse2_psrli_d:
8160 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8161 break;
8162 case Intrinsic::x86_sse2_psrli_q:
8163 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8164 break;
8165 case Intrinsic::x86_sse2_psrai_w:
8166 NewIntNo = Intrinsic::x86_sse2_psra_w;
8167 break;
8168 case Intrinsic::x86_sse2_psrai_d:
8169 NewIntNo = Intrinsic::x86_sse2_psra_d;
8170 break;
8171 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008172 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008173 switch (IntNo) {
8174 case Intrinsic::x86_mmx_pslli_w:
8175 NewIntNo = Intrinsic::x86_mmx_psll_w;
8176 break;
8177 case Intrinsic::x86_mmx_pslli_d:
8178 NewIntNo = Intrinsic::x86_mmx_psll_d;
8179 break;
8180 case Intrinsic::x86_mmx_pslli_q:
8181 NewIntNo = Intrinsic::x86_mmx_psll_q;
8182 break;
8183 case Intrinsic::x86_mmx_psrli_w:
8184 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8185 break;
8186 case Intrinsic::x86_mmx_psrli_d:
8187 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8188 break;
8189 case Intrinsic::x86_mmx_psrli_q:
8190 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8191 break;
8192 case Intrinsic::x86_mmx_psrai_w:
8193 NewIntNo = Intrinsic::x86_mmx_psra_w;
8194 break;
8195 case Intrinsic::x86_mmx_psrai_d:
8196 NewIntNo = Intrinsic::x86_mmx_psra_d;
8197 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008198 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008199 }
8200 break;
8201 }
8202 }
Mon P Wangefa42202009-09-03 19:56:25 +00008203
8204 // The vector shift intrinsics with scalars uses 32b shift amounts but
8205 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8206 // to be zero.
8207 SDValue ShOps[4];
8208 ShOps[0] = ShAmt;
8209 ShOps[1] = DAG.getConstant(0, MVT::i32);
8210 if (ShAmtVT == MVT::v4i32) {
8211 ShOps[2] = DAG.getUNDEF(MVT::i32);
8212 ShOps[3] = DAG.getUNDEF(MVT::i32);
8213 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8214 } else {
8215 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008216// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008217 }
8218
Owen Andersone50ed302009-08-10 22:56:29 +00008219 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008220 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008221 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008222 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008223 Op.getOperand(1), ShAmt);
8224 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008225 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008226}
Evan Cheng72261582005-12-20 06:22:03 +00008227
Dan Gohmand858e902010-04-17 15:26:15 +00008228SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8229 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008230 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8231 MFI->setReturnAddressIsTaken(true);
8232
Bill Wendling64e87322009-01-16 19:25:27 +00008233 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008234 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008235
8236 if (Depth > 0) {
8237 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8238 SDValue Offset =
8239 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008240 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008241 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008242 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008243 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008244 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008245 }
8246
8247 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008248 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008249 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008250 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008251}
8252
Dan Gohmand858e902010-04-17 15:26:15 +00008253SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008254 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8255 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008256
Owen Andersone50ed302009-08-10 22:56:29 +00008257 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008258 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008259 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8260 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008261 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008262 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008263 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8264 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008265 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008266 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008267}
8268
Dan Gohman475871a2008-07-27 21:46:04 +00008269SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008270 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008271 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008272}
8273
Dan Gohmand858e902010-04-17 15:26:15 +00008274SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008275 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008276 SDValue Chain = Op.getOperand(0);
8277 SDValue Offset = Op.getOperand(1);
8278 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008279 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008280
Dan Gohmand8816272010-08-11 18:14:00 +00008281 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8282 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8283 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008284 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008285
Dan Gohmand8816272010-08-11 18:14:00 +00008286 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8287 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008288 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008289 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8290 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008291 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008292 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008293
Dale Johannesene4d209d2009-02-03 20:21:25 +00008294 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008295 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008296 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008297}
8298
Dan Gohman475871a2008-07-27 21:46:04 +00008299SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008300 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008301 SDValue Root = Op.getOperand(0);
8302 SDValue Trmp = Op.getOperand(1); // trampoline
8303 SDValue FPtr = Op.getOperand(2); // nested function
8304 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008305 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008306
Dan Gohman69de1932008-02-06 22:27:42 +00008307 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008308
8309 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008310 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008311
8312 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008313 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8314 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008315
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008316 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8317 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008318
8319 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8320
8321 // Load the pointer to the nested function into R11.
8322 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008323 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008324 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008325 Addr, MachinePointerInfo(TrmpAddr),
8326 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008327
Owen Anderson825b72b2009-08-11 20:47:22 +00008328 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8329 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008330 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8331 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008332 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008333
8334 // Load the 'nest' parameter value into R10.
8335 // R10 is specified in X86CallingConv.td
8336 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008337 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8338 DAG.getConstant(10, MVT::i64));
8339 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008340 Addr, MachinePointerInfo(TrmpAddr, 10),
8341 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008342
Owen Anderson825b72b2009-08-11 20:47:22 +00008343 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8344 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008345 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8346 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008347 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008348
8349 // Jump to the nested function.
8350 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008351 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8352 DAG.getConstant(20, MVT::i64));
8353 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008354 Addr, MachinePointerInfo(TrmpAddr, 20),
8355 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008356
8357 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008358 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8359 DAG.getConstant(22, MVT::i64));
8360 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008361 MachinePointerInfo(TrmpAddr, 22),
8362 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008363
Dan Gohman475871a2008-07-27 21:46:04 +00008364 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008365 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008366 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008367 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008368 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008369 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008370 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008371 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008372
8373 switch (CC) {
8374 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008375 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008376 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008377 case CallingConv::X86_StdCall: {
8378 // Pass 'nest' parameter in ECX.
8379 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008380 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008381
8382 // Check that ECX wasn't needed by an 'inreg' parameter.
8383 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008384 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008385
Chris Lattner58d74912008-03-12 17:45:29 +00008386 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008387 unsigned InRegCount = 0;
8388 unsigned Idx = 1;
8389
8390 for (FunctionType::param_iterator I = FTy->param_begin(),
8391 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008392 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008393 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008394 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008395
8396 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008397 report_fatal_error("Nest register in use - reduce number of inreg"
8398 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008399 }
8400 }
8401 break;
8402 }
8403 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008404 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008405 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008406 // Pass 'nest' parameter in EAX.
8407 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008408 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008409 break;
8410 }
8411
Dan Gohman475871a2008-07-27 21:46:04 +00008412 SDValue OutChains[4];
8413 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008414
Owen Anderson825b72b2009-08-11 20:47:22 +00008415 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8416 DAG.getConstant(10, MVT::i32));
8417 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008418
Chris Lattnera62fe662010-02-05 19:20:30 +00008419 // This is storing the opcode for MOV32ri.
8420 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008421 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008422 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008423 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008424 Trmp, MachinePointerInfo(TrmpAddr),
8425 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008426
Owen Anderson825b72b2009-08-11 20:47:22 +00008427 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8428 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008429 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8430 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008431 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008432
Chris Lattnera62fe662010-02-05 19:20:30 +00008433 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008434 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8435 DAG.getConstant(5, MVT::i32));
8436 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008437 MachinePointerInfo(TrmpAddr, 5),
8438 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008439
Owen Anderson825b72b2009-08-11 20:47:22 +00008440 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8441 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008442 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8443 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008444 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008445
Dan Gohman475871a2008-07-27 21:46:04 +00008446 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008447 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008448 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008449 }
8450}
8451
Dan Gohmand858e902010-04-17 15:26:15 +00008452SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8453 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008454 /*
8455 The rounding mode is in bits 11:10 of FPSR, and has the following
8456 settings:
8457 00 Round to nearest
8458 01 Round to -inf
8459 10 Round to +inf
8460 11 Round to 0
8461
8462 FLT_ROUNDS, on the other hand, expects the following:
8463 -1 Undefined
8464 0 Round to 0
8465 1 Round to nearest
8466 2 Round to +inf
8467 3 Round to -inf
8468
8469 To perform the conversion, we do:
8470 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8471 */
8472
8473 MachineFunction &MF = DAG.getMachineFunction();
8474 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008475 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008476 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008477 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008478 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008479
8480 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008481 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008482 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008483
Michael J. Spencerec38de22010-10-10 22:04:20 +00008484
Chris Lattner2156b792010-09-22 01:11:26 +00008485 MachineMemOperand *MMO =
8486 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8487 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008488
Chris Lattner2156b792010-09-22 01:11:26 +00008489 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8490 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8491 DAG.getVTList(MVT::Other),
8492 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008493
8494 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008495 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008496 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008497
8498 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008499 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008500 DAG.getNode(ISD::SRL, DL, MVT::i16,
8501 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008502 CWD, DAG.getConstant(0x800, MVT::i16)),
8503 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008504 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008505 DAG.getNode(ISD::SRL, DL, MVT::i16,
8506 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008507 CWD, DAG.getConstant(0x400, MVT::i16)),
8508 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008509
Dan Gohman475871a2008-07-27 21:46:04 +00008510 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008511 DAG.getNode(ISD::AND, DL, MVT::i16,
8512 DAG.getNode(ISD::ADD, DL, MVT::i16,
8513 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008514 DAG.getConstant(1, MVT::i16)),
8515 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008516
8517
Duncan Sands83ec4b62008-06-06 12:08:01 +00008518 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008519 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008520}
8521
Dan Gohmand858e902010-04-17 15:26:15 +00008522SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008523 EVT VT = Op.getValueType();
8524 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008525 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008526 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008527
8528 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008529 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008530 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008531 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008532 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008533 }
Evan Cheng18efe262007-12-14 02:13:44 +00008534
Evan Cheng152804e2007-12-14 08:30:15 +00008535 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008536 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008537 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008538
8539 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008540 SDValue Ops[] = {
8541 Op,
8542 DAG.getConstant(NumBits+NumBits-1, OpVT),
8543 DAG.getConstant(X86::COND_E, MVT::i8),
8544 Op.getValue(1)
8545 };
8546 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008547
8548 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008549 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008550
Owen Anderson825b72b2009-08-11 20:47:22 +00008551 if (VT == MVT::i8)
8552 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008553 return Op;
8554}
8555
Dan Gohmand858e902010-04-17 15:26:15 +00008556SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008557 EVT VT = Op.getValueType();
8558 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008559 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008560 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008561
8562 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008563 if (VT == MVT::i8) {
8564 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008565 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008566 }
Evan Cheng152804e2007-12-14 08:30:15 +00008567
8568 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008569 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008570 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008571
8572 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008573 SDValue Ops[] = {
8574 Op,
8575 DAG.getConstant(NumBits, OpVT),
8576 DAG.getConstant(X86::COND_E, MVT::i8),
8577 Op.getValue(1)
8578 };
8579 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008580
Owen Anderson825b72b2009-08-11 20:47:22 +00008581 if (VT == MVT::i8)
8582 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008583 return Op;
8584}
8585
Dan Gohmand858e902010-04-17 15:26:15 +00008586SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008587 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008588 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008589 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008590
Mon P Wangaf9b9522008-12-18 21:42:19 +00008591 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8592 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8593 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8594 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8595 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8596 //
8597 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8598 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8599 // return AloBlo + AloBhi + AhiBlo;
8600
8601 SDValue A = Op.getOperand(0);
8602 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008603
Dale Johannesene4d209d2009-02-03 20:21:25 +00008604 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008605 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8606 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008607 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008608 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8609 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008610 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008611 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008612 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008613 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008614 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008615 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008616 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008617 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008618 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008619 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008620 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8621 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008622 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008623 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8624 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008625 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8626 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008627 return Res;
8628}
8629
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008630SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8631 EVT VT = Op.getValueType();
8632 DebugLoc dl = Op.getDebugLoc();
8633 SDValue R = Op.getOperand(0);
8634
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008635 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008636
Nate Begeman51409212010-07-28 00:21:48 +00008637 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8638
8639 if (VT == MVT::v4i32) {
8640 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8641 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8642 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8643
8644 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008645
Nate Begeman51409212010-07-28 00:21:48 +00008646 std::vector<Constant*> CV(4, CI);
8647 Constant *C = ConstantVector::get(CV);
8648 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8649 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008650 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008651 false, false, 16);
8652
8653 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008654 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008655 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8656 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8657 }
8658 if (VT == MVT::v16i8) {
8659 // a = a << 5;
8660 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8661 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8662 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8663
8664 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8665 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8666
8667 std::vector<Constant*> CVM1(16, CM1);
8668 std::vector<Constant*> CVM2(16, CM2);
8669 Constant *C = ConstantVector::get(CVM1);
8670 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8671 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008672 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008673 false, false, 16);
8674
8675 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8676 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8677 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8678 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8679 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008680 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008681 // a += a
8682 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008683
Nate Begeman51409212010-07-28 00:21:48 +00008684 C = ConstantVector::get(CVM2);
8685 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8686 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008687 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008688 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008689
Nate Begeman51409212010-07-28 00:21:48 +00008690 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8691 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8692 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8693 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8694 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008695 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008696 // a += a
8697 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008698
Nate Begeman51409212010-07-28 00:21:48 +00008699 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008700 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008701 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8702 return R;
8703 }
8704 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008705}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008706
Dan Gohmand858e902010-04-17 15:26:15 +00008707SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008708 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8709 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008710 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8711 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008712 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008713 SDValue LHS = N->getOperand(0);
8714 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008715 unsigned BaseOp = 0;
8716 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008717 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008718 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008719 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008720 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008721 // A subtract of one will be selected as a INC. Note that INC doesn't
8722 // set CF, so we can't do this for UADDO.
8723 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8724 if (C->getAPIntValue() == 1) {
8725 BaseOp = X86ISD::INC;
8726 Cond = X86::COND_O;
8727 break;
8728 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008729 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008730 Cond = X86::COND_O;
8731 break;
8732 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008733 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008734 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008735 break;
8736 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008737 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8738 // set CF, so we can't do this for USUBO.
8739 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8740 if (C->getAPIntValue() == 1) {
8741 BaseOp = X86ISD::DEC;
8742 Cond = X86::COND_O;
8743 break;
8744 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008745 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008746 Cond = X86::COND_O;
8747 break;
8748 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008749 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008750 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008751 break;
8752 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008753 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008754 Cond = X86::COND_O;
8755 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008756 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8757 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8758 MVT::i32);
8759 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008760
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008761 SDValue SetCC =
8762 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8763 DAG.getConstant(X86::COND_O, MVT::i32),
8764 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008765
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008766 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8767 return Sum;
8768 }
Bill Wendling74c37652008-12-09 22:08:41 +00008769 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008770
Bill Wendling61edeb52008-12-02 01:06:39 +00008771 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008772 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008773 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008774
Bill Wendling61edeb52008-12-02 01:06:39 +00008775 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008776 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8777 DAG.getConstant(Cond, MVT::i32),
8778 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008779
Bill Wendling61edeb52008-12-02 01:06:39 +00008780 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8781 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008782}
8783
Eric Christopher9a9d2752010-07-22 02:48:34 +00008784SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8785 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008786
Eric Christopherb6729dc2010-08-04 23:03:04 +00008787 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008788 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008789 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008790 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008791 SDValue Ops[] = {
8792 DAG.getRegister(X86::ESP, MVT::i32), // Base
8793 DAG.getTargetConstant(1, MVT::i8), // Scale
8794 DAG.getRegister(0, MVT::i32), // Index
8795 DAG.getTargetConstant(0, MVT::i32), // Disp
8796 DAG.getRegister(0, MVT::i32), // Segment.
8797 Zero,
8798 Chain
8799 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008800 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008801 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8802 array_lengthof(Ops));
8803 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008804 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008805
Eric Christopher9a9d2752010-07-22 02:48:34 +00008806 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008807 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008808 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008809
Chris Lattner132929a2010-08-14 17:26:09 +00008810 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8811 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8812 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8813 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008814
Chris Lattner132929a2010-08-14 17:26:09 +00008815 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8816 if (!Op1 && !Op2 && !Op3 && Op4)
8817 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008818
Chris Lattner132929a2010-08-14 17:26:09 +00008819 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8820 if (Op1 && !Op2 && !Op3 && !Op4)
8821 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008822
8823 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008824 // (MFENCE)>;
8825 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008826}
8827
Dan Gohmand858e902010-04-17 15:26:15 +00008828SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008829 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008830 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008831 unsigned Reg = 0;
8832 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008833 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008834 default:
8835 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008836 case MVT::i8: Reg = X86::AL; size = 1; break;
8837 case MVT::i16: Reg = X86::AX; size = 2; break;
8838 case MVT::i32: Reg = X86::EAX; size = 4; break;
8839 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008840 assert(Subtarget->is64Bit() && "Node not type legal!");
8841 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008842 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008843 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008844 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008845 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008846 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008847 Op.getOperand(1),
8848 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008849 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008850 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008851 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008852 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8853 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8854 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008855 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008856 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008857 return cpOut;
8858}
8859
Duncan Sands1607f052008-12-01 11:39:25 +00008860SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008861 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008862 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008863 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008864 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008865 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008866 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008867 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8868 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008869 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008870 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8871 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008872 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008873 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008874 rdx.getValue(1)
8875 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008876 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008877}
8878
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008879SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008880 SelectionDAG &DAG) const {
8881 EVT SrcVT = Op.getOperand(0).getValueType();
8882 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008883 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8884 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008885 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008886 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008887 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008888 // i64 <=> MMX conversions are Legal.
8889 if (SrcVT==MVT::i64 && DstVT.isVector())
8890 return Op;
8891 if (DstVT==MVT::i64 && SrcVT.isVector())
8892 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008893 // MMX <=> MMX conversions are Legal.
8894 if (SrcVT.isVector() && DstVT.isVector())
8895 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008896 // All other conversions need to be expanded.
8897 return SDValue();
8898}
Chris Lattner5b856542010-12-20 00:59:46 +00008899
Dan Gohmand858e902010-04-17 15:26:15 +00008900SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008901 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008902 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008903 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008904 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008905 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008906 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008907 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008908 Node->getOperand(0),
8909 Node->getOperand(1), negOp,
8910 cast<AtomicSDNode>(Node)->getSrcValue(),
8911 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008912}
8913
Chris Lattner5b856542010-12-20 00:59:46 +00008914static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8915 EVT VT = Op.getNode()->getValueType(0);
8916
8917 // Let legalize expand this if it isn't a legal type yet.
8918 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8919 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008920
Chris Lattner5b856542010-12-20 00:59:46 +00008921 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008922
Chris Lattner5b856542010-12-20 00:59:46 +00008923 unsigned Opc;
8924 bool ExtraOp = false;
8925 switch (Op.getOpcode()) {
8926 default: assert(0 && "Invalid code");
8927 case ISD::ADDC: Opc = X86ISD::ADD; break;
8928 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8929 case ISD::SUBC: Opc = X86ISD::SUB; break;
8930 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8931 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008932
Chris Lattner5b856542010-12-20 00:59:46 +00008933 if (!ExtraOp)
8934 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8935 Op.getOperand(1));
8936 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8937 Op.getOperand(1), Op.getOperand(2));
8938}
8939
Evan Cheng0db9fe62006-04-25 20:13:52 +00008940/// LowerOperation - Provide custom lowering hooks for some operations.
8941///
Dan Gohmand858e902010-04-17 15:26:15 +00008942SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008943 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008944 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008945 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008946 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8947 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008948 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008949 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008950 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8951 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8952 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008953 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008954 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008955 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8956 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8957 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008958 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008959 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008960 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008961 case ISD::SHL_PARTS:
8962 case ISD::SRA_PARTS:
8963 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8964 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008965 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008966 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008967 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008968 case ISD::FABS: return LowerFABS(Op, DAG);
8969 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008970 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008971 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008972 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008973 case ISD::SELECT: return LowerSELECT(Op, DAG);
8974 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008975 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008976 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008977 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008978 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008979 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008980 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8981 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008982 case ISD::FRAME_TO_ARGS_OFFSET:
8983 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008984 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008985 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008986 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008987 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008988 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8989 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008990 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008991 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008992 case ISD::SADDO:
8993 case ISD::UADDO:
8994 case ISD::SSUBO:
8995 case ISD::USUBO:
8996 case ISD::SMULO:
8997 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008998 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008999 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009000 case ISD::ADDC:
9001 case ISD::ADDE:
9002 case ISD::SUBC:
9003 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009004 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009005}
9006
Duncan Sands1607f052008-12-01 11:39:25 +00009007void X86TargetLowering::
9008ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009009 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009010 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009011 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009012 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009013
9014 SDValue Chain = Node->getOperand(0);
9015 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009016 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009017 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009018 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009019 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009020 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009021 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009022 SDValue Result =
9023 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9024 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009025 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009026 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009027 Results.push_back(Result.getValue(2));
9028}
9029
Duncan Sands126d9072008-07-04 11:47:58 +00009030/// ReplaceNodeResults - Replace a node with an illegal result type
9031/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009032void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9033 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009034 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009035 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009036 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009037 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009038 assert(false && "Do not know how to custom type legalize this operation!");
9039 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009040 case ISD::ADDC:
9041 case ISD::ADDE:
9042 case ISD::SUBC:
9043 case ISD::SUBE:
9044 // We don't want to expand or promote these.
9045 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009046 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009047 std::pair<SDValue,SDValue> Vals =
9048 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009049 SDValue FIST = Vals.first, StackSlot = Vals.second;
9050 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009051 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009052 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009053 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9054 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009055 }
9056 return;
9057 }
9058 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009059 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009060 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009061 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009062 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009063 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009064 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009065 eax.getValue(2));
9066 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9067 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009068 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009069 Results.push_back(edx.getValue(1));
9070 return;
9071 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009072 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009073 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009074 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009075 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009076 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9077 DAG.getConstant(0, MVT::i32));
9078 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9079 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009080 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9081 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009082 cpInL.getValue(1));
9083 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009084 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9085 DAG.getConstant(0, MVT::i32));
9086 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9087 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009088 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009089 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009090 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009091 swapInL.getValue(1));
9092 SDValue Ops[] = { swapInH.getValue(0),
9093 N->getOperand(1),
9094 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009095 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009096 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9097 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9098 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009099 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009100 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009101 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009102 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009103 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009104 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009105 Results.push_back(cpOutH.getValue(1));
9106 return;
9107 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009108 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009109 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9110 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009111 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009112 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9113 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009114 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009115 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9116 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009117 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009118 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9119 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009120 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009121 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9122 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009123 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009124 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9125 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009126 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009127 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9128 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009129 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009130}
9131
Evan Cheng72261582005-12-20 06:22:03 +00009132const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9133 switch (Opcode) {
9134 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009135 case X86ISD::BSF: return "X86ISD::BSF";
9136 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009137 case X86ISD::SHLD: return "X86ISD::SHLD";
9138 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009139 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009140 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009141 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009142 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009143 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009144 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009145 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9146 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9147 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009148 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009149 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009150 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009151 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009152 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009153 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009154 case X86ISD::COMI: return "X86ISD::COMI";
9155 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009156 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009157 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009158 case X86ISD::CMOV: return "X86ISD::CMOV";
9159 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009160 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009161 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9162 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009163 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009164 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009165 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009166 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009167 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009168 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9169 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009170 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009171 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009172 case X86ISD::PANDN: return "X86ISD::PANDN";
9173 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9174 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9175 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009176 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009177 case X86ISD::FMAX: return "X86ISD::FMAX";
9178 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009179 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9180 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009181 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009182 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009183 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009184 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009185 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009186 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9187 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009188 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9189 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9190 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9191 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9192 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9193 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009194 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9195 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009196 case X86ISD::VSHL: return "X86ISD::VSHL";
9197 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009198 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9199 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9200 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9201 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9202 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9203 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9204 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9205 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9206 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9207 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009208 case X86ISD::ADD: return "X86ISD::ADD";
9209 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009210 case X86ISD::ADC: return "X86ISD::ADC";
9211 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009212 case X86ISD::SMUL: return "X86ISD::SMUL";
9213 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009214 case X86ISD::INC: return "X86ISD::INC";
9215 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009216 case X86ISD::OR: return "X86ISD::OR";
9217 case X86ISD::XOR: return "X86ISD::XOR";
9218 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009219 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009220 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009221 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009222 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9223 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9224 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9225 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9226 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9227 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9228 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9229 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9230 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009231 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009232 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009233 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009234 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9235 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009236 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9237 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9238 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9239 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9240 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9241 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9242 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9243 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9244 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
9245 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9246 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9247 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9248 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9249 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9250 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9251 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9252 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9253 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9254 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009255 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009256 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009257 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009258 }
9259}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009260
Chris Lattnerc9addb72007-03-30 23:15:24 +00009261// isLegalAddressingMode - Return true if the addressing mode represented
9262// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009263bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009264 const Type *Ty) const {
9265 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009266 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009267 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009268
Chris Lattnerc9addb72007-03-30 23:15:24 +00009269 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009270 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009271 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009272
Chris Lattnerc9addb72007-03-30 23:15:24 +00009273 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009274 unsigned GVFlags =
9275 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009276
Chris Lattnerdfed4132009-07-10 07:38:24 +00009277 // If a reference to this global requires an extra load, we can't fold it.
9278 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009279 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009280
Chris Lattnerdfed4132009-07-10 07:38:24 +00009281 // If BaseGV requires a register for the PIC base, we cannot also have a
9282 // BaseReg specified.
9283 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009284 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009285
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009286 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009287 if ((M != CodeModel::Small || R != Reloc::Static) &&
9288 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009289 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009291
Chris Lattnerc9addb72007-03-30 23:15:24 +00009292 switch (AM.Scale) {
9293 case 0:
9294 case 1:
9295 case 2:
9296 case 4:
9297 case 8:
9298 // These scales always work.
9299 break;
9300 case 3:
9301 case 5:
9302 case 9:
9303 // These scales are formed with basereg+scalereg. Only accept if there is
9304 // no basereg yet.
9305 if (AM.HasBaseReg)
9306 return false;
9307 break;
9308 default: // Other stuff never works.
9309 return false;
9310 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009311
Chris Lattnerc9addb72007-03-30 23:15:24 +00009312 return true;
9313}
9314
9315
Evan Cheng2bd122c2007-10-26 01:56:11 +00009316bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009317 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009318 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009319 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9320 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009321 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009322 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009323 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009324}
9325
Owen Andersone50ed302009-08-10 22:56:29 +00009326bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009327 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009328 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009329 unsigned NumBits1 = VT1.getSizeInBits();
9330 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009331 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009332 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009333 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009334}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009335
Dan Gohman97121ba2009-04-08 00:15:30 +00009336bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009337 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009338 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009339}
9340
Owen Andersone50ed302009-08-10 22:56:29 +00009341bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009342 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009343 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009344}
9345
Owen Andersone50ed302009-08-10 22:56:29 +00009346bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009347 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009348 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009349}
9350
Evan Cheng60c07e12006-07-05 22:17:51 +00009351/// isShuffleMaskLegal - Targets can use this to indicate that they only
9352/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9353/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9354/// are assumed to be legal.
9355bool
Eric Christopherfd179292009-08-27 18:07:15 +00009356X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009357 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009358 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009359 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009360 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009361
Nate Begemana09008b2009-10-19 02:17:23 +00009362 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009363 return (VT.getVectorNumElements() == 2 ||
9364 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9365 isMOVLMask(M, VT) ||
9366 isSHUFPMask(M, VT) ||
9367 isPSHUFDMask(M, VT) ||
9368 isPSHUFHWMask(M, VT) ||
9369 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009370 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009371 isUNPCKLMask(M, VT) ||
9372 isUNPCKHMask(M, VT) ||
9373 isUNPCKL_v_undef_Mask(M, VT) ||
9374 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009375}
9376
Dan Gohman7d8143f2008-04-09 20:09:42 +00009377bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009378X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009379 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009380 unsigned NumElts = VT.getVectorNumElements();
9381 // FIXME: This collection of masks seems suspect.
9382 if (NumElts == 2)
9383 return true;
9384 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9385 return (isMOVLMask(Mask, VT) ||
9386 isCommutedMOVLMask(Mask, VT, true) ||
9387 isSHUFPMask(Mask, VT) ||
9388 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009389 }
9390 return false;
9391}
9392
9393//===----------------------------------------------------------------------===//
9394// X86 Scheduler Hooks
9395//===----------------------------------------------------------------------===//
9396
Mon P Wang63307c32008-05-05 19:05:59 +00009397// private utility function
9398MachineBasicBlock *
9399X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9400 MachineBasicBlock *MBB,
9401 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009402 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009403 unsigned LoadOpc,
9404 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009405 unsigned notOpc,
9406 unsigned EAXreg,
9407 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009408 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009409 // For the atomic bitwise operator, we generate
9410 // thisMBB:
9411 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009412 // ld t1 = [bitinstr.addr]
9413 // op t2 = t1, [bitinstr.val]
9414 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009415 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9416 // bz newMBB
9417 // fallthrough -->nextMBB
9418 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9419 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009420 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009421 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009422
Mon P Wang63307c32008-05-05 19:05:59 +00009423 /// First build the CFG
9424 MachineFunction *F = MBB->getParent();
9425 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009426 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9427 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9428 F->insert(MBBIter, newMBB);
9429 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009430
Dan Gohman14152b42010-07-06 20:24:04 +00009431 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9432 nextMBB->splice(nextMBB->begin(), thisMBB,
9433 llvm::next(MachineBasicBlock::iterator(bInstr)),
9434 thisMBB->end());
9435 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009436
Mon P Wang63307c32008-05-05 19:05:59 +00009437 // Update thisMBB to fall through to newMBB
9438 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009439
Mon P Wang63307c32008-05-05 19:05:59 +00009440 // newMBB jumps to itself and fall through to nextMBB
9441 newMBB->addSuccessor(nextMBB);
9442 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009443
Mon P Wang63307c32008-05-05 19:05:59 +00009444 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009445 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009446 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009447 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009448 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009449 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009450 int numArgs = bInstr->getNumOperands() - 1;
9451 for (int i=0; i < numArgs; ++i)
9452 argOpers[i] = &bInstr->getOperand(i+1);
9453
9454 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009455 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009456 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009457
Dale Johannesen140be2d2008-08-19 18:47:28 +00009458 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009459 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009460 for (int i=0; i <= lastAddrIndx; ++i)
9461 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009462
Dale Johannesen140be2d2008-08-19 18:47:28 +00009463 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009464 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009465 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009467 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009468 tt = t1;
9469
Dale Johannesen140be2d2008-08-19 18:47:28 +00009470 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009471 assert((argOpers[valArgIndx]->isReg() ||
9472 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009473 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009474 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009475 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009476 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009477 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009478 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009479 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009480
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009481 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009482 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009483
Dale Johannesene4d209d2009-02-03 20:21:25 +00009484 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009485 for (int i=0; i <= lastAddrIndx; ++i)
9486 (*MIB).addOperand(*argOpers[i]);
9487 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009488 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009489 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9490 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009491
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009492 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009493 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009494
Mon P Wang63307c32008-05-05 19:05:59 +00009495 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009496 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009497
Dan Gohman14152b42010-07-06 20:24:04 +00009498 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009499 return nextMBB;
9500}
9501
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009502// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009503MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009504X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9505 MachineBasicBlock *MBB,
9506 unsigned regOpcL,
9507 unsigned regOpcH,
9508 unsigned immOpcL,
9509 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009510 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009511 // For the atomic bitwise operator, we generate
9512 // thisMBB (instructions are in pairs, except cmpxchg8b)
9513 // ld t1,t2 = [bitinstr.addr]
9514 // newMBB:
9515 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9516 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009517 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009518 // mov ECX, EBX <- t5, t6
9519 // mov EAX, EDX <- t1, t2
9520 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9521 // mov t3, t4 <- EAX, EDX
9522 // bz newMBB
9523 // result in out1, out2
9524 // fallthrough -->nextMBB
9525
9526 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9527 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009528 const unsigned NotOpc = X86::NOT32r;
9529 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9530 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9531 MachineFunction::iterator MBBIter = MBB;
9532 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009533
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009534 /// First build the CFG
9535 MachineFunction *F = MBB->getParent();
9536 MachineBasicBlock *thisMBB = MBB;
9537 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9538 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9539 F->insert(MBBIter, newMBB);
9540 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009541
Dan Gohman14152b42010-07-06 20:24:04 +00009542 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9543 nextMBB->splice(nextMBB->begin(), thisMBB,
9544 llvm::next(MachineBasicBlock::iterator(bInstr)),
9545 thisMBB->end());
9546 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009547
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009548 // Update thisMBB to fall through to newMBB
9549 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009550
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009551 // newMBB jumps to itself and fall through to nextMBB
9552 newMBB->addSuccessor(nextMBB);
9553 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009554
Dale Johannesene4d209d2009-02-03 20:21:25 +00009555 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009556 // Insert instructions into newMBB based on incoming instruction
9557 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009558 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009559 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009560 MachineOperand& dest1Oper = bInstr->getOperand(0);
9561 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009562 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9563 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009564 argOpers[i] = &bInstr->getOperand(i+2);
9565
Dan Gohman71ea4e52010-05-14 21:01:44 +00009566 // We use some of the operands multiple times, so conservatively just
9567 // clear any kill flags that might be present.
9568 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9569 argOpers[i]->setIsKill(false);
9570 }
9571
Evan Chengad5b52f2010-01-08 19:14:57 +00009572 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009573 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009574
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009575 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009576 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009577 for (int i=0; i <= lastAddrIndx; ++i)
9578 (*MIB).addOperand(*argOpers[i]);
9579 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009580 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009581 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009582 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009583 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009584 MachineOperand newOp3 = *(argOpers[3]);
9585 if (newOp3.isImm())
9586 newOp3.setImm(newOp3.getImm()+4);
9587 else
9588 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009589 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009590 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009591
9592 // t3/4 are defined later, at the bottom of the loop
9593 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9594 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009595 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009596 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009597 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009598 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9599
Evan Cheng306b4ca2010-01-08 23:41:50 +00009600 // The subsequent operations should be using the destination registers of
9601 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009602 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009603 t1 = F->getRegInfo().createVirtualRegister(RC);
9604 t2 = F->getRegInfo().createVirtualRegister(RC);
9605 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9606 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009607 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009608 t1 = dest1Oper.getReg();
9609 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009610 }
9611
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009612 int valArgIndx = lastAddrIndx + 1;
9613 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009614 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009615 "invalid operand");
9616 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9617 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009618 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009619 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009620 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009621 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009622 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009623 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009624 (*MIB).addOperand(*argOpers[valArgIndx]);
9625 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009626 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009627 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009628 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009629 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009630 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009631 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009632 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009633 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009634 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009635 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009636
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009637 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009638 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009639 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009640 MIB.addReg(t2);
9641
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009642 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009643 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009644 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009645 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009646
Dale Johannesene4d209d2009-02-03 20:21:25 +00009647 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009648 for (int i=0; i <= lastAddrIndx; ++i)
9649 (*MIB).addOperand(*argOpers[i]);
9650
9651 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009652 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9653 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009654
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009655 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009656 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009657 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009658 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009659
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009660 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009661 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009662
Dan Gohman14152b42010-07-06 20:24:04 +00009663 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009664 return nextMBB;
9665}
9666
9667// private utility function
9668MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009669X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9670 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009671 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009672 // For the atomic min/max operator, we generate
9673 // thisMBB:
9674 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009675 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009676 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009677 // cmp t1, t2
9678 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009679 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009680 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9681 // bz newMBB
9682 // fallthrough -->nextMBB
9683 //
9684 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9685 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009686 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009687 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009688
Mon P Wang63307c32008-05-05 19:05:59 +00009689 /// First build the CFG
9690 MachineFunction *F = MBB->getParent();
9691 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009692 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9693 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9694 F->insert(MBBIter, newMBB);
9695 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009696
Dan Gohman14152b42010-07-06 20:24:04 +00009697 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9698 nextMBB->splice(nextMBB->begin(), thisMBB,
9699 llvm::next(MachineBasicBlock::iterator(mInstr)),
9700 thisMBB->end());
9701 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009702
Mon P Wang63307c32008-05-05 19:05:59 +00009703 // Update thisMBB to fall through to newMBB
9704 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009705
Mon P Wang63307c32008-05-05 19:05:59 +00009706 // newMBB jumps to newMBB and fall through to nextMBB
9707 newMBB->addSuccessor(nextMBB);
9708 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009709
Dale Johannesene4d209d2009-02-03 20:21:25 +00009710 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009711 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009712 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009713 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009714 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009715 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009716 int numArgs = mInstr->getNumOperands() - 1;
9717 for (int i=0; i < numArgs; ++i)
9718 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009719
Mon P Wang63307c32008-05-05 19:05:59 +00009720 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009721 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009722 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009723
Mon P Wangab3e7472008-05-05 22:56:23 +00009724 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009725 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009726 for (int i=0; i <= lastAddrIndx; ++i)
9727 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009728
Mon P Wang63307c32008-05-05 19:05:59 +00009729 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009730 assert((argOpers[valArgIndx]->isReg() ||
9731 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009732 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009733
9734 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009735 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009736 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009737 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009738 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009739 (*MIB).addOperand(*argOpers[valArgIndx]);
9740
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009741 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009742 MIB.addReg(t1);
9743
Dale Johannesene4d209d2009-02-03 20:21:25 +00009744 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009745 MIB.addReg(t1);
9746 MIB.addReg(t2);
9747
9748 // Generate movc
9749 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009750 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009751 MIB.addReg(t2);
9752 MIB.addReg(t1);
9753
9754 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009755 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009756 for (int i=0; i <= lastAddrIndx; ++i)
9757 (*MIB).addOperand(*argOpers[i]);
9758 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009759 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009760 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9761 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009762
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009763 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009764 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009765
Mon P Wang63307c32008-05-05 19:05:59 +00009766 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009767 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009768
Dan Gohman14152b42010-07-06 20:24:04 +00009769 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009770 return nextMBB;
9771}
9772
Eric Christopherf83a5de2009-08-27 18:08:16 +00009773// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009774// or XMM0_V32I8 in AVX all of this code can be replaced with that
9775// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009776MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009777X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009778 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009779 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9780 "Target must have SSE4.2 or AVX features enabled");
9781
Eric Christopherb120ab42009-08-18 22:50:32 +00009782 DebugLoc dl = MI->getDebugLoc();
9783 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009784 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009785 if (!Subtarget->hasAVX()) {
9786 if (memArg)
9787 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9788 else
9789 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9790 } else {
9791 if (memArg)
9792 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9793 else
9794 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9795 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009796
Eric Christopher41c902f2010-11-30 08:20:21 +00009797 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009798 for (unsigned i = 0; i < numArgs; ++i) {
9799 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009800 if (!(Op.isReg() && Op.isImplicit()))
9801 MIB.addOperand(Op);
9802 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009803 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009804 .addReg(X86::XMM0);
9805
Dan Gohman14152b42010-07-06 20:24:04 +00009806 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009807 return BB;
9808}
9809
9810MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009811X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009812 DebugLoc dl = MI->getDebugLoc();
9813 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009814
Eric Christopher228232b2010-11-30 07:20:12 +00009815 // Address into RAX/EAX, other two args into ECX, EDX.
9816 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9817 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9818 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9819 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009820 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009821
Eric Christopher228232b2010-11-30 07:20:12 +00009822 unsigned ValOps = X86::AddrNumOperands;
9823 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9824 .addReg(MI->getOperand(ValOps).getReg());
9825 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9826 .addReg(MI->getOperand(ValOps+1).getReg());
9827
9828 // The instruction doesn't actually take any operands though.
9829 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009830
Eric Christopher228232b2010-11-30 07:20:12 +00009831 MI->eraseFromParent(); // The pseudo is gone now.
9832 return BB;
9833}
9834
9835MachineBasicBlock *
9836X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009837 DebugLoc dl = MI->getDebugLoc();
9838 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009839
Eric Christopher228232b2010-11-30 07:20:12 +00009840 // First arg in ECX, the second in EAX.
9841 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9842 .addReg(MI->getOperand(0).getReg());
9843 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9844 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009845
Eric Christopher228232b2010-11-30 07:20:12 +00009846 // The instruction doesn't actually take any operands though.
9847 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009848
Eric Christopher228232b2010-11-30 07:20:12 +00009849 MI->eraseFromParent(); // The pseudo is gone now.
9850 return BB;
9851}
9852
9853MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009854X86TargetLowering::EmitVAARG64WithCustomInserter(
9855 MachineInstr *MI,
9856 MachineBasicBlock *MBB) const {
9857 // Emit va_arg instruction on X86-64.
9858
9859 // Operands to this pseudo-instruction:
9860 // 0 ) Output : destination address (reg)
9861 // 1-5) Input : va_list address (addr, i64mem)
9862 // 6 ) ArgSize : Size (in bytes) of vararg type
9863 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9864 // 8 ) Align : Alignment of type
9865 // 9 ) EFLAGS (implicit-def)
9866
9867 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9868 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9869
9870 unsigned DestReg = MI->getOperand(0).getReg();
9871 MachineOperand &Base = MI->getOperand(1);
9872 MachineOperand &Scale = MI->getOperand(2);
9873 MachineOperand &Index = MI->getOperand(3);
9874 MachineOperand &Disp = MI->getOperand(4);
9875 MachineOperand &Segment = MI->getOperand(5);
9876 unsigned ArgSize = MI->getOperand(6).getImm();
9877 unsigned ArgMode = MI->getOperand(7).getImm();
9878 unsigned Align = MI->getOperand(8).getImm();
9879
9880 // Memory Reference
9881 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9882 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9883 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9884
9885 // Machine Information
9886 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9887 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9888 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9889 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9890 DebugLoc DL = MI->getDebugLoc();
9891
9892 // struct va_list {
9893 // i32 gp_offset
9894 // i32 fp_offset
9895 // i64 overflow_area (address)
9896 // i64 reg_save_area (address)
9897 // }
9898 // sizeof(va_list) = 24
9899 // alignment(va_list) = 8
9900
9901 unsigned TotalNumIntRegs = 6;
9902 unsigned TotalNumXMMRegs = 8;
9903 bool UseGPOffset = (ArgMode == 1);
9904 bool UseFPOffset = (ArgMode == 2);
9905 unsigned MaxOffset = TotalNumIntRegs * 8 +
9906 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9907
9908 /* Align ArgSize to a multiple of 8 */
9909 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9910 bool NeedsAlign = (Align > 8);
9911
9912 MachineBasicBlock *thisMBB = MBB;
9913 MachineBasicBlock *overflowMBB;
9914 MachineBasicBlock *offsetMBB;
9915 MachineBasicBlock *endMBB;
9916
9917 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9918 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9919 unsigned OffsetReg = 0;
9920
9921 if (!UseGPOffset && !UseFPOffset) {
9922 // If we only pull from the overflow region, we don't create a branch.
9923 // We don't need to alter control flow.
9924 OffsetDestReg = 0; // unused
9925 OverflowDestReg = DestReg;
9926
9927 offsetMBB = NULL;
9928 overflowMBB = thisMBB;
9929 endMBB = thisMBB;
9930 } else {
9931 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9932 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9933 // If not, pull from overflow_area. (branch to overflowMBB)
9934 //
9935 // thisMBB
9936 // | .
9937 // | .
9938 // offsetMBB overflowMBB
9939 // | .
9940 // | .
9941 // endMBB
9942
9943 // Registers for the PHI in endMBB
9944 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9945 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9946
9947 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9948 MachineFunction *MF = MBB->getParent();
9949 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9950 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9951 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9952
9953 MachineFunction::iterator MBBIter = MBB;
9954 ++MBBIter;
9955
9956 // Insert the new basic blocks
9957 MF->insert(MBBIter, offsetMBB);
9958 MF->insert(MBBIter, overflowMBB);
9959 MF->insert(MBBIter, endMBB);
9960
9961 // Transfer the remainder of MBB and its successor edges to endMBB.
9962 endMBB->splice(endMBB->begin(), thisMBB,
9963 llvm::next(MachineBasicBlock::iterator(MI)),
9964 thisMBB->end());
9965 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9966
9967 // Make offsetMBB and overflowMBB successors of thisMBB
9968 thisMBB->addSuccessor(offsetMBB);
9969 thisMBB->addSuccessor(overflowMBB);
9970
9971 // endMBB is a successor of both offsetMBB and overflowMBB
9972 offsetMBB->addSuccessor(endMBB);
9973 overflowMBB->addSuccessor(endMBB);
9974
9975 // Load the offset value into a register
9976 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9977 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9978 .addOperand(Base)
9979 .addOperand(Scale)
9980 .addOperand(Index)
9981 .addDisp(Disp, UseFPOffset ? 4 : 0)
9982 .addOperand(Segment)
9983 .setMemRefs(MMOBegin, MMOEnd);
9984
9985 // Check if there is enough room left to pull this argument.
9986 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9987 .addReg(OffsetReg)
9988 .addImm(MaxOffset + 8 - ArgSizeA8);
9989
9990 // Branch to "overflowMBB" if offset >= max
9991 // Fall through to "offsetMBB" otherwise
9992 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9993 .addMBB(overflowMBB);
9994 }
9995
9996 // In offsetMBB, emit code to use the reg_save_area.
9997 if (offsetMBB) {
9998 assert(OffsetReg != 0);
9999
10000 // Read the reg_save_area address.
10001 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10002 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10003 .addOperand(Base)
10004 .addOperand(Scale)
10005 .addOperand(Index)
10006 .addDisp(Disp, 16)
10007 .addOperand(Segment)
10008 .setMemRefs(MMOBegin, MMOEnd);
10009
10010 // Zero-extend the offset
10011 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10012 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10013 .addImm(0)
10014 .addReg(OffsetReg)
10015 .addImm(X86::sub_32bit);
10016
10017 // Add the offset to the reg_save_area to get the final address.
10018 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10019 .addReg(OffsetReg64)
10020 .addReg(RegSaveReg);
10021
10022 // Compute the offset for the next argument
10023 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10024 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10025 .addReg(OffsetReg)
10026 .addImm(UseFPOffset ? 16 : 8);
10027
10028 // Store it back into the va_list.
10029 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10030 .addOperand(Base)
10031 .addOperand(Scale)
10032 .addOperand(Index)
10033 .addDisp(Disp, UseFPOffset ? 4 : 0)
10034 .addOperand(Segment)
10035 .addReg(NextOffsetReg)
10036 .setMemRefs(MMOBegin, MMOEnd);
10037
10038 // Jump to endMBB
10039 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10040 .addMBB(endMBB);
10041 }
10042
10043 //
10044 // Emit code to use overflow area
10045 //
10046
10047 // Load the overflow_area address into a register.
10048 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10049 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10050 .addOperand(Base)
10051 .addOperand(Scale)
10052 .addOperand(Index)
10053 .addDisp(Disp, 8)
10054 .addOperand(Segment)
10055 .setMemRefs(MMOBegin, MMOEnd);
10056
10057 // If we need to align it, do so. Otherwise, just copy the address
10058 // to OverflowDestReg.
10059 if (NeedsAlign) {
10060 // Align the overflow address
10061 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10062 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10063
10064 // aligned_addr = (addr + (align-1)) & ~(align-1)
10065 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10066 .addReg(OverflowAddrReg)
10067 .addImm(Align-1);
10068
10069 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10070 .addReg(TmpReg)
10071 .addImm(~(uint64_t)(Align-1));
10072 } else {
10073 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10074 .addReg(OverflowAddrReg);
10075 }
10076
10077 // Compute the next overflow address after this argument.
10078 // (the overflow address should be kept 8-byte aligned)
10079 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10080 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10081 .addReg(OverflowDestReg)
10082 .addImm(ArgSizeA8);
10083
10084 // Store the new overflow address.
10085 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10086 .addOperand(Base)
10087 .addOperand(Scale)
10088 .addOperand(Index)
10089 .addDisp(Disp, 8)
10090 .addOperand(Segment)
10091 .addReg(NextAddrReg)
10092 .setMemRefs(MMOBegin, MMOEnd);
10093
10094 // If we branched, emit the PHI to the front of endMBB.
10095 if (offsetMBB) {
10096 BuildMI(*endMBB, endMBB->begin(), DL,
10097 TII->get(X86::PHI), DestReg)
10098 .addReg(OffsetDestReg).addMBB(offsetMBB)
10099 .addReg(OverflowDestReg).addMBB(overflowMBB);
10100 }
10101
10102 // Erase the pseudo instruction
10103 MI->eraseFromParent();
10104
10105 return endMBB;
10106}
10107
10108MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010109X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10110 MachineInstr *MI,
10111 MachineBasicBlock *MBB) const {
10112 // Emit code to save XMM registers to the stack. The ABI says that the
10113 // number of registers to save is given in %al, so it's theoretically
10114 // possible to do an indirect jump trick to avoid saving all of them,
10115 // however this code takes a simpler approach and just executes all
10116 // of the stores if %al is non-zero. It's less code, and it's probably
10117 // easier on the hardware branch predictor, and stores aren't all that
10118 // expensive anyway.
10119
10120 // Create the new basic blocks. One block contains all the XMM stores,
10121 // and one block is the final destination regardless of whether any
10122 // stores were performed.
10123 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10124 MachineFunction *F = MBB->getParent();
10125 MachineFunction::iterator MBBIter = MBB;
10126 ++MBBIter;
10127 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10128 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10129 F->insert(MBBIter, XMMSaveMBB);
10130 F->insert(MBBIter, EndMBB);
10131
Dan Gohman14152b42010-07-06 20:24:04 +000010132 // Transfer the remainder of MBB and its successor edges to EndMBB.
10133 EndMBB->splice(EndMBB->begin(), MBB,
10134 llvm::next(MachineBasicBlock::iterator(MI)),
10135 MBB->end());
10136 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10137
Dan Gohmand6708ea2009-08-15 01:38:56 +000010138 // The original block will now fall through to the XMM save block.
10139 MBB->addSuccessor(XMMSaveMBB);
10140 // The XMMSaveMBB will fall through to the end block.
10141 XMMSaveMBB->addSuccessor(EndMBB);
10142
10143 // Now add the instructions.
10144 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10145 DebugLoc DL = MI->getDebugLoc();
10146
10147 unsigned CountReg = MI->getOperand(0).getReg();
10148 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10149 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10150
10151 if (!Subtarget->isTargetWin64()) {
10152 // If %al is 0, branch around the XMM save block.
10153 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010154 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010155 MBB->addSuccessor(EndMBB);
10156 }
10157
10158 // In the XMM save block, save all the XMM argument registers.
10159 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10160 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010161 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010162 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010163 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010164 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010165 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010166 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10167 .addFrameIndex(RegSaveFrameIndex)
10168 .addImm(/*Scale=*/1)
10169 .addReg(/*IndexReg=*/0)
10170 .addImm(/*Disp=*/Offset)
10171 .addReg(/*Segment=*/0)
10172 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010173 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010174 }
10175
Dan Gohman14152b42010-07-06 20:24:04 +000010176 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010177
10178 return EndMBB;
10179}
Mon P Wang63307c32008-05-05 19:05:59 +000010180
Evan Cheng60c07e12006-07-05 22:17:51 +000010181MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010182X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010183 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010184 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10185 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010186
Chris Lattner52600972009-09-02 05:57:00 +000010187 // To "insert" a SELECT_CC instruction, we actually have to insert the
10188 // diamond control-flow pattern. The incoming instruction knows the
10189 // destination vreg to set, the condition code register to branch on, the
10190 // true/false values to select between, and a branch opcode to use.
10191 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10192 MachineFunction::iterator It = BB;
10193 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010194
Chris Lattner52600972009-09-02 05:57:00 +000010195 // thisMBB:
10196 // ...
10197 // TrueVal = ...
10198 // cmpTY ccX, r1, r2
10199 // bCC copy1MBB
10200 // fallthrough --> copy0MBB
10201 MachineBasicBlock *thisMBB = BB;
10202 MachineFunction *F = BB->getParent();
10203 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10204 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010205 F->insert(It, copy0MBB);
10206 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010207
Bill Wendling730c07e2010-06-25 20:48:10 +000010208 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10209 // live into the sink and copy blocks.
10210 const MachineFunction *MF = BB->getParent();
10211 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10212 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010213
Dan Gohman14152b42010-07-06 20:24:04 +000010214 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10215 const MachineOperand &MO = MI->getOperand(I);
10216 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010217 unsigned Reg = MO.getReg();
10218 if (Reg != X86::EFLAGS) continue;
10219 copy0MBB->addLiveIn(Reg);
10220 sinkMBB->addLiveIn(Reg);
10221 }
10222
Dan Gohman14152b42010-07-06 20:24:04 +000010223 // Transfer the remainder of BB and its successor edges to sinkMBB.
10224 sinkMBB->splice(sinkMBB->begin(), BB,
10225 llvm::next(MachineBasicBlock::iterator(MI)),
10226 BB->end());
10227 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10228
10229 // Add the true and fallthrough blocks as its successors.
10230 BB->addSuccessor(copy0MBB);
10231 BB->addSuccessor(sinkMBB);
10232
10233 // Create the conditional branch instruction.
10234 unsigned Opc =
10235 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10236 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10237
Chris Lattner52600972009-09-02 05:57:00 +000010238 // copy0MBB:
10239 // %FalseValue = ...
10240 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010241 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010242
Chris Lattner52600972009-09-02 05:57:00 +000010243 // sinkMBB:
10244 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10245 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010246 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10247 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010248 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10249 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10250
Dan Gohman14152b42010-07-06 20:24:04 +000010251 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010252 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010253}
10254
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010255MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010256X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010257 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010258 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10259 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010260
10261 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10262 // non-trivial part is impdef of ESP.
10263 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10264 // mingw-w64.
10265
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010266 const char *StackProbeSymbol =
10267 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10268
Dan Gohman14152b42010-07-06 20:24:04 +000010269 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010270 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010271 .addReg(X86::EAX, RegState::Implicit)
10272 .addReg(X86::ESP, RegState::Implicit)
10273 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010274 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10275 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010276
Dan Gohman14152b42010-07-06 20:24:04 +000010277 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010278 return BB;
10279}
Chris Lattner52600972009-09-02 05:57:00 +000010280
10281MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010282X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10283 MachineBasicBlock *BB) const {
10284 // This is pretty easy. We're taking the value that we received from
10285 // our load from the relocation, sticking it in either RDI (x86-64)
10286 // or EAX and doing an indirect call. The return value will then
10287 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010288 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010289 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010290 DebugLoc DL = MI->getDebugLoc();
10291 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010292
10293 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010294 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010295
Eric Christopher30ef0e52010-06-03 04:07:48 +000010296 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010297 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10298 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010299 .addReg(X86::RIP)
10300 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010301 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010302 MI->getOperand(3).getTargetFlags())
10303 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010304 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010305 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010306 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010307 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10308 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010309 .addReg(0)
10310 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010311 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010312 MI->getOperand(3).getTargetFlags())
10313 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010314 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010315 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010316 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010317 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10318 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010319 .addReg(TII->getGlobalBaseReg(F))
10320 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010321 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010322 MI->getOperand(3).getTargetFlags())
10323 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010324 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010325 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010326 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010327
Dan Gohman14152b42010-07-06 20:24:04 +000010328 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010329 return BB;
10330}
10331
10332MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010333X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010334 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010335 switch (MI->getOpcode()) {
10336 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010337 case X86::TAILJMPd64:
10338 case X86::TAILJMPr64:
10339 case X86::TAILJMPm64:
10340 assert(!"TAILJMP64 would not be touched here.");
10341 case X86::TCRETURNdi64:
10342 case X86::TCRETURNri64:
10343 case X86::TCRETURNmi64:
10344 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10345 // On AMD64, additional defs should be added before register allocation.
10346 if (!Subtarget->isTargetWin64()) {
10347 MI->addRegisterDefined(X86::RSI);
10348 MI->addRegisterDefined(X86::RDI);
10349 MI->addRegisterDefined(X86::XMM6);
10350 MI->addRegisterDefined(X86::XMM7);
10351 MI->addRegisterDefined(X86::XMM8);
10352 MI->addRegisterDefined(X86::XMM9);
10353 MI->addRegisterDefined(X86::XMM10);
10354 MI->addRegisterDefined(X86::XMM11);
10355 MI->addRegisterDefined(X86::XMM12);
10356 MI->addRegisterDefined(X86::XMM13);
10357 MI->addRegisterDefined(X86::XMM14);
10358 MI->addRegisterDefined(X86::XMM15);
10359 }
10360 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010361 case X86::WIN_ALLOCA:
10362 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010363 case X86::TLSCall_32:
10364 case X86::TLSCall_64:
10365 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010366 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010367 case X86::CMOV_FR32:
10368 case X86::CMOV_FR64:
10369 case X86::CMOV_V4F32:
10370 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010371 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010372 case X86::CMOV_GR16:
10373 case X86::CMOV_GR32:
10374 case X86::CMOV_RFP32:
10375 case X86::CMOV_RFP64:
10376 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010377 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010378
Dale Johannesen849f2142007-07-03 00:53:03 +000010379 case X86::FP32_TO_INT16_IN_MEM:
10380 case X86::FP32_TO_INT32_IN_MEM:
10381 case X86::FP32_TO_INT64_IN_MEM:
10382 case X86::FP64_TO_INT16_IN_MEM:
10383 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010384 case X86::FP64_TO_INT64_IN_MEM:
10385 case X86::FP80_TO_INT16_IN_MEM:
10386 case X86::FP80_TO_INT32_IN_MEM:
10387 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010388 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10389 DebugLoc DL = MI->getDebugLoc();
10390
Evan Cheng60c07e12006-07-05 22:17:51 +000010391 // Change the floating point control register to use "round towards zero"
10392 // mode when truncating to an integer value.
10393 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010394 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010395 addFrameReference(BuildMI(*BB, MI, DL,
10396 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010397
10398 // Load the old value of the high byte of the control word...
10399 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010400 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010401 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010402 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010403
10404 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010405 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010406 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010407
10408 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010409 addFrameReference(BuildMI(*BB, MI, DL,
10410 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010411
10412 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010413 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010414 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010415
10416 // Get the X86 opcode to use.
10417 unsigned Opc;
10418 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010419 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010420 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10421 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10422 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10423 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10424 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10425 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010426 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10427 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10428 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010429 }
10430
10431 X86AddressMode AM;
10432 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010433 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010434 AM.BaseType = X86AddressMode::RegBase;
10435 AM.Base.Reg = Op.getReg();
10436 } else {
10437 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010438 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010439 }
10440 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010441 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010442 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010443 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010444 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010445 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010446 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010447 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010448 AM.GV = Op.getGlobal();
10449 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010450 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010451 }
Dan Gohman14152b42010-07-06 20:24:04 +000010452 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010453 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010454
10455 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010456 addFrameReference(BuildMI(*BB, MI, DL,
10457 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010458
Dan Gohman14152b42010-07-06 20:24:04 +000010459 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010460 return BB;
10461 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010462 // String/text processing lowering.
10463 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010464 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010465 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10466 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010467 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010468 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10469 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010470 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010471 return EmitPCMP(MI, BB, 5, false /* in mem */);
10472 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010473 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010474 return EmitPCMP(MI, BB, 5, true /* in mem */);
10475
Eric Christopher228232b2010-11-30 07:20:12 +000010476 // Thread synchronization.
10477 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010478 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010479 case X86::MWAIT:
10480 return EmitMwait(MI, BB);
10481
Eric Christopherb120ab42009-08-18 22:50:32 +000010482 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010483 case X86::ATOMAND32:
10484 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010485 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010486 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010487 X86::NOT32r, X86::EAX,
10488 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010489 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010490 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10491 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010492 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010493 X86::NOT32r, X86::EAX,
10494 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010495 case X86::ATOMXOR32:
10496 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010497 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010498 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010499 X86::NOT32r, X86::EAX,
10500 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010501 case X86::ATOMNAND32:
10502 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010503 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010504 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010505 X86::NOT32r, X86::EAX,
10506 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010507 case X86::ATOMMIN32:
10508 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10509 case X86::ATOMMAX32:
10510 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10511 case X86::ATOMUMIN32:
10512 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10513 case X86::ATOMUMAX32:
10514 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010515
10516 case X86::ATOMAND16:
10517 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10518 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010519 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010520 X86::NOT16r, X86::AX,
10521 X86::GR16RegisterClass);
10522 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010523 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010524 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010525 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010526 X86::NOT16r, X86::AX,
10527 X86::GR16RegisterClass);
10528 case X86::ATOMXOR16:
10529 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10530 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010531 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010532 X86::NOT16r, X86::AX,
10533 X86::GR16RegisterClass);
10534 case X86::ATOMNAND16:
10535 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10536 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010537 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010538 X86::NOT16r, X86::AX,
10539 X86::GR16RegisterClass, true);
10540 case X86::ATOMMIN16:
10541 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10542 case X86::ATOMMAX16:
10543 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10544 case X86::ATOMUMIN16:
10545 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10546 case X86::ATOMUMAX16:
10547 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10548
10549 case X86::ATOMAND8:
10550 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10551 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010552 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010553 X86::NOT8r, X86::AL,
10554 X86::GR8RegisterClass);
10555 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010556 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010557 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010558 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010559 X86::NOT8r, X86::AL,
10560 X86::GR8RegisterClass);
10561 case X86::ATOMXOR8:
10562 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10563 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010564 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010565 X86::NOT8r, X86::AL,
10566 X86::GR8RegisterClass);
10567 case X86::ATOMNAND8:
10568 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10569 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010570 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010571 X86::NOT8r, X86::AL,
10572 X86::GR8RegisterClass, true);
10573 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010574 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010575 case X86::ATOMAND64:
10576 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010577 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010578 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010579 X86::NOT64r, X86::RAX,
10580 X86::GR64RegisterClass);
10581 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010582 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10583 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010584 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010585 X86::NOT64r, X86::RAX,
10586 X86::GR64RegisterClass);
10587 case X86::ATOMXOR64:
10588 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010589 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010590 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010591 X86::NOT64r, X86::RAX,
10592 X86::GR64RegisterClass);
10593 case X86::ATOMNAND64:
10594 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10595 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010596 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010597 X86::NOT64r, X86::RAX,
10598 X86::GR64RegisterClass, true);
10599 case X86::ATOMMIN64:
10600 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10601 case X86::ATOMMAX64:
10602 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10603 case X86::ATOMUMIN64:
10604 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10605 case X86::ATOMUMAX64:
10606 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010607
10608 // This group does 64-bit operations on a 32-bit host.
10609 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010610 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010611 X86::AND32rr, X86::AND32rr,
10612 X86::AND32ri, X86::AND32ri,
10613 false);
10614 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010615 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010616 X86::OR32rr, X86::OR32rr,
10617 X86::OR32ri, X86::OR32ri,
10618 false);
10619 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010620 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010621 X86::XOR32rr, X86::XOR32rr,
10622 X86::XOR32ri, X86::XOR32ri,
10623 false);
10624 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010625 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010626 X86::AND32rr, X86::AND32rr,
10627 X86::AND32ri, X86::AND32ri,
10628 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010629 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010630 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010631 X86::ADD32rr, X86::ADC32rr,
10632 X86::ADD32ri, X86::ADC32ri,
10633 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010634 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010635 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010636 X86::SUB32rr, X86::SBB32rr,
10637 X86::SUB32ri, X86::SBB32ri,
10638 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010639 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010640 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010641 X86::MOV32rr, X86::MOV32rr,
10642 X86::MOV32ri, X86::MOV32ri,
10643 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010644 case X86::VASTART_SAVE_XMM_REGS:
10645 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010646
10647 case X86::VAARG_64:
10648 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010649 }
10650}
10651
10652//===----------------------------------------------------------------------===//
10653// X86 Optimization Hooks
10654//===----------------------------------------------------------------------===//
10655
Dan Gohman475871a2008-07-27 21:46:04 +000010656void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010657 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010658 APInt &KnownZero,
10659 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010660 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010661 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010662 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010663 assert((Opc >= ISD::BUILTIN_OP_END ||
10664 Opc == ISD::INTRINSIC_WO_CHAIN ||
10665 Opc == ISD::INTRINSIC_W_CHAIN ||
10666 Opc == ISD::INTRINSIC_VOID) &&
10667 "Should use MaskedValueIsZero if you don't know whether Op"
10668 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010669
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010670 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010671 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010672 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010673 case X86ISD::ADD:
10674 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010675 case X86ISD::ADC:
10676 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010677 case X86ISD::SMUL:
10678 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010679 case X86ISD::INC:
10680 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010681 case X86ISD::OR:
10682 case X86ISD::XOR:
10683 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010684 // These nodes' second result is a boolean.
10685 if (Op.getResNo() == 0)
10686 break;
10687 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010688 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010689 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10690 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010691 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010692 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010693}
Chris Lattner259e97c2006-01-31 19:43:35 +000010694
Owen Andersonbc146b02010-09-21 20:42:50 +000010695unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10696 unsigned Depth) const {
10697 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10698 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10699 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010700
Owen Andersonbc146b02010-09-21 20:42:50 +000010701 // Fallback case.
10702 return 1;
10703}
10704
Evan Cheng206ee9d2006-07-07 08:33:52 +000010705/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010706/// node is a GlobalAddress + offset.
10707bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010708 const GlobalValue* &GA,
10709 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010710 if (N->getOpcode() == X86ISD::Wrapper) {
10711 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010712 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010713 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010714 return true;
10715 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010716 }
Evan Chengad4196b2008-05-12 19:56:52 +000010717 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010718}
10719
Evan Cheng206ee9d2006-07-07 08:33:52 +000010720/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10721/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10722/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010723/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010724static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010725 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010726 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010727 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010728
Eli Friedman7a5e5552009-06-07 06:52:44 +000010729 if (VT.getSizeInBits() != 128)
10730 return SDValue();
10731
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010732 // Don't create instructions with illegal types after legalize types has run.
10733 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10734 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10735 return SDValue();
10736
Nate Begemanfdea31a2010-03-24 20:49:50 +000010737 SmallVector<SDValue, 16> Elts;
10738 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010739 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010740
Nate Begemanfdea31a2010-03-24 20:49:50 +000010741 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010742}
Evan Chengd880b972008-05-09 21:53:03 +000010743
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010744/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10745/// generation and convert it from being a bunch of shuffles and extracts
10746/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010747static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10748 const TargetLowering &TLI) {
10749 SDValue InputVector = N->getOperand(0);
10750
10751 // Only operate on vectors of 4 elements, where the alternative shuffling
10752 // gets to be more expensive.
10753 if (InputVector.getValueType() != MVT::v4i32)
10754 return SDValue();
10755
10756 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10757 // single use which is a sign-extend or zero-extend, and all elements are
10758 // used.
10759 SmallVector<SDNode *, 4> Uses;
10760 unsigned ExtractedElements = 0;
10761 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10762 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10763 if (UI.getUse().getResNo() != InputVector.getResNo())
10764 return SDValue();
10765
10766 SDNode *Extract = *UI;
10767 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10768 return SDValue();
10769
10770 if (Extract->getValueType(0) != MVT::i32)
10771 return SDValue();
10772 if (!Extract->hasOneUse())
10773 return SDValue();
10774 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10775 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10776 return SDValue();
10777 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10778 return SDValue();
10779
10780 // Record which element was extracted.
10781 ExtractedElements |=
10782 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10783
10784 Uses.push_back(Extract);
10785 }
10786
10787 // If not all the elements were used, this may not be worthwhile.
10788 if (ExtractedElements != 15)
10789 return SDValue();
10790
10791 // Ok, we've now decided to do the transformation.
10792 DebugLoc dl = InputVector.getDebugLoc();
10793
10794 // Store the value to a temporary stack slot.
10795 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010796 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10797 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010798
10799 // Replace each use (extract) with a load of the appropriate element.
10800 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10801 UE = Uses.end(); UI != UE; ++UI) {
10802 SDNode *Extract = *UI;
10803
10804 // Compute the element's address.
10805 SDValue Idx = Extract->getOperand(1);
10806 unsigned EltSize =
10807 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10808 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10809 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10810
Eric Christopher90eb4022010-07-22 00:26:08 +000010811 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010812 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010813
10814 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010815 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010816 ScalarAddr, MachinePointerInfo(),
10817 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010818
10819 // Replace the exact with the load.
10820 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10821 }
10822
10823 // The replacement was made in place; don't return anything.
10824 return SDValue();
10825}
10826
Chris Lattner83e6c992006-10-04 06:57:07 +000010827/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010828static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010829 const X86Subtarget *Subtarget) {
10830 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010831 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010832 // Get the LHS/RHS of the select.
10833 SDValue LHS = N->getOperand(1);
10834 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010835
Dan Gohman670e5392009-09-21 18:03:22 +000010836 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010837 // instructions match the semantics of the common C idiom x<y?x:y but not
10838 // x<=y?x:y, because of how they handle negative zero (which can be
10839 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010840 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010841 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010842 Cond.getOpcode() == ISD::SETCC) {
10843 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010844
Chris Lattner47b4ce82009-03-11 05:48:52 +000010845 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010846 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010847 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10848 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010849 switch (CC) {
10850 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010851 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010852 // Converting this to a min would handle NaNs incorrectly, and swapping
10853 // the operands would cause it to handle comparisons between positive
10854 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010855 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010856 if (!UnsafeFPMath &&
10857 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10858 break;
10859 std::swap(LHS, RHS);
10860 }
Dan Gohman670e5392009-09-21 18:03:22 +000010861 Opcode = X86ISD::FMIN;
10862 break;
10863 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010864 // Converting this to a min would handle comparisons between positive
10865 // and negative zero incorrectly.
10866 if (!UnsafeFPMath &&
10867 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10868 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010869 Opcode = X86ISD::FMIN;
10870 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010871 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010872 // Converting this to a min would handle both negative zeros and NaNs
10873 // incorrectly, but we can swap the operands to fix both.
10874 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010875 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010876 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010877 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010878 Opcode = X86ISD::FMIN;
10879 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010880
Dan Gohman670e5392009-09-21 18:03:22 +000010881 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010882 // Converting this to a max would handle comparisons between positive
10883 // and negative zero incorrectly.
10884 if (!UnsafeFPMath &&
10885 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10886 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010887 Opcode = X86ISD::FMAX;
10888 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010889 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010890 // Converting this to a max would handle NaNs incorrectly, and swapping
10891 // the operands would cause it to handle comparisons between positive
10892 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010893 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010894 if (!UnsafeFPMath &&
10895 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10896 break;
10897 std::swap(LHS, RHS);
10898 }
Dan Gohman670e5392009-09-21 18:03:22 +000010899 Opcode = X86ISD::FMAX;
10900 break;
10901 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010902 // Converting this to a max would handle both negative zeros and NaNs
10903 // incorrectly, but we can swap the operands to fix both.
10904 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010905 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010906 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010907 case ISD::SETGE:
10908 Opcode = X86ISD::FMAX;
10909 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010910 }
Dan Gohman670e5392009-09-21 18:03:22 +000010911 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010912 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10913 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010914 switch (CC) {
10915 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010916 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010917 // Converting this to a min would handle comparisons between positive
10918 // and negative zero incorrectly, and swapping the operands would
10919 // cause it to handle NaNs incorrectly.
10920 if (!UnsafeFPMath &&
10921 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010922 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010923 break;
10924 std::swap(LHS, RHS);
10925 }
Dan Gohman670e5392009-09-21 18:03:22 +000010926 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010927 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010928 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010929 // Converting this to a min would handle NaNs incorrectly.
10930 if (!UnsafeFPMath &&
10931 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10932 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010933 Opcode = X86ISD::FMIN;
10934 break;
10935 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010936 // Converting this to a min would handle both negative zeros and NaNs
10937 // incorrectly, but we can swap the operands to fix both.
10938 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010939 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010940 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010941 case ISD::SETGE:
10942 Opcode = X86ISD::FMIN;
10943 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010944
Dan Gohman670e5392009-09-21 18:03:22 +000010945 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010946 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010947 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010948 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010949 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010950 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010951 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010952 // Converting this to a max would handle comparisons between positive
10953 // and negative zero incorrectly, and swapping the operands would
10954 // cause it to handle NaNs incorrectly.
10955 if (!UnsafeFPMath &&
10956 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010957 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010958 break;
10959 std::swap(LHS, RHS);
10960 }
Dan Gohman670e5392009-09-21 18:03:22 +000010961 Opcode = X86ISD::FMAX;
10962 break;
10963 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010964 // Converting this to a max would handle both negative zeros and NaNs
10965 // incorrectly, but we can swap the operands to fix both.
10966 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010967 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010968 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010969 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010970 Opcode = X86ISD::FMAX;
10971 break;
10972 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010973 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010974
Chris Lattner47b4ce82009-03-11 05:48:52 +000010975 if (Opcode)
10976 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010977 }
Eric Christopherfd179292009-08-27 18:07:15 +000010978
Chris Lattnerd1980a52009-03-12 06:52:53 +000010979 // If this is a select between two integer constants, try to do some
10980 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010981 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10982 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010983 // Don't do this for crazy integer types.
10984 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10985 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010986 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010987 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010988
Chris Lattnercee56e72009-03-13 05:53:31 +000010989 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010990 // Efficiently invertible.
10991 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10992 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10993 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10994 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010995 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010996 }
Eric Christopherfd179292009-08-27 18:07:15 +000010997
Chris Lattnerd1980a52009-03-12 06:52:53 +000010998 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010999 if (FalseC->getAPIntValue() == 0 &&
11000 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011001 if (NeedsCondInvert) // Invert the condition if needed.
11002 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11003 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011004
Chris Lattnerd1980a52009-03-12 06:52:53 +000011005 // Zero extend the condition if needed.
11006 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011007
Chris Lattnercee56e72009-03-13 05:53:31 +000011008 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011009 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011010 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011011 }
Eric Christopherfd179292009-08-27 18:07:15 +000011012
Chris Lattner97a29a52009-03-13 05:22:11 +000011013 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011014 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011015 if (NeedsCondInvert) // Invert the condition if needed.
11016 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11017 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011018
Chris Lattner97a29a52009-03-13 05:22:11 +000011019 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011020 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11021 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011022 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011023 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011024 }
Eric Christopherfd179292009-08-27 18:07:15 +000011025
Chris Lattnercee56e72009-03-13 05:53:31 +000011026 // Optimize cases that will turn into an LEA instruction. This requires
11027 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011028 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011029 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011030 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011031
Chris Lattnercee56e72009-03-13 05:53:31 +000011032 bool isFastMultiplier = false;
11033 if (Diff < 10) {
11034 switch ((unsigned char)Diff) {
11035 default: break;
11036 case 1: // result = add base, cond
11037 case 2: // result = lea base( , cond*2)
11038 case 3: // result = lea base(cond, cond*2)
11039 case 4: // result = lea base( , cond*4)
11040 case 5: // result = lea base(cond, cond*4)
11041 case 8: // result = lea base( , cond*8)
11042 case 9: // result = lea base(cond, cond*8)
11043 isFastMultiplier = true;
11044 break;
11045 }
11046 }
Eric Christopherfd179292009-08-27 18:07:15 +000011047
Chris Lattnercee56e72009-03-13 05:53:31 +000011048 if (isFastMultiplier) {
11049 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11050 if (NeedsCondInvert) // Invert the condition if needed.
11051 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11052 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011053
Chris Lattnercee56e72009-03-13 05:53:31 +000011054 // Zero extend the condition if needed.
11055 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11056 Cond);
11057 // Scale the condition by the difference.
11058 if (Diff != 1)
11059 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11060 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011061
Chris Lattnercee56e72009-03-13 05:53:31 +000011062 // Add the base if non-zero.
11063 if (FalseC->getAPIntValue() != 0)
11064 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11065 SDValue(FalseC, 0));
11066 return Cond;
11067 }
Eric Christopherfd179292009-08-27 18:07:15 +000011068 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011069 }
11070 }
Eric Christopherfd179292009-08-27 18:07:15 +000011071
Dan Gohman475871a2008-07-27 21:46:04 +000011072 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011073}
11074
Chris Lattnerd1980a52009-03-12 06:52:53 +000011075/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11076static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11077 TargetLowering::DAGCombinerInfo &DCI) {
11078 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011079
Chris Lattnerd1980a52009-03-12 06:52:53 +000011080 // If the flag operand isn't dead, don't touch this CMOV.
11081 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11082 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011083
Chris Lattnerd1980a52009-03-12 06:52:53 +000011084 // If this is a select between two integer constants, try to do some
11085 // optimizations. Note that the operands are ordered the opposite of SELECT
11086 // operands.
11087 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11088 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11089 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11090 // larger than FalseC (the false value).
11091 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011092
Chris Lattnerd1980a52009-03-12 06:52:53 +000011093 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11094 CC = X86::GetOppositeBranchCondition(CC);
11095 std::swap(TrueC, FalseC);
11096 }
Eric Christopherfd179292009-08-27 18:07:15 +000011097
Chris Lattnerd1980a52009-03-12 06:52:53 +000011098 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011099 // This is efficient for any integer data type (including i8/i16) and
11100 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011101 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11102 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011103 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11104 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011105
Chris Lattnerd1980a52009-03-12 06:52:53 +000011106 // Zero extend the condition if needed.
11107 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011108
Chris Lattnerd1980a52009-03-12 06:52:53 +000011109 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11110 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011111 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011112 if (N->getNumValues() == 2) // Dead flag value?
11113 return DCI.CombineTo(N, Cond, SDValue());
11114 return Cond;
11115 }
Eric Christopherfd179292009-08-27 18:07:15 +000011116
Chris Lattnercee56e72009-03-13 05:53:31 +000011117 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11118 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011119 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11120 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011121 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11122 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011123
Chris Lattner97a29a52009-03-13 05:22:11 +000011124 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011125 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11126 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011127 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11128 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011129
Chris Lattner97a29a52009-03-13 05:22:11 +000011130 if (N->getNumValues() == 2) // Dead flag value?
11131 return DCI.CombineTo(N, Cond, SDValue());
11132 return Cond;
11133 }
Eric Christopherfd179292009-08-27 18:07:15 +000011134
Chris Lattnercee56e72009-03-13 05:53:31 +000011135 // Optimize cases that will turn into an LEA instruction. This requires
11136 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011137 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011138 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011139 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011140
Chris Lattnercee56e72009-03-13 05:53:31 +000011141 bool isFastMultiplier = false;
11142 if (Diff < 10) {
11143 switch ((unsigned char)Diff) {
11144 default: break;
11145 case 1: // result = add base, cond
11146 case 2: // result = lea base( , cond*2)
11147 case 3: // result = lea base(cond, cond*2)
11148 case 4: // result = lea base( , cond*4)
11149 case 5: // result = lea base(cond, cond*4)
11150 case 8: // result = lea base( , cond*8)
11151 case 9: // result = lea base(cond, cond*8)
11152 isFastMultiplier = true;
11153 break;
11154 }
11155 }
Eric Christopherfd179292009-08-27 18:07:15 +000011156
Chris Lattnercee56e72009-03-13 05:53:31 +000011157 if (isFastMultiplier) {
11158 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11159 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011160 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11161 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011162 // Zero extend the condition if needed.
11163 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11164 Cond);
11165 // Scale the condition by the difference.
11166 if (Diff != 1)
11167 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11168 DAG.getConstant(Diff, Cond.getValueType()));
11169
11170 // Add the base if non-zero.
11171 if (FalseC->getAPIntValue() != 0)
11172 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11173 SDValue(FalseC, 0));
11174 if (N->getNumValues() == 2) // Dead flag value?
11175 return DCI.CombineTo(N, Cond, SDValue());
11176 return Cond;
11177 }
Eric Christopherfd179292009-08-27 18:07:15 +000011178 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011179 }
11180 }
11181 return SDValue();
11182}
11183
11184
Evan Cheng0b0cd912009-03-28 05:57:29 +000011185/// PerformMulCombine - Optimize a single multiply with constant into two
11186/// in order to implement it with two cheaper instructions, e.g.
11187/// LEA + SHL, LEA + LEA.
11188static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11189 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011190 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11191 return SDValue();
11192
Owen Andersone50ed302009-08-10 22:56:29 +000011193 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011194 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011195 return SDValue();
11196
11197 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11198 if (!C)
11199 return SDValue();
11200 uint64_t MulAmt = C->getZExtValue();
11201 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11202 return SDValue();
11203
11204 uint64_t MulAmt1 = 0;
11205 uint64_t MulAmt2 = 0;
11206 if ((MulAmt % 9) == 0) {
11207 MulAmt1 = 9;
11208 MulAmt2 = MulAmt / 9;
11209 } else if ((MulAmt % 5) == 0) {
11210 MulAmt1 = 5;
11211 MulAmt2 = MulAmt / 5;
11212 } else if ((MulAmt % 3) == 0) {
11213 MulAmt1 = 3;
11214 MulAmt2 = MulAmt / 3;
11215 }
11216 if (MulAmt2 &&
11217 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11218 DebugLoc DL = N->getDebugLoc();
11219
11220 if (isPowerOf2_64(MulAmt2) &&
11221 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11222 // If second multiplifer is pow2, issue it first. We want the multiply by
11223 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11224 // is an add.
11225 std::swap(MulAmt1, MulAmt2);
11226
11227 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011228 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011229 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011230 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011231 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011232 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011233 DAG.getConstant(MulAmt1, VT));
11234
Eric Christopherfd179292009-08-27 18:07:15 +000011235 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011236 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011237 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011238 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011239 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011240 DAG.getConstant(MulAmt2, VT));
11241
11242 // Do not add new nodes to DAG combiner worklist.
11243 DCI.CombineTo(N, NewMul, false);
11244 }
11245 return SDValue();
11246}
11247
Evan Chengad9c0a32009-12-15 00:53:42 +000011248static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11249 SDValue N0 = N->getOperand(0);
11250 SDValue N1 = N->getOperand(1);
11251 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11252 EVT VT = N0.getValueType();
11253
11254 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11255 // since the result of setcc_c is all zero's or all ones.
11256 if (N1C && N0.getOpcode() == ISD::AND &&
11257 N0.getOperand(1).getOpcode() == ISD::Constant) {
11258 SDValue N00 = N0.getOperand(0);
11259 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11260 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11261 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11262 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11263 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11264 APInt ShAmt = N1C->getAPIntValue();
11265 Mask = Mask.shl(ShAmt);
11266 if (Mask != 0)
11267 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11268 N00, DAG.getConstant(Mask, VT));
11269 }
11270 }
11271
11272 return SDValue();
11273}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011274
Nate Begeman740ab032009-01-26 00:52:55 +000011275/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11276/// when possible.
11277static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11278 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011279 EVT VT = N->getValueType(0);
11280 if (!VT.isVector() && VT.isInteger() &&
11281 N->getOpcode() == ISD::SHL)
11282 return PerformSHLCombine(N, DAG);
11283
Nate Begeman740ab032009-01-26 00:52:55 +000011284 // On X86 with SSE2 support, we can transform this to a vector shift if
11285 // all elements are shifted by the same amount. We can't do this in legalize
11286 // because the a constant vector is typically transformed to a constant pool
11287 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011288 if (!Subtarget->hasSSE2())
11289 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011290
Owen Anderson825b72b2009-08-11 20:47:22 +000011291 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011292 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011293
Mon P Wang3becd092009-01-28 08:12:05 +000011294 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011295 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011296 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011297 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011298 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11299 unsigned NumElts = VT.getVectorNumElements();
11300 unsigned i = 0;
11301 for (; i != NumElts; ++i) {
11302 SDValue Arg = ShAmtOp.getOperand(i);
11303 if (Arg.getOpcode() == ISD::UNDEF) continue;
11304 BaseShAmt = Arg;
11305 break;
11306 }
11307 for (; i != NumElts; ++i) {
11308 SDValue Arg = ShAmtOp.getOperand(i);
11309 if (Arg.getOpcode() == ISD::UNDEF) continue;
11310 if (Arg != BaseShAmt) {
11311 return SDValue();
11312 }
11313 }
11314 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011315 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011316 SDValue InVec = ShAmtOp.getOperand(0);
11317 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11318 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11319 unsigned i = 0;
11320 for (; i != NumElts; ++i) {
11321 SDValue Arg = InVec.getOperand(i);
11322 if (Arg.getOpcode() == ISD::UNDEF) continue;
11323 BaseShAmt = Arg;
11324 break;
11325 }
11326 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11327 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011328 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011329 if (C->getZExtValue() == SplatIdx)
11330 BaseShAmt = InVec.getOperand(1);
11331 }
11332 }
11333 if (BaseShAmt.getNode() == 0)
11334 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11335 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011336 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011337 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011338
Mon P Wangefa42202009-09-03 19:56:25 +000011339 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011340 if (EltVT.bitsGT(MVT::i32))
11341 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11342 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011343 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011344
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011345 // The shift amount is identical so we can do a vector shift.
11346 SDValue ValOp = N->getOperand(0);
11347 switch (N->getOpcode()) {
11348 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011349 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011350 break;
11351 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011352 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011353 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011354 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011355 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011356 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011357 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011358 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011359 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011360 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011361 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011362 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011363 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011364 break;
11365 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011366 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011367 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011368 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011369 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011370 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011371 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011372 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011373 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011374 break;
11375 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011376 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011377 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011378 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011379 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011380 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011381 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011382 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011383 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011384 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011385 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011386 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011387 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011388 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011389 }
11390 return SDValue();
11391}
11392
Nate Begemanb65c1752010-12-17 22:55:37 +000011393
11394static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11395 TargetLowering::DAGCombinerInfo &DCI,
11396 const X86Subtarget *Subtarget) {
11397 if (DCI.isBeforeLegalizeOps())
11398 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011399
Nate Begemanb65c1752010-12-17 22:55:37 +000011400 // Want to form PANDN nodes, in the hopes of then easily combining them with
11401 // OR and AND nodes to form PBLEND/PSIGN.
11402 EVT VT = N->getValueType(0);
11403 if (VT != MVT::v2i64)
11404 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011405
Nate Begemanb65c1752010-12-17 22:55:37 +000011406 SDValue N0 = N->getOperand(0);
11407 SDValue N1 = N->getOperand(1);
11408 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011409
Nate Begemanb65c1752010-12-17 22:55:37 +000011410 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011411 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011412 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11413 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11414
11415 // Check RHS for vnot
11416 if (N1.getOpcode() == ISD::XOR &&
11417 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11418 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011419
Nate Begemanb65c1752010-12-17 22:55:37 +000011420 return SDValue();
11421}
11422
Evan Cheng760d1942010-01-04 21:22:48 +000011423static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011424 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011425 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011426 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011427 return SDValue();
11428
Evan Cheng760d1942010-01-04 21:22:48 +000011429 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011430 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011431 return SDValue();
11432
Evan Cheng760d1942010-01-04 21:22:48 +000011433 SDValue N0 = N->getOperand(0);
11434 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011435
Nate Begemanb65c1752010-12-17 22:55:37 +000011436 // look for psign/blend
11437 if (Subtarget->hasSSSE3()) {
11438 if (VT == MVT::v2i64) {
11439 // Canonicalize pandn to RHS
11440 if (N0.getOpcode() == X86ISD::PANDN)
11441 std::swap(N0, N1);
11442 // or (and (m, x), (pandn m, y))
11443 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11444 SDValue Mask = N1.getOperand(0);
11445 SDValue X = N1.getOperand(1);
11446 SDValue Y;
11447 if (N0.getOperand(0) == Mask)
11448 Y = N0.getOperand(1);
11449 if (N0.getOperand(1) == Mask)
11450 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011451
Nate Begemanb65c1752010-12-17 22:55:37 +000011452 // Check to see if the mask appeared in both the AND and PANDN and
11453 if (!Y.getNode())
11454 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011455
Nate Begemanb65c1752010-12-17 22:55:37 +000011456 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11457 if (Mask.getOpcode() != ISD::BITCAST ||
11458 X.getOpcode() != ISD::BITCAST ||
11459 Y.getOpcode() != ISD::BITCAST)
11460 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011461
Nate Begemanb65c1752010-12-17 22:55:37 +000011462 // Look through mask bitcast.
11463 Mask = Mask.getOperand(0);
11464 EVT MaskVT = Mask.getValueType();
11465
11466 // Validate that the Mask operand is a vector sra node. The sra node
11467 // will be an intrinsic.
11468 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11469 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011470
Nate Begemanb65c1752010-12-17 22:55:37 +000011471 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11472 // there is no psrai.b
11473 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11474 case Intrinsic::x86_sse2_psrai_w:
11475 case Intrinsic::x86_sse2_psrai_d:
11476 break;
11477 default: return SDValue();
11478 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011479
Nate Begemanb65c1752010-12-17 22:55:37 +000011480 // Check that the SRA is all signbits.
11481 SDValue SraC = Mask.getOperand(2);
11482 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11483 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11484 if ((SraAmt + 1) != EltBits)
11485 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011486
Nate Begemanb65c1752010-12-17 22:55:37 +000011487 DebugLoc DL = N->getDebugLoc();
11488
11489 // Now we know we at least have a plendvb with the mask val. See if
11490 // we can form a psignb/w/d.
11491 // psign = x.type == y.type == mask.type && y = sub(0, x);
11492 X = X.getOperand(0);
11493 Y = Y.getOperand(0);
11494 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11495 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11496 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11497 unsigned Opc = 0;
11498 switch (EltBits) {
11499 case 8: Opc = X86ISD::PSIGNB; break;
11500 case 16: Opc = X86ISD::PSIGNW; break;
11501 case 32: Opc = X86ISD::PSIGND; break;
11502 default: break;
11503 }
11504 if (Opc) {
11505 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11506 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11507 }
11508 }
11509 // PBLENDVB only available on SSE 4.1
11510 if (!Subtarget->hasSSE41())
11511 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011512
Nate Begemanb65c1752010-12-17 22:55:37 +000011513 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11514 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11515 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011516 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011517 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11518 }
11519 }
11520 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011521
Nate Begemanb65c1752010-12-17 22:55:37 +000011522 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011523 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11524 std::swap(N0, N1);
11525 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11526 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011527 if (!N0.hasOneUse() || !N1.hasOneUse())
11528 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011529
11530 SDValue ShAmt0 = N0.getOperand(1);
11531 if (ShAmt0.getValueType() != MVT::i8)
11532 return SDValue();
11533 SDValue ShAmt1 = N1.getOperand(1);
11534 if (ShAmt1.getValueType() != MVT::i8)
11535 return SDValue();
11536 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11537 ShAmt0 = ShAmt0.getOperand(0);
11538 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11539 ShAmt1 = ShAmt1.getOperand(0);
11540
11541 DebugLoc DL = N->getDebugLoc();
11542 unsigned Opc = X86ISD::SHLD;
11543 SDValue Op0 = N0.getOperand(0);
11544 SDValue Op1 = N1.getOperand(0);
11545 if (ShAmt0.getOpcode() == ISD::SUB) {
11546 Opc = X86ISD::SHRD;
11547 std::swap(Op0, Op1);
11548 std::swap(ShAmt0, ShAmt1);
11549 }
11550
Evan Cheng8b1190a2010-04-28 01:18:01 +000011551 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011552 if (ShAmt1.getOpcode() == ISD::SUB) {
11553 SDValue Sum = ShAmt1.getOperand(0);
11554 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011555 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11556 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11557 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11558 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011559 return DAG.getNode(Opc, DL, VT,
11560 Op0, Op1,
11561 DAG.getNode(ISD::TRUNCATE, DL,
11562 MVT::i8, ShAmt0));
11563 }
11564 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11565 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11566 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011567 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011568 return DAG.getNode(Opc, DL, VT,
11569 N0.getOperand(0), N1.getOperand(0),
11570 DAG.getNode(ISD::TRUNCATE, DL,
11571 MVT::i8, ShAmt0));
11572 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011573
Evan Cheng760d1942010-01-04 21:22:48 +000011574 return SDValue();
11575}
11576
Chris Lattner149a4e52008-02-22 02:09:43 +000011577/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011578static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011579 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011580 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11581 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011582 // A preferable solution to the general problem is to figure out the right
11583 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011584
11585 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011586 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011587 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011588 if (VT.getSizeInBits() != 64)
11589 return SDValue();
11590
Devang Patel578efa92009-06-05 21:57:13 +000011591 const Function *F = DAG.getMachineFunction().getFunction();
11592 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011593 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011594 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011595 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011596 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011597 isa<LoadSDNode>(St->getValue()) &&
11598 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11599 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011600 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011601 LoadSDNode *Ld = 0;
11602 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011603 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011604 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011605 // Must be a store of a load. We currently handle two cases: the load
11606 // is a direct child, and it's under an intervening TokenFactor. It is
11607 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011608 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011609 Ld = cast<LoadSDNode>(St->getChain());
11610 else if (St->getValue().hasOneUse() &&
11611 ChainVal->getOpcode() == ISD::TokenFactor) {
11612 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011613 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011614 TokenFactorIndex = i;
11615 Ld = cast<LoadSDNode>(St->getValue());
11616 } else
11617 Ops.push_back(ChainVal->getOperand(i));
11618 }
11619 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011620
Evan Cheng536e6672009-03-12 05:59:15 +000011621 if (!Ld || !ISD::isNormalLoad(Ld))
11622 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011623
Evan Cheng536e6672009-03-12 05:59:15 +000011624 // If this is not the MMX case, i.e. we are just turning i64 load/store
11625 // into f64 load/store, avoid the transformation if there are multiple
11626 // uses of the loaded value.
11627 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11628 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011629
Evan Cheng536e6672009-03-12 05:59:15 +000011630 DebugLoc LdDL = Ld->getDebugLoc();
11631 DebugLoc StDL = N->getDebugLoc();
11632 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11633 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11634 // pair instead.
11635 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011636 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011637 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11638 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011639 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011640 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011641 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011642 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011643 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011644 Ops.size());
11645 }
Evan Cheng536e6672009-03-12 05:59:15 +000011646 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011647 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011648 St->isVolatile(), St->isNonTemporal(),
11649 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011650 }
Evan Cheng536e6672009-03-12 05:59:15 +000011651
11652 // Otherwise, lower to two pairs of 32-bit loads / stores.
11653 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011654 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11655 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011656
Owen Anderson825b72b2009-08-11 20:47:22 +000011657 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011658 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011659 Ld->isVolatile(), Ld->isNonTemporal(),
11660 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011661 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011662 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011663 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011664 MinAlign(Ld->getAlignment(), 4));
11665
11666 SDValue NewChain = LoLd.getValue(1);
11667 if (TokenFactorIndex != -1) {
11668 Ops.push_back(LoLd);
11669 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011670 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011671 Ops.size());
11672 }
11673
11674 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011675 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11676 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011677
11678 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011679 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011680 St->isVolatile(), St->isNonTemporal(),
11681 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011682 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011683 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011684 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011685 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011686 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011687 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011688 }
Dan Gohman475871a2008-07-27 21:46:04 +000011689 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011690}
11691
Chris Lattner6cf73262008-01-25 06:14:17 +000011692/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11693/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011694static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011695 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11696 // F[X]OR(0.0, x) -> x
11697 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011698 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11699 if (C->getValueAPF().isPosZero())
11700 return N->getOperand(1);
11701 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11702 if (C->getValueAPF().isPosZero())
11703 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011704 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011705}
11706
11707/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011708static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011709 // FAND(0.0, x) -> 0.0
11710 // FAND(x, 0.0) -> 0.0
11711 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11712 if (C->getValueAPF().isPosZero())
11713 return N->getOperand(0);
11714 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11715 if (C->getValueAPF().isPosZero())
11716 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011717 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011718}
11719
Dan Gohmane5af2d32009-01-29 01:59:02 +000011720static SDValue PerformBTCombine(SDNode *N,
11721 SelectionDAG &DAG,
11722 TargetLowering::DAGCombinerInfo &DCI) {
11723 // BT ignores high bits in the bit index operand.
11724 SDValue Op1 = N->getOperand(1);
11725 if (Op1.hasOneUse()) {
11726 unsigned BitWidth = Op1.getValueSizeInBits();
11727 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11728 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011729 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11730 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011731 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011732 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11733 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11734 DCI.CommitTargetLoweringOpt(TLO);
11735 }
11736 return SDValue();
11737}
Chris Lattner83e6c992006-10-04 06:57:07 +000011738
Eli Friedman7a5e5552009-06-07 06:52:44 +000011739static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11740 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011741 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011742 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011743 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011744 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011745 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011746 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011747 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011748 }
11749 return SDValue();
11750}
11751
Evan Cheng2e489c42009-12-16 00:53:11 +000011752static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11753 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11754 // (and (i32 x86isd::setcc_carry), 1)
11755 // This eliminates the zext. This transformation is necessary because
11756 // ISD::SETCC is always legalized to i8.
11757 DebugLoc dl = N->getDebugLoc();
11758 SDValue N0 = N->getOperand(0);
11759 EVT VT = N->getValueType(0);
11760 if (N0.getOpcode() == ISD::AND &&
11761 N0.hasOneUse() &&
11762 N0.getOperand(0).hasOneUse()) {
11763 SDValue N00 = N0.getOperand(0);
11764 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11765 return SDValue();
11766 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11767 if (!C || C->getZExtValue() != 1)
11768 return SDValue();
11769 return DAG.getNode(ISD::AND, dl, VT,
11770 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11771 N00.getOperand(0), N00.getOperand(1)),
11772 DAG.getConstant(1, VT));
11773 }
11774
11775 return SDValue();
11776}
11777
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011778// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11779static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11780 unsigned X86CC = N->getConstantOperandVal(0);
11781 SDValue EFLAG = N->getOperand(1);
11782 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011783
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011784 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11785 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11786 // cases.
11787 if (X86CC == X86::COND_B)
11788 return DAG.getNode(ISD::AND, DL, MVT::i8,
11789 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11790 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11791 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011792
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011793 return SDValue();
11794}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011795
Chris Lattner23a01992010-12-20 01:37:09 +000011796// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11797static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11798 X86TargetLowering::DAGCombinerInfo &DCI) {
11799 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11800 // the result is either zero or one (depending on the input carry bit).
11801 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11802 if (X86::isZeroNode(N->getOperand(0)) &&
11803 X86::isZeroNode(N->getOperand(1)) &&
11804 // We don't have a good way to replace an EFLAGS use, so only do this when
11805 // dead right now.
11806 SDValue(N, 1).use_empty()) {
11807 DebugLoc DL = N->getDebugLoc();
11808 EVT VT = N->getValueType(0);
11809 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11810 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11811 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11812 DAG.getConstant(X86::COND_B,MVT::i8),
11813 N->getOperand(2)),
11814 DAG.getConstant(1, VT));
11815 return DCI.CombineTo(N, Res1, CarryOut);
11816 }
11817
11818 return SDValue();
11819}
11820
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011821// fold (add Y, (sete X, 0)) -> adc 0, Y
11822// (add Y, (setne X, 0)) -> sbb -1, Y
11823// (sub (sete X, 0), Y) -> sbb 0, Y
11824// (sub (setne X, 0), Y) -> adc -1, Y
11825static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11826 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011827
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011828 // Look through ZExts.
11829 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11830 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11831 return SDValue();
11832
11833 SDValue SetCC = Ext.getOperand(0);
11834 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11835 return SDValue();
11836
11837 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11838 if (CC != X86::COND_E && CC != X86::COND_NE)
11839 return SDValue();
11840
11841 SDValue Cmp = SetCC.getOperand(1);
11842 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011843 !X86::isZeroNode(Cmp.getOperand(1)) ||
11844 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011845 return SDValue();
11846
11847 SDValue CmpOp0 = Cmp.getOperand(0);
11848 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11849 DAG.getConstant(1, CmpOp0.getValueType()));
11850
11851 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11852 if (CC == X86::COND_NE)
11853 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11854 DL, OtherVal.getValueType(), OtherVal,
11855 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11856 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11857 DL, OtherVal.getValueType(), OtherVal,
11858 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11859}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011860
Dan Gohman475871a2008-07-27 21:46:04 +000011861SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011862 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011863 SelectionDAG &DAG = DCI.DAG;
11864 switch (N->getOpcode()) {
11865 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011866 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011867 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011868 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011869 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011870 case ISD::ADD:
11871 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011872 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011873 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011874 case ISD::SHL:
11875 case ISD::SRA:
11876 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011877 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011878 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011879 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011880 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011881 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11882 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011883 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011884 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011885 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011886 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011887 case X86ISD::SHUFPS: // Handle all target specific shuffles
11888 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011889 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011890 case X86ISD::PUNPCKHBW:
11891 case X86ISD::PUNPCKHWD:
11892 case X86ISD::PUNPCKHDQ:
11893 case X86ISD::PUNPCKHQDQ:
11894 case X86ISD::UNPCKHPS:
11895 case X86ISD::UNPCKHPD:
11896 case X86ISD::PUNPCKLBW:
11897 case X86ISD::PUNPCKLWD:
11898 case X86ISD::PUNPCKLDQ:
11899 case X86ISD::PUNPCKLQDQ:
11900 case X86ISD::UNPCKLPS:
11901 case X86ISD::UNPCKLPD:
11902 case X86ISD::MOVHLPS:
11903 case X86ISD::MOVLHPS:
11904 case X86ISD::PSHUFD:
11905 case X86ISD::PSHUFHW:
11906 case X86ISD::PSHUFLW:
11907 case X86ISD::MOVSS:
11908 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011909 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011910 }
11911
Dan Gohman475871a2008-07-27 21:46:04 +000011912 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011913}
11914
Evan Chenge5b51ac2010-04-17 06:13:15 +000011915/// isTypeDesirableForOp - Return true if the target has native support for
11916/// the specified value type and it is 'desirable' to use the type for the
11917/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11918/// instruction encodings are longer and some i16 instructions are slow.
11919bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11920 if (!isTypeLegal(VT))
11921 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011922 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011923 return true;
11924
11925 switch (Opc) {
11926 default:
11927 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011928 case ISD::LOAD:
11929 case ISD::SIGN_EXTEND:
11930 case ISD::ZERO_EXTEND:
11931 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011932 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011933 case ISD::SRL:
11934 case ISD::SUB:
11935 case ISD::ADD:
11936 case ISD::MUL:
11937 case ISD::AND:
11938 case ISD::OR:
11939 case ISD::XOR:
11940 return false;
11941 }
11942}
11943
11944/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011945/// beneficial for dag combiner to promote the specified node. If true, it
11946/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011947bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011948 EVT VT = Op.getValueType();
11949 if (VT != MVT::i16)
11950 return false;
11951
Evan Cheng4c26e932010-04-19 19:29:22 +000011952 bool Promote = false;
11953 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011954 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011955 default: break;
11956 case ISD::LOAD: {
11957 LoadSDNode *LD = cast<LoadSDNode>(Op);
11958 // If the non-extending load has a single use and it's not live out, then it
11959 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011960 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11961 Op.hasOneUse()*/) {
11962 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11963 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11964 // The only case where we'd want to promote LOAD (rather then it being
11965 // promoted as an operand is when it's only use is liveout.
11966 if (UI->getOpcode() != ISD::CopyToReg)
11967 return false;
11968 }
11969 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011970 Promote = true;
11971 break;
11972 }
11973 case ISD::SIGN_EXTEND:
11974 case ISD::ZERO_EXTEND:
11975 case ISD::ANY_EXTEND:
11976 Promote = true;
11977 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011978 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011979 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011980 SDValue N0 = Op.getOperand(0);
11981 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011982 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011983 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011984 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011985 break;
11986 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011987 case ISD::ADD:
11988 case ISD::MUL:
11989 case ISD::AND:
11990 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011991 case ISD::XOR:
11992 Commute = true;
11993 // fallthrough
11994 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011995 SDValue N0 = Op.getOperand(0);
11996 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011997 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011998 return false;
11999 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012000 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012001 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012002 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012003 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012004 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012005 }
12006 }
12007
12008 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012009 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012010}
12011
Evan Cheng60c07e12006-07-05 22:17:51 +000012012//===----------------------------------------------------------------------===//
12013// X86 Inline Assembly Support
12014//===----------------------------------------------------------------------===//
12015
Chris Lattnerb8105652009-07-20 17:51:36 +000012016bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12017 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012018
12019 std::string AsmStr = IA->getAsmString();
12020
12021 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012022 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012023 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012024
12025 switch (AsmPieces.size()) {
12026 default: return false;
12027 case 1:
12028 AsmStr = AsmPieces[0];
12029 AsmPieces.clear();
12030 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12031
Evan Cheng55d42002011-01-08 01:24:27 +000012032 // FIXME: this should verify that we are targetting a 486 or better. If not,
12033 // we will turn this bswap into something that will be lowered to logical ops
12034 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12035 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012036 // bswap $0
12037 if (AsmPieces.size() == 2 &&
12038 (AsmPieces[0] == "bswap" ||
12039 AsmPieces[0] == "bswapq" ||
12040 AsmPieces[0] == "bswapl") &&
12041 (AsmPieces[1] == "$0" ||
12042 AsmPieces[1] == "${0:q}")) {
12043 // No need to check constraints, nothing other than the equivalent of
12044 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012045 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12046 if (!Ty || Ty->getBitWidth() % 16 != 0)
12047 return false;
12048 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012049 }
12050 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012051 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012052 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012053 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012054 AsmPieces[1] == "$$8," &&
12055 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012056 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12057 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012058 const std::string &ConstraintsStr = IA->getConstraintString();
12059 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012060 std::sort(AsmPieces.begin(), AsmPieces.end());
12061 if (AsmPieces.size() == 4 &&
12062 AsmPieces[0] == "~{cc}" &&
12063 AsmPieces[1] == "~{dirflag}" &&
12064 AsmPieces[2] == "~{flags}" &&
12065 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012066 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12067 if (!Ty || Ty->getBitWidth() % 16 != 0)
12068 return false;
12069 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012070 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012071 }
12072 break;
12073 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012074 if (CI->getType()->isIntegerTy(32) &&
12075 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12076 SmallVector<StringRef, 4> Words;
12077 SplitString(AsmPieces[0], Words, " \t,");
12078 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12079 Words[2] == "${0:w}") {
12080 Words.clear();
12081 SplitString(AsmPieces[1], Words, " \t,");
12082 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12083 Words[2] == "$0") {
12084 Words.clear();
12085 SplitString(AsmPieces[2], Words, " \t,");
12086 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12087 Words[2] == "${0:w}") {
12088 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012089 const std::string &ConstraintsStr = IA->getConstraintString();
12090 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012091 std::sort(AsmPieces.begin(), AsmPieces.end());
12092 if (AsmPieces.size() == 4 &&
12093 AsmPieces[0] == "~{cc}" &&
12094 AsmPieces[1] == "~{dirflag}" &&
12095 AsmPieces[2] == "~{flags}" &&
12096 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012097 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12098 if (!Ty || Ty->getBitWidth() % 16 != 0)
12099 return false;
12100 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012101 }
12102 }
12103 }
12104 }
12105 }
Evan Cheng55d42002011-01-08 01:24:27 +000012106
12107 if (CI->getType()->isIntegerTy(64)) {
12108 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12109 if (Constraints.size() >= 2 &&
12110 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12111 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12112 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12113 SmallVector<StringRef, 4> Words;
12114 SplitString(AsmPieces[0], Words, " \t");
12115 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012116 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012117 SplitString(AsmPieces[1], Words, " \t");
12118 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12119 Words.clear();
12120 SplitString(AsmPieces[2], Words, " \t,");
12121 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12122 Words[2] == "%edx") {
12123 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12124 if (!Ty || Ty->getBitWidth() % 16 != 0)
12125 return false;
12126 return IntrinsicLowering::LowerToByteSwap(CI);
12127 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012128 }
12129 }
12130 }
12131 }
12132 break;
12133 }
12134 return false;
12135}
12136
12137
12138
Chris Lattnerf4dff842006-07-11 02:54:03 +000012139/// getConstraintType - Given a constraint letter, return the type of
12140/// constraint it is for this target.
12141X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012142X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12143 if (Constraint.size() == 1) {
12144 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012145 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012146 case 'q':
12147 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012148 case 'f':
12149 case 't':
12150 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012151 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012152 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012153 case 'Y':
12154 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012155 case 'a':
12156 case 'b':
12157 case 'c':
12158 case 'd':
12159 case 'S':
12160 case 'D':
12161 case 'A':
12162 return C_Register;
12163 case 'I':
12164 case 'J':
12165 case 'K':
12166 case 'L':
12167 case 'M':
12168 case 'N':
12169 case 'G':
12170 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012171 case 'e':
12172 case 'Z':
12173 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012174 default:
12175 break;
12176 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012177 }
Chris Lattner4234f572007-03-25 02:14:49 +000012178 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012179}
12180
John Thompson44ab89e2010-10-29 17:29:13 +000012181/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012182/// This object must already have been set up with the operand type
12183/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012184TargetLowering::ConstraintWeight
12185 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012186 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012187 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012188 Value *CallOperandVal = info.CallOperandVal;
12189 // If we don't have a value, we can't do a match,
12190 // but allow it at the lowest weight.
12191 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012192 return CW_Default;
12193 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012194 // Look at the constraint type.
12195 switch (*constraint) {
12196 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012197 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12198 case 'R':
12199 case 'q':
12200 case 'Q':
12201 case 'a':
12202 case 'b':
12203 case 'c':
12204 case 'd':
12205 case 'S':
12206 case 'D':
12207 case 'A':
12208 if (CallOperandVal->getType()->isIntegerTy())
12209 weight = CW_SpecificReg;
12210 break;
12211 case 'f':
12212 case 't':
12213 case 'u':
12214 if (type->isFloatingPointTy())
12215 weight = CW_SpecificReg;
12216 break;
12217 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012218 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012219 weight = CW_SpecificReg;
12220 break;
12221 case 'x':
12222 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012223 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012224 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012225 break;
12226 case 'I':
12227 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12228 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012229 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012230 }
12231 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012232 case 'J':
12233 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12234 if (C->getZExtValue() <= 63)
12235 weight = CW_Constant;
12236 }
12237 break;
12238 case 'K':
12239 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12240 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12241 weight = CW_Constant;
12242 }
12243 break;
12244 case 'L':
12245 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12246 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12247 weight = CW_Constant;
12248 }
12249 break;
12250 case 'M':
12251 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12252 if (C->getZExtValue() <= 3)
12253 weight = CW_Constant;
12254 }
12255 break;
12256 case 'N':
12257 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12258 if (C->getZExtValue() <= 0xff)
12259 weight = CW_Constant;
12260 }
12261 break;
12262 case 'G':
12263 case 'C':
12264 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12265 weight = CW_Constant;
12266 }
12267 break;
12268 case 'e':
12269 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12270 if ((C->getSExtValue() >= -0x80000000LL) &&
12271 (C->getSExtValue() <= 0x7fffffffLL))
12272 weight = CW_Constant;
12273 }
12274 break;
12275 case 'Z':
12276 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12277 if (C->getZExtValue() <= 0xffffffff)
12278 weight = CW_Constant;
12279 }
12280 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012281 }
12282 return weight;
12283}
12284
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012285/// LowerXConstraint - try to replace an X constraint, which matches anything,
12286/// with another that has more specific requirements based on the type of the
12287/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012288const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012289LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012290 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12291 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012292 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012293 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012294 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012295 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012296 return "x";
12297 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012298
Chris Lattner5e764232008-04-26 23:02:14 +000012299 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012300}
12301
Chris Lattner48884cd2007-08-25 00:47:38 +000012302/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12303/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012304void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012305 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012306 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012307 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012308 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012309
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012310 switch (Constraint) {
12311 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012312 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012313 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012314 if (C->getZExtValue() <= 31) {
12315 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012316 break;
12317 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012318 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012319 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012320 case 'J':
12321 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012322 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012323 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12324 break;
12325 }
12326 }
12327 return;
12328 case 'K':
12329 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012330 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012331 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12332 break;
12333 }
12334 }
12335 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012336 case 'N':
12337 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012338 if (C->getZExtValue() <= 255) {
12339 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012340 break;
12341 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012342 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012343 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012344 case 'e': {
12345 // 32-bit signed value
12346 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012347 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12348 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012349 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012350 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012351 break;
12352 }
12353 // FIXME gcc accepts some relocatable values here too, but only in certain
12354 // memory models; it's complicated.
12355 }
12356 return;
12357 }
12358 case 'Z': {
12359 // 32-bit unsigned value
12360 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012361 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12362 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012363 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12364 break;
12365 }
12366 }
12367 // FIXME gcc accepts some relocatable values here too, but only in certain
12368 // memory models; it's complicated.
12369 return;
12370 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012371 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012372 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012373 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012374 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012375 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012376 break;
12377 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012378
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012379 // In any sort of PIC mode addresses need to be computed at runtime by
12380 // adding in a register or some sort of table lookup. These can't
12381 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012382 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012383 return;
12384
Chris Lattnerdc43a882007-05-03 16:52:29 +000012385 // If we are in non-pic codegen mode, we allow the address of a global (with
12386 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012387 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012388 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012389
Chris Lattner49921962009-05-08 18:23:14 +000012390 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12391 while (1) {
12392 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12393 Offset += GA->getOffset();
12394 break;
12395 } else if (Op.getOpcode() == ISD::ADD) {
12396 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12397 Offset += C->getZExtValue();
12398 Op = Op.getOperand(0);
12399 continue;
12400 }
12401 } else if (Op.getOpcode() == ISD::SUB) {
12402 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12403 Offset += -C->getZExtValue();
12404 Op = Op.getOperand(0);
12405 continue;
12406 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012407 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012408
Chris Lattner49921962009-05-08 18:23:14 +000012409 // Otherwise, this isn't something we can handle, reject it.
12410 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012411 }
Eric Christopherfd179292009-08-27 18:07:15 +000012412
Dan Gohman46510a72010-04-15 01:51:59 +000012413 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012414 // If we require an extra load to get this address, as in PIC mode, we
12415 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012416 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12417 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012418 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012419
Devang Patel0d881da2010-07-06 22:08:15 +000012420 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12421 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012422 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012423 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012424 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012425
Gabor Greifba36cb52008-08-28 21:40:38 +000012426 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012427 Ops.push_back(Result);
12428 return;
12429 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012430 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012431}
12432
Chris Lattner259e97c2006-01-31 19:43:35 +000012433std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012434getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012435 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012436 if (Constraint.size() == 1) {
12437 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012438 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012439 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012440 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12441 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012442 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012443 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12444 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12445 X86::R10D,X86::R11D,X86::R12D,
12446 X86::R13D,X86::R14D,X86::R15D,
12447 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012448 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012449 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12450 X86::SI, X86::DI, X86::R8W,X86::R9W,
12451 X86::R10W,X86::R11W,X86::R12W,
12452 X86::R13W,X86::R14W,X86::R15W,
12453 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012454 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012455 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12456 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12457 X86::R10B,X86::R11B,X86::R12B,
12458 X86::R13B,X86::R14B,X86::R15B,
12459 X86::BPL, X86::SPL, 0);
12460
Owen Anderson825b72b2009-08-11 20:47:22 +000012461 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012462 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12463 X86::RSI, X86::RDI, X86::R8, X86::R9,
12464 X86::R10, X86::R11, X86::R12,
12465 X86::R13, X86::R14, X86::R15,
12466 X86::RBP, X86::RSP, 0);
12467
12468 break;
12469 }
Eric Christopherfd179292009-08-27 18:07:15 +000012470 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012471 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012472 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012473 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012474 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012475 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012476 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012477 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012478 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012479 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12480 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012481 }
12482 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012483
Chris Lattner1efa40f2006-02-22 00:56:39 +000012484 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012485}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012486
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012487std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012488X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012489 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012490 // First, see if this is a constraint that directly corresponds to an LLVM
12491 // register class.
12492 if (Constraint.size() == 1) {
12493 // GCC Constraint Letters
12494 switch (Constraint[0]) {
12495 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012496 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012497 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012498 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012499 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012500 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012501 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012502 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012503 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012504 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012505 case 'R': // LEGACY_REGS
12506 if (VT == MVT::i8)
12507 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12508 if (VT == MVT::i16)
12509 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12510 if (VT == MVT::i32 || !Subtarget->is64Bit())
12511 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12512 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012513 case 'f': // FP Stack registers.
12514 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12515 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012516 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012517 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012518 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012519 return std::make_pair(0U, X86::RFP64RegisterClass);
12520 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012521 case 'y': // MMX_REGS if MMX allowed.
12522 if (!Subtarget->hasMMX()) break;
12523 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012524 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012525 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012526 // FALL THROUGH.
12527 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012528 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012529
Owen Anderson825b72b2009-08-11 20:47:22 +000012530 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012531 default: break;
12532 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012533 case MVT::f32:
12534 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012535 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012536 case MVT::f64:
12537 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012538 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012539 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012540 case MVT::v16i8:
12541 case MVT::v8i16:
12542 case MVT::v4i32:
12543 case MVT::v2i64:
12544 case MVT::v4f32:
12545 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012546 return std::make_pair(0U, X86::VR128RegisterClass);
12547 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012548 break;
12549 }
12550 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012551
Chris Lattnerf76d1802006-07-31 23:26:50 +000012552 // Use the default implementation in TargetLowering to convert the register
12553 // constraint into a member of a register class.
12554 std::pair<unsigned, const TargetRegisterClass*> Res;
12555 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012556
12557 // Not found as a standard register?
12558 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012559 // Map st(0) -> st(7) -> ST0
12560 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12561 tolower(Constraint[1]) == 's' &&
12562 tolower(Constraint[2]) == 't' &&
12563 Constraint[3] == '(' &&
12564 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12565 Constraint[5] == ')' &&
12566 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012567
Chris Lattner56d77c72009-09-13 22:41:48 +000012568 Res.first = X86::ST0+Constraint[4]-'0';
12569 Res.second = X86::RFP80RegisterClass;
12570 return Res;
12571 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012572
Chris Lattner56d77c72009-09-13 22:41:48 +000012573 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012574 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012575 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012576 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012577 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012578 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012579
12580 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012581 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012582 Res.first = X86::EFLAGS;
12583 Res.second = X86::CCRRegisterClass;
12584 return Res;
12585 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012586
Dale Johannesen330169f2008-11-13 21:52:36 +000012587 // 'A' means EAX + EDX.
12588 if (Constraint == "A") {
12589 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012590 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012591 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012592 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012593 return Res;
12594 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012595
Chris Lattnerf76d1802006-07-31 23:26:50 +000012596 // Otherwise, check to see if this is a register class of the wrong value
12597 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12598 // turn into {ax},{dx}.
12599 if (Res.second->hasType(VT))
12600 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012601
Chris Lattnerf76d1802006-07-31 23:26:50 +000012602 // All of the single-register GCC register classes map their values onto
12603 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12604 // really want an 8-bit or 32-bit register, map to the appropriate register
12605 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012606 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012607 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012608 unsigned DestReg = 0;
12609 switch (Res.first) {
12610 default: break;
12611 case X86::AX: DestReg = X86::AL; break;
12612 case X86::DX: DestReg = X86::DL; break;
12613 case X86::CX: DestReg = X86::CL; break;
12614 case X86::BX: DestReg = X86::BL; break;
12615 }
12616 if (DestReg) {
12617 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012618 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012619 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012620 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012621 unsigned DestReg = 0;
12622 switch (Res.first) {
12623 default: break;
12624 case X86::AX: DestReg = X86::EAX; break;
12625 case X86::DX: DestReg = X86::EDX; break;
12626 case X86::CX: DestReg = X86::ECX; break;
12627 case X86::BX: DestReg = X86::EBX; break;
12628 case X86::SI: DestReg = X86::ESI; break;
12629 case X86::DI: DestReg = X86::EDI; break;
12630 case X86::BP: DestReg = X86::EBP; break;
12631 case X86::SP: DestReg = X86::ESP; break;
12632 }
12633 if (DestReg) {
12634 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012635 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012636 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012637 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012638 unsigned DestReg = 0;
12639 switch (Res.first) {
12640 default: break;
12641 case X86::AX: DestReg = X86::RAX; break;
12642 case X86::DX: DestReg = X86::RDX; break;
12643 case X86::CX: DestReg = X86::RCX; break;
12644 case X86::BX: DestReg = X86::RBX; break;
12645 case X86::SI: DestReg = X86::RSI; break;
12646 case X86::DI: DestReg = X86::RDI; break;
12647 case X86::BP: DestReg = X86::RBP; break;
12648 case X86::SP: DestReg = X86::RSP; break;
12649 }
12650 if (DestReg) {
12651 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012652 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012653 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012654 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012655 } else if (Res.second == X86::FR32RegisterClass ||
12656 Res.second == X86::FR64RegisterClass ||
12657 Res.second == X86::VR128RegisterClass) {
12658 // Handle references to XMM physical registers that got mapped into the
12659 // wrong class. This can happen with constraints like {xmm0} where the
12660 // target independent register mapper will just pick the first match it can
12661 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012662 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012663 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012664 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012665 Res.second = X86::FR64RegisterClass;
12666 else if (X86::VR128RegisterClass->hasType(VT))
12667 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012668 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012669
Chris Lattnerf76d1802006-07-31 23:26:50 +000012670 return Res;
12671}