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/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/SelectionDAG.h"
31 #include "llvm/CodeGen/SSARegMap.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/ADT/StringExtras.h"
39 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
40 cl::desc("Enable fastcc on X86"));
41 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
43 Subtarget = &TM.getSubtarget<X86Subtarget>();
44 X86ScalarSSE = Subtarget->hasSSE2();
45 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
47 // Set up the TargetLowering object.
49 // X86 is weird, it always uses i8 for shift amounts and setcc results.
50 setShiftAmountType(MVT::i8);
51 setSetCCResultType(MVT::i8);
52 setSetCCResultContents(ZeroOrOneSetCCResult);
53 setSchedulingPreference(SchedulingForRegPressure);
54 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
55 setStackPointerRegisterToSaveRestore(X86StackPtr);
57 if (Subtarget->isTargetDarwin()) {
58 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
59 setUseUnderscoreSetJmp(false);
60 setUseUnderscoreLongJmp(false);
61 } else if (Subtarget->isTargetMingw()) {
62 // MS runtime is weird: it exports _setjmp, but longjmp!
63 setUseUnderscoreSetJmp(true);
64 setUseUnderscoreLongJmp(false);
66 setUseUnderscoreSetJmp(true);
67 setUseUnderscoreLongJmp(true);
70 // Add legal addressing mode scale values.
71 addLegalAddressScale(8);
72 addLegalAddressScale(4);
73 addLegalAddressScale(2);
74 // Enter the ones which require both scale + index last. These are more
76 addLegalAddressScale(9);
77 addLegalAddressScale(5);
78 addLegalAddressScale(3);
80 // Set up the register classes.
81 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
84 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
87 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
89 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
91 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
92 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
93 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
95 if (Subtarget->is64Bit()) {
96 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
97 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
100 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
101 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
103 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
106 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
108 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
109 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
110 // SSE has no i16 to fp conversion, only i32
112 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
114 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
115 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
118 if (!Subtarget->is64Bit()) {
119 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
120 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
121 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
124 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
126 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
127 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
130 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
132 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
133 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
136 // Handle FP_TO_UINT by promoting the destination to a larger signed
138 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
140 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
142 if (Subtarget->is64Bit()) {
143 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
144 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
146 if (X86ScalarSSE && !Subtarget->hasSSE3())
147 // Expand FP_TO_UINT into a select.
148 // FIXME: We would like to use a Custom expander here eventually to do
149 // the optimal thing for SSE vs. the default expansion in the legalizer.
150 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
152 // With SSE3 we can use fisttpll to convert to a signed i64.
153 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
156 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
158 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
159 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
162 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
163 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
164 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
165 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
166 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
167 if (Subtarget->is64Bit())
168 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
169 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
170 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
171 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
172 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
173 setOperationAction(ISD::FREM , MVT::f64 , Expand);
175 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
176 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
177 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
178 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
179 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
180 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
181 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
182 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
183 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
184 if (Subtarget->is64Bit()) {
185 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
186 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
187 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
190 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
191 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
193 // These should be promoted to a larger select which is supported.
194 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
195 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
196 // X86 wants to expand cmov itself.
197 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
198 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
199 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
200 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
201 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
202 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
203 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
204 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
205 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
206 if (Subtarget->is64Bit()) {
207 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
208 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
210 // X86 ret instruction may pop stack.
211 setOperationAction(ISD::RET , MVT::Other, Custom);
213 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
214 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
215 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
216 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
217 if (Subtarget->is64Bit()) {
218 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
219 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
220 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
221 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
223 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
224 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
225 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
226 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
227 // X86 wants to expand memset / memcpy itself.
228 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
229 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
231 // We don't have line number support yet.
232 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
233 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
234 // FIXME - use subtarget debug flags
235 if (!Subtarget->isTargetDarwin() &&
236 !Subtarget->isTargetELF() &&
237 !Subtarget->isTargetCygMing())
238 setOperationAction(ISD::LABEL, MVT::Other, Expand);
240 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
241 setOperationAction(ISD::VASTART , MVT::Other, Custom);
243 // Use the default implementation.
244 setOperationAction(ISD::VAARG , MVT::Other, Expand);
245 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
246 setOperationAction(ISD::VAEND , MVT::Other, Expand);
247 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
248 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
249 if (Subtarget->is64Bit())
250 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
251 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
254 // Set up the FP register classes.
255 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
256 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
258 // Use ANDPD to simulate FABS.
259 setOperationAction(ISD::FABS , MVT::f64, Custom);
260 setOperationAction(ISD::FABS , MVT::f32, Custom);
262 // Use XORP to simulate FNEG.
263 setOperationAction(ISD::FNEG , MVT::f64, Custom);
264 setOperationAction(ISD::FNEG , MVT::f32, Custom);
266 // Use ANDPD and ORPD to simulate FCOPYSIGN.
267 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
268 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
270 // We don't support sin/cos/fmod
271 setOperationAction(ISD::FSIN , MVT::f64, Expand);
272 setOperationAction(ISD::FCOS , MVT::f64, Expand);
273 setOperationAction(ISD::FREM , MVT::f64, Expand);
274 setOperationAction(ISD::FSIN , MVT::f32, Expand);
275 setOperationAction(ISD::FCOS , MVT::f32, Expand);
276 setOperationAction(ISD::FREM , MVT::f32, Expand);
278 // Expand FP immediates into loads from the stack, except for the special
280 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
281 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
282 addLegalFPImmediate(+0.0); // xorps / xorpd
284 // Set up the FP register classes.
285 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
287 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
288 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
289 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
292 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
293 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
296 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
297 addLegalFPImmediate(+0.0); // FLD0
298 addLegalFPImmediate(+1.0); // FLD1
299 addLegalFPImmediate(-0.0); // FLD0/FCHS
300 addLegalFPImmediate(-1.0); // FLD1/FCHS
303 // First set operation action for all vector types to expand. Then we
304 // will selectively turn on ones that can be effectively codegen'd.
305 for (unsigned VT = (unsigned)MVT::Vector + 1;
306 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
307 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
308 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
309 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
310 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
311 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
312 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
313 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
314 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
315 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
316 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
317 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
318 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
319 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
320 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
321 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
324 if (Subtarget->hasMMX()) {
325 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
326 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
327 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
329 // FIXME: add MMX packed arithmetics
330 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
331 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
332 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
335 if (Subtarget->hasSSE1()) {
336 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
338 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
339 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
340 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
341 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
342 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
343 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
344 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
345 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
346 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
349 if (Subtarget->hasSSE2()) {
350 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
351 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
352 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
353 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
354 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
356 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
357 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
358 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
359 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
360 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
361 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
362 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
363 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
364 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
365 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
366 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
368 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
369 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
370 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
371 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
372 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
373 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
375 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
376 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
377 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
378 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
379 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
381 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
382 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
383 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
384 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
385 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
386 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
388 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
389 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
390 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
391 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
392 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
393 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
394 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
395 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
396 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
397 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
398 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
399 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
402 // Custom lower v2i64 and v2f64 selects.
403 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
404 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
405 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
406 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
409 // We want to custom lower some of our intrinsics.
410 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
412 // We have target-specific dag combine patterns for the following nodes:
413 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
414 setTargetDAGCombine(ISD::SELECT);
416 computeRegisterProperties();
418 // FIXME: These should be based on subtarget info. Plus, the values should
419 // be smaller when we are in optimizing for size mode.
420 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
421 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
422 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
423 allowUnalignedMemoryAccesses = true; // x86 supports it!
426 //===----------------------------------------------------------------------===//
427 // C & StdCall Calling Convention implementation
428 //===----------------------------------------------------------------------===//
429 // StdCall calling convention seems to be standard for many Windows' API
430 // routines and around. It differs from C calling convention just a little:
431 // callee should clean up the stack, not caller. Symbols should be also
432 // decorated in some fancy way :) It doesn't support any vector arguments.
434 /// AddLiveIn - This helper function adds the specified physical register to the
435 /// MachineFunction as a live in value. It also creates a corresponding virtual
437 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
438 const TargetRegisterClass *RC) {
439 assert(RC->contains(PReg) && "Not the correct regclass!");
440 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
441 MF.addLiveIn(PReg, VReg);
445 /// HowToPassArgument - Returns how an formal argument of the specified type
446 /// should be passed. If it is through stack, returns the size of the stack
447 /// slot; if it is through integer or XMM register, returns the number of
448 /// integer or XMM registers are needed.
450 HowToPassCallArgument(MVT::ValueType ObjectVT,
452 unsigned NumIntRegs, unsigned NumXMMRegs,
453 unsigned MaxNumIntRegs,
454 unsigned &ObjSize, unsigned &ObjIntRegs,
455 unsigned &ObjXMMRegs,
456 bool AllowVectors = true) {
461 if (MaxNumIntRegs>3) {
462 // We don't have too much registers on ia32! :)
467 default: assert(0 && "Unhandled argument type!");
469 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
475 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
481 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
487 if (ArgInReg && (NumIntRegs+2 <= MaxNumIntRegs)) {
489 } else if (ArgInReg && (NumIntRegs+1 <= MaxNumIntRegs)) {
513 assert(0 && "Unhandled argument type [vector]!");
517 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
519 unsigned NumArgs = Op.Val->getNumValues() - 1;
520 MachineFunction &MF = DAG.getMachineFunction();
521 MachineFrameInfo *MFI = MF.getFrameInfo();
522 SDOperand Root = Op.getOperand(0);
523 std::vector<SDOperand> ArgValues;
524 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
526 // Add DAG nodes to load the arguments... On entry to a function on the X86,
527 // the stack frame looks like this:
529 // [ESP] -- return address
530 // [ESP + 4] -- first argument (leftmost lexically)
531 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
534 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
535 unsigned NumSRetBytes= 0; // How much bytes on stack used for struct return
536 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
537 unsigned NumIntRegs = 0; // Integer regs used for parameter passing
539 static const unsigned XMMArgRegs[] = {
540 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
542 static const unsigned GPRArgRegs[][3] = {
543 { X86::AL, X86::DL, X86::CL },
544 { X86::AX, X86::DX, X86::CX },
545 { X86::EAX, X86::EDX, X86::ECX }
547 static const TargetRegisterClass* GPRClasses[3] = {
548 X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass
551 // Handle regparm attribute
552 std::vector<bool> ArgInRegs(NumArgs, false);
553 std::vector<bool> SRetArgs(NumArgs, false);
555 for (unsigned i = 0; i<NumArgs; ++i) {
556 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3+i))->getValue();
557 ArgInRegs[i] = (Flags >> 1) & 1;
558 SRetArgs[i] = (Flags >> 2) & 1;
562 for (unsigned i = 0; i < NumArgs; ++i) {
563 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
564 unsigned ArgIncrement = 4;
565 unsigned ObjSize = 0;
566 unsigned ObjXMMRegs = 0;
567 unsigned ObjIntRegs = 0;
571 HowToPassCallArgument(ObjectVT,
573 NumIntRegs, NumXMMRegs, 3,
574 ObjSize, ObjIntRegs, ObjXMMRegs,
578 ArgIncrement = ObjSize;
580 if (ObjIntRegs || ObjXMMRegs) {
582 default: assert(0 && "Unhandled argument type!");
586 unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][NumIntRegs];
587 Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]);
588 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
597 assert(!isStdCall && "Unhandled argument type!");
598 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
599 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
602 NumIntRegs += ObjIntRegs;
603 NumXMMRegs += ObjXMMRegs;
606 // XMM arguments have to be aligned on 16-byte boundary.
608 ArgOffset = ((ArgOffset + 15) / 16) * 16;
609 // Create the SelectionDAG nodes corresponding to a load from this
611 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
612 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
613 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
615 ArgOffset += ArgIncrement; // Move on to the next argument.
617 NumSRetBytes += ArgIncrement;
620 ArgValues.push_back(ArgValue);
623 ArgValues.push_back(Root);
625 // If the function takes variable number of arguments, make a frame index for
626 // the start of the first vararg value... for expansion of llvm.va_start.
628 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
630 if (isStdCall && !isVarArg) {
631 BytesToPopOnReturn = ArgOffset; // Callee pops everything..
632 BytesCallerReserves = 0;
634 BytesToPopOnReturn = NumSRetBytes; // Callee pops hidden struct pointer.
635 BytesCallerReserves = ArgOffset;
638 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
639 ReturnAddrIndex = 0; // No return address slot generated yet.
642 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
644 // Return the new list of results.
645 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
646 Op.Val->value_end());
647 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
650 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
652 SDOperand Chain = Op.getOperand(0);
653 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
654 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
655 SDOperand Callee = Op.getOperand(4);
656 MVT::ValueType RetVT= Op.Val->getValueType(0);
657 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
659 static const unsigned XMMArgRegs[] = {
660 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
662 static const unsigned GPR32ArgRegs[] = {
663 X86::EAX, X86::EDX, X86::ECX
666 // Count how many bytes are to be pushed on the stack.
667 unsigned NumBytes = 0;
668 // Keep track of the number of integer regs passed so far.
669 unsigned NumIntRegs = 0;
670 // Keep track of the number of XMM regs passed so far.
671 unsigned NumXMMRegs = 0;
672 // How much bytes on stack used for struct return
673 unsigned NumSRetBytes= 0;
675 // Handle regparm attribute
676 std::vector<bool> ArgInRegs(NumOps, false);
677 std::vector<bool> SRetArgs(NumOps, false);
678 for (unsigned i = 0; i<NumOps; ++i) {
680 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue();
681 ArgInRegs[i] = (Flags >> 1) & 1;
682 SRetArgs[i] = (Flags >> 2) & 1;
685 // Calculate stack frame size
686 for (unsigned i = 0; i != NumOps; ++i) {
687 SDOperand Arg = Op.getOperand(5+2*i);
688 unsigned ArgIncrement = 4;
689 unsigned ObjSize = 0;
690 unsigned ObjIntRegs = 0;
691 unsigned ObjXMMRegs = 0;
693 HowToPassCallArgument(Arg.getValueType(),
695 NumIntRegs, NumXMMRegs, 3,
696 ObjSize, ObjIntRegs, ObjXMMRegs,
699 ArgIncrement = ObjSize;
701 NumIntRegs += ObjIntRegs;
702 NumXMMRegs += ObjXMMRegs;
704 // XMM arguments have to be aligned on 16-byte boundary.
706 NumBytes = ((NumBytes + 15) / 16) * 16;
707 NumBytes += ArgIncrement;
711 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
713 // Arguments go on the stack in reverse order, as specified by the ABI.
714 unsigned ArgOffset = 0;
717 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
718 std::vector<SDOperand> MemOpChains;
719 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
720 for (unsigned i = 0; i != NumOps; ++i) {
721 SDOperand Arg = Op.getOperand(5+2*i);
722 unsigned ArgIncrement = 4;
723 unsigned ObjSize = 0;
724 unsigned ObjIntRegs = 0;
725 unsigned ObjXMMRegs = 0;
727 HowToPassCallArgument(Arg.getValueType(),
729 NumIntRegs, NumXMMRegs, 3,
730 ObjSize, ObjIntRegs, ObjXMMRegs,
734 ArgIncrement = ObjSize;
736 if (Arg.getValueType() == MVT::i8 || Arg.getValueType() == MVT::i16) {
737 // Promote the integer to 32 bits. If the input type is signed use a
738 // sign extend, otherwise use a zero extend.
739 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue();
741 unsigned ExtOp = (Flags & 1) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
742 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
745 if (ObjIntRegs || ObjXMMRegs) {
746 switch (Arg.getValueType()) {
747 default: assert(0 && "Unhandled argument type!");
749 RegsToPass.push_back(std::make_pair(GPR32ArgRegs[NumIntRegs], Arg));
757 assert(!isStdCall && "Unhandled argument type!");
758 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
762 NumIntRegs += ObjIntRegs;
763 NumXMMRegs += ObjXMMRegs;
766 // XMM arguments have to be aligned on 16-byte boundary.
768 ArgOffset = ((ArgOffset + 15) / 16) * 16;
770 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
771 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
772 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
774 ArgOffset += ArgIncrement; // Move on to the next argument.
776 NumSRetBytes += ArgIncrement;
780 // Sanity check: we haven't seen NumSRetBytes > 4
781 assert((NumSRetBytes<=4) &&
782 "Too much space for struct-return pointer requested");
784 if (!MemOpChains.empty())
785 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
786 &MemOpChains[0], MemOpChains.size());
788 // Build a sequence of copy-to-reg nodes chained together with token chain
789 // and flag operands which copy the outgoing args into registers.
791 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
792 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
794 InFlag = Chain.getValue(1);
797 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
798 Subtarget->isPICStyleGOT()) {
799 Chain = DAG.getCopyToReg(Chain, X86::EBX,
800 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
802 InFlag = Chain.getValue(1);
805 // If the callee is a GlobalAddress node (quite common, every direct call is)
806 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
807 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
808 // We should use extra load for direct calls to dllimported functions in
810 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
811 getTargetMachine(), true))
812 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
813 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
814 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
816 std::vector<MVT::ValueType> NodeTys;
817 NodeTys.push_back(MVT::Other); // Returns a chain
818 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
819 std::vector<SDOperand> Ops;
820 Ops.push_back(Chain);
821 Ops.push_back(Callee);
823 // Add argument registers to the end of the list so that they are known live
825 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
826 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
827 RegsToPass[i].second.getValueType()));
830 Ops.push_back(InFlag);
832 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
833 NodeTys, &Ops[0], Ops.size());
834 InFlag = Chain.getValue(1);
836 // Create the CALLSEQ_END node.
837 unsigned NumBytesForCalleeToPush = 0;
841 NumBytesForCalleeToPush = NumSRetBytes;
843 NumBytesForCalleeToPush = NumBytes;
846 // If this is is a call to a struct-return function, the callee
847 // pops the hidden struct pointer, so we have to push it back.
848 // This is common for Darwin/X86, Linux & Mingw32 targets.
849 NumBytesForCalleeToPush = NumSRetBytes;
853 NodeTys.push_back(MVT::Other); // Returns a chain
854 if (RetVT != MVT::Other)
855 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
857 Ops.push_back(Chain);
858 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
859 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
860 Ops.push_back(InFlag);
861 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
862 if (RetVT != MVT::Other)
863 InFlag = Chain.getValue(1);
865 std::vector<SDOperand> ResultVals;
868 default: assert(0 && "Unknown value type to return!");
869 case MVT::Other: break;
871 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
872 ResultVals.push_back(Chain.getValue(0));
873 NodeTys.push_back(MVT::i8);
876 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
877 ResultVals.push_back(Chain.getValue(0));
878 NodeTys.push_back(MVT::i16);
881 if (Op.Val->getValueType(1) == MVT::i32) {
882 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
883 ResultVals.push_back(Chain.getValue(0));
884 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
885 Chain.getValue(2)).getValue(1);
886 ResultVals.push_back(Chain.getValue(0));
887 NodeTys.push_back(MVT::i32);
889 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
890 ResultVals.push_back(Chain.getValue(0));
892 NodeTys.push_back(MVT::i32);
900 assert(!isStdCall && "Unknown value type to return!");
901 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
902 ResultVals.push_back(Chain.getValue(0));
903 NodeTys.push_back(RetVT);
907 std::vector<MVT::ValueType> Tys;
908 Tys.push_back(MVT::f64);
909 Tys.push_back(MVT::Other);
910 Tys.push_back(MVT::Flag);
911 std::vector<SDOperand> Ops;
912 Ops.push_back(Chain);
913 Ops.push_back(InFlag);
914 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
915 &Ops[0], Ops.size());
916 Chain = RetVal.getValue(1);
917 InFlag = RetVal.getValue(2);
919 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
920 // shouldn't be necessary except that RFP cannot be live across
921 // multiple blocks. When stackifier is fixed, they can be uncoupled.
922 MachineFunction &MF = DAG.getMachineFunction();
923 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
924 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
926 Tys.push_back(MVT::Other);
928 Ops.push_back(Chain);
929 Ops.push_back(RetVal);
930 Ops.push_back(StackSlot);
931 Ops.push_back(DAG.getValueType(RetVT));
932 Ops.push_back(InFlag);
933 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
934 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
935 Chain = RetVal.getValue(1);
938 if (RetVT == MVT::f32 && !X86ScalarSSE)
939 // FIXME: we would really like to remember that this FP_ROUND
940 // operation is okay to eliminate if we allow excess FP precision.
941 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
942 ResultVals.push_back(RetVal);
943 NodeTys.push_back(RetVT);
948 // If the function returns void, just return the chain.
949 if (ResultVals.empty())
952 // Otherwise, merge everything together with a MERGE_VALUES node.
953 NodeTys.push_back(MVT::Other);
954 ResultVals.push_back(Chain);
955 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
956 &ResultVals[0], ResultVals.size());
957 return Res.getValue(Op.ResNo);
961 //===----------------------------------------------------------------------===//
962 // X86-64 C Calling Convention implementation
963 //===----------------------------------------------------------------------===//
965 /// HowToPassX86_64CCCArgument - Returns how an formal argument of the specified
966 /// type should be passed. If it is through stack, returns the size of the stack
967 /// slot; if it is through integer or XMM register, returns the number of
968 /// integer or XMM registers are needed.
970 HowToPassX86_64CCCArgument(MVT::ValueType ObjectVT,
971 unsigned NumIntRegs, unsigned NumXMMRegs,
972 unsigned &ObjSize, unsigned &ObjIntRegs,
973 unsigned &ObjXMMRegs) {
979 default: assert(0 && "Unhandled argument type!");
989 case MVT::i8: ObjSize = 1; break;
990 case MVT::i16: ObjSize = 2; break;
991 case MVT::i32: ObjSize = 4; break;
992 case MVT::i64: ObjSize = 8; break;
1009 case MVT::f32: ObjSize = 4; break;
1010 case MVT::f64: ObjSize = 8; break;
1016 case MVT::v2f64: ObjSize = 16; break;
1024 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1025 unsigned NumArgs = Op.Val->getNumValues() - 1;
1026 MachineFunction &MF = DAG.getMachineFunction();
1027 MachineFrameInfo *MFI = MF.getFrameInfo();
1028 SDOperand Root = Op.getOperand(0);
1029 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1030 std::vector<SDOperand> ArgValues;
1032 // Add DAG nodes to load the arguments... On entry to a function on the X86,
1033 // the stack frame looks like this:
1035 // [RSP] -- return address
1036 // [RSP + 8] -- first nonreg argument (leftmost lexically)
1037 // [RSP +16] -- second nonreg argument, if 1st argument is <= 8 bytes in size
1040 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1041 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
1042 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1044 static const unsigned GPR8ArgRegs[] = {
1045 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
1047 static const unsigned GPR16ArgRegs[] = {
1048 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
1050 static const unsigned GPR32ArgRegs[] = {
1051 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1053 static const unsigned GPR64ArgRegs[] = {
1054 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1056 static const unsigned XMMArgRegs[] = {
1057 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1058 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1061 for (unsigned i = 0; i < NumArgs; ++i) {
1062 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1063 unsigned ArgIncrement = 8;
1064 unsigned ObjSize = 0;
1065 unsigned ObjIntRegs = 0;
1066 unsigned ObjXMMRegs = 0;
1068 // FIXME: __int128 and long double support?
1069 HowToPassX86_64CCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
1070 ObjSize, ObjIntRegs, ObjXMMRegs);
1072 ArgIncrement = ObjSize;
1076 if (ObjIntRegs || ObjXMMRegs) {
1078 default: assert(0 && "Unhandled argument type!");
1083 TargetRegisterClass *RC = NULL;
1087 RC = X86::GR8RegisterClass;
1088 Reg = GPR8ArgRegs[NumIntRegs];
1091 RC = X86::GR16RegisterClass;
1092 Reg = GPR16ArgRegs[NumIntRegs];
1095 RC = X86::GR32RegisterClass;
1096 Reg = GPR32ArgRegs[NumIntRegs];
1099 RC = X86::GR64RegisterClass;
1100 Reg = GPR64ArgRegs[NumIntRegs];
1103 Reg = AddLiveIn(MF, Reg, RC);
1104 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1115 TargetRegisterClass *RC= (ObjectVT == MVT::f32) ?
1116 X86::FR32RegisterClass : ((ObjectVT == MVT::f64) ?
1117 X86::FR64RegisterClass : X86::VR128RegisterClass);
1118 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], RC);
1119 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1123 NumIntRegs += ObjIntRegs;
1124 NumXMMRegs += ObjXMMRegs;
1125 } else if (ObjSize) {
1126 // XMM arguments have to be aligned on 16-byte boundary.
1128 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1129 // Create the SelectionDAG nodes corresponding to a load from this
1131 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1132 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1133 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1134 ArgOffset += ArgIncrement; // Move on to the next argument.
1137 ArgValues.push_back(ArgValue);
1140 // If the function takes variable number of arguments, make a frame index for
1141 // the start of the first vararg value... for expansion of llvm.va_start.
1143 // For X86-64, if there are vararg parameters that are passed via
1144 // registers, then we must store them to their spots on the stack so they
1145 // may be loaded by deferencing the result of va_next.
1146 VarArgsGPOffset = NumIntRegs * 8;
1147 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1148 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
1149 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1151 // Store the integer parameter registers.
1152 std::vector<SDOperand> MemOps;
1153 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1154 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1155 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1156 for (; NumIntRegs != 6; ++NumIntRegs) {
1157 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1158 X86::GR64RegisterClass);
1159 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1160 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1161 MemOps.push_back(Store);
1162 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1163 DAG.getConstant(8, getPointerTy()));
1166 // Now store the XMM (fp + vector) parameter registers.
1167 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1168 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1169 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1170 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1171 X86::VR128RegisterClass);
1172 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1173 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1174 MemOps.push_back(Store);
1175 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1176 DAG.getConstant(16, getPointerTy()));
1178 if (!MemOps.empty())
1179 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1180 &MemOps[0], MemOps.size());
1183 ArgValues.push_back(Root);
1185 ReturnAddrIndex = 0; // No return address slot generated yet.
1186 BytesToPopOnReturn = 0; // Callee pops nothing.
1187 BytesCallerReserves = ArgOffset;
1189 // Return the new list of results.
1190 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1191 Op.Val->value_end());
1192 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1196 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG) {
1197 SDOperand Chain = Op.getOperand(0);
1198 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1199 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1200 SDOperand Callee = Op.getOperand(4);
1201 MVT::ValueType RetVT= Op.Val->getValueType(0);
1202 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1204 // Count how many bytes are to be pushed on the stack.
1205 unsigned NumBytes = 0;
1206 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
1207 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1209 static const unsigned GPR8ArgRegs[] = {
1210 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
1212 static const unsigned GPR16ArgRegs[] = {
1213 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
1215 static const unsigned GPR32ArgRegs[] = {
1216 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1218 static const unsigned GPR64ArgRegs[] = {
1219 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1221 static const unsigned XMMArgRegs[] = {
1222 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1223 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1226 for (unsigned i = 0; i != NumOps; ++i) {
1227 SDOperand Arg = Op.getOperand(5+2*i);
1228 MVT::ValueType ArgVT = Arg.getValueType();
1231 default: assert(0 && "Unknown value type!");
1251 else if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1254 // XMM arguments have to be aligned on 16-byte boundary.
1255 NumBytes = ((NumBytes + 15) / 16) * 16;
1262 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1264 // Arguments go on the stack in reverse order, as specified by the ABI.
1265 unsigned ArgOffset = 0;
1268 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1269 std::vector<SDOperand> MemOpChains;
1270 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1271 for (unsigned i = 0; i != NumOps; ++i) {
1272 SDOperand Arg = Op.getOperand(5+2*i);
1273 MVT::ValueType ArgVT = Arg.getValueType();
1276 default: assert(0 && "Unexpected ValueType for argument!");
1281 if (NumIntRegs < 6) {
1285 case MVT::i8: Reg = GPR8ArgRegs[NumIntRegs]; break;
1286 case MVT::i16: Reg = GPR16ArgRegs[NumIntRegs]; break;
1287 case MVT::i32: Reg = GPR32ArgRegs[NumIntRegs]; break;
1288 case MVT::i64: Reg = GPR64ArgRegs[NumIntRegs]; break;
1290 RegsToPass.push_back(std::make_pair(Reg, Arg));
1293 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1294 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1295 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1307 if (NumXMMRegs < 8) {
1308 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1311 if (ArgVT != MVT::f32 && ArgVT != MVT::f64) {
1312 // XMM arguments have to be aligned on 16-byte boundary.
1313 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1315 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1316 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1317 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1318 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1326 if (!MemOpChains.empty())
1327 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1328 &MemOpChains[0], MemOpChains.size());
1330 // Build a sequence of copy-to-reg nodes chained together with token chain
1331 // and flag operands which copy the outgoing args into registers.
1333 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1334 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1336 InFlag = Chain.getValue(1);
1340 // From AMD64 ABI document:
1341 // For calls that may call functions that use varargs or stdargs
1342 // (prototype-less calls or calls to functions containing ellipsis (...) in
1343 // the declaration) %al is used as hidden argument to specify the number
1344 // of SSE registers used. The contents of %al do not need to match exactly
1345 // the number of registers, but must be an ubound on the number of SSE
1346 // registers used and is in the range 0 - 8 inclusive.
1347 Chain = DAG.getCopyToReg(Chain, X86::AL,
1348 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1349 InFlag = Chain.getValue(1);
1352 // If the callee is a GlobalAddress node (quite common, every direct call is)
1353 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1354 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1355 // We should use extra load for direct calls to dllimported functions in
1357 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1358 getTargetMachine(), true))
1359 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1360 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1361 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1363 std::vector<MVT::ValueType> NodeTys;
1364 NodeTys.push_back(MVT::Other); // Returns a chain
1365 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1366 std::vector<SDOperand> Ops;
1367 Ops.push_back(Chain);
1368 Ops.push_back(Callee);
1370 // Add argument registers to the end of the list so that they are known live
1372 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1373 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1374 RegsToPass[i].second.getValueType()));
1377 Ops.push_back(InFlag);
1379 // FIXME: Do not generate X86ISD::TAILCALL for now.
1380 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1381 NodeTys, &Ops[0], Ops.size());
1382 InFlag = Chain.getValue(1);
1385 NodeTys.push_back(MVT::Other); // Returns a chain
1386 if (RetVT != MVT::Other)
1387 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1389 Ops.push_back(Chain);
1390 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1391 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1392 Ops.push_back(InFlag);
1393 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1394 if (RetVT != MVT::Other)
1395 InFlag = Chain.getValue(1);
1397 std::vector<SDOperand> ResultVals;
1400 default: assert(0 && "Unknown value type to return!");
1401 case MVT::Other: break;
1403 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1404 ResultVals.push_back(Chain.getValue(0));
1405 NodeTys.push_back(MVT::i8);
1408 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1409 ResultVals.push_back(Chain.getValue(0));
1410 NodeTys.push_back(MVT::i16);
1413 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1414 ResultVals.push_back(Chain.getValue(0));
1415 NodeTys.push_back(MVT::i32);
1418 if (Op.Val->getValueType(1) == MVT::i64) {
1419 // FIXME: __int128 support?
1420 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1421 ResultVals.push_back(Chain.getValue(0));
1422 Chain = DAG.getCopyFromReg(Chain, X86::RDX, MVT::i64,
1423 Chain.getValue(2)).getValue(1);
1424 ResultVals.push_back(Chain.getValue(0));
1425 NodeTys.push_back(MVT::i64);
1427 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1428 ResultVals.push_back(Chain.getValue(0));
1430 NodeTys.push_back(MVT::i64);
1440 // FIXME: long double support?
1441 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1442 ResultVals.push_back(Chain.getValue(0));
1443 NodeTys.push_back(RetVT);
1447 // If the function returns void, just return the chain.
1448 if (ResultVals.empty())
1451 // Otherwise, merge everything together with a MERGE_VALUES node.
1452 NodeTys.push_back(MVT::Other);
1453 ResultVals.push_back(Chain);
1454 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1455 &ResultVals[0], ResultVals.size());
1456 return Res.getValue(Op.ResNo);
1459 //===----------------------------------------------------------------------===//
1460 // Fast & FastCall Calling Convention implementation
1461 //===----------------------------------------------------------------------===//
1463 // The X86 'fast' calling convention passes up to two integer arguments in
1464 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
1465 // and requires that the callee pop its arguments off the stack (allowing proper
1466 // tail calls), and has the same return value conventions as C calling convs.
1468 // This calling convention always arranges for the callee pop value to be 8n+4
1469 // bytes, which is needed for tail recursion elimination and stack alignment
1472 // Note that this can be enhanced in the future to pass fp vals in registers
1473 // (when we have a global fp allocator) and do other tricks.
1475 //===----------------------------------------------------------------------===//
1476 // The X86 'fastcall' calling convention passes up to two integer arguments in
1477 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1478 // and requires that the callee pop its arguments off the stack (allowing proper
1479 // tail calls), and has the same return value conventions as C calling convs.
1481 // This calling convention always arranges for the callee pop value to be 8n+4
1482 // bytes, which is needed for tail recursion elimination and stack alignment
1487 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG,
1489 unsigned NumArgs = Op.Val->getNumValues()-1;
1490 MachineFunction &MF = DAG.getMachineFunction();
1491 MachineFrameInfo *MFI = MF.getFrameInfo();
1492 SDOperand Root = Op.getOperand(0);
1493 std::vector<SDOperand> ArgValues;
1495 // Add DAG nodes to load the arguments... On entry to a function the stack
1496 // frame looks like this:
1498 // [ESP] -- return address
1499 // [ESP + 4] -- first nonreg argument (leftmost lexically)
1500 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
1502 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1504 // Keep track of the number of integer regs passed so far. This can be either
1505 // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX
1507 unsigned NumIntRegs = 0;
1508 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1510 static const unsigned XMMArgRegs[] = {
1511 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1514 static const unsigned GPRArgRegs[][2][2] = {
1515 {{ X86::AL, X86::DL }, { X86::CL, X86::DL }},
1516 {{ X86::AX, X86::DX }, { X86::CX, X86::DX }},
1517 {{ X86::EAX, X86::EDX }, { X86::ECX, X86::EDX }}
1520 static const TargetRegisterClass* GPRClasses[3] = {
1521 X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass
1524 unsigned GPRInd = (isFastCall ? 1 : 0);
1525 for (unsigned i = 0; i < NumArgs; ++i) {
1526 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1527 unsigned ArgIncrement = 4;
1528 unsigned ObjSize = 0;
1529 unsigned ObjXMMRegs = 0;
1530 unsigned ObjIntRegs = 0;
1534 HowToPassCallArgument(ObjectVT,
1535 true, // Use as much registers as possible
1536 NumIntRegs, NumXMMRegs,
1537 (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS),
1538 ObjSize, ObjIntRegs, ObjXMMRegs,
1542 ArgIncrement = ObjSize;
1544 if (ObjIntRegs || ObjXMMRegs) {
1546 default: assert(0 && "Unhandled argument type!");
1550 unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][GPRInd][NumIntRegs];
1551 Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]);
1552 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1561 assert(!isFastCall && "Unhandled argument type!");
1562 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
1563 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1567 NumIntRegs += ObjIntRegs;
1568 NumXMMRegs += ObjXMMRegs;
1571 // XMM arguments have to be aligned on 16-byte boundary.
1573 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1574 // Create the SelectionDAG nodes corresponding to a load from this
1576 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1577 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1578 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1580 ArgOffset += ArgIncrement; // Move on to the next argument.
1583 ArgValues.push_back(ArgValue);
1586 ArgValues.push_back(Root);
1588 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1589 // arguments and the arguments after the retaddr has been pushed are aligned.
1590 if ((ArgOffset & 7) == 0)
1593 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1594 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1595 ReturnAddrIndex = 0; // No return address slot generated yet.
1596 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1597 BytesCallerReserves = 0;
1599 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
1601 // Finally, inform the code generator which regs we return values in.
1602 switch (getValueType(MF.getFunction()->getReturnType())) {
1603 default: assert(0 && "Unknown type!");
1604 case MVT::isVoid: break;
1609 MF.addLiveOut(X86::EAX);
1612 MF.addLiveOut(X86::EAX);
1613 MF.addLiveOut(X86::EDX);
1617 MF.addLiveOut(X86::ST0);
1625 assert(!isFastCall && "Unknown result type");
1626 MF.addLiveOut(X86::XMM0);
1630 // Return the new list of results.
1631 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1632 Op.Val->value_end());
1633 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1636 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1638 SDOperand Chain = Op.getOperand(0);
1639 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1640 SDOperand Callee = Op.getOperand(4);
1641 MVT::ValueType RetVT= Op.Val->getValueType(0);
1642 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1644 // Count how many bytes are to be pushed on the stack.
1645 unsigned NumBytes = 0;
1647 // Keep track of the number of integer regs passed so far. This can be either
1648 // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX
1650 unsigned NumIntRegs = 0;
1651 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1653 static const unsigned GPRArgRegs[][2][2] = {
1654 {{ X86::AL, X86::DL }, { X86::CL, X86::DL }},
1655 {{ X86::AX, X86::DX }, { X86::CX, X86::DX }},
1656 {{ X86::EAX, X86::EDX }, { X86::ECX, X86::EDX }}
1658 static const unsigned XMMArgRegs[] = {
1659 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1662 unsigned GPRInd = (isFastCall ? 1 : 0);
1663 for (unsigned i = 0; i != NumOps; ++i) {
1664 SDOperand Arg = Op.getOperand(5+2*i);
1666 switch (Arg.getValueType()) {
1667 default: assert(0 && "Unknown value type!");
1671 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1672 if (NumIntRegs < MaxNumIntRegs) {
1689 assert(!isFastCall && "Unknown value type!");
1693 // XMM arguments have to be aligned on 16-byte boundary.
1694 NumBytes = ((NumBytes + 15) / 16) * 16;
1701 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1702 // arguments and the arguments after the retaddr has been pushed are aligned.
1703 if ((NumBytes & 7) == 0)
1706 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1708 // Arguments go on the stack in reverse order, as specified by the ABI.
1709 unsigned ArgOffset = 0;
1711 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1712 std::vector<SDOperand> MemOpChains;
1713 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1714 for (unsigned i = 0; i != NumOps; ++i) {
1715 SDOperand Arg = Op.getOperand(5+2*i);
1717 switch (Arg.getValueType()) {
1718 default: assert(0 && "Unexpected ValueType for argument!");
1722 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1723 if (NumIntRegs < MaxNumIntRegs) {
1725 GPRArgRegs[Arg.getValueType()-MVT::i8][GPRInd][NumIntRegs];
1726 RegsToPass.push_back(std::make_pair(RegToUse, Arg));
1732 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1733 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1734 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1739 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1740 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1741 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1751 assert(!isFastCall && "Unexpected ValueType for argument!");
1752 if (NumXMMRegs < 4) {
1753 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1756 // XMM arguments have to be aligned on 16-byte boundary.
1757 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1758 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1759 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1760 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1767 if (!MemOpChains.empty())
1768 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1769 &MemOpChains[0], MemOpChains.size());
1771 // Build a sequence of copy-to-reg nodes chained together with token chain
1772 // and flag operands which copy the outgoing args into registers.
1774 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1775 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1777 InFlag = Chain.getValue(1);
1780 // If the callee is a GlobalAddress node (quite common, every direct call is)
1781 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1782 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1783 // We should use extra load for direct calls to dllimported functions in
1785 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1786 getTargetMachine(), true))
1787 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1788 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1789 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1791 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1792 Subtarget->isPICStyleGOT()) {
1793 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1794 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1796 InFlag = Chain.getValue(1);
1799 std::vector<MVT::ValueType> NodeTys;
1800 NodeTys.push_back(MVT::Other); // Returns a chain
1801 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1802 std::vector<SDOperand> Ops;
1803 Ops.push_back(Chain);
1804 Ops.push_back(Callee);
1806 // Add argument registers to the end of the list so that they are known live
1808 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1809 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1810 RegsToPass[i].second.getValueType()));
1813 Ops.push_back(InFlag);
1815 // FIXME: Do not generate X86ISD::TAILCALL for now.
1816 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1817 NodeTys, &Ops[0], Ops.size());
1818 InFlag = Chain.getValue(1);
1821 NodeTys.push_back(MVT::Other); // Returns a chain
1822 if (RetVT != MVT::Other)
1823 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1825 Ops.push_back(Chain);
1826 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1827 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1828 Ops.push_back(InFlag);
1829 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1830 if (RetVT != MVT::Other)
1831 InFlag = Chain.getValue(1);
1833 std::vector<SDOperand> ResultVals;
1836 default: assert(0 && "Unknown value type to return!");
1837 case MVT::Other: break;
1839 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1840 ResultVals.push_back(Chain.getValue(0));
1841 NodeTys.push_back(MVT::i8);
1844 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1845 ResultVals.push_back(Chain.getValue(0));
1846 NodeTys.push_back(MVT::i16);
1849 if (Op.Val->getValueType(1) == MVT::i32) {
1850 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1851 ResultVals.push_back(Chain.getValue(0));
1852 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1853 Chain.getValue(2)).getValue(1);
1854 ResultVals.push_back(Chain.getValue(0));
1855 NodeTys.push_back(MVT::i32);
1857 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1858 ResultVals.push_back(Chain.getValue(0));
1860 NodeTys.push_back(MVT::i32);
1869 assert(0 && "Unknown value type to return!");
1871 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1872 ResultVals.push_back(Chain.getValue(0));
1873 NodeTys.push_back(RetVT);
1878 std::vector<MVT::ValueType> Tys;
1879 Tys.push_back(MVT::f64);
1880 Tys.push_back(MVT::Other);
1881 Tys.push_back(MVT::Flag);
1882 std::vector<SDOperand> Ops;
1883 Ops.push_back(Chain);
1884 Ops.push_back(InFlag);
1885 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
1886 &Ops[0], Ops.size());
1887 Chain = RetVal.getValue(1);
1888 InFlag = RetVal.getValue(2);
1890 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1891 // shouldn't be necessary except that RFP cannot be live across
1892 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1893 MachineFunction &MF = DAG.getMachineFunction();
1894 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1895 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1897 Tys.push_back(MVT::Other);
1899 Ops.push_back(Chain);
1900 Ops.push_back(RetVal);
1901 Ops.push_back(StackSlot);
1902 Ops.push_back(DAG.getValueType(RetVT));
1903 Ops.push_back(InFlag);
1904 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
1905 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
1906 Chain = RetVal.getValue(1);
1909 if (RetVT == MVT::f32 && !X86ScalarSSE)
1910 // FIXME: we would really like to remember that this FP_ROUND
1911 // operation is okay to eliminate if we allow excess FP precision.
1912 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1913 ResultVals.push_back(RetVal);
1914 NodeTys.push_back(RetVT);
1920 // If the function returns void, just return the chain.
1921 if (ResultVals.empty())
1924 // Otherwise, merge everything together with a MERGE_VALUES node.
1925 NodeTys.push_back(MVT::Other);
1926 ResultVals.push_back(Chain);
1927 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1928 &ResultVals[0], ResultVals.size());
1929 return Res.getValue(Op.ResNo);
1932 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1933 if (ReturnAddrIndex == 0) {
1934 // Set up a frame object for the return address.
1935 MachineFunction &MF = DAG.getMachineFunction();
1936 if (Subtarget->is64Bit())
1937 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1939 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1942 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1947 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1948 /// specific condition code. It returns a false if it cannot do a direct
1949 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1951 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1952 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1953 SelectionDAG &DAG) {
1954 X86CC = X86::COND_INVALID;
1956 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1957 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1958 // X > -1 -> X == 0, jump !sign.
1959 RHS = DAG.getConstant(0, RHS.getValueType());
1960 X86CC = X86::COND_NS;
1962 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1963 // X < 0 -> X == 0, jump on sign.
1964 X86CC = X86::COND_S;
1969 switch (SetCCOpcode) {
1971 case ISD::SETEQ: X86CC = X86::COND_E; break;
1972 case ISD::SETGT: X86CC = X86::COND_G; break;
1973 case ISD::SETGE: X86CC = X86::COND_GE; break;
1974 case ISD::SETLT: X86CC = X86::COND_L; break;
1975 case ISD::SETLE: X86CC = X86::COND_LE; break;
1976 case ISD::SETNE: X86CC = X86::COND_NE; break;
1977 case ISD::SETULT: X86CC = X86::COND_B; break;
1978 case ISD::SETUGT: X86CC = X86::COND_A; break;
1979 case ISD::SETULE: X86CC = X86::COND_BE; break;
1980 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1983 // On a floating point condition, the flags are set as follows:
1985 // 0 | 0 | 0 | X > Y
1986 // 0 | 0 | 1 | X < Y
1987 // 1 | 0 | 0 | X == Y
1988 // 1 | 1 | 1 | unordered
1990 switch (SetCCOpcode) {
1993 case ISD::SETEQ: X86CC = X86::COND_E; break;
1994 case ISD::SETOLT: Flip = true; // Fallthrough
1996 case ISD::SETGT: X86CC = X86::COND_A; break;
1997 case ISD::SETOLE: Flip = true; // Fallthrough
1999 case ISD::SETGE: X86CC = X86::COND_AE; break;
2000 case ISD::SETUGT: Flip = true; // Fallthrough
2002 case ISD::SETLT: X86CC = X86::COND_B; break;
2003 case ISD::SETUGE: Flip = true; // Fallthrough
2005 case ISD::SETLE: X86CC = X86::COND_BE; break;
2007 case ISD::SETNE: X86CC = X86::COND_NE; break;
2008 case ISD::SETUO: X86CC = X86::COND_P; break;
2009 case ISD::SETO: X86CC = X86::COND_NP; break;
2012 std::swap(LHS, RHS);
2015 return X86CC != X86::COND_INVALID;
2018 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
2019 /// code. Current x86 isa includes the following FP cmov instructions:
2020 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2021 static bool hasFPCMov(unsigned X86CC) {
2037 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2038 /// true if Op is undef or if its value falls within the specified range (L, H].
2039 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2040 if (Op.getOpcode() == ISD::UNDEF)
2043 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2044 return (Val >= Low && Val < Hi);
2047 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2048 /// true if Op is undef or if its value equal to the specified value.
2049 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2050 if (Op.getOpcode() == ISD::UNDEF)
2052 return cast<ConstantSDNode>(Op)->getValue() == Val;
2055 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2056 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
2057 bool X86::isPSHUFDMask(SDNode *N) {
2058 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2060 if (N->getNumOperands() != 4)
2063 // Check if the value doesn't reference the second vector.
2064 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2065 SDOperand Arg = N->getOperand(i);
2066 if (Arg.getOpcode() == ISD::UNDEF) continue;
2067 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2068 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
2075 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2076 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2077 bool X86::isPSHUFHWMask(SDNode *N) {
2078 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2080 if (N->getNumOperands() != 8)
2083 // Lower quadword copied in order.
2084 for (unsigned i = 0; i != 4; ++i) {
2085 SDOperand Arg = N->getOperand(i);
2086 if (Arg.getOpcode() == ISD::UNDEF) continue;
2087 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2088 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2092 // Upper quadword shuffled.
2093 for (unsigned i = 4; i != 8; ++i) {
2094 SDOperand Arg = N->getOperand(i);
2095 if (Arg.getOpcode() == ISD::UNDEF) continue;
2096 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2097 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2098 if (Val < 4 || Val > 7)
2105 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2106 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2107 bool X86::isPSHUFLWMask(SDNode *N) {
2108 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2110 if (N->getNumOperands() != 8)
2113 // Upper quadword copied in order.
2114 for (unsigned i = 4; i != 8; ++i)
2115 if (!isUndefOrEqual(N->getOperand(i), i))
2118 // Lower quadword shuffled.
2119 for (unsigned i = 0; i != 4; ++i)
2120 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2126 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2127 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
2128 static bool isSHUFPMask(std::vector<SDOperand> &N) {
2129 unsigned NumElems = N.size();
2130 if (NumElems != 2 && NumElems != 4) return false;
2132 unsigned Half = NumElems / 2;
2133 for (unsigned i = 0; i < Half; ++i)
2134 if (!isUndefOrInRange(N[i], 0, NumElems))
2136 for (unsigned i = Half; i < NumElems; ++i)
2137 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
2143 bool X86::isSHUFPMask(SDNode *N) {
2144 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2145 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2146 return ::isSHUFPMask(Ops);
2149 /// isCommutedSHUFP - Returns true if the shuffle mask is except
2150 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2151 /// half elements to come from vector 1 (which would equal the dest.) and
2152 /// the upper half to come from vector 2.
2153 static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
2154 unsigned NumElems = Ops.size();
2155 if (NumElems != 2 && NumElems != 4) return false;
2157 unsigned Half = NumElems / 2;
2158 for (unsigned i = 0; i < Half; ++i)
2159 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
2161 for (unsigned i = Half; i < NumElems; ++i)
2162 if (!isUndefOrInRange(Ops[i], 0, NumElems))
2167 static bool isCommutedSHUFP(SDNode *N) {
2168 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2169 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2170 return isCommutedSHUFP(Ops);
2173 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2174 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2175 bool X86::isMOVHLPSMask(SDNode *N) {
2176 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2178 if (N->getNumOperands() != 4)
2181 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2182 return isUndefOrEqual(N->getOperand(0), 6) &&
2183 isUndefOrEqual(N->getOperand(1), 7) &&
2184 isUndefOrEqual(N->getOperand(2), 2) &&
2185 isUndefOrEqual(N->getOperand(3), 3);
2188 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2189 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2191 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2192 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2194 if (N->getNumOperands() != 4)
2197 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2198 return isUndefOrEqual(N->getOperand(0), 2) &&
2199 isUndefOrEqual(N->getOperand(1), 3) &&
2200 isUndefOrEqual(N->getOperand(2), 2) &&
2201 isUndefOrEqual(N->getOperand(3), 3);
2204 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2205 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2206 bool X86::isMOVLPMask(SDNode *N) {
2207 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2209 unsigned NumElems = N->getNumOperands();
2210 if (NumElems != 2 && NumElems != 4)
2213 for (unsigned i = 0; i < NumElems/2; ++i)
2214 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2217 for (unsigned i = NumElems/2; i < NumElems; ++i)
2218 if (!isUndefOrEqual(N->getOperand(i), i))
2224 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2225 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2227 bool X86::isMOVHPMask(SDNode *N) {
2228 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2230 unsigned NumElems = N->getNumOperands();
2231 if (NumElems != 2 && NumElems != 4)
2234 for (unsigned i = 0; i < NumElems/2; ++i)
2235 if (!isUndefOrEqual(N->getOperand(i), i))
2238 for (unsigned i = 0; i < NumElems/2; ++i) {
2239 SDOperand Arg = N->getOperand(i + NumElems/2);
2240 if (!isUndefOrEqual(Arg, i + NumElems))
2247 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2248 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
2249 bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2250 unsigned NumElems = N.size();
2251 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2254 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2255 SDOperand BitI = N[i];
2256 SDOperand BitI1 = N[i+1];
2257 if (!isUndefOrEqual(BitI, j))
2260 if (isUndefOrEqual(BitI1, NumElems))
2263 if (!isUndefOrEqual(BitI1, j + NumElems))
2271 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2272 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2273 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2274 return ::isUNPCKLMask(Ops, V2IsSplat);
2277 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2278 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
2279 bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2280 unsigned NumElems = N.size();
2281 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2284 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2285 SDOperand BitI = N[i];
2286 SDOperand BitI1 = N[i+1];
2287 if (!isUndefOrEqual(BitI, j + NumElems/2))
2290 if (isUndefOrEqual(BitI1, NumElems))
2293 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
2301 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2302 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2303 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2304 return ::isUNPCKHMask(Ops, V2IsSplat);
2307 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2308 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2310 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2311 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2313 unsigned NumElems = N->getNumOperands();
2314 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
2317 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2318 SDOperand BitI = N->getOperand(i);
2319 SDOperand BitI1 = N->getOperand(i+1);
2321 if (!isUndefOrEqual(BitI, j))
2323 if (!isUndefOrEqual(BitI1, j))
2330 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2331 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2332 /// MOVSD, and MOVD, i.e. setting the lowest element.
2333 static bool isMOVLMask(std::vector<SDOperand> &N) {
2334 unsigned NumElems = N.size();
2335 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2338 if (!isUndefOrEqual(N[0], NumElems))
2341 for (unsigned i = 1; i < NumElems; ++i) {
2342 SDOperand Arg = N[i];
2343 if (!isUndefOrEqual(Arg, i))
2350 bool X86::isMOVLMask(SDNode *N) {
2351 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2352 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2353 return ::isMOVLMask(Ops);
2356 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2357 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2358 /// element of vector 2 and the other elements to come from vector 1 in order.
2359 static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false,
2360 bool V2IsUndef = false) {
2361 unsigned NumElems = Ops.size();
2362 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2365 if (!isUndefOrEqual(Ops[0], 0))
2368 for (unsigned i = 1; i < NumElems; ++i) {
2369 SDOperand Arg = Ops[i];
2370 if (!(isUndefOrEqual(Arg, i+NumElems) ||
2371 (V2IsUndef && isUndefOrInRange(Arg, NumElems, NumElems*2)) ||
2372 (V2IsSplat && isUndefOrEqual(Arg, NumElems))))
2379 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2380 bool V2IsUndef = false) {
2381 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2382 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2383 return isCommutedMOVL(Ops, V2IsSplat, V2IsUndef);
2386 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2387 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2388 bool X86::isMOVSHDUPMask(SDNode *N) {
2389 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2391 if (N->getNumOperands() != 4)
2394 // Expect 1, 1, 3, 3
2395 for (unsigned i = 0; i < 2; ++i) {
2396 SDOperand Arg = N->getOperand(i);
2397 if (Arg.getOpcode() == ISD::UNDEF) continue;
2398 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2399 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2400 if (Val != 1) return false;
2404 for (unsigned i = 2; i < 4; ++i) {
2405 SDOperand Arg = N->getOperand(i);
2406 if (Arg.getOpcode() == ISD::UNDEF) continue;
2407 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2408 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2409 if (Val != 3) return false;
2413 // Don't use movshdup if it can be done with a shufps.
2417 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2418 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2419 bool X86::isMOVSLDUPMask(SDNode *N) {
2420 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2422 if (N->getNumOperands() != 4)
2425 // Expect 0, 0, 2, 2
2426 for (unsigned i = 0; i < 2; ++i) {
2427 SDOperand Arg = N->getOperand(i);
2428 if (Arg.getOpcode() == ISD::UNDEF) continue;
2429 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2430 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2431 if (Val != 0) return false;
2435 for (unsigned i = 2; i < 4; ++i) {
2436 SDOperand Arg = N->getOperand(i);
2437 if (Arg.getOpcode() == ISD::UNDEF) continue;
2438 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2439 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2440 if (Val != 2) return false;
2444 // Don't use movshdup if it can be done with a shufps.
2448 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2449 /// a splat of a single element.
2450 static bool isSplatMask(SDNode *N) {
2451 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2453 // This is a splat operation if each element of the permute is the same, and
2454 // if the value doesn't reference the second vector.
2455 unsigned NumElems = N->getNumOperands();
2456 SDOperand ElementBase;
2458 for (; i != NumElems; ++i) {
2459 SDOperand Elt = N->getOperand(i);
2460 if (isa<ConstantSDNode>(Elt)) {
2466 if (!ElementBase.Val)
2469 for (; i != NumElems; ++i) {
2470 SDOperand Arg = N->getOperand(i);
2471 if (Arg.getOpcode() == ISD::UNDEF) continue;
2472 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2473 if (Arg != ElementBase) return false;
2476 // Make sure it is a splat of the first vector operand.
2477 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2480 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2481 /// a splat of a single element and it's a 2 or 4 element mask.
2482 bool X86::isSplatMask(SDNode *N) {
2483 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2485 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2486 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2488 return ::isSplatMask(N);
2491 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2492 /// specifies a splat of zero element.
2493 bool X86::isSplatLoMask(SDNode *N) {
2494 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2496 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2497 if (!isUndefOrEqual(N->getOperand(i), 0))
2502 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2503 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2505 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2506 unsigned NumOperands = N->getNumOperands();
2507 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2509 for (unsigned i = 0; i < NumOperands; ++i) {
2511 SDOperand Arg = N->getOperand(NumOperands-i-1);
2512 if (Arg.getOpcode() != ISD::UNDEF)
2513 Val = cast<ConstantSDNode>(Arg)->getValue();
2514 if (Val >= NumOperands) Val -= NumOperands;
2516 if (i != NumOperands - 1)
2523 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2524 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2526 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2528 // 8 nodes, but we only care about the last 4.
2529 for (unsigned i = 7; i >= 4; --i) {
2531 SDOperand Arg = N->getOperand(i);
2532 if (Arg.getOpcode() != ISD::UNDEF)
2533 Val = cast<ConstantSDNode>(Arg)->getValue();
2542 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2543 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2545 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2547 // 8 nodes, but we only care about the first 4.
2548 for (int i = 3; i >= 0; --i) {
2550 SDOperand Arg = N->getOperand(i);
2551 if (Arg.getOpcode() != ISD::UNDEF)
2552 Val = cast<ConstantSDNode>(Arg)->getValue();
2561 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2562 /// specifies a 8 element shuffle that can be broken into a pair of
2563 /// PSHUFHW and PSHUFLW.
2564 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2565 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2567 if (N->getNumOperands() != 8)
2570 // Lower quadword shuffled.
2571 for (unsigned i = 0; i != 4; ++i) {
2572 SDOperand Arg = N->getOperand(i);
2573 if (Arg.getOpcode() == ISD::UNDEF) continue;
2574 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2575 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2580 // Upper quadword shuffled.
2581 for (unsigned i = 4; i != 8; ++i) {
2582 SDOperand Arg = N->getOperand(i);
2583 if (Arg.getOpcode() == ISD::UNDEF) continue;
2584 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2585 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2586 if (Val < 4 || Val > 7)
2593 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2594 /// values in ther permute mask.
2595 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2596 SDOperand &V2, SDOperand &Mask,
2597 SelectionDAG &DAG) {
2598 MVT::ValueType VT = Op.getValueType();
2599 MVT::ValueType MaskVT = Mask.getValueType();
2600 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2601 unsigned NumElems = Mask.getNumOperands();
2602 std::vector<SDOperand> MaskVec;
2604 for (unsigned i = 0; i != NumElems; ++i) {
2605 SDOperand Arg = Mask.getOperand(i);
2606 if (Arg.getOpcode() == ISD::UNDEF) {
2607 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2610 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2611 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2613 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2615 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2619 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2620 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2623 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2624 /// match movhlps. The lower half elements should come from upper half of
2625 /// V1 (and in order), and the upper half elements should come from the upper
2626 /// half of V2 (and in order).
2627 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2628 unsigned NumElems = Mask->getNumOperands();
2631 for (unsigned i = 0, e = 2; i != e; ++i)
2632 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2634 for (unsigned i = 2; i != 4; ++i)
2635 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2640 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2641 /// is promoted to a vector.
2642 static inline bool isScalarLoadToVector(SDNode *N) {
2643 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2644 N = N->getOperand(0).Val;
2645 return ISD::isNON_EXTLoad(N);
2650 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2651 /// match movlp{s|d}. The lower half elements should come from lower half of
2652 /// V1 (and in order), and the upper half elements should come from the upper
2653 /// half of V2 (and in order). And since V1 will become the source of the
2654 /// MOVLP, it must be either a vector load or a scalar load to vector.
2655 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2656 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2658 // Is V2 is a vector load, don't do this transformation. We will try to use
2659 // load folding shufps op.
2660 if (ISD::isNON_EXTLoad(V2))
2663 unsigned NumElems = Mask->getNumOperands();
2664 if (NumElems != 2 && NumElems != 4)
2666 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2667 if (!isUndefOrEqual(Mask->getOperand(i), i))
2669 for (unsigned i = NumElems/2; i != NumElems; ++i)
2670 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2675 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2677 static bool isSplatVector(SDNode *N) {
2678 if (N->getOpcode() != ISD::BUILD_VECTOR)
2681 SDOperand SplatValue = N->getOperand(0);
2682 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2683 if (N->getOperand(i) != SplatValue)
2688 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2690 static bool isUndefShuffle(SDNode *N) {
2691 if (N->getOpcode() != ISD::BUILD_VECTOR)
2694 SDOperand V1 = N->getOperand(0);
2695 SDOperand V2 = N->getOperand(1);
2696 SDOperand Mask = N->getOperand(2);
2697 unsigned NumElems = Mask.getNumOperands();
2698 for (unsigned i = 0; i != NumElems; ++i) {
2699 SDOperand Arg = Mask.getOperand(i);
2700 if (Arg.getOpcode() != ISD::UNDEF) {
2701 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2702 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2704 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2711 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2712 /// that point to V2 points to its first element.
2713 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2714 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2716 bool Changed = false;
2717 std::vector<SDOperand> MaskVec;
2718 unsigned NumElems = Mask.getNumOperands();
2719 for (unsigned i = 0; i != NumElems; ++i) {
2720 SDOperand Arg = Mask.getOperand(i);
2721 if (Arg.getOpcode() != ISD::UNDEF) {
2722 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2723 if (Val > NumElems) {
2724 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2728 MaskVec.push_back(Arg);
2732 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2733 &MaskVec[0], MaskVec.size());
2737 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2738 /// operation of specified width.
2739 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2740 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2741 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2743 std::vector<SDOperand> MaskVec;
2744 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2745 for (unsigned i = 1; i != NumElems; ++i)
2746 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2747 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2750 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2751 /// of specified width.
2752 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2753 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2754 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2755 std::vector<SDOperand> MaskVec;
2756 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2757 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2758 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2760 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2763 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2764 /// of specified width.
2765 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2766 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2767 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2768 unsigned Half = NumElems/2;
2769 std::vector<SDOperand> MaskVec;
2770 for (unsigned i = 0; i != Half; ++i) {
2771 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2772 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2774 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2777 /// getZeroVector - Returns a vector of specified type with all zero elements.
2779 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2780 assert(MVT::isVector(VT) && "Expected a vector type");
2781 unsigned NumElems = getVectorNumElements(VT);
2782 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2783 bool isFP = MVT::isFloatingPoint(EVT);
2784 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2785 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2786 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2789 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2791 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2792 SDOperand V1 = Op.getOperand(0);
2793 SDOperand Mask = Op.getOperand(2);
2794 MVT::ValueType VT = Op.getValueType();
2795 unsigned NumElems = Mask.getNumOperands();
2796 Mask = getUnpacklMask(NumElems, DAG);
2797 while (NumElems != 4) {
2798 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2801 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2803 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2804 Mask = getZeroVector(MaskVT, DAG);
2805 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2806 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2807 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2810 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2812 static inline bool isZeroNode(SDOperand Elt) {
2813 return ((isa<ConstantSDNode>(Elt) &&
2814 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2815 (isa<ConstantFPSDNode>(Elt) &&
2816 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2819 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2820 /// vector and zero or undef vector.
2821 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2822 unsigned NumElems, unsigned Idx,
2823 bool isZero, SelectionDAG &DAG) {
2824 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2825 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2826 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2827 SDOperand Zero = DAG.getConstant(0, EVT);
2828 std::vector<SDOperand> MaskVec(NumElems, Zero);
2829 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2830 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2831 &MaskVec[0], MaskVec.size());
2832 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2835 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2837 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2838 unsigned NumNonZero, unsigned NumZero,
2839 SelectionDAG &DAG, TargetLowering &TLI) {
2845 for (unsigned i = 0; i < 16; ++i) {
2846 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2847 if (ThisIsNonZero && First) {
2849 V = getZeroVector(MVT::v8i16, DAG);
2851 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2856 SDOperand ThisElt(0, 0), LastElt(0, 0);
2857 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2858 if (LastIsNonZero) {
2859 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2861 if (ThisIsNonZero) {
2862 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2863 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2864 ThisElt, DAG.getConstant(8, MVT::i8));
2866 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2871 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2872 DAG.getConstant(i/2, TLI.getPointerTy()));
2876 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2879 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2881 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2882 unsigned NumNonZero, unsigned NumZero,
2883 SelectionDAG &DAG, TargetLowering &TLI) {
2889 for (unsigned i = 0; i < 8; ++i) {
2890 bool isNonZero = (NonZeros & (1 << i)) != 0;
2894 V = getZeroVector(MVT::v8i16, DAG);
2896 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2899 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2900 DAG.getConstant(i, TLI.getPointerTy()));
2908 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2909 // All zero's are handled with pxor.
2910 if (ISD::isBuildVectorAllZeros(Op.Val))
2913 // All one's are handled with pcmpeqd.
2914 if (ISD::isBuildVectorAllOnes(Op.Val))
2917 MVT::ValueType VT = Op.getValueType();
2918 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2919 unsigned EVTBits = MVT::getSizeInBits(EVT);
2921 unsigned NumElems = Op.getNumOperands();
2922 unsigned NumZero = 0;
2923 unsigned NumNonZero = 0;
2924 unsigned NonZeros = 0;
2925 std::set<SDOperand> Values;
2926 for (unsigned i = 0; i < NumElems; ++i) {
2927 SDOperand Elt = Op.getOperand(i);
2928 if (Elt.getOpcode() != ISD::UNDEF) {
2930 if (isZeroNode(Elt))
2933 NonZeros |= (1 << i);
2939 if (NumNonZero == 0)
2940 // Must be a mix of zero and undef. Return a zero vector.
2941 return getZeroVector(VT, DAG);
2943 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2944 if (Values.size() == 1)
2947 // Special case for single non-zero element.
2948 if (NumNonZero == 1) {
2949 unsigned Idx = CountTrailingZeros_32(NonZeros);
2950 SDOperand Item = Op.getOperand(Idx);
2951 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2953 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2954 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2957 if (EVTBits == 32) {
2958 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2959 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2961 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2962 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2963 std::vector<SDOperand> MaskVec;
2964 for (unsigned i = 0; i < NumElems; i++)
2965 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2966 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2967 &MaskVec[0], MaskVec.size());
2968 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2969 DAG.getNode(ISD::UNDEF, VT), Mask);
2973 // Let legalizer expand 2-wide build_vector's.
2977 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2979 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2981 if (V.Val) return V;
2984 if (EVTBits == 16) {
2985 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2987 if (V.Val) return V;
2990 // If element VT is == 32 bits, turn it into a number of shuffles.
2991 std::vector<SDOperand> V(NumElems);
2992 if (NumElems == 4 && NumZero > 0) {
2993 for (unsigned i = 0; i < 4; ++i) {
2994 bool isZero = !(NonZeros & (1 << i));
2996 V[i] = getZeroVector(VT, DAG);
2998 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3001 for (unsigned i = 0; i < 2; ++i) {
3002 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3005 V[i] = V[i*2]; // Must be a zero vector.
3008 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3009 getMOVLMask(NumElems, DAG));
3012 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3013 getMOVLMask(NumElems, DAG));
3016 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3017 getUnpacklMask(NumElems, DAG));
3022 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3023 // clears the upper bits.
3024 // FIXME: we can do the same for v4f32 case when we know both parts of
3025 // the lower half come from scalar_to_vector (loadf32). We should do
3026 // that in post legalizer dag combiner with target specific hooks.
3027 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3029 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3030 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
3031 std::vector<SDOperand> MaskVec;
3032 bool Reverse = (NonZeros & 0x3) == 2;
3033 for (unsigned i = 0; i < 2; ++i)
3035 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3037 MaskVec.push_back(DAG.getConstant(i, EVT));
3038 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3039 for (unsigned i = 0; i < 2; ++i)
3041 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3043 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3044 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3045 &MaskVec[0], MaskVec.size());
3046 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3049 if (Values.size() > 2) {
3050 // Expand into a number of unpckl*.
3052 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3053 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3054 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3055 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3056 for (unsigned i = 0; i < NumElems; ++i)
3057 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3059 while (NumElems != 0) {
3060 for (unsigned i = 0; i < NumElems; ++i)
3061 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3072 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3073 SDOperand V1 = Op.getOperand(0);
3074 SDOperand V2 = Op.getOperand(1);
3075 SDOperand PermMask = Op.getOperand(2);
3076 MVT::ValueType VT = Op.getValueType();
3077 unsigned NumElems = PermMask.getNumOperands();
3078 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3079 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3080 bool V1IsSplat = false;
3081 bool V2IsSplat = false;
3083 if (isUndefShuffle(Op.Val))
3084 return DAG.getNode(ISD::UNDEF, VT);
3086 if (isSplatMask(PermMask.Val)) {
3087 if (NumElems <= 4) return Op;
3088 // Promote it to a v4i32 splat.
3089 return PromoteSplat(Op, DAG);
3092 if (X86::isMOVLMask(PermMask.Val))
3093 return (V1IsUndef) ? V2 : Op;
3095 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3096 X86::isMOVSLDUPMask(PermMask.Val) ||
3097 X86::isMOVHLPSMask(PermMask.Val) ||
3098 X86::isMOVHPMask(PermMask.Val) ||
3099 X86::isMOVLPMask(PermMask.Val))
3102 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3103 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3104 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3106 bool Commuted = false;
3107 V1IsSplat = isSplatVector(V1.Val);
3108 V2IsSplat = isSplatVector(V2.Val);
3109 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3110 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3111 std::swap(V1IsSplat, V2IsSplat);
3112 std::swap(V1IsUndef, V2IsUndef);
3116 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3117 if (V2IsUndef) return V1;
3118 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3120 // V2 is a splat, so the mask may be malformed. That is, it may point
3121 // to any V2 element. The instruction selectior won't like this. Get
3122 // a corrected mask and commute to form a proper MOVS{S|D}.
3123 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3124 if (NewMask.Val != PermMask.Val)
3125 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3130 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3131 X86::isUNPCKLMask(PermMask.Val) ||
3132 X86::isUNPCKHMask(PermMask.Val))
3136 // Normalize mask so all entries that point to V2 points to its first
3137 // element then try to match unpck{h|l} again. If match, return a
3138 // new vector_shuffle with the corrected mask.
3139 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3140 if (NewMask.Val != PermMask.Val) {
3141 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3142 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3143 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3144 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3145 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3146 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3151 // Normalize the node to match x86 shuffle ops if needed
3152 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3153 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3156 // Commute is back and try unpck* again.
3157 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3158 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3159 X86::isUNPCKLMask(PermMask.Val) ||
3160 X86::isUNPCKHMask(PermMask.Val))
3164 // If VT is integer, try PSHUF* first, then SHUFP*.
3165 if (MVT::isInteger(VT)) {
3166 if (X86::isPSHUFDMask(PermMask.Val) ||
3167 X86::isPSHUFHWMask(PermMask.Val) ||
3168 X86::isPSHUFLWMask(PermMask.Val)) {
3169 if (V2.getOpcode() != ISD::UNDEF)
3170 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3171 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3175 if (X86::isSHUFPMask(PermMask.Val))
3178 // Handle v8i16 shuffle high / low shuffle node pair.
3179 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3180 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3181 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3182 std::vector<SDOperand> MaskVec;
3183 for (unsigned i = 0; i != 4; ++i)
3184 MaskVec.push_back(PermMask.getOperand(i));
3185 for (unsigned i = 4; i != 8; ++i)
3186 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3187 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3188 &MaskVec[0], MaskVec.size());
3189 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3191 for (unsigned i = 0; i != 4; ++i)
3192 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3193 for (unsigned i = 4; i != 8; ++i)
3194 MaskVec.push_back(PermMask.getOperand(i));
3195 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
3196 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3199 // Floating point cases in the other order.
3200 if (X86::isSHUFPMask(PermMask.Val))
3202 if (X86::isPSHUFDMask(PermMask.Val) ||
3203 X86::isPSHUFHWMask(PermMask.Val) ||
3204 X86::isPSHUFLWMask(PermMask.Val)) {
3205 if (V2.getOpcode() != ISD::UNDEF)
3206 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3207 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3212 if (NumElems == 4) {
3213 MVT::ValueType MaskVT = PermMask.getValueType();
3214 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
3215 std::vector<std::pair<int, int> > Locs;
3216 Locs.reserve(NumElems);
3217 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3218 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3221 // If no more than two elements come from either vector. This can be
3222 // implemented with two shuffles. First shuffle gather the elements.
3223 // The second shuffle, which takes the first shuffle as both of its
3224 // vector operands, put the elements into the right order.
3225 for (unsigned i = 0; i != NumElems; ++i) {
3226 SDOperand Elt = PermMask.getOperand(i);
3227 if (Elt.getOpcode() == ISD::UNDEF) {
3228 Locs[i] = std::make_pair(-1, -1);
3230 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3231 if (Val < NumElems) {
3232 Locs[i] = std::make_pair(0, NumLo);
3236 Locs[i] = std::make_pair(1, NumHi);
3237 if (2+NumHi < NumElems)
3238 Mask1[2+NumHi] = Elt;
3243 if (NumLo <= 2 && NumHi <= 2) {
3244 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3245 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3246 &Mask1[0], Mask1.size()));
3247 for (unsigned i = 0; i != NumElems; ++i) {
3248 if (Locs[i].first == -1)
3251 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3252 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3253 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3257 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3258 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3259 &Mask2[0], Mask2.size()));
3262 // Break it into (shuffle shuffle_hi, shuffle_lo).
3264 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3265 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3266 std::vector<SDOperand> *MaskPtr = &LoMask;
3267 unsigned MaskIdx = 0;
3269 unsigned HiIdx = NumElems/2;
3270 for (unsigned i = 0; i != NumElems; ++i) {
3271 if (i == NumElems/2) {
3277 SDOperand Elt = PermMask.getOperand(i);
3278 if (Elt.getOpcode() == ISD::UNDEF) {
3279 Locs[i] = std::make_pair(-1, -1);
3280 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3281 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3282 (*MaskPtr)[LoIdx] = Elt;
3285 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3286 (*MaskPtr)[HiIdx] = Elt;
3291 SDOperand LoShuffle =
3292 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3293 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3294 &LoMask[0], LoMask.size()));
3295 SDOperand HiShuffle =
3296 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3297 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3298 &HiMask[0], HiMask.size()));
3299 std::vector<SDOperand> MaskOps;
3300 for (unsigned i = 0; i != NumElems; ++i) {
3301 if (Locs[i].first == -1) {
3302 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3304 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3305 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3308 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3309 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3310 &MaskOps[0], MaskOps.size()));
3317 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3318 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3321 MVT::ValueType VT = Op.getValueType();
3322 // TODO: handle v16i8.
3323 if (MVT::getSizeInBits(VT) == 16) {
3324 // Transform it so it match pextrw which produces a 32-bit result.
3325 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3326 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3327 Op.getOperand(0), Op.getOperand(1));
3328 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3329 DAG.getValueType(VT));
3330 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3331 } else if (MVT::getSizeInBits(VT) == 32) {
3332 SDOperand Vec = Op.getOperand(0);
3333 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3336 // SHUFPS the element to the lowest double word, then movss.
3337 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3338 std::vector<SDOperand> IdxVec;
3339 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
3340 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3341 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3342 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3343 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3344 &IdxVec[0], IdxVec.size());
3345 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3346 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3347 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3348 DAG.getConstant(0, getPointerTy()));
3349 } else if (MVT::getSizeInBits(VT) == 64) {
3350 SDOperand Vec = Op.getOperand(0);
3351 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3355 // UNPCKHPD the element to the lowest double word, then movsd.
3356 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3357 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3358 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3359 std::vector<SDOperand> IdxVec;
3360 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
3361 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3362 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3363 &IdxVec[0], IdxVec.size());
3364 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3365 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3366 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3367 DAG.getConstant(0, getPointerTy()));
3374 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3375 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3376 // as its second argument.
3377 MVT::ValueType VT = Op.getValueType();
3378 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
3379 SDOperand N0 = Op.getOperand(0);
3380 SDOperand N1 = Op.getOperand(1);
3381 SDOperand N2 = Op.getOperand(2);
3382 if (MVT::getSizeInBits(BaseVT) == 16) {
3383 if (N1.getValueType() != MVT::i32)
3384 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3385 if (N2.getValueType() != MVT::i32)
3386 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
3387 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3388 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3389 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3392 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3393 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3394 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3395 std::vector<SDOperand> MaskVec;
3396 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3397 for (unsigned i = 1; i <= 3; ++i)
3398 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3399 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3400 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3401 &MaskVec[0], MaskVec.size()));
3403 // Use two pinsrw instructions to insert a 32 bit value.
3405 if (MVT::isFloatingPoint(N1.getValueType())) {
3406 if (ISD::isNON_EXTLoad(N1.Val)) {
3407 // Just load directly from f32mem to GR32.
3408 LoadSDNode *LD = cast<LoadSDNode>(N1);
3409 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3410 LD->getSrcValue(), LD->getSrcValueOffset());
3412 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3413 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3414 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3415 DAG.getConstant(0, getPointerTy()));
3418 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3419 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3420 DAG.getConstant(Idx, getPointerTy()));
3421 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3422 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3423 DAG.getConstant(Idx+1, getPointerTy()));
3424 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3432 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3433 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3434 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3437 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3438 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3439 // one of the above mentioned nodes. It has to be wrapped because otherwise
3440 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3441 // be used to form addressing mode. These wrapped nodes will be selected
3444 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3445 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3446 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3448 CP->getAlignment());
3449 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3450 // With PIC, the address is actually $g + Offset.
3451 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3452 !Subtarget->isPICStyleRIPRel()) {
3453 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3454 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3462 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3463 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3464 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3465 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3466 // With PIC, the address is actually $g + Offset.
3467 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3468 !Subtarget->isPICStyleRIPRel()) {
3469 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3470 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3474 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3475 // load the value at address GV, not the value of GV itself. This means that
3476 // the GlobalAddress must be in the base or index register of the address, not
3477 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3478 // The same applies for external symbols during PIC codegen
3479 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3480 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3486 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3487 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3488 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3489 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3490 // With PIC, the address is actually $g + Offset.
3491 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3492 !Subtarget->isPICStyleRIPRel()) {
3493 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3494 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3501 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3502 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3503 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3504 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3505 // With PIC, the address is actually $g + Offset.
3506 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3507 !Subtarget->isPICStyleRIPRel()) {
3508 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3509 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3516 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3517 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3518 "Not an i64 shift!");
3519 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3520 SDOperand ShOpLo = Op.getOperand(0);
3521 SDOperand ShOpHi = Op.getOperand(1);
3522 SDOperand ShAmt = Op.getOperand(2);
3523 SDOperand Tmp1 = isSRA ?
3524 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3525 DAG.getConstant(0, MVT::i32);
3527 SDOperand Tmp2, Tmp3;
3528 if (Op.getOpcode() == ISD::SHL_PARTS) {
3529 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3530 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3532 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3533 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3536 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3537 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3538 DAG.getConstant(32, MVT::i8));
3539 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3540 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3543 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3545 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3546 SmallVector<SDOperand, 4> Ops;
3547 if (Op.getOpcode() == ISD::SHL_PARTS) {
3548 Ops.push_back(Tmp2);
3549 Ops.push_back(Tmp3);
3551 Ops.push_back(InFlag);
3552 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3553 InFlag = Hi.getValue(1);
3556 Ops.push_back(Tmp3);
3557 Ops.push_back(Tmp1);
3559 Ops.push_back(InFlag);
3560 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3562 Ops.push_back(Tmp2);
3563 Ops.push_back(Tmp3);
3565 Ops.push_back(InFlag);
3566 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3567 InFlag = Lo.getValue(1);
3570 Ops.push_back(Tmp3);
3571 Ops.push_back(Tmp1);
3573 Ops.push_back(InFlag);
3574 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3577 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3581 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3584 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3585 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3586 Op.getOperand(0).getValueType() >= MVT::i16 &&
3587 "Unknown SINT_TO_FP to lower!");
3590 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3591 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3592 MachineFunction &MF = DAG.getMachineFunction();
3593 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3594 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3595 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3596 StackSlot, NULL, 0);
3599 std::vector<MVT::ValueType> Tys;
3600 Tys.push_back(MVT::f64);
3601 Tys.push_back(MVT::Other);
3602 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3603 std::vector<SDOperand> Ops;
3604 Ops.push_back(Chain);
3605 Ops.push_back(StackSlot);
3606 Ops.push_back(DAG.getValueType(SrcVT));
3607 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3608 Tys, &Ops[0], Ops.size());
3611 Chain = Result.getValue(1);
3612 SDOperand InFlag = Result.getValue(2);
3614 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3615 // shouldn't be necessary except that RFP cannot be live across
3616 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3617 MachineFunction &MF = DAG.getMachineFunction();
3618 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3619 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3620 std::vector<MVT::ValueType> Tys;
3621 Tys.push_back(MVT::Other);
3622 std::vector<SDOperand> Ops;
3623 Ops.push_back(Chain);
3624 Ops.push_back(Result);
3625 Ops.push_back(StackSlot);
3626 Ops.push_back(DAG.getValueType(Op.getValueType()));
3627 Ops.push_back(InFlag);
3628 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3629 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3635 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3636 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3637 "Unknown FP_TO_SINT to lower!");
3638 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3640 MachineFunction &MF = DAG.getMachineFunction();
3641 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3642 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3643 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3646 switch (Op.getValueType()) {
3647 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3648 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3649 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3650 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3653 SDOperand Chain = DAG.getEntryNode();
3654 SDOperand Value = Op.getOperand(0);
3656 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3657 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3658 std::vector<MVT::ValueType> Tys;
3659 Tys.push_back(MVT::f64);
3660 Tys.push_back(MVT::Other);
3661 std::vector<SDOperand> Ops;
3662 Ops.push_back(Chain);
3663 Ops.push_back(StackSlot);
3664 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3665 Value = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
3666 Chain = Value.getValue(1);
3667 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3668 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3671 // Build the FP_TO_INT*_IN_MEM
3672 std::vector<SDOperand> Ops;
3673 Ops.push_back(Chain);
3674 Ops.push_back(Value);
3675 Ops.push_back(StackSlot);
3676 SDOperand FIST = DAG.getNode(Opc, MVT::Other, &Ops[0], Ops.size());
3679 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3682 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3683 MVT::ValueType VT = Op.getValueType();
3684 const Type *OpNTy = MVT::getTypeForValueType(VT);
3685 std::vector<Constant*> CV;
3686 if (VT == MVT::f64) {
3687 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3688 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3690 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3691 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3692 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3693 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3695 Constant *CS = ConstantStruct::get(CV);
3696 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3697 std::vector<MVT::ValueType> Tys;
3699 Tys.push_back(MVT::Other);
3700 SmallVector<SDOperand, 3> Ops;
3701 Ops.push_back(DAG.getEntryNode());
3702 Ops.push_back(CPIdx);
3703 Ops.push_back(DAG.getSrcValue(NULL));
3704 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3705 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3708 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3709 MVT::ValueType VT = Op.getValueType();
3710 const Type *OpNTy = MVT::getTypeForValueType(VT);
3711 std::vector<Constant*> CV;
3712 if (VT == MVT::f64) {
3713 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3714 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3716 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3717 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3718 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3719 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3721 Constant *CS = ConstantStruct::get(CV);
3722 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3723 std::vector<MVT::ValueType> Tys;
3725 Tys.push_back(MVT::Other);
3726 SmallVector<SDOperand, 3> Ops;
3727 Ops.push_back(DAG.getEntryNode());
3728 Ops.push_back(CPIdx);
3729 Ops.push_back(DAG.getSrcValue(NULL));
3730 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3731 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3734 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3735 SDOperand Op0 = Op.getOperand(0);
3736 SDOperand Op1 = Op.getOperand(1);
3737 MVT::ValueType VT = Op.getValueType();
3738 MVT::ValueType SrcVT = Op1.getValueType();
3739 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3741 // If second operand is smaller, extend it first.
3742 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3743 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3747 // First get the sign bit of second operand.
3748 std::vector<Constant*> CV;
3749 if (SrcVT == MVT::f64) {
3750 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3751 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3753 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3754 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3755 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3756 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3758 Constant *CS = ConstantStruct::get(CV);
3759 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3760 std::vector<MVT::ValueType> Tys;
3761 Tys.push_back(SrcVT);
3762 Tys.push_back(MVT::Other);
3763 SmallVector<SDOperand, 3> Ops;
3764 Ops.push_back(DAG.getEntryNode());
3765 Ops.push_back(CPIdx);
3766 Ops.push_back(DAG.getSrcValue(NULL));
3767 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3768 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3770 // Shift sign bit right or left if the two operands have different types.
3771 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3772 // Op0 is MVT::f32, Op1 is MVT::f64.
3773 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3774 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3775 DAG.getConstant(32, MVT::i32));
3776 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3777 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3778 DAG.getConstant(0, getPointerTy()));
3781 // Clear first operand sign bit.
3783 if (VT == MVT::f64) {
3784 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3785 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3787 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3788 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3789 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3790 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3792 CS = ConstantStruct::get(CV);
3793 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3796 Tys.push_back(MVT::Other);
3798 Ops.push_back(DAG.getEntryNode());
3799 Ops.push_back(CPIdx);
3800 Ops.push_back(DAG.getSrcValue(NULL));
3801 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3802 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3804 // Or the value with the sign bit.
3805 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3808 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3810 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3812 SDOperand Op0 = Op.getOperand(0);
3813 SDOperand Op1 = Op.getOperand(1);
3814 SDOperand CC = Op.getOperand(2);
3815 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3816 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3817 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3818 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3821 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3823 SDOperand Ops1[] = { Chain, Op0, Op1 };
3824 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3825 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3826 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3829 assert(isFP && "Illegal integer SetCC!");
3831 SDOperand COps[] = { Chain, Op0, Op1 };
3832 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3834 switch (SetCCOpcode) {
3835 default: assert(false && "Illegal floating point SetCC!");
3836 case ISD::SETOEQ: { // !PF & ZF
3837 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3838 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3839 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3841 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3842 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3844 case ISD::SETUNE: { // PF | !ZF
3845 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3846 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3847 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3849 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3850 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3855 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3856 bool addTest = true;
3857 SDOperand Chain = DAG.getEntryNode();
3858 SDOperand Cond = Op.getOperand(0);
3860 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3862 if (Cond.getOpcode() == ISD::SETCC)
3863 Cond = LowerSETCC(Cond, DAG, Chain);
3865 if (Cond.getOpcode() == X86ISD::SETCC) {
3866 CC = Cond.getOperand(0);
3868 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3869 // (since flag operand cannot be shared). Use it as the condition setting
3870 // operand in place of the X86ISD::SETCC.
3871 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3872 // to use a test instead of duplicating the X86ISD::CMP (for register
3873 // pressure reason)?
3874 SDOperand Cmp = Cond.getOperand(1);
3875 unsigned Opc = Cmp.getOpcode();
3876 bool IllegalFPCMov = !X86ScalarSSE &&
3877 MVT::isFloatingPoint(Op.getValueType()) &&
3878 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3879 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3881 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3882 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3888 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3889 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3890 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3893 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3894 SmallVector<SDOperand, 4> Ops;
3895 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3896 // condition is true.
3897 Ops.push_back(Op.getOperand(2));
3898 Ops.push_back(Op.getOperand(1));
3900 Ops.push_back(Cond.getValue(1));
3901 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3904 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3905 bool addTest = true;
3906 SDOperand Chain = Op.getOperand(0);
3907 SDOperand Cond = Op.getOperand(1);
3908 SDOperand Dest = Op.getOperand(2);
3910 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3912 if (Cond.getOpcode() == ISD::SETCC)
3913 Cond = LowerSETCC(Cond, DAG, Chain);
3915 if (Cond.getOpcode() == X86ISD::SETCC) {
3916 CC = Cond.getOperand(0);
3918 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3919 // (since flag operand cannot be shared). Use it as the condition setting
3920 // operand in place of the X86ISD::SETCC.
3921 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3922 // to use a test instead of duplicating the X86ISD::CMP (for register
3923 // pressure reason)?
3924 SDOperand Cmp = Cond.getOperand(1);
3925 unsigned Opc = Cmp.getOpcode();
3926 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3927 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3928 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3934 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3935 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3936 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3938 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3939 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3942 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3943 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3945 if (Subtarget->is64Bit())
3946 return LowerX86_64CCCCallTo(Op, DAG);
3948 switch (CallingConv) {
3950 assert(0 && "Unsupported calling convention");
3951 case CallingConv::Fast:
3953 return LowerFastCCCallTo(Op, DAG);
3956 case CallingConv::C:
3957 return LowerCCCCallTo(Op, DAG);
3958 case CallingConv::X86_StdCall:
3959 return LowerCCCCallTo(Op, DAG, true);
3960 case CallingConv::X86_FastCall:
3961 return LowerFastCCCallTo(Op, DAG, true);
3965 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3968 switch(Op.getNumOperands()) {
3970 assert(0 && "Do not know how to return this many arguments!");
3972 case 1: // ret void.
3973 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
3974 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
3976 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
3978 if (MVT::isVector(ArgVT) ||
3979 (Subtarget->is64Bit() && MVT::isFloatingPoint(ArgVT))) {
3980 // Integer or FP vector result -> XMM0.
3981 if (DAG.getMachineFunction().liveout_empty())
3982 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3983 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3985 } else if (MVT::isInteger(ArgVT)) {
3986 // Integer result -> EAX / RAX.
3987 // The C calling convention guarantees the return value has been
3988 // promoted to at least MVT::i32. The X86-64 ABI doesn't require the
3989 // value to be promoted MVT::i64. So we don't have to extend it to
3990 // 64-bit. Return the value in EAX, but mark RAX as liveout.
3991 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
3992 if (DAG.getMachineFunction().liveout_empty())
3993 DAG.getMachineFunction().addLiveOut(Reg);
3995 Reg = (ArgVT == MVT::i64) ? X86::RAX : X86::EAX;
3996 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg, Op.getOperand(1),
3998 } else if (!X86ScalarSSE) {
3999 // FP return with fp-stack value.
4000 if (DAG.getMachineFunction().liveout_empty())
4001 DAG.getMachineFunction().addLiveOut(X86::ST0);
4003 std::vector<MVT::ValueType> Tys;
4004 Tys.push_back(MVT::Other);
4005 Tys.push_back(MVT::Flag);
4006 std::vector<SDOperand> Ops;
4007 Ops.push_back(Op.getOperand(0));
4008 Ops.push_back(Op.getOperand(1));
4009 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
4011 // FP return with ScalarSSE (return on fp-stack).
4012 if (DAG.getMachineFunction().liveout_empty())
4013 DAG.getMachineFunction().addLiveOut(X86::ST0);
4016 SDOperand Chain = Op.getOperand(0);
4017 SDOperand Value = Op.getOperand(1);
4019 if (ISD::isNON_EXTLoad(Value.Val) &&
4020 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
4021 Chain = Value.getOperand(0);
4022 MemLoc = Value.getOperand(1);
4024 // Spill the value to memory and reload it into top of stack.
4025 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
4026 MachineFunction &MF = DAG.getMachineFunction();
4027 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4028 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
4029 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
4031 std::vector<MVT::ValueType> Tys;
4032 Tys.push_back(MVT::f64);
4033 Tys.push_back(MVT::Other);
4034 std::vector<SDOperand> Ops;
4035 Ops.push_back(Chain);
4036 Ops.push_back(MemLoc);
4037 Ops.push_back(DAG.getValueType(ArgVT));
4038 Copy = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
4040 Tys.push_back(MVT::Other);
4041 Tys.push_back(MVT::Flag);
4043 Ops.push_back(Copy.getValue(1));
4044 Ops.push_back(Copy);
4045 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
4050 unsigned Reg1 = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
4051 unsigned Reg2 = Subtarget->is64Bit() ? X86::RDX : X86::EDX;
4052 if (DAG.getMachineFunction().liveout_empty()) {
4053 DAG.getMachineFunction().addLiveOut(Reg1);
4054 DAG.getMachineFunction().addLiveOut(Reg2);
4057 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg2, Op.getOperand(3),
4059 Copy = DAG.getCopyToReg(Copy, Reg1, Op.getOperand(1), Copy.getValue(1));
4063 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
4064 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
4069 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
4070 MachineFunction &MF = DAG.getMachineFunction();
4071 const Function* Fn = MF.getFunction();
4072 if (Fn->hasExternalLinkage() &&
4073 Subtarget->isTargetCygMing() &&
4074 Fn->getName() == "main")
4075 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
4077 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4078 if (Subtarget->is64Bit())
4079 return LowerX86_64CCCArguments(Op, DAG);
4083 assert(0 && "Unsupported calling convention");
4084 case CallingConv::Fast:
4086 return LowerFastCCArguments(Op, DAG);
4089 case CallingConv::C:
4090 return LowerCCCArguments(Op, DAG);
4091 case CallingConv::X86_StdCall:
4092 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
4093 return LowerCCCArguments(Op, DAG, true);
4094 case CallingConv::X86_FastCall:
4095 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
4096 return LowerFastCCArguments(Op, DAG, true);
4100 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4101 SDOperand InFlag(0, 0);
4102 SDOperand Chain = Op.getOperand(0);
4104 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4105 if (Align == 0) Align = 1;
4107 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4108 // If not DWORD aligned, call memset if size is less than the threshold.
4109 // It knows how to align to the right boundary first.
4110 if ((Align & 3) != 0 ||
4111 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4112 MVT::ValueType IntPtr = getPointerTy();
4113 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4114 TargetLowering::ArgListTy Args;
4115 TargetLowering::ArgListEntry Entry;
4116 Entry.Node = Op.getOperand(1);
4117 Entry.Ty = IntPtrTy;
4118 Entry.isSigned = false;
4119 Entry.isInReg = false;
4120 Entry.isSRet = false;
4121 Args.push_back(Entry);
4122 // Extend the unsigned i8 argument to be an int value for the call.
4123 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4124 Entry.Ty = IntPtrTy;
4125 Entry.isSigned = false;
4126 Entry.isInReg = false;
4127 Entry.isSRet = false;
4128 Args.push_back(Entry);
4129 Entry.Node = Op.getOperand(3);
4130 Args.push_back(Entry);
4131 std::pair<SDOperand,SDOperand> CallResult =
4132 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4133 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4134 return CallResult.second;
4139 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4140 unsigned BytesLeft = 0;
4141 bool TwoRepStos = false;
4144 uint64_t Val = ValC->getValue() & 255;
4146 // If the value is a constant, then we can potentially use larger sets.
4147 switch (Align & 3) {
4148 case 2: // WORD aligned
4151 Val = (Val << 8) | Val;
4153 case 0: // DWORD aligned
4156 Val = (Val << 8) | Val;
4157 Val = (Val << 16) | Val;
4158 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4161 Val = (Val << 32) | Val;
4164 default: // Byte aligned
4167 Count = Op.getOperand(3);
4171 if (AVT > MVT::i8) {
4173 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4174 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4175 BytesLeft = I->getValue() % UBytes;
4177 assert(AVT >= MVT::i32 &&
4178 "Do not use rep;stos if not at least DWORD aligned");
4179 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4180 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4185 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4187 InFlag = Chain.getValue(1);
4190 Count = Op.getOperand(3);
4191 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4192 InFlag = Chain.getValue(1);
4195 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4197 InFlag = Chain.getValue(1);
4198 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4199 Op.getOperand(1), InFlag);
4200 InFlag = Chain.getValue(1);
4202 std::vector<MVT::ValueType> Tys;
4203 Tys.push_back(MVT::Other);
4204 Tys.push_back(MVT::Flag);
4205 std::vector<SDOperand> Ops;
4206 Ops.push_back(Chain);
4207 Ops.push_back(DAG.getValueType(AVT));
4208 Ops.push_back(InFlag);
4209 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4212 InFlag = Chain.getValue(1);
4213 Count = Op.getOperand(3);
4214 MVT::ValueType CVT = Count.getValueType();
4215 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4216 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4217 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4219 InFlag = Chain.getValue(1);
4221 Tys.push_back(MVT::Other);
4222 Tys.push_back(MVT::Flag);
4224 Ops.push_back(Chain);
4225 Ops.push_back(DAG.getValueType(MVT::i8));
4226 Ops.push_back(InFlag);
4227 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4228 } else if (BytesLeft) {
4229 // Issue stores for the last 1 - 7 bytes.
4231 unsigned Val = ValC->getValue() & 255;
4232 unsigned Offset = I->getValue() - BytesLeft;
4233 SDOperand DstAddr = Op.getOperand(1);
4234 MVT::ValueType AddrVT = DstAddr.getValueType();
4235 if (BytesLeft >= 4) {
4236 Val = (Val << 8) | Val;
4237 Val = (Val << 16) | Val;
4238 Value = DAG.getConstant(Val, MVT::i32);
4239 Chain = DAG.getStore(Chain, Value,
4240 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4241 DAG.getConstant(Offset, AddrVT)),
4246 if (BytesLeft >= 2) {
4247 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4248 Chain = DAG.getStore(Chain, Value,
4249 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4250 DAG.getConstant(Offset, AddrVT)),
4255 if (BytesLeft == 1) {
4256 Value = DAG.getConstant(Val, MVT::i8);
4257 Chain = DAG.getStore(Chain, Value,
4258 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4259 DAG.getConstant(Offset, AddrVT)),
4267 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4268 SDOperand Chain = Op.getOperand(0);
4270 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4271 if (Align == 0) Align = 1;
4273 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4274 // If not DWORD aligned, call memcpy if size is less than the threshold.
4275 // It knows how to align to the right boundary first.
4276 if ((Align & 3) != 0 ||
4277 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4278 MVT::ValueType IntPtr = getPointerTy();
4279 TargetLowering::ArgListTy Args;
4280 TargetLowering::ArgListEntry Entry;
4281 Entry.Ty = getTargetData()->getIntPtrType();
4282 Entry.isSigned = false;
4283 Entry.isInReg = false;
4284 Entry.isSRet = false;
4285 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
4286 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
4287 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
4288 std::pair<SDOperand,SDOperand> CallResult =
4289 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4290 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4291 return CallResult.second;
4296 unsigned BytesLeft = 0;
4297 bool TwoRepMovs = false;
4298 switch (Align & 3) {
4299 case 2: // WORD aligned
4302 case 0: // DWORD aligned
4304 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4307 default: // Byte aligned
4309 Count = Op.getOperand(3);
4313 if (AVT > MVT::i8) {
4315 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4316 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4317 BytesLeft = I->getValue() % UBytes;
4319 assert(AVT >= MVT::i32 &&
4320 "Do not use rep;movs if not at least DWORD aligned");
4321 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4322 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4327 SDOperand InFlag(0, 0);
4328 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4330 InFlag = Chain.getValue(1);
4331 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4332 Op.getOperand(1), InFlag);
4333 InFlag = Chain.getValue(1);
4334 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4335 Op.getOperand(2), InFlag);
4336 InFlag = Chain.getValue(1);
4338 std::vector<MVT::ValueType> Tys;
4339 Tys.push_back(MVT::Other);
4340 Tys.push_back(MVT::Flag);
4341 std::vector<SDOperand> Ops;
4342 Ops.push_back(Chain);
4343 Ops.push_back(DAG.getValueType(AVT));
4344 Ops.push_back(InFlag);
4345 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4348 InFlag = Chain.getValue(1);
4349 Count = Op.getOperand(3);
4350 MVT::ValueType CVT = Count.getValueType();
4351 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4352 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4353 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4355 InFlag = Chain.getValue(1);
4357 Tys.push_back(MVT::Other);
4358 Tys.push_back(MVT::Flag);
4360 Ops.push_back(Chain);
4361 Ops.push_back(DAG.getValueType(MVT::i8));
4362 Ops.push_back(InFlag);
4363 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4364 } else if (BytesLeft) {
4365 // Issue loads and stores for the last 1 - 7 bytes.
4366 unsigned Offset = I->getValue() - BytesLeft;
4367 SDOperand DstAddr = Op.getOperand(1);
4368 MVT::ValueType DstVT = DstAddr.getValueType();
4369 SDOperand SrcAddr = Op.getOperand(2);
4370 MVT::ValueType SrcVT = SrcAddr.getValueType();
4372 if (BytesLeft >= 4) {
4373 Value = DAG.getLoad(MVT::i32, Chain,
4374 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4375 DAG.getConstant(Offset, SrcVT)),
4377 Chain = Value.getValue(1);
4378 Chain = DAG.getStore(Chain, Value,
4379 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4380 DAG.getConstant(Offset, DstVT)),
4385 if (BytesLeft >= 2) {
4386 Value = DAG.getLoad(MVT::i16, Chain,
4387 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4388 DAG.getConstant(Offset, SrcVT)),
4390 Chain = Value.getValue(1);
4391 Chain = DAG.getStore(Chain, Value,
4392 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4393 DAG.getConstant(Offset, DstVT)),
4399 if (BytesLeft == 1) {
4400 Value = DAG.getLoad(MVT::i8, Chain,
4401 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4402 DAG.getConstant(Offset, SrcVT)),
4404 Chain = Value.getValue(1);
4405 Chain = DAG.getStore(Chain, Value,
4406 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4407 DAG.getConstant(Offset, DstVT)),
4416 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4417 std::vector<MVT::ValueType> Tys;
4418 Tys.push_back(MVT::Other);
4419 Tys.push_back(MVT::Flag);
4420 std::vector<SDOperand> Ops;
4421 Ops.push_back(Op.getOperand(0));
4422 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &Ops[0], Ops.size());
4424 if (Subtarget->is64Bit()) {
4425 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4426 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4427 MVT::i64, Copy1.getValue(2));
4428 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4429 DAG.getConstant(32, MVT::i8));
4430 Ops.push_back(DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp));
4431 Ops.push_back(Copy2.getValue(1));
4433 Tys[1] = MVT::Other;
4435 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4436 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4437 MVT::i32, Copy1.getValue(2));
4438 Ops.push_back(Copy1);
4439 Ops.push_back(Copy2);
4440 Ops.push_back(Copy2.getValue(1));
4441 Tys[0] = Tys[1] = MVT::i32;
4442 Tys.push_back(MVT::Other);
4444 return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size());
4447 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4448 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4450 if (!Subtarget->is64Bit()) {
4451 // vastart just stores the address of the VarArgsFrameIndex slot into the
4452 // memory location argument.
4453 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4454 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4459 // gp_offset (0 - 6 * 8)
4460 // fp_offset (48 - 48 + 8 * 16)
4461 // overflow_arg_area (point to parameters coming in memory).
4463 std::vector<SDOperand> MemOps;
4464 SDOperand FIN = Op.getOperand(1);
4466 SDOperand Store = DAG.getStore(Op.getOperand(0),
4467 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4468 FIN, SV->getValue(), SV->getOffset());
4469 MemOps.push_back(Store);
4472 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4473 DAG.getConstant(4, getPointerTy()));
4474 Store = DAG.getStore(Op.getOperand(0),
4475 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4476 FIN, SV->getValue(), SV->getOffset());
4477 MemOps.push_back(Store);
4479 // Store ptr to overflow_arg_area
4480 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4481 DAG.getConstant(4, getPointerTy()));
4482 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4483 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4485 MemOps.push_back(Store);
4487 // Store ptr to reg_save_area.
4488 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4489 DAG.getConstant(8, getPointerTy()));
4490 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4491 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4493 MemOps.push_back(Store);
4494 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4498 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4499 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4501 default: return SDOperand(); // Don't custom lower most intrinsics.
4502 // Comparison intrinsics.
4503 case Intrinsic::x86_sse_comieq_ss:
4504 case Intrinsic::x86_sse_comilt_ss:
4505 case Intrinsic::x86_sse_comile_ss:
4506 case Intrinsic::x86_sse_comigt_ss:
4507 case Intrinsic::x86_sse_comige_ss:
4508 case Intrinsic::x86_sse_comineq_ss:
4509 case Intrinsic::x86_sse_ucomieq_ss:
4510 case Intrinsic::x86_sse_ucomilt_ss:
4511 case Intrinsic::x86_sse_ucomile_ss:
4512 case Intrinsic::x86_sse_ucomigt_ss:
4513 case Intrinsic::x86_sse_ucomige_ss:
4514 case Intrinsic::x86_sse_ucomineq_ss:
4515 case Intrinsic::x86_sse2_comieq_sd:
4516 case Intrinsic::x86_sse2_comilt_sd:
4517 case Intrinsic::x86_sse2_comile_sd:
4518 case Intrinsic::x86_sse2_comigt_sd:
4519 case Intrinsic::x86_sse2_comige_sd:
4520 case Intrinsic::x86_sse2_comineq_sd:
4521 case Intrinsic::x86_sse2_ucomieq_sd:
4522 case Intrinsic::x86_sse2_ucomilt_sd:
4523 case Intrinsic::x86_sse2_ucomile_sd:
4524 case Intrinsic::x86_sse2_ucomigt_sd:
4525 case Intrinsic::x86_sse2_ucomige_sd:
4526 case Intrinsic::x86_sse2_ucomineq_sd: {
4528 ISD::CondCode CC = ISD::SETCC_INVALID;
4531 case Intrinsic::x86_sse_comieq_ss:
4532 case Intrinsic::x86_sse2_comieq_sd:
4536 case Intrinsic::x86_sse_comilt_ss:
4537 case Intrinsic::x86_sse2_comilt_sd:
4541 case Intrinsic::x86_sse_comile_ss:
4542 case Intrinsic::x86_sse2_comile_sd:
4546 case Intrinsic::x86_sse_comigt_ss:
4547 case Intrinsic::x86_sse2_comigt_sd:
4551 case Intrinsic::x86_sse_comige_ss:
4552 case Intrinsic::x86_sse2_comige_sd:
4556 case Intrinsic::x86_sse_comineq_ss:
4557 case Intrinsic::x86_sse2_comineq_sd:
4561 case Intrinsic::x86_sse_ucomieq_ss:
4562 case Intrinsic::x86_sse2_ucomieq_sd:
4563 Opc = X86ISD::UCOMI;
4566 case Intrinsic::x86_sse_ucomilt_ss:
4567 case Intrinsic::x86_sse2_ucomilt_sd:
4568 Opc = X86ISD::UCOMI;
4571 case Intrinsic::x86_sse_ucomile_ss:
4572 case Intrinsic::x86_sse2_ucomile_sd:
4573 Opc = X86ISD::UCOMI;
4576 case Intrinsic::x86_sse_ucomigt_ss:
4577 case Intrinsic::x86_sse2_ucomigt_sd:
4578 Opc = X86ISD::UCOMI;
4581 case Intrinsic::x86_sse_ucomige_ss:
4582 case Intrinsic::x86_sse2_ucomige_sd:
4583 Opc = X86ISD::UCOMI;
4586 case Intrinsic::x86_sse_ucomineq_ss:
4587 case Intrinsic::x86_sse2_ucomineq_sd:
4588 Opc = X86ISD::UCOMI;
4594 SDOperand LHS = Op.getOperand(1);
4595 SDOperand RHS = Op.getOperand(2);
4596 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4598 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4599 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4600 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4601 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4602 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4603 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4604 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4609 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4610 // Depths > 0 not supported yet!
4611 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4614 // Just load the return address
4615 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4616 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4619 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4620 // Depths > 0 not supported yet!
4621 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4624 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4625 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4626 DAG.getConstant(4, getPointerTy()));
4629 /// LowerOperation - Provide custom lowering hooks for some operations.
4631 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4632 switch (Op.getOpcode()) {
4633 default: assert(0 && "Should not custom lower this!");
4634 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4635 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4636 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4637 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4638 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4639 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4640 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4641 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4642 case ISD::SHL_PARTS:
4643 case ISD::SRA_PARTS:
4644 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4645 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4646 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4647 case ISD::FABS: return LowerFABS(Op, DAG);
4648 case ISD::FNEG: return LowerFNEG(Op, DAG);
4649 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
4650 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
4651 case ISD::SELECT: return LowerSELECT(Op, DAG);
4652 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4653 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4654 case ISD::CALL: return LowerCALL(Op, DAG);
4655 case ISD::RET: return LowerRET(Op, DAG);
4656 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4657 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4658 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4659 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4660 case ISD::VASTART: return LowerVASTART(Op, DAG);
4661 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4662 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4663 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4667 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4669 default: return NULL;
4670 case X86ISD::SHLD: return "X86ISD::SHLD";
4671 case X86ISD::SHRD: return "X86ISD::SHRD";
4672 case X86ISD::FAND: return "X86ISD::FAND";
4673 case X86ISD::FOR: return "X86ISD::FOR";
4674 case X86ISD::FXOR: return "X86ISD::FXOR";
4675 case X86ISD::FSRL: return "X86ISD::FSRL";
4676 case X86ISD::FILD: return "X86ISD::FILD";
4677 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4678 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4679 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4680 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4681 case X86ISD::FLD: return "X86ISD::FLD";
4682 case X86ISD::FST: return "X86ISD::FST";
4683 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4684 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4685 case X86ISD::CALL: return "X86ISD::CALL";
4686 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4687 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4688 case X86ISD::CMP: return "X86ISD::CMP";
4689 case X86ISD::COMI: return "X86ISD::COMI";
4690 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4691 case X86ISD::SETCC: return "X86ISD::SETCC";
4692 case X86ISD::CMOV: return "X86ISD::CMOV";
4693 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4694 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4695 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4696 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4697 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
4698 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
4699 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4700 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4701 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4702 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4703 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4704 case X86ISD::FMAX: return "X86ISD::FMAX";
4705 case X86ISD::FMIN: return "X86ISD::FMIN";
4709 /// isLegalAddressImmediate - Return true if the integer value or
4710 /// GlobalValue can be used as the offset of the target addressing mode.
4711 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
4712 // X86 allows a sign-extended 32-bit immediate field.
4713 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4716 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
4717 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement
4718 // field unless we are in small code model.
4719 if (Subtarget->is64Bit() &&
4720 getTargetMachine().getCodeModel() != CodeModel::Small)
4723 return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false));
4726 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4727 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4728 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4729 /// are assumed to be legal.
4731 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4732 // Only do shuffles on 128-bit vector types for now.
4733 if (MVT::getSizeInBits(VT) == 64) return false;
4734 return (Mask.Val->getNumOperands() <= 4 ||
4735 isSplatMask(Mask.Val) ||
4736 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4737 X86::isUNPCKLMask(Mask.Val) ||
4738 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4739 X86::isUNPCKHMask(Mask.Val));
4742 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4744 SelectionDAG &DAG) const {
4745 unsigned NumElts = BVOps.size();
4746 // Only do shuffles on 128-bit vector types for now.
4747 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4748 if (NumElts == 2) return true;
4750 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
4751 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
4756 //===----------------------------------------------------------------------===//
4757 // X86 Scheduler Hooks
4758 //===----------------------------------------------------------------------===//
4761 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4762 MachineBasicBlock *BB) {
4763 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4764 switch (MI->getOpcode()) {
4765 default: assert(false && "Unexpected instr type to insert");
4766 case X86::CMOV_FR32:
4767 case X86::CMOV_FR64:
4768 case X86::CMOV_V4F32:
4769 case X86::CMOV_V2F64:
4770 case X86::CMOV_V2I64: {
4771 // To "insert" a SELECT_CC instruction, we actually have to insert the
4772 // diamond control-flow pattern. The incoming instruction knows the
4773 // destination vreg to set, the condition code register to branch on, the
4774 // true/false values to select between, and a branch opcode to use.
4775 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4776 ilist<MachineBasicBlock>::iterator It = BB;
4782 // cmpTY ccX, r1, r2
4784 // fallthrough --> copy0MBB
4785 MachineBasicBlock *thisMBB = BB;
4786 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4787 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4789 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4790 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4791 MachineFunction *F = BB->getParent();
4792 F->getBasicBlockList().insert(It, copy0MBB);
4793 F->getBasicBlockList().insert(It, sinkMBB);
4794 // Update machine-CFG edges by first adding all successors of the current
4795 // block to the new block which will contain the Phi node for the select.
4796 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4797 e = BB->succ_end(); i != e; ++i)
4798 sinkMBB->addSuccessor(*i);
4799 // Next, remove all successors of the current block, and add the true
4800 // and fallthrough blocks as its successors.
4801 while(!BB->succ_empty())
4802 BB->removeSuccessor(BB->succ_begin());
4803 BB->addSuccessor(copy0MBB);
4804 BB->addSuccessor(sinkMBB);
4807 // %FalseValue = ...
4808 // # fallthrough to sinkMBB
4811 // Update machine-CFG edges
4812 BB->addSuccessor(sinkMBB);
4815 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4818 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4819 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4820 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4822 delete MI; // The pseudo instruction is gone now.
4826 case X86::FP_TO_INT16_IN_MEM:
4827 case X86::FP_TO_INT32_IN_MEM:
4828 case X86::FP_TO_INT64_IN_MEM: {
4829 // Change the floating point control register to use "round towards zero"
4830 // mode when truncating to an integer value.
4831 MachineFunction *F = BB->getParent();
4832 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4833 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4835 // Load the old value of the high byte of the control word...
4837 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4838 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4840 // Set the high part to be round to zero...
4841 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4844 // Reload the modified control word now...
4845 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4847 // Restore the memory image of control word to original value
4848 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4851 // Get the X86 opcode to use.
4853 switch (MI->getOpcode()) {
4854 default: assert(0 && "illegal opcode!");
4855 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4856 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4857 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4861 MachineOperand &Op = MI->getOperand(0);
4862 if (Op.isRegister()) {
4863 AM.BaseType = X86AddressMode::RegBase;
4864 AM.Base.Reg = Op.getReg();
4866 AM.BaseType = X86AddressMode::FrameIndexBase;
4867 AM.Base.FrameIndex = Op.getFrameIndex();
4869 Op = MI->getOperand(1);
4870 if (Op.isImmediate())
4871 AM.Scale = Op.getImm();
4872 Op = MI->getOperand(2);
4873 if (Op.isImmediate())
4874 AM.IndexReg = Op.getImm();
4875 Op = MI->getOperand(3);
4876 if (Op.isGlobalAddress()) {
4877 AM.GV = Op.getGlobal();
4879 AM.Disp = Op.getImm();
4881 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4882 .addReg(MI->getOperand(4).getReg());
4884 // Reload the original control word now.
4885 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4887 delete MI; // The pseudo instruction is gone now.
4893 //===----------------------------------------------------------------------===//
4894 // X86 Optimization Hooks
4895 //===----------------------------------------------------------------------===//
4897 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4899 uint64_t &KnownZero,
4901 unsigned Depth) const {
4902 unsigned Opc = Op.getOpcode();
4903 assert((Opc >= ISD::BUILTIN_OP_END ||
4904 Opc == ISD::INTRINSIC_WO_CHAIN ||
4905 Opc == ISD::INTRINSIC_W_CHAIN ||
4906 Opc == ISD::INTRINSIC_VOID) &&
4907 "Should use MaskedValueIsZero if you don't know whether Op"
4908 " is a target node!");
4910 KnownZero = KnownOne = 0; // Don't know anything.
4914 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4919 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
4920 /// element of the result of the vector shuffle.
4921 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4922 MVT::ValueType VT = N->getValueType(0);
4923 SDOperand PermMask = N->getOperand(2);
4924 unsigned NumElems = PermMask.getNumOperands();
4925 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4927 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4929 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4930 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4931 SDOperand Idx = PermMask.getOperand(i);
4932 if (Idx.getOpcode() == ISD::UNDEF)
4933 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4934 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4939 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4940 /// node is a GlobalAddress + an offset.
4941 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4942 unsigned Opc = N->getOpcode();
4943 if (Opc == X86ISD::Wrapper) {
4944 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4945 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4948 } else if (Opc == ISD::ADD) {
4949 SDOperand N1 = N->getOperand(0);
4950 SDOperand N2 = N->getOperand(1);
4951 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4952 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4954 Offset += V->getSignExtended();
4957 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4958 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4960 Offset += V->getSignExtended();
4968 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4970 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4971 MachineFrameInfo *MFI) {
4972 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4975 SDOperand Loc = N->getOperand(1);
4976 SDOperand BaseLoc = Base->getOperand(1);
4977 if (Loc.getOpcode() == ISD::FrameIndex) {
4978 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4980 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4981 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4982 int FS = MFI->getObjectSize(FI);
4983 int BFS = MFI->getObjectSize(BFI);
4984 if (FS != BFS || FS != Size) return false;
4985 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4987 GlobalValue *GV1 = NULL;
4988 GlobalValue *GV2 = NULL;
4989 int64_t Offset1 = 0;
4990 int64_t Offset2 = 0;
4991 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4992 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4993 if (isGA1 && isGA2 && GV1 == GV2)
4994 return Offset1 == (Offset2 + Dist*Size);
5000 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5001 const X86Subtarget *Subtarget) {
5004 if (isGAPlusOffset(Base, GV, Offset))
5005 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5007 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
5008 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
5010 // Fixed objects do not specify alignment, however the offsets are known.
5011 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5012 (MFI->getObjectOffset(BFI) % 16) == 0);
5014 return MFI->getObjectAlignment(BFI) >= 16;
5020 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5021 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5022 /// if the load addresses are consecutive, non-overlapping, and in the right
5024 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5025 const X86Subtarget *Subtarget) {
5026 MachineFunction &MF = DAG.getMachineFunction();
5027 MachineFrameInfo *MFI = MF.getFrameInfo();
5028 MVT::ValueType VT = N->getValueType(0);
5029 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
5030 SDOperand PermMask = N->getOperand(2);
5031 int NumElems = (int)PermMask.getNumOperands();
5032 SDNode *Base = NULL;
5033 for (int i = 0; i < NumElems; ++i) {
5034 SDOperand Idx = PermMask.getOperand(i);
5035 if (Idx.getOpcode() == ISD::UNDEF) {
5036 if (!Base) return SDOperand();
5039 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5040 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5044 else if (!isConsecutiveLoad(Arg.Val, Base,
5045 i, MVT::getSizeInBits(EVT)/8,MFI))
5050 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
5052 LoadSDNode *LD = cast<LoadSDNode>(Base);
5053 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5054 LD->getSrcValueOffset());
5056 // Just use movups, it's shorter.
5057 std::vector<MVT::ValueType> Tys;
5058 Tys.push_back(MVT::v4f32);
5059 Tys.push_back(MVT::Other);
5060 SmallVector<SDOperand, 3> Ops;
5061 Ops.push_back(Base->getOperand(0));
5062 Ops.push_back(Base->getOperand(1));
5063 Ops.push_back(Base->getOperand(2));
5064 return DAG.getNode(ISD::BIT_CONVERT, VT,
5065 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
5069 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5070 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5071 const X86Subtarget *Subtarget) {
5072 SDOperand Cond = N->getOperand(0);
5074 // If we have SSE[12] support, try to form min/max nodes.
5075 if (Subtarget->hasSSE2() &&
5076 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5077 if (Cond.getOpcode() == ISD::SETCC) {
5078 // Get the LHS/RHS of the select.
5079 SDOperand LHS = N->getOperand(1);
5080 SDOperand RHS = N->getOperand(2);
5081 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5083 unsigned Opcode = 0;
5084 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5087 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5090 if (!UnsafeFPMath) break;
5092 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5094 Opcode = X86ISD::FMIN;
5097 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5100 if (!UnsafeFPMath) break;
5102 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5104 Opcode = X86ISD::FMAX;
5107 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5110 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5113 if (!UnsafeFPMath) break;
5115 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5117 Opcode = X86ISD::FMIN;
5120 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5123 if (!UnsafeFPMath) break;
5125 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5127 Opcode = X86ISD::FMAX;
5133 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5142 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5143 DAGCombinerInfo &DCI) const {
5144 SelectionDAG &DAG = DCI.DAG;
5145 switch (N->getOpcode()) {
5147 case ISD::VECTOR_SHUFFLE:
5148 return PerformShuffleCombine(N, DAG, Subtarget);
5150 return PerformSELECTCombine(N, DAG, Subtarget);
5156 //===----------------------------------------------------------------------===//
5157 // X86 Inline Assembly Support
5158 //===----------------------------------------------------------------------===//
5160 /// getConstraintType - Given a constraint letter, return the type of
5161 /// constraint it is for this target.
5162 X86TargetLowering::ConstraintType
5163 X86TargetLowering::getConstraintType(char ConstraintLetter) const {
5164 switch (ConstraintLetter) {
5173 return C_RegisterClass;
5174 default: return TargetLowering::getConstraintType(ConstraintLetter);
5178 /// isOperandValidForConstraint - Return the specified operand (possibly
5179 /// modified) if the specified SDOperand is valid for the specified target
5180 /// constraint letter, otherwise return null.
5181 SDOperand X86TargetLowering::
5182 isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
5183 switch (Constraint) {
5186 // Literal immediates are always ok.
5187 if (isa<ConstantSDNode>(Op)) return Op;
5189 // If we are in non-pic codegen mode, we allow the address of a global to
5190 // be used with 'i'.
5191 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
5192 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
5193 return SDOperand(0, 0);
5195 if (GA->getOpcode() != ISD::TargetGlobalAddress)
5196 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5201 // Otherwise, not valid for this mode.
5202 return SDOperand(0, 0);
5204 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
5208 std::vector<unsigned> X86TargetLowering::
5209 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5210 MVT::ValueType VT) const {
5211 if (Constraint.size() == 1) {
5212 // FIXME: not handling fp-stack yet!
5213 // FIXME: not handling MMX registers yet ('y' constraint).
5214 switch (Constraint[0]) { // GCC X86 Constraint Letters
5215 default: break; // Unknown constraint letter
5216 case 'A': // EAX/EDX
5217 if (VT == MVT::i32 || VT == MVT::i64)
5218 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5220 case 'r': // GENERAL_REGS
5221 case 'R': // LEGACY_REGS
5222 if (VT == MVT::i64 && Subtarget->is64Bit())
5223 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
5224 X86::RSI, X86::RDI, X86::RBP, X86::RSP,
5225 X86::R8, X86::R9, X86::R10, X86::R11,
5226 X86::R12, X86::R13, X86::R14, X86::R15, 0);
5228 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5229 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
5230 else if (VT == MVT::i16)
5231 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
5232 X86::SI, X86::DI, X86::BP, X86::SP, 0);
5233 else if (VT == MVT::i8)
5234 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
5236 case 'l': // INDEX_REGS
5238 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5239 X86::ESI, X86::EDI, X86::EBP, 0);
5240 else if (VT == MVT::i16)
5241 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
5242 X86::SI, X86::DI, X86::BP, 0);
5243 else if (VT == MVT::i8)
5244 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5246 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5249 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5250 else if (VT == MVT::i16)
5251 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5252 else if (VT == MVT::i8)
5253 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5255 case 'x': // SSE_REGS if SSE1 allowed
5256 if (Subtarget->hasSSE1())
5257 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5258 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5260 return std::vector<unsigned>();
5261 case 'Y': // SSE_REGS if SSE2 allowed
5262 if (Subtarget->hasSSE2())
5263 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5264 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5266 return std::vector<unsigned>();
5270 return std::vector<unsigned>();
5273 std::pair<unsigned, const TargetRegisterClass*>
5274 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5275 MVT::ValueType VT) const {
5276 // Use the default implementation in TargetLowering to convert the register
5277 // constraint into a member of a register class.
5278 std::pair<unsigned, const TargetRegisterClass*> Res;
5279 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5281 // Not found as a standard register?
5282 if (Res.second == 0) {
5283 // GCC calls "st(0)" just plain "st".
5284 if (StringsEqualNoCase("{st}", Constraint)) {
5285 Res.first = X86::ST0;
5286 Res.second = X86::RSTRegisterClass;
5292 // Otherwise, check to see if this is a register class of the wrong value
5293 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5294 // turn into {ax},{dx}.
5295 if (Res.second->hasType(VT))
5296 return Res; // Correct type already, nothing to do.
5298 // All of the single-register GCC register classes map their values onto
5299 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5300 // really want an 8-bit or 32-bit register, map to the appropriate register
5301 // class and return the appropriate register.
5302 if (Res.second != X86::GR16RegisterClass)
5305 if (VT == MVT::i8) {
5306 unsigned DestReg = 0;
5307 switch (Res.first) {
5309 case X86::AX: DestReg = X86::AL; break;
5310 case X86::DX: DestReg = X86::DL; break;
5311 case X86::CX: DestReg = X86::CL; break;
5312 case X86::BX: DestReg = X86::BL; break;
5315 Res.first = DestReg;
5316 Res.second = Res.second = X86::GR8RegisterClass;
5318 } else if (VT == MVT::i32) {
5319 unsigned DestReg = 0;
5320 switch (Res.first) {
5322 case X86::AX: DestReg = X86::EAX; break;
5323 case X86::DX: DestReg = X86::EDX; break;
5324 case X86::CX: DestReg = X86::ECX; break;
5325 case X86::BX: DestReg = X86::EBX; break;
5326 case X86::SI: DestReg = X86::ESI; break;
5327 case X86::DI: DestReg = X86::EDI; break;
5328 case X86::BP: DestReg = X86::EBP; break;
5329 case X86::SP: DestReg = X86::ESP; break;
5332 Res.first = DestReg;
5333 Res.second = Res.second = X86::GR32RegisterClass;
5335 } else if (VT == MVT::i64) {
5336 unsigned DestReg = 0;
5337 switch (Res.first) {
5339 case X86::AX: DestReg = X86::RAX; break;
5340 case X86::DX: DestReg = X86::RDX; break;
5341 case X86::CX: DestReg = X86::RCX; break;
5342 case X86::BX: DestReg = X86::RBX; break;
5343 case X86::SI: DestReg = X86::RSI; break;
5344 case X86::DI: DestReg = X86::RDI; break;
5345 case X86::BP: DestReg = X86::RBP; break;
5346 case X86::SP: DestReg = X86::RSP; break;
5349 Res.first = DestReg;
5350 Res.second = Res.second = X86::GR64RegisterClass;