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);
325 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
326 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
327 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
329 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
330 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
332 setOperationAction(ISD::AND, MVT::v8i8, Promote);
333 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
334 setOperationAction(ISD::AND, MVT::v4i16, Promote);
335 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
336 setOperationAction(ISD::AND, MVT::v2i32, Promote);
337 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
338 setOperationAction(ISD::AND, MVT::v1i64, Legal);
340 setOperationAction(ISD::OR, MVT::v8i8, Promote);
341 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
342 setOperationAction(ISD::OR, MVT::v4i16, Promote);
343 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
344 setOperationAction(ISD::OR, MVT::v2i32, Promote);
345 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
346 setOperationAction(ISD::OR, MVT::v1i64, Legal);
348 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
349 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
350 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
351 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
352 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
353 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
354 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
356 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
357 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
358 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
359 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
360 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
361 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
362 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
364 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
365 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
366 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
368 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
369 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
370 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
373 if (Subtarget->hasSSE1()) {
374 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
376 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
377 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
378 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
379 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
380 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
381 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
382 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
383 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
384 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
387 if (Subtarget->hasSSE2()) {
388 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
389 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
390 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
391 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
392 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
394 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
395 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
396 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
397 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
398 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
399 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
400 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
401 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
402 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
403 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
404 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
405 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
406 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
408 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
409 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
410 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
411 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
412 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
413 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
415 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
416 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
417 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
418 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
419 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
421 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
422 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
423 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
424 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
425 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
426 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
428 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
429 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
430 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
431 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
432 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
433 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
434 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
435 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
436 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
437 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
438 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
439 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
442 // Custom lower v2i64 and v2f64 selects.
443 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
444 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
445 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
446 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
449 // We want to custom lower some of our intrinsics.
450 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
452 // We have target-specific dag combine patterns for the following nodes:
453 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
454 setTargetDAGCombine(ISD::SELECT);
456 computeRegisterProperties();
458 // FIXME: These should be based on subtarget info. Plus, the values should
459 // be smaller when we are in optimizing for size mode.
460 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
461 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
462 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
463 allowUnalignedMemoryAccesses = true; // x86 supports it!
467 //===----------------------------------------------------------------------===//
468 // Return Value Calling Convention Implementation
469 //===----------------------------------------------------------------------===//
471 #include "X86GenCallingConv.inc"
473 /// LowerRET - Lower an ISD::RET node.
474 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
475 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
477 SmallVector<CCValAssign, 16> RVLocs;
478 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
479 CCState CCInfo(CC, getTargetMachine(), RVLocs);
480 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
483 // If this is the first return lowered for this function, add the regs to the
484 // liveout set for the function.
485 if (DAG.getMachineFunction().liveout_empty()) {
486 for (unsigned i = 0; i != RVLocs.size(); ++i)
487 if (RVLocs[i].isRegLoc())
488 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
491 SDOperand Chain = Op.getOperand(0);
494 // Copy the result values into the output registers.
495 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
496 RVLocs[0].getLocReg() != X86::ST0) {
497 for (unsigned i = 0; i != RVLocs.size(); ++i) {
498 CCValAssign &VA = RVLocs[i];
499 assert(VA.isRegLoc() && "Can only return in registers!");
500 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
502 Flag = Chain.getValue(1);
505 // We need to handle a destination of ST0 specially, because it isn't really
507 SDOperand Value = Op.getOperand(1);
509 // If this is an FP return with ScalarSSE, we need to move the value from
510 // an XMM register onto the fp-stack.
514 // If this is a load into a scalarsse value, don't store the loaded value
515 // back to the stack, only to reload it: just replace the scalar-sse load.
516 if (ISD::isNON_EXTLoad(Value.Val) &&
517 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
518 Chain = Value.getOperand(0);
519 MemLoc = Value.getOperand(1);
521 // Spill the value to memory and reload it into top of stack.
522 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
523 MachineFunction &MF = DAG.getMachineFunction();
524 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
525 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
526 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
528 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
529 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
530 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
531 Chain = Value.getValue(1);
534 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
535 SDOperand Ops[] = { Chain, Value };
536 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
537 Flag = Chain.getValue(1);
540 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
542 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
544 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
548 /// LowerCallResult - Lower the result values of an ISD::CALL into the
549 /// appropriate copies out of appropriate physical registers. This assumes that
550 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
551 /// being lowered. The returns a SDNode with the same number of values as the
553 SDNode *X86TargetLowering::
554 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
555 unsigned CallingConv, SelectionDAG &DAG) {
557 // Assign locations to each value returned by this call.
558 SmallVector<CCValAssign, 16> RVLocs;
559 CCState CCInfo(CallingConv, getTargetMachine(), RVLocs);
560 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
563 SmallVector<SDOperand, 8> ResultVals;
565 // Copy all of the result registers out of their specified physreg.
566 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
567 for (unsigned i = 0; i != RVLocs.size(); ++i) {
568 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
569 RVLocs[i].getValVT(), InFlag).getValue(1);
570 InFlag = Chain.getValue(2);
571 ResultVals.push_back(Chain.getValue(0));
574 // Copies from the FP stack are special, as ST0 isn't a valid register
575 // before the fp stackifier runs.
577 // Copy ST0 into an RFP register with FP_GET_RESULT.
578 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
579 SDOperand GROps[] = { Chain, InFlag };
580 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
581 Chain = RetVal.getValue(1);
582 InFlag = RetVal.getValue(2);
584 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
587 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
588 // shouldn't be necessary except that RFP cannot be live across
589 // multiple blocks. When stackifier is fixed, they can be uncoupled.
590 MachineFunction &MF = DAG.getMachineFunction();
591 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
592 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
594 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
596 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
597 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
598 Chain = RetVal.getValue(1);
601 if (RVLocs[0].getValVT() == MVT::f32 && !X86ScalarSSE)
602 // FIXME: we would really like to remember that this FP_ROUND
603 // operation is okay to eliminate if we allow excess FP precision.
604 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
605 ResultVals.push_back(RetVal);
608 // Merge everything together with a MERGE_VALUES node.
609 ResultVals.push_back(Chain);
610 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
611 &ResultVals[0], ResultVals.size()).Val;
615 //===----------------------------------------------------------------------===//
616 // C & StdCall Calling Convention implementation
617 //===----------------------------------------------------------------------===//
618 // StdCall calling convention seems to be standard for many Windows' API
619 // routines and around. It differs from C calling convention just a little:
620 // callee should clean up the stack, not caller. Symbols should be also
621 // decorated in some fancy way :) It doesn't support any vector arguments.
623 /// AddLiveIn - This helper function adds the specified physical register to the
624 /// MachineFunction as a live in value. It also creates a corresponding virtual
626 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
627 const TargetRegisterClass *RC) {
628 assert(RC->contains(PReg) && "Not the correct regclass!");
629 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
630 MF.addLiveIn(PReg, VReg);
634 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
636 unsigned NumArgs = Op.Val->getNumValues() - 1;
637 MachineFunction &MF = DAG.getMachineFunction();
638 MachineFrameInfo *MFI = MF.getFrameInfo();
639 SDOperand Root = Op.getOperand(0);
640 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
642 // Assign locations to all of the incoming arguments.
643 SmallVector<CCValAssign, 16> ArgLocs;
644 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
646 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
648 SmallVector<SDOperand, 8> ArgValues;
649 unsigned LastVal = ~0U;
650 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
651 CCValAssign &VA = ArgLocs[i];
652 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
654 assert(VA.getValNo() != LastVal &&
655 "Don't support value assigned to multiple locs yet");
656 LastVal = VA.getValNo();
659 MVT::ValueType RegVT = VA.getLocVT();
660 TargetRegisterClass *RC;
661 if (RegVT == MVT::i32)
662 RC = X86::GR32RegisterClass;
664 assert(MVT::isVector(RegVT));
665 RC = X86::VR128RegisterClass;
668 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
669 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
671 // If this is an 8 or 16-bit value, it is really passed promoted to 32
672 // bits. Insert an assert[sz]ext to capture this, then truncate to the
674 if (VA.getLocInfo() == CCValAssign::SExt)
675 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
676 DAG.getValueType(VA.getValVT()));
677 else if (VA.getLocInfo() == CCValAssign::ZExt)
678 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
679 DAG.getValueType(VA.getValVT()));
681 if (VA.getLocInfo() != CCValAssign::Full)
682 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
684 ArgValues.push_back(ArgValue);
686 assert(VA.isMemLoc());
688 // Create the nodes corresponding to a load from this parameter slot.
689 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
690 VA.getLocMemOffset());
691 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
692 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
696 unsigned StackSize = CCInfo.getNextStackOffset();
698 ArgValues.push_back(Root);
700 // If the function takes variable number of arguments, make a frame index for
701 // the start of the first vararg value... for expansion of llvm.va_start.
703 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
705 if (isStdCall && !isVarArg) {
706 BytesToPopOnReturn = StackSize; // Callee pops everything..
707 BytesCallerReserves = 0;
709 BytesToPopOnReturn = 0; // Callee pops nothing.
711 // If this is an sret function, the return should pop the hidden pointer.
713 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
714 ISD::ParamFlags::StructReturn))
715 BytesToPopOnReturn = 4;
717 BytesCallerReserves = StackSize;
720 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
721 ReturnAddrIndex = 0; // No return address slot generated yet.
723 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
725 // Return the new list of results.
726 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
727 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
730 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
732 SDOperand Chain = Op.getOperand(0);
733 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
734 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
735 SDOperand Callee = Op.getOperand(4);
736 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
738 // Analyze operands of the call, assigning locations to each operand.
739 SmallVector<CCValAssign, 16> ArgLocs;
740 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
741 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
743 // Get a count of how many bytes are to be pushed on the stack.
744 unsigned NumBytes = CCInfo.getNextStackOffset();
746 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
748 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
749 SmallVector<SDOperand, 8> MemOpChains;
753 // Walk the register/memloc assignments, inserting copies/loads.
754 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
755 CCValAssign &VA = ArgLocs[i];
756 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
758 // Promote the value if needed.
759 switch (VA.getLocInfo()) {
760 default: assert(0 && "Unknown loc info!");
761 case CCValAssign::Full: break;
762 case CCValAssign::SExt:
763 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
765 case CCValAssign::ZExt:
766 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
768 case CCValAssign::AExt:
769 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
774 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
776 assert(VA.isMemLoc());
777 if (StackPtr.Val == 0)
778 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
779 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
780 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
781 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
785 // If the first argument is an sret pointer, remember it.
786 bool isSRet = NumOps &&
787 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
788 ISD::ParamFlags::StructReturn);
790 if (!MemOpChains.empty())
791 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
792 &MemOpChains[0], MemOpChains.size());
794 // Build a sequence of copy-to-reg nodes chained together with token chain
795 // and flag operands which copy the outgoing args into registers.
797 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
798 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
800 InFlag = Chain.getValue(1);
803 // ELF / PIC requires GOT in the EBX register before function calls via PLT
805 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
806 Subtarget->isPICStyleGOT()) {
807 Chain = DAG.getCopyToReg(Chain, X86::EBX,
808 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
810 InFlag = Chain.getValue(1);
813 // If the callee is a GlobalAddress node (quite common, every direct call is)
814 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
815 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
816 // We should use extra load for direct calls to dllimported functions in
818 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
819 getTargetMachine(), true))
820 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
821 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
822 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
824 // Returns a chain & a flag for retval copy to use.
825 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
826 SmallVector<SDOperand, 8> Ops;
827 Ops.push_back(Chain);
828 Ops.push_back(Callee);
830 // Add argument registers to the end of the list so that they are known live
832 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
833 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
834 RegsToPass[i].second.getValueType()));
836 // Add an implicit use GOT pointer in EBX.
837 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
838 Subtarget->isPICStyleGOT())
839 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
842 Ops.push_back(InFlag);
844 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
845 NodeTys, &Ops[0], Ops.size());
846 InFlag = Chain.getValue(1);
848 // Create the CALLSEQ_END node.
849 unsigned NumBytesForCalleeToPush = 0;
851 if (CC == CallingConv::X86_StdCall) {
853 NumBytesForCalleeToPush = isSRet ? 4 : 0;
855 NumBytesForCalleeToPush = NumBytes;
857 // If this is is a call to a struct-return function, the callee
858 // pops the hidden struct pointer, so we have to push it back.
859 // This is common for Darwin/X86, Linux & Mingw32 targets.
860 NumBytesForCalleeToPush = isSRet ? 4 : 0;
863 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
865 Ops.push_back(Chain);
866 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
867 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
868 Ops.push_back(InFlag);
869 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
870 InFlag = Chain.getValue(1);
872 // Handle result values, copying them out of physregs into vregs that we
874 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
878 //===----------------------------------------------------------------------===//
879 // FastCall Calling Convention implementation
880 //===----------------------------------------------------------------------===//
882 // The X86 'fastcall' calling convention passes up to two integer arguments in
883 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
884 // and requires that the callee pop its arguments off the stack (allowing proper
885 // tail calls), and has the same return value conventions as C calling convs.
887 // This calling convention always arranges for the callee pop value to be 8n+4
888 // bytes, which is needed for tail recursion elimination and stack alignment
891 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
892 MachineFunction &MF = DAG.getMachineFunction();
893 MachineFrameInfo *MFI = MF.getFrameInfo();
894 SDOperand Root = Op.getOperand(0);
896 // Assign locations to all of the incoming arguments.
897 SmallVector<CCValAssign, 16> ArgLocs;
898 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
900 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
902 SmallVector<SDOperand, 8> ArgValues;
903 unsigned LastVal = ~0U;
904 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
905 CCValAssign &VA = ArgLocs[i];
906 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
908 assert(VA.getValNo() != LastVal &&
909 "Don't support value assigned to multiple locs yet");
910 LastVal = VA.getValNo();
913 MVT::ValueType RegVT = VA.getLocVT();
914 TargetRegisterClass *RC;
915 if (RegVT == MVT::i32)
916 RC = X86::GR32RegisterClass;
918 assert(MVT::isVector(RegVT));
919 RC = X86::VR128RegisterClass;
922 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
923 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
925 // If this is an 8 or 16-bit value, it is really passed promoted to 32
926 // bits. Insert an assert[sz]ext to capture this, then truncate to the
928 if (VA.getLocInfo() == CCValAssign::SExt)
929 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
930 DAG.getValueType(VA.getValVT()));
931 else if (VA.getLocInfo() == CCValAssign::ZExt)
932 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
933 DAG.getValueType(VA.getValVT()));
935 if (VA.getLocInfo() != CCValAssign::Full)
936 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
938 ArgValues.push_back(ArgValue);
940 assert(VA.isMemLoc());
942 // Create the nodes corresponding to a load from this parameter slot.
943 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
944 VA.getLocMemOffset());
945 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
946 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
950 ArgValues.push_back(Root);
952 unsigned StackSize = CCInfo.getNextStackOffset();
954 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
955 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
956 // arguments and the arguments after the retaddr has been pushed are aligned.
957 if ((StackSize & 7) == 0)
961 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
962 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
963 ReturnAddrIndex = 0; // No return address slot generated yet.
964 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
965 BytesCallerReserves = 0;
967 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
969 // Return the new list of results.
970 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
971 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
974 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
976 SDOperand Chain = Op.getOperand(0);
977 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
978 SDOperand Callee = Op.getOperand(4);
980 // Analyze operands of the call, assigning locations to each operand.
981 SmallVector<CCValAssign, 16> ArgLocs;
982 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
983 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
985 // Get a count of how many bytes are to be pushed on the stack.
986 unsigned NumBytes = CCInfo.getNextStackOffset();
988 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
989 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
990 // arguments and the arguments after the retaddr has been pushed are aligned.
991 if ((NumBytes & 7) == 0)
995 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
997 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
998 SmallVector<SDOperand, 8> MemOpChains;
1002 // Walk the register/memloc assignments, inserting copies/loads.
1003 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1004 CCValAssign &VA = ArgLocs[i];
1005 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1007 // Promote the value if needed.
1008 switch (VA.getLocInfo()) {
1009 default: assert(0 && "Unknown loc info!");
1010 case CCValAssign::Full: break;
1011 case CCValAssign::SExt:
1012 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1014 case CCValAssign::ZExt:
1015 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1017 case CCValAssign::AExt:
1018 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1022 if (VA.isRegLoc()) {
1023 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1025 assert(VA.isMemLoc());
1026 if (StackPtr.Val == 0)
1027 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1028 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1029 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1030 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1034 if (!MemOpChains.empty())
1035 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1036 &MemOpChains[0], MemOpChains.size());
1038 // Build a sequence of copy-to-reg nodes chained together with token chain
1039 // and flag operands which copy the outgoing args into registers.
1041 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1042 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1044 InFlag = Chain.getValue(1);
1047 // If the callee is a GlobalAddress node (quite common, every direct call is)
1048 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1049 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1050 // We should use extra load for direct calls to dllimported functions in
1052 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1053 getTargetMachine(), true))
1054 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1055 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1056 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1058 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1060 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1061 Subtarget->isPICStyleGOT()) {
1062 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1063 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1065 InFlag = Chain.getValue(1);
1068 // Returns a chain & a flag for retval copy to use.
1069 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1070 SmallVector<SDOperand, 8> Ops;
1071 Ops.push_back(Chain);
1072 Ops.push_back(Callee);
1074 // Add argument registers to the end of the list so that they are known live
1076 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1077 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1078 RegsToPass[i].second.getValueType()));
1080 // Add an implicit use GOT pointer in EBX.
1081 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1082 Subtarget->isPICStyleGOT())
1083 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1086 Ops.push_back(InFlag);
1088 // FIXME: Do not generate X86ISD::TAILCALL for now.
1089 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1090 NodeTys, &Ops[0], Ops.size());
1091 InFlag = Chain.getValue(1);
1093 // Returns a flag for retval copy to use.
1094 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1096 Ops.push_back(Chain);
1097 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1098 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1099 Ops.push_back(InFlag);
1100 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1101 InFlag = Chain.getValue(1);
1103 // Handle result values, copying them out of physregs into vregs that we
1105 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1109 //===----------------------------------------------------------------------===//
1110 // X86-64 C Calling Convention implementation
1111 //===----------------------------------------------------------------------===//
1114 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1115 MachineFunction &MF = DAG.getMachineFunction();
1116 MachineFrameInfo *MFI = MF.getFrameInfo();
1117 SDOperand Root = Op.getOperand(0);
1118 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1120 static const unsigned GPR64ArgRegs[] = {
1121 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1123 static const unsigned XMMArgRegs[] = {
1124 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1125 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1129 // Assign locations to all of the incoming arguments.
1130 SmallVector<CCValAssign, 16> ArgLocs;
1131 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
1133 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1135 SmallVector<SDOperand, 8> ArgValues;
1136 unsigned LastVal = ~0U;
1137 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1138 CCValAssign &VA = ArgLocs[i];
1139 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1141 assert(VA.getValNo() != LastVal &&
1142 "Don't support value assigned to multiple locs yet");
1143 LastVal = VA.getValNo();
1145 if (VA.isRegLoc()) {
1146 MVT::ValueType RegVT = VA.getLocVT();
1147 TargetRegisterClass *RC;
1148 if (RegVT == MVT::i32)
1149 RC = X86::GR32RegisterClass;
1150 else if (RegVT == MVT::i64)
1151 RC = X86::GR64RegisterClass;
1152 else if (RegVT == MVT::f32)
1153 RC = X86::FR32RegisterClass;
1154 else if (RegVT == MVT::f64)
1155 RC = X86::FR64RegisterClass;
1157 assert(MVT::isVector(RegVT));
1158 RC = X86::VR128RegisterClass;
1161 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1162 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1164 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1165 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1167 if (VA.getLocInfo() == CCValAssign::SExt)
1168 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1169 DAG.getValueType(VA.getValVT()));
1170 else if (VA.getLocInfo() == CCValAssign::ZExt)
1171 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1172 DAG.getValueType(VA.getValVT()));
1174 if (VA.getLocInfo() != CCValAssign::Full)
1175 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1177 ArgValues.push_back(ArgValue);
1179 assert(VA.isMemLoc());
1181 // Create the nodes corresponding to a load from this parameter slot.
1182 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1183 VA.getLocMemOffset());
1184 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1185 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1189 unsigned StackSize = CCInfo.getNextStackOffset();
1191 // If the function takes variable number of arguments, make a frame index for
1192 // the start of the first vararg value... for expansion of llvm.va_start.
1194 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1195 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1197 // For X86-64, if there are vararg parameters that are passed via
1198 // registers, then we must store them to their spots on the stack so they
1199 // may be loaded by deferencing the result of va_next.
1200 VarArgsGPOffset = NumIntRegs * 8;
1201 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1202 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1203 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1205 // Store the integer parameter registers.
1206 SmallVector<SDOperand, 8> MemOps;
1207 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1208 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1209 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1210 for (; NumIntRegs != 6; ++NumIntRegs) {
1211 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1212 X86::GR64RegisterClass);
1213 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1214 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1215 MemOps.push_back(Store);
1216 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1217 DAG.getConstant(8, getPointerTy()));
1220 // Now store the XMM (fp + vector) parameter registers.
1221 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1222 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1223 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1224 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1225 X86::VR128RegisterClass);
1226 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1227 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1228 MemOps.push_back(Store);
1229 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1230 DAG.getConstant(16, getPointerTy()));
1232 if (!MemOps.empty())
1233 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1234 &MemOps[0], MemOps.size());
1237 ArgValues.push_back(Root);
1239 ReturnAddrIndex = 0; // No return address slot generated yet.
1240 BytesToPopOnReturn = 0; // Callee pops nothing.
1241 BytesCallerReserves = StackSize;
1243 // Return the new list of results.
1244 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1245 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1249 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1251 SDOperand Chain = Op.getOperand(0);
1252 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1253 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1254 SDOperand Callee = Op.getOperand(4);
1256 // Analyze operands of the call, assigning locations to each operand.
1257 SmallVector<CCValAssign, 16> ArgLocs;
1258 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
1259 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1261 // Get a count of how many bytes are to be pushed on the stack.
1262 unsigned NumBytes = CCInfo.getNextStackOffset();
1263 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1265 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1266 SmallVector<SDOperand, 8> MemOpChains;
1270 // Walk the register/memloc assignments, inserting copies/loads.
1271 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1272 CCValAssign &VA = ArgLocs[i];
1273 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1275 // Promote the value if needed.
1276 switch (VA.getLocInfo()) {
1277 default: assert(0 && "Unknown loc info!");
1278 case CCValAssign::Full: break;
1279 case CCValAssign::SExt:
1280 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1282 case CCValAssign::ZExt:
1283 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1285 case CCValAssign::AExt:
1286 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1290 if (VA.isRegLoc()) {
1291 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1293 assert(VA.isMemLoc());
1294 if (StackPtr.Val == 0)
1295 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1296 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1297 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1298 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1302 if (!MemOpChains.empty())
1303 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1304 &MemOpChains[0], MemOpChains.size());
1306 // Build a sequence of copy-to-reg nodes chained together with token chain
1307 // and flag operands which copy the outgoing args into registers.
1309 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1310 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1312 InFlag = Chain.getValue(1);
1316 // From AMD64 ABI document:
1317 // For calls that may call functions that use varargs or stdargs
1318 // (prototype-less calls or calls to functions containing ellipsis (...) in
1319 // the declaration) %al is used as hidden argument to specify the number
1320 // of SSE registers used. The contents of %al do not need to match exactly
1321 // the number of registers, but must be an ubound on the number of SSE
1322 // registers used and is in the range 0 - 8 inclusive.
1324 // Count the number of XMM registers allocated.
1325 static const unsigned XMMArgRegs[] = {
1326 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1327 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1329 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1331 Chain = DAG.getCopyToReg(Chain, X86::AL,
1332 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1333 InFlag = Chain.getValue(1);
1336 // If the callee is a GlobalAddress node (quite common, every direct call is)
1337 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1338 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1339 // We should use extra load for direct calls to dllimported functions in
1341 if (getTargetMachine().getCodeModel() != CodeModel::Large
1342 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1343 getTargetMachine(), true))
1344 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1345 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1346 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1347 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1349 // Returns a chain & a flag for retval copy to use.
1350 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1351 SmallVector<SDOperand, 8> Ops;
1352 Ops.push_back(Chain);
1353 Ops.push_back(Callee);
1355 // Add argument registers to the end of the list so that they are known live
1357 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1358 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1359 RegsToPass[i].second.getValueType()));
1362 Ops.push_back(InFlag);
1364 // FIXME: Do not generate X86ISD::TAILCALL for now.
1365 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1366 NodeTys, &Ops[0], Ops.size());
1367 InFlag = Chain.getValue(1);
1369 // Returns a flag for retval copy to use.
1370 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1372 Ops.push_back(Chain);
1373 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1374 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1375 Ops.push_back(InFlag);
1376 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1377 InFlag = Chain.getValue(1);
1379 // Handle result values, copying them out of physregs into vregs that we
1381 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1385 //===----------------------------------------------------------------------===//
1386 // Other Lowering Hooks
1387 //===----------------------------------------------------------------------===//
1390 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1391 if (ReturnAddrIndex == 0) {
1392 // Set up a frame object for the return address.
1393 MachineFunction &MF = DAG.getMachineFunction();
1394 if (Subtarget->is64Bit())
1395 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1397 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1400 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1405 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1406 /// specific condition code. It returns a false if it cannot do a direct
1407 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1409 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1410 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1411 SelectionDAG &DAG) {
1412 X86CC = X86::COND_INVALID;
1414 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1415 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1416 // X > -1 -> X == 0, jump !sign.
1417 RHS = DAG.getConstant(0, RHS.getValueType());
1418 X86CC = X86::COND_NS;
1420 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1421 // X < 0 -> X == 0, jump on sign.
1422 X86CC = X86::COND_S;
1427 switch (SetCCOpcode) {
1429 case ISD::SETEQ: X86CC = X86::COND_E; break;
1430 case ISD::SETGT: X86CC = X86::COND_G; break;
1431 case ISD::SETGE: X86CC = X86::COND_GE; break;
1432 case ISD::SETLT: X86CC = X86::COND_L; break;
1433 case ISD::SETLE: X86CC = X86::COND_LE; break;
1434 case ISD::SETNE: X86CC = X86::COND_NE; break;
1435 case ISD::SETULT: X86CC = X86::COND_B; break;
1436 case ISD::SETUGT: X86CC = X86::COND_A; break;
1437 case ISD::SETULE: X86CC = X86::COND_BE; break;
1438 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1441 // On a floating point condition, the flags are set as follows:
1443 // 0 | 0 | 0 | X > Y
1444 // 0 | 0 | 1 | X < Y
1445 // 1 | 0 | 0 | X == Y
1446 // 1 | 1 | 1 | unordered
1448 switch (SetCCOpcode) {
1451 case ISD::SETEQ: X86CC = X86::COND_E; break;
1452 case ISD::SETOLT: Flip = true; // Fallthrough
1454 case ISD::SETGT: X86CC = X86::COND_A; break;
1455 case ISD::SETOLE: Flip = true; // Fallthrough
1457 case ISD::SETGE: X86CC = X86::COND_AE; break;
1458 case ISD::SETUGT: Flip = true; // Fallthrough
1460 case ISD::SETLT: X86CC = X86::COND_B; break;
1461 case ISD::SETUGE: Flip = true; // Fallthrough
1463 case ISD::SETLE: X86CC = X86::COND_BE; break;
1465 case ISD::SETNE: X86CC = X86::COND_NE; break;
1466 case ISD::SETUO: X86CC = X86::COND_P; break;
1467 case ISD::SETO: X86CC = X86::COND_NP; break;
1470 std::swap(LHS, RHS);
1473 return X86CC != X86::COND_INVALID;
1476 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1477 /// code. Current x86 isa includes the following FP cmov instructions:
1478 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1479 static bool hasFPCMov(unsigned X86CC) {
1495 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1496 /// true if Op is undef or if its value falls within the specified range (L, H].
1497 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1498 if (Op.getOpcode() == ISD::UNDEF)
1501 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1502 return (Val >= Low && Val < Hi);
1505 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1506 /// true if Op is undef or if its value equal to the specified value.
1507 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1508 if (Op.getOpcode() == ISD::UNDEF)
1510 return cast<ConstantSDNode>(Op)->getValue() == Val;
1513 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1514 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1515 bool X86::isPSHUFDMask(SDNode *N) {
1516 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1518 if (N->getNumOperands() != 4)
1521 // Check if the value doesn't reference the second vector.
1522 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1523 SDOperand Arg = N->getOperand(i);
1524 if (Arg.getOpcode() == ISD::UNDEF) continue;
1525 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1526 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1533 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1534 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1535 bool X86::isPSHUFHWMask(SDNode *N) {
1536 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1538 if (N->getNumOperands() != 8)
1541 // Lower quadword copied in order.
1542 for (unsigned i = 0; i != 4; ++i) {
1543 SDOperand Arg = N->getOperand(i);
1544 if (Arg.getOpcode() == ISD::UNDEF) continue;
1545 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1546 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1550 // Upper quadword shuffled.
1551 for (unsigned i = 4; i != 8; ++i) {
1552 SDOperand Arg = N->getOperand(i);
1553 if (Arg.getOpcode() == ISD::UNDEF) continue;
1554 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1555 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1556 if (Val < 4 || Val > 7)
1563 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1564 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1565 bool X86::isPSHUFLWMask(SDNode *N) {
1566 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1568 if (N->getNumOperands() != 8)
1571 // Upper quadword copied in order.
1572 for (unsigned i = 4; i != 8; ++i)
1573 if (!isUndefOrEqual(N->getOperand(i), i))
1576 // Lower quadword shuffled.
1577 for (unsigned i = 0; i != 4; ++i)
1578 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1584 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1585 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1586 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1587 if (NumElems != 2 && NumElems != 4) return false;
1589 unsigned Half = NumElems / 2;
1590 for (unsigned i = 0; i < Half; ++i)
1591 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1593 for (unsigned i = Half; i < NumElems; ++i)
1594 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1600 bool X86::isSHUFPMask(SDNode *N) {
1601 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1602 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1605 /// isCommutedSHUFP - Returns true if the shuffle mask is except
1606 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1607 /// half elements to come from vector 1 (which would equal the dest.) and
1608 /// the upper half to come from vector 2.
1609 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1610 if (NumOps != 2 && NumOps != 4) return false;
1612 unsigned Half = NumOps / 2;
1613 for (unsigned i = 0; i < Half; ++i)
1614 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1616 for (unsigned i = Half; i < NumOps; ++i)
1617 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1622 static bool isCommutedSHUFP(SDNode *N) {
1623 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1624 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1627 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1628 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1629 bool X86::isMOVHLPSMask(SDNode *N) {
1630 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1632 if (N->getNumOperands() != 4)
1635 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1636 return isUndefOrEqual(N->getOperand(0), 6) &&
1637 isUndefOrEqual(N->getOperand(1), 7) &&
1638 isUndefOrEqual(N->getOperand(2), 2) &&
1639 isUndefOrEqual(N->getOperand(3), 3);
1642 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1643 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1645 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1646 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1648 if (N->getNumOperands() != 4)
1651 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1652 return isUndefOrEqual(N->getOperand(0), 2) &&
1653 isUndefOrEqual(N->getOperand(1), 3) &&
1654 isUndefOrEqual(N->getOperand(2), 2) &&
1655 isUndefOrEqual(N->getOperand(3), 3);
1658 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1659 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1660 bool X86::isMOVLPMask(SDNode *N) {
1661 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1663 unsigned NumElems = N->getNumOperands();
1664 if (NumElems != 2 && NumElems != 4)
1667 for (unsigned i = 0; i < NumElems/2; ++i)
1668 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1671 for (unsigned i = NumElems/2; i < NumElems; ++i)
1672 if (!isUndefOrEqual(N->getOperand(i), i))
1678 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1679 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1681 bool X86::isMOVHPMask(SDNode *N) {
1682 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1684 unsigned NumElems = N->getNumOperands();
1685 if (NumElems != 2 && NumElems != 4)
1688 for (unsigned i = 0; i < NumElems/2; ++i)
1689 if (!isUndefOrEqual(N->getOperand(i), i))
1692 for (unsigned i = 0; i < NumElems/2; ++i) {
1693 SDOperand Arg = N->getOperand(i + NumElems/2);
1694 if (!isUndefOrEqual(Arg, i + NumElems))
1701 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1702 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1703 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1704 bool V2IsSplat = false) {
1705 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1708 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1709 SDOperand BitI = Elts[i];
1710 SDOperand BitI1 = Elts[i+1];
1711 if (!isUndefOrEqual(BitI, j))
1714 if (isUndefOrEqual(BitI1, NumElts))
1717 if (!isUndefOrEqual(BitI1, j + NumElts))
1725 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1726 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1727 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1730 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1731 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1732 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1733 bool V2IsSplat = false) {
1734 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1737 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1738 SDOperand BitI = Elts[i];
1739 SDOperand BitI1 = Elts[i+1];
1740 if (!isUndefOrEqual(BitI, j + NumElts/2))
1743 if (isUndefOrEqual(BitI1, NumElts))
1746 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1754 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1755 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1756 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1759 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1760 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1762 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1763 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1765 unsigned NumElems = N->getNumOperands();
1766 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1769 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1770 SDOperand BitI = N->getOperand(i);
1771 SDOperand BitI1 = N->getOperand(i+1);
1773 if (!isUndefOrEqual(BitI, j))
1775 if (!isUndefOrEqual(BitI1, j))
1782 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1783 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1784 /// MOVSD, and MOVD, i.e. setting the lowest element.
1785 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1786 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1789 if (!isUndefOrEqual(Elts[0], NumElts))
1792 for (unsigned i = 1; i < NumElts; ++i) {
1793 if (!isUndefOrEqual(Elts[i], i))
1800 bool X86::isMOVLMask(SDNode *N) {
1801 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1802 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
1805 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1806 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1807 /// element of vector 2 and the other elements to come from vector 1 in order.
1808 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1809 bool V2IsSplat = false,
1810 bool V2IsUndef = false) {
1811 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
1814 if (!isUndefOrEqual(Ops[0], 0))
1817 for (unsigned i = 1; i < NumOps; ++i) {
1818 SDOperand Arg = Ops[i];
1819 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1820 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1821 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
1828 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1829 bool V2IsUndef = false) {
1830 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1831 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1832 V2IsSplat, V2IsUndef);
1835 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1836 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1837 bool X86::isMOVSHDUPMask(SDNode *N) {
1838 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1840 if (N->getNumOperands() != 4)
1843 // Expect 1, 1, 3, 3
1844 for (unsigned i = 0; i < 2; ++i) {
1845 SDOperand Arg = N->getOperand(i);
1846 if (Arg.getOpcode() == ISD::UNDEF) continue;
1847 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1848 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1849 if (Val != 1) return false;
1853 for (unsigned i = 2; i < 4; ++i) {
1854 SDOperand Arg = N->getOperand(i);
1855 if (Arg.getOpcode() == ISD::UNDEF) continue;
1856 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1857 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1858 if (Val != 3) return false;
1862 // Don't use movshdup if it can be done with a shufps.
1866 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1867 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1868 bool X86::isMOVSLDUPMask(SDNode *N) {
1869 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1871 if (N->getNumOperands() != 4)
1874 // Expect 0, 0, 2, 2
1875 for (unsigned i = 0; i < 2; ++i) {
1876 SDOperand Arg = N->getOperand(i);
1877 if (Arg.getOpcode() == ISD::UNDEF) continue;
1878 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1879 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1880 if (Val != 0) return false;
1884 for (unsigned i = 2; i < 4; ++i) {
1885 SDOperand Arg = N->getOperand(i);
1886 if (Arg.getOpcode() == ISD::UNDEF) continue;
1887 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1888 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1889 if (Val != 2) return false;
1893 // Don't use movshdup if it can be done with a shufps.
1897 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1898 /// a splat of a single element.
1899 static bool isSplatMask(SDNode *N) {
1900 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1902 // This is a splat operation if each element of the permute is the same, and
1903 // if the value doesn't reference the second vector.
1904 unsigned NumElems = N->getNumOperands();
1905 SDOperand ElementBase;
1907 for (; i != NumElems; ++i) {
1908 SDOperand Elt = N->getOperand(i);
1909 if (isa<ConstantSDNode>(Elt)) {
1915 if (!ElementBase.Val)
1918 for (; i != NumElems; ++i) {
1919 SDOperand Arg = N->getOperand(i);
1920 if (Arg.getOpcode() == ISD::UNDEF) continue;
1921 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1922 if (Arg != ElementBase) return false;
1925 // Make sure it is a splat of the first vector operand.
1926 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
1929 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1930 /// a splat of a single element and it's a 2 or 4 element mask.
1931 bool X86::isSplatMask(SDNode *N) {
1932 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1934 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
1935 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1937 return ::isSplatMask(N);
1940 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
1941 /// specifies a splat of zero element.
1942 bool X86::isSplatLoMask(SDNode *N) {
1943 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1945 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
1946 if (!isUndefOrEqual(N->getOperand(i), 0))
1951 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1952 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1954 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
1955 unsigned NumOperands = N->getNumOperands();
1956 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1958 for (unsigned i = 0; i < NumOperands; ++i) {
1960 SDOperand Arg = N->getOperand(NumOperands-i-1);
1961 if (Arg.getOpcode() != ISD::UNDEF)
1962 Val = cast<ConstantSDNode>(Arg)->getValue();
1963 if (Val >= NumOperands) Val -= NumOperands;
1965 if (i != NumOperands - 1)
1972 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1973 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1975 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1977 // 8 nodes, but we only care about the last 4.
1978 for (unsigned i = 7; i >= 4; --i) {
1980 SDOperand Arg = N->getOperand(i);
1981 if (Arg.getOpcode() != ISD::UNDEF)
1982 Val = cast<ConstantSDNode>(Arg)->getValue();
1991 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1992 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1994 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1996 // 8 nodes, but we only care about the first 4.
1997 for (int i = 3; i >= 0; --i) {
1999 SDOperand Arg = N->getOperand(i);
2000 if (Arg.getOpcode() != ISD::UNDEF)
2001 Val = cast<ConstantSDNode>(Arg)->getValue();
2010 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2011 /// specifies a 8 element shuffle that can be broken into a pair of
2012 /// PSHUFHW and PSHUFLW.
2013 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2014 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2016 if (N->getNumOperands() != 8)
2019 // Lower quadword shuffled.
2020 for (unsigned i = 0; i != 4; ++i) {
2021 SDOperand Arg = N->getOperand(i);
2022 if (Arg.getOpcode() == ISD::UNDEF) continue;
2023 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2024 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2029 // Upper quadword shuffled.
2030 for (unsigned i = 4; i != 8; ++i) {
2031 SDOperand Arg = N->getOperand(i);
2032 if (Arg.getOpcode() == ISD::UNDEF) continue;
2033 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2034 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2035 if (Val < 4 || Val > 7)
2042 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2043 /// values in ther permute mask.
2044 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2045 SDOperand &V2, SDOperand &Mask,
2046 SelectionDAG &DAG) {
2047 MVT::ValueType VT = Op.getValueType();
2048 MVT::ValueType MaskVT = Mask.getValueType();
2049 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2050 unsigned NumElems = Mask.getNumOperands();
2051 SmallVector<SDOperand, 8> MaskVec;
2053 for (unsigned i = 0; i != NumElems; ++i) {
2054 SDOperand Arg = Mask.getOperand(i);
2055 if (Arg.getOpcode() == ISD::UNDEF) {
2056 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2059 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2060 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2062 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2064 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2068 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2069 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2072 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2073 /// match movhlps. The lower half elements should come from upper half of
2074 /// V1 (and in order), and the upper half elements should come from the upper
2075 /// half of V2 (and in order).
2076 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2077 unsigned NumElems = Mask->getNumOperands();
2080 for (unsigned i = 0, e = 2; i != e; ++i)
2081 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2083 for (unsigned i = 2; i != 4; ++i)
2084 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2089 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2090 /// is promoted to a vector.
2091 static inline bool isScalarLoadToVector(SDNode *N) {
2092 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2093 N = N->getOperand(0).Val;
2094 return ISD::isNON_EXTLoad(N);
2099 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2100 /// match movlp{s|d}. The lower half elements should come from lower half of
2101 /// V1 (and in order), and the upper half elements should come from the upper
2102 /// half of V2 (and in order). And since V1 will become the source of the
2103 /// MOVLP, it must be either a vector load or a scalar load to vector.
2104 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2105 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2107 // Is V2 is a vector load, don't do this transformation. We will try to use
2108 // load folding shufps op.
2109 if (ISD::isNON_EXTLoad(V2))
2112 unsigned NumElems = Mask->getNumOperands();
2113 if (NumElems != 2 && NumElems != 4)
2115 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2116 if (!isUndefOrEqual(Mask->getOperand(i), i))
2118 for (unsigned i = NumElems/2; i != NumElems; ++i)
2119 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2124 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2126 static bool isSplatVector(SDNode *N) {
2127 if (N->getOpcode() != ISD::BUILD_VECTOR)
2130 SDOperand SplatValue = N->getOperand(0);
2131 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2132 if (N->getOperand(i) != SplatValue)
2137 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2139 static bool isUndefShuffle(SDNode *N) {
2140 if (N->getOpcode() != ISD::BUILD_VECTOR)
2143 SDOperand V1 = N->getOperand(0);
2144 SDOperand V2 = N->getOperand(1);
2145 SDOperand Mask = N->getOperand(2);
2146 unsigned NumElems = Mask.getNumOperands();
2147 for (unsigned i = 0; i != NumElems; ++i) {
2148 SDOperand Arg = Mask.getOperand(i);
2149 if (Arg.getOpcode() != ISD::UNDEF) {
2150 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2151 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2153 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2160 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2161 /// that point to V2 points to its first element.
2162 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2163 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2165 bool Changed = false;
2166 SmallVector<SDOperand, 8> MaskVec;
2167 unsigned NumElems = Mask.getNumOperands();
2168 for (unsigned i = 0; i != NumElems; ++i) {
2169 SDOperand Arg = Mask.getOperand(i);
2170 if (Arg.getOpcode() != ISD::UNDEF) {
2171 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2172 if (Val > NumElems) {
2173 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2177 MaskVec.push_back(Arg);
2181 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2182 &MaskVec[0], MaskVec.size());
2186 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2187 /// operation of specified width.
2188 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2189 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2190 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2192 SmallVector<SDOperand, 8> MaskVec;
2193 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2194 for (unsigned i = 1; i != NumElems; ++i)
2195 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2196 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2199 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2200 /// of specified width.
2201 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2202 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2203 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2204 SmallVector<SDOperand, 8> MaskVec;
2205 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2206 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2207 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2209 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2212 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2213 /// of specified width.
2214 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2215 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2216 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2217 unsigned Half = NumElems/2;
2218 SmallVector<SDOperand, 8> MaskVec;
2219 for (unsigned i = 0; i != Half; ++i) {
2220 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2221 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2223 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2226 /// getZeroVector - Returns a vector of specified type with all zero elements.
2228 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2229 assert(MVT::isVector(VT) && "Expected a vector type");
2230 unsigned NumElems = getVectorNumElements(VT);
2231 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2232 bool isFP = MVT::isFloatingPoint(EVT);
2233 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2234 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2235 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2238 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2240 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2241 SDOperand V1 = Op.getOperand(0);
2242 SDOperand Mask = Op.getOperand(2);
2243 MVT::ValueType VT = Op.getValueType();
2244 unsigned NumElems = Mask.getNumOperands();
2245 Mask = getUnpacklMask(NumElems, DAG);
2246 while (NumElems != 4) {
2247 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2250 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2252 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2253 Mask = getZeroVector(MaskVT, DAG);
2254 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2255 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2256 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2259 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2261 static inline bool isZeroNode(SDOperand Elt) {
2262 return ((isa<ConstantSDNode>(Elt) &&
2263 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2264 (isa<ConstantFPSDNode>(Elt) &&
2265 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2268 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2269 /// vector and zero or undef vector.
2270 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2271 unsigned NumElems, unsigned Idx,
2272 bool isZero, SelectionDAG &DAG) {
2273 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2274 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2275 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2276 SDOperand Zero = DAG.getConstant(0, EVT);
2277 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2278 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2279 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2280 &MaskVec[0], MaskVec.size());
2281 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2284 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2286 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2287 unsigned NumNonZero, unsigned NumZero,
2288 SelectionDAG &DAG, TargetLowering &TLI) {
2294 for (unsigned i = 0; i < 16; ++i) {
2295 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2296 if (ThisIsNonZero && First) {
2298 V = getZeroVector(MVT::v8i16, DAG);
2300 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2305 SDOperand ThisElt(0, 0), LastElt(0, 0);
2306 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2307 if (LastIsNonZero) {
2308 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2310 if (ThisIsNonZero) {
2311 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2312 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2313 ThisElt, DAG.getConstant(8, MVT::i8));
2315 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2320 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2321 DAG.getConstant(i/2, TLI.getPointerTy()));
2325 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2328 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2330 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2331 unsigned NumNonZero, unsigned NumZero,
2332 SelectionDAG &DAG, TargetLowering &TLI) {
2338 for (unsigned i = 0; i < 8; ++i) {
2339 bool isNonZero = (NonZeros & (1 << i)) != 0;
2343 V = getZeroVector(MVT::v8i16, DAG);
2345 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2348 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2349 DAG.getConstant(i, TLI.getPointerTy()));
2357 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2358 // All zero's are handled with pxor.
2359 if (ISD::isBuildVectorAllZeros(Op.Val))
2362 // All one's are handled with pcmpeqd.
2363 if (ISD::isBuildVectorAllOnes(Op.Val))
2366 MVT::ValueType VT = Op.getValueType();
2367 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2368 unsigned EVTBits = MVT::getSizeInBits(EVT);
2370 unsigned NumElems = Op.getNumOperands();
2371 unsigned NumZero = 0;
2372 unsigned NumNonZero = 0;
2373 unsigned NonZeros = 0;
2374 std::set<SDOperand> Values;
2375 for (unsigned i = 0; i < NumElems; ++i) {
2376 SDOperand Elt = Op.getOperand(i);
2377 if (Elt.getOpcode() != ISD::UNDEF) {
2379 if (isZeroNode(Elt))
2382 NonZeros |= (1 << i);
2388 if (NumNonZero == 0)
2389 // Must be a mix of zero and undef. Return a zero vector.
2390 return getZeroVector(VT, DAG);
2392 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2393 if (Values.size() == 1)
2396 // Special case for single non-zero element.
2397 if (NumNonZero == 1) {
2398 unsigned Idx = CountTrailingZeros_32(NonZeros);
2399 SDOperand Item = Op.getOperand(Idx);
2400 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2402 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2403 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2406 if (EVTBits == 32) {
2407 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2408 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2410 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2411 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2412 SmallVector<SDOperand, 8> MaskVec;
2413 for (unsigned i = 0; i < NumElems; i++)
2414 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2415 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2416 &MaskVec[0], MaskVec.size());
2417 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2418 DAG.getNode(ISD::UNDEF, VT), Mask);
2422 // Let legalizer expand 2-wide build_vector's.
2426 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2428 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2430 if (V.Val) return V;
2433 if (EVTBits == 16) {
2434 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2436 if (V.Val) return V;
2439 // If element VT is == 32 bits, turn it into a number of shuffles.
2440 SmallVector<SDOperand, 8> V;
2442 if (NumElems == 4 && NumZero > 0) {
2443 for (unsigned i = 0; i < 4; ++i) {
2444 bool isZero = !(NonZeros & (1 << i));
2446 V[i] = getZeroVector(VT, DAG);
2448 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2451 for (unsigned i = 0; i < 2; ++i) {
2452 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2455 V[i] = V[i*2]; // Must be a zero vector.
2458 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2459 getMOVLMask(NumElems, DAG));
2462 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2463 getMOVLMask(NumElems, DAG));
2466 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2467 getUnpacklMask(NumElems, DAG));
2472 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2473 // clears the upper bits.
2474 // FIXME: we can do the same for v4f32 case when we know both parts of
2475 // the lower half come from scalar_to_vector (loadf32). We should do
2476 // that in post legalizer dag combiner with target specific hooks.
2477 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2479 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2480 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2481 SmallVector<SDOperand, 8> MaskVec;
2482 bool Reverse = (NonZeros & 0x3) == 2;
2483 for (unsigned i = 0; i < 2; ++i)
2485 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2487 MaskVec.push_back(DAG.getConstant(i, EVT));
2488 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2489 for (unsigned i = 0; i < 2; ++i)
2491 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2493 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2494 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2495 &MaskVec[0], MaskVec.size());
2496 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2499 if (Values.size() > 2) {
2500 // Expand into a number of unpckl*.
2502 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2503 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2504 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2505 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2506 for (unsigned i = 0; i < NumElems; ++i)
2507 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2509 while (NumElems != 0) {
2510 for (unsigned i = 0; i < NumElems; ++i)
2511 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2522 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2523 SDOperand V1 = Op.getOperand(0);
2524 SDOperand V2 = Op.getOperand(1);
2525 SDOperand PermMask = Op.getOperand(2);
2526 MVT::ValueType VT = Op.getValueType();
2527 unsigned NumElems = PermMask.getNumOperands();
2528 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2529 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2530 bool V1IsSplat = false;
2531 bool V2IsSplat = false;
2533 if (isUndefShuffle(Op.Val))
2534 return DAG.getNode(ISD::UNDEF, VT);
2536 if (isSplatMask(PermMask.Val)) {
2537 if (NumElems <= 4) return Op;
2538 // Promote it to a v4i32 splat.
2539 return PromoteSplat(Op, DAG);
2542 if (X86::isMOVLMask(PermMask.Val))
2543 return (V1IsUndef) ? V2 : Op;
2545 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2546 X86::isMOVSLDUPMask(PermMask.Val) ||
2547 X86::isMOVHLPSMask(PermMask.Val) ||
2548 X86::isMOVHPMask(PermMask.Val) ||
2549 X86::isMOVLPMask(PermMask.Val))
2552 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2553 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2554 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2556 bool Commuted = false;
2557 V1IsSplat = isSplatVector(V1.Val);
2558 V2IsSplat = isSplatVector(V2.Val);
2559 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2560 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2561 std::swap(V1IsSplat, V2IsSplat);
2562 std::swap(V1IsUndef, V2IsUndef);
2566 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2567 if (V2IsUndef) return V1;
2568 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2570 // V2 is a splat, so the mask may be malformed. That is, it may point
2571 // to any V2 element. The instruction selectior won't like this. Get
2572 // a corrected mask and commute to form a proper MOVS{S|D}.
2573 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2574 if (NewMask.Val != PermMask.Val)
2575 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2580 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2581 X86::isUNPCKLMask(PermMask.Val) ||
2582 X86::isUNPCKHMask(PermMask.Val))
2586 // Normalize mask so all entries that point to V2 points to its first
2587 // element then try to match unpck{h|l} again. If match, return a
2588 // new vector_shuffle with the corrected mask.
2589 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2590 if (NewMask.Val != PermMask.Val) {
2591 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2592 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2593 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2594 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2595 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2596 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2601 // Normalize the node to match x86 shuffle ops if needed
2602 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2603 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2606 // Commute is back and try unpck* again.
2607 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2608 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2609 X86::isUNPCKLMask(PermMask.Val) ||
2610 X86::isUNPCKHMask(PermMask.Val))
2614 // If VT is integer, try PSHUF* first, then SHUFP*.
2615 if (MVT::isInteger(VT)) {
2616 if (X86::isPSHUFDMask(PermMask.Val) ||
2617 X86::isPSHUFHWMask(PermMask.Val) ||
2618 X86::isPSHUFLWMask(PermMask.Val)) {
2619 if (V2.getOpcode() != ISD::UNDEF)
2620 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2621 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2625 if (X86::isSHUFPMask(PermMask.Val))
2628 // Handle v8i16 shuffle high / low shuffle node pair.
2629 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2630 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2631 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2632 SmallVector<SDOperand, 8> MaskVec;
2633 for (unsigned i = 0; i != 4; ++i)
2634 MaskVec.push_back(PermMask.getOperand(i));
2635 for (unsigned i = 4; i != 8; ++i)
2636 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2637 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2638 &MaskVec[0], MaskVec.size());
2639 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2641 for (unsigned i = 0; i != 4; ++i)
2642 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2643 for (unsigned i = 4; i != 8; ++i)
2644 MaskVec.push_back(PermMask.getOperand(i));
2645 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2646 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2649 // Floating point cases in the other order.
2650 if (X86::isSHUFPMask(PermMask.Val))
2652 if (X86::isPSHUFDMask(PermMask.Val) ||
2653 X86::isPSHUFHWMask(PermMask.Val) ||
2654 X86::isPSHUFLWMask(PermMask.Val)) {
2655 if (V2.getOpcode() != ISD::UNDEF)
2656 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2657 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2662 if (NumElems == 4) {
2663 MVT::ValueType MaskVT = PermMask.getValueType();
2664 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2665 SmallVector<std::pair<int, int>, 8> Locs;
2666 Locs.reserve(NumElems);
2667 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2668 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2671 // If no more than two elements come from either vector. This can be
2672 // implemented with two shuffles. First shuffle gather the elements.
2673 // The second shuffle, which takes the first shuffle as both of its
2674 // vector operands, put the elements into the right order.
2675 for (unsigned i = 0; i != NumElems; ++i) {
2676 SDOperand Elt = PermMask.getOperand(i);
2677 if (Elt.getOpcode() == ISD::UNDEF) {
2678 Locs[i] = std::make_pair(-1, -1);
2680 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2681 if (Val < NumElems) {
2682 Locs[i] = std::make_pair(0, NumLo);
2686 Locs[i] = std::make_pair(1, NumHi);
2687 if (2+NumHi < NumElems)
2688 Mask1[2+NumHi] = Elt;
2693 if (NumLo <= 2 && NumHi <= 2) {
2694 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2695 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2696 &Mask1[0], Mask1.size()));
2697 for (unsigned i = 0; i != NumElems; ++i) {
2698 if (Locs[i].first == -1)
2701 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2702 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2703 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2707 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2708 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2709 &Mask2[0], Mask2.size()));
2712 // Break it into (shuffle shuffle_hi, shuffle_lo).
2714 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2715 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2716 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
2717 unsigned MaskIdx = 0;
2719 unsigned HiIdx = NumElems/2;
2720 for (unsigned i = 0; i != NumElems; ++i) {
2721 if (i == NumElems/2) {
2727 SDOperand Elt = PermMask.getOperand(i);
2728 if (Elt.getOpcode() == ISD::UNDEF) {
2729 Locs[i] = std::make_pair(-1, -1);
2730 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2731 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2732 (*MaskPtr)[LoIdx] = Elt;
2735 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2736 (*MaskPtr)[HiIdx] = Elt;
2741 SDOperand LoShuffle =
2742 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2743 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2744 &LoMask[0], LoMask.size()));
2745 SDOperand HiShuffle =
2746 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2747 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2748 &HiMask[0], HiMask.size()));
2749 SmallVector<SDOperand, 8> MaskOps;
2750 for (unsigned i = 0; i != NumElems; ++i) {
2751 if (Locs[i].first == -1) {
2752 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2754 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2755 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2758 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2759 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2760 &MaskOps[0], MaskOps.size()));
2767 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2768 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2771 MVT::ValueType VT = Op.getValueType();
2772 // TODO: handle v16i8.
2773 if (MVT::getSizeInBits(VT) == 16) {
2774 // Transform it so it match pextrw which produces a 32-bit result.
2775 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2776 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2777 Op.getOperand(0), Op.getOperand(1));
2778 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2779 DAG.getValueType(VT));
2780 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2781 } else if (MVT::getSizeInBits(VT) == 32) {
2782 SDOperand Vec = Op.getOperand(0);
2783 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2786 // SHUFPS the element to the lowest double word, then movss.
2787 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2788 SmallVector<SDOperand, 8> IdxVec;
2789 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2790 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2791 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2792 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2793 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2794 &IdxVec[0], IdxVec.size());
2795 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2796 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2797 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2798 DAG.getConstant(0, getPointerTy()));
2799 } else if (MVT::getSizeInBits(VT) == 64) {
2800 SDOperand Vec = Op.getOperand(0);
2801 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2805 // UNPCKHPD the element to the lowest double word, then movsd.
2806 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2807 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2808 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2809 SmallVector<SDOperand, 8> IdxVec;
2810 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2811 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2812 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2813 &IdxVec[0], IdxVec.size());
2814 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2815 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2816 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2817 DAG.getConstant(0, getPointerTy()));
2824 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2825 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2826 // as its second argument.
2827 MVT::ValueType VT = Op.getValueType();
2828 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2829 SDOperand N0 = Op.getOperand(0);
2830 SDOperand N1 = Op.getOperand(1);
2831 SDOperand N2 = Op.getOperand(2);
2832 if (MVT::getSizeInBits(BaseVT) == 16) {
2833 if (N1.getValueType() != MVT::i32)
2834 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2835 if (N2.getValueType() != MVT::i32)
2836 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2837 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2838 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2839 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2842 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2843 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2844 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2845 SmallVector<SDOperand, 8> MaskVec;
2846 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2847 for (unsigned i = 1; i <= 3; ++i)
2848 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2849 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2850 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2851 &MaskVec[0], MaskVec.size()));
2853 // Use two pinsrw instructions to insert a 32 bit value.
2855 if (MVT::isFloatingPoint(N1.getValueType())) {
2856 if (ISD::isNON_EXTLoad(N1.Val)) {
2857 // Just load directly from f32mem to GR32.
2858 LoadSDNode *LD = cast<LoadSDNode>(N1);
2859 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
2860 LD->getSrcValue(), LD->getSrcValueOffset());
2862 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2863 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2864 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2865 DAG.getConstant(0, getPointerTy()));
2868 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2869 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2870 DAG.getConstant(Idx, getPointerTy()));
2871 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2872 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2873 DAG.getConstant(Idx+1, getPointerTy()));
2874 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2882 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2883 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2884 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2887 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2888 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2889 // one of the above mentioned nodes. It has to be wrapped because otherwise
2890 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2891 // be used to form addressing mode. These wrapped nodes will be selected
2894 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2895 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2896 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
2898 CP->getAlignment());
2899 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2900 // With PIC, the address is actually $g + Offset.
2901 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2902 !Subtarget->isPICStyleRIPRel()) {
2903 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2904 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2912 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2913 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2914 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
2915 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2916 // With PIC, the address is actually $g + Offset.
2917 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2918 !Subtarget->isPICStyleRIPRel()) {
2919 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2920 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2924 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
2925 // load the value at address GV, not the value of GV itself. This means that
2926 // the GlobalAddress must be in the base or index register of the address, not
2927 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
2928 // The same applies for external symbols during PIC codegen
2929 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
2930 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
2936 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2937 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2938 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
2939 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2940 // With PIC, the address is actually $g + Offset.
2941 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2942 !Subtarget->isPICStyleRIPRel()) {
2943 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2944 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2951 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
2952 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
2953 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
2954 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2955 // With PIC, the address is actually $g + Offset.
2956 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2957 !Subtarget->isPICStyleRIPRel()) {
2958 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2959 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2966 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
2967 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2968 "Not an i64 shift!");
2969 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2970 SDOperand ShOpLo = Op.getOperand(0);
2971 SDOperand ShOpHi = Op.getOperand(1);
2972 SDOperand ShAmt = Op.getOperand(2);
2973 SDOperand Tmp1 = isSRA ?
2974 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
2975 DAG.getConstant(0, MVT::i32);
2977 SDOperand Tmp2, Tmp3;
2978 if (Op.getOpcode() == ISD::SHL_PARTS) {
2979 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2980 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2982 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
2983 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
2986 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
2987 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
2988 DAG.getConstant(32, MVT::i8));
2989 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
2990 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
2993 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
2995 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
2996 SmallVector<SDOperand, 4> Ops;
2997 if (Op.getOpcode() == ISD::SHL_PARTS) {
2998 Ops.push_back(Tmp2);
2999 Ops.push_back(Tmp3);
3001 Ops.push_back(InFlag);
3002 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3003 InFlag = Hi.getValue(1);
3006 Ops.push_back(Tmp3);
3007 Ops.push_back(Tmp1);
3009 Ops.push_back(InFlag);
3010 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3012 Ops.push_back(Tmp2);
3013 Ops.push_back(Tmp3);
3015 Ops.push_back(InFlag);
3016 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3017 InFlag = Lo.getValue(1);
3020 Ops.push_back(Tmp3);
3021 Ops.push_back(Tmp1);
3023 Ops.push_back(InFlag);
3024 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3027 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3031 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3034 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3035 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3036 Op.getOperand(0).getValueType() >= MVT::i16 &&
3037 "Unknown SINT_TO_FP to lower!");
3040 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3041 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3042 MachineFunction &MF = DAG.getMachineFunction();
3043 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3044 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3045 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3046 StackSlot, NULL, 0);
3051 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3053 Tys = DAG.getVTList(MVT::f64, MVT::Other);
3054 SmallVector<SDOperand, 8> Ops;
3055 Ops.push_back(Chain);
3056 Ops.push_back(StackSlot);
3057 Ops.push_back(DAG.getValueType(SrcVT));
3058 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3059 Tys, &Ops[0], Ops.size());
3062 Chain = Result.getValue(1);
3063 SDOperand InFlag = Result.getValue(2);
3065 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3066 // shouldn't be necessary except that RFP cannot be live across
3067 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3068 MachineFunction &MF = DAG.getMachineFunction();
3069 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3070 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3071 Tys = DAG.getVTList(MVT::Other);
3072 SmallVector<SDOperand, 8> Ops;
3073 Ops.push_back(Chain);
3074 Ops.push_back(Result);
3075 Ops.push_back(StackSlot);
3076 Ops.push_back(DAG.getValueType(Op.getValueType()));
3077 Ops.push_back(InFlag);
3078 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3079 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3085 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3086 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3087 "Unknown FP_TO_SINT to lower!");
3088 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3090 MachineFunction &MF = DAG.getMachineFunction();
3091 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3092 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3093 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3096 switch (Op.getValueType()) {
3097 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3098 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3099 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3100 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3103 SDOperand Chain = DAG.getEntryNode();
3104 SDOperand Value = Op.getOperand(0);
3106 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3107 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3108 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
3110 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3112 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3113 Chain = Value.getValue(1);
3114 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3115 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3118 // Build the FP_TO_INT*_IN_MEM
3119 SDOperand Ops[] = { Chain, Value, StackSlot };
3120 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3123 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3126 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3127 MVT::ValueType VT = Op.getValueType();
3128 const Type *OpNTy = MVT::getTypeForValueType(VT);
3129 std::vector<Constant*> CV;
3130 if (VT == MVT::f64) {
3131 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3132 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3134 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3135 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3136 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3137 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3139 Constant *CS = ConstantStruct::get(CV);
3140 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3141 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
3142 SmallVector<SDOperand, 3> Ops;
3143 Ops.push_back(DAG.getEntryNode());
3144 Ops.push_back(CPIdx);
3145 Ops.push_back(DAG.getSrcValue(NULL));
3146 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3147 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3150 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3151 MVT::ValueType VT = Op.getValueType();
3152 const Type *OpNTy = MVT::getTypeForValueType(VT);
3153 std::vector<Constant*> CV;
3154 if (VT == MVT::f64) {
3155 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3156 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3158 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3159 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3160 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3161 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3163 Constant *CS = ConstantStruct::get(CV);
3164 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3165 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
3166 SmallVector<SDOperand, 3> Ops;
3167 Ops.push_back(DAG.getEntryNode());
3168 Ops.push_back(CPIdx);
3169 Ops.push_back(DAG.getSrcValue(NULL));
3170 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3171 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3174 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3175 SDOperand Op0 = Op.getOperand(0);
3176 SDOperand Op1 = Op.getOperand(1);
3177 MVT::ValueType VT = Op.getValueType();
3178 MVT::ValueType SrcVT = Op1.getValueType();
3179 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3181 // If second operand is smaller, extend it first.
3182 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3183 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3187 // First get the sign bit of second operand.
3188 std::vector<Constant*> CV;
3189 if (SrcVT == MVT::f64) {
3190 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3191 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3193 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3194 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3195 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3196 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3198 Constant *CS = ConstantStruct::get(CV);
3199 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3200 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other);
3201 SmallVector<SDOperand, 3> Ops;
3202 Ops.push_back(DAG.getEntryNode());
3203 Ops.push_back(CPIdx);
3204 Ops.push_back(DAG.getSrcValue(NULL));
3205 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3206 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3208 // Shift sign bit right or left if the two operands have different types.
3209 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3210 // Op0 is MVT::f32, Op1 is MVT::f64.
3211 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3212 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3213 DAG.getConstant(32, MVT::i32));
3214 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3215 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3216 DAG.getConstant(0, getPointerTy()));
3219 // Clear first operand sign bit.
3221 if (VT == MVT::f64) {
3222 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3223 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3225 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3226 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3227 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3228 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3230 CS = ConstantStruct::get(CV);
3231 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3232 Tys = DAG.getVTList(VT, MVT::Other);
3234 Ops.push_back(DAG.getEntryNode());
3235 Ops.push_back(CPIdx);
3236 Ops.push_back(DAG.getSrcValue(NULL));
3237 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3238 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3240 // Or the value with the sign bit.
3241 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3244 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3246 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3248 SDOperand Op0 = Op.getOperand(0);
3249 SDOperand Op1 = Op.getOperand(1);
3250 SDOperand CC = Op.getOperand(2);
3251 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3252 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3253 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3254 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3257 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3259 SDOperand Ops1[] = { Chain, Op0, Op1 };
3260 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3261 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3262 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3265 assert(isFP && "Illegal integer SetCC!");
3267 SDOperand COps[] = { Chain, Op0, Op1 };
3268 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3270 switch (SetCCOpcode) {
3271 default: assert(false && "Illegal floating point SetCC!");
3272 case ISD::SETOEQ: { // !PF & ZF
3273 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3274 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3275 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3277 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3278 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3280 case ISD::SETUNE: { // PF | !ZF
3281 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3282 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3283 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3285 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3286 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3291 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3292 bool addTest = true;
3293 SDOperand Chain = DAG.getEntryNode();
3294 SDOperand Cond = Op.getOperand(0);
3296 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3298 if (Cond.getOpcode() == ISD::SETCC)
3299 Cond = LowerSETCC(Cond, DAG, Chain);
3301 if (Cond.getOpcode() == X86ISD::SETCC) {
3302 CC = Cond.getOperand(0);
3304 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3305 // (since flag operand cannot be shared). Use it as the condition setting
3306 // operand in place of the X86ISD::SETCC.
3307 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3308 // to use a test instead of duplicating the X86ISD::CMP (for register
3309 // pressure reason)?
3310 SDOperand Cmp = Cond.getOperand(1);
3311 unsigned Opc = Cmp.getOpcode();
3312 bool IllegalFPCMov = !X86ScalarSSE &&
3313 MVT::isFloatingPoint(Op.getValueType()) &&
3314 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3315 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3317 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3318 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3324 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3325 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3326 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3329 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3330 SmallVector<SDOperand, 4> Ops;
3331 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3332 // condition is true.
3333 Ops.push_back(Op.getOperand(2));
3334 Ops.push_back(Op.getOperand(1));
3336 Ops.push_back(Cond.getValue(1));
3337 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3340 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3341 bool addTest = true;
3342 SDOperand Chain = Op.getOperand(0);
3343 SDOperand Cond = Op.getOperand(1);
3344 SDOperand Dest = Op.getOperand(2);
3346 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3348 if (Cond.getOpcode() == ISD::SETCC)
3349 Cond = LowerSETCC(Cond, DAG, Chain);
3351 if (Cond.getOpcode() == X86ISD::SETCC) {
3352 CC = Cond.getOperand(0);
3354 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3355 // (since flag operand cannot be shared). Use it as the condition setting
3356 // operand in place of the X86ISD::SETCC.
3357 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3358 // to use a test instead of duplicating the X86ISD::CMP (for register
3359 // pressure reason)?
3360 SDOperand Cmp = Cond.getOperand(1);
3361 unsigned Opc = Cmp.getOpcode();
3362 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3363 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3364 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3370 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3371 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3372 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3374 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3375 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3378 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3379 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3381 if (Subtarget->is64Bit())
3382 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3384 switch (CallingConv) {
3386 assert(0 && "Unsupported calling convention");
3387 case CallingConv::Fast:
3388 // TODO: Implement fastcc
3390 case CallingConv::C:
3391 case CallingConv::X86_StdCall:
3392 return LowerCCCCallTo(Op, DAG, CallingConv);
3393 case CallingConv::X86_FastCall:
3394 return LowerFastCCCallTo(Op, DAG, CallingConv);
3399 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3400 MachineFunction &MF = DAG.getMachineFunction();
3401 const Function* Fn = MF.getFunction();
3402 if (Fn->hasExternalLinkage() &&
3403 Subtarget->isTargetCygMing() &&
3404 Fn->getName() == "main")
3405 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3407 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3408 if (Subtarget->is64Bit())
3409 return LowerX86_64CCCArguments(Op, DAG);
3413 assert(0 && "Unsupported calling convention");
3414 case CallingConv::Fast:
3415 // TODO: implement fastcc.
3418 case CallingConv::C:
3419 return LowerCCCArguments(Op, DAG);
3420 case CallingConv::X86_StdCall:
3421 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
3422 return LowerCCCArguments(Op, DAG, true);
3423 case CallingConv::X86_FastCall:
3424 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
3425 return LowerFastCCArguments(Op, DAG);
3429 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3430 SDOperand InFlag(0, 0);
3431 SDOperand Chain = Op.getOperand(0);
3433 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3434 if (Align == 0) Align = 1;
3436 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3437 // If not DWORD aligned, call memset if size is less than the threshold.
3438 // It knows how to align to the right boundary first.
3439 if ((Align & 3) != 0 ||
3440 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3441 MVT::ValueType IntPtr = getPointerTy();
3442 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3443 TargetLowering::ArgListTy Args;
3444 TargetLowering::ArgListEntry Entry;
3445 Entry.Node = Op.getOperand(1);
3446 Entry.Ty = IntPtrTy;
3447 Args.push_back(Entry);
3448 // Extend the unsigned i8 argument to be an int value for the call.
3449 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3450 Entry.Ty = IntPtrTy;
3451 Args.push_back(Entry);
3452 Entry.Node = Op.getOperand(3);
3453 Args.push_back(Entry);
3454 std::pair<SDOperand,SDOperand> CallResult =
3455 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3456 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3457 return CallResult.second;
3462 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3463 unsigned BytesLeft = 0;
3464 bool TwoRepStos = false;
3467 uint64_t Val = ValC->getValue() & 255;
3469 // If the value is a constant, then we can potentially use larger sets.
3470 switch (Align & 3) {
3471 case 2: // WORD aligned
3474 Val = (Val << 8) | Val;
3476 case 0: // DWORD aligned
3479 Val = (Val << 8) | Val;
3480 Val = (Val << 16) | Val;
3481 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3484 Val = (Val << 32) | Val;
3487 default: // Byte aligned
3490 Count = Op.getOperand(3);
3494 if (AVT > MVT::i8) {
3496 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3497 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3498 BytesLeft = I->getValue() % UBytes;
3500 assert(AVT >= MVT::i32 &&
3501 "Do not use rep;stos if not at least DWORD aligned");
3502 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3503 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3508 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3510 InFlag = Chain.getValue(1);
3513 Count = Op.getOperand(3);
3514 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3515 InFlag = Chain.getValue(1);
3518 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3520 InFlag = Chain.getValue(1);
3521 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3522 Op.getOperand(1), InFlag);
3523 InFlag = Chain.getValue(1);
3525 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3526 SmallVector<SDOperand, 8> Ops;
3527 Ops.push_back(Chain);
3528 Ops.push_back(DAG.getValueType(AVT));
3529 Ops.push_back(InFlag);
3530 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3533 InFlag = Chain.getValue(1);
3534 Count = Op.getOperand(3);
3535 MVT::ValueType CVT = Count.getValueType();
3536 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3537 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3538 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3540 InFlag = Chain.getValue(1);
3541 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3543 Ops.push_back(Chain);
3544 Ops.push_back(DAG.getValueType(MVT::i8));
3545 Ops.push_back(InFlag);
3546 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3547 } else if (BytesLeft) {
3548 // Issue stores for the last 1 - 7 bytes.
3550 unsigned Val = ValC->getValue() & 255;
3551 unsigned Offset = I->getValue() - BytesLeft;
3552 SDOperand DstAddr = Op.getOperand(1);
3553 MVT::ValueType AddrVT = DstAddr.getValueType();
3554 if (BytesLeft >= 4) {
3555 Val = (Val << 8) | Val;
3556 Val = (Val << 16) | Val;
3557 Value = DAG.getConstant(Val, MVT::i32);
3558 Chain = DAG.getStore(Chain, Value,
3559 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3560 DAG.getConstant(Offset, AddrVT)),
3565 if (BytesLeft >= 2) {
3566 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3567 Chain = DAG.getStore(Chain, Value,
3568 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3569 DAG.getConstant(Offset, AddrVT)),
3574 if (BytesLeft == 1) {
3575 Value = DAG.getConstant(Val, MVT::i8);
3576 Chain = DAG.getStore(Chain, Value,
3577 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3578 DAG.getConstant(Offset, AddrVT)),
3586 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3587 SDOperand Chain = Op.getOperand(0);
3589 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3590 if (Align == 0) Align = 1;
3592 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3593 // If not DWORD aligned, call memcpy if size is less than the threshold.
3594 // It knows how to align to the right boundary first.
3595 if ((Align & 3) != 0 ||
3596 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3597 MVT::ValueType IntPtr = getPointerTy();
3598 TargetLowering::ArgListTy Args;
3599 TargetLowering::ArgListEntry Entry;
3600 Entry.Ty = getTargetData()->getIntPtrType();
3601 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3602 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3603 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
3604 std::pair<SDOperand,SDOperand> CallResult =
3605 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3606 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3607 return CallResult.second;
3612 unsigned BytesLeft = 0;
3613 bool TwoRepMovs = false;
3614 switch (Align & 3) {
3615 case 2: // WORD aligned
3618 case 0: // DWORD aligned
3620 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3623 default: // Byte aligned
3625 Count = Op.getOperand(3);
3629 if (AVT > MVT::i8) {
3631 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3632 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3633 BytesLeft = I->getValue() % UBytes;
3635 assert(AVT >= MVT::i32 &&
3636 "Do not use rep;movs if not at least DWORD aligned");
3637 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3638 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3643 SDOperand InFlag(0, 0);
3644 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3646 InFlag = Chain.getValue(1);
3647 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3648 Op.getOperand(1), InFlag);
3649 InFlag = Chain.getValue(1);
3650 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3651 Op.getOperand(2), InFlag);
3652 InFlag = Chain.getValue(1);
3654 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3655 SmallVector<SDOperand, 8> Ops;
3656 Ops.push_back(Chain);
3657 Ops.push_back(DAG.getValueType(AVT));
3658 Ops.push_back(InFlag);
3659 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3662 InFlag = Chain.getValue(1);
3663 Count = Op.getOperand(3);
3664 MVT::ValueType CVT = Count.getValueType();
3665 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3666 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3667 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3669 InFlag = Chain.getValue(1);
3670 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3672 Ops.push_back(Chain);
3673 Ops.push_back(DAG.getValueType(MVT::i8));
3674 Ops.push_back(InFlag);
3675 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3676 } else if (BytesLeft) {
3677 // Issue loads and stores for the last 1 - 7 bytes.
3678 unsigned Offset = I->getValue() - BytesLeft;
3679 SDOperand DstAddr = Op.getOperand(1);
3680 MVT::ValueType DstVT = DstAddr.getValueType();
3681 SDOperand SrcAddr = Op.getOperand(2);
3682 MVT::ValueType SrcVT = SrcAddr.getValueType();
3684 if (BytesLeft >= 4) {
3685 Value = DAG.getLoad(MVT::i32, Chain,
3686 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3687 DAG.getConstant(Offset, SrcVT)),
3689 Chain = Value.getValue(1);
3690 Chain = DAG.getStore(Chain, Value,
3691 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3692 DAG.getConstant(Offset, DstVT)),
3697 if (BytesLeft >= 2) {
3698 Value = DAG.getLoad(MVT::i16, Chain,
3699 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3700 DAG.getConstant(Offset, SrcVT)),
3702 Chain = Value.getValue(1);
3703 Chain = DAG.getStore(Chain, Value,
3704 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3705 DAG.getConstant(Offset, DstVT)),
3711 if (BytesLeft == 1) {
3712 Value = DAG.getLoad(MVT::i8, Chain,
3713 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3714 DAG.getConstant(Offset, SrcVT)),
3716 Chain = Value.getValue(1);
3717 Chain = DAG.getStore(Chain, Value,
3718 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3719 DAG.getConstant(Offset, DstVT)),
3728 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3729 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3730 SDOperand TheOp = Op.getOperand(0);
3731 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
3732 if (Subtarget->is64Bit()) {
3733 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
3734 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
3735 MVT::i64, Copy1.getValue(2));
3736 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
3737 DAG.getConstant(32, MVT::i8));
3739 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
3742 Tys = DAG.getVTList(MVT::i64, MVT::Other);
3743 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
3746 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
3747 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
3748 MVT::i32, Copy1.getValue(2));
3749 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
3750 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
3751 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
3754 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3755 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
3757 if (!Subtarget->is64Bit()) {
3758 // vastart just stores the address of the VarArgsFrameIndex slot into the
3759 // memory location argument.
3760 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
3761 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
3766 // gp_offset (0 - 6 * 8)
3767 // fp_offset (48 - 48 + 8 * 16)
3768 // overflow_arg_area (point to parameters coming in memory).
3770 SmallVector<SDOperand, 8> MemOps;
3771 SDOperand FIN = Op.getOperand(1);
3773 SDOperand Store = DAG.getStore(Op.getOperand(0),
3774 DAG.getConstant(VarArgsGPOffset, MVT::i32),
3775 FIN, SV->getValue(), SV->getOffset());
3776 MemOps.push_back(Store);
3779 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3780 DAG.getConstant(4, getPointerTy()));
3781 Store = DAG.getStore(Op.getOperand(0),
3782 DAG.getConstant(VarArgsFPOffset, MVT::i32),
3783 FIN, SV->getValue(), SV->getOffset());
3784 MemOps.push_back(Store);
3786 // Store ptr to overflow_arg_area
3787 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3788 DAG.getConstant(4, getPointerTy()));
3789 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
3790 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
3792 MemOps.push_back(Store);
3794 // Store ptr to reg_save_area.
3795 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3796 DAG.getConstant(8, getPointerTy()));
3797 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
3798 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
3800 MemOps.push_back(Store);
3801 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
3804 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
3805 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
3806 SDOperand Chain = Op.getOperand(0);
3807 SDOperand DstPtr = Op.getOperand(1);
3808 SDOperand SrcPtr = Op.getOperand(2);
3809 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
3810 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
3812 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
3813 SrcSV->getValue(), SrcSV->getOffset());
3814 Chain = SrcPtr.getValue(1);
3815 for (unsigned i = 0; i < 3; ++i) {
3816 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
3817 SrcSV->getValue(), SrcSV->getOffset());
3818 Chain = Val.getValue(1);
3819 Chain = DAG.getStore(Chain, Val, DstPtr,
3820 DstSV->getValue(), DstSV->getOffset());
3823 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
3824 DAG.getConstant(8, getPointerTy()));
3825 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
3826 DAG.getConstant(8, getPointerTy()));
3832 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3833 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3835 default: return SDOperand(); // Don't custom lower most intrinsics.
3836 // Comparison intrinsics.
3837 case Intrinsic::x86_sse_comieq_ss:
3838 case Intrinsic::x86_sse_comilt_ss:
3839 case Intrinsic::x86_sse_comile_ss:
3840 case Intrinsic::x86_sse_comigt_ss:
3841 case Intrinsic::x86_sse_comige_ss:
3842 case Intrinsic::x86_sse_comineq_ss:
3843 case Intrinsic::x86_sse_ucomieq_ss:
3844 case Intrinsic::x86_sse_ucomilt_ss:
3845 case Intrinsic::x86_sse_ucomile_ss:
3846 case Intrinsic::x86_sse_ucomigt_ss:
3847 case Intrinsic::x86_sse_ucomige_ss:
3848 case Intrinsic::x86_sse_ucomineq_ss:
3849 case Intrinsic::x86_sse2_comieq_sd:
3850 case Intrinsic::x86_sse2_comilt_sd:
3851 case Intrinsic::x86_sse2_comile_sd:
3852 case Intrinsic::x86_sse2_comigt_sd:
3853 case Intrinsic::x86_sse2_comige_sd:
3854 case Intrinsic::x86_sse2_comineq_sd:
3855 case Intrinsic::x86_sse2_ucomieq_sd:
3856 case Intrinsic::x86_sse2_ucomilt_sd:
3857 case Intrinsic::x86_sse2_ucomile_sd:
3858 case Intrinsic::x86_sse2_ucomigt_sd:
3859 case Intrinsic::x86_sse2_ucomige_sd:
3860 case Intrinsic::x86_sse2_ucomineq_sd: {
3862 ISD::CondCode CC = ISD::SETCC_INVALID;
3865 case Intrinsic::x86_sse_comieq_ss:
3866 case Intrinsic::x86_sse2_comieq_sd:
3870 case Intrinsic::x86_sse_comilt_ss:
3871 case Intrinsic::x86_sse2_comilt_sd:
3875 case Intrinsic::x86_sse_comile_ss:
3876 case Intrinsic::x86_sse2_comile_sd:
3880 case Intrinsic::x86_sse_comigt_ss:
3881 case Intrinsic::x86_sse2_comigt_sd:
3885 case Intrinsic::x86_sse_comige_ss:
3886 case Intrinsic::x86_sse2_comige_sd:
3890 case Intrinsic::x86_sse_comineq_ss:
3891 case Intrinsic::x86_sse2_comineq_sd:
3895 case Intrinsic::x86_sse_ucomieq_ss:
3896 case Intrinsic::x86_sse2_ucomieq_sd:
3897 Opc = X86ISD::UCOMI;
3900 case Intrinsic::x86_sse_ucomilt_ss:
3901 case Intrinsic::x86_sse2_ucomilt_sd:
3902 Opc = X86ISD::UCOMI;
3905 case Intrinsic::x86_sse_ucomile_ss:
3906 case Intrinsic::x86_sse2_ucomile_sd:
3907 Opc = X86ISD::UCOMI;
3910 case Intrinsic::x86_sse_ucomigt_ss:
3911 case Intrinsic::x86_sse2_ucomigt_sd:
3912 Opc = X86ISD::UCOMI;
3915 case Intrinsic::x86_sse_ucomige_ss:
3916 case Intrinsic::x86_sse2_ucomige_sd:
3917 Opc = X86ISD::UCOMI;
3920 case Intrinsic::x86_sse_ucomineq_ss:
3921 case Intrinsic::x86_sse2_ucomineq_sd:
3922 Opc = X86ISD::UCOMI;
3928 SDOperand LHS = Op.getOperand(1);
3929 SDOperand RHS = Op.getOperand(2);
3930 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
3932 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3933 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
3934 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
3935 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3936 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3937 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
3938 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
3943 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
3944 // Depths > 0 not supported yet!
3945 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3948 // Just load the return address
3949 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
3950 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
3953 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
3954 // Depths > 0 not supported yet!
3955 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3958 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
3959 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
3960 DAG.getConstant(4, getPointerTy()));
3963 /// LowerOperation - Provide custom lowering hooks for some operations.
3965 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3966 switch (Op.getOpcode()) {
3967 default: assert(0 && "Should not custom lower this!");
3968 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3969 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3970 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3971 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3972 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3973 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3974 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3975 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3976 case ISD::SHL_PARTS:
3977 case ISD::SRA_PARTS:
3978 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3979 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3980 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3981 case ISD::FABS: return LowerFABS(Op, DAG);
3982 case ISD::FNEG: return LowerFNEG(Op, DAG);
3983 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
3984 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
3985 case ISD::SELECT: return LowerSELECT(Op, DAG);
3986 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3987 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3988 case ISD::CALL: return LowerCALL(Op, DAG);
3989 case ISD::RET: return LowerRET(Op, DAG);
3990 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
3991 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3992 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3993 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3994 case ISD::VASTART: return LowerVASTART(Op, DAG);
3995 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
3996 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3997 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
3998 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4003 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4005 default: return NULL;
4006 case X86ISD::SHLD: return "X86ISD::SHLD";
4007 case X86ISD::SHRD: return "X86ISD::SHRD";
4008 case X86ISD::FAND: return "X86ISD::FAND";
4009 case X86ISD::FOR: return "X86ISD::FOR";
4010 case X86ISD::FXOR: return "X86ISD::FXOR";
4011 case X86ISD::FSRL: return "X86ISD::FSRL";
4012 case X86ISD::FILD: return "X86ISD::FILD";
4013 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4014 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4015 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4016 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4017 case X86ISD::FLD: return "X86ISD::FLD";
4018 case X86ISD::FST: return "X86ISD::FST";
4019 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4020 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4021 case X86ISD::CALL: return "X86ISD::CALL";
4022 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4023 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4024 case X86ISD::CMP: return "X86ISD::CMP";
4025 case X86ISD::COMI: return "X86ISD::COMI";
4026 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4027 case X86ISD::SETCC: return "X86ISD::SETCC";
4028 case X86ISD::CMOV: return "X86ISD::CMOV";
4029 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4030 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4031 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4032 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4033 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
4034 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
4035 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4036 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4037 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4038 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4039 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4040 case X86ISD::FMAX: return "X86ISD::FMAX";
4041 case X86ISD::FMIN: return "X86ISD::FMIN";
4045 /// isLegalAddressImmediate - Return true if the integer value can be used
4046 /// as the offset of the target addressing mode for load / store of the
4048 bool X86TargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
4049 // X86 allows a sign-extended 32-bit immediate field.
4050 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4053 /// isLegalAddressImmediate - Return true if the GlobalValue can be used as
4054 /// the offset of the target addressing mode.
4055 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
4056 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement
4057 // field unless we are in small code model.
4058 if (Subtarget->is64Bit() &&
4059 getTargetMachine().getCodeModel() != CodeModel::Small)
4062 return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false));
4065 /// isLegalAddressScale - Return true if the integer value can be used as the
4066 /// scale of the target addressing mode for load / store of the given type.
4067 bool X86TargetLowering::isLegalAddressScale(int64_t S, const Type *Ty) const {
4071 case 2: case 4: case 8:
4073 // FIXME: These require both scale + index last and thus more expensive.
4074 // How to tell LSR to try for 2, 4, 8 first?
4075 case 3: case 5: case 9:
4080 /// isLegalAddressScaleAndImm - Return true if S works for IsLegalAddressScale
4081 /// and V works for isLegalAddressImmediate _and_ both can be applied
4082 /// simultaneously to the same instruction.
4083 bool X86TargetLowering::isLegalAddressScaleAndImm(int64_t S, int64_t V,
4084 const Type* Ty) const {
4085 return isLegalAddressScale(S, Ty) && isLegalAddressImmediate(V, Ty);
4088 /// isLegalAddressScaleAndImm - Return true if S works for IsLegalAddressScale
4089 /// and GV works for isLegalAddressImmediate _and_ both can be applied
4090 /// simultaneously to the same instruction.
4091 bool X86TargetLowering::isLegalAddressScaleAndImm(int64_t S, GlobalValue *GV,
4092 const Type* Ty) const {
4093 return isLegalAddressScale(S, Ty) && isLegalAddressImmediate(GV);
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);
4594 std::vector<unsigned> X86TargetLowering::
4595 getRegClassForInlineAsmConstraint(const std::string &Constraint,
4596 MVT::ValueType VT) const {
4597 if (Constraint.size() == 1) {
4598 // FIXME: not handling fp-stack yet!
4599 // FIXME: not handling MMX registers yet ('y' constraint).
4600 switch (Constraint[0]) { // GCC X86 Constraint Letters
4601 default: break; // Unknown constraint letter
4602 case 'A': // EAX/EDX
4603 if (VT == MVT::i32 || VT == MVT::i64)
4604 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4606 case 'r': // GENERAL_REGS
4607 case 'R': // LEGACY_REGS
4608 if (VT == MVT::i64 && Subtarget->is64Bit())
4609 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
4610 X86::RSI, X86::RDI, X86::RBP, X86::RSP,
4611 X86::R8, X86::R9, X86::R10, X86::R11,
4612 X86::R12, X86::R13, X86::R14, X86::R15, 0);
4614 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4615 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4616 else if (VT == MVT::i16)
4617 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4618 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4619 else if (VT == MVT::i8)
4620 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
4622 case 'l': // INDEX_REGS
4624 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4625 X86::ESI, X86::EDI, X86::EBP, 0);
4626 else if (VT == MVT::i16)
4627 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4628 X86::SI, X86::DI, X86::BP, 0);
4629 else if (VT == MVT::i8)
4630 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4632 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4635 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4636 else if (VT == MVT::i16)
4637 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4638 else if (VT == MVT::i8)
4639 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4641 case 'x': // SSE_REGS if SSE1 allowed
4642 if (Subtarget->hasSSE1())
4643 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4644 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4646 return std::vector<unsigned>();
4647 case 'Y': // SSE_REGS if SSE2 allowed
4648 if (Subtarget->hasSSE2())
4649 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4650 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4652 return std::vector<unsigned>();
4656 return std::vector<unsigned>();
4659 std::pair<unsigned, const TargetRegisterClass*>
4660 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4661 MVT::ValueType VT) const {
4662 // Use the default implementation in TargetLowering to convert the register
4663 // constraint into a member of a register class.
4664 std::pair<unsigned, const TargetRegisterClass*> Res;
4665 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4667 // Not found as a standard register?
4668 if (Res.second == 0) {
4669 // GCC calls "st(0)" just plain "st".
4670 if (StringsEqualNoCase("{st}", Constraint)) {
4671 Res.first = X86::ST0;
4672 Res.second = X86::RSTRegisterClass;
4678 // Otherwise, check to see if this is a register class of the wrong value
4679 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4680 // turn into {ax},{dx}.
4681 if (Res.second->hasType(VT))
4682 return Res; // Correct type already, nothing to do.
4684 // All of the single-register GCC register classes map their values onto
4685 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4686 // really want an 8-bit or 32-bit register, map to the appropriate register
4687 // class and return the appropriate register.
4688 if (Res.second != X86::GR16RegisterClass)
4691 if (VT == MVT::i8) {
4692 unsigned DestReg = 0;
4693 switch (Res.first) {
4695 case X86::AX: DestReg = X86::AL; break;
4696 case X86::DX: DestReg = X86::DL; break;
4697 case X86::CX: DestReg = X86::CL; break;
4698 case X86::BX: DestReg = X86::BL; break;
4701 Res.first = DestReg;
4702 Res.second = Res.second = X86::GR8RegisterClass;
4704 } else if (VT == MVT::i32) {
4705 unsigned DestReg = 0;
4706 switch (Res.first) {
4708 case X86::AX: DestReg = X86::EAX; break;
4709 case X86::DX: DestReg = X86::EDX; break;
4710 case X86::CX: DestReg = X86::ECX; break;
4711 case X86::BX: DestReg = X86::EBX; break;
4712 case X86::SI: DestReg = X86::ESI; break;
4713 case X86::DI: DestReg = X86::EDI; break;
4714 case X86::BP: DestReg = X86::EBP; break;
4715 case X86::SP: DestReg = X86::ESP; break;
4718 Res.first = DestReg;
4719 Res.second = Res.second = X86::GR32RegisterClass;
4721 } else if (VT == MVT::i64) {
4722 unsigned DestReg = 0;
4723 switch (Res.first) {
4725 case X86::AX: DestReg = X86::RAX; break;
4726 case X86::DX: DestReg = X86::RDX; break;
4727 case X86::CX: DestReg = X86::RCX; break;
4728 case X86::BX: DestReg = X86::RBX; break;
4729 case X86::SI: DestReg = X86::RSI; break;
4730 case X86::DI: DestReg = X86::RDI; break;
4731 case X86::BP: DestReg = X86::RBP; break;
4732 case X86::SP: DestReg = X86::RSP; break;
4735 Res.first = DestReg;
4736 Res.second = Res.second = X86::GR64RegisterClass;