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 Calling Convention implementation
428 //===----------------------------------------------------------------------===//
430 /// AddLiveIn - This helper function adds the specified physical register to the
431 /// MachineFunction as a live in value. It also creates a corresponding virtual
433 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
434 TargetRegisterClass *RC) {
435 assert(RC->contains(PReg) && "Not the correct regclass!");
436 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
437 MF.addLiveIn(PReg, VReg);
441 /// HowToPassCCCArgument - Returns how an formal argument of the specified type
442 /// should be passed. If it is through stack, returns the size of the stack
443 /// slot; if it is through XMM register, returns the number of XMM registers
446 HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
447 unsigned &ObjSize, unsigned &ObjXMMRegs) {
451 default: assert(0 && "Unhandled argument type!");
452 case MVT::i8: ObjSize = 1; break;
453 case MVT::i16: ObjSize = 2; break;
454 case MVT::i32: ObjSize = 4; break;
455 case MVT::i64: ObjSize = 8; break;
456 case MVT::f32: ObjSize = 4; break;
457 case MVT::f64: ObjSize = 8; break;
472 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
473 unsigned NumArgs = Op.Val->getNumValues() - 1;
474 MachineFunction &MF = DAG.getMachineFunction();
475 MachineFrameInfo *MFI = MF.getFrameInfo();
476 SDOperand Root = Op.getOperand(0);
477 std::vector<SDOperand> ArgValues;
479 // Add DAG nodes to load the arguments... On entry to a function on the X86,
480 // the stack frame looks like this:
482 // [ESP] -- return address
483 // [ESP + 4] -- first argument (leftmost lexically)
484 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
487 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
488 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
489 static const unsigned XMMArgRegs[] = {
490 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
492 for (unsigned i = 0; i < NumArgs; ++i) {
493 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
494 unsigned ArgIncrement = 4;
495 unsigned ObjSize = 0;
496 unsigned ObjXMMRegs = 0;
497 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
499 ArgIncrement = ObjSize;
503 // Passed in a XMM register.
504 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
505 X86::VR128RegisterClass);
506 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
507 ArgValues.push_back(ArgValue);
508 NumXMMRegs += ObjXMMRegs;
510 // XMM arguments have to be aligned on 16-byte boundary.
512 ArgOffset = ((ArgOffset + 15) / 16) * 16;
513 // Create the frame index object for this incoming parameter...
514 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
515 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
516 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
517 ArgValues.push_back(ArgValue);
518 ArgOffset += ArgIncrement; // Move on to the next argument...
522 ArgValues.push_back(Root);
524 // If the function takes variable number of arguments, make a frame index for
525 // the start of the first vararg value... for expansion of llvm.va_start.
526 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
528 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
529 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
530 ReturnAddrIndex = 0; // No return address slot generated yet.
531 BytesToPopOnReturn = 0; // Callee pops nothing.
532 BytesCallerReserves = ArgOffset;
534 // If this is a struct return on, the callee pops the hidden struct
535 // pointer. This is common for Darwin/X86, Linux & Mingw32 targets.
536 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet)
537 BytesToPopOnReturn = 4;
539 // Return the new list of results.
540 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
541 Op.Val->value_end());
542 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
546 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
547 SDOperand Chain = Op.getOperand(0);
548 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
549 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
550 SDOperand Callee = Op.getOperand(4);
551 MVT::ValueType RetVT= Op.Val->getValueType(0);
552 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
554 // Keep track of the number of XMM regs passed so far.
555 unsigned NumXMMRegs = 0;
556 static const unsigned XMMArgRegs[] = {
557 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
560 // Count how many bytes are to be pushed on the stack.
561 unsigned NumBytes = 0;
562 for (unsigned i = 0; i != NumOps; ++i) {
563 SDOperand Arg = Op.getOperand(5+2*i);
565 switch (Arg.getValueType()) {
566 default: assert(0 && "Unexpected ValueType for argument!");
586 // XMM arguments have to be aligned on 16-byte boundary.
587 NumBytes = ((NumBytes + 15) / 16) * 16;
594 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
596 // Arguments go on the stack in reverse order, as specified by the ABI.
597 unsigned ArgOffset = 0;
599 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
600 std::vector<SDOperand> MemOpChains;
601 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
602 for (unsigned i = 0; i != NumOps; ++i) {
603 SDOperand Arg = Op.getOperand(5+2*i);
605 switch (Arg.getValueType()) {
606 default: assert(0 && "Unexpected ValueType for argument!");
609 // Promote the integer to 32 bits. If the input type is signed use a
610 // sign extend, otherwise use a zero extend.
612 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
613 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
614 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
620 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
621 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
622 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
628 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
629 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
630 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
640 if (NumXMMRegs < 4) {
641 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
644 // XMM arguments have to be aligned on 16-byte boundary.
645 ArgOffset = ((ArgOffset + 15) / 16) * 16;
646 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
647 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
648 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
654 if (!MemOpChains.empty())
655 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
656 &MemOpChains[0], MemOpChains.size());
658 // Build a sequence of copy-to-reg nodes chained together with token chain
659 // and flag operands which copy the outgoing args into registers.
661 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
662 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
664 InFlag = Chain.getValue(1);
667 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
668 Subtarget->isPICStyleGOT()) {
669 Chain = DAG.getCopyToReg(Chain, X86::EBX,
670 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
672 InFlag = Chain.getValue(1);
675 // If the callee is a GlobalAddress node (quite common, every direct call is)
676 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
677 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
678 // We should use extra load for direct calls to dllimported functions in
680 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
681 getTargetMachine(), true))
682 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
683 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
684 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
686 std::vector<MVT::ValueType> NodeTys;
687 NodeTys.push_back(MVT::Other); // Returns a chain
688 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
689 std::vector<SDOperand> Ops;
690 Ops.push_back(Chain);
691 Ops.push_back(Callee);
693 // Add argument registers to the end of the list so that they are known live
695 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
696 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
697 RegsToPass[i].second.getValueType()));
700 Ops.push_back(InFlag);
702 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
703 NodeTys, &Ops[0], Ops.size());
704 InFlag = Chain.getValue(1);
706 // Create the CALLSEQ_END node.
707 unsigned NumBytesForCalleeToPush = 0;
709 // If this is is a call to a struct-return function, the callee
710 // pops the hidden struct pointer, so we have to push it back.
711 // This is common for Darwin/X86, Linux & Mingw32 targets.
712 if (CallingConv == CallingConv::CSRet)
713 NumBytesForCalleeToPush = 4;
716 NodeTys.push_back(MVT::Other); // Returns a chain
717 if (RetVT != MVT::Other)
718 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
720 Ops.push_back(Chain);
721 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
722 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
723 Ops.push_back(InFlag);
724 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
725 if (RetVT != MVT::Other)
726 InFlag = Chain.getValue(1);
728 std::vector<SDOperand> ResultVals;
731 default: assert(0 && "Unknown value type to return!");
732 case MVT::Other: break;
734 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
735 ResultVals.push_back(Chain.getValue(0));
736 NodeTys.push_back(MVT::i8);
739 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
740 ResultVals.push_back(Chain.getValue(0));
741 NodeTys.push_back(MVT::i16);
744 if (Op.Val->getValueType(1) == MVT::i32) {
745 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
746 ResultVals.push_back(Chain.getValue(0));
747 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
748 Chain.getValue(2)).getValue(1);
749 ResultVals.push_back(Chain.getValue(0));
750 NodeTys.push_back(MVT::i32);
752 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
753 ResultVals.push_back(Chain.getValue(0));
755 NodeTys.push_back(MVT::i32);
763 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
764 ResultVals.push_back(Chain.getValue(0));
765 NodeTys.push_back(RetVT);
769 std::vector<MVT::ValueType> Tys;
770 Tys.push_back(MVT::f64);
771 Tys.push_back(MVT::Other);
772 Tys.push_back(MVT::Flag);
773 std::vector<SDOperand> Ops;
774 Ops.push_back(Chain);
775 Ops.push_back(InFlag);
776 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
777 &Ops[0], Ops.size());
778 Chain = RetVal.getValue(1);
779 InFlag = RetVal.getValue(2);
781 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
782 // shouldn't be necessary except that RFP cannot be live across
783 // multiple blocks. When stackifier is fixed, they can be uncoupled.
784 MachineFunction &MF = DAG.getMachineFunction();
785 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
786 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
788 Tys.push_back(MVT::Other);
790 Ops.push_back(Chain);
791 Ops.push_back(RetVal);
792 Ops.push_back(StackSlot);
793 Ops.push_back(DAG.getValueType(RetVT));
794 Ops.push_back(InFlag);
795 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
796 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
797 Chain = RetVal.getValue(1);
800 if (RetVT == MVT::f32 && !X86ScalarSSE)
801 // FIXME: we would really like to remember that this FP_ROUND
802 // operation is okay to eliminate if we allow excess FP precision.
803 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
804 ResultVals.push_back(RetVal);
805 NodeTys.push_back(RetVT);
810 // If the function returns void, just return the chain.
811 if (ResultVals.empty())
814 // Otherwise, merge everything together with a MERGE_VALUES node.
815 NodeTys.push_back(MVT::Other);
816 ResultVals.push_back(Chain);
817 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
818 &ResultVals[0], ResultVals.size());
819 return Res.getValue(Op.ResNo);
823 //===----------------------------------------------------------------------===//
824 // X86-64 C Calling Convention implementation
825 //===----------------------------------------------------------------------===//
827 /// HowToPassX86_64CCCArgument - Returns how an formal argument of the specified
828 /// type should be passed. If it is through stack, returns the size of the stack
829 /// slot; if it is through integer or XMM register, returns the number of
830 /// integer or XMM registers are needed.
832 HowToPassX86_64CCCArgument(MVT::ValueType ObjectVT,
833 unsigned NumIntRegs, unsigned NumXMMRegs,
834 unsigned &ObjSize, unsigned &ObjIntRegs,
835 unsigned &ObjXMMRegs) {
841 default: assert(0 && "Unhandled argument type!");
851 case MVT::i8: ObjSize = 1; break;
852 case MVT::i16: ObjSize = 2; break;
853 case MVT::i32: ObjSize = 4; break;
854 case MVT::i64: ObjSize = 8; break;
871 case MVT::f32: ObjSize = 4; break;
872 case MVT::f64: ObjSize = 8; break;
878 case MVT::v2f64: ObjSize = 16; break;
886 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
887 unsigned NumArgs = Op.Val->getNumValues() - 1;
888 MachineFunction &MF = DAG.getMachineFunction();
889 MachineFrameInfo *MFI = MF.getFrameInfo();
890 SDOperand Root = Op.getOperand(0);
891 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
892 std::vector<SDOperand> ArgValues;
894 // Add DAG nodes to load the arguments... On entry to a function on the X86,
895 // the stack frame looks like this:
897 // [RSP] -- return address
898 // [RSP + 8] -- first nonreg argument (leftmost lexically)
899 // [RSP +16] -- second nonreg argument, if 1st argument is <= 8 bytes in size
902 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
903 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
904 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
906 static const unsigned GPR8ArgRegs[] = {
907 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
909 static const unsigned GPR16ArgRegs[] = {
910 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
912 static const unsigned GPR32ArgRegs[] = {
913 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
915 static const unsigned GPR64ArgRegs[] = {
916 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
918 static const unsigned XMMArgRegs[] = {
919 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
920 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
923 for (unsigned i = 0; i < NumArgs; ++i) {
924 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
925 unsigned ArgIncrement = 8;
926 unsigned ObjSize = 0;
927 unsigned ObjIntRegs = 0;
928 unsigned ObjXMMRegs = 0;
930 // FIXME: __int128 and long double support?
931 HowToPassX86_64CCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
932 ObjSize, ObjIntRegs, ObjXMMRegs);
934 ArgIncrement = ObjSize;
938 if (ObjIntRegs || ObjXMMRegs) {
940 default: assert(0 && "Unhandled argument type!");
945 TargetRegisterClass *RC = NULL;
949 RC = X86::GR8RegisterClass;
950 Reg = GPR8ArgRegs[NumIntRegs];
953 RC = X86::GR16RegisterClass;
954 Reg = GPR16ArgRegs[NumIntRegs];
957 RC = X86::GR32RegisterClass;
958 Reg = GPR32ArgRegs[NumIntRegs];
961 RC = X86::GR64RegisterClass;
962 Reg = GPR64ArgRegs[NumIntRegs];
965 Reg = AddLiveIn(MF, Reg, RC);
966 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
977 TargetRegisterClass *RC= (ObjectVT == MVT::f32) ?
978 X86::FR32RegisterClass : ((ObjectVT == MVT::f64) ?
979 X86::FR64RegisterClass : X86::VR128RegisterClass);
980 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], RC);
981 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
985 NumIntRegs += ObjIntRegs;
986 NumXMMRegs += ObjXMMRegs;
987 } else if (ObjSize) {
988 // XMM arguments have to be aligned on 16-byte boundary.
990 ArgOffset = ((ArgOffset + 15) / 16) * 16;
991 // Create the SelectionDAG nodes corresponding to a load from this
993 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
994 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
995 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
996 ArgOffset += ArgIncrement; // Move on to the next argument.
999 ArgValues.push_back(ArgValue);
1002 // If the function takes variable number of arguments, make a frame index for
1003 // the start of the first vararg value... for expansion of llvm.va_start.
1005 // For X86-64, if there are vararg parameters that are passed via
1006 // registers, then we must store them to their spots on the stack so they
1007 // may be loaded by deferencing the result of va_next.
1008 VarArgsGPOffset = NumIntRegs * 8;
1009 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1010 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
1011 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1013 // Store the integer parameter registers.
1014 std::vector<SDOperand> MemOps;
1015 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1016 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1017 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1018 for (; NumIntRegs != 6; ++NumIntRegs) {
1019 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1020 X86::GR64RegisterClass);
1021 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1022 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1023 MemOps.push_back(Store);
1024 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1025 DAG.getConstant(8, getPointerTy()));
1028 // Now store the XMM (fp + vector) parameter registers.
1029 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1030 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1031 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1032 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1033 X86::VR128RegisterClass);
1034 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1035 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1036 MemOps.push_back(Store);
1037 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1038 DAG.getConstant(16, getPointerTy()));
1040 if (!MemOps.empty())
1041 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1042 &MemOps[0], MemOps.size());
1045 ArgValues.push_back(Root);
1047 ReturnAddrIndex = 0; // No return address slot generated yet.
1048 BytesToPopOnReturn = 0; // Callee pops nothing.
1049 BytesCallerReserves = ArgOffset;
1051 // Return the new list of results.
1052 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1053 Op.Val->value_end());
1054 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1058 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG) {
1059 SDOperand Chain = Op.getOperand(0);
1060 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1061 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1062 SDOperand Callee = Op.getOperand(4);
1063 MVT::ValueType RetVT= Op.Val->getValueType(0);
1064 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1066 // Count how many bytes are to be pushed on the stack.
1067 unsigned NumBytes = 0;
1068 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
1069 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1071 static const unsigned GPR8ArgRegs[] = {
1072 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
1074 static const unsigned GPR16ArgRegs[] = {
1075 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
1077 static const unsigned GPR32ArgRegs[] = {
1078 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1080 static const unsigned GPR64ArgRegs[] = {
1081 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1083 static const unsigned XMMArgRegs[] = {
1084 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1085 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1088 for (unsigned i = 0; i != NumOps; ++i) {
1089 SDOperand Arg = Op.getOperand(5+2*i);
1090 MVT::ValueType ArgVT = Arg.getValueType();
1093 default: assert(0 && "Unknown value type!");
1113 else if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1116 // XMM arguments have to be aligned on 16-byte boundary.
1117 NumBytes = ((NumBytes + 15) / 16) * 16;
1124 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1126 // Arguments go on the stack in reverse order, as specified by the ABI.
1127 unsigned ArgOffset = 0;
1130 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1131 std::vector<SDOperand> MemOpChains;
1132 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1133 for (unsigned i = 0; i != NumOps; ++i) {
1134 SDOperand Arg = Op.getOperand(5+2*i);
1135 MVT::ValueType ArgVT = Arg.getValueType();
1138 default: assert(0 && "Unexpected ValueType for argument!");
1143 if (NumIntRegs < 6) {
1147 case MVT::i8: Reg = GPR8ArgRegs[NumIntRegs]; break;
1148 case MVT::i16: Reg = GPR16ArgRegs[NumIntRegs]; break;
1149 case MVT::i32: Reg = GPR32ArgRegs[NumIntRegs]; break;
1150 case MVT::i64: Reg = GPR64ArgRegs[NumIntRegs]; break;
1152 RegsToPass.push_back(std::make_pair(Reg, Arg));
1155 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1156 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1157 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1169 if (NumXMMRegs < 8) {
1170 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1173 if (ArgVT != MVT::f32 && ArgVT != MVT::f64) {
1174 // XMM arguments have to be aligned on 16-byte boundary.
1175 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1177 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1178 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1179 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1180 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1188 if (!MemOpChains.empty())
1189 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1190 &MemOpChains[0], MemOpChains.size());
1192 // Build a sequence of copy-to-reg nodes chained together with token chain
1193 // and flag operands which copy the outgoing args into registers.
1195 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1196 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1198 InFlag = Chain.getValue(1);
1202 // From AMD64 ABI document:
1203 // For calls that may call functions that use varargs or stdargs
1204 // (prototype-less calls or calls to functions containing ellipsis (...) in
1205 // the declaration) %al is used as hidden argument to specify the number
1206 // of SSE registers used. The contents of %al do not need to match exactly
1207 // the number of registers, but must be an ubound on the number of SSE
1208 // registers used and is in the range 0 - 8 inclusive.
1209 Chain = DAG.getCopyToReg(Chain, X86::AL,
1210 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1211 InFlag = Chain.getValue(1);
1214 // If the callee is a GlobalAddress node (quite common, every direct call is)
1215 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1216 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1217 // We should use extra load for direct calls to dllimported functions in
1219 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1220 getTargetMachine(), true))
1221 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1222 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1223 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1225 std::vector<MVT::ValueType> NodeTys;
1226 NodeTys.push_back(MVT::Other); // Returns a chain
1227 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1228 std::vector<SDOperand> Ops;
1229 Ops.push_back(Chain);
1230 Ops.push_back(Callee);
1232 // Add argument registers to the end of the list so that they are known live
1234 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1235 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1236 RegsToPass[i].second.getValueType()));
1239 Ops.push_back(InFlag);
1241 // FIXME: Do not generate X86ISD::TAILCALL for now.
1242 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1243 NodeTys, &Ops[0], Ops.size());
1244 InFlag = Chain.getValue(1);
1247 NodeTys.push_back(MVT::Other); // Returns a chain
1248 if (RetVT != MVT::Other)
1249 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1251 Ops.push_back(Chain);
1252 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1253 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1254 Ops.push_back(InFlag);
1255 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1256 if (RetVT != MVT::Other)
1257 InFlag = Chain.getValue(1);
1259 std::vector<SDOperand> ResultVals;
1262 default: assert(0 && "Unknown value type to return!");
1263 case MVT::Other: break;
1265 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1266 ResultVals.push_back(Chain.getValue(0));
1267 NodeTys.push_back(MVT::i8);
1270 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1271 ResultVals.push_back(Chain.getValue(0));
1272 NodeTys.push_back(MVT::i16);
1275 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1276 ResultVals.push_back(Chain.getValue(0));
1277 NodeTys.push_back(MVT::i32);
1280 if (Op.Val->getValueType(1) == MVT::i64) {
1281 // FIXME: __int128 support?
1282 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1283 ResultVals.push_back(Chain.getValue(0));
1284 Chain = DAG.getCopyFromReg(Chain, X86::RDX, MVT::i64,
1285 Chain.getValue(2)).getValue(1);
1286 ResultVals.push_back(Chain.getValue(0));
1287 NodeTys.push_back(MVT::i64);
1289 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1290 ResultVals.push_back(Chain.getValue(0));
1292 NodeTys.push_back(MVT::i64);
1302 // FIXME: long double support?
1303 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1304 ResultVals.push_back(Chain.getValue(0));
1305 NodeTys.push_back(RetVT);
1309 // If the function returns void, just return the chain.
1310 if (ResultVals.empty())
1313 // Otherwise, merge everything together with a MERGE_VALUES node.
1314 NodeTys.push_back(MVT::Other);
1315 ResultVals.push_back(Chain);
1316 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1317 &ResultVals[0], ResultVals.size());
1318 return Res.getValue(Op.ResNo);
1321 //===----------------------------------------------------------------------===//
1322 // Fast Calling Convention implementation
1323 //===----------------------------------------------------------------------===//
1325 // The X86 'fast' calling convention passes up to two integer arguments in
1326 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
1327 // and requires that the callee pop its arguments off the stack (allowing proper
1328 // tail calls), and has the same return value conventions as C calling convs.
1330 // This calling convention always arranges for the callee pop value to be 8n+4
1331 // bytes, which is needed for tail recursion elimination and stack alignment
1334 // Note that this can be enhanced in the future to pass fp vals in registers
1335 // (when we have a global fp allocator) and do other tricks.
1338 /// HowToPassFastCCArgument - Returns how an formal argument of the specified
1339 /// type should be passed. If it is through stack, returns the size of the stack
1340 /// slot; if it is through integer or XMM register, returns the number of
1341 /// integer or XMM registers are needed.
1343 HowToPassFastCCArgument(MVT::ValueType ObjectVT,
1344 unsigned NumIntRegs, unsigned NumXMMRegs,
1345 unsigned &ObjSize, unsigned &ObjIntRegs,
1346 unsigned &ObjXMMRegs) {
1352 default: assert(0 && "Unhandled argument type!");
1354 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1355 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1362 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1363 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1370 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1371 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1378 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1379 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
1381 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
1408 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1409 unsigned NumArgs = Op.Val->getNumValues()-1;
1410 MachineFunction &MF = DAG.getMachineFunction();
1411 MachineFrameInfo *MFI = MF.getFrameInfo();
1412 SDOperand Root = Op.getOperand(0);
1413 std::vector<SDOperand> ArgValues;
1415 // Add DAG nodes to load the arguments... On entry to a function the stack
1416 // frame looks like this:
1418 // [ESP] -- return address
1419 // [ESP + 4] -- first nonreg argument (leftmost lexically)
1420 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
1422 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1424 // Keep track of the number of integer regs passed so far. This can be either
1425 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1427 unsigned NumIntRegs = 0;
1428 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1430 static const unsigned XMMArgRegs[] = {
1431 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1434 for (unsigned i = 0; i < NumArgs; ++i) {
1435 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1436 unsigned ArgIncrement = 4;
1437 unsigned ObjSize = 0;
1438 unsigned ObjIntRegs = 0;
1439 unsigned ObjXMMRegs = 0;
1441 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
1442 ObjSize, ObjIntRegs, ObjXMMRegs);
1444 ArgIncrement = ObjSize;
1448 if (ObjIntRegs || ObjXMMRegs) {
1450 default: assert(0 && "Unhandled argument type!");
1452 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
1453 X86::GR8RegisterClass);
1454 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
1457 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
1458 X86::GR16RegisterClass);
1459 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
1462 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
1463 X86::GR32RegisterClass);
1464 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1467 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
1468 X86::GR32RegisterClass);
1469 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1470 if (ObjIntRegs == 2) {
1471 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
1472 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1473 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
1482 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
1483 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1486 NumIntRegs += ObjIntRegs;
1487 NumXMMRegs += ObjXMMRegs;
1491 // XMM arguments have to be aligned on 16-byte boundary.
1493 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1494 // Create the SelectionDAG nodes corresponding to a load from this
1496 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1497 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1498 if (ObjectVT == MVT::i64 && ObjIntRegs) {
1499 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
1501 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
1503 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1504 ArgOffset += ArgIncrement; // Move on to the next argument.
1507 ArgValues.push_back(ArgValue);
1510 ArgValues.push_back(Root);
1512 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1513 // arguments and the arguments after the retaddr has been pushed are aligned.
1514 if ((ArgOffset & 7) == 0)
1517 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1518 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1519 ReturnAddrIndex = 0; // No return address slot generated yet.
1520 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1521 BytesCallerReserves = 0;
1523 // Finally, inform the code generator which regs we return values in.
1524 switch (getValueType(MF.getFunction()->getReturnType())) {
1525 default: assert(0 && "Unknown type!");
1526 case MVT::isVoid: break;
1531 MF.addLiveOut(X86::EAX);
1534 MF.addLiveOut(X86::EAX);
1535 MF.addLiveOut(X86::EDX);
1539 MF.addLiveOut(X86::ST0);
1547 MF.addLiveOut(X86::XMM0);
1551 // Return the new list of results.
1552 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1553 Op.Val->value_end());
1554 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1557 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1559 SDOperand Chain = Op.getOperand(0);
1560 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1561 SDOperand Callee = Op.getOperand(4);
1562 MVT::ValueType RetVT= Op.Val->getValueType(0);
1563 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1565 // Count how many bytes are to be pushed on the stack.
1566 unsigned NumBytes = 0;
1568 // Keep track of the number of integer regs passed so far. This can be either
1569 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1571 unsigned NumIntRegs = 0;
1572 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1574 static const unsigned GPRArgRegs[][2] = {
1575 { X86::AL, X86::DL },
1576 { X86::AX, X86::DX },
1577 { X86::EAX, X86::EDX }
1580 static const unsigned FastCallGPRArgRegs[][2] = {
1581 { X86::CL, X86::DL },
1582 { X86::CX, X86::DX },
1583 { X86::ECX, X86::EDX }
1586 static const unsigned XMMArgRegs[] = {
1587 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1590 for (unsigned i = 0; i != NumOps; ++i) {
1591 SDOperand Arg = Op.getOperand(5+2*i);
1593 switch (Arg.getValueType()) {
1594 default: assert(0 && "Unknown value type!");
1598 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1599 if (NumIntRegs < MaxNumIntRegs) {
1617 assert(0 && "Unknown value type!");
1622 // XMM arguments have to be aligned on 16-byte boundary.
1623 NumBytes = ((NumBytes + 15) / 16) * 16;
1631 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1632 // arguments and the arguments after the retaddr has been pushed are aligned.
1633 if ((NumBytes & 7) == 0)
1636 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1638 // Arguments go on the stack in reverse order, as specified by the ABI.
1639 unsigned ArgOffset = 0;
1641 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1642 std::vector<SDOperand> MemOpChains;
1643 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1644 for (unsigned i = 0; i != NumOps; ++i) {
1645 SDOperand Arg = Op.getOperand(5+2*i);
1647 switch (Arg.getValueType()) {
1648 default: assert(0 && "Unexpected ValueType for argument!");
1652 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1653 if (NumIntRegs < MaxNumIntRegs) {
1654 RegsToPass.push_back(
1655 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1662 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1663 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1664 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1669 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1670 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1671 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1682 assert(0 && "Unexpected ValueType for argument!");
1684 if (NumXMMRegs < 4) {
1685 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1688 // XMM arguments have to be aligned on 16-byte boundary.
1689 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1690 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1691 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1692 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1700 if (!MemOpChains.empty())
1701 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1702 &MemOpChains[0], MemOpChains.size());
1704 // Build a sequence of copy-to-reg nodes chained together with token chain
1705 // and flag operands which copy the outgoing args into registers.
1707 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1708 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1710 InFlag = Chain.getValue(1);
1713 // If the callee is a GlobalAddress node (quite common, every direct call is)
1714 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1715 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1716 // We should use extra load for direct calls to dllimported functions in
1718 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1719 getTargetMachine(), true))
1720 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1721 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1722 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1724 std::vector<MVT::ValueType> NodeTys;
1725 NodeTys.push_back(MVT::Other); // Returns a chain
1726 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1727 std::vector<SDOperand> Ops;
1728 Ops.push_back(Chain);
1729 Ops.push_back(Callee);
1731 // Add argument registers to the end of the list so that they are known live
1733 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1734 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1735 RegsToPass[i].second.getValueType()));
1738 Ops.push_back(InFlag);
1740 // FIXME: Do not generate X86ISD::TAILCALL for now.
1741 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1742 NodeTys, &Ops[0], Ops.size());
1743 InFlag = Chain.getValue(1);
1746 NodeTys.push_back(MVT::Other); // Returns a chain
1747 if (RetVT != MVT::Other)
1748 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1750 Ops.push_back(Chain);
1751 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1752 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1753 Ops.push_back(InFlag);
1754 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1755 if (RetVT != MVT::Other)
1756 InFlag = Chain.getValue(1);
1758 std::vector<SDOperand> ResultVals;
1761 default: assert(0 && "Unknown value type to return!");
1762 case MVT::Other: break;
1764 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1765 ResultVals.push_back(Chain.getValue(0));
1766 NodeTys.push_back(MVT::i8);
1769 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1770 ResultVals.push_back(Chain.getValue(0));
1771 NodeTys.push_back(MVT::i16);
1774 if (Op.Val->getValueType(1) == MVT::i32) {
1775 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1776 ResultVals.push_back(Chain.getValue(0));
1777 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1778 Chain.getValue(2)).getValue(1);
1779 ResultVals.push_back(Chain.getValue(0));
1780 NodeTys.push_back(MVT::i32);
1782 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1783 ResultVals.push_back(Chain.getValue(0));
1785 NodeTys.push_back(MVT::i32);
1794 assert(0 && "Unknown value type to return!");
1796 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1797 ResultVals.push_back(Chain.getValue(0));
1798 NodeTys.push_back(RetVT);
1803 std::vector<MVT::ValueType> Tys;
1804 Tys.push_back(MVT::f64);
1805 Tys.push_back(MVT::Other);
1806 Tys.push_back(MVT::Flag);
1807 std::vector<SDOperand> Ops;
1808 Ops.push_back(Chain);
1809 Ops.push_back(InFlag);
1810 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
1811 &Ops[0], Ops.size());
1812 Chain = RetVal.getValue(1);
1813 InFlag = RetVal.getValue(2);
1815 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1816 // shouldn't be necessary except that RFP cannot be live across
1817 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1818 MachineFunction &MF = DAG.getMachineFunction();
1819 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1820 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1822 Tys.push_back(MVT::Other);
1824 Ops.push_back(Chain);
1825 Ops.push_back(RetVal);
1826 Ops.push_back(StackSlot);
1827 Ops.push_back(DAG.getValueType(RetVT));
1828 Ops.push_back(InFlag);
1829 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
1830 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
1831 Chain = RetVal.getValue(1);
1834 if (RetVT == MVT::f32 && !X86ScalarSSE)
1835 // FIXME: we would really like to remember that this FP_ROUND
1836 // operation is okay to eliminate if we allow excess FP precision.
1837 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1838 ResultVals.push_back(RetVal);
1839 NodeTys.push_back(RetVT);
1845 // If the function returns void, just return the chain.
1846 if (ResultVals.empty())
1849 // Otherwise, merge everything together with a MERGE_VALUES node.
1850 NodeTys.push_back(MVT::Other);
1851 ResultVals.push_back(Chain);
1852 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1853 &ResultVals[0], ResultVals.size());
1854 return Res.getValue(Op.ResNo);
1857 //===----------------------------------------------------------------------===//
1858 // StdCall Calling Convention implementation
1859 //===----------------------------------------------------------------------===//
1860 // StdCall calling convention seems to be standard for many Windows' API
1861 // routines and around. It differs from C calling convention just a little:
1862 // callee should clean up the stack, not caller. Symbols should be also
1863 // decorated in some fancy way :) It doesn't support any vector arguments.
1865 /// HowToPassStdCallCCArgument - Returns how an formal argument of the specified
1866 /// type should be passed. Returns the size of the stack slot
1868 HowToPassStdCallCCArgument(MVT::ValueType ObjectVT, unsigned &ObjSize) {
1870 default: assert(0 && "Unhandled argument type!");
1871 case MVT::i8: ObjSize = 1; break;
1872 case MVT::i16: ObjSize = 2; break;
1873 case MVT::i32: ObjSize = 4; break;
1874 case MVT::i64: ObjSize = 8; break;
1875 case MVT::f32: ObjSize = 4; break;
1876 case MVT::f64: ObjSize = 8; break;
1880 SDOperand X86TargetLowering::LowerStdCallCCArguments(SDOperand Op,
1881 SelectionDAG &DAG) {
1882 unsigned NumArgs = Op.Val->getNumValues() - 1;
1883 MachineFunction &MF = DAG.getMachineFunction();
1884 MachineFrameInfo *MFI = MF.getFrameInfo();
1885 SDOperand Root = Op.getOperand(0);
1886 std::vector<SDOperand> ArgValues;
1888 // Add DAG nodes to load the arguments... On entry to a function on the X86,
1889 // the stack frame looks like this:
1891 // [ESP] -- return address
1892 // [ESP + 4] -- first argument (leftmost lexically)
1893 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
1896 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1897 for (unsigned i = 0; i < NumArgs; ++i) {
1898 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1899 unsigned ArgIncrement = 4;
1900 unsigned ObjSize = 0;
1901 HowToPassStdCallCCArgument(ObjectVT, ObjSize);
1903 ArgIncrement = ObjSize;
1906 // Create the frame index object for this incoming parameter...
1907 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1908 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1909 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1910 ArgValues.push_back(ArgValue);
1911 ArgOffset += ArgIncrement; // Move on to the next argument...
1914 ArgValues.push_back(Root);
1916 // If the function takes variable number of arguments, make a frame index for
1917 // the start of the first vararg value... for expansion of llvm.va_start.
1918 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1920 BytesToPopOnReturn = 0; // Callee pops nothing.
1921 BytesCallerReserves = ArgOffset;
1922 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
1924 BytesToPopOnReturn = ArgOffset; // Callee pops everything..
1925 BytesCallerReserves = 0;
1927 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1928 ReturnAddrIndex = 0; // No return address slot generated yet.
1930 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
1932 // Return the new list of results.
1933 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1934 Op.Val->value_end());
1935 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1939 SDOperand X86TargetLowering::LowerStdCallCCCallTo(SDOperand Op,
1940 SelectionDAG &DAG) {
1941 SDOperand Chain = Op.getOperand(0);
1942 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1943 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1944 SDOperand Callee = Op.getOperand(4);
1945 MVT::ValueType RetVT= Op.Val->getValueType(0);
1946 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1948 // Count how many bytes are to be pushed on the stack.
1949 unsigned NumBytes = 0;
1950 for (unsigned i = 0; i != NumOps; ++i) {
1951 SDOperand Arg = Op.getOperand(5+2*i);
1953 switch (Arg.getValueType()) {
1954 default: assert(0 && "Unexpected ValueType for argument!");
1968 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1970 // Arguments go on the stack in reverse order, as specified by the ABI.
1971 unsigned ArgOffset = 0;
1972 std::vector<SDOperand> MemOpChains;
1973 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1974 for (unsigned i = 0; i != NumOps; ++i) {
1975 SDOperand Arg = Op.getOperand(5+2*i);
1977 switch (Arg.getValueType()) {
1978 default: assert(0 && "Unexpected ValueType for argument!");
1981 // Promote the integer to 32 bits. If the input type is signed use a
1982 // sign extend, otherwise use a zero extend.
1984 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
1985 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1986 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
1992 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1993 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1994 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
2000 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
2001 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
2002 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
2009 if (!MemOpChains.empty())
2010 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2011 &MemOpChains[0], MemOpChains.size());
2013 // If the callee is a GlobalAddress node (quite common, every direct call is)
2014 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
2015 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2016 // We should use extra load for direct calls to dllimported functions in
2018 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
2019 getTargetMachine(), true))
2020 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
2021 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
2022 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
2024 std::vector<MVT::ValueType> NodeTys;
2025 NodeTys.push_back(MVT::Other); // Returns a chain
2026 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2027 std::vector<SDOperand> Ops;
2028 Ops.push_back(Chain);
2029 Ops.push_back(Callee);
2031 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
2032 NodeTys, &Ops[0], Ops.size());
2033 SDOperand InFlag = Chain.getValue(1);
2035 // Create the CALLSEQ_END node.
2036 unsigned NumBytesForCalleeToPush;
2039 NumBytesForCalleeToPush = 0;
2041 NumBytesForCalleeToPush = NumBytes;
2045 NodeTys.push_back(MVT::Other); // Returns a chain
2046 if (RetVT != MVT::Other)
2047 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2049 Ops.push_back(Chain);
2050 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
2051 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
2052 Ops.push_back(InFlag);
2053 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2054 if (RetVT != MVT::Other)
2055 InFlag = Chain.getValue(1);
2057 std::vector<SDOperand> ResultVals;
2060 default: assert(0 && "Unknown value type to return!");
2061 case MVT::Other: break;
2063 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
2064 ResultVals.push_back(Chain.getValue(0));
2065 NodeTys.push_back(MVT::i8);
2068 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
2069 ResultVals.push_back(Chain.getValue(0));
2070 NodeTys.push_back(MVT::i16);
2073 if (Op.Val->getValueType(1) == MVT::i32) {
2074 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
2075 ResultVals.push_back(Chain.getValue(0));
2076 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
2077 Chain.getValue(2)).getValue(1);
2078 ResultVals.push_back(Chain.getValue(0));
2079 NodeTys.push_back(MVT::i32);
2081 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
2082 ResultVals.push_back(Chain.getValue(0));
2084 NodeTys.push_back(MVT::i32);
2088 std::vector<MVT::ValueType> Tys;
2089 Tys.push_back(MVT::f64);
2090 Tys.push_back(MVT::Other);
2091 Tys.push_back(MVT::Flag);
2092 std::vector<SDOperand> Ops;
2093 Ops.push_back(Chain);
2094 Ops.push_back(InFlag);
2095 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
2096 &Ops[0], Ops.size());
2097 Chain = RetVal.getValue(1);
2098 InFlag = RetVal.getValue(2);
2100 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
2101 // shouldn't be necessary except that RFP cannot be live across
2102 // multiple blocks. When stackifier is fixed, they can be uncoupled.
2103 MachineFunction &MF = DAG.getMachineFunction();
2104 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2105 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2107 Tys.push_back(MVT::Other);
2109 Ops.push_back(Chain);
2110 Ops.push_back(RetVal);
2111 Ops.push_back(StackSlot);
2112 Ops.push_back(DAG.getValueType(RetVT));
2113 Ops.push_back(InFlag);
2114 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
2115 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
2116 Chain = RetVal.getValue(1);
2119 if (RetVT == MVT::f32 && !X86ScalarSSE)
2120 // FIXME: we would really like to remember that this FP_ROUND
2121 // operation is okay to eliminate if we allow excess FP precision.
2122 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
2123 ResultVals.push_back(RetVal);
2124 NodeTys.push_back(RetVT);
2129 // If the function returns void, just return the chain.
2130 if (ResultVals.empty())
2133 // Otherwise, merge everything together with a MERGE_VALUES node.
2134 NodeTys.push_back(MVT::Other);
2135 ResultVals.push_back(Chain);
2136 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
2137 &ResultVals[0], ResultVals.size());
2138 return Res.getValue(Op.ResNo);
2141 //===----------------------------------------------------------------------===//
2142 // FastCall Calling Convention implementation
2143 //===----------------------------------------------------------------------===//
2145 // The X86 'fastcall' calling convention passes up to two integer arguments in
2146 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
2147 // and requires that the callee pop its arguments off the stack (allowing proper
2148 // tail calls), and has the same return value conventions as C calling convs.
2150 // This calling convention always arranges for the callee pop value to be 8n+4
2151 // bytes, which is needed for tail recursion elimination and stack alignment
2155 /// HowToPassFastCallCCArgument - Returns how an formal argument of the
2156 /// specified type should be passed. If it is through stack, returns the size of
2157 /// the stack slot; if it is through integer register, returns the number of
2158 /// integer registers are needed.
2160 HowToPassFastCallCCArgument(MVT::ValueType ObjectVT,
2161 unsigned NumIntRegs,
2163 unsigned &ObjIntRegs)
2169 default: assert(0 && "Unhandled argument type!");
2189 if (NumIntRegs+2 <= 2) {
2191 } else if (NumIntRegs+1 <= 2) {
2206 X86TargetLowering::LowerFastCallCCArguments(SDOperand Op, SelectionDAG &DAG) {
2207 unsigned NumArgs = Op.Val->getNumValues()-1;
2208 MachineFunction &MF = DAG.getMachineFunction();
2209 MachineFrameInfo *MFI = MF.getFrameInfo();
2210 SDOperand Root = Op.getOperand(0);
2211 std::vector<SDOperand> ArgValues;
2213 // Add DAG nodes to load the arguments... On entry to a function the stack
2214 // frame looks like this:
2216 // [ESP] -- return address
2217 // [ESP + 4] -- first nonreg argument (leftmost lexically)
2218 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
2220 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
2222 // Keep track of the number of integer regs passed so far. This can be either
2223 // 0 (neither ECX or EDX used), 1 (ECX is used) or 2 (ECX and EDX are both
2225 unsigned NumIntRegs = 0;
2227 for (unsigned i = 0; i < NumArgs; ++i) {
2228 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
2229 unsigned ArgIncrement = 4;
2230 unsigned ObjSize = 0;
2231 unsigned ObjIntRegs = 0;
2233 HowToPassFastCallCCArgument(ObjectVT, NumIntRegs, ObjSize, ObjIntRegs);
2235 ArgIncrement = ObjSize;
2241 default: assert(0 && "Unhandled argument type!");
2243 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::CL,
2244 X86::GR8RegisterClass);
2245 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
2248 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::CX,
2249 X86::GR16RegisterClass);
2250 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
2253 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::ECX,
2254 X86::GR32RegisterClass);
2255 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2258 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::ECX,
2259 X86::GR32RegisterClass);
2260 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2261 if (ObjIntRegs == 2) {
2262 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
2263 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2264 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
2269 NumIntRegs += ObjIntRegs;
2273 // Create the SelectionDAG nodes corresponding to a load from this
2275 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
2276 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
2277 if (ObjectVT == MVT::i64 && ObjIntRegs) {
2278 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
2280 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
2282 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
2283 ArgOffset += ArgIncrement; // Move on to the next argument.
2286 ArgValues.push_back(ArgValue);
2289 ArgValues.push_back(Root);
2291 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
2292 // arguments and the arguments after the retaddr has been pushed are aligned.
2293 if ((ArgOffset & 7) == 0)
2296 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
2297 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
2298 ReturnAddrIndex = 0; // No return address slot generated yet.
2299 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
2300 BytesCallerReserves = 0;
2302 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
2304 // Finally, inform the code generator which regs we return values in.
2305 switch (getValueType(MF.getFunction()->getReturnType())) {
2306 default: assert(0 && "Unknown type!");
2307 case MVT::isVoid: break;
2312 MF.addLiveOut(X86::ECX);
2315 MF.addLiveOut(X86::ECX);
2316 MF.addLiveOut(X86::EDX);
2320 MF.addLiveOut(X86::ST0);
2324 // Return the new list of results.
2325 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
2326 Op.Val->value_end());
2327 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
2330 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
2331 if (ReturnAddrIndex == 0) {
2332 // Set up a frame object for the return address.
2333 MachineFunction &MF = DAG.getMachineFunction();
2334 if (Subtarget->is64Bit())
2335 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2337 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
2340 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2345 std::pair<SDOperand, SDOperand> X86TargetLowering::
2346 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
2347 SelectionDAG &DAG) {
2349 if (Depth) // Depths > 0 not supported yet!
2350 Result = DAG.getConstant(0, getPointerTy());
2352 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
2353 if (!isFrameAddress)
2354 // Just load the return address
2355 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI,
2358 Result = DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
2359 DAG.getConstant(4, getPointerTy()));
2361 return std::make_pair(Result, Chain);
2364 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2365 /// specific condition code. It returns a false if it cannot do a direct
2366 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2368 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2369 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2370 SelectionDAG &DAG) {
2371 X86CC = X86::COND_INVALID;
2373 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2374 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2375 // X > -1 -> X == 0, jump !sign.
2376 RHS = DAG.getConstant(0, RHS.getValueType());
2377 X86CC = X86::COND_NS;
2379 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2380 // X < 0 -> X == 0, jump on sign.
2381 X86CC = X86::COND_S;
2386 switch (SetCCOpcode) {
2388 case ISD::SETEQ: X86CC = X86::COND_E; break;
2389 case ISD::SETGT: X86CC = X86::COND_G; break;
2390 case ISD::SETGE: X86CC = X86::COND_GE; break;
2391 case ISD::SETLT: X86CC = X86::COND_L; break;
2392 case ISD::SETLE: X86CC = X86::COND_LE; break;
2393 case ISD::SETNE: X86CC = X86::COND_NE; break;
2394 case ISD::SETULT: X86CC = X86::COND_B; break;
2395 case ISD::SETUGT: X86CC = X86::COND_A; break;
2396 case ISD::SETULE: X86CC = X86::COND_BE; break;
2397 case ISD::SETUGE: X86CC = X86::COND_AE; break;
2400 // On a floating point condition, the flags are set as follows:
2402 // 0 | 0 | 0 | X > Y
2403 // 0 | 0 | 1 | X < Y
2404 // 1 | 0 | 0 | X == Y
2405 // 1 | 1 | 1 | unordered
2407 switch (SetCCOpcode) {
2410 case ISD::SETEQ: X86CC = X86::COND_E; break;
2411 case ISD::SETOLT: Flip = true; // Fallthrough
2413 case ISD::SETGT: X86CC = X86::COND_A; break;
2414 case ISD::SETOLE: Flip = true; // Fallthrough
2416 case ISD::SETGE: X86CC = X86::COND_AE; break;
2417 case ISD::SETUGT: Flip = true; // Fallthrough
2419 case ISD::SETLT: X86CC = X86::COND_B; break;
2420 case ISD::SETUGE: Flip = true; // Fallthrough
2422 case ISD::SETLE: X86CC = X86::COND_BE; break;
2424 case ISD::SETNE: X86CC = X86::COND_NE; break;
2425 case ISD::SETUO: X86CC = X86::COND_P; break;
2426 case ISD::SETO: X86CC = X86::COND_NP; break;
2429 std::swap(LHS, RHS);
2432 return X86CC != X86::COND_INVALID;
2435 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
2436 /// code. Current x86 isa includes the following FP cmov instructions:
2437 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2438 static bool hasFPCMov(unsigned X86CC) {
2454 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2455 /// true if Op is undef or if its value falls within the specified range (L, H].
2456 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2457 if (Op.getOpcode() == ISD::UNDEF)
2460 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2461 return (Val >= Low && Val < Hi);
2464 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2465 /// true if Op is undef or if its value equal to the specified value.
2466 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2467 if (Op.getOpcode() == ISD::UNDEF)
2469 return cast<ConstantSDNode>(Op)->getValue() == Val;
2472 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2473 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
2474 bool X86::isPSHUFDMask(SDNode *N) {
2475 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2477 if (N->getNumOperands() != 4)
2480 // Check if the value doesn't reference the second vector.
2481 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2482 SDOperand Arg = N->getOperand(i);
2483 if (Arg.getOpcode() == ISD::UNDEF) continue;
2484 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2485 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
2492 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2493 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2494 bool X86::isPSHUFHWMask(SDNode *N) {
2495 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2497 if (N->getNumOperands() != 8)
2500 // Lower quadword copied in order.
2501 for (unsigned i = 0; i != 4; ++i) {
2502 SDOperand Arg = N->getOperand(i);
2503 if (Arg.getOpcode() == ISD::UNDEF) continue;
2504 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2505 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2509 // Upper quadword shuffled.
2510 for (unsigned i = 4; i != 8; ++i) {
2511 SDOperand Arg = N->getOperand(i);
2512 if (Arg.getOpcode() == ISD::UNDEF) continue;
2513 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2514 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2515 if (Val < 4 || Val > 7)
2522 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2523 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2524 bool X86::isPSHUFLWMask(SDNode *N) {
2525 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2527 if (N->getNumOperands() != 8)
2530 // Upper quadword copied in order.
2531 for (unsigned i = 4; i != 8; ++i)
2532 if (!isUndefOrEqual(N->getOperand(i), i))
2535 // Lower quadword shuffled.
2536 for (unsigned i = 0; i != 4; ++i)
2537 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2543 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2544 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
2545 static bool isSHUFPMask(std::vector<SDOperand> &N) {
2546 unsigned NumElems = N.size();
2547 if (NumElems != 2 && NumElems != 4) return false;
2549 unsigned Half = NumElems / 2;
2550 for (unsigned i = 0; i < Half; ++i)
2551 if (!isUndefOrInRange(N[i], 0, NumElems))
2553 for (unsigned i = Half; i < NumElems; ++i)
2554 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
2560 bool X86::isSHUFPMask(SDNode *N) {
2561 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2562 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2563 return ::isSHUFPMask(Ops);
2566 /// isCommutedSHUFP - Returns true if the shuffle mask is except
2567 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2568 /// half elements to come from vector 1 (which would equal the dest.) and
2569 /// the upper half to come from vector 2.
2570 static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
2571 unsigned NumElems = Ops.size();
2572 if (NumElems != 2 && NumElems != 4) return false;
2574 unsigned Half = NumElems / 2;
2575 for (unsigned i = 0; i < Half; ++i)
2576 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
2578 for (unsigned i = Half; i < NumElems; ++i)
2579 if (!isUndefOrInRange(Ops[i], 0, NumElems))
2584 static bool isCommutedSHUFP(SDNode *N) {
2585 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2586 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2587 return isCommutedSHUFP(Ops);
2590 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2591 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2592 bool X86::isMOVHLPSMask(SDNode *N) {
2593 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2595 if (N->getNumOperands() != 4)
2598 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2599 return isUndefOrEqual(N->getOperand(0), 6) &&
2600 isUndefOrEqual(N->getOperand(1), 7) &&
2601 isUndefOrEqual(N->getOperand(2), 2) &&
2602 isUndefOrEqual(N->getOperand(3), 3);
2605 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2606 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2608 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2609 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2611 if (N->getNumOperands() != 4)
2614 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2615 return isUndefOrEqual(N->getOperand(0), 2) &&
2616 isUndefOrEqual(N->getOperand(1), 3) &&
2617 isUndefOrEqual(N->getOperand(2), 2) &&
2618 isUndefOrEqual(N->getOperand(3), 3);
2621 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2622 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2623 bool X86::isMOVLPMask(SDNode *N) {
2624 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2626 unsigned NumElems = N->getNumOperands();
2627 if (NumElems != 2 && NumElems != 4)
2630 for (unsigned i = 0; i < NumElems/2; ++i)
2631 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2634 for (unsigned i = NumElems/2; i < NumElems; ++i)
2635 if (!isUndefOrEqual(N->getOperand(i), i))
2641 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2642 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2644 bool X86::isMOVHPMask(SDNode *N) {
2645 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2647 unsigned NumElems = N->getNumOperands();
2648 if (NumElems != 2 && NumElems != 4)
2651 for (unsigned i = 0; i < NumElems/2; ++i)
2652 if (!isUndefOrEqual(N->getOperand(i), i))
2655 for (unsigned i = 0; i < NumElems/2; ++i) {
2656 SDOperand Arg = N->getOperand(i + NumElems/2);
2657 if (!isUndefOrEqual(Arg, i + NumElems))
2664 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2665 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
2666 bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2667 unsigned NumElems = N.size();
2668 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2671 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2672 SDOperand BitI = N[i];
2673 SDOperand BitI1 = N[i+1];
2674 if (!isUndefOrEqual(BitI, j))
2677 if (isUndefOrEqual(BitI1, NumElems))
2680 if (!isUndefOrEqual(BitI1, j + NumElems))
2688 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2689 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2690 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2691 return ::isUNPCKLMask(Ops, V2IsSplat);
2694 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2695 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
2696 bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2697 unsigned NumElems = N.size();
2698 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2701 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2702 SDOperand BitI = N[i];
2703 SDOperand BitI1 = N[i+1];
2704 if (!isUndefOrEqual(BitI, j + NumElems/2))
2707 if (isUndefOrEqual(BitI1, NumElems))
2710 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
2718 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2719 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2720 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2721 return ::isUNPCKHMask(Ops, V2IsSplat);
2724 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2725 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2727 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2728 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2730 unsigned NumElems = N->getNumOperands();
2731 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
2734 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2735 SDOperand BitI = N->getOperand(i);
2736 SDOperand BitI1 = N->getOperand(i+1);
2738 if (!isUndefOrEqual(BitI, j))
2740 if (!isUndefOrEqual(BitI1, j))
2747 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2748 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2749 /// MOVSD, and MOVD, i.e. setting the lowest element.
2750 static bool isMOVLMask(std::vector<SDOperand> &N) {
2751 unsigned NumElems = N.size();
2752 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2755 if (!isUndefOrEqual(N[0], NumElems))
2758 for (unsigned i = 1; i < NumElems; ++i) {
2759 SDOperand Arg = N[i];
2760 if (!isUndefOrEqual(Arg, i))
2767 bool X86::isMOVLMask(SDNode *N) {
2768 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2769 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2770 return ::isMOVLMask(Ops);
2773 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2774 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2775 /// element of vector 2 and the other elements to come from vector 1 in order.
2776 static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false,
2777 bool V2IsUndef = false) {
2778 unsigned NumElems = Ops.size();
2779 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2782 if (!isUndefOrEqual(Ops[0], 0))
2785 for (unsigned i = 1; i < NumElems; ++i) {
2786 SDOperand Arg = Ops[i];
2787 if (!(isUndefOrEqual(Arg, i+NumElems) ||
2788 (V2IsUndef && isUndefOrInRange(Arg, NumElems, NumElems*2)) ||
2789 (V2IsSplat && isUndefOrEqual(Arg, NumElems))))
2796 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2797 bool V2IsUndef = false) {
2798 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2799 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2800 return isCommutedMOVL(Ops, V2IsSplat, V2IsUndef);
2803 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2804 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2805 bool X86::isMOVSHDUPMask(SDNode *N) {
2806 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2808 if (N->getNumOperands() != 4)
2811 // Expect 1, 1, 3, 3
2812 for (unsigned i = 0; i < 2; ++i) {
2813 SDOperand Arg = N->getOperand(i);
2814 if (Arg.getOpcode() == ISD::UNDEF) continue;
2815 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2816 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2817 if (Val != 1) return false;
2821 for (unsigned i = 2; i < 4; ++i) {
2822 SDOperand Arg = N->getOperand(i);
2823 if (Arg.getOpcode() == ISD::UNDEF) continue;
2824 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2825 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2826 if (Val != 3) return false;
2830 // Don't use movshdup if it can be done with a shufps.
2834 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2835 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2836 bool X86::isMOVSLDUPMask(SDNode *N) {
2837 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2839 if (N->getNumOperands() != 4)
2842 // Expect 0, 0, 2, 2
2843 for (unsigned i = 0; i < 2; ++i) {
2844 SDOperand Arg = N->getOperand(i);
2845 if (Arg.getOpcode() == ISD::UNDEF) continue;
2846 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2847 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2848 if (Val != 0) return false;
2852 for (unsigned i = 2; i < 4; ++i) {
2853 SDOperand Arg = N->getOperand(i);
2854 if (Arg.getOpcode() == ISD::UNDEF) continue;
2855 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2856 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2857 if (Val != 2) return false;
2861 // Don't use movshdup if it can be done with a shufps.
2865 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2866 /// a splat of a single element.
2867 static bool isSplatMask(SDNode *N) {
2868 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2870 // This is a splat operation if each element of the permute is the same, and
2871 // if the value doesn't reference the second vector.
2872 unsigned NumElems = N->getNumOperands();
2873 SDOperand ElementBase;
2875 for (; i != NumElems; ++i) {
2876 SDOperand Elt = N->getOperand(i);
2877 if (isa<ConstantSDNode>(Elt)) {
2883 if (!ElementBase.Val)
2886 for (; i != NumElems; ++i) {
2887 SDOperand Arg = N->getOperand(i);
2888 if (Arg.getOpcode() == ISD::UNDEF) continue;
2889 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2890 if (Arg != ElementBase) return false;
2893 // Make sure it is a splat of the first vector operand.
2894 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2897 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2898 /// a splat of a single element and it's a 2 or 4 element mask.
2899 bool X86::isSplatMask(SDNode *N) {
2900 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2902 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2903 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2905 return ::isSplatMask(N);
2908 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2909 /// specifies a splat of zero element.
2910 bool X86::isSplatLoMask(SDNode *N) {
2911 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2913 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2914 if (!isUndefOrEqual(N->getOperand(i), 0))
2919 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2920 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2922 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2923 unsigned NumOperands = N->getNumOperands();
2924 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2926 for (unsigned i = 0; i < NumOperands; ++i) {
2928 SDOperand Arg = N->getOperand(NumOperands-i-1);
2929 if (Arg.getOpcode() != ISD::UNDEF)
2930 Val = cast<ConstantSDNode>(Arg)->getValue();
2931 if (Val >= NumOperands) Val -= NumOperands;
2933 if (i != NumOperands - 1)
2940 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2941 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2943 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2945 // 8 nodes, but we only care about the last 4.
2946 for (unsigned i = 7; i >= 4; --i) {
2948 SDOperand Arg = N->getOperand(i);
2949 if (Arg.getOpcode() != ISD::UNDEF)
2950 Val = cast<ConstantSDNode>(Arg)->getValue();
2959 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2960 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2962 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2964 // 8 nodes, but we only care about the first 4.
2965 for (int i = 3; i >= 0; --i) {
2967 SDOperand Arg = N->getOperand(i);
2968 if (Arg.getOpcode() != ISD::UNDEF)
2969 Val = cast<ConstantSDNode>(Arg)->getValue();
2978 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2979 /// specifies a 8 element shuffle that can be broken into a pair of
2980 /// PSHUFHW and PSHUFLW.
2981 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2982 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2984 if (N->getNumOperands() != 8)
2987 // Lower quadword shuffled.
2988 for (unsigned i = 0; i != 4; ++i) {
2989 SDOperand Arg = N->getOperand(i);
2990 if (Arg.getOpcode() == ISD::UNDEF) continue;
2991 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2992 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2997 // Upper quadword shuffled.
2998 for (unsigned i = 4; i != 8; ++i) {
2999 SDOperand Arg = N->getOperand(i);
3000 if (Arg.getOpcode() == ISD::UNDEF) continue;
3001 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
3002 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3003 if (Val < 4 || Val > 7)
3010 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
3011 /// values in ther permute mask.
3012 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
3013 SDOperand &V2, SDOperand &Mask,
3014 SelectionDAG &DAG) {
3015 MVT::ValueType VT = Op.getValueType();
3016 MVT::ValueType MaskVT = Mask.getValueType();
3017 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
3018 unsigned NumElems = Mask.getNumOperands();
3019 std::vector<SDOperand> MaskVec;
3021 for (unsigned i = 0; i != NumElems; ++i) {
3022 SDOperand Arg = Mask.getOperand(i);
3023 if (Arg.getOpcode() == ISD::UNDEF) {
3024 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
3027 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
3028 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3030 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
3032 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
3036 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3037 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3040 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3041 /// match movhlps. The lower half elements should come from upper half of
3042 /// V1 (and in order), and the upper half elements should come from the upper
3043 /// half of V2 (and in order).
3044 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
3045 unsigned NumElems = Mask->getNumOperands();
3048 for (unsigned i = 0, e = 2; i != e; ++i)
3049 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
3051 for (unsigned i = 2; i != 4; ++i)
3052 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
3057 /// isScalarLoadToVector - Returns true if the node is a scalar load that
3058 /// is promoted to a vector.
3059 static inline bool isScalarLoadToVector(SDNode *N) {
3060 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
3061 N = N->getOperand(0).Val;
3062 return ISD::isNON_EXTLoad(N);
3067 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3068 /// match movlp{s|d}. The lower half elements should come from lower half of
3069 /// V1 (and in order), and the upper half elements should come from the upper
3070 /// half of V2 (and in order). And since V1 will become the source of the
3071 /// MOVLP, it must be either a vector load or a scalar load to vector.
3072 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
3073 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
3075 // Is V2 is a vector load, don't do this transformation. We will try to use
3076 // load folding shufps op.
3077 if (ISD::isNON_EXTLoad(V2))
3080 unsigned NumElems = Mask->getNumOperands();
3081 if (NumElems != 2 && NumElems != 4)
3083 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3084 if (!isUndefOrEqual(Mask->getOperand(i), i))
3086 for (unsigned i = NumElems/2; i != NumElems; ++i)
3087 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
3092 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3094 static bool isSplatVector(SDNode *N) {
3095 if (N->getOpcode() != ISD::BUILD_VECTOR)
3098 SDOperand SplatValue = N->getOperand(0);
3099 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3100 if (N->getOperand(i) != SplatValue)
3105 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
3107 static bool isUndefShuffle(SDNode *N) {
3108 if (N->getOpcode() != ISD::BUILD_VECTOR)
3111 SDOperand V1 = N->getOperand(0);
3112 SDOperand V2 = N->getOperand(1);
3113 SDOperand Mask = N->getOperand(2);
3114 unsigned NumElems = Mask.getNumOperands();
3115 for (unsigned i = 0; i != NumElems; ++i) {
3116 SDOperand Arg = Mask.getOperand(i);
3117 if (Arg.getOpcode() != ISD::UNDEF) {
3118 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3119 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
3121 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
3128 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3129 /// that point to V2 points to its first element.
3130 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
3131 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
3133 bool Changed = false;
3134 std::vector<SDOperand> MaskVec;
3135 unsigned NumElems = Mask.getNumOperands();
3136 for (unsigned i = 0; i != NumElems; ++i) {
3137 SDOperand Arg = Mask.getOperand(i);
3138 if (Arg.getOpcode() != ISD::UNDEF) {
3139 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3140 if (Val > NumElems) {
3141 Arg = DAG.getConstant(NumElems, Arg.getValueType());
3145 MaskVec.push_back(Arg);
3149 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
3150 &MaskVec[0], MaskVec.size());
3154 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3155 /// operation of specified width.
3156 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
3157 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3158 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3160 std::vector<SDOperand> MaskVec;
3161 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
3162 for (unsigned i = 1; i != NumElems; ++i)
3163 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3164 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3167 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
3168 /// of specified width.
3169 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
3170 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3171 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3172 std::vector<SDOperand> MaskVec;
3173 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
3174 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3175 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
3177 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3180 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3181 /// of specified width.
3182 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
3183 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3184 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3185 unsigned Half = NumElems/2;
3186 std::vector<SDOperand> MaskVec;
3187 for (unsigned i = 0; i != Half; ++i) {
3188 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3189 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3191 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3194 /// getZeroVector - Returns a vector of specified type with all zero elements.
3196 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
3197 assert(MVT::isVector(VT) && "Expected a vector type");
3198 unsigned NumElems = getVectorNumElements(VT);
3199 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
3200 bool isFP = MVT::isFloatingPoint(EVT);
3201 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
3202 std::vector<SDOperand> ZeroVec(NumElems, Zero);
3203 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
3206 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
3208 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
3209 SDOperand V1 = Op.getOperand(0);
3210 SDOperand Mask = Op.getOperand(2);
3211 MVT::ValueType VT = Op.getValueType();
3212 unsigned NumElems = Mask.getNumOperands();
3213 Mask = getUnpacklMask(NumElems, DAG);
3214 while (NumElems != 4) {
3215 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3218 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3220 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3221 Mask = getZeroVector(MaskVT, DAG);
3222 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
3223 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
3224 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3227 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
3229 static inline bool isZeroNode(SDOperand Elt) {
3230 return ((isa<ConstantSDNode>(Elt) &&
3231 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
3232 (isa<ConstantFPSDNode>(Elt) &&
3233 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
3236 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
3237 /// vector and zero or undef vector.
3238 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
3239 unsigned NumElems, unsigned Idx,
3240 bool isZero, SelectionDAG &DAG) {
3241 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
3242 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3243 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
3244 SDOperand Zero = DAG.getConstant(0, EVT);
3245 std::vector<SDOperand> MaskVec(NumElems, Zero);
3246 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
3247 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3248 &MaskVec[0], MaskVec.size());
3249 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3252 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3254 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3255 unsigned NumNonZero, unsigned NumZero,
3256 SelectionDAG &DAG, TargetLowering &TLI) {
3262 for (unsigned i = 0; i < 16; ++i) {
3263 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3264 if (ThisIsNonZero && First) {
3266 V = getZeroVector(MVT::v8i16, DAG);
3268 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3273 SDOperand ThisElt(0, 0), LastElt(0, 0);
3274 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3275 if (LastIsNonZero) {
3276 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3278 if (ThisIsNonZero) {
3279 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3280 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3281 ThisElt, DAG.getConstant(8, MVT::i8));
3283 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3288 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
3289 DAG.getConstant(i/2, TLI.getPointerTy()));
3293 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3296 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
3298 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3299 unsigned NumNonZero, unsigned NumZero,
3300 SelectionDAG &DAG, TargetLowering &TLI) {
3306 for (unsigned i = 0; i < 8; ++i) {
3307 bool isNonZero = (NonZeros & (1 << i)) != 0;
3311 V = getZeroVector(MVT::v8i16, DAG);
3313 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3316 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
3317 DAG.getConstant(i, TLI.getPointerTy()));
3325 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3326 // All zero's are handled with pxor.
3327 if (ISD::isBuildVectorAllZeros(Op.Val))
3330 // All one's are handled with pcmpeqd.
3331 if (ISD::isBuildVectorAllOnes(Op.Val))
3334 MVT::ValueType VT = Op.getValueType();
3335 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
3336 unsigned EVTBits = MVT::getSizeInBits(EVT);
3338 unsigned NumElems = Op.getNumOperands();
3339 unsigned NumZero = 0;
3340 unsigned NumNonZero = 0;
3341 unsigned NonZeros = 0;
3342 std::set<SDOperand> Values;
3343 for (unsigned i = 0; i < NumElems; ++i) {
3344 SDOperand Elt = Op.getOperand(i);
3345 if (Elt.getOpcode() != ISD::UNDEF) {
3347 if (isZeroNode(Elt))
3350 NonZeros |= (1 << i);
3356 if (NumNonZero == 0)
3357 // Must be a mix of zero and undef. Return a zero vector.
3358 return getZeroVector(VT, DAG);
3360 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3361 if (Values.size() == 1)
3364 // Special case for single non-zero element.
3365 if (NumNonZero == 1) {
3366 unsigned Idx = CountTrailingZeros_32(NonZeros);
3367 SDOperand Item = Op.getOperand(Idx);
3368 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3370 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3371 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3374 if (EVTBits == 32) {
3375 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3376 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3378 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3379 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
3380 std::vector<SDOperand> MaskVec;
3381 for (unsigned i = 0; i < NumElems; i++)
3382 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3383 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3384 &MaskVec[0], MaskVec.size());
3385 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3386 DAG.getNode(ISD::UNDEF, VT), Mask);
3390 // Let legalizer expand 2-wide build_vector's.
3394 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3396 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3398 if (V.Val) return V;
3401 if (EVTBits == 16) {
3402 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3404 if (V.Val) return V;
3407 // If element VT is == 32 bits, turn it into a number of shuffles.
3408 std::vector<SDOperand> V(NumElems);
3409 if (NumElems == 4 && NumZero > 0) {
3410 for (unsigned i = 0; i < 4; ++i) {
3411 bool isZero = !(NonZeros & (1 << i));
3413 V[i] = getZeroVector(VT, DAG);
3415 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3418 for (unsigned i = 0; i < 2; ++i) {
3419 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3422 V[i] = V[i*2]; // Must be a zero vector.
3425 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3426 getMOVLMask(NumElems, DAG));
3429 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3430 getMOVLMask(NumElems, DAG));
3433 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3434 getUnpacklMask(NumElems, DAG));
3439 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3440 // clears the upper bits.
3441 // FIXME: we can do the same for v4f32 case when we know both parts of
3442 // the lower half come from scalar_to_vector (loadf32). We should do
3443 // that in post legalizer dag combiner with target specific hooks.
3444 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3446 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3447 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
3448 std::vector<SDOperand> MaskVec;
3449 bool Reverse = (NonZeros & 0x3) == 2;
3450 for (unsigned i = 0; i < 2; ++i)
3452 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3454 MaskVec.push_back(DAG.getConstant(i, EVT));
3455 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3456 for (unsigned i = 0; i < 2; ++i)
3458 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3460 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3461 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3462 &MaskVec[0], MaskVec.size());
3463 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3466 if (Values.size() > 2) {
3467 // Expand into a number of unpckl*.
3469 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3470 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3471 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3472 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3473 for (unsigned i = 0; i < NumElems; ++i)
3474 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3476 while (NumElems != 0) {
3477 for (unsigned i = 0; i < NumElems; ++i)
3478 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3489 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3490 SDOperand V1 = Op.getOperand(0);
3491 SDOperand V2 = Op.getOperand(1);
3492 SDOperand PermMask = Op.getOperand(2);
3493 MVT::ValueType VT = Op.getValueType();
3494 unsigned NumElems = PermMask.getNumOperands();
3495 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3496 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3497 bool V1IsSplat = false;
3498 bool V2IsSplat = false;
3500 if (isUndefShuffle(Op.Val))
3501 return DAG.getNode(ISD::UNDEF, VT);
3503 if (isSplatMask(PermMask.Val)) {
3504 if (NumElems <= 4) return Op;
3505 // Promote it to a v4i32 splat.
3506 return PromoteSplat(Op, DAG);
3509 if (X86::isMOVLMask(PermMask.Val))
3510 return (V1IsUndef) ? V2 : Op;
3512 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3513 X86::isMOVSLDUPMask(PermMask.Val) ||
3514 X86::isMOVHLPSMask(PermMask.Val) ||
3515 X86::isMOVHPMask(PermMask.Val) ||
3516 X86::isMOVLPMask(PermMask.Val))
3519 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3520 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3521 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3523 bool Commuted = false;
3524 V1IsSplat = isSplatVector(V1.Val);
3525 V2IsSplat = isSplatVector(V2.Val);
3526 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3527 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3528 std::swap(V1IsSplat, V2IsSplat);
3529 std::swap(V1IsUndef, V2IsUndef);
3533 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3534 if (V2IsUndef) return V1;
3535 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3537 // V2 is a splat, so the mask may be malformed. That is, it may point
3538 // to any V2 element. The instruction selectior won't like this. Get
3539 // a corrected mask and commute to form a proper MOVS{S|D}.
3540 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3541 if (NewMask.Val != PermMask.Val)
3542 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3547 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3548 X86::isUNPCKLMask(PermMask.Val) ||
3549 X86::isUNPCKHMask(PermMask.Val))
3553 // Normalize mask so all entries that point to V2 points to its first
3554 // element then try to match unpck{h|l} again. If match, return a
3555 // new vector_shuffle with the corrected mask.
3556 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3557 if (NewMask.Val != PermMask.Val) {
3558 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3559 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3560 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3561 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3562 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3563 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3568 // Normalize the node to match x86 shuffle ops if needed
3569 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3570 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3573 // Commute is back and try unpck* again.
3574 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3575 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3576 X86::isUNPCKLMask(PermMask.Val) ||
3577 X86::isUNPCKHMask(PermMask.Val))
3581 // If VT is integer, try PSHUF* first, then SHUFP*.
3582 if (MVT::isInteger(VT)) {
3583 if (X86::isPSHUFDMask(PermMask.Val) ||
3584 X86::isPSHUFHWMask(PermMask.Val) ||
3585 X86::isPSHUFLWMask(PermMask.Val)) {
3586 if (V2.getOpcode() != ISD::UNDEF)
3587 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3588 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3592 if (X86::isSHUFPMask(PermMask.Val))
3595 // Handle v8i16 shuffle high / low shuffle node pair.
3596 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3597 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3598 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3599 std::vector<SDOperand> MaskVec;
3600 for (unsigned i = 0; i != 4; ++i)
3601 MaskVec.push_back(PermMask.getOperand(i));
3602 for (unsigned i = 4; i != 8; ++i)
3603 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3604 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3605 &MaskVec[0], MaskVec.size());
3606 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3608 for (unsigned i = 0; i != 4; ++i)
3609 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3610 for (unsigned i = 4; i != 8; ++i)
3611 MaskVec.push_back(PermMask.getOperand(i));
3612 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
3613 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3616 // Floating point cases in the other order.
3617 if (X86::isSHUFPMask(PermMask.Val))
3619 if (X86::isPSHUFDMask(PermMask.Val) ||
3620 X86::isPSHUFHWMask(PermMask.Val) ||
3621 X86::isPSHUFLWMask(PermMask.Val)) {
3622 if (V2.getOpcode() != ISD::UNDEF)
3623 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3624 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3629 if (NumElems == 4) {
3630 MVT::ValueType MaskVT = PermMask.getValueType();
3631 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
3632 std::vector<std::pair<int, int> > Locs;
3633 Locs.reserve(NumElems);
3634 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3635 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3638 // If no more than two elements come from either vector. This can be
3639 // implemented with two shuffles. First shuffle gather the elements.
3640 // The second shuffle, which takes the first shuffle as both of its
3641 // vector operands, put the elements into the right order.
3642 for (unsigned i = 0; i != NumElems; ++i) {
3643 SDOperand Elt = PermMask.getOperand(i);
3644 if (Elt.getOpcode() == ISD::UNDEF) {
3645 Locs[i] = std::make_pair(-1, -1);
3647 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3648 if (Val < NumElems) {
3649 Locs[i] = std::make_pair(0, NumLo);
3653 Locs[i] = std::make_pair(1, NumHi);
3654 if (2+NumHi < NumElems)
3655 Mask1[2+NumHi] = Elt;
3660 if (NumLo <= 2 && NumHi <= 2) {
3661 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3662 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3663 &Mask1[0], Mask1.size()));
3664 for (unsigned i = 0; i != NumElems; ++i) {
3665 if (Locs[i].first == -1)
3668 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3669 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3670 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3674 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3675 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3676 &Mask2[0], Mask2.size()));
3679 // Break it into (shuffle shuffle_hi, shuffle_lo).
3681 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3682 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3683 std::vector<SDOperand> *MaskPtr = &LoMask;
3684 unsigned MaskIdx = 0;
3686 unsigned HiIdx = NumElems/2;
3687 for (unsigned i = 0; i != NumElems; ++i) {
3688 if (i == NumElems/2) {
3694 SDOperand Elt = PermMask.getOperand(i);
3695 if (Elt.getOpcode() == ISD::UNDEF) {
3696 Locs[i] = std::make_pair(-1, -1);
3697 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3698 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3699 (*MaskPtr)[LoIdx] = Elt;
3702 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3703 (*MaskPtr)[HiIdx] = Elt;
3708 SDOperand LoShuffle =
3709 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3710 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3711 &LoMask[0], LoMask.size()));
3712 SDOperand HiShuffle =
3713 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3714 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3715 &HiMask[0], HiMask.size()));
3716 std::vector<SDOperand> MaskOps;
3717 for (unsigned i = 0; i != NumElems; ++i) {
3718 if (Locs[i].first == -1) {
3719 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3721 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3722 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3725 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3726 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3727 &MaskOps[0], MaskOps.size()));
3734 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3735 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3738 MVT::ValueType VT = Op.getValueType();
3739 // TODO: handle v16i8.
3740 if (MVT::getSizeInBits(VT) == 16) {
3741 // Transform it so it match pextrw which produces a 32-bit result.
3742 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3743 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3744 Op.getOperand(0), Op.getOperand(1));
3745 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3746 DAG.getValueType(VT));
3747 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3748 } else if (MVT::getSizeInBits(VT) == 32) {
3749 SDOperand Vec = Op.getOperand(0);
3750 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3753 // SHUFPS the element to the lowest double word, then movss.
3754 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3755 std::vector<SDOperand> IdxVec;
3756 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
3757 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3758 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3759 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3760 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3761 &IdxVec[0], IdxVec.size());
3762 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3763 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3764 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3765 DAG.getConstant(0, getPointerTy()));
3766 } else if (MVT::getSizeInBits(VT) == 64) {
3767 SDOperand Vec = Op.getOperand(0);
3768 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3772 // UNPCKHPD the element to the lowest double word, then movsd.
3773 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3774 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3775 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3776 std::vector<SDOperand> IdxVec;
3777 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
3778 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3779 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3780 &IdxVec[0], IdxVec.size());
3781 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3782 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3783 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3784 DAG.getConstant(0, getPointerTy()));
3791 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3792 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3793 // as its second argument.
3794 MVT::ValueType VT = Op.getValueType();
3795 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
3796 SDOperand N0 = Op.getOperand(0);
3797 SDOperand N1 = Op.getOperand(1);
3798 SDOperand N2 = Op.getOperand(2);
3799 if (MVT::getSizeInBits(BaseVT) == 16) {
3800 if (N1.getValueType() != MVT::i32)
3801 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3802 if (N2.getValueType() != MVT::i32)
3803 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
3804 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3805 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3806 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3809 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3810 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3811 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3812 std::vector<SDOperand> MaskVec;
3813 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3814 for (unsigned i = 1; i <= 3; ++i)
3815 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3816 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3817 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3818 &MaskVec[0], MaskVec.size()));
3820 // Use two pinsrw instructions to insert a 32 bit value.
3822 if (MVT::isFloatingPoint(N1.getValueType())) {
3823 if (ISD::isNON_EXTLoad(N1.Val)) {
3824 // Just load directly from f32mem to GR32.
3825 LoadSDNode *LD = cast<LoadSDNode>(N1);
3826 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3827 LD->getSrcValue(), LD->getSrcValueOffset());
3829 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3830 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3831 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3832 DAG.getConstant(0, getPointerTy()));
3835 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3836 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3837 DAG.getConstant(Idx, getPointerTy()));
3838 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3839 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3840 DAG.getConstant(Idx+1, getPointerTy()));
3841 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3849 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3850 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3851 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3854 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3855 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3856 // one of the above mentioned nodes. It has to be wrapped because otherwise
3857 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3858 // be used to form addressing mode. These wrapped nodes will be selected
3861 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3862 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3863 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3865 CP->getAlignment());
3866 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3867 // With PIC, the address is actually $g + Offset.
3868 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3869 !Subtarget->isPICStyleRIPRel()) {
3870 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3871 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3879 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3880 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3881 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3882 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3883 // With PIC, the address is actually $g + Offset.
3884 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3885 !Subtarget->isPICStyleRIPRel()) {
3886 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3887 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3891 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3892 // load the value at address GV, not the value of GV itself. This means that
3893 // the GlobalAddress must be in the base or index register of the address, not
3894 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3895 // The same applies for external symbols during PIC codegen
3896 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3897 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3903 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3904 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3905 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3906 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3907 // With PIC, the address is actually $g + Offset.
3908 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3909 !Subtarget->isPICStyleRIPRel()) {
3910 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3911 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3918 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3919 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3920 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3921 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3922 // With PIC, the address is actually $g + Offset.
3923 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3924 !Subtarget->isPICStyleRIPRel()) {
3925 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3926 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3933 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3934 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3935 "Not an i64 shift!");
3936 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3937 SDOperand ShOpLo = Op.getOperand(0);
3938 SDOperand ShOpHi = Op.getOperand(1);
3939 SDOperand ShAmt = Op.getOperand(2);
3940 SDOperand Tmp1 = isSRA ?
3941 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3942 DAG.getConstant(0, MVT::i32);
3944 SDOperand Tmp2, Tmp3;
3945 if (Op.getOpcode() == ISD::SHL_PARTS) {
3946 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3947 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3949 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3950 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3953 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3954 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3955 DAG.getConstant(32, MVT::i8));
3956 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3957 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3960 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3962 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3963 SmallVector<SDOperand, 4> Ops;
3964 if (Op.getOpcode() == ISD::SHL_PARTS) {
3965 Ops.push_back(Tmp2);
3966 Ops.push_back(Tmp3);
3968 Ops.push_back(InFlag);
3969 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3970 InFlag = Hi.getValue(1);
3973 Ops.push_back(Tmp3);
3974 Ops.push_back(Tmp1);
3976 Ops.push_back(InFlag);
3977 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3979 Ops.push_back(Tmp2);
3980 Ops.push_back(Tmp3);
3982 Ops.push_back(InFlag);
3983 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3984 InFlag = Lo.getValue(1);
3987 Ops.push_back(Tmp3);
3988 Ops.push_back(Tmp1);
3990 Ops.push_back(InFlag);
3991 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3994 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3998 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
4001 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4002 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4003 Op.getOperand(0).getValueType() >= MVT::i16 &&
4004 "Unknown SINT_TO_FP to lower!");
4007 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4008 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4009 MachineFunction &MF = DAG.getMachineFunction();
4010 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4011 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4012 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
4013 StackSlot, NULL, 0);
4016 std::vector<MVT::ValueType> Tys;
4017 Tys.push_back(MVT::f64);
4018 Tys.push_back(MVT::Other);
4019 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
4020 std::vector<SDOperand> Ops;
4021 Ops.push_back(Chain);
4022 Ops.push_back(StackSlot);
4023 Ops.push_back(DAG.getValueType(SrcVT));
4024 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
4025 Tys, &Ops[0], Ops.size());
4028 Chain = Result.getValue(1);
4029 SDOperand InFlag = Result.getValue(2);
4031 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4032 // shouldn't be necessary except that RFP cannot be live across
4033 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4034 MachineFunction &MF = DAG.getMachineFunction();
4035 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4036 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4037 std::vector<MVT::ValueType> Tys;
4038 Tys.push_back(MVT::Other);
4039 std::vector<SDOperand> Ops;
4040 Ops.push_back(Chain);
4041 Ops.push_back(Result);
4042 Ops.push_back(StackSlot);
4043 Ops.push_back(DAG.getValueType(Op.getValueType()));
4044 Ops.push_back(InFlag);
4045 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
4046 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
4052 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
4053 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4054 "Unknown FP_TO_SINT to lower!");
4055 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4057 MachineFunction &MF = DAG.getMachineFunction();
4058 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4059 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4060 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4063 switch (Op.getValueType()) {
4064 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4065 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4066 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4067 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
4070 SDOperand Chain = DAG.getEntryNode();
4071 SDOperand Value = Op.getOperand(0);
4073 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
4074 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
4075 std::vector<MVT::ValueType> Tys;
4076 Tys.push_back(MVT::f64);
4077 Tys.push_back(MVT::Other);
4078 std::vector<SDOperand> Ops;
4079 Ops.push_back(Chain);
4080 Ops.push_back(StackSlot);
4081 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
4082 Value = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
4083 Chain = Value.getValue(1);
4084 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4085 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4088 // Build the FP_TO_INT*_IN_MEM
4089 std::vector<SDOperand> Ops;
4090 Ops.push_back(Chain);
4091 Ops.push_back(Value);
4092 Ops.push_back(StackSlot);
4093 SDOperand FIST = DAG.getNode(Opc, MVT::Other, &Ops[0], Ops.size());
4096 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4099 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4100 MVT::ValueType VT = Op.getValueType();
4101 const Type *OpNTy = MVT::getTypeForValueType(VT);
4102 std::vector<Constant*> CV;
4103 if (VT == MVT::f64) {
4104 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
4105 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4107 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
4108 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4109 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4110 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4112 Constant *CS = ConstantStruct::get(CV);
4113 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
4114 std::vector<MVT::ValueType> Tys;
4116 Tys.push_back(MVT::Other);
4117 SmallVector<SDOperand, 3> Ops;
4118 Ops.push_back(DAG.getEntryNode());
4119 Ops.push_back(CPIdx);
4120 Ops.push_back(DAG.getSrcValue(NULL));
4121 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
4122 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4125 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4126 MVT::ValueType VT = Op.getValueType();
4127 const Type *OpNTy = MVT::getTypeForValueType(VT);
4128 std::vector<Constant*> CV;
4129 if (VT == MVT::f64) {
4130 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
4131 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4133 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
4134 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4135 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4136 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4138 Constant *CS = ConstantStruct::get(CV);
4139 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
4140 std::vector<MVT::ValueType> Tys;
4142 Tys.push_back(MVT::Other);
4143 SmallVector<SDOperand, 3> Ops;
4144 Ops.push_back(DAG.getEntryNode());
4145 Ops.push_back(CPIdx);
4146 Ops.push_back(DAG.getSrcValue(NULL));
4147 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
4148 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4151 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4152 SDOperand Op0 = Op.getOperand(0);
4153 SDOperand Op1 = Op.getOperand(1);
4154 MVT::ValueType VT = Op.getValueType();
4155 MVT::ValueType SrcVT = Op1.getValueType();
4156 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4158 // If second operand is smaller, extend it first.
4159 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4160 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4164 // First get the sign bit of second operand.
4165 std::vector<Constant*> CV;
4166 if (SrcVT == MVT::f64) {
4167 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
4168 CV.push_back(ConstantFP::get(SrcTy, 0.0));
4170 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
4171 CV.push_back(ConstantFP::get(SrcTy, 0.0));
4172 CV.push_back(ConstantFP::get(SrcTy, 0.0));
4173 CV.push_back(ConstantFP::get(SrcTy, 0.0));
4175 Constant *CS = ConstantStruct::get(CV);
4176 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
4177 std::vector<MVT::ValueType> Tys;
4178 Tys.push_back(SrcVT);
4179 Tys.push_back(MVT::Other);
4180 SmallVector<SDOperand, 3> Ops;
4181 Ops.push_back(DAG.getEntryNode());
4182 Ops.push_back(CPIdx);
4183 Ops.push_back(DAG.getSrcValue(NULL));
4184 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
4185 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4187 // Shift sign bit right or left if the two operands have different types.
4188 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4189 // Op0 is MVT::f32, Op1 is MVT::f64.
4190 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4191 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4192 DAG.getConstant(32, MVT::i32));
4193 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4194 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4195 DAG.getConstant(0, getPointerTy()));
4198 // Clear first operand sign bit.
4200 if (VT == MVT::f64) {
4201 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
4202 CV.push_back(ConstantFP::get(SrcTy, 0.0));
4204 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
4205 CV.push_back(ConstantFP::get(SrcTy, 0.0));
4206 CV.push_back(ConstantFP::get(SrcTy, 0.0));
4207 CV.push_back(ConstantFP::get(SrcTy, 0.0));
4209 CS = ConstantStruct::get(CV);
4210 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
4213 Tys.push_back(MVT::Other);
4215 Ops.push_back(DAG.getEntryNode());
4216 Ops.push_back(CPIdx);
4217 Ops.push_back(DAG.getSrcValue(NULL));
4218 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
4219 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4221 // Or the value with the sign bit.
4222 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4225 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
4227 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
4229 SDOperand Op0 = Op.getOperand(0);
4230 SDOperand Op1 = Op.getOperand(1);
4231 SDOperand CC = Op.getOperand(2);
4232 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4233 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4234 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4235 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4238 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
4240 SDOperand Ops1[] = { Chain, Op0, Op1 };
4241 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
4242 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4243 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
4246 assert(isFP && "Illegal integer SetCC!");
4248 SDOperand COps[] = { Chain, Op0, Op1 };
4249 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
4251 switch (SetCCOpcode) {
4252 default: assert(false && "Illegal floating point SetCC!");
4253 case ISD::SETOEQ: { // !PF & ZF
4254 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
4255 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
4256 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
4258 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
4259 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4261 case ISD::SETUNE: { // PF | !ZF
4262 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
4263 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
4264 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
4266 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
4267 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4272 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4273 bool addTest = true;
4274 SDOperand Chain = DAG.getEntryNode();
4275 SDOperand Cond = Op.getOperand(0);
4277 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4279 if (Cond.getOpcode() == ISD::SETCC)
4280 Cond = LowerSETCC(Cond, DAG, Chain);
4282 if (Cond.getOpcode() == X86ISD::SETCC) {
4283 CC = Cond.getOperand(0);
4285 // If condition flag is set by a X86ISD::CMP, then make a copy of it
4286 // (since flag operand cannot be shared). Use it as the condition setting
4287 // operand in place of the X86ISD::SETCC.
4288 // If the X86ISD::SETCC has more than one use, then perhaps it's better
4289 // to use a test instead of duplicating the X86ISD::CMP (for register
4290 // pressure reason)?
4291 SDOperand Cmp = Cond.getOperand(1);
4292 unsigned Opc = Cmp.getOpcode();
4293 bool IllegalFPCMov = !X86ScalarSSE &&
4294 MVT::isFloatingPoint(Op.getValueType()) &&
4295 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4296 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
4298 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
4299 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
4305 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4306 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
4307 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
4310 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
4311 SmallVector<SDOperand, 4> Ops;
4312 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4313 // condition is true.
4314 Ops.push_back(Op.getOperand(2));
4315 Ops.push_back(Op.getOperand(1));
4317 Ops.push_back(Cond.getValue(1));
4318 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
4321 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4322 bool addTest = true;
4323 SDOperand Chain = Op.getOperand(0);
4324 SDOperand Cond = Op.getOperand(1);
4325 SDOperand Dest = Op.getOperand(2);
4327 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4329 if (Cond.getOpcode() == ISD::SETCC)
4330 Cond = LowerSETCC(Cond, DAG, Chain);
4332 if (Cond.getOpcode() == X86ISD::SETCC) {
4333 CC = Cond.getOperand(0);
4335 // If condition flag is set by a X86ISD::CMP, then make a copy of it
4336 // (since flag operand cannot be shared). Use it as the condition setting
4337 // operand in place of the X86ISD::SETCC.
4338 // If the X86ISD::SETCC has more than one use, then perhaps it's better
4339 // to use a test instead of duplicating the X86ISD::CMP (for register
4340 // pressure reason)?
4341 SDOperand Cmp = Cond.getOperand(1);
4342 unsigned Opc = Cmp.getOpcode();
4343 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
4344 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
4345 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
4351 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4352 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
4353 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
4355 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
4356 Cond, Op.getOperand(2), CC, Cond.getValue(1));
4359 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
4360 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4362 if (Subtarget->is64Bit())
4363 return LowerX86_64CCCCallTo(Op, DAG);
4365 switch (CallingConv) {
4367 assert(0 && "Unsupported calling convention");
4368 case CallingConv::Fast:
4370 return LowerFastCCCallTo(Op, DAG, false);
4373 case CallingConv::C:
4374 case CallingConv::CSRet:
4375 return LowerCCCCallTo(Op, DAG);
4376 case CallingConv::X86_StdCall:
4377 return LowerStdCallCCCallTo(Op, DAG);
4378 case CallingConv::X86_FastCall:
4379 return LowerFastCCCallTo(Op, DAG, true);
4383 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
4386 switch(Op.getNumOperands()) {
4388 assert(0 && "Do not know how to return this many arguments!");
4390 case 1: // ret void.
4391 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
4392 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
4394 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
4396 if (MVT::isVector(ArgVT) ||
4397 (Subtarget->is64Bit() && MVT::isFloatingPoint(ArgVT))) {
4398 // Integer or FP vector result -> XMM0.
4399 if (DAG.getMachineFunction().liveout_empty())
4400 DAG.getMachineFunction().addLiveOut(X86::XMM0);
4401 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
4403 } else if (MVT::isInteger(ArgVT)) {
4404 // Integer result -> EAX / RAX.
4405 // The C calling convention guarantees the return value has been
4406 // promoted to at least MVT::i32. The X86-64 ABI doesn't require the
4407 // value to be promoted MVT::i64. So we don't have to extend it to
4408 // 64-bit. Return the value in EAX, but mark RAX as liveout.
4409 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
4410 if (DAG.getMachineFunction().liveout_empty())
4411 DAG.getMachineFunction().addLiveOut(Reg);
4413 Reg = (ArgVT == MVT::i64) ? X86::RAX : X86::EAX;
4414 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg, Op.getOperand(1),
4416 } else if (!X86ScalarSSE) {
4417 // FP return with fp-stack value.
4418 if (DAG.getMachineFunction().liveout_empty())
4419 DAG.getMachineFunction().addLiveOut(X86::ST0);
4421 std::vector<MVT::ValueType> Tys;
4422 Tys.push_back(MVT::Other);
4423 Tys.push_back(MVT::Flag);
4424 std::vector<SDOperand> Ops;
4425 Ops.push_back(Op.getOperand(0));
4426 Ops.push_back(Op.getOperand(1));
4427 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
4429 // FP return with ScalarSSE (return on fp-stack).
4430 if (DAG.getMachineFunction().liveout_empty())
4431 DAG.getMachineFunction().addLiveOut(X86::ST0);
4434 SDOperand Chain = Op.getOperand(0);
4435 SDOperand Value = Op.getOperand(1);
4437 if (ISD::isNON_EXTLoad(Value.Val) &&
4438 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
4439 Chain = Value.getOperand(0);
4440 MemLoc = Value.getOperand(1);
4442 // Spill the value to memory and reload it into top of stack.
4443 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
4444 MachineFunction &MF = DAG.getMachineFunction();
4445 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4446 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
4447 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
4449 std::vector<MVT::ValueType> Tys;
4450 Tys.push_back(MVT::f64);
4451 Tys.push_back(MVT::Other);
4452 std::vector<SDOperand> Ops;
4453 Ops.push_back(Chain);
4454 Ops.push_back(MemLoc);
4455 Ops.push_back(DAG.getValueType(ArgVT));
4456 Copy = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
4458 Tys.push_back(MVT::Other);
4459 Tys.push_back(MVT::Flag);
4461 Ops.push_back(Copy.getValue(1));
4462 Ops.push_back(Copy);
4463 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
4468 unsigned Reg1 = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
4469 unsigned Reg2 = Subtarget->is64Bit() ? X86::RDX : X86::EDX;
4470 if (DAG.getMachineFunction().liveout_empty()) {
4471 DAG.getMachineFunction().addLiveOut(Reg1);
4472 DAG.getMachineFunction().addLiveOut(Reg2);
4475 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg2, Op.getOperand(3),
4477 Copy = DAG.getCopyToReg(Copy, Reg1, Op.getOperand(1), Copy.getValue(1));
4481 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
4482 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
4487 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
4488 MachineFunction &MF = DAG.getMachineFunction();
4489 const Function* Fn = MF.getFunction();
4490 if (Fn->hasExternalLinkage() &&
4491 Subtarget->isTargetCygMing() &&
4492 Fn->getName() == "main")
4493 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
4495 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4496 if (Subtarget->is64Bit())
4497 return LowerX86_64CCCArguments(Op, DAG);
4501 assert(0 && "Unsupported calling convention");
4502 case CallingConv::Fast:
4504 return LowerFastCCArguments(Op, DAG);
4507 case CallingConv::C:
4508 case CallingConv::CSRet:
4509 return LowerCCCArguments(Op, DAG);
4510 case CallingConv::X86_StdCall:
4511 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
4512 return LowerStdCallCCArguments(Op, DAG);
4513 case CallingConv::X86_FastCall:
4514 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
4515 return LowerFastCallCCArguments(Op, DAG);
4519 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4520 SDOperand InFlag(0, 0);
4521 SDOperand Chain = Op.getOperand(0);
4523 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4524 if (Align == 0) Align = 1;
4526 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4527 // If not DWORD aligned, call memset if size is less than the threshold.
4528 // It knows how to align to the right boundary first.
4529 if ((Align & 3) != 0 ||
4530 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4531 MVT::ValueType IntPtr = getPointerTy();
4532 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4533 TargetLowering::ArgListTy Args;
4534 TargetLowering::ArgListEntry Entry;
4535 Entry.Node = Op.getOperand(1);
4536 Entry.Ty = IntPtrTy;
4537 Entry.isSigned = false;
4538 Args.push_back(Entry);
4539 // Extend the unsigned i8 argument to be an int value for the call.
4540 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4541 Entry.Ty = IntPtrTy;
4542 Entry.isSigned = false;
4543 Args.push_back(Entry);
4544 Entry.Node = Op.getOperand(3);
4545 Args.push_back(Entry);
4546 std::pair<SDOperand,SDOperand> CallResult =
4547 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4548 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4549 return CallResult.second;
4554 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4555 unsigned BytesLeft = 0;
4556 bool TwoRepStos = false;
4559 uint64_t Val = ValC->getValue() & 255;
4561 // If the value is a constant, then we can potentially use larger sets.
4562 switch (Align & 3) {
4563 case 2: // WORD aligned
4566 Val = (Val << 8) | Val;
4568 case 0: // DWORD aligned
4571 Val = (Val << 8) | Val;
4572 Val = (Val << 16) | Val;
4573 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4576 Val = (Val << 32) | Val;
4579 default: // Byte aligned
4582 Count = Op.getOperand(3);
4586 if (AVT > MVT::i8) {
4588 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4589 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4590 BytesLeft = I->getValue() % UBytes;
4592 assert(AVT >= MVT::i32 &&
4593 "Do not use rep;stos if not at least DWORD aligned");
4594 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4595 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4600 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4602 InFlag = Chain.getValue(1);
4605 Count = Op.getOperand(3);
4606 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4607 InFlag = Chain.getValue(1);
4610 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4612 InFlag = Chain.getValue(1);
4613 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4614 Op.getOperand(1), InFlag);
4615 InFlag = Chain.getValue(1);
4617 std::vector<MVT::ValueType> Tys;
4618 Tys.push_back(MVT::Other);
4619 Tys.push_back(MVT::Flag);
4620 std::vector<SDOperand> Ops;
4621 Ops.push_back(Chain);
4622 Ops.push_back(DAG.getValueType(AVT));
4623 Ops.push_back(InFlag);
4624 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4627 InFlag = Chain.getValue(1);
4628 Count = Op.getOperand(3);
4629 MVT::ValueType CVT = Count.getValueType();
4630 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4631 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4632 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4634 InFlag = Chain.getValue(1);
4636 Tys.push_back(MVT::Other);
4637 Tys.push_back(MVT::Flag);
4639 Ops.push_back(Chain);
4640 Ops.push_back(DAG.getValueType(MVT::i8));
4641 Ops.push_back(InFlag);
4642 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4643 } else if (BytesLeft) {
4644 // Issue stores for the last 1 - 7 bytes.
4646 unsigned Val = ValC->getValue() & 255;
4647 unsigned Offset = I->getValue() - BytesLeft;
4648 SDOperand DstAddr = Op.getOperand(1);
4649 MVT::ValueType AddrVT = DstAddr.getValueType();
4650 if (BytesLeft >= 4) {
4651 Val = (Val << 8) | Val;
4652 Val = (Val << 16) | Val;
4653 Value = DAG.getConstant(Val, MVT::i32);
4654 Chain = DAG.getStore(Chain, Value,
4655 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4656 DAG.getConstant(Offset, AddrVT)),
4661 if (BytesLeft >= 2) {
4662 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4663 Chain = DAG.getStore(Chain, Value,
4664 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4665 DAG.getConstant(Offset, AddrVT)),
4670 if (BytesLeft == 1) {
4671 Value = DAG.getConstant(Val, MVT::i8);
4672 Chain = DAG.getStore(Chain, Value,
4673 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4674 DAG.getConstant(Offset, AddrVT)),
4682 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4683 SDOperand Chain = Op.getOperand(0);
4685 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4686 if (Align == 0) Align = 1;
4688 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4689 // If not DWORD aligned, call memcpy if size is less than the threshold.
4690 // It knows how to align to the right boundary first.
4691 if ((Align & 3) != 0 ||
4692 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4693 MVT::ValueType IntPtr = getPointerTy();
4694 TargetLowering::ArgListTy Args;
4695 TargetLowering::ArgListEntry Entry;
4696 Entry.Ty = getTargetData()->getIntPtrType(); Entry.isSigned = false;
4697 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
4698 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
4699 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
4700 std::pair<SDOperand,SDOperand> CallResult =
4701 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4702 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4703 return CallResult.second;
4708 unsigned BytesLeft = 0;
4709 bool TwoRepMovs = false;
4710 switch (Align & 3) {
4711 case 2: // WORD aligned
4714 case 0: // DWORD aligned
4716 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4719 default: // Byte aligned
4721 Count = Op.getOperand(3);
4725 if (AVT > MVT::i8) {
4727 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4728 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4729 BytesLeft = I->getValue() % UBytes;
4731 assert(AVT >= MVT::i32 &&
4732 "Do not use rep;movs if not at least DWORD aligned");
4733 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4734 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4739 SDOperand InFlag(0, 0);
4740 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4742 InFlag = Chain.getValue(1);
4743 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4744 Op.getOperand(1), InFlag);
4745 InFlag = Chain.getValue(1);
4746 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4747 Op.getOperand(2), InFlag);
4748 InFlag = Chain.getValue(1);
4750 std::vector<MVT::ValueType> Tys;
4751 Tys.push_back(MVT::Other);
4752 Tys.push_back(MVT::Flag);
4753 std::vector<SDOperand> Ops;
4754 Ops.push_back(Chain);
4755 Ops.push_back(DAG.getValueType(AVT));
4756 Ops.push_back(InFlag);
4757 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4760 InFlag = Chain.getValue(1);
4761 Count = Op.getOperand(3);
4762 MVT::ValueType CVT = Count.getValueType();
4763 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4764 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4765 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4767 InFlag = Chain.getValue(1);
4769 Tys.push_back(MVT::Other);
4770 Tys.push_back(MVT::Flag);
4772 Ops.push_back(Chain);
4773 Ops.push_back(DAG.getValueType(MVT::i8));
4774 Ops.push_back(InFlag);
4775 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4776 } else if (BytesLeft) {
4777 // Issue loads and stores for the last 1 - 7 bytes.
4778 unsigned Offset = I->getValue() - BytesLeft;
4779 SDOperand DstAddr = Op.getOperand(1);
4780 MVT::ValueType DstVT = DstAddr.getValueType();
4781 SDOperand SrcAddr = Op.getOperand(2);
4782 MVT::ValueType SrcVT = SrcAddr.getValueType();
4784 if (BytesLeft >= 4) {
4785 Value = DAG.getLoad(MVT::i32, Chain,
4786 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4787 DAG.getConstant(Offset, SrcVT)),
4789 Chain = Value.getValue(1);
4790 Chain = DAG.getStore(Chain, Value,
4791 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4792 DAG.getConstant(Offset, DstVT)),
4797 if (BytesLeft >= 2) {
4798 Value = DAG.getLoad(MVT::i16, Chain,
4799 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4800 DAG.getConstant(Offset, SrcVT)),
4802 Chain = Value.getValue(1);
4803 Chain = DAG.getStore(Chain, Value,
4804 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4805 DAG.getConstant(Offset, DstVT)),
4811 if (BytesLeft == 1) {
4812 Value = DAG.getLoad(MVT::i8, Chain,
4813 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4814 DAG.getConstant(Offset, SrcVT)),
4816 Chain = Value.getValue(1);
4817 Chain = DAG.getStore(Chain, Value,
4818 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4819 DAG.getConstant(Offset, DstVT)),
4828 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4829 std::vector<MVT::ValueType> Tys;
4830 Tys.push_back(MVT::Other);
4831 Tys.push_back(MVT::Flag);
4832 std::vector<SDOperand> Ops;
4833 Ops.push_back(Op.getOperand(0));
4834 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &Ops[0], Ops.size());
4836 if (Subtarget->is64Bit()) {
4837 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4838 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4839 MVT::i64, Copy1.getValue(2));
4840 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4841 DAG.getConstant(32, MVT::i8));
4842 Ops.push_back(DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp));
4843 Ops.push_back(Copy2.getValue(1));
4845 Tys[1] = MVT::Other;
4847 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4848 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4849 MVT::i32, Copy1.getValue(2));
4850 Ops.push_back(Copy1);
4851 Ops.push_back(Copy2);
4852 Ops.push_back(Copy2.getValue(1));
4853 Tys[0] = Tys[1] = MVT::i32;
4854 Tys.push_back(MVT::Other);
4856 return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size());
4859 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4860 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4862 if (!Subtarget->is64Bit()) {
4863 // vastart just stores the address of the VarArgsFrameIndex slot into the
4864 // memory location argument.
4865 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4866 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4871 // gp_offset (0 - 6 * 8)
4872 // fp_offset (48 - 48 + 8 * 16)
4873 // overflow_arg_area (point to parameters coming in memory).
4875 std::vector<SDOperand> MemOps;
4876 SDOperand FIN = Op.getOperand(1);
4878 SDOperand Store = DAG.getStore(Op.getOperand(0),
4879 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4880 FIN, SV->getValue(), SV->getOffset());
4881 MemOps.push_back(Store);
4884 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4885 DAG.getConstant(4, getPointerTy()));
4886 Store = DAG.getStore(Op.getOperand(0),
4887 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4888 FIN, SV->getValue(), SV->getOffset());
4889 MemOps.push_back(Store);
4891 // Store ptr to overflow_arg_area
4892 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4893 DAG.getConstant(4, getPointerTy()));
4894 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4895 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4897 MemOps.push_back(Store);
4899 // Store ptr to reg_save_area.
4900 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4901 DAG.getConstant(8, getPointerTy()));
4902 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4903 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4905 MemOps.push_back(Store);
4906 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4910 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4911 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4913 default: return SDOperand(); // Don't custom lower most intrinsics.
4914 // Comparison intrinsics.
4915 case Intrinsic::x86_sse_comieq_ss:
4916 case Intrinsic::x86_sse_comilt_ss:
4917 case Intrinsic::x86_sse_comile_ss:
4918 case Intrinsic::x86_sse_comigt_ss:
4919 case Intrinsic::x86_sse_comige_ss:
4920 case Intrinsic::x86_sse_comineq_ss:
4921 case Intrinsic::x86_sse_ucomieq_ss:
4922 case Intrinsic::x86_sse_ucomilt_ss:
4923 case Intrinsic::x86_sse_ucomile_ss:
4924 case Intrinsic::x86_sse_ucomigt_ss:
4925 case Intrinsic::x86_sse_ucomige_ss:
4926 case Intrinsic::x86_sse_ucomineq_ss:
4927 case Intrinsic::x86_sse2_comieq_sd:
4928 case Intrinsic::x86_sse2_comilt_sd:
4929 case Intrinsic::x86_sse2_comile_sd:
4930 case Intrinsic::x86_sse2_comigt_sd:
4931 case Intrinsic::x86_sse2_comige_sd:
4932 case Intrinsic::x86_sse2_comineq_sd:
4933 case Intrinsic::x86_sse2_ucomieq_sd:
4934 case Intrinsic::x86_sse2_ucomilt_sd:
4935 case Intrinsic::x86_sse2_ucomile_sd:
4936 case Intrinsic::x86_sse2_ucomigt_sd:
4937 case Intrinsic::x86_sse2_ucomige_sd:
4938 case Intrinsic::x86_sse2_ucomineq_sd: {
4940 ISD::CondCode CC = ISD::SETCC_INVALID;
4943 case Intrinsic::x86_sse_comieq_ss:
4944 case Intrinsic::x86_sse2_comieq_sd:
4948 case Intrinsic::x86_sse_comilt_ss:
4949 case Intrinsic::x86_sse2_comilt_sd:
4953 case Intrinsic::x86_sse_comile_ss:
4954 case Intrinsic::x86_sse2_comile_sd:
4958 case Intrinsic::x86_sse_comigt_ss:
4959 case Intrinsic::x86_sse2_comigt_sd:
4963 case Intrinsic::x86_sse_comige_ss:
4964 case Intrinsic::x86_sse2_comige_sd:
4968 case Intrinsic::x86_sse_comineq_ss:
4969 case Intrinsic::x86_sse2_comineq_sd:
4973 case Intrinsic::x86_sse_ucomieq_ss:
4974 case Intrinsic::x86_sse2_ucomieq_sd:
4975 Opc = X86ISD::UCOMI;
4978 case Intrinsic::x86_sse_ucomilt_ss:
4979 case Intrinsic::x86_sse2_ucomilt_sd:
4980 Opc = X86ISD::UCOMI;
4983 case Intrinsic::x86_sse_ucomile_ss:
4984 case Intrinsic::x86_sse2_ucomile_sd:
4985 Opc = X86ISD::UCOMI;
4988 case Intrinsic::x86_sse_ucomigt_ss:
4989 case Intrinsic::x86_sse2_ucomigt_sd:
4990 Opc = X86ISD::UCOMI;
4993 case Intrinsic::x86_sse_ucomige_ss:
4994 case Intrinsic::x86_sse2_ucomige_sd:
4995 Opc = X86ISD::UCOMI;
4998 case Intrinsic::x86_sse_ucomineq_ss:
4999 case Intrinsic::x86_sse2_ucomineq_sd:
5000 Opc = X86ISD::UCOMI;
5006 SDOperand LHS = Op.getOperand(1);
5007 SDOperand RHS = Op.getOperand(2);
5008 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5010 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
5011 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
5012 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
5013 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
5014 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
5015 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
5016 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
5021 /// LowerOperation - Provide custom lowering hooks for some operations.
5023 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5024 switch (Op.getOpcode()) {
5025 default: assert(0 && "Should not custom lower this!");
5026 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5027 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5028 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5029 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5030 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5031 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5032 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5033 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5034 case ISD::SHL_PARTS:
5035 case ISD::SRA_PARTS:
5036 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5037 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5038 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5039 case ISD::FABS: return LowerFABS(Op, DAG);
5040 case ISD::FNEG: return LowerFNEG(Op, DAG);
5041 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
5042 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
5043 case ISD::SELECT: return LowerSELECT(Op, DAG);
5044 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
5045 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5046 case ISD::CALL: return LowerCALL(Op, DAG);
5047 case ISD::RET: return LowerRET(Op, DAG);
5048 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5049 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5050 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
5051 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
5052 case ISD::VASTART: return LowerVASTART(Op, DAG);
5053 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5057 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5059 default: return NULL;
5060 case X86ISD::SHLD: return "X86ISD::SHLD";
5061 case X86ISD::SHRD: return "X86ISD::SHRD";
5062 case X86ISD::FAND: return "X86ISD::FAND";
5063 case X86ISD::FOR: return "X86ISD::FOR";
5064 case X86ISD::FXOR: return "X86ISD::FXOR";
5065 case X86ISD::FSRL: return "X86ISD::FSRL";
5066 case X86ISD::FILD: return "X86ISD::FILD";
5067 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5068 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5069 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5070 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5071 case X86ISD::FLD: return "X86ISD::FLD";
5072 case X86ISD::FST: return "X86ISD::FST";
5073 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
5074 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5075 case X86ISD::CALL: return "X86ISD::CALL";
5076 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5077 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5078 case X86ISD::CMP: return "X86ISD::CMP";
5079 case X86ISD::COMI: return "X86ISD::COMI";
5080 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5081 case X86ISD::SETCC: return "X86ISD::SETCC";
5082 case X86ISD::CMOV: return "X86ISD::CMOV";
5083 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5084 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5085 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5086 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
5087 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
5088 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
5089 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5090 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5091 case X86ISD::S2VEC: return "X86ISD::S2VEC";
5092 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5093 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5094 case X86ISD::FMAX: return "X86ISD::FMAX";
5095 case X86ISD::FMIN: return "X86ISD::FMIN";
5099 /// isLegalAddressImmediate - Return true if the integer value or
5100 /// GlobalValue can be used as the offset of the target addressing mode.
5101 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
5102 // X86 allows a sign-extended 32-bit immediate field.
5103 return (V > -(1LL << 32) && V < (1LL << 32)-1);
5106 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
5107 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement
5108 // field unless we are in small code model.
5109 if (Subtarget->is64Bit() &&
5110 getTargetMachine().getCodeModel() != CodeModel::Small)
5113 return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false));
5116 /// isShuffleMaskLegal - Targets can use this to indicate that they only
5117 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5118 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5119 /// are assumed to be legal.
5121 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5122 // Only do shuffles on 128-bit vector types for now.
5123 if (MVT::getSizeInBits(VT) == 64) return false;
5124 return (Mask.Val->getNumOperands() <= 4 ||
5125 isSplatMask(Mask.Val) ||
5126 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5127 X86::isUNPCKLMask(Mask.Val) ||
5128 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5129 X86::isUNPCKHMask(Mask.Val));
5132 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5134 SelectionDAG &DAG) const {
5135 unsigned NumElts = BVOps.size();
5136 // Only do shuffles on 128-bit vector types for now.
5137 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5138 if (NumElts == 2) return true;
5140 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
5141 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
5146 //===----------------------------------------------------------------------===//
5147 // X86 Scheduler Hooks
5148 //===----------------------------------------------------------------------===//
5151 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5152 MachineBasicBlock *BB) {
5153 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5154 switch (MI->getOpcode()) {
5155 default: assert(false && "Unexpected instr type to insert");
5156 case X86::CMOV_FR32:
5157 case X86::CMOV_FR64:
5158 case X86::CMOV_V4F32:
5159 case X86::CMOV_V2F64:
5160 case X86::CMOV_V2I64: {
5161 // To "insert" a SELECT_CC instruction, we actually have to insert the
5162 // diamond control-flow pattern. The incoming instruction knows the
5163 // destination vreg to set, the condition code register to branch on, the
5164 // true/false values to select between, and a branch opcode to use.
5165 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5166 ilist<MachineBasicBlock>::iterator It = BB;
5172 // cmpTY ccX, r1, r2
5174 // fallthrough --> copy0MBB
5175 MachineBasicBlock *thisMBB = BB;
5176 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5177 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5179 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5180 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5181 MachineFunction *F = BB->getParent();
5182 F->getBasicBlockList().insert(It, copy0MBB);
5183 F->getBasicBlockList().insert(It, sinkMBB);
5184 // Update machine-CFG edges by first adding all successors of the current
5185 // block to the new block which will contain the Phi node for the select.
5186 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5187 e = BB->succ_end(); i != e; ++i)
5188 sinkMBB->addSuccessor(*i);
5189 // Next, remove all successors of the current block, and add the true
5190 // and fallthrough blocks as its successors.
5191 while(!BB->succ_empty())
5192 BB->removeSuccessor(BB->succ_begin());
5193 BB->addSuccessor(copy0MBB);
5194 BB->addSuccessor(sinkMBB);
5197 // %FalseValue = ...
5198 // # fallthrough to sinkMBB
5201 // Update machine-CFG edges
5202 BB->addSuccessor(sinkMBB);
5205 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5208 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5209 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5210 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5212 delete MI; // The pseudo instruction is gone now.
5216 case X86::FP_TO_INT16_IN_MEM:
5217 case X86::FP_TO_INT32_IN_MEM:
5218 case X86::FP_TO_INT64_IN_MEM: {
5219 // Change the floating point control register to use "round towards zero"
5220 // mode when truncating to an integer value.
5221 MachineFunction *F = BB->getParent();
5222 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5223 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5225 // Load the old value of the high byte of the control word...
5227 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
5228 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5230 // Set the high part to be round to zero...
5231 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5234 // Reload the modified control word now...
5235 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5237 // Restore the memory image of control word to original value
5238 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5241 // Get the X86 opcode to use.
5243 switch (MI->getOpcode()) {
5244 default: assert(0 && "illegal opcode!");
5245 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
5246 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
5247 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
5251 MachineOperand &Op = MI->getOperand(0);
5252 if (Op.isRegister()) {
5253 AM.BaseType = X86AddressMode::RegBase;
5254 AM.Base.Reg = Op.getReg();
5256 AM.BaseType = X86AddressMode::FrameIndexBase;
5257 AM.Base.FrameIndex = Op.getFrameIndex();
5259 Op = MI->getOperand(1);
5260 if (Op.isImmediate())
5261 AM.Scale = Op.getImm();
5262 Op = MI->getOperand(2);
5263 if (Op.isImmediate())
5264 AM.IndexReg = Op.getImm();
5265 Op = MI->getOperand(3);
5266 if (Op.isGlobalAddress()) {
5267 AM.GV = Op.getGlobal();
5269 AM.Disp = Op.getImm();
5271 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5272 .addReg(MI->getOperand(4).getReg());
5274 // Reload the original control word now.
5275 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5277 delete MI; // The pseudo instruction is gone now.
5283 //===----------------------------------------------------------------------===//
5284 // X86 Optimization Hooks
5285 //===----------------------------------------------------------------------===//
5287 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5289 uint64_t &KnownZero,
5291 unsigned Depth) const {
5292 unsigned Opc = Op.getOpcode();
5293 assert((Opc >= ISD::BUILTIN_OP_END ||
5294 Opc == ISD::INTRINSIC_WO_CHAIN ||
5295 Opc == ISD::INTRINSIC_W_CHAIN ||
5296 Opc == ISD::INTRINSIC_VOID) &&
5297 "Should use MaskedValueIsZero if you don't know whether Op"
5298 " is a target node!");
5300 KnownZero = KnownOne = 0; // Don't know anything.
5304 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5309 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
5310 /// element of the result of the vector shuffle.
5311 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5312 MVT::ValueType VT = N->getValueType(0);
5313 SDOperand PermMask = N->getOperand(2);
5314 unsigned NumElems = PermMask.getNumOperands();
5315 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5317 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5319 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
5320 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5321 SDOperand Idx = PermMask.getOperand(i);
5322 if (Idx.getOpcode() == ISD::UNDEF)
5323 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
5324 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5329 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5330 /// node is a GlobalAddress + an offset.
5331 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5332 unsigned Opc = N->getOpcode();
5333 if (Opc == X86ISD::Wrapper) {
5334 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5335 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5338 } else if (Opc == ISD::ADD) {
5339 SDOperand N1 = N->getOperand(0);
5340 SDOperand N2 = N->getOperand(1);
5341 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5342 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5344 Offset += V->getSignExtended();
5347 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5348 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5350 Offset += V->getSignExtended();
5358 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
5360 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5361 MachineFrameInfo *MFI) {
5362 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5365 SDOperand Loc = N->getOperand(1);
5366 SDOperand BaseLoc = Base->getOperand(1);
5367 if (Loc.getOpcode() == ISD::FrameIndex) {
5368 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5370 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
5371 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5372 int FS = MFI->getObjectSize(FI);
5373 int BFS = MFI->getObjectSize(BFI);
5374 if (FS != BFS || FS != Size) return false;
5375 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5377 GlobalValue *GV1 = NULL;
5378 GlobalValue *GV2 = NULL;
5379 int64_t Offset1 = 0;
5380 int64_t Offset2 = 0;
5381 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5382 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5383 if (isGA1 && isGA2 && GV1 == GV2)
5384 return Offset1 == (Offset2 + Dist*Size);
5390 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5391 const X86Subtarget *Subtarget) {
5394 if (isGAPlusOffset(Base, GV, Offset))
5395 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5397 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
5398 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
5400 // Fixed objects do not specify alignment, however the offsets are known.
5401 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5402 (MFI->getObjectOffset(BFI) % 16) == 0);
5404 return MFI->getObjectAlignment(BFI) >= 16;
5410 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5411 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5412 /// if the load addresses are consecutive, non-overlapping, and in the right
5414 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5415 const X86Subtarget *Subtarget) {
5416 MachineFunction &MF = DAG.getMachineFunction();
5417 MachineFrameInfo *MFI = MF.getFrameInfo();
5418 MVT::ValueType VT = N->getValueType(0);
5419 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
5420 SDOperand PermMask = N->getOperand(2);
5421 int NumElems = (int)PermMask.getNumOperands();
5422 SDNode *Base = NULL;
5423 for (int i = 0; i < NumElems; ++i) {
5424 SDOperand Idx = PermMask.getOperand(i);
5425 if (Idx.getOpcode() == ISD::UNDEF) {
5426 if (!Base) return SDOperand();
5429 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5430 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5434 else if (!isConsecutiveLoad(Arg.Val, Base,
5435 i, MVT::getSizeInBits(EVT)/8,MFI))
5440 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
5442 LoadSDNode *LD = cast<LoadSDNode>(Base);
5443 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5444 LD->getSrcValueOffset());
5446 // Just use movups, it's shorter.
5447 std::vector<MVT::ValueType> Tys;
5448 Tys.push_back(MVT::v4f32);
5449 Tys.push_back(MVT::Other);
5450 SmallVector<SDOperand, 3> Ops;
5451 Ops.push_back(Base->getOperand(0));
5452 Ops.push_back(Base->getOperand(1));
5453 Ops.push_back(Base->getOperand(2));
5454 return DAG.getNode(ISD::BIT_CONVERT, VT,
5455 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
5459 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5460 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5461 const X86Subtarget *Subtarget) {
5462 SDOperand Cond = N->getOperand(0);
5464 // If we have SSE[12] support, try to form min/max nodes.
5465 if (Subtarget->hasSSE2() &&
5466 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5467 if (Cond.getOpcode() == ISD::SETCC) {
5468 // Get the LHS/RHS of the select.
5469 SDOperand LHS = N->getOperand(1);
5470 SDOperand RHS = N->getOperand(2);
5471 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5473 unsigned Opcode = 0;
5474 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5477 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5480 if (!UnsafeFPMath) break;
5482 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5484 Opcode = X86ISD::FMIN;
5487 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5490 if (!UnsafeFPMath) break;
5492 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5494 Opcode = X86ISD::FMAX;
5497 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5500 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5503 if (!UnsafeFPMath) break;
5505 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5507 Opcode = X86ISD::FMIN;
5510 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5513 if (!UnsafeFPMath) break;
5515 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5517 Opcode = X86ISD::FMAX;
5523 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5532 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5533 DAGCombinerInfo &DCI) const {
5534 SelectionDAG &DAG = DCI.DAG;
5535 switch (N->getOpcode()) {
5537 case ISD::VECTOR_SHUFFLE:
5538 return PerformShuffleCombine(N, DAG, Subtarget);
5540 return PerformSELECTCombine(N, DAG, Subtarget);
5546 //===----------------------------------------------------------------------===//
5547 // X86 Inline Assembly Support
5548 //===----------------------------------------------------------------------===//
5550 /// getConstraintType - Given a constraint letter, return the type of
5551 /// constraint it is for this target.
5552 X86TargetLowering::ConstraintType
5553 X86TargetLowering::getConstraintType(char ConstraintLetter) const {
5554 switch (ConstraintLetter) {
5563 return C_RegisterClass;
5564 default: return TargetLowering::getConstraintType(ConstraintLetter);
5568 /// isOperandValidForConstraint - Return the specified operand (possibly
5569 /// modified) if the specified SDOperand is valid for the specified target
5570 /// constraint letter, otherwise return null.
5571 SDOperand X86TargetLowering::
5572 isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
5573 switch (Constraint) {
5576 // Literal immediates are always ok.
5577 if (isa<ConstantSDNode>(Op)) return Op;
5579 // If we are in non-pic codegen mode, we allow the address of a global to
5580 // be used with 'i'.
5581 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
5582 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
5583 return SDOperand(0, 0);
5585 if (GA->getOpcode() != ISD::TargetGlobalAddress)
5586 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5591 // Otherwise, not valid for this mode.
5592 return SDOperand(0, 0);
5594 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
5598 std::vector<unsigned> X86TargetLowering::
5599 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5600 MVT::ValueType VT) const {
5601 if (Constraint.size() == 1) {
5602 // FIXME: not handling fp-stack yet!
5603 // FIXME: not handling MMX registers yet ('y' constraint).
5604 switch (Constraint[0]) { // GCC X86 Constraint Letters
5605 default: break; // Unknown constraint letter
5606 case 'A': // EAX/EDX
5607 if (VT == MVT::i32 || VT == MVT::i64)
5608 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5610 case 'r': // GENERAL_REGS
5611 case 'R': // LEGACY_REGS
5612 if (VT == MVT::i64 && Subtarget->is64Bit())
5613 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
5614 X86::RSI, X86::RDI, X86::RBP, X86::RSP,
5615 X86::R8, X86::R9, X86::R10, X86::R11,
5616 X86::R12, X86::R13, X86::R14, X86::R15, 0);
5618 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5619 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
5620 else if (VT == MVT::i16)
5621 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
5622 X86::SI, X86::DI, X86::BP, X86::SP, 0);
5623 else if (VT == MVT::i8)
5624 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
5626 case 'l': // INDEX_REGS
5628 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5629 X86::ESI, X86::EDI, X86::EBP, 0);
5630 else if (VT == MVT::i16)
5631 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
5632 X86::SI, X86::DI, X86::BP, 0);
5633 else if (VT == MVT::i8)
5634 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5636 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5639 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5640 else if (VT == MVT::i16)
5641 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5642 else if (VT == MVT::i8)
5643 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5645 case 'x': // SSE_REGS if SSE1 allowed
5646 if (Subtarget->hasSSE1())
5647 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5648 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5650 return std::vector<unsigned>();
5651 case 'Y': // SSE_REGS if SSE2 allowed
5652 if (Subtarget->hasSSE2())
5653 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5654 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5656 return std::vector<unsigned>();
5660 return std::vector<unsigned>();
5663 std::pair<unsigned, const TargetRegisterClass*>
5664 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5665 MVT::ValueType VT) const {
5666 // Use the default implementation in TargetLowering to convert the register
5667 // constraint into a member of a register class.
5668 std::pair<unsigned, const TargetRegisterClass*> Res;
5669 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5671 // Not found as a standard register?
5672 if (Res.second == 0) {
5673 // GCC calls "st(0)" just plain "st".
5674 if (StringsEqualNoCase("{st}", Constraint)) {
5675 Res.first = X86::ST0;
5676 Res.second = X86::RSTRegisterClass;
5682 // Otherwise, check to see if this is a register class of the wrong value
5683 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5684 // turn into {ax},{dx}.
5685 if (Res.second->hasType(VT))
5686 return Res; // Correct type already, nothing to do.
5688 // All of the single-register GCC register classes map their values onto
5689 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5690 // really want an 8-bit or 32-bit register, map to the appropriate register
5691 // class and return the appropriate register.
5692 if (Res.second != X86::GR16RegisterClass)
5695 if (VT == MVT::i8) {
5696 unsigned DestReg = 0;
5697 switch (Res.first) {
5699 case X86::AX: DestReg = X86::AL; break;
5700 case X86::DX: DestReg = X86::DL; break;
5701 case X86::CX: DestReg = X86::CL; break;
5702 case X86::BX: DestReg = X86::BL; break;
5705 Res.first = DestReg;
5706 Res.second = Res.second = X86::GR8RegisterClass;
5708 } else if (VT == MVT::i32) {
5709 unsigned DestReg = 0;
5710 switch (Res.first) {
5712 case X86::AX: DestReg = X86::EAX; break;
5713 case X86::DX: DestReg = X86::EDX; break;
5714 case X86::CX: DestReg = X86::ECX; break;
5715 case X86::BX: DestReg = X86::EBX; break;
5716 case X86::SI: DestReg = X86::ESI; break;
5717 case X86::DI: DestReg = X86::EDI; break;
5718 case X86::BP: DestReg = X86::EBP; break;
5719 case X86::SP: DestReg = X86::ESP; break;
5722 Res.first = DestReg;
5723 Res.second = Res.second = X86::GR32RegisterClass;
5725 } else if (VT == MVT::i64) {
5726 unsigned DestReg = 0;
5727 switch (Res.first) {
5729 case X86::AX: DestReg = X86::RAX; break;
5730 case X86::DX: DestReg = X86::RDX; break;
5731 case X86::CX: DestReg = X86::RCX; break;
5732 case X86::BX: DestReg = X86::RBX; break;
5733 case X86::SI: DestReg = X86::RSI; break;
5734 case X86::DI: DestReg = X86::RDI; break;
5735 case X86::BP: DestReg = X86::RBP; break;
5736 case X86::SP: DestReg = X86::RSP; break;
5739 Res.first = DestReg;
5740 Res.second = Res.second = X86::GR64RegisterClass;