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"
38 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
39 cl::desc("Enable fastcc on X86"));
40 static cl::opt<bool> NoShuffleOpti("disable-x86-shuffle-opti", cl::Hidden,
41 cl::desc("Disable vector shuffle optimizations on X86"));
43 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
44 : TargetLowering(TM) {
45 Subtarget = &TM.getSubtarget<X86Subtarget>();
46 X86ScalarSSE = Subtarget->hasSSE2();
47 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
49 // Set up the TargetLowering object.
51 // X86 is weird, it always uses i8 for shift amounts and setcc results.
52 setShiftAmountType(MVT::i8);
53 setSetCCResultType(MVT::i8);
54 setSetCCResultContents(ZeroOrOneSetCCResult);
55 setSchedulingPreference(SchedulingForRegPressure);
56 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
57 setStackPointerRegisterToSaveRestore(X86StackPtr);
59 if (!Subtarget->isTargetDarwin())
60 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
61 setUseUnderscoreSetJmpLongJmp(true);
63 // Add legal addressing mode scale values.
64 addLegalAddressScale(8);
65 addLegalAddressScale(4);
66 addLegalAddressScale(2);
67 // Enter the ones which require both scale + index last. These are more
69 addLegalAddressScale(9);
70 addLegalAddressScale(5);
71 addLegalAddressScale(3);
73 // Set up the register classes.
74 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
75 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
76 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
77 if (Subtarget->is64Bit())
78 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
80 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
82 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
84 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
85 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
86 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
88 if (Subtarget->is64Bit()) {
89 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
90 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
93 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
94 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
96 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
99 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
101 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
102 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
103 // SSE has no i16 to fp conversion, only i32
105 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
107 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
108 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
111 if (!Subtarget->is64Bit()) {
112 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
113 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
114 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
117 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
119 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
120 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
123 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
125 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
126 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
129 // Handle FP_TO_UINT by promoting the destination to a larger signed
131 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
132 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
133 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
135 if (Subtarget->is64Bit()) {
136 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
137 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
139 if (X86ScalarSSE && !Subtarget->hasSSE3())
140 // Expand FP_TO_UINT into a select.
141 // FIXME: We would like to use a Custom expander here eventually to do
142 // the optimal thing for SSE vs. the default expansion in the legalizer.
143 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
145 // With SSE3 we can use fisttpll to convert to a signed i64.
146 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
149 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
150 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
152 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
153 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
154 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
155 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
156 if (Subtarget->is64Bit())
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
160 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
161 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
162 setOperationAction(ISD::FREM , MVT::f64 , Expand);
164 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
165 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
166 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
167 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
168 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
169 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
170 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
171 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
172 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
173 if (Subtarget->is64Bit()) {
174 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
175 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
176 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
179 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
180 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
182 // These should be promoted to a larger select which is supported.
183 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
184 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
185 // X86 wants to expand cmov itself.
186 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
187 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
188 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
189 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
190 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
191 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
192 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
193 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
194 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
195 if (Subtarget->is64Bit()) {
196 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
197 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
199 // X86 ret instruction may pop stack.
200 setOperationAction(ISD::RET , MVT::Other, Custom);
202 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
203 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
204 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
205 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
206 if (Subtarget->is64Bit()) {
207 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
208 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
209 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
210 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
212 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
213 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
214 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
215 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
216 // X86 wants to expand memset / memcpy itself.
217 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
218 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
220 // We don't have line number support yet.
221 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
222 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
223 // FIXME - use subtarget debug flags
224 if (!Subtarget->isTargetDarwin())
225 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
227 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
228 setOperationAction(ISD::VASTART , MVT::Other, Custom);
230 // Use the default implementation.
231 setOperationAction(ISD::VAARG , MVT::Other, Expand);
232 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
233 setOperationAction(ISD::VAEND , MVT::Other, Expand);
234 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
235 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
236 if (Subtarget->is64Bit())
237 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
238 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
240 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
241 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
244 // Set up the FP register classes.
245 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
246 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
248 // Use ANDPD to simulate FABS.
249 setOperationAction(ISD::FABS , MVT::f64, Custom);
250 setOperationAction(ISD::FABS , MVT::f32, Custom);
252 // Use XORP to simulate FNEG.
253 setOperationAction(ISD::FNEG , MVT::f64, Custom);
254 setOperationAction(ISD::FNEG , MVT::f32, Custom);
256 // We don't support sin/cos/fmod
257 setOperationAction(ISD::FSIN , MVT::f64, Expand);
258 setOperationAction(ISD::FCOS , MVT::f64, Expand);
259 setOperationAction(ISD::FREM , MVT::f64, Expand);
260 setOperationAction(ISD::FSIN , MVT::f32, Expand);
261 setOperationAction(ISD::FCOS , MVT::f32, Expand);
262 setOperationAction(ISD::FREM , MVT::f32, Expand);
264 // Expand FP immediates into loads from the stack, except for the special
266 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
267 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
268 addLegalFPImmediate(+0.0); // xorps / xorpd
270 // Set up the FP register classes.
271 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
273 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
276 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
277 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
280 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
281 addLegalFPImmediate(+0.0); // FLD0
282 addLegalFPImmediate(+1.0); // FLD1
283 addLegalFPImmediate(-0.0); // FLD0/FCHS
284 addLegalFPImmediate(-1.0); // FLD1/FCHS
287 // First set operation action for all vector types to expand. Then we
288 // will selectively turn on ones that can be effectively codegen'd.
289 for (unsigned VT = (unsigned)MVT::Vector + 1;
290 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
291 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
292 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
293 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
294 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
295 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
296 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
297 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
300 if (Subtarget->hasMMX()) {
301 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
302 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
303 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
305 // FIXME: add MMX packed arithmetics
306 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
307 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
308 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
311 if (Subtarget->hasSSE1()) {
312 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
314 setOperationAction(ISD::AND, MVT::v4f32, Legal);
315 setOperationAction(ISD::OR, MVT::v4f32, Legal);
316 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
317 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
318 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
319 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
320 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
321 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
322 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
323 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
324 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
327 if (Subtarget->hasSSE2()) {
328 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
329 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
330 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
331 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
332 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
334 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
335 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
336 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
337 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
338 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
339 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
340 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
341 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
342 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
343 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
345 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
346 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
347 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
348 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
349 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
350 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
352 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
353 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
354 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
355 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
356 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
358 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
359 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
360 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
361 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
362 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
363 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
365 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
366 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
367 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
368 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
369 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
370 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
371 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
372 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
373 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
374 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
375 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
376 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
379 // Custom lower v2i64 and v2f64 selects.
380 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
381 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
382 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
383 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
386 // We want to custom lower some of our intrinsics.
387 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
389 // We have target-specific dag combine patterns for the following nodes:
390 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
391 setTargetDAGCombine(ISD::SELECT);
393 computeRegisterProperties();
395 // FIXME: These should be based on subtarget info. Plus, the values should
396 // be smaller when we are in optimizing for size mode.
397 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
398 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
399 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
400 allowUnalignedMemoryAccesses = true; // x86 supports it!
403 //===----------------------------------------------------------------------===//
404 // C Calling Convention implementation
405 //===----------------------------------------------------------------------===//
407 /// AddLiveIn - This helper function adds the specified physical register to the
408 /// MachineFunction as a live in value. It also creates a corresponding virtual
410 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
411 TargetRegisterClass *RC) {
412 assert(RC->contains(PReg) && "Not the correct regclass!");
413 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
414 MF.addLiveIn(PReg, VReg);
418 /// HowToPassCCCArgument - Returns how an formal argument of the specified type
419 /// should be passed. If it is through stack, returns the size of the stack
420 /// slot; if it is through XMM register, returns the number of XMM registers
423 HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
424 unsigned &ObjSize, unsigned &ObjXMMRegs) {
428 default: assert(0 && "Unhandled argument type!");
429 case MVT::i8: ObjSize = 1; break;
430 case MVT::i16: ObjSize = 2; break;
431 case MVT::i32: ObjSize = 4; break;
432 case MVT::i64: ObjSize = 8; break;
433 case MVT::f32: ObjSize = 4; break;
434 case MVT::f64: ObjSize = 8; break;
449 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
450 unsigned NumArgs = Op.Val->getNumValues() - 1;
451 MachineFunction &MF = DAG.getMachineFunction();
452 MachineFrameInfo *MFI = MF.getFrameInfo();
453 SDOperand Root = Op.getOperand(0);
454 std::vector<SDOperand> ArgValues;
456 // Add DAG nodes to load the arguments... On entry to a function on the X86,
457 // the stack frame looks like this:
459 // [ESP] -- return address
460 // [ESP + 4] -- first argument (leftmost lexically)
461 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
464 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
465 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
466 static const unsigned XMMArgRegs[] = {
467 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
469 for (unsigned i = 0; i < NumArgs; ++i) {
470 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
471 unsigned ArgIncrement = 4;
472 unsigned ObjSize = 0;
473 unsigned ObjXMMRegs = 0;
474 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
476 ArgIncrement = ObjSize;
480 // Passed in a XMM register.
481 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
482 X86::VR128RegisterClass);
483 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
484 ArgValues.push_back(ArgValue);
485 NumXMMRegs += ObjXMMRegs;
487 // XMM arguments have to be aligned on 16-byte boundary.
489 ArgOffset = ((ArgOffset + 15) / 16) * 16;
490 // Create the frame index object for this incoming parameter...
491 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
492 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
493 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
494 ArgValues.push_back(ArgValue);
495 ArgOffset += ArgIncrement; // Move on to the next argument...
499 ArgValues.push_back(Root);
501 // If the function takes variable number of arguments, make a frame index for
502 // the start of the first vararg value... for expansion of llvm.va_start.
503 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
505 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
506 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
507 ReturnAddrIndex = 0; // No return address slot generated yet.
508 BytesToPopOnReturn = 0; // Callee pops nothing.
509 BytesCallerReserves = ArgOffset;
511 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
513 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
514 Subtarget->isTargetDarwin())
515 BytesToPopOnReturn = 4;
517 // Return the new list of results.
518 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
519 Op.Val->value_end());
520 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
524 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
525 SDOperand Chain = Op.getOperand(0);
526 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
527 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
528 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
529 SDOperand Callee = Op.getOperand(4);
530 MVT::ValueType RetVT= Op.Val->getValueType(0);
531 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
533 // Keep track of the number of XMM regs passed so far.
534 unsigned NumXMMRegs = 0;
535 static const unsigned XMMArgRegs[] = {
536 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
539 // Count how many bytes are to be pushed on the stack.
540 unsigned NumBytes = 0;
541 for (unsigned i = 0; i != NumOps; ++i) {
542 SDOperand Arg = Op.getOperand(5+2*i);
544 switch (Arg.getValueType()) {
545 default: assert(0 && "Unexpected ValueType for argument!");
565 // XMM arguments have to be aligned on 16-byte boundary.
566 NumBytes = ((NumBytes + 15) / 16) * 16;
573 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
575 // Arguments go on the stack in reverse order, as specified by the ABI.
576 unsigned ArgOffset = 0;
578 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
579 std::vector<SDOperand> MemOpChains;
580 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
581 for (unsigned i = 0; i != NumOps; ++i) {
582 SDOperand Arg = Op.getOperand(5+2*i);
584 switch (Arg.getValueType()) {
585 default: assert(0 && "Unexpected ValueType for argument!");
588 // Promote the integer to 32 bits. If the input type is signed use a
589 // sign extend, otherwise use a zero extend.
591 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
592 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
593 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
599 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
600 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
601 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
602 DAG.getSrcValue(NULL)));
608 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
609 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
610 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
611 DAG.getSrcValue(NULL)));
621 if (NumXMMRegs < 4) {
622 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
625 // XMM arguments have to be aligned on 16-byte boundary.
626 ArgOffset = ((ArgOffset + 15) / 16) * 16;
627 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
628 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
629 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
630 DAG.getSrcValue(NULL)));
636 if (!MemOpChains.empty())
637 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
638 &MemOpChains[0], MemOpChains.size());
640 // Build a sequence of copy-to-reg nodes chained together with token chain
641 // and flag operands which copy the outgoing args into registers.
643 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
644 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
646 InFlag = Chain.getValue(1);
649 // If the callee is a GlobalAddress node (quite common, every direct call is)
650 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
651 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
652 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
653 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
654 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
656 std::vector<MVT::ValueType> NodeTys;
657 NodeTys.push_back(MVT::Other); // Returns a chain
658 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
659 std::vector<SDOperand> Ops;
660 Ops.push_back(Chain);
661 Ops.push_back(Callee);
663 // Add argument registers to the end of the list so that they are known live
665 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
666 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
667 RegsToPass[i].second.getValueType()));
670 Ops.push_back(InFlag);
672 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
673 NodeTys, &Ops[0], Ops.size());
674 InFlag = Chain.getValue(1);
676 // Create the CALLSEQ_END node.
677 unsigned NumBytesForCalleeToPush = 0;
679 // If this is is a call to a struct-return function on Darwin/X86, the callee
680 // pops the hidden struct pointer, so we have to push it back.
681 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
682 NumBytesForCalleeToPush = 4;
685 NodeTys.push_back(MVT::Other); // Returns a chain
686 if (RetVT != MVT::Other)
687 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
689 Ops.push_back(Chain);
690 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
691 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
692 Ops.push_back(InFlag);
693 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
694 if (RetVT != MVT::Other)
695 InFlag = Chain.getValue(1);
697 std::vector<SDOperand> ResultVals;
700 default: assert(0 && "Unknown value type to return!");
701 case MVT::Other: break;
703 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
704 ResultVals.push_back(Chain.getValue(0));
705 NodeTys.push_back(MVT::i8);
708 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
709 ResultVals.push_back(Chain.getValue(0));
710 NodeTys.push_back(MVT::i16);
713 if (Op.Val->getValueType(1) == MVT::i32) {
714 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
715 ResultVals.push_back(Chain.getValue(0));
716 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
717 Chain.getValue(2)).getValue(1);
718 ResultVals.push_back(Chain.getValue(0));
719 NodeTys.push_back(MVT::i32);
721 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
722 ResultVals.push_back(Chain.getValue(0));
724 NodeTys.push_back(MVT::i32);
732 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
733 ResultVals.push_back(Chain.getValue(0));
734 NodeTys.push_back(RetVT);
738 std::vector<MVT::ValueType> Tys;
739 Tys.push_back(MVT::f64);
740 Tys.push_back(MVT::Other);
741 Tys.push_back(MVT::Flag);
742 std::vector<SDOperand> Ops;
743 Ops.push_back(Chain);
744 Ops.push_back(InFlag);
745 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
746 &Ops[0], Ops.size());
747 Chain = RetVal.getValue(1);
748 InFlag = RetVal.getValue(2);
750 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
751 // shouldn't be necessary except that RFP cannot be live across
752 // multiple blocks. When stackifier is fixed, they can be uncoupled.
753 MachineFunction &MF = DAG.getMachineFunction();
754 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
755 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
757 Tys.push_back(MVT::Other);
759 Ops.push_back(Chain);
760 Ops.push_back(RetVal);
761 Ops.push_back(StackSlot);
762 Ops.push_back(DAG.getValueType(RetVT));
763 Ops.push_back(InFlag);
764 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
765 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
766 Chain = RetVal.getValue(1);
769 if (RetVT == MVT::f32 && !X86ScalarSSE)
770 // FIXME: we would really like to remember that this FP_ROUND
771 // operation is okay to eliminate if we allow excess FP precision.
772 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
773 ResultVals.push_back(RetVal);
774 NodeTys.push_back(RetVT);
779 // If the function returns void, just return the chain.
780 if (ResultVals.empty())
783 // Otherwise, merge everything together with a MERGE_VALUES node.
784 NodeTys.push_back(MVT::Other);
785 ResultVals.push_back(Chain);
786 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
787 &ResultVals[0], ResultVals.size());
788 return Res.getValue(Op.ResNo);
792 //===----------------------------------------------------------------------===//
793 // X86-64 C Calling Convention implementation
794 //===----------------------------------------------------------------------===//
796 /// HowToPassX86_64CCCArgument - Returns how an formal argument of the specified
797 /// type should be passed. If it is through stack, returns the size of the stack
798 /// slot; if it is through integer or XMM register, returns the number of
799 /// integer or XMM registers are needed.
801 HowToPassX86_64CCCArgument(MVT::ValueType ObjectVT,
802 unsigned NumIntRegs, unsigned NumXMMRegs,
803 unsigned &ObjSize, unsigned &ObjIntRegs,
804 unsigned &ObjXMMRegs) {
810 default: assert(0 && "Unhandled argument type!");
820 case MVT::i8: ObjSize = 1; break;
821 case MVT::i16: ObjSize = 2; break;
822 case MVT::i32: ObjSize = 4; break;
823 case MVT::i64: ObjSize = 8; break;
840 case MVT::f32: ObjSize = 4; break;
841 case MVT::f64: ObjSize = 8; break;
847 case MVT::v2f64: ObjSize = 16; break;
855 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
856 unsigned NumArgs = Op.Val->getNumValues() - 1;
857 MachineFunction &MF = DAG.getMachineFunction();
858 MachineFrameInfo *MFI = MF.getFrameInfo();
859 SDOperand Root = Op.getOperand(0);
860 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
861 std::vector<SDOperand> ArgValues;
863 // Add DAG nodes to load the arguments... On entry to a function on the X86,
864 // the stack frame looks like this:
866 // [RSP] -- return address
867 // [RSP + 8] -- first nonreg argument (leftmost lexically)
868 // [RSP +16] -- second nonreg argument, if 1st argument is <= 8 bytes in size
871 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
872 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
873 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
875 static const unsigned GPR8ArgRegs[] = {
876 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
878 static const unsigned GPR16ArgRegs[] = {
879 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
881 static const unsigned GPR32ArgRegs[] = {
882 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
884 static const unsigned GPR64ArgRegs[] = {
885 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
887 static const unsigned XMMArgRegs[] = {
888 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
889 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
892 for (unsigned i = 0; i < NumArgs; ++i) {
893 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
894 unsigned ArgIncrement = 8;
895 unsigned ObjSize = 0;
896 unsigned ObjIntRegs = 0;
897 unsigned ObjXMMRegs = 0;
899 // FIXME: __int128 and long double support?
900 HowToPassX86_64CCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
901 ObjSize, ObjIntRegs, ObjXMMRegs);
903 ArgIncrement = ObjSize;
907 if (ObjIntRegs || ObjXMMRegs) {
909 default: assert(0 && "Unhandled argument type!");
914 TargetRegisterClass *RC = NULL;
918 RC = X86::GR8RegisterClass;
919 Reg = GPR8ArgRegs[NumIntRegs];
922 RC = X86::GR16RegisterClass;
923 Reg = GPR16ArgRegs[NumIntRegs];
926 RC = X86::GR32RegisterClass;
927 Reg = GPR32ArgRegs[NumIntRegs];
930 RC = X86::GR64RegisterClass;
931 Reg = GPR64ArgRegs[NumIntRegs];
934 Reg = AddLiveIn(MF, Reg, RC);
935 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
946 TargetRegisterClass *RC= (ObjectVT == MVT::f32) ?
947 X86::FR32RegisterClass : ((ObjectVT == MVT::f64) ?
948 X86::FR64RegisterClass : X86::VR128RegisterClass);
949 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], RC);
950 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
954 NumIntRegs += ObjIntRegs;
955 NumXMMRegs += ObjXMMRegs;
956 } else if (ObjSize) {
957 // XMM arguments have to be aligned on 16-byte boundary.
959 ArgOffset = ((ArgOffset + 15) / 16) * 16;
960 // Create the SelectionDAG nodes corresponding to a load from this
962 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
963 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
964 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
965 ArgOffset += ArgIncrement; // Move on to the next argument.
968 ArgValues.push_back(ArgValue);
971 // If the function takes variable number of arguments, make a frame index for
972 // the start of the first vararg value... for expansion of llvm.va_start.
974 // For X86-64, if there are vararg parameters that are passed via
975 // registers, then we must store them to their spots on the stack so they
976 // may be loaded by deferencing the result of va_next.
977 VarArgsGPOffset = NumIntRegs * 8;
978 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
979 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
980 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
982 // Store the integer parameter registers.
983 std::vector<SDOperand> MemOps;
984 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
985 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
986 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
987 for (; NumIntRegs != 6; ++NumIntRegs) {
988 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
989 X86::GR64RegisterClass);
990 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
991 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
992 DAG.getSrcValue(NULL));
993 MemOps.push_back(Store);
994 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
995 DAG.getConstant(8, getPointerTy()));
998 // Now store the XMM (fp + vector) parameter registers.
999 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1000 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1001 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1002 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1003 X86::VR128RegisterClass);
1004 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1005 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
1006 DAG.getSrcValue(NULL));
1007 MemOps.push_back(Store);
1008 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1009 DAG.getConstant(16, getPointerTy()));
1011 if (!MemOps.empty())
1012 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1013 &MemOps[0], MemOps.size());
1016 ArgValues.push_back(Root);
1018 ReturnAddrIndex = 0; // No return address slot generated yet.
1019 BytesToPopOnReturn = 0; // Callee pops nothing.
1020 BytesCallerReserves = ArgOffset;
1022 // Return the new list of results.
1023 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1024 Op.Val->value_end());
1025 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1029 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG) {
1030 SDOperand Chain = Op.getOperand(0);
1031 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1032 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1033 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1034 SDOperand Callee = Op.getOperand(4);
1035 MVT::ValueType RetVT= Op.Val->getValueType(0);
1036 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1038 // Count how many bytes are to be pushed on the stack.
1039 unsigned NumBytes = 0;
1040 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
1041 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1043 static const unsigned GPR8ArgRegs[] = {
1044 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
1046 static const unsigned GPR16ArgRegs[] = {
1047 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
1049 static const unsigned GPR32ArgRegs[] = {
1050 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1052 static const unsigned GPR64ArgRegs[] = {
1053 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1055 static const unsigned XMMArgRegs[] = {
1056 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1057 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1060 for (unsigned i = 0; i != NumOps; ++i) {
1061 SDOperand Arg = Op.getOperand(5+2*i);
1062 MVT::ValueType ArgVT = Arg.getValueType();
1065 default: assert(0 && "Unknown value type!");
1085 else if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1088 // XMM arguments have to be aligned on 16-byte boundary.
1089 NumBytes = ((NumBytes + 15) / 16) * 16;
1096 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1098 // Arguments go on the stack in reverse order, as specified by the ABI.
1099 unsigned ArgOffset = 0;
1102 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1103 std::vector<SDOperand> MemOpChains;
1104 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1105 for (unsigned i = 0; i != NumOps; ++i) {
1106 SDOperand Arg = Op.getOperand(5+2*i);
1107 MVT::ValueType ArgVT = Arg.getValueType();
1110 default: assert(0 && "Unexpected ValueType for argument!");
1115 if (NumIntRegs < 6) {
1119 case MVT::i8: Reg = GPR8ArgRegs[NumIntRegs]; break;
1120 case MVT::i16: Reg = GPR16ArgRegs[NumIntRegs]; break;
1121 case MVT::i32: Reg = GPR32ArgRegs[NumIntRegs]; break;
1122 case MVT::i64: Reg = GPR64ArgRegs[NumIntRegs]; break;
1124 RegsToPass.push_back(std::make_pair(Reg, Arg));
1127 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1128 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1129 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
1130 DAG.getSrcValue(NULL)));
1142 if (NumXMMRegs < 8) {
1143 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1146 if (ArgVT != MVT::f32 && ArgVT != MVT::f64) {
1147 // XMM arguments have to be aligned on 16-byte boundary.
1148 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1150 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1151 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1152 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
1153 DAG.getSrcValue(NULL)));
1154 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1162 if (!MemOpChains.empty())
1163 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1164 &MemOpChains[0], MemOpChains.size());
1166 // Build a sequence of copy-to-reg nodes chained together with token chain
1167 // and flag operands which copy the outgoing args into registers.
1169 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1170 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1172 InFlag = Chain.getValue(1);
1176 // From AMD64 ABI document:
1177 // For calls that may call functions that use varargs or stdargs
1178 // (prototype-less calls or calls to functions containing ellipsis (...) in
1179 // the declaration) %al is used as hidden argument to specify the number
1180 // of SSE registers used. The contents of %al do not need to match exactly
1181 // the number of registers, but must be an ubound on the number of SSE
1182 // registers used and is in the range 0 - 8 inclusive.
1183 Chain = DAG.getCopyToReg(Chain, X86::AL,
1184 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1185 InFlag = Chain.getValue(1);
1188 // If the callee is a GlobalAddress node (quite common, every direct call is)
1189 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1190 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1191 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1192 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1193 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1195 std::vector<MVT::ValueType> NodeTys;
1196 NodeTys.push_back(MVT::Other); // Returns a chain
1197 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1198 std::vector<SDOperand> Ops;
1199 Ops.push_back(Chain);
1200 Ops.push_back(Callee);
1202 // Add argument registers to the end of the list so that they are known live
1204 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1205 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1206 RegsToPass[i].second.getValueType()));
1209 Ops.push_back(InFlag);
1211 // FIXME: Do not generate X86ISD::TAILCALL for now.
1212 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1213 NodeTys, &Ops[0], Ops.size());
1214 InFlag = Chain.getValue(1);
1217 NodeTys.push_back(MVT::Other); // Returns a chain
1218 if (RetVT != MVT::Other)
1219 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1221 Ops.push_back(Chain);
1222 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1223 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1224 Ops.push_back(InFlag);
1225 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1226 if (RetVT != MVT::Other)
1227 InFlag = Chain.getValue(1);
1229 std::vector<SDOperand> ResultVals;
1232 default: assert(0 && "Unknown value type to return!");
1233 case MVT::Other: break;
1235 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1236 ResultVals.push_back(Chain.getValue(0));
1237 NodeTys.push_back(MVT::i8);
1240 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1241 ResultVals.push_back(Chain.getValue(0));
1242 NodeTys.push_back(MVT::i16);
1245 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1246 ResultVals.push_back(Chain.getValue(0));
1247 NodeTys.push_back(MVT::i32);
1250 if (Op.Val->getValueType(1) == MVT::i64) {
1251 // FIXME: __int128 support?
1252 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1253 ResultVals.push_back(Chain.getValue(0));
1254 Chain = DAG.getCopyFromReg(Chain, X86::RDX, MVT::i64,
1255 Chain.getValue(2)).getValue(1);
1256 ResultVals.push_back(Chain.getValue(0));
1257 NodeTys.push_back(MVT::i64);
1259 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1260 ResultVals.push_back(Chain.getValue(0));
1262 NodeTys.push_back(MVT::i64);
1272 // FIXME: long double support?
1273 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1274 ResultVals.push_back(Chain.getValue(0));
1275 NodeTys.push_back(RetVT);
1279 // If the function returns void, just return the chain.
1280 if (ResultVals.empty())
1283 // Otherwise, merge everything together with a MERGE_VALUES node.
1284 NodeTys.push_back(MVT::Other);
1285 ResultVals.push_back(Chain);
1286 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1287 &ResultVals[0], ResultVals.size());
1288 return Res.getValue(Op.ResNo);
1291 //===----------------------------------------------------------------------===//
1292 // Fast Calling Convention implementation
1293 //===----------------------------------------------------------------------===//
1295 // The X86 'fast' calling convention passes up to two integer arguments in
1296 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
1297 // and requires that the callee pop its arguments off the stack (allowing proper
1298 // tail calls), and has the same return value conventions as C calling convs.
1300 // This calling convention always arranges for the callee pop value to be 8n+4
1301 // bytes, which is needed for tail recursion elimination and stack alignment
1304 // Note that this can be enhanced in the future to pass fp vals in registers
1305 // (when we have a global fp allocator) and do other tricks.
1308 /// HowToPassFastCCArgument - Returns how an formal argument of the specified
1309 /// type should be passed. If it is through stack, returns the size of the stack
1310 /// slot; if it is through integer or XMM register, returns the number of
1311 /// integer or XMM registers are needed.
1313 HowToPassFastCCArgument(MVT::ValueType ObjectVT,
1314 unsigned NumIntRegs, unsigned NumXMMRegs,
1315 unsigned &ObjSize, unsigned &ObjIntRegs,
1316 unsigned &ObjXMMRegs) {
1322 default: assert(0 && "Unhandled argument type!");
1324 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1325 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1332 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1333 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1340 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1341 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1348 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1349 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
1351 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
1378 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1379 unsigned NumArgs = Op.Val->getNumValues()-1;
1380 MachineFunction &MF = DAG.getMachineFunction();
1381 MachineFrameInfo *MFI = MF.getFrameInfo();
1382 SDOperand Root = Op.getOperand(0);
1383 std::vector<SDOperand> ArgValues;
1385 // Add DAG nodes to load the arguments... On entry to a function the stack
1386 // frame looks like this:
1388 // [ESP] -- return address
1389 // [ESP + 4] -- first nonreg argument (leftmost lexically)
1390 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
1392 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1394 // Keep track of the number of integer regs passed so far. This can be either
1395 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1397 unsigned NumIntRegs = 0;
1398 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1400 static const unsigned XMMArgRegs[] = {
1401 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1404 for (unsigned i = 0; i < NumArgs; ++i) {
1405 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1406 unsigned ArgIncrement = 4;
1407 unsigned ObjSize = 0;
1408 unsigned ObjIntRegs = 0;
1409 unsigned ObjXMMRegs = 0;
1411 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
1412 ObjSize, ObjIntRegs, ObjXMMRegs);
1414 ArgIncrement = ObjSize;
1418 if (ObjIntRegs || ObjXMMRegs) {
1420 default: assert(0 && "Unhandled argument type!");
1422 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
1423 X86::GR8RegisterClass);
1424 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
1427 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
1428 X86::GR16RegisterClass);
1429 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
1432 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
1433 X86::GR32RegisterClass);
1434 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1437 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
1438 X86::GR32RegisterClass);
1439 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1440 if (ObjIntRegs == 2) {
1441 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
1442 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1443 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
1452 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
1453 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1456 NumIntRegs += ObjIntRegs;
1457 NumXMMRegs += ObjXMMRegs;
1461 // XMM arguments have to be aligned on 16-byte boundary.
1463 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1464 // Create the SelectionDAG nodes corresponding to a load from this
1466 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1467 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1468 if (ObjectVT == MVT::i64 && ObjIntRegs) {
1469 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
1471 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
1473 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1474 ArgOffset += ArgIncrement; // Move on to the next argument.
1477 ArgValues.push_back(ArgValue);
1480 ArgValues.push_back(Root);
1482 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1483 // arguments and the arguments after the retaddr has been pushed are aligned.
1484 if ((ArgOffset & 7) == 0)
1487 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1488 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1489 ReturnAddrIndex = 0; // No return address slot generated yet.
1490 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1491 BytesCallerReserves = 0;
1493 // Finally, inform the code generator which regs we return values in.
1494 switch (getValueType(MF.getFunction()->getReturnType())) {
1495 default: assert(0 && "Unknown type!");
1496 case MVT::isVoid: break;
1501 MF.addLiveOut(X86::EAX);
1504 MF.addLiveOut(X86::EAX);
1505 MF.addLiveOut(X86::EDX);
1509 MF.addLiveOut(X86::ST0);
1517 MF.addLiveOut(X86::XMM0);
1521 // Return the new list of results.
1522 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1523 Op.Val->value_end());
1524 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1527 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1529 SDOperand Chain = Op.getOperand(0);
1530 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1531 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1532 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1533 SDOperand Callee = Op.getOperand(4);
1534 MVT::ValueType RetVT= Op.Val->getValueType(0);
1535 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1537 // Count how many bytes are to be pushed on the stack.
1538 unsigned NumBytes = 0;
1540 // Keep track of the number of integer regs passed so far. This can be either
1541 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1543 unsigned NumIntRegs = 0;
1544 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1546 static const unsigned GPRArgRegs[][2] = {
1547 { X86::AL, X86::DL },
1548 { X86::AX, X86::DX },
1549 { X86::EAX, X86::EDX }
1551 static const unsigned FastCallGPRArgRegs[][2] = {
1552 { X86::CL, X86::DL },
1553 { X86::CX, X86::DX },
1554 { X86::ECX, X86::EDX }
1556 static const unsigned XMMArgRegs[] = {
1557 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1560 for (unsigned i = 0; i != NumOps; ++i) {
1561 SDOperand Arg = Op.getOperand(5+2*i);
1563 switch (Arg.getValueType()) {
1564 default: assert(0 && "Unknown value type!");
1568 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1569 if (NumIntRegs < MaxNumIntRegs) {
1587 assert(0 && "Unknown value type!");
1592 // XMM arguments have to be aligned on 16-byte boundary.
1593 NumBytes = ((NumBytes + 15) / 16) * 16;
1601 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1602 // arguments and the arguments after the retaddr has been pushed are aligned.
1603 if ((NumBytes & 7) == 0)
1606 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1608 // Arguments go on the stack in reverse order, as specified by the ABI.
1609 unsigned ArgOffset = 0;
1611 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1612 std::vector<SDOperand> MemOpChains;
1613 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1614 for (unsigned i = 0; i != NumOps; ++i) {
1615 SDOperand Arg = Op.getOperand(5+2*i);
1617 switch (Arg.getValueType()) {
1618 default: assert(0 && "Unexpected ValueType for argument!");
1622 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1623 if (NumIntRegs < MaxNumIntRegs) {
1624 RegsToPass.push_back(
1625 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1632 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1633 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1634 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
1635 DAG.getSrcValue(NULL)));
1640 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1641 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1642 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
1643 DAG.getSrcValue(NULL)));
1654 assert(0 && "Unexpected ValueType for argument!");
1656 if (NumXMMRegs < 4) {
1657 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1660 // XMM arguments have to be aligned on 16-byte boundary.
1661 ArgOffset = ((ArgOffset + 15) / 16) * 16;
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,
1665 DAG.getSrcValue(NULL)));
1673 if (!MemOpChains.empty())
1674 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1675 &MemOpChains[0], MemOpChains.size());
1677 // Build a sequence of copy-to-reg nodes chained together with token chain
1678 // and flag operands which copy the outgoing args into registers.
1680 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1681 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1683 InFlag = Chain.getValue(1);
1686 // If the callee is a GlobalAddress node (quite common, every direct call is)
1687 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1688 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1689 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1690 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1691 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1693 std::vector<MVT::ValueType> NodeTys;
1694 NodeTys.push_back(MVT::Other); // Returns a chain
1695 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1696 std::vector<SDOperand> Ops;
1697 Ops.push_back(Chain);
1698 Ops.push_back(Callee);
1700 // Add argument registers to the end of the list so that they are known live
1702 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1703 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1704 RegsToPass[i].second.getValueType()));
1707 Ops.push_back(InFlag);
1709 // FIXME: Do not generate X86ISD::TAILCALL for now.
1710 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1711 NodeTys, &Ops[0], Ops.size());
1712 InFlag = Chain.getValue(1);
1715 NodeTys.push_back(MVT::Other); // Returns a chain
1716 if (RetVT != MVT::Other)
1717 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1719 Ops.push_back(Chain);
1720 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1721 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1722 Ops.push_back(InFlag);
1723 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1724 if (RetVT != MVT::Other)
1725 InFlag = Chain.getValue(1);
1727 std::vector<SDOperand> ResultVals;
1730 default: assert(0 && "Unknown value type to return!");
1731 case MVT::Other: break;
1733 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1734 ResultVals.push_back(Chain.getValue(0));
1735 NodeTys.push_back(MVT::i8);
1738 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1739 ResultVals.push_back(Chain.getValue(0));
1740 NodeTys.push_back(MVT::i16);
1743 if (Op.Val->getValueType(1) == MVT::i32) {
1744 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1745 ResultVals.push_back(Chain.getValue(0));
1746 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1747 Chain.getValue(2)).getValue(1);
1748 ResultVals.push_back(Chain.getValue(0));
1749 NodeTys.push_back(MVT::i32);
1751 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1752 ResultVals.push_back(Chain.getValue(0));
1754 NodeTys.push_back(MVT::i32);
1763 assert(0 && "Unknown value type to return!");
1765 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1766 ResultVals.push_back(Chain.getValue(0));
1767 NodeTys.push_back(RetVT);
1772 std::vector<MVT::ValueType> Tys;
1773 Tys.push_back(MVT::f64);
1774 Tys.push_back(MVT::Other);
1775 Tys.push_back(MVT::Flag);
1776 std::vector<SDOperand> Ops;
1777 Ops.push_back(Chain);
1778 Ops.push_back(InFlag);
1779 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
1780 &Ops[0], Ops.size());
1781 Chain = RetVal.getValue(1);
1782 InFlag = RetVal.getValue(2);
1784 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1785 // shouldn't be necessary except that RFP cannot be live across
1786 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1787 MachineFunction &MF = DAG.getMachineFunction();
1788 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1789 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1791 Tys.push_back(MVT::Other);
1793 Ops.push_back(Chain);
1794 Ops.push_back(RetVal);
1795 Ops.push_back(StackSlot);
1796 Ops.push_back(DAG.getValueType(RetVT));
1797 Ops.push_back(InFlag);
1798 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
1799 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
1800 Chain = RetVal.getValue(1);
1803 if (RetVT == MVT::f32 && !X86ScalarSSE)
1804 // FIXME: we would really like to remember that this FP_ROUND
1805 // operation is okay to eliminate if we allow excess FP precision.
1806 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1807 ResultVals.push_back(RetVal);
1808 NodeTys.push_back(RetVT);
1814 // If the function returns void, just return the chain.
1815 if (ResultVals.empty())
1818 // Otherwise, merge everything together with a MERGE_VALUES node.
1819 NodeTys.push_back(MVT::Other);
1820 ResultVals.push_back(Chain);
1821 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1822 &ResultVals[0], ResultVals.size());
1823 return Res.getValue(Op.ResNo);
1826 //===----------------------------------------------------------------------===//
1827 // StdCall Calling Convention implementation
1828 //===----------------------------------------------------------------------===//
1829 // StdCall calling convention seems to be standard for many Windows' API
1830 // routines and around. It differs from C calling convention just a little:
1831 // callee should clean up the stack, not caller. Symbols should be also
1832 // decorated in some fancy way :) It doesn't support any vector arguments.
1834 /// HowToPassStdCallCCArgument - Returns how an formal argument of the specified
1835 /// type should be passed. Returns the size of the stack slot
1837 HowToPassStdCallCCArgument(MVT::ValueType ObjectVT, unsigned &ObjSize) {
1839 default: assert(0 && "Unhandled argument type!");
1840 case MVT::i8: ObjSize = 1; break;
1841 case MVT::i16: ObjSize = 2; break;
1842 case MVT::i32: ObjSize = 4; break;
1843 case MVT::i64: ObjSize = 8; break;
1844 case MVT::f32: ObjSize = 4; break;
1845 case MVT::f64: ObjSize = 8; break;
1849 SDOperand X86TargetLowering::LowerStdCallCCArguments(SDOperand Op,
1850 SelectionDAG &DAG) {
1851 unsigned NumArgs = Op.Val->getNumValues() - 1;
1852 MachineFunction &MF = DAG.getMachineFunction();
1853 MachineFrameInfo *MFI = MF.getFrameInfo();
1854 SDOperand Root = Op.getOperand(0);
1855 std::vector<SDOperand> ArgValues;
1857 // Add DAG nodes to load the arguments... On entry to a function on the X86,
1858 // the stack frame looks like this:
1860 // [ESP] -- return address
1861 // [ESP + 4] -- first argument (leftmost lexically)
1862 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
1865 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1866 for (unsigned i = 0; i < NumArgs; ++i) {
1867 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1868 unsigned ArgIncrement = 4;
1869 unsigned ObjSize = 0;
1870 HowToPassStdCallCCArgument(ObjectVT, ObjSize);
1872 ArgIncrement = ObjSize;
1875 // Create the frame index object for this incoming parameter...
1876 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1877 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1878 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1879 ArgValues.push_back(ArgValue);
1880 ArgOffset += ArgIncrement; // Move on to the next argument...
1883 ArgValues.push_back(Root);
1885 // If the function takes variable number of arguments, make a frame index for
1886 // the start of the first vararg value... for expansion of llvm.va_start.
1887 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1889 BytesToPopOnReturn = 0; // Callee pops nothing.
1890 BytesCallerReserves = ArgOffset;
1891 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
1893 BytesToPopOnReturn = ArgOffset; // Callee pops everything..
1894 BytesCallerReserves = 0;
1896 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1897 ReturnAddrIndex = 0; // No return address slot generated yet.
1899 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
1901 // Return the new list of results.
1902 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1903 Op.Val->value_end());
1904 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1908 SDOperand X86TargetLowering::LowerStdCallCCCallTo(SDOperand Op,
1909 SelectionDAG &DAG) {
1910 SDOperand Chain = Op.getOperand(0);
1911 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1912 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1913 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1914 SDOperand Callee = Op.getOperand(4);
1915 MVT::ValueType RetVT= Op.Val->getValueType(0);
1916 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1918 // Count how many bytes are to be pushed on the stack.
1919 unsigned NumBytes = 0;
1920 for (unsigned i = 0; i != NumOps; ++i) {
1921 SDOperand Arg = Op.getOperand(5+2*i);
1923 switch (Arg.getValueType()) {
1924 default: assert(0 && "Unexpected ValueType for argument!");
1938 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1940 // Arguments go on the stack in reverse order, as specified by the ABI.
1941 unsigned ArgOffset = 0;
1942 std::vector<SDOperand> MemOpChains;
1943 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1944 for (unsigned i = 0; i != NumOps; ++i) {
1945 SDOperand Arg = Op.getOperand(5+2*i);
1947 switch (Arg.getValueType()) {
1948 default: assert(0 && "Unexpected ValueType for argument!");
1951 // Promote the integer to 32 bits. If the input type is signed use a
1952 // sign extend, otherwise use a zero extend.
1954 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
1955 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1956 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
1962 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1963 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1964 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
1965 DAG.getSrcValue(NULL)));
1971 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1972 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1973 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
1974 DAG.getSrcValue(NULL)));
1981 if (!MemOpChains.empty())
1982 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1983 &MemOpChains[0], MemOpChains.size());
1985 // If the callee is a GlobalAddress node (quite common, every direct call is)
1986 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1987 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1988 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1989 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1990 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1992 std::vector<MVT::ValueType> NodeTys;
1993 NodeTys.push_back(MVT::Other); // Returns a chain
1994 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1995 std::vector<SDOperand> Ops;
1996 Ops.push_back(Chain);
1997 Ops.push_back(Callee);
1999 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
2000 NodeTys, &Ops[0], Ops.size());
2001 SDOperand InFlag = Chain.getValue(1);
2003 // Create the CALLSEQ_END node.
2004 unsigned NumBytesForCalleeToPush;
2007 NumBytesForCalleeToPush = 0;
2009 NumBytesForCalleeToPush = NumBytes;
2013 NodeTys.push_back(MVT::Other); // Returns a chain
2014 if (RetVT != MVT::Other)
2015 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2017 Ops.push_back(Chain);
2018 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
2019 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
2020 Ops.push_back(InFlag);
2021 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2022 if (RetVT != MVT::Other)
2023 InFlag = Chain.getValue(1);
2025 std::vector<SDOperand> ResultVals;
2028 default: assert(0 && "Unknown value type to return!");
2029 case MVT::Other: break;
2031 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
2032 ResultVals.push_back(Chain.getValue(0));
2033 NodeTys.push_back(MVT::i8);
2036 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
2037 ResultVals.push_back(Chain.getValue(0));
2038 NodeTys.push_back(MVT::i16);
2041 if (Op.Val->getValueType(1) == MVT::i32) {
2042 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
2043 ResultVals.push_back(Chain.getValue(0));
2044 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
2045 Chain.getValue(2)).getValue(1);
2046 ResultVals.push_back(Chain.getValue(0));
2047 NodeTys.push_back(MVT::i32);
2049 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
2050 ResultVals.push_back(Chain.getValue(0));
2052 NodeTys.push_back(MVT::i32);
2056 std::vector<MVT::ValueType> Tys;
2057 Tys.push_back(MVT::f64);
2058 Tys.push_back(MVT::Other);
2059 Tys.push_back(MVT::Flag);
2060 std::vector<SDOperand> Ops;
2061 Ops.push_back(Chain);
2062 Ops.push_back(InFlag);
2063 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
2064 &Ops[0], Ops.size());
2065 Chain = RetVal.getValue(1);
2066 InFlag = RetVal.getValue(2);
2068 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
2069 // shouldn't be necessary except that RFP cannot be live across
2070 // multiple blocks. When stackifier is fixed, they can be uncoupled.
2071 MachineFunction &MF = DAG.getMachineFunction();
2072 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2073 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2075 Tys.push_back(MVT::Other);
2077 Ops.push_back(Chain);
2078 Ops.push_back(RetVal);
2079 Ops.push_back(StackSlot);
2080 Ops.push_back(DAG.getValueType(RetVT));
2081 Ops.push_back(InFlag);
2082 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
2083 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
2084 Chain = RetVal.getValue(1);
2087 if (RetVT == MVT::f32 && !X86ScalarSSE)
2088 // FIXME: we would really like to remember that this FP_ROUND
2089 // operation is okay to eliminate if we allow excess FP precision.
2090 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
2091 ResultVals.push_back(RetVal);
2092 NodeTys.push_back(RetVT);
2097 // If the function returns void, just return the chain.
2098 if (ResultVals.empty())
2101 // Otherwise, merge everything together with a MERGE_VALUES node.
2102 NodeTys.push_back(MVT::Other);
2103 ResultVals.push_back(Chain);
2104 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
2105 &ResultVals[0], ResultVals.size());
2106 return Res.getValue(Op.ResNo);
2109 //===----------------------------------------------------------------------===//
2110 // FastCall Calling Convention implementation
2111 //===----------------------------------------------------------------------===//
2113 // The X86 'fastcall' calling convention passes up to two integer arguments in
2114 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
2115 // and requires that the callee pop its arguments off the stack (allowing proper
2116 // tail calls), and has the same return value conventions as C calling convs.
2118 // This calling convention always arranges for the callee pop value to be 8n+4
2119 // bytes, which is needed for tail recursion elimination and stack alignment
2123 /// HowToPassFastCallCCArgument - Returns how an formal argument of the
2124 /// specified type should be passed. If it is through stack, returns the size of
2125 /// the stack slot; if it is through integer register, returns the number of
2126 /// integer registers are needed.
2128 HowToPassFastCallCCArgument(MVT::ValueType ObjectVT,
2129 unsigned NumIntRegs,
2131 unsigned &ObjIntRegs)
2137 default: assert(0 && "Unhandled argument type!");
2157 if (NumIntRegs+2 <= 2) {
2159 } else if (NumIntRegs+1 <= 2) {
2174 X86TargetLowering::LowerFastCallCCArguments(SDOperand Op, SelectionDAG &DAG) {
2175 unsigned NumArgs = Op.Val->getNumValues()-1;
2176 MachineFunction &MF = DAG.getMachineFunction();
2177 MachineFrameInfo *MFI = MF.getFrameInfo();
2178 SDOperand Root = Op.getOperand(0);
2179 std::vector<SDOperand> ArgValues;
2181 // Add DAG nodes to load the arguments... On entry to a function the stack
2182 // frame looks like this:
2184 // [ESP] -- return address
2185 // [ESP + 4] -- first nonreg argument (leftmost lexically)
2186 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
2188 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
2190 // Keep track of the number of integer regs passed so far. This can be either
2191 // 0 (neither ECX or EDX used), 1 (ECX is used) or 2 (ECX and EDX are both
2193 unsigned NumIntRegs = 0;
2195 for (unsigned i = 0; i < NumArgs; ++i) {
2196 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
2197 unsigned ArgIncrement = 4;
2198 unsigned ObjSize = 0;
2199 unsigned ObjIntRegs = 0;
2201 HowToPassFastCallCCArgument(ObjectVT, NumIntRegs, ObjSize, ObjIntRegs);
2203 ArgIncrement = ObjSize;
2209 default: assert(0 && "Unhandled argument type!");
2211 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::CL,
2212 X86::GR8RegisterClass);
2213 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
2216 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::CX,
2217 X86::GR16RegisterClass);
2218 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
2221 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::ECX,
2222 X86::GR32RegisterClass);
2223 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2226 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::ECX,
2227 X86::GR32RegisterClass);
2228 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2229 if (ObjIntRegs == 2) {
2230 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
2231 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2232 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
2237 NumIntRegs += ObjIntRegs;
2241 // Create the SelectionDAG nodes corresponding to a load from this
2243 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
2244 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
2245 if (ObjectVT == MVT::i64 && ObjIntRegs) {
2246 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
2248 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
2250 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
2251 ArgOffset += ArgIncrement; // Move on to the next argument.
2254 ArgValues.push_back(ArgValue);
2257 ArgValues.push_back(Root);
2259 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
2260 // arguments and the arguments after the retaddr has been pushed are aligned.
2261 if ((ArgOffset & 7) == 0)
2264 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
2265 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
2266 ReturnAddrIndex = 0; // No return address slot generated yet.
2267 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
2268 BytesCallerReserves = 0;
2270 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
2272 // Finally, inform the code generator which regs we return values in.
2273 switch (getValueType(MF.getFunction()->getReturnType())) {
2274 default: assert(0 && "Unknown type!");
2275 case MVT::isVoid: break;
2280 MF.addLiveOut(X86::ECX);
2283 MF.addLiveOut(X86::ECX);
2284 MF.addLiveOut(X86::EDX);
2288 MF.addLiveOut(X86::ST0);
2292 // Return the new list of results.
2293 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
2294 Op.Val->value_end());
2295 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
2298 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
2299 if (ReturnAddrIndex == 0) {
2300 // Set up a frame object for the return address.
2301 MachineFunction &MF = DAG.getMachineFunction();
2302 if (Subtarget->is64Bit())
2303 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2305 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
2308 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2313 std::pair<SDOperand, SDOperand> X86TargetLowering::
2314 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
2315 SelectionDAG &DAG) {
2317 if (Depth) // Depths > 0 not supported yet!
2318 Result = DAG.getConstant(0, getPointerTy());
2320 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
2321 if (!isFrameAddress)
2322 // Just load the return address
2323 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI,
2326 Result = DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
2327 DAG.getConstant(4, getPointerTy()));
2329 return std::make_pair(Result, Chain);
2332 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
2333 /// which corresponds to the condition code.
2334 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
2336 default: assert(0 && "Unknown X86 conditional code!");
2337 case X86ISD::COND_A: return X86::JA;
2338 case X86ISD::COND_AE: return X86::JAE;
2339 case X86ISD::COND_B: return X86::JB;
2340 case X86ISD::COND_BE: return X86::JBE;
2341 case X86ISD::COND_E: return X86::JE;
2342 case X86ISD::COND_G: return X86::JG;
2343 case X86ISD::COND_GE: return X86::JGE;
2344 case X86ISD::COND_L: return X86::JL;
2345 case X86ISD::COND_LE: return X86::JLE;
2346 case X86ISD::COND_NE: return X86::JNE;
2347 case X86ISD::COND_NO: return X86::JNO;
2348 case X86ISD::COND_NP: return X86::JNP;
2349 case X86ISD::COND_NS: return X86::JNS;
2350 case X86ISD::COND_O: return X86::JO;
2351 case X86ISD::COND_P: return X86::JP;
2352 case X86ISD::COND_S: return X86::JS;
2356 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2357 /// specific condition code. It returns a false if it cannot do a direct
2358 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2360 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2361 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2362 SelectionDAG &DAG) {
2363 X86CC = X86ISD::COND_INVALID;
2365 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2366 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2367 // X > -1 -> X == 0, jump !sign.
2368 RHS = DAG.getConstant(0, RHS.getValueType());
2369 X86CC = X86ISD::COND_NS;
2371 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2372 // X < 0 -> X == 0, jump on sign.
2373 X86CC = X86ISD::COND_S;
2378 switch (SetCCOpcode) {
2380 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
2381 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
2382 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
2383 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
2384 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
2385 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
2386 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
2387 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
2388 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
2389 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
2392 // On a floating point condition, the flags are set as follows:
2394 // 0 | 0 | 0 | X > Y
2395 // 0 | 0 | 1 | X < Y
2396 // 1 | 0 | 0 | X == Y
2397 // 1 | 1 | 1 | unordered
2399 switch (SetCCOpcode) {
2402 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
2403 case ISD::SETOLT: Flip = true; // Fallthrough
2405 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
2406 case ISD::SETOLE: Flip = true; // Fallthrough
2408 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
2409 case ISD::SETUGT: Flip = true; // Fallthrough
2411 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
2412 case ISD::SETUGE: Flip = true; // Fallthrough
2414 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
2416 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
2417 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
2418 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
2421 std::swap(LHS, RHS);
2424 return X86CC != X86ISD::COND_INVALID;
2427 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
2428 /// code. Current x86 isa includes the following FP cmov instructions:
2429 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2430 static bool hasFPCMov(unsigned X86CC) {
2434 case X86ISD::COND_B:
2435 case X86ISD::COND_BE:
2436 case X86ISD::COND_E:
2437 case X86ISD::COND_P:
2438 case X86ISD::COND_A:
2439 case X86ISD::COND_AE:
2440 case X86ISD::COND_NE:
2441 case X86ISD::COND_NP:
2446 /// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
2447 /// load. For Darwin, external and weak symbols are indirect, loading the value
2448 /// at address GV rather then the value of GV itself. This means that the
2449 /// GlobalAddress must be in the base or index register of the address, not the
2450 /// GV offset field.
2451 static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
2452 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
2453 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
2456 /// WindowsGVRequiresExtraLoad - true if accessing the GV requires an extra
2457 /// load. For Windows, dllimported symbols are indirect, loading the value at
2458 /// address GV rather then the value of GV itself. This means that the
2459 /// GlobalAddress must be in the base or index register of the address, not the
2460 /// GV offset field.
2461 static bool WindowsGVRequiresExtraLoad(GlobalValue *GV) {
2462 return (GV->hasDLLImportLinkage());
2465 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2466 /// true if Op is undef or if its value falls within the specified range (L, H].
2467 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2468 if (Op.getOpcode() == ISD::UNDEF)
2471 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2472 return (Val >= Low && Val < Hi);
2475 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2476 /// true if Op is undef or if its value equal to the specified value.
2477 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2478 if (Op.getOpcode() == ISD::UNDEF)
2480 return cast<ConstantSDNode>(Op)->getValue() == Val;
2483 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2484 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
2485 bool X86::isPSHUFDMask(SDNode *N) {
2486 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2488 if (N->getNumOperands() != 4)
2491 // Check if the value doesn't reference the second vector.
2492 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2493 SDOperand Arg = N->getOperand(i);
2494 if (Arg.getOpcode() == ISD::UNDEF) continue;
2495 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2496 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
2503 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2504 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2505 bool X86::isPSHUFHWMask(SDNode *N) {
2506 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2508 if (N->getNumOperands() != 8)
2511 // Lower quadword copied in order.
2512 for (unsigned i = 0; i != 4; ++i) {
2513 SDOperand Arg = N->getOperand(i);
2514 if (Arg.getOpcode() == ISD::UNDEF) continue;
2515 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2516 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2520 // Upper quadword shuffled.
2521 for (unsigned i = 4; i != 8; ++i) {
2522 SDOperand Arg = N->getOperand(i);
2523 if (Arg.getOpcode() == ISD::UNDEF) continue;
2524 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2525 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2526 if (Val < 4 || Val > 7)
2533 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2534 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2535 bool X86::isPSHUFLWMask(SDNode *N) {
2536 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2538 if (N->getNumOperands() != 8)
2541 // Upper quadword copied in order.
2542 for (unsigned i = 4; i != 8; ++i)
2543 if (!isUndefOrEqual(N->getOperand(i), i))
2546 // Lower quadword shuffled.
2547 for (unsigned i = 0; i != 4; ++i)
2548 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2554 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2555 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
2556 static bool isSHUFPMask(std::vector<SDOperand> &N) {
2557 unsigned NumElems = N.size();
2558 if (NumElems != 2 && NumElems != 4) return false;
2560 unsigned Half = NumElems / 2;
2561 for (unsigned i = 0; i < Half; ++i)
2562 if (!isUndefOrInRange(N[i], 0, NumElems))
2564 for (unsigned i = Half; i < NumElems; ++i)
2565 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
2571 bool X86::isSHUFPMask(SDNode *N) {
2572 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2573 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2574 return ::isSHUFPMask(Ops);
2577 /// isCommutedSHUFP - Returns true if the shuffle mask is except
2578 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2579 /// half elements to come from vector 1 (which would equal the dest.) and
2580 /// the upper half to come from vector 2.
2581 static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
2582 unsigned NumElems = Ops.size();
2583 if (NumElems != 2 && NumElems != 4) return false;
2585 unsigned Half = NumElems / 2;
2586 for (unsigned i = 0; i < Half; ++i)
2587 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
2589 for (unsigned i = Half; i < NumElems; ++i)
2590 if (!isUndefOrInRange(Ops[i], 0, NumElems))
2595 static bool isCommutedSHUFP(SDNode *N) {
2596 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2597 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2598 return isCommutedSHUFP(Ops);
2601 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2602 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2603 bool X86::isMOVHLPSMask(SDNode *N) {
2604 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2606 if (N->getNumOperands() != 4)
2609 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2610 return isUndefOrEqual(N->getOperand(0), 6) &&
2611 isUndefOrEqual(N->getOperand(1), 7) &&
2612 isUndefOrEqual(N->getOperand(2), 2) &&
2613 isUndefOrEqual(N->getOperand(3), 3);
2616 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2617 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2618 bool X86::isMOVLPMask(SDNode *N) {
2619 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2621 unsigned NumElems = N->getNumOperands();
2622 if (NumElems != 2 && NumElems != 4)
2625 for (unsigned i = 0; i < NumElems/2; ++i)
2626 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2629 for (unsigned i = NumElems/2; i < NumElems; ++i)
2630 if (!isUndefOrEqual(N->getOperand(i), i))
2636 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2637 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2639 bool X86::isMOVHPMask(SDNode *N) {
2640 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2642 unsigned NumElems = N->getNumOperands();
2643 if (NumElems != 2 && NumElems != 4)
2646 for (unsigned i = 0; i < NumElems/2; ++i)
2647 if (!isUndefOrEqual(N->getOperand(i), i))
2650 for (unsigned i = 0; i < NumElems/2; ++i) {
2651 SDOperand Arg = N->getOperand(i + NumElems/2);
2652 if (!isUndefOrEqual(Arg, i + NumElems))
2659 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2660 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
2661 bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2662 unsigned NumElems = N.size();
2663 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2666 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2667 SDOperand BitI = N[i];
2668 SDOperand BitI1 = N[i+1];
2669 if (!isUndefOrEqual(BitI, j))
2672 if (isUndefOrEqual(BitI1, NumElems))
2675 if (!isUndefOrEqual(BitI1, j + NumElems))
2683 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2684 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2685 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2686 return ::isUNPCKLMask(Ops, V2IsSplat);
2689 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2690 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
2691 bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2692 unsigned NumElems = N.size();
2693 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2696 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2697 SDOperand BitI = N[i];
2698 SDOperand BitI1 = N[i+1];
2699 if (!isUndefOrEqual(BitI, j + NumElems/2))
2702 if (isUndefOrEqual(BitI1, NumElems))
2705 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
2713 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2714 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2715 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2716 return ::isUNPCKHMask(Ops, V2IsSplat);
2719 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2720 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2722 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2723 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2725 unsigned NumElems = N->getNumOperands();
2726 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
2729 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2730 SDOperand BitI = N->getOperand(i);
2731 SDOperand BitI1 = N->getOperand(i+1);
2733 if (!isUndefOrEqual(BitI, j))
2735 if (!isUndefOrEqual(BitI1, j))
2742 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2743 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2744 /// MOVSD, and MOVD, i.e. setting the lowest element.
2745 static bool isMOVLMask(std::vector<SDOperand> &N) {
2746 unsigned NumElems = N.size();
2747 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2750 if (!isUndefOrEqual(N[0], NumElems))
2753 for (unsigned i = 1; i < NumElems; ++i) {
2754 SDOperand Arg = N[i];
2755 if (!isUndefOrEqual(Arg, i))
2762 bool X86::isMOVLMask(SDNode *N) {
2763 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2764 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2765 return ::isMOVLMask(Ops);
2768 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2769 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2770 /// element of vector 2 and the other elements to come from vector 1 in order.
2771 static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false,
2772 bool V2IsUndef = false) {
2773 unsigned NumElems = Ops.size();
2774 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2777 if (!isUndefOrEqual(Ops[0], 0))
2780 for (unsigned i = 1; i < NumElems; ++i) {
2781 SDOperand Arg = Ops[i];
2782 if (!(isUndefOrEqual(Arg, i+NumElems) ||
2783 (V2IsUndef && isUndefOrInRange(Arg, NumElems, NumElems*2)) ||
2784 (V2IsSplat && isUndefOrEqual(Arg, NumElems))))
2791 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2792 bool V2IsUndef = false) {
2793 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2794 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2795 return isCommutedMOVL(Ops, V2IsSplat, V2IsUndef);
2798 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2799 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2800 bool X86::isMOVSHDUPMask(SDNode *N) {
2801 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2803 if (N->getNumOperands() != 4)
2806 // Expect 1, 1, 3, 3
2807 for (unsigned i = 0; i < 2; ++i) {
2808 SDOperand Arg = N->getOperand(i);
2809 if (Arg.getOpcode() == ISD::UNDEF) continue;
2810 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2811 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2812 if (Val != 1) return false;
2816 for (unsigned i = 2; i < 4; ++i) {
2817 SDOperand Arg = N->getOperand(i);
2818 if (Arg.getOpcode() == ISD::UNDEF) continue;
2819 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2820 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2821 if (Val != 3) return false;
2825 // Don't use movshdup if it can be done with a shufps.
2829 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2830 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2831 bool X86::isMOVSLDUPMask(SDNode *N) {
2832 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2834 if (N->getNumOperands() != 4)
2837 // Expect 0, 0, 2, 2
2838 for (unsigned i = 0; i < 2; ++i) {
2839 SDOperand Arg = N->getOperand(i);
2840 if (Arg.getOpcode() == ISD::UNDEF) continue;
2841 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2842 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2843 if (Val != 0) return false;
2847 for (unsigned i = 2; i < 4; ++i) {
2848 SDOperand Arg = N->getOperand(i);
2849 if (Arg.getOpcode() == ISD::UNDEF) continue;
2850 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2851 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2852 if (Val != 2) return false;
2856 // Don't use movshdup if it can be done with a shufps.
2860 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2861 /// a splat of a single element.
2862 static bool isSplatMask(SDNode *N) {
2863 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2865 // This is a splat operation if each element of the permute is the same, and
2866 // if the value doesn't reference the second vector.
2867 unsigned NumElems = N->getNumOperands();
2868 SDOperand ElementBase;
2870 for (; i != NumElems; ++i) {
2871 SDOperand Elt = N->getOperand(i);
2872 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
2878 if (!ElementBase.Val)
2881 for (; i != NumElems; ++i) {
2882 SDOperand Arg = N->getOperand(i);
2883 if (Arg.getOpcode() == ISD::UNDEF) continue;
2884 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2885 if (Arg != ElementBase) return false;
2888 // Make sure it is a splat of the first vector operand.
2889 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2892 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2893 /// a splat of a single element and it's a 2 or 4 element mask.
2894 bool X86::isSplatMask(SDNode *N) {
2895 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2897 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2898 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2900 return ::isSplatMask(N);
2903 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2904 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2906 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2907 unsigned NumOperands = N->getNumOperands();
2908 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2910 for (unsigned i = 0; i < NumOperands; ++i) {
2912 SDOperand Arg = N->getOperand(NumOperands-i-1);
2913 if (Arg.getOpcode() != ISD::UNDEF)
2914 Val = cast<ConstantSDNode>(Arg)->getValue();
2915 if (Val >= NumOperands) Val -= NumOperands;
2917 if (i != NumOperands - 1)
2924 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2925 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2927 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2929 // 8 nodes, but we only care about the last 4.
2930 for (unsigned i = 7; i >= 4; --i) {
2932 SDOperand Arg = N->getOperand(i);
2933 if (Arg.getOpcode() != ISD::UNDEF)
2934 Val = cast<ConstantSDNode>(Arg)->getValue();
2943 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2944 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2946 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2948 // 8 nodes, but we only care about the first 4.
2949 for (int i = 3; i >= 0; --i) {
2951 SDOperand Arg = N->getOperand(i);
2952 if (Arg.getOpcode() != ISD::UNDEF)
2953 Val = cast<ConstantSDNode>(Arg)->getValue();
2962 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2963 /// specifies a 8 element shuffle that can be broken into a pair of
2964 /// PSHUFHW and PSHUFLW.
2965 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2966 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2968 if (N->getNumOperands() != 8)
2971 // Lower quadword shuffled.
2972 for (unsigned i = 0; i != 4; ++i) {
2973 SDOperand Arg = N->getOperand(i);
2974 if (Arg.getOpcode() == ISD::UNDEF) continue;
2975 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2976 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2981 // Upper quadword shuffled.
2982 for (unsigned i = 4; i != 8; ++i) {
2983 SDOperand Arg = N->getOperand(i);
2984 if (Arg.getOpcode() == ISD::UNDEF) continue;
2985 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2986 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2987 if (Val < 4 || Val > 7)
2994 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2995 /// values in ther permute mask.
2996 static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2997 SDOperand V1 = Op.getOperand(0);
2998 SDOperand V2 = Op.getOperand(1);
2999 SDOperand Mask = Op.getOperand(2);
3000 MVT::ValueType VT = Op.getValueType();
3001 MVT::ValueType MaskVT = Mask.getValueType();
3002 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
3003 unsigned NumElems = Mask.getNumOperands();
3004 std::vector<SDOperand> MaskVec;
3006 for (unsigned i = 0; i != NumElems; ++i) {
3007 SDOperand Arg = Mask.getOperand(i);
3008 if (Arg.getOpcode() == ISD::UNDEF) {
3009 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
3012 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
3013 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3015 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
3017 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
3020 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3021 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
3024 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3025 /// match movhlps. The lower half elements should come from upper half of
3026 /// V1 (and in order), and the upper half elements should come from the upper
3027 /// half of V2 (and in order).
3028 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
3029 unsigned NumElems = Mask->getNumOperands();
3032 for (unsigned i = 0, e = 2; i != e; ++i)
3033 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
3035 for (unsigned i = 2; i != 4; ++i)
3036 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
3041 /// isScalarLoadToVector - Returns true if the node is a scalar load that
3042 /// is promoted to a vector.
3043 static inline bool isScalarLoadToVector(SDNode *N) {
3044 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
3045 N = N->getOperand(0).Val;
3046 return ISD::isNON_EXTLoad(N);
3051 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3052 /// match movlp{s|d}. The lower half elements should come from lower half of
3053 /// V1 (and in order), and the upper half elements should come from the upper
3054 /// half of V2 (and in order). And since V1 will become the source of the
3055 /// MOVLP, it must be either a vector load or a scalar load to vector.
3056 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
3057 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
3060 unsigned NumElems = Mask->getNumOperands();
3061 if (NumElems != 2 && NumElems != 4)
3063 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3064 if (!isUndefOrEqual(Mask->getOperand(i), i))
3066 for (unsigned i = NumElems/2; i != NumElems; ++i)
3067 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
3072 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3074 static bool isSplatVector(SDNode *N) {
3075 if (N->getOpcode() != ISD::BUILD_VECTOR)
3078 SDOperand SplatValue = N->getOperand(0);
3079 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3080 if (N->getOperand(i) != SplatValue)
3085 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
3087 static bool isUndefShuffle(SDNode *N) {
3088 if (N->getOpcode() != ISD::BUILD_VECTOR)
3091 SDOperand V1 = N->getOperand(0);
3092 SDOperand V2 = N->getOperand(1);
3093 SDOperand Mask = N->getOperand(2);
3094 unsigned NumElems = Mask.getNumOperands();
3095 for (unsigned i = 0; i != NumElems; ++i) {
3096 SDOperand Arg = Mask.getOperand(i);
3097 if (Arg.getOpcode() != ISD::UNDEF) {
3098 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3099 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
3101 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
3108 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3109 /// that point to V2 points to its first element.
3110 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
3111 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
3113 bool Changed = false;
3114 std::vector<SDOperand> MaskVec;
3115 unsigned NumElems = Mask.getNumOperands();
3116 for (unsigned i = 0; i != NumElems; ++i) {
3117 SDOperand Arg = Mask.getOperand(i);
3118 if (Arg.getOpcode() != ISD::UNDEF) {
3119 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3120 if (Val > NumElems) {
3121 Arg = DAG.getConstant(NumElems, Arg.getValueType());
3125 MaskVec.push_back(Arg);
3129 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
3130 &MaskVec[0], MaskVec.size());
3134 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3135 /// operation of specified width.
3136 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
3137 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3138 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3140 std::vector<SDOperand> MaskVec;
3141 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
3142 for (unsigned i = 1; i != NumElems; ++i)
3143 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3144 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3147 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
3148 /// of specified width.
3149 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
3150 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3151 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3152 std::vector<SDOperand> MaskVec;
3153 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
3154 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3155 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
3157 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3160 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3161 /// of specified width.
3162 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
3163 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3164 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3165 unsigned Half = NumElems/2;
3166 std::vector<SDOperand> MaskVec;
3167 for (unsigned i = 0; i != Half; ++i) {
3168 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3169 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3171 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3174 /// getZeroVector - Returns a vector of specified type with all zero elements.
3176 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
3177 assert(MVT::isVector(VT) && "Expected a vector type");
3178 unsigned NumElems = getVectorNumElements(VT);
3179 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
3180 bool isFP = MVT::isFloatingPoint(EVT);
3181 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
3182 std::vector<SDOperand> ZeroVec(NumElems, Zero);
3183 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
3186 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
3188 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
3189 SDOperand V1 = Op.getOperand(0);
3190 SDOperand Mask = Op.getOperand(2);
3191 MVT::ValueType VT = Op.getValueType();
3192 unsigned NumElems = Mask.getNumOperands();
3193 Mask = getUnpacklMask(NumElems, DAG);
3194 while (NumElems != 4) {
3195 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3198 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3200 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3201 Mask = getZeroVector(MaskVT, DAG);
3202 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
3203 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
3204 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3207 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
3209 static inline bool isZeroNode(SDOperand Elt) {
3210 return ((isa<ConstantSDNode>(Elt) &&
3211 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
3212 (isa<ConstantFPSDNode>(Elt) &&
3213 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
3216 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
3217 /// vector and zero or undef vector.
3218 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
3219 unsigned NumElems, unsigned Idx,
3220 bool isZero, SelectionDAG &DAG) {
3221 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
3222 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3223 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
3224 SDOperand Zero = DAG.getConstant(0, EVT);
3225 std::vector<SDOperand> MaskVec(NumElems, Zero);
3226 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
3227 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3228 &MaskVec[0], MaskVec.size());
3229 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3232 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3234 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3235 unsigned NumNonZero, unsigned NumZero,
3236 SelectionDAG &DAG, TargetLowering &TLI) {
3242 for (unsigned i = 0; i < 16; ++i) {
3243 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3244 if (ThisIsNonZero && First) {
3246 V = getZeroVector(MVT::v8i16, DAG);
3248 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3253 SDOperand ThisElt(0, 0), LastElt(0, 0);
3254 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3255 if (LastIsNonZero) {
3256 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3258 if (ThisIsNonZero) {
3259 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3260 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3261 ThisElt, DAG.getConstant(8, MVT::i8));
3263 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3268 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
3269 DAG.getConstant(i/2, TLI.getPointerTy()));
3273 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3276 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
3278 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3279 unsigned NumNonZero, unsigned NumZero,
3280 SelectionDAG &DAG, TargetLowering &TLI) {
3286 for (unsigned i = 0; i < 8; ++i) {
3287 bool isNonZero = (NonZeros & (1 << i)) != 0;
3291 V = getZeroVector(MVT::v8i16, DAG);
3293 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3296 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
3297 DAG.getConstant(i, TLI.getPointerTy()));
3305 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3306 // All zero's are handled with pxor.
3307 if (ISD::isBuildVectorAllZeros(Op.Val))
3310 // All one's are handled with pcmpeqd.
3311 if (ISD::isBuildVectorAllOnes(Op.Val))
3314 MVT::ValueType VT = Op.getValueType();
3315 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
3316 unsigned EVTBits = MVT::getSizeInBits(EVT);
3318 unsigned NumElems = Op.getNumOperands();
3319 unsigned NumZero = 0;
3320 unsigned NumNonZero = 0;
3321 unsigned NonZeros = 0;
3322 std::set<SDOperand> Values;
3323 for (unsigned i = 0; i < NumElems; ++i) {
3324 SDOperand Elt = Op.getOperand(i);
3325 if (Elt.getOpcode() != ISD::UNDEF) {
3327 if (isZeroNode(Elt))
3330 NonZeros |= (1 << i);
3336 if (NumNonZero == 0)
3337 // Must be a mix of zero and undef. Return a zero vector.
3338 return getZeroVector(VT, DAG);
3340 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3341 if (Values.size() == 1)
3344 // Special case for single non-zero element.
3345 if (!NoShuffleOpti && NumNonZero == 1) {
3346 unsigned Idx = CountTrailingZeros_32(NonZeros);
3347 SDOperand Item = Op.getOperand(Idx);
3348 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3350 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3351 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3354 if (EVTBits == 32) {
3355 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3356 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3358 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3359 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
3360 std::vector<SDOperand> MaskVec;
3361 for (unsigned i = 0; i < NumElems; i++)
3362 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3363 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3364 &MaskVec[0], MaskVec.size());
3365 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3366 DAG.getNode(ISD::UNDEF, VT), Mask);
3370 // Let legalizer expand 2-wide build_vector's.
3374 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3376 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3378 if (V.Val) return V;
3381 if (EVTBits == 16) {
3382 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3384 if (V.Val) return V;
3387 // If element VT is == 32 bits, turn it into a number of shuffles.
3388 std::vector<SDOperand> V(NumElems);
3389 if (NumElems == 4 && NumZero > 0) {
3390 for (unsigned i = 0; i < 4; ++i) {
3391 bool isZero = !(NonZeros & (1 << i));
3393 V[i] = getZeroVector(VT, DAG);
3395 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3398 for (unsigned i = 0; i < 2; ++i) {
3399 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3402 V[i] = V[i*2]; // Must be a zero vector.
3405 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3406 getMOVLMask(NumElems, DAG));
3409 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3410 getMOVLMask(NumElems, DAG));
3413 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3414 getUnpacklMask(NumElems, DAG));
3419 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3420 // clears the upper bits.
3421 // FIXME: we can do the same for v4f32 case when we know both parts of
3422 // the lower half come from scalar_to_vector (loadf32). We should do
3423 // that in post legalizer dag combiner with target specific hooks.
3424 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3426 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3427 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
3428 std::vector<SDOperand> MaskVec;
3429 bool Reverse = (NonZeros & 0x3) == 2;
3430 for (unsigned i = 0; i < 2; ++i)
3432 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3434 MaskVec.push_back(DAG.getConstant(i, EVT));
3435 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3436 for (unsigned i = 0; i < 2; ++i)
3438 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3440 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3441 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3442 &MaskVec[0], MaskVec.size());
3443 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3446 if (Values.size() > 2) {
3447 // Expand into a number of unpckl*.
3449 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3450 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3451 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3452 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3453 for (unsigned i = 0; i < NumElems; ++i)
3454 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3456 while (NumElems != 0) {
3457 for (unsigned i = 0; i < NumElems; ++i)
3458 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3469 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3470 SDOperand V1 = Op.getOperand(0);
3471 SDOperand V2 = Op.getOperand(1);
3472 SDOperand PermMask = Op.getOperand(2);
3473 MVT::ValueType VT = Op.getValueType();
3474 unsigned NumElems = PermMask.getNumOperands();
3475 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3476 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3478 if (isUndefShuffle(Op.Val))
3479 return DAG.getNode(ISD::UNDEF, VT);
3481 if (isSplatMask(PermMask.Val)) {
3482 if (NumElems <= 4) return Op;
3483 // Promote it to a v4i32 splat.
3485 return PromoteSplat(Op, DAG);
3488 if (!NoShuffleOpti) {
3489 if (X86::isMOVLMask(PermMask.Val))
3490 return (V1IsUndef) ? V2 : Op;
3492 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3493 X86::isMOVSLDUPMask(PermMask.Val) ||
3494 X86::isMOVHLPSMask(PermMask.Val) ||
3495 X86::isMOVHPMask(PermMask.Val) ||
3496 X86::isMOVLPMask(PermMask.Val))
3499 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3500 ShouldXformToMOVLP(V1.Val, PermMask.Val))
3501 return CommuteVectorShuffle(Op, DAG);
3503 bool V1IsSplat = isSplatVector(V1.Val);
3504 bool V2IsSplat = isSplatVector(V2.Val);
3505 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3506 Op = CommuteVectorShuffle(Op, DAG);
3507 V1 = Op.getOperand(0);
3508 V2 = Op.getOperand(1);
3509 PermMask = Op.getOperand(2);
3510 std::swap(V1IsSplat, V2IsSplat);
3511 std::swap(V1IsUndef, V2IsUndef);
3514 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3515 if (V2IsUndef) return V1;
3516 Op = CommuteVectorShuffle(Op, DAG);
3517 V1 = Op.getOperand(0);
3518 V2 = Op.getOperand(1);
3519 PermMask = Op.getOperand(2);
3521 // V2 is a splat, so the mask may be malformed. That is, it may point
3522 // to any V2 element. The instruction selectior won't like this. Get
3523 // a corrected mask and commute to form a proper MOVS{S|D}.
3524 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3525 if (NewMask.Val != PermMask.Val)
3526 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3531 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3532 X86::isUNPCKLMask(PermMask.Val) ||
3533 X86::isUNPCKHMask(PermMask.Val))
3537 // Normalize mask so all entries that point to V2 points to its first
3538 // element then try to match unpck{h|l} again. If match, return a
3539 // new vector_shuffle with the corrected mask.
3540 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3541 if (NewMask.Val != PermMask.Val) {
3542 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3543 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3544 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3545 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3546 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3547 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3553 // Normalize the node to match x86 shuffle ops if needed
3554 if (V2.getOpcode() != ISD::UNDEF)
3555 if (isCommutedSHUFP(PermMask.Val)) {
3556 Op = CommuteVectorShuffle(Op, DAG);
3557 V1 = Op.getOperand(0);
3558 V2 = Op.getOperand(1);
3559 PermMask = Op.getOperand(2);
3562 // If VT is integer, try PSHUF* first, then SHUFP*.
3563 if (MVT::isInteger(VT)) {
3564 if (X86::isPSHUFDMask(PermMask.Val) ||
3565 X86::isPSHUFHWMask(PermMask.Val) ||
3566 X86::isPSHUFLWMask(PermMask.Val)) {
3567 if (V2.getOpcode() != ISD::UNDEF)
3568 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3569 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3573 if (X86::isSHUFPMask(PermMask.Val))
3576 // Handle v8i16 shuffle high / low shuffle node pair.
3577 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3578 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3579 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3580 std::vector<SDOperand> MaskVec;
3581 for (unsigned i = 0; i != 4; ++i)
3582 MaskVec.push_back(PermMask.getOperand(i));
3583 for (unsigned i = 4; i != 8; ++i)
3584 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3585 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3586 &MaskVec[0], MaskVec.size());
3587 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3589 for (unsigned i = 0; i != 4; ++i)
3590 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3591 for (unsigned i = 4; i != 8; ++i)
3592 MaskVec.push_back(PermMask.getOperand(i));
3593 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
3594 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3597 // Floating point cases in the other order.
3598 if (X86::isSHUFPMask(PermMask.Val))
3600 if (X86::isPSHUFDMask(PermMask.Val) ||
3601 X86::isPSHUFHWMask(PermMask.Val) ||
3602 X86::isPSHUFLWMask(PermMask.Val)) {
3603 if (V2.getOpcode() != ISD::UNDEF)
3604 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3605 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3610 if (NumElems == 4) {
3611 MVT::ValueType MaskVT = PermMask.getValueType();
3612 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
3613 std::vector<std::pair<int, int> > Locs;
3614 Locs.reserve(NumElems);
3615 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3616 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3619 // If no more than two elements come from either vector. This can be
3620 // implemented with two shuffles. First shuffle gather the elements.
3621 // The second shuffle, which takes the first shuffle as both of its
3622 // vector operands, put the elements into the right order.
3623 for (unsigned i = 0; i != NumElems; ++i) {
3624 SDOperand Elt = PermMask.getOperand(i);
3625 if (Elt.getOpcode() == ISD::UNDEF) {
3626 Locs[i] = std::make_pair(-1, -1);
3628 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3629 if (Val < NumElems) {
3630 Locs[i] = std::make_pair(0, NumLo);
3634 Locs[i] = std::make_pair(1, NumHi);
3635 if (2+NumHi < NumElems)
3636 Mask1[2+NumHi] = Elt;
3641 if (NumLo <= 2 && NumHi <= 2) {
3642 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3643 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3644 &Mask1[0], Mask1.size()));
3645 for (unsigned i = 0; i != NumElems; ++i) {
3646 if (Locs[i].first == -1)
3649 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3650 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3651 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3655 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3656 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3657 &Mask2[0], Mask2.size()));
3660 // Break it into (shuffle shuffle_hi, shuffle_lo).
3662 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3663 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3664 std::vector<SDOperand> *MaskPtr = &LoMask;
3665 unsigned MaskIdx = 0;
3667 unsigned HiIdx = NumElems/2;
3668 for (unsigned i = 0; i != NumElems; ++i) {
3669 if (i == NumElems/2) {
3675 SDOperand Elt = PermMask.getOperand(i);
3676 if (Elt.getOpcode() == ISD::UNDEF) {
3677 Locs[i] = std::make_pair(-1, -1);
3678 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3679 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3680 (*MaskPtr)[LoIdx] = Elt;
3683 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3684 (*MaskPtr)[HiIdx] = Elt;
3689 SDOperand LoShuffle =
3690 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3691 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3692 &LoMask[0], LoMask.size()));
3693 SDOperand HiShuffle =
3694 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3695 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3696 &HiMask[0], HiMask.size()));
3697 std::vector<SDOperand> MaskOps;
3698 for (unsigned i = 0; i != NumElems; ++i) {
3699 if (Locs[i].first == -1) {
3700 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3702 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3703 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3706 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3707 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3708 &MaskOps[0], MaskOps.size()));
3715 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3716 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3719 MVT::ValueType VT = Op.getValueType();
3720 // TODO: handle v16i8.
3721 if (MVT::getSizeInBits(VT) == 16) {
3722 // Transform it so it match pextrw which produces a 32-bit result.
3723 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3724 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3725 Op.getOperand(0), Op.getOperand(1));
3726 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3727 DAG.getValueType(VT));
3728 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3729 } else if (MVT::getSizeInBits(VT) == 32) {
3730 SDOperand Vec = Op.getOperand(0);
3731 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3734 // SHUFPS the element to the lowest double word, then movss.
3735 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3736 std::vector<SDOperand> IdxVec;
3737 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
3738 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3739 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3740 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3741 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3742 &IdxVec[0], IdxVec.size());
3743 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3745 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3746 DAG.getConstant(0, getPointerTy()));
3747 } else if (MVT::getSizeInBits(VT) == 64) {
3748 SDOperand Vec = Op.getOperand(0);
3749 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3753 // UNPCKHPD the element to the lowest double word, then movsd.
3754 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3755 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3756 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3757 std::vector<SDOperand> IdxVec;
3758 IdxVec.push_back(DAG.getConstant(1, 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()));
3772 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3773 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3774 // as its second argument.
3775 MVT::ValueType VT = Op.getValueType();
3776 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
3777 SDOperand N0 = Op.getOperand(0);
3778 SDOperand N1 = Op.getOperand(1);
3779 SDOperand N2 = Op.getOperand(2);
3780 if (MVT::getSizeInBits(BaseVT) == 16) {
3781 if (N1.getValueType() != MVT::i32)
3782 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3783 if (N2.getValueType() != MVT::i32)
3784 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
3785 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3786 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3787 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3790 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3791 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3792 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3793 std::vector<SDOperand> MaskVec;
3794 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3795 for (unsigned i = 1; i <= 3; ++i)
3796 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3797 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3798 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3799 &MaskVec[0], MaskVec.size()));
3801 // Use two pinsrw instructions to insert a 32 bit value.
3803 if (MVT::isFloatingPoint(N1.getValueType())) {
3804 if (ISD::isNON_EXTLoad(N1.Val)) {
3805 // Just load directly from f32mem to GR32.
3806 LoadSDNode *LD = cast<LoadSDNode>(N1);
3807 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3808 LD->getSrcValue(), LD->getSrcValueOffset());
3810 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3811 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3812 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3813 DAG.getConstant(0, getPointerTy()));
3816 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3817 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3818 DAG.getConstant(Idx, getPointerTy()));
3819 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3820 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3821 DAG.getConstant(Idx+1, getPointerTy()));
3822 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3830 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3831 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3832 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3835 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3836 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3837 // one of the above mentioned nodes. It has to be wrapped because otherwise
3838 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3839 // be used to form addressing mode. These wrapped nodes will be selected
3842 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3843 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3844 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3845 DAG.getTargetConstantPool(CP->getConstVal(),
3847 CP->getAlignment()));
3848 if (Subtarget->isTargetDarwin()) {
3849 // With PIC, the address is actually $g + Offset.
3850 if (!Subtarget->is64Bit() &&
3851 getTargetMachine().getRelocationModel() == Reloc::PIC_)
3852 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3853 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
3860 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3861 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3862 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3863 DAG.getTargetGlobalAddress(GV,
3865 if (Subtarget->isTargetDarwin()) {
3866 // With PIC, the address is actually $g + Offset.
3867 if (!Subtarget->is64Bit() &&
3868 getTargetMachine().getRelocationModel() == Reloc::PIC_)
3869 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3870 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3873 // For Darwin, external and weak symbols are indirect, so we want to load
3874 // the value at address GV, not the value of GV itself. This means that
3875 // the GlobalAddress must be in the base or index register of the address,
3876 // not the GV offset field.
3877 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
3878 DarwinGVRequiresExtraLoad(GV))
3879 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3880 } else if (Subtarget->isTargetCygwin() || Subtarget->isTargetWindows()) {
3881 // FIXME: What about PIC?
3882 if (WindowsGVRequiresExtraLoad(GV))
3883 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3891 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3892 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3893 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3894 DAG.getTargetExternalSymbol(Sym,
3896 if (Subtarget->isTargetDarwin()) {
3897 // With PIC, the address is actually $g + Offset.
3898 if (!Subtarget->is64Bit() &&
3899 getTargetMachine().getRelocationModel() == Reloc::PIC_)
3900 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3901 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3908 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3909 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3910 "Not an i64 shift!");
3911 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3912 SDOperand ShOpLo = Op.getOperand(0);
3913 SDOperand ShOpHi = Op.getOperand(1);
3914 SDOperand ShAmt = Op.getOperand(2);
3915 SDOperand Tmp1 = isSRA ?
3916 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3917 DAG.getConstant(0, MVT::i32);
3919 SDOperand Tmp2, Tmp3;
3920 if (Op.getOpcode() == ISD::SHL_PARTS) {
3921 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3922 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3924 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3925 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3928 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3929 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3930 DAG.getConstant(32, MVT::i8));
3931 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3932 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3935 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3937 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3938 SmallVector<SDOperand, 4> Ops;
3939 if (Op.getOpcode() == ISD::SHL_PARTS) {
3940 Ops.push_back(Tmp2);
3941 Ops.push_back(Tmp3);
3943 Ops.push_back(InFlag);
3944 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3945 InFlag = Hi.getValue(1);
3948 Ops.push_back(Tmp3);
3949 Ops.push_back(Tmp1);
3951 Ops.push_back(InFlag);
3952 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3954 Ops.push_back(Tmp2);
3955 Ops.push_back(Tmp3);
3957 Ops.push_back(InFlag);
3958 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3959 InFlag = Lo.getValue(1);
3962 Ops.push_back(Tmp3);
3963 Ops.push_back(Tmp1);
3965 Ops.push_back(InFlag);
3966 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3969 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3973 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3976 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3977 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3978 Op.getOperand(0).getValueType() >= MVT::i16 &&
3979 "Unknown SINT_TO_FP to lower!");
3982 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3983 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3984 MachineFunction &MF = DAG.getMachineFunction();
3985 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3986 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3987 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3988 StackSlot, DAG.getSrcValue(NULL));
3991 std::vector<MVT::ValueType> Tys;
3992 Tys.push_back(MVT::f64);
3993 Tys.push_back(MVT::Other);
3994 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3995 std::vector<SDOperand> Ops;
3996 Ops.push_back(Chain);
3997 Ops.push_back(StackSlot);
3998 Ops.push_back(DAG.getValueType(SrcVT));
3999 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
4000 Tys, &Ops[0], Ops.size());
4003 Chain = Result.getValue(1);
4004 SDOperand InFlag = Result.getValue(2);
4006 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4007 // shouldn't be necessary except that RFP cannot be live across
4008 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4009 MachineFunction &MF = DAG.getMachineFunction();
4010 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4011 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4012 std::vector<MVT::ValueType> Tys;
4013 Tys.push_back(MVT::Other);
4014 std::vector<SDOperand> Ops;
4015 Ops.push_back(Chain);
4016 Ops.push_back(Result);
4017 Ops.push_back(StackSlot);
4018 Ops.push_back(DAG.getValueType(Op.getValueType()));
4019 Ops.push_back(InFlag);
4020 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
4021 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
4027 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
4028 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4029 "Unknown FP_TO_SINT to lower!");
4030 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4032 MachineFunction &MF = DAG.getMachineFunction();
4033 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4034 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4035 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4038 switch (Op.getValueType()) {
4039 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4040 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4041 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4042 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
4045 SDOperand Chain = DAG.getEntryNode();
4046 SDOperand Value = Op.getOperand(0);
4048 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
4049 Chain = DAG.getStore(Chain, Value, StackSlot, DAG.getSrcValue(0));
4050 std::vector<MVT::ValueType> Tys;
4051 Tys.push_back(MVT::f64);
4052 Tys.push_back(MVT::Other);
4053 std::vector<SDOperand> Ops;
4054 Ops.push_back(Chain);
4055 Ops.push_back(StackSlot);
4056 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
4057 Value = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
4058 Chain = Value.getValue(1);
4059 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4060 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4063 // Build the FP_TO_INT*_IN_MEM
4064 std::vector<SDOperand> Ops;
4065 Ops.push_back(Chain);
4066 Ops.push_back(Value);
4067 Ops.push_back(StackSlot);
4068 SDOperand FIST = DAG.getNode(Opc, MVT::Other, &Ops[0], Ops.size());
4071 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4074 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4075 MVT::ValueType VT = Op.getValueType();
4076 const Type *OpNTy = MVT::getTypeForValueType(VT);
4077 std::vector<Constant*> CV;
4078 if (VT == MVT::f64) {
4079 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
4080 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4082 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
4083 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4084 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4085 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4087 Constant *CS = ConstantStruct::get(CV);
4088 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
4089 std::vector<MVT::ValueType> Tys;
4091 Tys.push_back(MVT::Other);
4092 SmallVector<SDOperand, 3> Ops;
4093 Ops.push_back(DAG.getEntryNode());
4094 Ops.push_back(CPIdx);
4095 Ops.push_back(DAG.getSrcValue(NULL));
4096 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
4097 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4100 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4101 MVT::ValueType VT = Op.getValueType();
4102 const Type *OpNTy = MVT::getTypeForValueType(VT);
4103 std::vector<Constant*> CV;
4104 if (VT == MVT::f64) {
4105 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
4106 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4108 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
4109 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4110 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4111 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4113 Constant *CS = ConstantStruct::get(CV);
4114 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
4115 std::vector<MVT::ValueType> Tys;
4117 Tys.push_back(MVT::Other);
4118 SmallVector<SDOperand, 3> Ops;
4119 Ops.push_back(DAG.getEntryNode());
4120 Ops.push_back(CPIdx);
4121 Ops.push_back(DAG.getSrcValue(NULL));
4122 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
4123 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4126 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
4128 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
4130 SDOperand Op0 = Op.getOperand(0);
4131 SDOperand Op1 = Op.getOperand(1);
4132 SDOperand CC = Op.getOperand(2);
4133 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4134 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4135 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4138 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4139 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
4141 SDOperand Ops1[] = { Chain, Op0, Op1 };
4142 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops1, 3).getValue(1);
4143 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4144 return DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4147 assert(isFP && "Illegal integer SetCC!");
4149 SDOperand COps[] = { Chain, Op0, Op1 };
4150 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
4152 switch (SetCCOpcode) {
4153 default: assert(false && "Illegal floating point SetCC!");
4154 case ISD::SETOEQ: { // !PF & ZF
4155 SDOperand Ops1[] = { DAG.getConstant(X86ISD::COND_NP, MVT::i8), Cond };
4156 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops1, 2);
4157 SDOperand Ops2[] = { DAG.getConstant(X86ISD::COND_E, MVT::i8),
4159 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4160 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4162 case ISD::SETUNE: { // PF | !ZF
4163 SDOperand Ops1[] = { DAG.getConstant(X86ISD::COND_P, MVT::i8), Cond };
4164 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops1, 2);
4165 SDOperand Ops2[] = { DAG.getConstant(X86ISD::COND_NE, MVT::i8),
4167 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4168 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4173 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4174 bool addTest = true;
4175 SDOperand Chain = DAG.getEntryNode();
4176 SDOperand Cond = Op.getOperand(0);
4178 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4180 if (Cond.getOpcode() == ISD::SETCC)
4181 Cond = LowerSETCC(Cond, DAG, Chain);
4183 if (Cond.getOpcode() == X86ISD::SETCC) {
4184 CC = Cond.getOperand(0);
4186 // If condition flag is set by a X86ISD::CMP, then make a copy of it
4187 // (since flag operand cannot be shared). Use it as the condition setting
4188 // operand in place of the X86ISD::SETCC.
4189 // If the X86ISD::SETCC has more than one use, then perhaps it's better
4190 // to use a test instead of duplicating the X86ISD::CMP (for register
4191 // pressure reason)?
4192 SDOperand Cmp = Cond.getOperand(1);
4193 unsigned Opc = Cmp.getOpcode();
4194 bool IllegalFPCMov = !X86ScalarSSE &&
4195 MVT::isFloatingPoint(Op.getValueType()) &&
4196 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4197 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
4199 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
4200 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
4206 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
4207 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
4208 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
4211 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
4212 SmallVector<SDOperand, 4> Ops;
4213 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4214 // condition is true.
4215 Ops.push_back(Op.getOperand(2));
4216 Ops.push_back(Op.getOperand(1));
4218 Ops.push_back(Cond.getValue(1));
4219 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
4222 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4223 bool addTest = true;
4224 SDOperand Chain = Op.getOperand(0);
4225 SDOperand Cond = Op.getOperand(1);
4226 SDOperand Dest = Op.getOperand(2);
4228 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4230 if (Cond.getOpcode() == ISD::SETCC)
4231 Cond = LowerSETCC(Cond, DAG, Chain);
4233 if (Cond.getOpcode() == X86ISD::SETCC) {
4234 CC = Cond.getOperand(0);
4236 // If condition flag is set by a X86ISD::CMP, then make a copy of it
4237 // (since flag operand cannot be shared). Use it as the condition setting
4238 // operand in place of the X86ISD::SETCC.
4239 // If the X86ISD::SETCC has more than one use, then perhaps it's better
4240 // to use a test instead of duplicating the X86ISD::CMP (for register
4241 // pressure reason)?
4242 SDOperand Cmp = Cond.getOperand(1);
4243 unsigned Opc = Cmp.getOpcode();
4244 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
4245 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
4246 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
4252 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
4253 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
4254 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
4256 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
4257 Cond, Op.getOperand(2), CC, Cond.getValue(1));
4260 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4261 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4262 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
4263 DAG.getTargetJumpTable(JT->getIndex(),
4265 if (Subtarget->isTargetDarwin()) {
4266 // With PIC, the address is actually $g + Offset.
4267 if (!Subtarget->is64Bit() &&
4268 getTargetMachine().getRelocationModel() == Reloc::PIC_)
4269 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4270 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4277 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
4278 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4280 if (Subtarget->is64Bit())
4281 return LowerX86_64CCCCallTo(Op, DAG);
4283 switch (CallingConv) {
4285 assert(0 && "Unsupported calling convention");
4286 case CallingConv::Fast:
4288 return LowerFastCCCallTo(Op, DAG, false);
4291 case CallingConv::C:
4292 case CallingConv::CSRet:
4293 return LowerCCCCallTo(Op, DAG);
4294 case CallingConv::X86_StdCall:
4295 return LowerStdCallCCCallTo(Op, DAG);
4296 case CallingConv::X86_FastCall:
4297 return LowerFastCCCallTo(Op, DAG, true);
4301 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
4304 switch(Op.getNumOperands()) {
4306 assert(0 && "Do not know how to return this many arguments!");
4308 case 1: // ret void.
4309 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
4310 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
4312 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
4314 if (MVT::isVector(ArgVT) ||
4315 (Subtarget->is64Bit() && MVT::isFloatingPoint(ArgVT))) {
4316 // Integer or FP vector result -> XMM0.
4317 if (DAG.getMachineFunction().liveout_empty())
4318 DAG.getMachineFunction().addLiveOut(X86::XMM0);
4319 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
4321 } else if (MVT::isInteger(ArgVT)) {
4322 // Integer result -> EAX / RAX.
4323 // The C calling convention guarantees the return value has been
4324 // promoted to at least MVT::i32. The X86-64 ABI doesn't require the
4325 // value to be promoted MVT::i64. So we don't have to extend it to
4326 // 64-bit. Return the value in EAX, but mark RAX as liveout.
4327 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
4328 if (DAG.getMachineFunction().liveout_empty())
4329 DAG.getMachineFunction().addLiveOut(Reg);
4331 Reg = (ArgVT == MVT::i64) ? X86::RAX : X86::EAX;
4332 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg, Op.getOperand(1),
4334 } else if (!X86ScalarSSE) {
4335 // FP return with fp-stack value.
4336 if (DAG.getMachineFunction().liveout_empty())
4337 DAG.getMachineFunction().addLiveOut(X86::ST0);
4339 std::vector<MVT::ValueType> Tys;
4340 Tys.push_back(MVT::Other);
4341 Tys.push_back(MVT::Flag);
4342 std::vector<SDOperand> Ops;
4343 Ops.push_back(Op.getOperand(0));
4344 Ops.push_back(Op.getOperand(1));
4345 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
4347 // FP return with ScalarSSE (return on fp-stack).
4348 if (DAG.getMachineFunction().liveout_empty())
4349 DAG.getMachineFunction().addLiveOut(X86::ST0);
4352 SDOperand Chain = Op.getOperand(0);
4353 SDOperand Value = Op.getOperand(1);
4355 if (ISD::isNON_EXTLoad(Value.Val) &&
4356 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
4357 Chain = Value.getOperand(0);
4358 MemLoc = Value.getOperand(1);
4360 // Spill the value to memory and reload it into top of stack.
4361 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
4362 MachineFunction &MF = DAG.getMachineFunction();
4363 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4364 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
4365 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc,
4366 DAG.getSrcValue(0));
4368 std::vector<MVT::ValueType> Tys;
4369 Tys.push_back(MVT::f64);
4370 Tys.push_back(MVT::Other);
4371 std::vector<SDOperand> Ops;
4372 Ops.push_back(Chain);
4373 Ops.push_back(MemLoc);
4374 Ops.push_back(DAG.getValueType(ArgVT));
4375 Copy = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
4377 Tys.push_back(MVT::Other);
4378 Tys.push_back(MVT::Flag);
4380 Ops.push_back(Copy.getValue(1));
4381 Ops.push_back(Copy);
4382 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
4387 unsigned Reg1 = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
4388 unsigned Reg2 = Subtarget->is64Bit() ? X86::RDX : X86::EDX;
4389 if (DAG.getMachineFunction().liveout_empty()) {
4390 DAG.getMachineFunction().addLiveOut(Reg1);
4391 DAG.getMachineFunction().addLiveOut(Reg2);
4394 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg2, Op.getOperand(3),
4396 Copy = DAG.getCopyToReg(Copy, Reg1, Op.getOperand(1), Copy.getValue(1));
4400 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
4401 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
4406 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
4407 MachineFunction &MF = DAG.getMachineFunction();
4408 const Function* Fn = MF.getFunction();
4409 if (Fn->hasExternalLinkage() &&
4410 Subtarget->isTargetCygwin() &&
4411 Fn->getName() == "main")
4412 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
4414 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4415 if (Subtarget->is64Bit())
4416 return LowerX86_64CCCArguments(Op, DAG);
4420 assert(0 && "Unsupported calling convention");
4421 case CallingConv::Fast:
4423 return LowerFastCCArguments(Op, DAG);
4426 case CallingConv::C:
4427 case CallingConv::CSRet:
4428 return LowerCCCArguments(Op, DAG);
4429 case CallingConv::X86_StdCall:
4430 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
4431 return LowerStdCallCCArguments(Op, DAG);
4432 case CallingConv::X86_FastCall:
4433 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
4434 return LowerFastCallCCArguments(Op, DAG);
4438 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4439 SDOperand InFlag(0, 0);
4440 SDOperand Chain = Op.getOperand(0);
4442 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4443 if (Align == 0) Align = 1;
4445 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4446 // If not DWORD aligned, call memset if size is less than the threshold.
4447 // It knows how to align to the right boundary first.
4448 if ((Align & 3) != 0 ||
4449 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4450 MVT::ValueType IntPtr = getPointerTy();
4451 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4452 std::vector<std::pair<SDOperand, const Type*> > Args;
4453 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
4454 // Extend the ubyte argument to be an int value for the call.
4455 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4456 Args.push_back(std::make_pair(Val, IntPtrTy));
4457 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
4458 std::pair<SDOperand,SDOperand> CallResult =
4459 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
4460 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4461 return CallResult.second;
4466 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4467 unsigned BytesLeft = 0;
4468 bool TwoRepStos = false;
4471 uint64_t Val = ValC->getValue() & 255;
4473 // If the value is a constant, then we can potentially use larger sets.
4474 switch (Align & 3) {
4475 case 2: // WORD aligned
4478 Val = (Val << 8) | Val;
4480 case 0: // DWORD aligned
4483 Val = (Val << 8) | Val;
4484 Val = (Val << 16) | Val;
4485 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4488 Val = (Val << 32) | Val;
4491 default: // Byte aligned
4494 Count = Op.getOperand(3);
4498 if (AVT > MVT::i8) {
4500 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4501 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4502 BytesLeft = I->getValue() % UBytes;
4504 assert(AVT >= MVT::i32 &&
4505 "Do not use rep;stos if not at least DWORD aligned");
4506 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4507 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4512 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4514 InFlag = Chain.getValue(1);
4517 Count = Op.getOperand(3);
4518 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4519 InFlag = Chain.getValue(1);
4522 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4524 InFlag = Chain.getValue(1);
4525 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4526 Op.getOperand(1), InFlag);
4527 InFlag = Chain.getValue(1);
4529 std::vector<MVT::ValueType> Tys;
4530 Tys.push_back(MVT::Other);
4531 Tys.push_back(MVT::Flag);
4532 std::vector<SDOperand> Ops;
4533 Ops.push_back(Chain);
4534 Ops.push_back(DAG.getValueType(AVT));
4535 Ops.push_back(InFlag);
4536 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4539 InFlag = Chain.getValue(1);
4540 Count = Op.getOperand(3);
4541 MVT::ValueType CVT = Count.getValueType();
4542 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4543 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4544 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4546 InFlag = Chain.getValue(1);
4548 Tys.push_back(MVT::Other);
4549 Tys.push_back(MVT::Flag);
4551 Ops.push_back(Chain);
4552 Ops.push_back(DAG.getValueType(MVT::i8));
4553 Ops.push_back(InFlag);
4554 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4555 } else if (BytesLeft) {
4556 // Issue stores for the last 1 - 7 bytes.
4558 unsigned Val = ValC->getValue() & 255;
4559 unsigned Offset = I->getValue() - BytesLeft;
4560 SDOperand DstAddr = Op.getOperand(1);
4561 MVT::ValueType AddrVT = DstAddr.getValueType();
4562 if (BytesLeft >= 4) {
4563 Val = (Val << 8) | Val;
4564 Val = (Val << 16) | Val;
4565 Value = DAG.getConstant(Val, MVT::i32);
4566 Chain = DAG.getStore(Chain, Value,
4567 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4568 DAG.getConstant(Offset, AddrVT)),
4569 DAG.getSrcValue(NULL));
4573 if (BytesLeft >= 2) {
4574 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4575 Chain = DAG.getStore(Chain, Value,
4576 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4577 DAG.getConstant(Offset, AddrVT)),
4578 DAG.getSrcValue(NULL));
4582 if (BytesLeft == 1) {
4583 Value = DAG.getConstant(Val, MVT::i8);
4584 Chain = DAG.getStore(Chain, Value,
4585 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4586 DAG.getConstant(Offset, AddrVT)),
4587 DAG.getSrcValue(NULL));
4594 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4595 SDOperand Chain = Op.getOperand(0);
4597 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4598 if (Align == 0) Align = 1;
4600 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4601 // If not DWORD aligned, call memcpy if size is less than the threshold.
4602 // It knows how to align to the right boundary first.
4603 if ((Align & 3) != 0 ||
4604 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4605 MVT::ValueType IntPtr = getPointerTy();
4606 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4607 std::vector<std::pair<SDOperand, const Type*> > Args;
4608 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
4609 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
4610 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
4611 std::pair<SDOperand,SDOperand> CallResult =
4612 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
4613 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4614 return CallResult.second;
4619 unsigned BytesLeft = 0;
4620 bool TwoRepMovs = false;
4621 switch (Align & 3) {
4622 case 2: // WORD aligned
4625 case 0: // DWORD aligned
4627 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4630 default: // Byte aligned
4632 Count = Op.getOperand(3);
4636 if (AVT > MVT::i8) {
4638 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4639 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4640 BytesLeft = I->getValue() % UBytes;
4642 assert(AVT >= MVT::i32 &&
4643 "Do not use rep;movs if not at least DWORD aligned");
4644 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4645 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4650 SDOperand InFlag(0, 0);
4651 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4653 InFlag = Chain.getValue(1);
4654 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4655 Op.getOperand(1), InFlag);
4656 InFlag = Chain.getValue(1);
4657 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4658 Op.getOperand(2), InFlag);
4659 InFlag = Chain.getValue(1);
4661 std::vector<MVT::ValueType> Tys;
4662 Tys.push_back(MVT::Other);
4663 Tys.push_back(MVT::Flag);
4664 std::vector<SDOperand> Ops;
4665 Ops.push_back(Chain);
4666 Ops.push_back(DAG.getValueType(AVT));
4667 Ops.push_back(InFlag);
4668 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4671 InFlag = Chain.getValue(1);
4672 Count = Op.getOperand(3);
4673 MVT::ValueType CVT = Count.getValueType();
4674 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4675 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4676 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4678 InFlag = Chain.getValue(1);
4680 Tys.push_back(MVT::Other);
4681 Tys.push_back(MVT::Flag);
4683 Ops.push_back(Chain);
4684 Ops.push_back(DAG.getValueType(MVT::i8));
4685 Ops.push_back(InFlag);
4686 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4687 } else if (BytesLeft) {
4688 // Issue loads and stores for the last 1 - 7 bytes.
4689 unsigned Offset = I->getValue() - BytesLeft;
4690 SDOperand DstAddr = Op.getOperand(1);
4691 MVT::ValueType DstVT = DstAddr.getValueType();
4692 SDOperand SrcAddr = Op.getOperand(2);
4693 MVT::ValueType SrcVT = SrcAddr.getValueType();
4695 if (BytesLeft >= 4) {
4696 Value = DAG.getLoad(MVT::i32, Chain,
4697 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4698 DAG.getConstant(Offset, SrcVT)),
4700 Chain = Value.getValue(1);
4701 Chain = DAG.getStore(Chain, Value,
4702 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4703 DAG.getConstant(Offset, DstVT)),
4704 DAG.getSrcValue(NULL));
4708 if (BytesLeft >= 2) {
4709 Value = DAG.getLoad(MVT::i16, Chain,
4710 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4711 DAG.getConstant(Offset, SrcVT)),
4713 Chain = Value.getValue(1);
4714 Chain = DAG.getStore(Chain, Value,
4715 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4716 DAG.getConstant(Offset, DstVT)),
4717 DAG.getSrcValue(NULL));
4722 if (BytesLeft == 1) {
4723 Value = DAG.getLoad(MVT::i8, Chain,
4724 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4725 DAG.getConstant(Offset, SrcVT)),
4727 Chain = Value.getValue(1);
4728 Chain = DAG.getStore(Chain, Value,
4729 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4730 DAG.getConstant(Offset, DstVT)),
4731 DAG.getSrcValue(NULL));
4739 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4740 std::vector<MVT::ValueType> Tys;
4741 Tys.push_back(MVT::Other);
4742 Tys.push_back(MVT::Flag);
4743 std::vector<SDOperand> Ops;
4744 Ops.push_back(Op.getOperand(0));
4745 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &Ops[0], Ops.size());
4747 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
4748 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
4749 MVT::i32, Ops[0].getValue(2)));
4750 Ops.push_back(Ops[1].getValue(1));
4751 Tys[0] = Tys[1] = MVT::i32;
4752 Tys.push_back(MVT::Other);
4753 return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size());
4756 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4757 if (!Subtarget->is64Bit()) {
4758 // vastart just stores the address of the VarArgsFrameIndex slot into the
4759 // memory location argument.
4760 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4761 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1),Op.getOperand(2));
4765 // gp_offset (0 - 6 * 8)
4766 // fp_offset (48 - 48 + 8 * 16)
4767 // overflow_arg_area (point to parameters coming in memory).
4769 std::vector<SDOperand> MemOps;
4770 SDOperand FIN = Op.getOperand(1);
4772 SDOperand Store = DAG.getStore(Op.getOperand(0),
4773 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4774 FIN, Op.getOperand(2));
4775 MemOps.push_back(Store);
4778 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4779 DAG.getConstant(4, getPointerTy()));
4780 Store = DAG.getStore(Op.getOperand(0),
4781 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4782 FIN, Op.getOperand(2));
4783 MemOps.push_back(Store);
4785 // Store ptr to overflow_arg_area
4786 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4787 DAG.getConstant(4, getPointerTy()));
4788 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4789 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, Op.getOperand(2));
4790 MemOps.push_back(Store);
4792 // Store ptr to reg_save_area.
4793 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4794 DAG.getConstant(8, getPointerTy()));
4795 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4796 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, Op.getOperand(2));
4797 MemOps.push_back(Store);
4798 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4802 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4803 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4805 default: return SDOperand(); // Don't custom lower most intrinsics.
4806 // Comparison intrinsics.
4807 case Intrinsic::x86_sse_comieq_ss:
4808 case Intrinsic::x86_sse_comilt_ss:
4809 case Intrinsic::x86_sse_comile_ss:
4810 case Intrinsic::x86_sse_comigt_ss:
4811 case Intrinsic::x86_sse_comige_ss:
4812 case Intrinsic::x86_sse_comineq_ss:
4813 case Intrinsic::x86_sse_ucomieq_ss:
4814 case Intrinsic::x86_sse_ucomilt_ss:
4815 case Intrinsic::x86_sse_ucomile_ss:
4816 case Intrinsic::x86_sse_ucomigt_ss:
4817 case Intrinsic::x86_sse_ucomige_ss:
4818 case Intrinsic::x86_sse_ucomineq_ss:
4819 case Intrinsic::x86_sse2_comieq_sd:
4820 case Intrinsic::x86_sse2_comilt_sd:
4821 case Intrinsic::x86_sse2_comile_sd:
4822 case Intrinsic::x86_sse2_comigt_sd:
4823 case Intrinsic::x86_sse2_comige_sd:
4824 case Intrinsic::x86_sse2_comineq_sd:
4825 case Intrinsic::x86_sse2_ucomieq_sd:
4826 case Intrinsic::x86_sse2_ucomilt_sd:
4827 case Intrinsic::x86_sse2_ucomile_sd:
4828 case Intrinsic::x86_sse2_ucomigt_sd:
4829 case Intrinsic::x86_sse2_ucomige_sd:
4830 case Intrinsic::x86_sse2_ucomineq_sd: {
4832 ISD::CondCode CC = ISD::SETCC_INVALID;
4835 case Intrinsic::x86_sse_comieq_ss:
4836 case Intrinsic::x86_sse2_comieq_sd:
4840 case Intrinsic::x86_sse_comilt_ss:
4841 case Intrinsic::x86_sse2_comilt_sd:
4845 case Intrinsic::x86_sse_comile_ss:
4846 case Intrinsic::x86_sse2_comile_sd:
4850 case Intrinsic::x86_sse_comigt_ss:
4851 case Intrinsic::x86_sse2_comigt_sd:
4855 case Intrinsic::x86_sse_comige_ss:
4856 case Intrinsic::x86_sse2_comige_sd:
4860 case Intrinsic::x86_sse_comineq_ss:
4861 case Intrinsic::x86_sse2_comineq_sd:
4865 case Intrinsic::x86_sse_ucomieq_ss:
4866 case Intrinsic::x86_sse2_ucomieq_sd:
4867 Opc = X86ISD::UCOMI;
4870 case Intrinsic::x86_sse_ucomilt_ss:
4871 case Intrinsic::x86_sse2_ucomilt_sd:
4872 Opc = X86ISD::UCOMI;
4875 case Intrinsic::x86_sse_ucomile_ss:
4876 case Intrinsic::x86_sse2_ucomile_sd:
4877 Opc = X86ISD::UCOMI;
4880 case Intrinsic::x86_sse_ucomigt_ss:
4881 case Intrinsic::x86_sse2_ucomigt_sd:
4882 Opc = X86ISD::UCOMI;
4885 case Intrinsic::x86_sse_ucomige_ss:
4886 case Intrinsic::x86_sse2_ucomige_sd:
4887 Opc = X86ISD::UCOMI;
4890 case Intrinsic::x86_sse_ucomineq_ss:
4891 case Intrinsic::x86_sse2_ucomineq_sd:
4892 Opc = X86ISD::UCOMI;
4898 SDOperand LHS = Op.getOperand(1);
4899 SDOperand RHS = Op.getOperand(2);
4900 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4902 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4903 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4904 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4905 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4906 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4907 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4908 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4913 /// LowerOperation - Provide custom lowering hooks for some operations.
4915 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4916 switch (Op.getOpcode()) {
4917 default: assert(0 && "Should not custom lower this!");
4918 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4919 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4920 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4921 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4922 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4923 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4924 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4925 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4926 case ISD::SHL_PARTS:
4927 case ISD::SRA_PARTS:
4928 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4929 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4930 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4931 case ISD::FABS: return LowerFABS(Op, DAG);
4932 case ISD::FNEG: return LowerFNEG(Op, DAG);
4933 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
4934 case ISD::SELECT: return LowerSELECT(Op, DAG);
4935 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4936 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4937 case ISD::CALL: return LowerCALL(Op, DAG);
4938 case ISD::RET: return LowerRET(Op, DAG);
4939 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4940 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4941 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4942 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4943 case ISD::VASTART: return LowerVASTART(Op, DAG);
4944 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4948 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4950 default: return NULL;
4951 case X86ISD::SHLD: return "X86ISD::SHLD";
4952 case X86ISD::SHRD: return "X86ISD::SHRD";
4953 case X86ISD::FAND: return "X86ISD::FAND";
4954 case X86ISD::FXOR: return "X86ISD::FXOR";
4955 case X86ISD::FILD: return "X86ISD::FILD";
4956 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4957 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4958 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4959 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4960 case X86ISD::FLD: return "X86ISD::FLD";
4961 case X86ISD::FST: return "X86ISD::FST";
4962 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4963 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4964 case X86ISD::CALL: return "X86ISD::CALL";
4965 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4966 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4967 case X86ISD::CMP: return "X86ISD::CMP";
4968 case X86ISD::COMI: return "X86ISD::COMI";
4969 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4970 case X86ISD::SETCC: return "X86ISD::SETCC";
4971 case X86ISD::CMOV: return "X86ISD::CMOV";
4972 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4973 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4974 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4975 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4976 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
4977 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
4978 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4979 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4980 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4981 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4982 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4986 /// isLegalAddressImmediate - Return true if the integer value or
4987 /// GlobalValue can be used as the offset of the target addressing mode.
4988 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
4989 // X86 allows a sign-extended 32-bit immediate field.
4990 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4993 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
4994 // GV is 64-bit but displacement field is 32-bit unless we are in small code
4995 // model. Mac OS X happens to support only small PIC code model.
4996 // FIXME: better support for other OS's.
4997 if (Subtarget->is64Bit() && !Subtarget->isTargetDarwin())
4999 if (Subtarget->isTargetDarwin()) {
5000 Reloc::Model RModel = getTargetMachine().getRelocationModel();
5001 if (RModel == Reloc::Static)
5003 else if (RModel == Reloc::DynamicNoPIC)
5004 return !DarwinGVRequiresExtraLoad(GV);
5011 /// isShuffleMaskLegal - Targets can use this to indicate that they only
5012 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5013 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5014 /// are assumed to be legal.
5016 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5017 // Only do shuffles on 128-bit vector types for now.
5018 if (MVT::getSizeInBits(VT) == 64) return false;
5019 return (Mask.Val->getNumOperands() <= 4 ||
5020 isSplatMask(Mask.Val) ||
5021 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5022 X86::isUNPCKLMask(Mask.Val) ||
5023 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5024 X86::isUNPCKHMask(Mask.Val));
5027 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5029 SelectionDAG &DAG) const {
5030 unsigned NumElts = BVOps.size();
5031 // Only do shuffles on 128-bit vector types for now.
5032 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5033 if (NumElts == 2) return true;
5035 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
5036 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
5041 //===----------------------------------------------------------------------===//
5042 // X86 Scheduler Hooks
5043 //===----------------------------------------------------------------------===//
5046 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5047 MachineBasicBlock *BB) {
5048 switch (MI->getOpcode()) {
5049 default: assert(false && "Unexpected instr type to insert");
5050 case X86::CMOV_FR32:
5051 case X86::CMOV_FR64:
5052 case X86::CMOV_V4F32:
5053 case X86::CMOV_V2F64:
5054 case X86::CMOV_V2I64: {
5055 // To "insert" a SELECT_CC instruction, we actually have to insert the
5056 // diamond control-flow pattern. The incoming instruction knows the
5057 // destination vreg to set, the condition code register to branch on, the
5058 // true/false values to select between, and a branch opcode to use.
5059 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5060 ilist<MachineBasicBlock>::iterator It = BB;
5066 // cmpTY ccX, r1, r2
5068 // fallthrough --> copy0MBB
5069 MachineBasicBlock *thisMBB = BB;
5070 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5071 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5072 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
5073 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
5074 MachineFunction *F = BB->getParent();
5075 F->getBasicBlockList().insert(It, copy0MBB);
5076 F->getBasicBlockList().insert(It, sinkMBB);
5077 // Update machine-CFG edges by first adding all successors of the current
5078 // block to the new block which will contain the Phi node for the select.
5079 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5080 e = BB->succ_end(); i != e; ++i)
5081 sinkMBB->addSuccessor(*i);
5082 // Next, remove all successors of the current block, and add the true
5083 // and fallthrough blocks as its successors.
5084 while(!BB->succ_empty())
5085 BB->removeSuccessor(BB->succ_begin());
5086 BB->addSuccessor(copy0MBB);
5087 BB->addSuccessor(sinkMBB);
5090 // %FalseValue = ...
5091 // # fallthrough to sinkMBB
5094 // Update machine-CFG edges
5095 BB->addSuccessor(sinkMBB);
5098 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5101 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
5102 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5103 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5105 delete MI; // The pseudo instruction is gone now.
5109 case X86::FP_TO_INT16_IN_MEM:
5110 case X86::FP_TO_INT32_IN_MEM:
5111 case X86::FP_TO_INT64_IN_MEM: {
5112 // Change the floating point control register to use "round towards zero"
5113 // mode when truncating to an integer value.
5114 MachineFunction *F = BB->getParent();
5115 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5116 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
5118 // Load the old value of the high byte of the control word...
5120 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
5121 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
5123 // Set the high part to be round to zero...
5124 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
5126 // Reload the modified control word now...
5127 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
5129 // Restore the memory image of control word to original value
5130 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
5132 // Get the X86 opcode to use.
5134 switch (MI->getOpcode()) {
5135 default: assert(0 && "illegal opcode!");
5136 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
5137 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
5138 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
5142 MachineOperand &Op = MI->getOperand(0);
5143 if (Op.isRegister()) {
5144 AM.BaseType = X86AddressMode::RegBase;
5145 AM.Base.Reg = Op.getReg();
5147 AM.BaseType = X86AddressMode::FrameIndexBase;
5148 AM.Base.FrameIndex = Op.getFrameIndex();
5150 Op = MI->getOperand(1);
5151 if (Op.isImmediate())
5152 AM.Scale = Op.getImmedValue();
5153 Op = MI->getOperand(2);
5154 if (Op.isImmediate())
5155 AM.IndexReg = Op.getImmedValue();
5156 Op = MI->getOperand(3);
5157 if (Op.isGlobalAddress()) {
5158 AM.GV = Op.getGlobal();
5160 AM.Disp = Op.getImmedValue();
5162 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
5164 // Reload the original control word now.
5165 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
5167 delete MI; // The pseudo instruction is gone now.
5173 //===----------------------------------------------------------------------===//
5174 // X86 Optimization Hooks
5175 //===----------------------------------------------------------------------===//
5177 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5179 uint64_t &KnownZero,
5181 unsigned Depth) const {
5182 unsigned Opc = Op.getOpcode();
5183 assert((Opc >= ISD::BUILTIN_OP_END ||
5184 Opc == ISD::INTRINSIC_WO_CHAIN ||
5185 Opc == ISD::INTRINSIC_W_CHAIN ||
5186 Opc == ISD::INTRINSIC_VOID) &&
5187 "Should use MaskedValueIsZero if you don't know whether Op"
5188 " is a target node!");
5190 KnownZero = KnownOne = 0; // Don't know anything.
5194 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5199 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
5200 /// element of the result of the vector shuffle.
5201 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5202 MVT::ValueType VT = N->getValueType(0);
5203 SDOperand PermMask = N->getOperand(2);
5204 unsigned NumElems = PermMask.getNumOperands();
5205 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5207 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5209 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
5210 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5211 SDOperand Idx = PermMask.getOperand(i);
5212 if (Idx.getOpcode() == ISD::UNDEF)
5213 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
5214 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5219 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5220 /// node is a GlobalAddress + an offset.
5221 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5222 if (N->getOpcode() == X86ISD::Wrapper) {
5223 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5224 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5227 } else if (N->getOpcode() == ISD::ADD) {
5228 SDOperand N1 = N->getOperand(0);
5229 SDOperand N2 = N->getOperand(1);
5230 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5231 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5233 Offset += V->getSignExtended();
5236 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5237 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5239 Offset += V->getSignExtended();
5247 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
5249 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5250 MachineFrameInfo *MFI) {
5251 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5254 SDOperand Loc = N->getOperand(1);
5255 SDOperand BaseLoc = Base->getOperand(1);
5256 if (Loc.getOpcode() == ISD::FrameIndex) {
5257 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5259 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
5260 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5261 int FS = MFI->getObjectSize(FI);
5262 int BFS = MFI->getObjectSize(BFI);
5263 if (FS != BFS || FS != Size) return false;
5264 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5266 GlobalValue *GV1 = NULL;
5267 GlobalValue *GV2 = NULL;
5268 int64_t Offset1 = 0;
5269 int64_t Offset2 = 0;
5270 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5271 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5272 if (isGA1 && isGA2 && GV1 == GV2)
5273 return Offset1 == (Offset2 + Dist*Size);
5279 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5280 const X86Subtarget *Subtarget) {
5283 if (isGAPlusOffset(Base, GV, Offset))
5284 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5286 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
5287 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
5289 // Fixed objects do not specify alignment, however the offsets are known.
5290 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5291 (MFI->getObjectOffset(BFI) % 16) == 0);
5293 return MFI->getObjectAlignment(BFI) >= 16;
5299 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5300 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5301 /// if the load addresses are consecutive, non-overlapping, and in the right
5303 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5304 const X86Subtarget *Subtarget) {
5305 MachineFunction &MF = DAG.getMachineFunction();
5306 MachineFrameInfo *MFI = MF.getFrameInfo();
5307 MVT::ValueType VT = N->getValueType(0);
5308 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
5309 SDOperand PermMask = N->getOperand(2);
5310 int NumElems = (int)PermMask.getNumOperands();
5311 SDNode *Base = NULL;
5312 for (int i = 0; i < NumElems; ++i) {
5313 SDOperand Idx = PermMask.getOperand(i);
5314 if (Idx.getOpcode() == ISD::UNDEF) {
5315 if (!Base) return SDOperand();
5318 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5319 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5323 else if (!isConsecutiveLoad(Arg.Val, Base,
5324 i, MVT::getSizeInBits(EVT)/8,MFI))
5329 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
5331 LoadSDNode *LD = cast<LoadSDNode>(Base);
5332 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5333 LD->getSrcValueOffset());
5335 // Just use movups, it's shorter.
5336 std::vector<MVT::ValueType> Tys;
5337 Tys.push_back(MVT::v4f32);
5338 Tys.push_back(MVT::Other);
5339 SmallVector<SDOperand, 3> Ops;
5340 Ops.push_back(Base->getOperand(0));
5341 Ops.push_back(Base->getOperand(1));
5342 Ops.push_back(Base->getOperand(2));
5343 return DAG.getNode(ISD::BIT_CONVERT, VT,
5344 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
5348 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5349 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5350 const X86Subtarget *Subtarget) {
5351 SDOperand Cond = N->getOperand(0);
5353 // If we have SSE[12] support, try to form min/max nodes.
5354 if (Subtarget->hasSSE2() &&
5355 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5356 if (Cond.getOpcode() == ISD::SETCC) {
5357 // Get the LHS/RHS of the select.
5358 SDOperand LHS = N->getOperand(1);
5359 SDOperand RHS = N->getOperand(2);
5360 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5363 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5366 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5369 if (!UnsafeFPMath) break;
5371 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5373 IntNo = LHS.getValueType() == MVT::f32 ? Intrinsic::x86_sse_min_ss :
5374 Intrinsic::x86_sse2_min_sd;
5377 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5380 if (!UnsafeFPMath) break;
5382 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5384 IntNo = LHS.getValueType() == MVT::f32 ? Intrinsic::x86_sse_max_ss :
5385 Intrinsic::x86_sse2_max_sd;
5388 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5391 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5394 if (!UnsafeFPMath) break;
5396 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5398 IntNo = LHS.getValueType() == MVT::f32 ? Intrinsic::x86_sse_min_ss :
5399 Intrinsic::x86_sse2_min_sd;
5402 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5405 if (!UnsafeFPMath) break;
5407 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5409 IntNo = LHS.getValueType() == MVT::f32 ? Intrinsic::x86_sse_max_ss :
5410 Intrinsic::x86_sse2_max_sd;
5415 // minss/maxss take a v4f32 operand.
5417 if (LHS.getValueType() == MVT::f32) {
5418 LHS = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, LHS);
5419 RHS = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, RHS);
5421 LHS = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, LHS);
5422 RHS = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, RHS);
5425 MVT::ValueType PtrTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
5426 SDOperand IntNoN = DAG.getConstant(IntNo, PtrTy);
5428 SDOperand Val = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, LHS.getValueType(),
5430 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getValueType(0), Val,
5431 DAG.getConstant(0, PtrTy));
5441 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5442 DAGCombinerInfo &DCI) const {
5443 TargetMachine &TM = getTargetMachine();
5444 SelectionDAG &DAG = DCI.DAG;
5445 switch (N->getOpcode()) {
5447 case ISD::VECTOR_SHUFFLE:
5448 return PerformShuffleCombine(N, DAG, Subtarget);
5450 return PerformSELECTCombine(N, DAG, Subtarget);
5456 //===----------------------------------------------------------------------===//
5457 // X86 Inline Assembly Support
5458 //===----------------------------------------------------------------------===//
5460 /// getConstraintType - Given a constraint letter, return the type of
5461 /// constraint it is for this target.
5462 X86TargetLowering::ConstraintType
5463 X86TargetLowering::getConstraintType(char ConstraintLetter) const {
5464 switch (ConstraintLetter) {
5473 return C_RegisterClass;
5474 default: return TargetLowering::getConstraintType(ConstraintLetter);
5478 std::vector<unsigned> X86TargetLowering::
5479 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5480 MVT::ValueType VT) const {
5481 if (Constraint.size() == 1) {
5482 // FIXME: not handling fp-stack yet!
5483 // FIXME: not handling MMX registers yet ('y' constraint).
5484 switch (Constraint[0]) { // GCC X86 Constraint Letters
5485 default: break; // Unknown constraint letter
5486 case 'A': // EAX/EDX
5487 if (VT == MVT::i32 || VT == MVT::i64)
5488 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5490 case 'r': // GENERAL_REGS
5491 case 'R': // LEGACY_REGS
5493 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5494 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
5495 else if (VT == MVT::i16)
5496 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
5497 X86::SI, X86::DI, X86::BP, X86::SP, 0);
5498 else if (VT == MVT::i8)
5499 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5501 case 'l': // INDEX_REGS
5503 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5504 X86::ESI, X86::EDI, X86::EBP, 0);
5505 else if (VT == MVT::i16)
5506 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
5507 X86::SI, X86::DI, X86::BP, 0);
5508 else if (VT == MVT::i8)
5509 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5511 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5514 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5515 else if (VT == MVT::i16)
5516 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5517 else if (VT == MVT::i8)
5518 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5520 case 'x': // SSE_REGS if SSE1 allowed
5521 if (Subtarget->hasSSE1())
5522 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5523 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5525 return std::vector<unsigned>();
5526 case 'Y': // SSE_REGS if SSE2 allowed
5527 if (Subtarget->hasSSE2())
5528 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5529 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5531 return std::vector<unsigned>();
5535 return std::vector<unsigned>();
5538 std::pair<unsigned, const TargetRegisterClass*>
5539 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5540 MVT::ValueType VT) const {
5541 // Use the default implementation in TargetLowering to convert the register
5542 // constraint into a member of a register class.
5543 std::pair<unsigned, const TargetRegisterClass*> Res;
5544 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5546 // Not found? Bail out.
5547 if (Res.second == 0) return Res;
5549 // Otherwise, check to see if this is a register class of the wrong value
5550 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5551 // turn into {ax},{dx}.
5552 if (Res.second->hasType(VT))
5553 return Res; // Correct type already, nothing to do.
5555 // All of the single-register GCC register classes map their values onto
5556 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5557 // really want an 8-bit or 32-bit register, map to the appropriate register
5558 // class and return the appropriate register.
5559 if (Res.second != X86::GR16RegisterClass)
5562 if (VT == MVT::i8) {
5563 unsigned DestReg = 0;
5564 switch (Res.first) {
5566 case X86::AX: DestReg = X86::AL; break;
5567 case X86::DX: DestReg = X86::DL; break;
5568 case X86::CX: DestReg = X86::CL; break;
5569 case X86::BX: DestReg = X86::BL; break;
5572 Res.first = DestReg;
5573 Res.second = Res.second = X86::GR8RegisterClass;
5575 } else if (VT == MVT::i32) {
5576 unsigned DestReg = 0;
5577 switch (Res.first) {
5579 case X86::AX: DestReg = X86::EAX; break;
5580 case X86::DX: DestReg = X86::EDX; break;
5581 case X86::CX: DestReg = X86::ECX; break;
5582 case X86::BX: DestReg = X86::EBX; break;
5583 case X86::SI: DestReg = X86::ESI; break;
5584 case X86::DI: DestReg = X86::EDI; break;
5585 case X86::BP: DestReg = X86::EBP; break;
5586 case X86::SP: DestReg = X86::ESP; break;
5589 Res.first = DestReg;
5590 Res.second = Res.second = X86::GR32RegisterClass;
5592 } else if (VT == MVT::i64) {
5593 unsigned DestReg = 0;
5594 switch (Res.first) {
5596 case X86::AX: DestReg = X86::RAX; break;
5597 case X86::DX: DestReg = X86::RDX; break;
5598 case X86::CX: DestReg = X86::RCX; break;
5599 case X86::BX: DestReg = X86::RBX; break;
5600 case X86::SI: DestReg = X86::RSI; break;
5601 case X86::DI: DestReg = X86::RDI; break;
5602 case X86::BP: DestReg = X86::RBP; break;
5603 case X86::SP: DestReg = X86::RSP; break;
5606 Res.first = DestReg;
5607 Res.second = Res.second = X86::GR64RegisterClass;