1 //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that X86 uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
16 #include "X86InstrBuilder.h"
17 #include "X86ISelLowering.h"
18 #include "X86MachineFunctionInfo.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/CallingConv.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/ADT/VectorExtras.h"
26 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
27 #include "llvm/CodeGen/CallingConvLower.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/SelectionDAG.h"
32 #include "llvm/CodeGen/SSARegMap.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Target/TargetOptions.h"
35 #include "llvm/ADT/StringExtras.h"
38 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
39 : TargetLowering(TM) {
40 Subtarget = &TM.getSubtarget<X86Subtarget>();
41 X86ScalarSSE = Subtarget->hasSSE2();
42 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
44 // Set up the TargetLowering object.
46 // X86 is weird, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
49 setSetCCResultContents(ZeroOrOneSetCCResult);
50 setSchedulingPreference(SchedulingForRegPressure);
51 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
52 setStackPointerRegisterToSaveRestore(X86StackPtr);
54 if (Subtarget->isTargetDarwin()) {
55 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
56 setUseUnderscoreSetJmp(false);
57 setUseUnderscoreLongJmp(false);
58 } else if (Subtarget->isTargetMingw()) {
59 // MS runtime is weird: it exports _setjmp, but longjmp!
60 setUseUnderscoreSetJmp(true);
61 setUseUnderscoreLongJmp(false);
63 setUseUnderscoreSetJmp(true);
64 setUseUnderscoreLongJmp(true);
67 // Set up the register classes.
68 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
69 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
70 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
71 if (Subtarget->is64Bit())
72 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
74 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
76 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
78 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
79 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
80 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
82 if (Subtarget->is64Bit()) {
83 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
84 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
87 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
88 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
90 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
93 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
95 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
96 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
97 // SSE has no i16 to fp conversion, only i32
99 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
101 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
102 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
105 if (!Subtarget->is64Bit()) {
106 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
107 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
108 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
111 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
113 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
114 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
117 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
119 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
120 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
123 // Handle FP_TO_UINT by promoting the destination to a larger signed
125 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
126 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
127 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
129 if (Subtarget->is64Bit()) {
130 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
131 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
133 if (X86ScalarSSE && !Subtarget->hasSSE3())
134 // Expand FP_TO_UINT into a select.
135 // FIXME: We would like to use a Custom expander here eventually to do
136 // the optimal thing for SSE vs. the default expansion in the legalizer.
137 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
139 // With SSE3 we can use fisttpll to convert to a signed i64.
140 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
143 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
145 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
146 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
149 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
150 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
151 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
152 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
153 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
154 if (Subtarget->is64Bit())
155 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
159 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
160 setOperationAction(ISD::FREM , MVT::f64 , Expand);
162 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
163 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
164 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
165 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
166 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
167 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
168 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
169 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
170 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
171 if (Subtarget->is64Bit()) {
172 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
173 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
174 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
177 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
178 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
180 // These should be promoted to a larger select which is supported.
181 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
182 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
183 // X86 wants to expand cmov itself.
184 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
185 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
186 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
187 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
188 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
189 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
190 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
191 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
192 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
193 if (Subtarget->is64Bit()) {
194 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
195 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
197 // X86 ret instruction may pop stack.
198 setOperationAction(ISD::RET , MVT::Other, Custom);
200 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
201 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
202 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
203 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
204 if (Subtarget->is64Bit()) {
205 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
206 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
207 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
208 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
210 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
211 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
212 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
213 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
214 // X86 wants to expand memset / memcpy itself.
215 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
216 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
218 // We don't have line number support yet.
219 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
220 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
221 // FIXME - use subtarget debug flags
222 if (!Subtarget->isTargetDarwin() &&
223 !Subtarget->isTargetELF() &&
224 !Subtarget->isTargetCygMing())
225 setOperationAction(ISD::LABEL, MVT::Other, Expand);
227 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
228 setOperationAction(ISD::VASTART , MVT::Other, Custom);
229 setOperationAction(ISD::VAARG , MVT::Other, Expand);
230 setOperationAction(ISD::VAEND , MVT::Other, Expand);
231 if (Subtarget->is64Bit())
232 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
234 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
236 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
237 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
238 if (Subtarget->is64Bit())
239 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
240 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
243 // Set up the FP register classes.
244 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
245 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
247 // Use ANDPD to simulate FABS.
248 setOperationAction(ISD::FABS , MVT::f64, Custom);
249 setOperationAction(ISD::FABS , MVT::f32, Custom);
251 // Use XORP to simulate FNEG.
252 setOperationAction(ISD::FNEG , MVT::f64, Custom);
253 setOperationAction(ISD::FNEG , MVT::f32, Custom);
255 // Use ANDPD and ORPD to simulate FCOPYSIGN.
256 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
257 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
259 // We don't support sin/cos/fmod
260 setOperationAction(ISD::FSIN , MVT::f64, Expand);
261 setOperationAction(ISD::FCOS , MVT::f64, Expand);
262 setOperationAction(ISD::FREM , MVT::f64, Expand);
263 setOperationAction(ISD::FSIN , MVT::f32, Expand);
264 setOperationAction(ISD::FCOS , MVT::f32, Expand);
265 setOperationAction(ISD::FREM , MVT::f32, Expand);
267 // Expand FP immediates into loads from the stack, except for the special
269 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
270 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
271 addLegalFPImmediate(+0.0); // xorps / xorpd
273 // Set up the FP register classes.
274 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
276 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
277 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
278 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
281 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
282 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
285 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
286 addLegalFPImmediate(+0.0); // FLD0
287 addLegalFPImmediate(+1.0); // FLD1
288 addLegalFPImmediate(-0.0); // FLD0/FCHS
289 addLegalFPImmediate(-1.0); // FLD1/FCHS
292 // First set operation action for all vector types to expand. Then we
293 // will selectively turn on ones that can be effectively codegen'd.
294 for (unsigned VT = (unsigned)MVT::Vector + 1;
295 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
296 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
297 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
298 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
299 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
300 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
301 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
302 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
303 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
304 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
305 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
306 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
307 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
308 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
309 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
310 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
313 if (Subtarget->hasMMX()) {
314 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
315 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
316 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
317 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
319 // FIXME: add MMX packed arithmetics
321 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
322 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
323 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
324 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
326 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
327 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
328 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
330 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
331 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
333 setOperationAction(ISD::AND, MVT::v8i8, Promote);
334 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
335 setOperationAction(ISD::AND, MVT::v4i16, Promote);
336 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
337 setOperationAction(ISD::AND, MVT::v2i32, Promote);
338 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
339 setOperationAction(ISD::AND, MVT::v1i64, Legal);
341 setOperationAction(ISD::OR, MVT::v8i8, Promote);
342 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
343 setOperationAction(ISD::OR, MVT::v4i16, Promote);
344 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
345 setOperationAction(ISD::OR, MVT::v2i32, Promote);
346 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
347 setOperationAction(ISD::OR, MVT::v1i64, Legal);
349 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
350 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
351 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
352 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
353 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
354 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
355 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
357 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
358 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
359 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
360 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
361 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
362 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
363 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
365 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
366 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
367 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
368 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
370 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
371 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
372 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
373 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
375 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
376 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
379 if (Subtarget->hasSSE1()) {
380 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
382 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
383 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
384 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
385 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
386 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
387 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
388 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
389 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
390 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
393 if (Subtarget->hasSSE2()) {
394 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
395 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
396 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
397 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
398 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
400 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
401 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
402 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
403 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
404 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
405 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
406 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
407 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
408 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
409 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
410 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
411 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
412 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
414 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
415 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
416 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
417 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
418 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
419 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
421 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
422 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
423 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
424 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
425 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
427 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
428 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
429 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
430 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
431 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
432 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
434 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
435 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
436 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
437 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
438 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
439 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
440 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
441 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
442 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
443 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
444 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
445 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
448 // Custom lower v2i64 and v2f64 selects.
449 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
450 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
451 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
452 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
455 // We want to custom lower some of our intrinsics.
456 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
458 // We have target-specific dag combine patterns for the following nodes:
459 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
460 setTargetDAGCombine(ISD::SELECT);
462 computeRegisterProperties();
464 // FIXME: These should be based on subtarget info. Plus, the values should
465 // be smaller when we are in optimizing for size mode.
466 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
467 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
468 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
469 allowUnalignedMemoryAccesses = true; // x86 supports it!
473 //===----------------------------------------------------------------------===//
474 // Return Value Calling Convention Implementation
475 //===----------------------------------------------------------------------===//
477 #include "X86GenCallingConv.inc"
479 /// LowerRET - Lower an ISD::RET node.
480 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
481 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
483 SmallVector<CCValAssign, 16> RVLocs;
484 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
485 CCState CCInfo(CC, getTargetMachine(), RVLocs);
486 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
489 // If this is the first return lowered for this function, add the regs to the
490 // liveout set for the function.
491 if (DAG.getMachineFunction().liveout_empty()) {
492 for (unsigned i = 0; i != RVLocs.size(); ++i)
493 if (RVLocs[i].isRegLoc())
494 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
497 SDOperand Chain = Op.getOperand(0);
500 // Copy the result values into the output registers.
501 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
502 RVLocs[0].getLocReg() != X86::ST0) {
503 for (unsigned i = 0; i != RVLocs.size(); ++i) {
504 CCValAssign &VA = RVLocs[i];
505 assert(VA.isRegLoc() && "Can only return in registers!");
506 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
508 Flag = Chain.getValue(1);
511 // We need to handle a destination of ST0 specially, because it isn't really
513 SDOperand Value = Op.getOperand(1);
515 // If this is an FP return with ScalarSSE, we need to move the value from
516 // an XMM register onto the fp-stack.
520 // If this is a load into a scalarsse value, don't store the loaded value
521 // back to the stack, only to reload it: just replace the scalar-sse load.
522 if (ISD::isNON_EXTLoad(Value.Val) &&
523 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
524 Chain = Value.getOperand(0);
525 MemLoc = Value.getOperand(1);
527 // Spill the value to memory and reload it into top of stack.
528 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
529 MachineFunction &MF = DAG.getMachineFunction();
530 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
531 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
532 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
534 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
535 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
536 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
537 Chain = Value.getValue(1);
540 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
541 SDOperand Ops[] = { Chain, Value };
542 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
543 Flag = Chain.getValue(1);
546 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
548 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
550 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
554 /// LowerCallResult - Lower the result values of an ISD::CALL into the
555 /// appropriate copies out of appropriate physical registers. This assumes that
556 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
557 /// being lowered. The returns a SDNode with the same number of values as the
559 SDNode *X86TargetLowering::
560 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
561 unsigned CallingConv, SelectionDAG &DAG) {
563 // Assign locations to each value returned by this call.
564 SmallVector<CCValAssign, 16> RVLocs;
565 CCState CCInfo(CallingConv, getTargetMachine(), RVLocs);
566 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
569 SmallVector<SDOperand, 8> ResultVals;
571 // Copy all of the result registers out of their specified physreg.
572 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
573 for (unsigned i = 0; i != RVLocs.size(); ++i) {
574 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
575 RVLocs[i].getValVT(), InFlag).getValue(1);
576 InFlag = Chain.getValue(2);
577 ResultVals.push_back(Chain.getValue(0));
580 // Copies from the FP stack are special, as ST0 isn't a valid register
581 // before the fp stackifier runs.
583 // Copy ST0 into an RFP register with FP_GET_RESULT.
584 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
585 SDOperand GROps[] = { Chain, InFlag };
586 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
587 Chain = RetVal.getValue(1);
588 InFlag = RetVal.getValue(2);
590 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
593 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
594 // shouldn't be necessary except that RFP cannot be live across
595 // multiple blocks. When stackifier is fixed, they can be uncoupled.
596 MachineFunction &MF = DAG.getMachineFunction();
597 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
598 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
600 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
602 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
603 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
604 Chain = RetVal.getValue(1);
607 if (RVLocs[0].getValVT() == MVT::f32 && !X86ScalarSSE)
608 // FIXME: we would really like to remember that this FP_ROUND
609 // operation is okay to eliminate if we allow excess FP precision.
610 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
611 ResultVals.push_back(RetVal);
614 // Merge everything together with a MERGE_VALUES node.
615 ResultVals.push_back(Chain);
616 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
617 &ResultVals[0], ResultVals.size()).Val;
621 //===----------------------------------------------------------------------===//
622 // C & StdCall Calling Convention implementation
623 //===----------------------------------------------------------------------===//
624 // StdCall calling convention seems to be standard for many Windows' API
625 // routines and around. It differs from C calling convention just a little:
626 // callee should clean up the stack, not caller. Symbols should be also
627 // decorated in some fancy way :) It doesn't support any vector arguments.
629 /// AddLiveIn - This helper function adds the specified physical register to the
630 /// MachineFunction as a live in value. It also creates a corresponding virtual
632 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
633 const TargetRegisterClass *RC) {
634 assert(RC->contains(PReg) && "Not the correct regclass!");
635 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
636 MF.addLiveIn(PReg, VReg);
640 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
642 unsigned NumArgs = Op.Val->getNumValues() - 1;
643 MachineFunction &MF = DAG.getMachineFunction();
644 MachineFrameInfo *MFI = MF.getFrameInfo();
645 SDOperand Root = Op.getOperand(0);
646 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
648 // Assign locations to all of the incoming arguments.
649 SmallVector<CCValAssign, 16> ArgLocs;
650 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
652 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
654 SmallVector<SDOperand, 8> ArgValues;
655 unsigned LastVal = ~0U;
656 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
657 CCValAssign &VA = ArgLocs[i];
658 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
660 assert(VA.getValNo() != LastVal &&
661 "Don't support value assigned to multiple locs yet");
662 LastVal = VA.getValNo();
665 MVT::ValueType RegVT = VA.getLocVT();
666 TargetRegisterClass *RC;
667 if (RegVT == MVT::i32)
668 RC = X86::GR32RegisterClass;
670 assert(MVT::isVector(RegVT));
671 RC = X86::VR128RegisterClass;
674 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
675 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
677 // If this is an 8 or 16-bit value, it is really passed promoted to 32
678 // bits. Insert an assert[sz]ext to capture this, then truncate to the
680 if (VA.getLocInfo() == CCValAssign::SExt)
681 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
682 DAG.getValueType(VA.getValVT()));
683 else if (VA.getLocInfo() == CCValAssign::ZExt)
684 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
685 DAG.getValueType(VA.getValVT()));
687 if (VA.getLocInfo() != CCValAssign::Full)
688 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
690 ArgValues.push_back(ArgValue);
692 assert(VA.isMemLoc());
694 // Create the nodes corresponding to a load from this parameter slot.
695 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
696 VA.getLocMemOffset());
697 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
698 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
702 unsigned StackSize = CCInfo.getNextStackOffset();
704 ArgValues.push_back(Root);
706 // If the function takes variable number of arguments, make a frame index for
707 // the start of the first vararg value... for expansion of llvm.va_start.
709 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
711 if (isStdCall && !isVarArg) {
712 BytesToPopOnReturn = StackSize; // Callee pops everything..
713 BytesCallerReserves = 0;
715 BytesToPopOnReturn = 0; // Callee pops nothing.
717 // If this is an sret function, the return should pop the hidden pointer.
719 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
720 ISD::ParamFlags::StructReturn))
721 BytesToPopOnReturn = 4;
723 BytesCallerReserves = StackSize;
726 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
727 ReturnAddrIndex = 0; // No return address slot generated yet.
729 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
731 // Return the new list of results.
732 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
733 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
736 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
738 SDOperand Chain = Op.getOperand(0);
739 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
740 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
741 SDOperand Callee = Op.getOperand(4);
742 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
744 // Analyze operands of the call, assigning locations to each operand.
745 SmallVector<CCValAssign, 16> ArgLocs;
746 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
747 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
749 // Get a count of how many bytes are to be pushed on the stack.
750 unsigned NumBytes = CCInfo.getNextStackOffset();
752 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
754 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
755 SmallVector<SDOperand, 8> MemOpChains;
759 // Walk the register/memloc assignments, inserting copies/loads.
760 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
761 CCValAssign &VA = ArgLocs[i];
762 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
764 // Promote the value if needed.
765 switch (VA.getLocInfo()) {
766 default: assert(0 && "Unknown loc info!");
767 case CCValAssign::Full: break;
768 case CCValAssign::SExt:
769 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
771 case CCValAssign::ZExt:
772 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
774 case CCValAssign::AExt:
775 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
780 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
782 assert(VA.isMemLoc());
783 if (StackPtr.Val == 0)
784 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
785 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
786 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
787 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
791 // If the first argument is an sret pointer, remember it.
792 bool isSRet = NumOps &&
793 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
794 ISD::ParamFlags::StructReturn);
796 if (!MemOpChains.empty())
797 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
798 &MemOpChains[0], MemOpChains.size());
800 // Build a sequence of copy-to-reg nodes chained together with token chain
801 // and flag operands which copy the outgoing args into registers.
803 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
804 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
806 InFlag = Chain.getValue(1);
809 // ELF / PIC requires GOT in the EBX register before function calls via PLT
811 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
812 Subtarget->isPICStyleGOT()) {
813 Chain = DAG.getCopyToReg(Chain, X86::EBX,
814 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
816 InFlag = Chain.getValue(1);
819 // If the callee is a GlobalAddress node (quite common, every direct call is)
820 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
821 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
822 // We should use extra load for direct calls to dllimported functions in
824 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
825 getTargetMachine(), true))
826 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
827 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
828 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
830 // Returns a chain & a flag for retval copy to use.
831 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
832 SmallVector<SDOperand, 8> Ops;
833 Ops.push_back(Chain);
834 Ops.push_back(Callee);
836 // Add argument registers to the end of the list so that they are known live
838 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
839 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
840 RegsToPass[i].second.getValueType()));
842 // Add an implicit use GOT pointer in EBX.
843 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
844 Subtarget->isPICStyleGOT())
845 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
848 Ops.push_back(InFlag);
850 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
851 NodeTys, &Ops[0], Ops.size());
852 InFlag = Chain.getValue(1);
854 // Create the CALLSEQ_END node.
855 unsigned NumBytesForCalleeToPush = 0;
857 if (CC == CallingConv::X86_StdCall) {
859 NumBytesForCalleeToPush = isSRet ? 4 : 0;
861 NumBytesForCalleeToPush = NumBytes;
863 // If this is is a call to a struct-return function, the callee
864 // pops the hidden struct pointer, so we have to push it back.
865 // This is common for Darwin/X86, Linux & Mingw32 targets.
866 NumBytesForCalleeToPush = isSRet ? 4 : 0;
869 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
871 Ops.push_back(Chain);
872 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
873 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
874 Ops.push_back(InFlag);
875 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
876 InFlag = Chain.getValue(1);
878 // Handle result values, copying them out of physregs into vregs that we
880 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
884 //===----------------------------------------------------------------------===//
885 // FastCall Calling Convention implementation
886 //===----------------------------------------------------------------------===//
888 // The X86 'fastcall' calling convention passes up to two integer arguments in
889 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
890 // and requires that the callee pop its arguments off the stack (allowing proper
891 // tail calls), and has the same return value conventions as C calling convs.
893 // This calling convention always arranges for the callee pop value to be 8n+4
894 // bytes, which is needed for tail recursion elimination and stack alignment
897 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
898 MachineFunction &MF = DAG.getMachineFunction();
899 MachineFrameInfo *MFI = MF.getFrameInfo();
900 SDOperand Root = Op.getOperand(0);
902 // Assign locations to all of the incoming arguments.
903 SmallVector<CCValAssign, 16> ArgLocs;
904 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
906 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
908 SmallVector<SDOperand, 8> ArgValues;
909 unsigned LastVal = ~0U;
910 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
911 CCValAssign &VA = ArgLocs[i];
912 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
914 assert(VA.getValNo() != LastVal &&
915 "Don't support value assigned to multiple locs yet");
916 LastVal = VA.getValNo();
919 MVT::ValueType RegVT = VA.getLocVT();
920 TargetRegisterClass *RC;
921 if (RegVT == MVT::i32)
922 RC = X86::GR32RegisterClass;
924 assert(MVT::isVector(RegVT));
925 RC = X86::VR128RegisterClass;
928 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
929 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
931 // If this is an 8 or 16-bit value, it is really passed promoted to 32
932 // bits. Insert an assert[sz]ext to capture this, then truncate to the
934 if (VA.getLocInfo() == CCValAssign::SExt)
935 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
936 DAG.getValueType(VA.getValVT()));
937 else if (VA.getLocInfo() == CCValAssign::ZExt)
938 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
939 DAG.getValueType(VA.getValVT()));
941 if (VA.getLocInfo() != CCValAssign::Full)
942 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
944 ArgValues.push_back(ArgValue);
946 assert(VA.isMemLoc());
948 // Create the nodes corresponding to a load from this parameter slot.
949 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
950 VA.getLocMemOffset());
951 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
952 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
956 ArgValues.push_back(Root);
958 unsigned StackSize = CCInfo.getNextStackOffset();
960 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
961 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
962 // arguments and the arguments after the retaddr has been pushed are aligned.
963 if ((StackSize & 7) == 0)
967 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
968 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
969 ReturnAddrIndex = 0; // No return address slot generated yet.
970 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
971 BytesCallerReserves = 0;
973 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
975 // Return the new list of results.
976 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
977 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
980 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
982 SDOperand Chain = Op.getOperand(0);
983 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
984 SDOperand Callee = Op.getOperand(4);
986 // Analyze operands of the call, assigning locations to each operand.
987 SmallVector<CCValAssign, 16> ArgLocs;
988 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
989 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
991 // Get a count of how many bytes are to be pushed on the stack.
992 unsigned NumBytes = CCInfo.getNextStackOffset();
994 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
995 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
996 // arguments and the arguments after the retaddr has been pushed are aligned.
997 if ((NumBytes & 7) == 0)
1001 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1003 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1004 SmallVector<SDOperand, 8> MemOpChains;
1008 // Walk the register/memloc assignments, inserting copies/loads.
1009 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1010 CCValAssign &VA = ArgLocs[i];
1011 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1013 // Promote the value if needed.
1014 switch (VA.getLocInfo()) {
1015 default: assert(0 && "Unknown loc info!");
1016 case CCValAssign::Full: break;
1017 case CCValAssign::SExt:
1018 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1020 case CCValAssign::ZExt:
1021 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1023 case CCValAssign::AExt:
1024 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1028 if (VA.isRegLoc()) {
1029 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1031 assert(VA.isMemLoc());
1032 if (StackPtr.Val == 0)
1033 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1034 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1035 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1036 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1040 if (!MemOpChains.empty())
1041 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1042 &MemOpChains[0], MemOpChains.size());
1044 // Build a sequence of copy-to-reg nodes chained together with token chain
1045 // and flag operands which copy the outgoing args into registers.
1047 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1048 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1050 InFlag = Chain.getValue(1);
1053 // If the callee is a GlobalAddress node (quite common, every direct call is)
1054 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1055 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1056 // We should use extra load for direct calls to dllimported functions in
1058 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1059 getTargetMachine(), true))
1060 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1061 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1062 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1064 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1066 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1067 Subtarget->isPICStyleGOT()) {
1068 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1069 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1071 InFlag = Chain.getValue(1);
1074 // Returns a chain & a flag for retval copy to use.
1075 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1076 SmallVector<SDOperand, 8> Ops;
1077 Ops.push_back(Chain);
1078 Ops.push_back(Callee);
1080 // Add argument registers to the end of the list so that they are known live
1082 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1083 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1084 RegsToPass[i].second.getValueType()));
1086 // Add an implicit use GOT pointer in EBX.
1087 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1088 Subtarget->isPICStyleGOT())
1089 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1092 Ops.push_back(InFlag);
1094 // FIXME: Do not generate X86ISD::TAILCALL for now.
1095 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1096 NodeTys, &Ops[0], Ops.size());
1097 InFlag = Chain.getValue(1);
1099 // Returns a flag for retval copy to use.
1100 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1102 Ops.push_back(Chain);
1103 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1104 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1105 Ops.push_back(InFlag);
1106 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1107 InFlag = Chain.getValue(1);
1109 // Handle result values, copying them out of physregs into vregs that we
1111 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1115 //===----------------------------------------------------------------------===//
1116 // X86-64 C Calling Convention implementation
1117 //===----------------------------------------------------------------------===//
1120 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1121 MachineFunction &MF = DAG.getMachineFunction();
1122 MachineFrameInfo *MFI = MF.getFrameInfo();
1123 SDOperand Root = Op.getOperand(0);
1124 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1126 static const unsigned GPR64ArgRegs[] = {
1127 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1129 static const unsigned XMMArgRegs[] = {
1130 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1131 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1135 // Assign locations to all of the incoming arguments.
1136 SmallVector<CCValAssign, 16> ArgLocs;
1137 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
1139 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1141 SmallVector<SDOperand, 8> ArgValues;
1142 unsigned LastVal = ~0U;
1143 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1144 CCValAssign &VA = ArgLocs[i];
1145 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1147 assert(VA.getValNo() != LastVal &&
1148 "Don't support value assigned to multiple locs yet");
1149 LastVal = VA.getValNo();
1151 if (VA.isRegLoc()) {
1152 MVT::ValueType RegVT = VA.getLocVT();
1153 TargetRegisterClass *RC;
1154 if (RegVT == MVT::i32)
1155 RC = X86::GR32RegisterClass;
1156 else if (RegVT == MVT::i64)
1157 RC = X86::GR64RegisterClass;
1158 else if (RegVT == MVT::f32)
1159 RC = X86::FR32RegisterClass;
1160 else if (RegVT == MVT::f64)
1161 RC = X86::FR64RegisterClass;
1163 assert(MVT::isVector(RegVT));
1164 RC = X86::VR128RegisterClass;
1167 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1168 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1170 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1171 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1173 if (VA.getLocInfo() == CCValAssign::SExt)
1174 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1175 DAG.getValueType(VA.getValVT()));
1176 else if (VA.getLocInfo() == CCValAssign::ZExt)
1177 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1178 DAG.getValueType(VA.getValVT()));
1180 if (VA.getLocInfo() != CCValAssign::Full)
1181 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1183 ArgValues.push_back(ArgValue);
1185 assert(VA.isMemLoc());
1187 // Create the nodes corresponding to a load from this parameter slot.
1188 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1189 VA.getLocMemOffset());
1190 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1191 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1195 unsigned StackSize = CCInfo.getNextStackOffset();
1197 // If the function takes variable number of arguments, make a frame index for
1198 // the start of the first vararg value... for expansion of llvm.va_start.
1200 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1201 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1203 // For X86-64, if there are vararg parameters that are passed via
1204 // registers, then we must store them to their spots on the stack so they
1205 // may be loaded by deferencing the result of va_next.
1206 VarArgsGPOffset = NumIntRegs * 8;
1207 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1208 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1209 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1211 // Store the integer parameter registers.
1212 SmallVector<SDOperand, 8> MemOps;
1213 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1214 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1215 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1216 for (; NumIntRegs != 6; ++NumIntRegs) {
1217 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1218 X86::GR64RegisterClass);
1219 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1220 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1221 MemOps.push_back(Store);
1222 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1223 DAG.getConstant(8, getPointerTy()));
1226 // Now store the XMM (fp + vector) parameter registers.
1227 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1228 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1229 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1230 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1231 X86::VR128RegisterClass);
1232 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1233 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1234 MemOps.push_back(Store);
1235 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1236 DAG.getConstant(16, getPointerTy()));
1238 if (!MemOps.empty())
1239 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1240 &MemOps[0], MemOps.size());
1243 ArgValues.push_back(Root);
1245 ReturnAddrIndex = 0; // No return address slot generated yet.
1246 BytesToPopOnReturn = 0; // Callee pops nothing.
1247 BytesCallerReserves = StackSize;
1249 // Return the new list of results.
1250 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1251 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1255 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1257 SDOperand Chain = Op.getOperand(0);
1258 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1259 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1260 SDOperand Callee = Op.getOperand(4);
1262 // Analyze operands of the call, assigning locations to each operand.
1263 SmallVector<CCValAssign, 16> ArgLocs;
1264 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
1265 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1267 // Get a count of how many bytes are to be pushed on the stack.
1268 unsigned NumBytes = CCInfo.getNextStackOffset();
1269 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1271 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1272 SmallVector<SDOperand, 8> MemOpChains;
1276 // Walk the register/memloc assignments, inserting copies/loads.
1277 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1278 CCValAssign &VA = ArgLocs[i];
1279 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1281 // Promote the value if needed.
1282 switch (VA.getLocInfo()) {
1283 default: assert(0 && "Unknown loc info!");
1284 case CCValAssign::Full: break;
1285 case CCValAssign::SExt:
1286 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1288 case CCValAssign::ZExt:
1289 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1291 case CCValAssign::AExt:
1292 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1296 if (VA.isRegLoc()) {
1297 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1299 assert(VA.isMemLoc());
1300 if (StackPtr.Val == 0)
1301 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1302 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1303 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1304 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1308 if (!MemOpChains.empty())
1309 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1310 &MemOpChains[0], MemOpChains.size());
1312 // Build a sequence of copy-to-reg nodes chained together with token chain
1313 // and flag operands which copy the outgoing args into registers.
1315 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1316 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1318 InFlag = Chain.getValue(1);
1322 // From AMD64 ABI document:
1323 // For calls that may call functions that use varargs or stdargs
1324 // (prototype-less calls or calls to functions containing ellipsis (...) in
1325 // the declaration) %al is used as hidden argument to specify the number
1326 // of SSE registers used. The contents of %al do not need to match exactly
1327 // the number of registers, but must be an ubound on the number of SSE
1328 // registers used and is in the range 0 - 8 inclusive.
1330 // Count the number of XMM registers allocated.
1331 static const unsigned XMMArgRegs[] = {
1332 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1333 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1335 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1337 Chain = DAG.getCopyToReg(Chain, X86::AL,
1338 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1339 InFlag = Chain.getValue(1);
1342 // If the callee is a GlobalAddress node (quite common, every direct call is)
1343 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1344 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1345 // We should use extra load for direct calls to dllimported functions in
1347 if (getTargetMachine().getCodeModel() != CodeModel::Large
1348 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1349 getTargetMachine(), true))
1350 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1351 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1352 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1353 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1355 // Returns a chain & a flag for retval copy to use.
1356 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1357 SmallVector<SDOperand, 8> Ops;
1358 Ops.push_back(Chain);
1359 Ops.push_back(Callee);
1361 // Add argument registers to the end of the list so that they are known live
1363 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1364 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1365 RegsToPass[i].second.getValueType()));
1368 Ops.push_back(InFlag);
1370 // FIXME: Do not generate X86ISD::TAILCALL for now.
1371 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1372 NodeTys, &Ops[0], Ops.size());
1373 InFlag = Chain.getValue(1);
1375 // Returns a flag for retval copy to use.
1376 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1378 Ops.push_back(Chain);
1379 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1380 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1381 Ops.push_back(InFlag);
1382 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1383 InFlag = Chain.getValue(1);
1385 // Handle result values, copying them out of physregs into vregs that we
1387 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1391 //===----------------------------------------------------------------------===//
1392 // Other Lowering Hooks
1393 //===----------------------------------------------------------------------===//
1396 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1397 if (ReturnAddrIndex == 0) {
1398 // Set up a frame object for the return address.
1399 MachineFunction &MF = DAG.getMachineFunction();
1400 if (Subtarget->is64Bit())
1401 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1403 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1406 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1411 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1412 /// specific condition code. It returns a false if it cannot do a direct
1413 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1415 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1416 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1417 SelectionDAG &DAG) {
1418 X86CC = X86::COND_INVALID;
1420 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1421 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1422 // X > -1 -> X == 0, jump !sign.
1423 RHS = DAG.getConstant(0, RHS.getValueType());
1424 X86CC = X86::COND_NS;
1426 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1427 // X < 0 -> X == 0, jump on sign.
1428 X86CC = X86::COND_S;
1433 switch (SetCCOpcode) {
1435 case ISD::SETEQ: X86CC = X86::COND_E; break;
1436 case ISD::SETGT: X86CC = X86::COND_G; break;
1437 case ISD::SETGE: X86CC = X86::COND_GE; break;
1438 case ISD::SETLT: X86CC = X86::COND_L; break;
1439 case ISD::SETLE: X86CC = X86::COND_LE; break;
1440 case ISD::SETNE: X86CC = X86::COND_NE; break;
1441 case ISD::SETULT: X86CC = X86::COND_B; break;
1442 case ISD::SETUGT: X86CC = X86::COND_A; break;
1443 case ISD::SETULE: X86CC = X86::COND_BE; break;
1444 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1447 // On a floating point condition, the flags are set as follows:
1449 // 0 | 0 | 0 | X > Y
1450 // 0 | 0 | 1 | X < Y
1451 // 1 | 0 | 0 | X == Y
1452 // 1 | 1 | 1 | unordered
1454 switch (SetCCOpcode) {
1457 case ISD::SETEQ: X86CC = X86::COND_E; break;
1458 case ISD::SETOLT: Flip = true; // Fallthrough
1460 case ISD::SETGT: X86CC = X86::COND_A; break;
1461 case ISD::SETOLE: Flip = true; // Fallthrough
1463 case ISD::SETGE: X86CC = X86::COND_AE; break;
1464 case ISD::SETUGT: Flip = true; // Fallthrough
1466 case ISD::SETLT: X86CC = X86::COND_B; break;
1467 case ISD::SETUGE: Flip = true; // Fallthrough
1469 case ISD::SETLE: X86CC = X86::COND_BE; break;
1471 case ISD::SETNE: X86CC = X86::COND_NE; break;
1472 case ISD::SETUO: X86CC = X86::COND_P; break;
1473 case ISD::SETO: X86CC = X86::COND_NP; break;
1476 std::swap(LHS, RHS);
1479 return X86CC != X86::COND_INVALID;
1482 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1483 /// code. Current x86 isa includes the following FP cmov instructions:
1484 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1485 static bool hasFPCMov(unsigned X86CC) {
1501 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1502 /// true if Op is undef or if its value falls within the specified range (L, H].
1503 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1504 if (Op.getOpcode() == ISD::UNDEF)
1507 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1508 return (Val >= Low && Val < Hi);
1511 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1512 /// true if Op is undef or if its value equal to the specified value.
1513 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1514 if (Op.getOpcode() == ISD::UNDEF)
1516 return cast<ConstantSDNode>(Op)->getValue() == Val;
1519 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1520 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1521 bool X86::isPSHUFDMask(SDNode *N) {
1522 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1524 if (N->getNumOperands() != 4)
1527 // Check if the value doesn't reference the second vector.
1528 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1529 SDOperand Arg = N->getOperand(i);
1530 if (Arg.getOpcode() == ISD::UNDEF) continue;
1531 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1532 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1539 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1540 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1541 bool X86::isPSHUFHWMask(SDNode *N) {
1542 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1544 if (N->getNumOperands() != 8)
1547 // Lower quadword copied in order.
1548 for (unsigned i = 0; i != 4; ++i) {
1549 SDOperand Arg = N->getOperand(i);
1550 if (Arg.getOpcode() == ISD::UNDEF) continue;
1551 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1552 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1556 // Upper quadword shuffled.
1557 for (unsigned i = 4; i != 8; ++i) {
1558 SDOperand Arg = N->getOperand(i);
1559 if (Arg.getOpcode() == ISD::UNDEF) continue;
1560 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1561 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1562 if (Val < 4 || Val > 7)
1569 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1570 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1571 bool X86::isPSHUFLWMask(SDNode *N) {
1572 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1574 if (N->getNumOperands() != 8)
1577 // Upper quadword copied in order.
1578 for (unsigned i = 4; i != 8; ++i)
1579 if (!isUndefOrEqual(N->getOperand(i), i))
1582 // Lower quadword shuffled.
1583 for (unsigned i = 0; i != 4; ++i)
1584 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1590 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1591 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1592 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1593 if (NumElems != 2 && NumElems != 4) return false;
1595 unsigned Half = NumElems / 2;
1596 for (unsigned i = 0; i < Half; ++i)
1597 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1599 for (unsigned i = Half; i < NumElems; ++i)
1600 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1606 bool X86::isSHUFPMask(SDNode *N) {
1607 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1608 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1611 /// isCommutedSHUFP - Returns true if the shuffle mask is except
1612 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1613 /// half elements to come from vector 1 (which would equal the dest.) and
1614 /// the upper half to come from vector 2.
1615 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1616 if (NumOps != 2 && NumOps != 4) return false;
1618 unsigned Half = NumOps / 2;
1619 for (unsigned i = 0; i < Half; ++i)
1620 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1622 for (unsigned i = Half; i < NumOps; ++i)
1623 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1628 static bool isCommutedSHUFP(SDNode *N) {
1629 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1630 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1633 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1634 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1635 bool X86::isMOVHLPSMask(SDNode *N) {
1636 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1638 if (N->getNumOperands() != 4)
1641 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1642 return isUndefOrEqual(N->getOperand(0), 6) &&
1643 isUndefOrEqual(N->getOperand(1), 7) &&
1644 isUndefOrEqual(N->getOperand(2), 2) &&
1645 isUndefOrEqual(N->getOperand(3), 3);
1648 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1649 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1651 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1652 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1654 if (N->getNumOperands() != 4)
1657 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1658 return isUndefOrEqual(N->getOperand(0), 2) &&
1659 isUndefOrEqual(N->getOperand(1), 3) &&
1660 isUndefOrEqual(N->getOperand(2), 2) &&
1661 isUndefOrEqual(N->getOperand(3), 3);
1664 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1665 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1666 bool X86::isMOVLPMask(SDNode *N) {
1667 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1669 unsigned NumElems = N->getNumOperands();
1670 if (NumElems != 2 && NumElems != 4)
1673 for (unsigned i = 0; i < NumElems/2; ++i)
1674 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1677 for (unsigned i = NumElems/2; i < NumElems; ++i)
1678 if (!isUndefOrEqual(N->getOperand(i), i))
1684 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1685 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1687 bool X86::isMOVHPMask(SDNode *N) {
1688 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1690 unsigned NumElems = N->getNumOperands();
1691 if (NumElems != 2 && NumElems != 4)
1694 for (unsigned i = 0; i < NumElems/2; ++i)
1695 if (!isUndefOrEqual(N->getOperand(i), i))
1698 for (unsigned i = 0; i < NumElems/2; ++i) {
1699 SDOperand Arg = N->getOperand(i + NumElems/2);
1700 if (!isUndefOrEqual(Arg, i + NumElems))
1707 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1708 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1709 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1710 bool V2IsSplat = false) {
1711 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1714 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1715 SDOperand BitI = Elts[i];
1716 SDOperand BitI1 = Elts[i+1];
1717 if (!isUndefOrEqual(BitI, j))
1720 if (isUndefOrEqual(BitI1, NumElts))
1723 if (!isUndefOrEqual(BitI1, j + NumElts))
1731 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1732 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1733 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1736 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1737 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1738 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1739 bool V2IsSplat = false) {
1740 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1743 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1744 SDOperand BitI = Elts[i];
1745 SDOperand BitI1 = Elts[i+1];
1746 if (!isUndefOrEqual(BitI, j + NumElts/2))
1749 if (isUndefOrEqual(BitI1, NumElts))
1752 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1760 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1761 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1762 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1765 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1766 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1768 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1769 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1771 unsigned NumElems = N->getNumOperands();
1772 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1775 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1776 SDOperand BitI = N->getOperand(i);
1777 SDOperand BitI1 = N->getOperand(i+1);
1779 if (!isUndefOrEqual(BitI, j))
1781 if (!isUndefOrEqual(BitI1, j))
1788 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1789 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1790 /// MOVSD, and MOVD, i.e. setting the lowest element.
1791 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1792 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1795 if (!isUndefOrEqual(Elts[0], NumElts))
1798 for (unsigned i = 1; i < NumElts; ++i) {
1799 if (!isUndefOrEqual(Elts[i], i))
1806 bool X86::isMOVLMask(SDNode *N) {
1807 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1808 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
1811 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1812 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1813 /// element of vector 2 and the other elements to come from vector 1 in order.
1814 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1815 bool V2IsSplat = false,
1816 bool V2IsUndef = false) {
1817 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
1820 if (!isUndefOrEqual(Ops[0], 0))
1823 for (unsigned i = 1; i < NumOps; ++i) {
1824 SDOperand Arg = Ops[i];
1825 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1826 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1827 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
1834 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1835 bool V2IsUndef = false) {
1836 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1837 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1838 V2IsSplat, V2IsUndef);
1841 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1842 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1843 bool X86::isMOVSHDUPMask(SDNode *N) {
1844 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1846 if (N->getNumOperands() != 4)
1849 // Expect 1, 1, 3, 3
1850 for (unsigned i = 0; i < 2; ++i) {
1851 SDOperand Arg = N->getOperand(i);
1852 if (Arg.getOpcode() == ISD::UNDEF) continue;
1853 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1854 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1855 if (Val != 1) return false;
1859 for (unsigned i = 2; i < 4; ++i) {
1860 SDOperand Arg = N->getOperand(i);
1861 if (Arg.getOpcode() == ISD::UNDEF) continue;
1862 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1863 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1864 if (Val != 3) return false;
1868 // Don't use movshdup if it can be done with a shufps.
1872 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1873 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1874 bool X86::isMOVSLDUPMask(SDNode *N) {
1875 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1877 if (N->getNumOperands() != 4)
1880 // Expect 0, 0, 2, 2
1881 for (unsigned i = 0; i < 2; ++i) {
1882 SDOperand Arg = N->getOperand(i);
1883 if (Arg.getOpcode() == ISD::UNDEF) continue;
1884 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1885 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1886 if (Val != 0) return false;
1890 for (unsigned i = 2; i < 4; ++i) {
1891 SDOperand Arg = N->getOperand(i);
1892 if (Arg.getOpcode() == ISD::UNDEF) continue;
1893 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1894 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1895 if (Val != 2) return false;
1899 // Don't use movshdup if it can be done with a shufps.
1903 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1904 /// a splat of a single element.
1905 static bool isSplatMask(SDNode *N) {
1906 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1908 // This is a splat operation if each element of the permute is the same, and
1909 // if the value doesn't reference the second vector.
1910 unsigned NumElems = N->getNumOperands();
1911 SDOperand ElementBase;
1913 for (; i != NumElems; ++i) {
1914 SDOperand Elt = N->getOperand(i);
1915 if (isa<ConstantSDNode>(Elt)) {
1921 if (!ElementBase.Val)
1924 for (; i != NumElems; ++i) {
1925 SDOperand Arg = N->getOperand(i);
1926 if (Arg.getOpcode() == ISD::UNDEF) continue;
1927 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1928 if (Arg != ElementBase) return false;
1931 // Make sure it is a splat of the first vector operand.
1932 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
1935 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1936 /// a splat of a single element and it's a 2 or 4 element mask.
1937 bool X86::isSplatMask(SDNode *N) {
1938 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1940 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
1941 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1943 return ::isSplatMask(N);
1946 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
1947 /// specifies a splat of zero element.
1948 bool X86::isSplatLoMask(SDNode *N) {
1949 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1951 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
1952 if (!isUndefOrEqual(N->getOperand(i), 0))
1957 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1958 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1960 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
1961 unsigned NumOperands = N->getNumOperands();
1962 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1964 for (unsigned i = 0; i < NumOperands; ++i) {
1966 SDOperand Arg = N->getOperand(NumOperands-i-1);
1967 if (Arg.getOpcode() != ISD::UNDEF)
1968 Val = cast<ConstantSDNode>(Arg)->getValue();
1969 if (Val >= NumOperands) Val -= NumOperands;
1971 if (i != NumOperands - 1)
1978 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1979 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1981 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1983 // 8 nodes, but we only care about the last 4.
1984 for (unsigned i = 7; i >= 4; --i) {
1986 SDOperand Arg = N->getOperand(i);
1987 if (Arg.getOpcode() != ISD::UNDEF)
1988 Val = cast<ConstantSDNode>(Arg)->getValue();
1997 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1998 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2000 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2002 // 8 nodes, but we only care about the first 4.
2003 for (int i = 3; i >= 0; --i) {
2005 SDOperand Arg = N->getOperand(i);
2006 if (Arg.getOpcode() != ISD::UNDEF)
2007 Val = cast<ConstantSDNode>(Arg)->getValue();
2016 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2017 /// specifies a 8 element shuffle that can be broken into a pair of
2018 /// PSHUFHW and PSHUFLW.
2019 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2020 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2022 if (N->getNumOperands() != 8)
2025 // Lower quadword shuffled.
2026 for (unsigned i = 0; i != 4; ++i) {
2027 SDOperand Arg = N->getOperand(i);
2028 if (Arg.getOpcode() == ISD::UNDEF) continue;
2029 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2030 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2035 // Upper quadword shuffled.
2036 for (unsigned i = 4; i != 8; ++i) {
2037 SDOperand Arg = N->getOperand(i);
2038 if (Arg.getOpcode() == ISD::UNDEF) continue;
2039 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2040 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2041 if (Val < 4 || Val > 7)
2048 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2049 /// values in ther permute mask.
2050 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2051 SDOperand &V2, SDOperand &Mask,
2052 SelectionDAG &DAG) {
2053 MVT::ValueType VT = Op.getValueType();
2054 MVT::ValueType MaskVT = Mask.getValueType();
2055 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2056 unsigned NumElems = Mask.getNumOperands();
2057 SmallVector<SDOperand, 8> MaskVec;
2059 for (unsigned i = 0; i != NumElems; ++i) {
2060 SDOperand Arg = Mask.getOperand(i);
2061 if (Arg.getOpcode() == ISD::UNDEF) {
2062 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2065 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2066 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2068 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2070 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2074 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2075 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2078 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2079 /// match movhlps. The lower half elements should come from upper half of
2080 /// V1 (and in order), and the upper half elements should come from the upper
2081 /// half of V2 (and in order).
2082 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2083 unsigned NumElems = Mask->getNumOperands();
2086 for (unsigned i = 0, e = 2; i != e; ++i)
2087 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2089 for (unsigned i = 2; i != 4; ++i)
2090 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2095 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2096 /// is promoted to a vector.
2097 static inline bool isScalarLoadToVector(SDNode *N) {
2098 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2099 N = N->getOperand(0).Val;
2100 return ISD::isNON_EXTLoad(N);
2105 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2106 /// match movlp{s|d}. The lower half elements should come from lower half of
2107 /// V1 (and in order), and the upper half elements should come from the upper
2108 /// half of V2 (and in order). And since V1 will become the source of the
2109 /// MOVLP, it must be either a vector load or a scalar load to vector.
2110 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2111 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2113 // Is V2 is a vector load, don't do this transformation. We will try to use
2114 // load folding shufps op.
2115 if (ISD::isNON_EXTLoad(V2))
2118 unsigned NumElems = Mask->getNumOperands();
2119 if (NumElems != 2 && NumElems != 4)
2121 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2122 if (!isUndefOrEqual(Mask->getOperand(i), i))
2124 for (unsigned i = NumElems/2; i != NumElems; ++i)
2125 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2130 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2132 static bool isSplatVector(SDNode *N) {
2133 if (N->getOpcode() != ISD::BUILD_VECTOR)
2136 SDOperand SplatValue = N->getOperand(0);
2137 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2138 if (N->getOperand(i) != SplatValue)
2143 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2145 static bool isUndefShuffle(SDNode *N) {
2146 if (N->getOpcode() != ISD::BUILD_VECTOR)
2149 SDOperand V1 = N->getOperand(0);
2150 SDOperand V2 = N->getOperand(1);
2151 SDOperand Mask = N->getOperand(2);
2152 unsigned NumElems = Mask.getNumOperands();
2153 for (unsigned i = 0; i != NumElems; ++i) {
2154 SDOperand Arg = Mask.getOperand(i);
2155 if (Arg.getOpcode() != ISD::UNDEF) {
2156 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2157 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2159 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2166 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2167 /// that point to V2 points to its first element.
2168 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2169 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2171 bool Changed = false;
2172 SmallVector<SDOperand, 8> MaskVec;
2173 unsigned NumElems = Mask.getNumOperands();
2174 for (unsigned i = 0; i != NumElems; ++i) {
2175 SDOperand Arg = Mask.getOperand(i);
2176 if (Arg.getOpcode() != ISD::UNDEF) {
2177 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2178 if (Val > NumElems) {
2179 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2183 MaskVec.push_back(Arg);
2187 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2188 &MaskVec[0], MaskVec.size());
2192 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2193 /// operation of specified width.
2194 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2195 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2196 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2198 SmallVector<SDOperand, 8> MaskVec;
2199 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2200 for (unsigned i = 1; i != NumElems; ++i)
2201 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2202 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2205 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2206 /// of specified width.
2207 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2208 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2209 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2210 SmallVector<SDOperand, 8> MaskVec;
2211 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2212 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2213 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2215 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2218 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2219 /// of specified width.
2220 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2221 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2222 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2223 unsigned Half = NumElems/2;
2224 SmallVector<SDOperand, 8> MaskVec;
2225 for (unsigned i = 0; i != Half; ++i) {
2226 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2227 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2229 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2232 /// getZeroVector - Returns a vector of specified type with all zero elements.
2234 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2235 assert(MVT::isVector(VT) && "Expected a vector type");
2236 unsigned NumElems = getVectorNumElements(VT);
2237 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2238 bool isFP = MVT::isFloatingPoint(EVT);
2239 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2240 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2241 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2244 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2246 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2247 SDOperand V1 = Op.getOperand(0);
2248 SDOperand Mask = Op.getOperand(2);
2249 MVT::ValueType VT = Op.getValueType();
2250 unsigned NumElems = Mask.getNumOperands();
2251 Mask = getUnpacklMask(NumElems, DAG);
2252 while (NumElems != 4) {
2253 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2256 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2258 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2259 Mask = getZeroVector(MaskVT, DAG);
2260 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2261 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2262 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2265 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2267 static inline bool isZeroNode(SDOperand Elt) {
2268 return ((isa<ConstantSDNode>(Elt) &&
2269 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2270 (isa<ConstantFPSDNode>(Elt) &&
2271 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2274 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2275 /// vector and zero or undef vector.
2276 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2277 unsigned NumElems, unsigned Idx,
2278 bool isZero, SelectionDAG &DAG) {
2279 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2280 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2281 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2282 SDOperand Zero = DAG.getConstant(0, EVT);
2283 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2284 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2285 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2286 &MaskVec[0], MaskVec.size());
2287 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2290 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2292 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2293 unsigned NumNonZero, unsigned NumZero,
2294 SelectionDAG &DAG, TargetLowering &TLI) {
2300 for (unsigned i = 0; i < 16; ++i) {
2301 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2302 if (ThisIsNonZero && First) {
2304 V = getZeroVector(MVT::v8i16, DAG);
2306 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2311 SDOperand ThisElt(0, 0), LastElt(0, 0);
2312 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2313 if (LastIsNonZero) {
2314 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2316 if (ThisIsNonZero) {
2317 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2318 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2319 ThisElt, DAG.getConstant(8, MVT::i8));
2321 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2326 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2327 DAG.getConstant(i/2, TLI.getPointerTy()));
2331 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2334 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2336 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2337 unsigned NumNonZero, unsigned NumZero,
2338 SelectionDAG &DAG, TargetLowering &TLI) {
2344 for (unsigned i = 0; i < 8; ++i) {
2345 bool isNonZero = (NonZeros & (1 << i)) != 0;
2349 V = getZeroVector(MVT::v8i16, DAG);
2351 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2354 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2355 DAG.getConstant(i, TLI.getPointerTy()));
2363 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2364 // All zero's are handled with pxor.
2365 if (ISD::isBuildVectorAllZeros(Op.Val))
2368 // All one's are handled with pcmpeqd.
2369 if (ISD::isBuildVectorAllOnes(Op.Val))
2372 MVT::ValueType VT = Op.getValueType();
2373 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2374 unsigned EVTBits = MVT::getSizeInBits(EVT);
2376 unsigned NumElems = Op.getNumOperands();
2377 unsigned NumZero = 0;
2378 unsigned NumNonZero = 0;
2379 unsigned NonZeros = 0;
2380 std::set<SDOperand> Values;
2381 for (unsigned i = 0; i < NumElems; ++i) {
2382 SDOperand Elt = Op.getOperand(i);
2383 if (Elt.getOpcode() != ISD::UNDEF) {
2385 if (isZeroNode(Elt))
2388 NonZeros |= (1 << i);
2394 if (NumNonZero == 0)
2395 // Must be a mix of zero and undef. Return a zero vector.
2396 return getZeroVector(VT, DAG);
2398 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2399 if (Values.size() == 1)
2402 // Special case for single non-zero element.
2403 if (NumNonZero == 1) {
2404 unsigned Idx = CountTrailingZeros_32(NonZeros);
2405 SDOperand Item = Op.getOperand(Idx);
2406 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2408 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2409 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2412 if (EVTBits == 32) {
2413 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2414 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2416 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2417 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2418 SmallVector<SDOperand, 8> MaskVec;
2419 for (unsigned i = 0; i < NumElems; i++)
2420 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2421 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2422 &MaskVec[0], MaskVec.size());
2423 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2424 DAG.getNode(ISD::UNDEF, VT), Mask);
2428 // Let legalizer expand 2-wide build_vector's.
2432 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2433 if (EVTBits == 8 && NumElems == 16) {
2434 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2436 if (V.Val) return V;
2439 if (EVTBits == 16 && NumElems == 8) {
2440 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2442 if (V.Val) return V;
2445 // If element VT is == 32 bits, turn it into a number of shuffles.
2446 SmallVector<SDOperand, 8> V;
2448 if (NumElems == 4 && NumZero > 0) {
2449 for (unsigned i = 0; i < 4; ++i) {
2450 bool isZero = !(NonZeros & (1 << i));
2452 V[i] = getZeroVector(VT, DAG);
2454 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2457 for (unsigned i = 0; i < 2; ++i) {
2458 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2461 V[i] = V[i*2]; // Must be a zero vector.
2464 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2465 getMOVLMask(NumElems, DAG));
2468 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2469 getMOVLMask(NumElems, DAG));
2472 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2473 getUnpacklMask(NumElems, DAG));
2478 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2479 // clears the upper bits.
2480 // FIXME: we can do the same for v4f32 case when we know both parts of
2481 // the lower half come from scalar_to_vector (loadf32). We should do
2482 // that in post legalizer dag combiner with target specific hooks.
2483 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2485 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2486 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2487 SmallVector<SDOperand, 8> MaskVec;
2488 bool Reverse = (NonZeros & 0x3) == 2;
2489 for (unsigned i = 0; i < 2; ++i)
2491 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2493 MaskVec.push_back(DAG.getConstant(i, EVT));
2494 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2495 for (unsigned i = 0; i < 2; ++i)
2497 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2499 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2500 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2501 &MaskVec[0], MaskVec.size());
2502 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2505 if (Values.size() > 2) {
2506 // Expand into a number of unpckl*.
2508 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2509 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2510 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2511 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2512 for (unsigned i = 0; i < NumElems; ++i)
2513 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2515 while (NumElems != 0) {
2516 for (unsigned i = 0; i < NumElems; ++i)
2517 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2528 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2529 SDOperand V1 = Op.getOperand(0);
2530 SDOperand V2 = Op.getOperand(1);
2531 SDOperand PermMask = Op.getOperand(2);
2532 MVT::ValueType VT = Op.getValueType();
2533 unsigned NumElems = PermMask.getNumOperands();
2534 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2535 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2536 bool V1IsSplat = false;
2537 bool V2IsSplat = false;
2539 if (isUndefShuffle(Op.Val))
2540 return DAG.getNode(ISD::UNDEF, VT);
2542 if (isSplatMask(PermMask.Val)) {
2543 if (NumElems <= 4) return Op;
2544 // Promote it to a v4i32 splat.
2545 return PromoteSplat(Op, DAG);
2548 if (X86::isMOVLMask(PermMask.Val))
2549 return (V1IsUndef) ? V2 : Op;
2551 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2552 X86::isMOVSLDUPMask(PermMask.Val) ||
2553 X86::isMOVHLPSMask(PermMask.Val) ||
2554 X86::isMOVHPMask(PermMask.Val) ||
2555 X86::isMOVLPMask(PermMask.Val))
2558 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2559 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2560 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2562 bool Commuted = false;
2563 V1IsSplat = isSplatVector(V1.Val);
2564 V2IsSplat = isSplatVector(V2.Val);
2565 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2566 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2567 std::swap(V1IsSplat, V2IsSplat);
2568 std::swap(V1IsUndef, V2IsUndef);
2572 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2573 if (V2IsUndef) return V1;
2574 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2576 // V2 is a splat, so the mask may be malformed. That is, it may point
2577 // to any V2 element. The instruction selectior won't like this. Get
2578 // a corrected mask and commute to form a proper MOVS{S|D}.
2579 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2580 if (NewMask.Val != PermMask.Val)
2581 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2586 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2587 X86::isUNPCKLMask(PermMask.Val) ||
2588 X86::isUNPCKHMask(PermMask.Val))
2592 // Normalize mask so all entries that point to V2 points to its first
2593 // element then try to match unpck{h|l} again. If match, return a
2594 // new vector_shuffle with the corrected mask.
2595 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2596 if (NewMask.Val != PermMask.Val) {
2597 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2598 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2599 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2600 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2601 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2602 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2607 // Normalize the node to match x86 shuffle ops if needed
2608 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2609 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2612 // Commute is back and try unpck* again.
2613 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2614 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2615 X86::isUNPCKLMask(PermMask.Val) ||
2616 X86::isUNPCKHMask(PermMask.Val))
2620 // If VT is integer, try PSHUF* first, then SHUFP*.
2621 if (MVT::isInteger(VT)) {
2622 if (X86::isPSHUFDMask(PermMask.Val) ||
2623 X86::isPSHUFHWMask(PermMask.Val) ||
2624 X86::isPSHUFLWMask(PermMask.Val)) {
2625 if (V2.getOpcode() != ISD::UNDEF)
2626 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2627 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2631 if (X86::isSHUFPMask(PermMask.Val))
2634 // Handle v8i16 shuffle high / low shuffle node pair.
2635 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2636 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2637 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2638 SmallVector<SDOperand, 8> MaskVec;
2639 for (unsigned i = 0; i != 4; ++i)
2640 MaskVec.push_back(PermMask.getOperand(i));
2641 for (unsigned i = 4; i != 8; ++i)
2642 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2643 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2644 &MaskVec[0], MaskVec.size());
2645 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2647 for (unsigned i = 0; i != 4; ++i)
2648 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2649 for (unsigned i = 4; i != 8; ++i)
2650 MaskVec.push_back(PermMask.getOperand(i));
2651 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2652 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2655 // Floating point cases in the other order.
2656 if (X86::isSHUFPMask(PermMask.Val))
2658 if (X86::isPSHUFDMask(PermMask.Val) ||
2659 X86::isPSHUFHWMask(PermMask.Val) ||
2660 X86::isPSHUFLWMask(PermMask.Val)) {
2661 if (V2.getOpcode() != ISD::UNDEF)
2662 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2663 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2668 if (NumElems == 4) {
2669 MVT::ValueType MaskVT = PermMask.getValueType();
2670 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2671 SmallVector<std::pair<int, int>, 8> Locs;
2672 Locs.reserve(NumElems);
2673 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2674 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2677 // If no more than two elements come from either vector. This can be
2678 // implemented with two shuffles. First shuffle gather the elements.
2679 // The second shuffle, which takes the first shuffle as both of its
2680 // vector operands, put the elements into the right order.
2681 for (unsigned i = 0; i != NumElems; ++i) {
2682 SDOperand Elt = PermMask.getOperand(i);
2683 if (Elt.getOpcode() == ISD::UNDEF) {
2684 Locs[i] = std::make_pair(-1, -1);
2686 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2687 if (Val < NumElems) {
2688 Locs[i] = std::make_pair(0, NumLo);
2692 Locs[i] = std::make_pair(1, NumHi);
2693 if (2+NumHi < NumElems)
2694 Mask1[2+NumHi] = Elt;
2699 if (NumLo <= 2 && NumHi <= 2) {
2700 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2701 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2702 &Mask1[0], Mask1.size()));
2703 for (unsigned i = 0; i != NumElems; ++i) {
2704 if (Locs[i].first == -1)
2707 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2708 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2709 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2713 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2714 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2715 &Mask2[0], Mask2.size()));
2718 // Break it into (shuffle shuffle_hi, shuffle_lo).
2720 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2721 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2722 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
2723 unsigned MaskIdx = 0;
2725 unsigned HiIdx = NumElems/2;
2726 for (unsigned i = 0; i != NumElems; ++i) {
2727 if (i == NumElems/2) {
2733 SDOperand Elt = PermMask.getOperand(i);
2734 if (Elt.getOpcode() == ISD::UNDEF) {
2735 Locs[i] = std::make_pair(-1, -1);
2736 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2737 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2738 (*MaskPtr)[LoIdx] = Elt;
2741 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2742 (*MaskPtr)[HiIdx] = Elt;
2747 SDOperand LoShuffle =
2748 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2749 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2750 &LoMask[0], LoMask.size()));
2751 SDOperand HiShuffle =
2752 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2753 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2754 &HiMask[0], HiMask.size()));
2755 SmallVector<SDOperand, 8> MaskOps;
2756 for (unsigned i = 0; i != NumElems; ++i) {
2757 if (Locs[i].first == -1) {
2758 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2760 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2761 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2764 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2765 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2766 &MaskOps[0], MaskOps.size()));
2773 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2774 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2777 MVT::ValueType VT = Op.getValueType();
2778 // TODO: handle v16i8.
2779 if (MVT::getSizeInBits(VT) == 16) {
2780 // Transform it so it match pextrw which produces a 32-bit result.
2781 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2782 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2783 Op.getOperand(0), Op.getOperand(1));
2784 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2785 DAG.getValueType(VT));
2786 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2787 } else if (MVT::getSizeInBits(VT) == 32) {
2788 SDOperand Vec = Op.getOperand(0);
2789 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2792 // SHUFPS the element to the lowest double word, then movss.
2793 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2794 SmallVector<SDOperand, 8> IdxVec;
2795 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2796 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2797 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2798 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2799 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2800 &IdxVec[0], IdxVec.size());
2801 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2802 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2803 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2804 DAG.getConstant(0, getPointerTy()));
2805 } else if (MVT::getSizeInBits(VT) == 64) {
2806 SDOperand Vec = Op.getOperand(0);
2807 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2811 // UNPCKHPD the element to the lowest double word, then movsd.
2812 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2813 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2814 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2815 SmallVector<SDOperand, 8> IdxVec;
2816 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2817 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2818 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2819 &IdxVec[0], IdxVec.size());
2820 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2821 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2822 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2823 DAG.getConstant(0, getPointerTy()));
2830 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2831 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2832 // as its second argument.
2833 MVT::ValueType VT = Op.getValueType();
2834 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2835 SDOperand N0 = Op.getOperand(0);
2836 SDOperand N1 = Op.getOperand(1);
2837 SDOperand N2 = Op.getOperand(2);
2838 if (MVT::getSizeInBits(BaseVT) == 16) {
2839 if (N1.getValueType() != MVT::i32)
2840 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2841 if (N2.getValueType() != MVT::i32)
2842 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2843 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2844 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2845 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2848 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2849 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2850 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2851 SmallVector<SDOperand, 8> MaskVec;
2852 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2853 for (unsigned i = 1; i <= 3; ++i)
2854 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2855 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2856 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2857 &MaskVec[0], MaskVec.size()));
2859 // Use two pinsrw instructions to insert a 32 bit value.
2861 if (MVT::isFloatingPoint(N1.getValueType())) {
2862 if (ISD::isNON_EXTLoad(N1.Val)) {
2863 // Just load directly from f32mem to GR32.
2864 LoadSDNode *LD = cast<LoadSDNode>(N1);
2865 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
2866 LD->getSrcValue(), LD->getSrcValueOffset());
2868 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2869 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2870 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2871 DAG.getConstant(0, getPointerTy()));
2874 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2875 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2876 DAG.getConstant(Idx, getPointerTy()));
2877 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2878 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2879 DAG.getConstant(Idx+1, getPointerTy()));
2880 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2888 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2889 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2890 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2893 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2894 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2895 // one of the above mentioned nodes. It has to be wrapped because otherwise
2896 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2897 // be used to form addressing mode. These wrapped nodes will be selected
2900 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2901 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2902 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
2904 CP->getAlignment());
2905 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2906 // With PIC, the address is actually $g + Offset.
2907 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2908 !Subtarget->isPICStyleRIPRel()) {
2909 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2910 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2918 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2919 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2920 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
2921 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2922 // With PIC, the address is actually $g + Offset.
2923 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2924 !Subtarget->isPICStyleRIPRel()) {
2925 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2926 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2930 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
2931 // load the value at address GV, not the value of GV itself. This means that
2932 // the GlobalAddress must be in the base or index register of the address, not
2933 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
2934 // The same applies for external symbols during PIC codegen
2935 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
2936 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
2942 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2943 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2944 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
2945 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2946 // With PIC, the address is actually $g + Offset.
2947 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2948 !Subtarget->isPICStyleRIPRel()) {
2949 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2950 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2957 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
2958 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
2959 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
2960 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2961 // With PIC, the address is actually $g + Offset.
2962 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2963 !Subtarget->isPICStyleRIPRel()) {
2964 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2965 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2972 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
2973 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2974 "Not an i64 shift!");
2975 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2976 SDOperand ShOpLo = Op.getOperand(0);
2977 SDOperand ShOpHi = Op.getOperand(1);
2978 SDOperand ShAmt = Op.getOperand(2);
2979 SDOperand Tmp1 = isSRA ?
2980 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
2981 DAG.getConstant(0, MVT::i32);
2983 SDOperand Tmp2, Tmp3;
2984 if (Op.getOpcode() == ISD::SHL_PARTS) {
2985 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2986 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2988 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
2989 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
2992 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
2993 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
2994 DAG.getConstant(32, MVT::i8));
2995 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
2996 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
2999 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3001 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3002 SmallVector<SDOperand, 4> Ops;
3003 if (Op.getOpcode() == ISD::SHL_PARTS) {
3004 Ops.push_back(Tmp2);
3005 Ops.push_back(Tmp3);
3007 Ops.push_back(InFlag);
3008 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3009 InFlag = Hi.getValue(1);
3012 Ops.push_back(Tmp3);
3013 Ops.push_back(Tmp1);
3015 Ops.push_back(InFlag);
3016 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3018 Ops.push_back(Tmp2);
3019 Ops.push_back(Tmp3);
3021 Ops.push_back(InFlag);
3022 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3023 InFlag = Lo.getValue(1);
3026 Ops.push_back(Tmp3);
3027 Ops.push_back(Tmp1);
3029 Ops.push_back(InFlag);
3030 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3033 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3037 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3040 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3041 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3042 Op.getOperand(0).getValueType() >= MVT::i16 &&
3043 "Unknown SINT_TO_FP to lower!");
3046 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3047 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3048 MachineFunction &MF = DAG.getMachineFunction();
3049 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3050 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3051 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3052 StackSlot, NULL, 0);
3057 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3059 Tys = DAG.getVTList(MVT::f64, MVT::Other);
3060 SmallVector<SDOperand, 8> Ops;
3061 Ops.push_back(Chain);
3062 Ops.push_back(StackSlot);
3063 Ops.push_back(DAG.getValueType(SrcVT));
3064 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3065 Tys, &Ops[0], Ops.size());
3068 Chain = Result.getValue(1);
3069 SDOperand InFlag = Result.getValue(2);
3071 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3072 // shouldn't be necessary except that RFP cannot be live across
3073 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3074 MachineFunction &MF = DAG.getMachineFunction();
3075 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3076 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3077 Tys = DAG.getVTList(MVT::Other);
3078 SmallVector<SDOperand, 8> Ops;
3079 Ops.push_back(Chain);
3080 Ops.push_back(Result);
3081 Ops.push_back(StackSlot);
3082 Ops.push_back(DAG.getValueType(Op.getValueType()));
3083 Ops.push_back(InFlag);
3084 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3085 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3091 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3092 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3093 "Unknown FP_TO_SINT to lower!");
3094 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3096 MachineFunction &MF = DAG.getMachineFunction();
3097 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3098 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3099 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3102 switch (Op.getValueType()) {
3103 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3104 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3105 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3106 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3109 SDOperand Chain = DAG.getEntryNode();
3110 SDOperand Value = Op.getOperand(0);
3112 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3113 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3114 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
3116 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3118 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3119 Chain = Value.getValue(1);
3120 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3121 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3124 // Build the FP_TO_INT*_IN_MEM
3125 SDOperand Ops[] = { Chain, Value, StackSlot };
3126 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3129 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3132 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3133 MVT::ValueType VT = Op.getValueType();
3134 const Type *OpNTy = MVT::getTypeForValueType(VT);
3135 std::vector<Constant*> CV;
3136 if (VT == MVT::f64) {
3137 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3138 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3140 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3141 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3142 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3143 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3145 Constant *CS = ConstantStruct::get(CV);
3146 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3147 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
3148 SmallVector<SDOperand, 3> Ops;
3149 Ops.push_back(DAG.getEntryNode());
3150 Ops.push_back(CPIdx);
3151 Ops.push_back(DAG.getSrcValue(NULL));
3152 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3153 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3156 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3157 MVT::ValueType VT = Op.getValueType();
3158 const Type *OpNTy = MVT::getTypeForValueType(VT);
3159 std::vector<Constant*> CV;
3160 if (VT == MVT::f64) {
3161 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3162 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3164 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3165 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3166 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3167 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3169 Constant *CS = ConstantStruct::get(CV);
3170 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3171 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
3172 SmallVector<SDOperand, 3> Ops;
3173 Ops.push_back(DAG.getEntryNode());
3174 Ops.push_back(CPIdx);
3175 Ops.push_back(DAG.getSrcValue(NULL));
3176 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3177 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3180 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3181 SDOperand Op0 = Op.getOperand(0);
3182 SDOperand Op1 = Op.getOperand(1);
3183 MVT::ValueType VT = Op.getValueType();
3184 MVT::ValueType SrcVT = Op1.getValueType();
3185 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3187 // If second operand is smaller, extend it first.
3188 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3189 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3193 // First get the sign bit of second operand.
3194 std::vector<Constant*> CV;
3195 if (SrcVT == MVT::f64) {
3196 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3197 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3199 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3200 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3201 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3202 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3204 Constant *CS = ConstantStruct::get(CV);
3205 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3206 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other);
3207 SmallVector<SDOperand, 3> Ops;
3208 Ops.push_back(DAG.getEntryNode());
3209 Ops.push_back(CPIdx);
3210 Ops.push_back(DAG.getSrcValue(NULL));
3211 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3212 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3214 // Shift sign bit right or left if the two operands have different types.
3215 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3216 // Op0 is MVT::f32, Op1 is MVT::f64.
3217 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3218 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3219 DAG.getConstant(32, MVT::i32));
3220 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3221 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3222 DAG.getConstant(0, getPointerTy()));
3225 // Clear first operand sign bit.
3227 if (VT == MVT::f64) {
3228 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3229 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3231 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3232 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3233 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3234 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3236 CS = ConstantStruct::get(CV);
3237 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3238 Tys = DAG.getVTList(VT, MVT::Other);
3240 Ops.push_back(DAG.getEntryNode());
3241 Ops.push_back(CPIdx);
3242 Ops.push_back(DAG.getSrcValue(NULL));
3243 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3244 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3246 // Or the value with the sign bit.
3247 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3250 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3252 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3254 SDOperand Op0 = Op.getOperand(0);
3255 SDOperand Op1 = Op.getOperand(1);
3256 SDOperand CC = Op.getOperand(2);
3257 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3258 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3259 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3260 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3263 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3265 SDOperand Ops1[] = { Chain, Op0, Op1 };
3266 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3267 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3268 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3271 assert(isFP && "Illegal integer SetCC!");
3273 SDOperand COps[] = { Chain, Op0, Op1 };
3274 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3276 switch (SetCCOpcode) {
3277 default: assert(false && "Illegal floating point SetCC!");
3278 case ISD::SETOEQ: { // !PF & ZF
3279 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3280 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3281 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3283 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3284 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3286 case ISD::SETUNE: { // PF | !ZF
3287 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3288 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3289 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3291 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3292 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3297 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3298 bool addTest = true;
3299 SDOperand Chain = DAG.getEntryNode();
3300 SDOperand Cond = Op.getOperand(0);
3302 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3304 if (Cond.getOpcode() == ISD::SETCC)
3305 Cond = LowerSETCC(Cond, DAG, Chain);
3307 if (Cond.getOpcode() == X86ISD::SETCC) {
3308 CC = Cond.getOperand(0);
3310 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3311 // (since flag operand cannot be shared). Use it as the condition setting
3312 // operand in place of the X86ISD::SETCC.
3313 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3314 // to use a test instead of duplicating the X86ISD::CMP (for register
3315 // pressure reason)?
3316 SDOperand Cmp = Cond.getOperand(1);
3317 unsigned Opc = Cmp.getOpcode();
3318 bool IllegalFPCMov = !X86ScalarSSE &&
3319 MVT::isFloatingPoint(Op.getValueType()) &&
3320 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3321 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3323 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3324 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3330 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3331 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3332 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3335 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3336 SmallVector<SDOperand, 4> Ops;
3337 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3338 // condition is true.
3339 Ops.push_back(Op.getOperand(2));
3340 Ops.push_back(Op.getOperand(1));
3342 Ops.push_back(Cond.getValue(1));
3343 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3346 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3347 bool addTest = true;
3348 SDOperand Chain = Op.getOperand(0);
3349 SDOperand Cond = Op.getOperand(1);
3350 SDOperand Dest = Op.getOperand(2);
3352 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3354 if (Cond.getOpcode() == ISD::SETCC)
3355 Cond = LowerSETCC(Cond, DAG, Chain);
3357 if (Cond.getOpcode() == X86ISD::SETCC) {
3358 CC = Cond.getOperand(0);
3360 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3361 // (since flag operand cannot be shared). Use it as the condition setting
3362 // operand in place of the X86ISD::SETCC.
3363 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3364 // to use a test instead of duplicating the X86ISD::CMP (for register
3365 // pressure reason)?
3366 SDOperand Cmp = Cond.getOperand(1);
3367 unsigned Opc = Cmp.getOpcode();
3368 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3369 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3370 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3376 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3377 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3378 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3380 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3381 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3384 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3385 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3387 if (Subtarget->is64Bit())
3388 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3390 switch (CallingConv) {
3392 assert(0 && "Unsupported calling convention");
3393 case CallingConv::Fast:
3394 // TODO: Implement fastcc
3396 case CallingConv::C:
3397 case CallingConv::X86_StdCall:
3398 return LowerCCCCallTo(Op, DAG, CallingConv);
3399 case CallingConv::X86_FastCall:
3400 return LowerFastCCCallTo(Op, DAG, CallingConv);
3405 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3406 MachineFunction &MF = DAG.getMachineFunction();
3407 const Function* Fn = MF.getFunction();
3408 if (Fn->hasExternalLinkage() &&
3409 Subtarget->isTargetCygMing() &&
3410 Fn->getName() == "main")
3411 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3413 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3414 if (Subtarget->is64Bit())
3415 return LowerX86_64CCCArguments(Op, DAG);
3419 assert(0 && "Unsupported calling convention");
3420 case CallingConv::Fast:
3421 // TODO: implement fastcc.
3424 case CallingConv::C:
3425 return LowerCCCArguments(Op, DAG);
3426 case CallingConv::X86_StdCall:
3427 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
3428 return LowerCCCArguments(Op, DAG, true);
3429 case CallingConv::X86_FastCall:
3430 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
3431 return LowerFastCCArguments(Op, DAG);
3435 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3436 SDOperand InFlag(0, 0);
3437 SDOperand Chain = Op.getOperand(0);
3439 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3440 if (Align == 0) Align = 1;
3442 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3443 // If not DWORD aligned, call memset if size is less than the threshold.
3444 // It knows how to align to the right boundary first.
3445 if ((Align & 3) != 0 ||
3446 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3447 MVT::ValueType IntPtr = getPointerTy();
3448 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3449 TargetLowering::ArgListTy Args;
3450 TargetLowering::ArgListEntry Entry;
3451 Entry.Node = Op.getOperand(1);
3452 Entry.Ty = IntPtrTy;
3453 Args.push_back(Entry);
3454 // Extend the unsigned i8 argument to be an int value for the call.
3455 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3456 Entry.Ty = IntPtrTy;
3457 Args.push_back(Entry);
3458 Entry.Node = Op.getOperand(3);
3459 Args.push_back(Entry);
3460 std::pair<SDOperand,SDOperand> CallResult =
3461 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3462 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3463 return CallResult.second;
3468 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3469 unsigned BytesLeft = 0;
3470 bool TwoRepStos = false;
3473 uint64_t Val = ValC->getValue() & 255;
3475 // If the value is a constant, then we can potentially use larger sets.
3476 switch (Align & 3) {
3477 case 2: // WORD aligned
3480 Val = (Val << 8) | Val;
3482 case 0: // DWORD aligned
3485 Val = (Val << 8) | Val;
3486 Val = (Val << 16) | Val;
3487 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3490 Val = (Val << 32) | Val;
3493 default: // Byte aligned
3496 Count = Op.getOperand(3);
3500 if (AVT > MVT::i8) {
3502 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3503 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3504 BytesLeft = I->getValue() % UBytes;
3506 assert(AVT >= MVT::i32 &&
3507 "Do not use rep;stos if not at least DWORD aligned");
3508 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3509 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3514 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3516 InFlag = Chain.getValue(1);
3519 Count = Op.getOperand(3);
3520 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3521 InFlag = Chain.getValue(1);
3524 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3526 InFlag = Chain.getValue(1);
3527 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3528 Op.getOperand(1), InFlag);
3529 InFlag = Chain.getValue(1);
3531 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3532 SmallVector<SDOperand, 8> Ops;
3533 Ops.push_back(Chain);
3534 Ops.push_back(DAG.getValueType(AVT));
3535 Ops.push_back(InFlag);
3536 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3539 InFlag = Chain.getValue(1);
3540 Count = Op.getOperand(3);
3541 MVT::ValueType CVT = Count.getValueType();
3542 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3543 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3544 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3546 InFlag = Chain.getValue(1);
3547 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3549 Ops.push_back(Chain);
3550 Ops.push_back(DAG.getValueType(MVT::i8));
3551 Ops.push_back(InFlag);
3552 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3553 } else if (BytesLeft) {
3554 // Issue stores for the last 1 - 7 bytes.
3556 unsigned Val = ValC->getValue() & 255;
3557 unsigned Offset = I->getValue() - BytesLeft;
3558 SDOperand DstAddr = Op.getOperand(1);
3559 MVT::ValueType AddrVT = DstAddr.getValueType();
3560 if (BytesLeft >= 4) {
3561 Val = (Val << 8) | Val;
3562 Val = (Val << 16) | Val;
3563 Value = DAG.getConstant(Val, MVT::i32);
3564 Chain = DAG.getStore(Chain, Value,
3565 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3566 DAG.getConstant(Offset, AddrVT)),
3571 if (BytesLeft >= 2) {
3572 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3573 Chain = DAG.getStore(Chain, Value,
3574 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3575 DAG.getConstant(Offset, AddrVT)),
3580 if (BytesLeft == 1) {
3581 Value = DAG.getConstant(Val, MVT::i8);
3582 Chain = DAG.getStore(Chain, Value,
3583 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3584 DAG.getConstant(Offset, AddrVT)),
3592 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3593 SDOperand Chain = Op.getOperand(0);
3595 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3596 if (Align == 0) Align = 1;
3598 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3599 // If not DWORD aligned, call memcpy if size is less than the threshold.
3600 // It knows how to align to the right boundary first.
3601 if ((Align & 3) != 0 ||
3602 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3603 MVT::ValueType IntPtr = getPointerTy();
3604 TargetLowering::ArgListTy Args;
3605 TargetLowering::ArgListEntry Entry;
3606 Entry.Ty = getTargetData()->getIntPtrType();
3607 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3608 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3609 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
3610 std::pair<SDOperand,SDOperand> CallResult =
3611 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3612 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3613 return CallResult.second;
3618 unsigned BytesLeft = 0;
3619 bool TwoRepMovs = false;
3620 switch (Align & 3) {
3621 case 2: // WORD aligned
3624 case 0: // DWORD aligned
3626 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3629 default: // Byte aligned
3631 Count = Op.getOperand(3);
3635 if (AVT > MVT::i8) {
3637 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3638 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3639 BytesLeft = I->getValue() % UBytes;
3641 assert(AVT >= MVT::i32 &&
3642 "Do not use rep;movs if not at least DWORD aligned");
3643 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3644 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3649 SDOperand InFlag(0, 0);
3650 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3652 InFlag = Chain.getValue(1);
3653 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3654 Op.getOperand(1), InFlag);
3655 InFlag = Chain.getValue(1);
3656 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3657 Op.getOperand(2), InFlag);
3658 InFlag = Chain.getValue(1);
3660 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3661 SmallVector<SDOperand, 8> Ops;
3662 Ops.push_back(Chain);
3663 Ops.push_back(DAG.getValueType(AVT));
3664 Ops.push_back(InFlag);
3665 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3668 InFlag = Chain.getValue(1);
3669 Count = Op.getOperand(3);
3670 MVT::ValueType CVT = Count.getValueType();
3671 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3672 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3673 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3675 InFlag = Chain.getValue(1);
3676 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3678 Ops.push_back(Chain);
3679 Ops.push_back(DAG.getValueType(MVT::i8));
3680 Ops.push_back(InFlag);
3681 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3682 } else if (BytesLeft) {
3683 // Issue loads and stores for the last 1 - 7 bytes.
3684 unsigned Offset = I->getValue() - BytesLeft;
3685 SDOperand DstAddr = Op.getOperand(1);
3686 MVT::ValueType DstVT = DstAddr.getValueType();
3687 SDOperand SrcAddr = Op.getOperand(2);
3688 MVT::ValueType SrcVT = SrcAddr.getValueType();
3690 if (BytesLeft >= 4) {
3691 Value = DAG.getLoad(MVT::i32, Chain,
3692 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3693 DAG.getConstant(Offset, SrcVT)),
3695 Chain = Value.getValue(1);
3696 Chain = DAG.getStore(Chain, Value,
3697 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3698 DAG.getConstant(Offset, DstVT)),
3703 if (BytesLeft >= 2) {
3704 Value = DAG.getLoad(MVT::i16, Chain,
3705 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3706 DAG.getConstant(Offset, SrcVT)),
3708 Chain = Value.getValue(1);
3709 Chain = DAG.getStore(Chain, Value,
3710 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3711 DAG.getConstant(Offset, DstVT)),
3717 if (BytesLeft == 1) {
3718 Value = DAG.getLoad(MVT::i8, Chain,
3719 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3720 DAG.getConstant(Offset, SrcVT)),
3722 Chain = Value.getValue(1);
3723 Chain = DAG.getStore(Chain, Value,
3724 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3725 DAG.getConstant(Offset, DstVT)),
3734 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3735 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3736 SDOperand TheOp = Op.getOperand(0);
3737 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
3738 if (Subtarget->is64Bit()) {
3739 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
3740 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
3741 MVT::i64, Copy1.getValue(2));
3742 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
3743 DAG.getConstant(32, MVT::i8));
3745 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
3748 Tys = DAG.getVTList(MVT::i64, MVT::Other);
3749 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
3752 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
3753 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
3754 MVT::i32, Copy1.getValue(2));
3755 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
3756 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
3757 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
3760 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3761 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
3763 if (!Subtarget->is64Bit()) {
3764 // vastart just stores the address of the VarArgsFrameIndex slot into the
3765 // memory location argument.
3766 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
3767 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
3772 // gp_offset (0 - 6 * 8)
3773 // fp_offset (48 - 48 + 8 * 16)
3774 // overflow_arg_area (point to parameters coming in memory).
3776 SmallVector<SDOperand, 8> MemOps;
3777 SDOperand FIN = Op.getOperand(1);
3779 SDOperand Store = DAG.getStore(Op.getOperand(0),
3780 DAG.getConstant(VarArgsGPOffset, MVT::i32),
3781 FIN, SV->getValue(), SV->getOffset());
3782 MemOps.push_back(Store);
3785 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3786 DAG.getConstant(4, getPointerTy()));
3787 Store = DAG.getStore(Op.getOperand(0),
3788 DAG.getConstant(VarArgsFPOffset, MVT::i32),
3789 FIN, SV->getValue(), SV->getOffset());
3790 MemOps.push_back(Store);
3792 // Store ptr to overflow_arg_area
3793 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3794 DAG.getConstant(4, getPointerTy()));
3795 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
3796 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
3798 MemOps.push_back(Store);
3800 // Store ptr to reg_save_area.
3801 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3802 DAG.getConstant(8, getPointerTy()));
3803 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
3804 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
3806 MemOps.push_back(Store);
3807 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
3810 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
3811 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
3812 SDOperand Chain = Op.getOperand(0);
3813 SDOperand DstPtr = Op.getOperand(1);
3814 SDOperand SrcPtr = Op.getOperand(2);
3815 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
3816 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
3818 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
3819 SrcSV->getValue(), SrcSV->getOffset());
3820 Chain = SrcPtr.getValue(1);
3821 for (unsigned i = 0; i < 3; ++i) {
3822 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
3823 SrcSV->getValue(), SrcSV->getOffset());
3824 Chain = Val.getValue(1);
3825 Chain = DAG.getStore(Chain, Val, DstPtr,
3826 DstSV->getValue(), DstSV->getOffset());
3829 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
3830 DAG.getConstant(8, getPointerTy()));
3831 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
3832 DAG.getConstant(8, getPointerTy()));
3838 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3839 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3841 default: return SDOperand(); // Don't custom lower most intrinsics.
3842 // Comparison intrinsics.
3843 case Intrinsic::x86_sse_comieq_ss:
3844 case Intrinsic::x86_sse_comilt_ss:
3845 case Intrinsic::x86_sse_comile_ss:
3846 case Intrinsic::x86_sse_comigt_ss:
3847 case Intrinsic::x86_sse_comige_ss:
3848 case Intrinsic::x86_sse_comineq_ss:
3849 case Intrinsic::x86_sse_ucomieq_ss:
3850 case Intrinsic::x86_sse_ucomilt_ss:
3851 case Intrinsic::x86_sse_ucomile_ss:
3852 case Intrinsic::x86_sse_ucomigt_ss:
3853 case Intrinsic::x86_sse_ucomige_ss:
3854 case Intrinsic::x86_sse_ucomineq_ss:
3855 case Intrinsic::x86_sse2_comieq_sd:
3856 case Intrinsic::x86_sse2_comilt_sd:
3857 case Intrinsic::x86_sse2_comile_sd:
3858 case Intrinsic::x86_sse2_comigt_sd:
3859 case Intrinsic::x86_sse2_comige_sd:
3860 case Intrinsic::x86_sse2_comineq_sd:
3861 case Intrinsic::x86_sse2_ucomieq_sd:
3862 case Intrinsic::x86_sse2_ucomilt_sd:
3863 case Intrinsic::x86_sse2_ucomile_sd:
3864 case Intrinsic::x86_sse2_ucomigt_sd:
3865 case Intrinsic::x86_sse2_ucomige_sd:
3866 case Intrinsic::x86_sse2_ucomineq_sd: {
3868 ISD::CondCode CC = ISD::SETCC_INVALID;
3871 case Intrinsic::x86_sse_comieq_ss:
3872 case Intrinsic::x86_sse2_comieq_sd:
3876 case Intrinsic::x86_sse_comilt_ss:
3877 case Intrinsic::x86_sse2_comilt_sd:
3881 case Intrinsic::x86_sse_comile_ss:
3882 case Intrinsic::x86_sse2_comile_sd:
3886 case Intrinsic::x86_sse_comigt_ss:
3887 case Intrinsic::x86_sse2_comigt_sd:
3891 case Intrinsic::x86_sse_comige_ss:
3892 case Intrinsic::x86_sse2_comige_sd:
3896 case Intrinsic::x86_sse_comineq_ss:
3897 case Intrinsic::x86_sse2_comineq_sd:
3901 case Intrinsic::x86_sse_ucomieq_ss:
3902 case Intrinsic::x86_sse2_ucomieq_sd:
3903 Opc = X86ISD::UCOMI;
3906 case Intrinsic::x86_sse_ucomilt_ss:
3907 case Intrinsic::x86_sse2_ucomilt_sd:
3908 Opc = X86ISD::UCOMI;
3911 case Intrinsic::x86_sse_ucomile_ss:
3912 case Intrinsic::x86_sse2_ucomile_sd:
3913 Opc = X86ISD::UCOMI;
3916 case Intrinsic::x86_sse_ucomigt_ss:
3917 case Intrinsic::x86_sse2_ucomigt_sd:
3918 Opc = X86ISD::UCOMI;
3921 case Intrinsic::x86_sse_ucomige_ss:
3922 case Intrinsic::x86_sse2_ucomige_sd:
3923 Opc = X86ISD::UCOMI;
3926 case Intrinsic::x86_sse_ucomineq_ss:
3927 case Intrinsic::x86_sse2_ucomineq_sd:
3928 Opc = X86ISD::UCOMI;
3934 SDOperand LHS = Op.getOperand(1);
3935 SDOperand RHS = Op.getOperand(2);
3936 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
3938 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3939 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
3940 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
3941 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3942 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3943 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
3944 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
3949 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
3950 // Depths > 0 not supported yet!
3951 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3954 // Just load the return address
3955 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
3956 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
3959 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
3960 // Depths > 0 not supported yet!
3961 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3964 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
3965 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
3966 DAG.getConstant(4, getPointerTy()));
3969 /// LowerOperation - Provide custom lowering hooks for some operations.
3971 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3972 switch (Op.getOpcode()) {
3973 default: assert(0 && "Should not custom lower this!");
3974 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3975 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3976 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3977 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3978 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3979 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3980 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3981 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3982 case ISD::SHL_PARTS:
3983 case ISD::SRA_PARTS:
3984 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3985 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3986 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3987 case ISD::FABS: return LowerFABS(Op, DAG);
3988 case ISD::FNEG: return LowerFNEG(Op, DAG);
3989 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
3990 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
3991 case ISD::SELECT: return LowerSELECT(Op, DAG);
3992 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3993 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3994 case ISD::CALL: return LowerCALL(Op, DAG);
3995 case ISD::RET: return LowerRET(Op, DAG);
3996 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
3997 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3998 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3999 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4000 case ISD::VASTART: return LowerVASTART(Op, DAG);
4001 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
4002 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4003 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4004 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4009 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4011 default: return NULL;
4012 case X86ISD::SHLD: return "X86ISD::SHLD";
4013 case X86ISD::SHRD: return "X86ISD::SHRD";
4014 case X86ISD::FAND: return "X86ISD::FAND";
4015 case X86ISD::FOR: return "X86ISD::FOR";
4016 case X86ISD::FXOR: return "X86ISD::FXOR";
4017 case X86ISD::FSRL: return "X86ISD::FSRL";
4018 case X86ISD::FILD: return "X86ISD::FILD";
4019 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4020 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4021 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4022 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4023 case X86ISD::FLD: return "X86ISD::FLD";
4024 case X86ISD::FST: return "X86ISD::FST";
4025 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4026 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4027 case X86ISD::CALL: return "X86ISD::CALL";
4028 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4029 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4030 case X86ISD::CMP: return "X86ISD::CMP";
4031 case X86ISD::COMI: return "X86ISD::COMI";
4032 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4033 case X86ISD::SETCC: return "X86ISD::SETCC";
4034 case X86ISD::CMOV: return "X86ISD::CMOV";
4035 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4036 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4037 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4038 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4039 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
4040 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
4041 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4042 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4043 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4044 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4045 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4046 case X86ISD::FMAX: return "X86ISD::FMAX";
4047 case X86ISD::FMIN: return "X86ISD::FMIN";
4051 // isLegalAddressingMode - Return true if the addressing mode represented
4052 // by AM is legal for this target, for a load/store of the specified type.
4053 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4054 const Type *Ty) const {
4055 // X86 supports extremely general addressing modes.
4057 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4058 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4062 // X86-64 only supports addr of globals in small code model.
4063 if (Subtarget->is64Bit() &&
4064 getTargetMachine().getCodeModel() != CodeModel::Small)
4067 // We can only fold this if we don't need a load either.
4068 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4078 // These scales always work.
4083 // These scales are formed with basereg+scalereg. Only accept if there is
4088 default: // Other stuff never works.
4096 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4097 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4098 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4099 /// are assumed to be legal.
4101 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4102 // Only do shuffles on 128-bit vector types for now.
4103 if (MVT::getSizeInBits(VT) == 64) return false;
4104 return (Mask.Val->getNumOperands() <= 4 ||
4105 isSplatMask(Mask.Val) ||
4106 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4107 X86::isUNPCKLMask(Mask.Val) ||
4108 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4109 X86::isUNPCKHMask(Mask.Val));
4112 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4114 SelectionDAG &DAG) const {
4115 unsigned NumElts = BVOps.size();
4116 // Only do shuffles on 128-bit vector types for now.
4117 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4118 if (NumElts == 2) return true;
4120 return (isMOVLMask(&BVOps[0], 4) ||
4121 isCommutedMOVL(&BVOps[0], 4, true) ||
4122 isSHUFPMask(&BVOps[0], 4) ||
4123 isCommutedSHUFP(&BVOps[0], 4));
4128 //===----------------------------------------------------------------------===//
4129 // X86 Scheduler Hooks
4130 //===----------------------------------------------------------------------===//
4133 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4134 MachineBasicBlock *BB) {
4135 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4136 switch (MI->getOpcode()) {
4137 default: assert(false && "Unexpected instr type to insert");
4138 case X86::CMOV_FR32:
4139 case X86::CMOV_FR64:
4140 case X86::CMOV_V4F32:
4141 case X86::CMOV_V2F64:
4142 case X86::CMOV_V2I64: {
4143 // To "insert" a SELECT_CC instruction, we actually have to insert the
4144 // diamond control-flow pattern. The incoming instruction knows the
4145 // destination vreg to set, the condition code register to branch on, the
4146 // true/false values to select between, and a branch opcode to use.
4147 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4148 ilist<MachineBasicBlock>::iterator It = BB;
4154 // cmpTY ccX, r1, r2
4156 // fallthrough --> copy0MBB
4157 MachineBasicBlock *thisMBB = BB;
4158 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4159 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4161 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4162 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4163 MachineFunction *F = BB->getParent();
4164 F->getBasicBlockList().insert(It, copy0MBB);
4165 F->getBasicBlockList().insert(It, sinkMBB);
4166 // Update machine-CFG edges by first adding all successors of the current
4167 // block to the new block which will contain the Phi node for the select.
4168 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4169 e = BB->succ_end(); i != e; ++i)
4170 sinkMBB->addSuccessor(*i);
4171 // Next, remove all successors of the current block, and add the true
4172 // and fallthrough blocks as its successors.
4173 while(!BB->succ_empty())
4174 BB->removeSuccessor(BB->succ_begin());
4175 BB->addSuccessor(copy0MBB);
4176 BB->addSuccessor(sinkMBB);
4179 // %FalseValue = ...
4180 // # fallthrough to sinkMBB
4183 // Update machine-CFG edges
4184 BB->addSuccessor(sinkMBB);
4187 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4190 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4191 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4192 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4194 delete MI; // The pseudo instruction is gone now.
4198 case X86::FP_TO_INT16_IN_MEM:
4199 case X86::FP_TO_INT32_IN_MEM:
4200 case X86::FP_TO_INT64_IN_MEM: {
4201 // Change the floating point control register to use "round towards zero"
4202 // mode when truncating to an integer value.
4203 MachineFunction *F = BB->getParent();
4204 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4205 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4207 // Load the old value of the high byte of the control word...
4209 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4210 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4212 // Set the high part to be round to zero...
4213 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4216 // Reload the modified control word now...
4217 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4219 // Restore the memory image of control word to original value
4220 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4223 // Get the X86 opcode to use.
4225 switch (MI->getOpcode()) {
4226 default: assert(0 && "illegal opcode!");
4227 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4228 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4229 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4233 MachineOperand &Op = MI->getOperand(0);
4234 if (Op.isRegister()) {
4235 AM.BaseType = X86AddressMode::RegBase;
4236 AM.Base.Reg = Op.getReg();
4238 AM.BaseType = X86AddressMode::FrameIndexBase;
4239 AM.Base.FrameIndex = Op.getFrameIndex();
4241 Op = MI->getOperand(1);
4242 if (Op.isImmediate())
4243 AM.Scale = Op.getImm();
4244 Op = MI->getOperand(2);
4245 if (Op.isImmediate())
4246 AM.IndexReg = Op.getImm();
4247 Op = MI->getOperand(3);
4248 if (Op.isGlobalAddress()) {
4249 AM.GV = Op.getGlobal();
4251 AM.Disp = Op.getImm();
4253 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4254 .addReg(MI->getOperand(4).getReg());
4256 // Reload the original control word now.
4257 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4259 delete MI; // The pseudo instruction is gone now.
4265 //===----------------------------------------------------------------------===//
4266 // X86 Optimization Hooks
4267 //===----------------------------------------------------------------------===//
4269 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4271 uint64_t &KnownZero,
4273 unsigned Depth) const {
4274 unsigned Opc = Op.getOpcode();
4275 assert((Opc >= ISD::BUILTIN_OP_END ||
4276 Opc == ISD::INTRINSIC_WO_CHAIN ||
4277 Opc == ISD::INTRINSIC_W_CHAIN ||
4278 Opc == ISD::INTRINSIC_VOID) &&
4279 "Should use MaskedValueIsZero if you don't know whether Op"
4280 " is a target node!");
4282 KnownZero = KnownOne = 0; // Don't know anything.
4286 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4291 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
4292 /// element of the result of the vector shuffle.
4293 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4294 MVT::ValueType VT = N->getValueType(0);
4295 SDOperand PermMask = N->getOperand(2);
4296 unsigned NumElems = PermMask.getNumOperands();
4297 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4299 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4301 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4302 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4303 SDOperand Idx = PermMask.getOperand(i);
4304 if (Idx.getOpcode() == ISD::UNDEF)
4305 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4306 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4311 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4312 /// node is a GlobalAddress + an offset.
4313 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4314 unsigned Opc = N->getOpcode();
4315 if (Opc == X86ISD::Wrapper) {
4316 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4317 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4320 } else if (Opc == ISD::ADD) {
4321 SDOperand N1 = N->getOperand(0);
4322 SDOperand N2 = N->getOperand(1);
4323 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4324 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4326 Offset += V->getSignExtended();
4329 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4330 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4332 Offset += V->getSignExtended();
4340 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4342 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4343 MachineFrameInfo *MFI) {
4344 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4347 SDOperand Loc = N->getOperand(1);
4348 SDOperand BaseLoc = Base->getOperand(1);
4349 if (Loc.getOpcode() == ISD::FrameIndex) {
4350 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4352 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4353 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4354 int FS = MFI->getObjectSize(FI);
4355 int BFS = MFI->getObjectSize(BFI);
4356 if (FS != BFS || FS != Size) return false;
4357 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4359 GlobalValue *GV1 = NULL;
4360 GlobalValue *GV2 = NULL;
4361 int64_t Offset1 = 0;
4362 int64_t Offset2 = 0;
4363 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4364 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4365 if (isGA1 && isGA2 && GV1 == GV2)
4366 return Offset1 == (Offset2 + Dist*Size);
4372 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4373 const X86Subtarget *Subtarget) {
4376 if (isGAPlusOffset(Base, GV, Offset))
4377 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4379 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4380 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
4382 // Fixed objects do not specify alignment, however the offsets are known.
4383 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4384 (MFI->getObjectOffset(BFI) % 16) == 0);
4386 return MFI->getObjectAlignment(BFI) >= 16;
4392 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4393 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4394 /// if the load addresses are consecutive, non-overlapping, and in the right
4396 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4397 const X86Subtarget *Subtarget) {
4398 MachineFunction &MF = DAG.getMachineFunction();
4399 MachineFrameInfo *MFI = MF.getFrameInfo();
4400 MVT::ValueType VT = N->getValueType(0);
4401 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4402 SDOperand PermMask = N->getOperand(2);
4403 int NumElems = (int)PermMask.getNumOperands();
4404 SDNode *Base = NULL;
4405 for (int i = 0; i < NumElems; ++i) {
4406 SDOperand Idx = PermMask.getOperand(i);
4407 if (Idx.getOpcode() == ISD::UNDEF) {
4408 if (!Base) return SDOperand();
4411 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4412 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
4416 else if (!isConsecutiveLoad(Arg.Val, Base,
4417 i, MVT::getSizeInBits(EVT)/8,MFI))
4422 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
4424 LoadSDNode *LD = cast<LoadSDNode>(Base);
4425 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4426 LD->getSrcValueOffset());
4428 // Just use movups, it's shorter.
4429 SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other);
4430 SmallVector<SDOperand, 3> Ops;
4431 Ops.push_back(Base->getOperand(0));
4432 Ops.push_back(Base->getOperand(1));
4433 Ops.push_back(Base->getOperand(2));
4434 return DAG.getNode(ISD::BIT_CONVERT, VT,
4435 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
4439 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4440 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4441 const X86Subtarget *Subtarget) {
4442 SDOperand Cond = N->getOperand(0);
4444 // If we have SSE[12] support, try to form min/max nodes.
4445 if (Subtarget->hasSSE2() &&
4446 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4447 if (Cond.getOpcode() == ISD::SETCC) {
4448 // Get the LHS/RHS of the select.
4449 SDOperand LHS = N->getOperand(1);
4450 SDOperand RHS = N->getOperand(2);
4451 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
4453 unsigned Opcode = 0;
4454 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
4457 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4460 if (!UnsafeFPMath) break;
4462 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4464 Opcode = X86ISD::FMIN;
4467 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4470 if (!UnsafeFPMath) break;
4472 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4474 Opcode = X86ISD::FMAX;
4477 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
4480 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4483 if (!UnsafeFPMath) break;
4485 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4487 Opcode = X86ISD::FMIN;
4490 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4493 if (!UnsafeFPMath) break;
4495 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4497 Opcode = X86ISD::FMAX;
4503 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
4512 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4513 DAGCombinerInfo &DCI) const {
4514 SelectionDAG &DAG = DCI.DAG;
4515 switch (N->getOpcode()) {
4517 case ISD::VECTOR_SHUFFLE:
4518 return PerformShuffleCombine(N, DAG, Subtarget);
4520 return PerformSELECTCombine(N, DAG, Subtarget);
4526 //===----------------------------------------------------------------------===//
4527 // X86 Inline Assembly Support
4528 //===----------------------------------------------------------------------===//
4530 /// getConstraintType - Given a constraint letter, return the type of
4531 /// constraint it is for this target.
4532 X86TargetLowering::ConstraintType
4533 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4534 if (Constraint.size() == 1) {
4535 switch (Constraint[0]) {
4544 return C_RegisterClass;
4549 return TargetLowering::getConstraintType(Constraint);
4552 /// isOperandValidForConstraint - Return the specified operand (possibly
4553 /// modified) if the specified SDOperand is valid for the specified target
4554 /// constraint letter, otherwise return null.
4555 SDOperand X86TargetLowering::
4556 isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4557 switch (Constraint) {
4560 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4561 if (C->getValue() <= 31)
4564 return SDOperand(0,0);
4566 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4567 if (C->getValue() <= 255)
4570 return SDOperand(0,0);
4572 // Literal immediates are always ok.
4573 if (isa<ConstantSDNode>(Op)) return Op;
4575 // If we are in non-pic codegen mode, we allow the address of a global to
4576 // be used with 'i'.
4577 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
4578 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4579 return SDOperand(0, 0);
4581 if (GA->getOpcode() != ISD::TargetGlobalAddress)
4582 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4587 // Otherwise, not valid for this mode.
4588 return SDOperand(0, 0);
4590 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
4593 std::vector<unsigned> X86TargetLowering::
4594 getRegClassForInlineAsmConstraint(const std::string &Constraint,
4595 MVT::ValueType VT) const {
4596 if (Constraint.size() == 1) {
4597 // FIXME: not handling fp-stack yet!
4598 switch (Constraint[0]) { // GCC X86 Constraint Letters
4599 default: break; // Unknown constraint letter
4600 case 'A': // EAX/EDX
4601 if (VT == MVT::i32 || VT == MVT::i64)
4602 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4604 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4607 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4608 else if (VT == MVT::i16)
4609 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4610 else if (VT == MVT::i8)
4611 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4616 return std::vector<unsigned>();
4619 std::pair<unsigned, const TargetRegisterClass*>
4620 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4621 MVT::ValueType VT) const {
4622 // First, see if this is a constraint that directly corresponds to an LLVM
4624 if (Constraint.size() == 1) {
4625 // GCC Constraint Letters
4626 switch (Constraint[0]) {
4628 case 'r': // GENERAL_REGS
4629 case 'R': // LEGACY_REGS
4630 case 'l': // INDEX_REGS
4631 if (VT == MVT::i64 && Subtarget->is64Bit())
4632 return std::make_pair(0U, X86::GR64RegisterClass);
4634 return std::make_pair(0U, X86::GR32RegisterClass);
4635 else if (VT == MVT::i16)
4636 return std::make_pair(0U, X86::GR16RegisterClass);
4637 else if (VT == MVT::i8)
4638 return std::make_pair(0U, X86::GR8RegisterClass);
4640 case 'y': // MMX_REGS if MMX allowed.
4641 if (!Subtarget->hasMMX()) break;
4642 return std::make_pair(0U, X86::VR64RegisterClass);
4644 case 'Y': // SSE_REGS if SSE2 allowed
4645 if (!Subtarget->hasSSE2()) break;
4647 case 'x': // SSE_REGS if SSE1 allowed
4648 if (!Subtarget->hasSSE1()) break;
4652 // Scalar SSE types.
4655 return std::make_pair(0U, X86::FR32RegisterClass);
4658 return std::make_pair(0U, X86::FR64RegisterClass);
4667 return std::make_pair(0U, X86::VR128RegisterClass);
4673 // Use the default implementation in TargetLowering to convert the register
4674 // constraint into a member of a register class.
4675 std::pair<unsigned, const TargetRegisterClass*> Res;
4676 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4678 // Not found as a standard register?
4679 if (Res.second == 0) {
4680 // GCC calls "st(0)" just plain "st".
4681 if (StringsEqualNoCase("{st}", Constraint)) {
4682 Res.first = X86::ST0;
4683 Res.second = X86::RSTRegisterClass;
4689 // Otherwise, check to see if this is a register class of the wrong value
4690 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4691 // turn into {ax},{dx}.
4692 if (Res.second->hasType(VT))
4693 return Res; // Correct type already, nothing to do.
4695 // All of the single-register GCC register classes map their values onto
4696 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4697 // really want an 8-bit or 32-bit register, map to the appropriate register
4698 // class and return the appropriate register.
4699 if (Res.second != X86::GR16RegisterClass)
4702 if (VT == MVT::i8) {
4703 unsigned DestReg = 0;
4704 switch (Res.first) {
4706 case X86::AX: DestReg = X86::AL; break;
4707 case X86::DX: DestReg = X86::DL; break;
4708 case X86::CX: DestReg = X86::CL; break;
4709 case X86::BX: DestReg = X86::BL; break;
4712 Res.first = DestReg;
4713 Res.second = Res.second = X86::GR8RegisterClass;
4715 } else if (VT == MVT::i32) {
4716 unsigned DestReg = 0;
4717 switch (Res.first) {
4719 case X86::AX: DestReg = X86::EAX; break;
4720 case X86::DX: DestReg = X86::EDX; break;
4721 case X86::CX: DestReg = X86::ECX; break;
4722 case X86::BX: DestReg = X86::EBX; break;
4723 case X86::SI: DestReg = X86::ESI; break;
4724 case X86::DI: DestReg = X86::EDI; break;
4725 case X86::BP: DestReg = X86::EBP; break;
4726 case X86::SP: DestReg = X86::ESP; break;
4729 Res.first = DestReg;
4730 Res.second = Res.second = X86::GR32RegisterClass;
4732 } else if (VT == MVT::i64) {
4733 unsigned DestReg = 0;
4734 switch (Res.first) {
4736 case X86::AX: DestReg = X86::RAX; break;
4737 case X86::DX: DestReg = X86::RDX; break;
4738 case X86::CX: DestReg = X86::RCX; break;
4739 case X86::BX: DestReg = X86::RBX; break;
4740 case X86::SI: DestReg = X86::RSI; break;
4741 case X86::DI: DestReg = X86::RDI; break;
4742 case X86::BP: DestReg = X86::RBP; break;
4743 case X86::SP: DestReg = X86::RSP; break;
4746 Res.first = DestReg;
4747 Res.second = Res.second = X86::GR64RegisterClass;