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 "X86TargetMachine.h"
19 #include "llvm/CallingConv.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/ADT/VectorExtras.h"
25 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/SelectionDAG.h"
30 #include "llvm/CodeGen/SSARegMap.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Target/TargetOptions.h"
36 #include "llvm/Support/CommandLine.h"
37 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
38 cl::desc("Enable fastcc on X86"));
40 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
41 : TargetLowering(TM) {
42 Subtarget = &TM.getSubtarget<X86Subtarget>();
43 X86ScalarSSE = Subtarget->hasSSE2();
45 // Set up the TargetLowering object.
47 // X86 is weird, it always uses i8 for shift amounts and setcc results.
48 setShiftAmountType(MVT::i8);
49 setSetCCResultType(MVT::i8);
50 setSetCCResultContents(ZeroOrOneSetCCResult);
51 setSchedulingPreference(SchedulingForRegPressure);
52 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
53 setStackPointerRegisterToSaveRestore(X86::ESP);
55 if (!Subtarget->isTargetDarwin())
56 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
57 setUseUnderscoreSetJmpLongJmp(true);
59 // Add legal addressing mode scale values.
60 addLegalAddressScale(8);
61 addLegalAddressScale(4);
62 addLegalAddressScale(2);
63 // Enter the ones which require both scale + index last. These are more
65 addLegalAddressScale(9);
66 addLegalAddressScale(5);
67 addLegalAddressScale(3);
69 // Set up the register classes.
70 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
71 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
72 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
74 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
76 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
77 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
78 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
81 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
82 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
84 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
86 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
88 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
89 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
90 // SSE has no i16 to fp conversion, only i32
92 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
94 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
95 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
98 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
100 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
101 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
103 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
105 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
106 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
109 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
111 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
112 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
115 // Handle FP_TO_UINT by promoting the destination to a larger signed
117 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
118 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
119 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
121 if (X86ScalarSSE && !Subtarget->hasSSE3())
122 // Expand FP_TO_UINT into a select.
123 // FIXME: We would like to use a Custom expander here eventually to do
124 // the optimal thing for SSE vs. the default expansion in the legalizer.
125 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
127 // With SSE3 we can use fisttpll to convert to a signed i64.
128 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
130 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
131 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
133 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
134 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
135 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
136 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
140 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
141 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
142 setOperationAction(ISD::FREM , MVT::f64 , Expand);
143 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
144 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
145 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
146 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
147 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
148 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
149 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
150 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
151 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
152 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
153 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
155 // These should be promoted to a larger select which is supported.
156 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
157 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
159 // X86 wants to expand cmov itself.
160 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
161 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
162 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
163 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
166 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
167 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
168 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
169 // X86 ret instruction may pop stack.
170 setOperationAction(ISD::RET , MVT::Other, Custom);
172 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
173 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
174 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
175 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
176 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
177 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
178 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
179 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
180 // X86 wants to expand memset / memcpy itself.
181 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
182 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
184 // We don't have line number support yet.
185 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
186 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
187 // FIXME - use subtarget debug flags
188 if (!Subtarget->isTargetDarwin())
189 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
191 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
192 setOperationAction(ISD::VASTART , MVT::Other, Custom);
194 // Use the default implementation.
195 setOperationAction(ISD::VAARG , MVT::Other, Expand);
196 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
197 setOperationAction(ISD::VAEND , MVT::Other, Expand);
198 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
199 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
200 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
202 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
203 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
206 // Set up the FP register classes.
207 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
208 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
210 // Use ANDPD to simulate FABS.
211 setOperationAction(ISD::FABS , MVT::f64, Custom);
212 setOperationAction(ISD::FABS , MVT::f32, Custom);
214 // Use XORP to simulate FNEG.
215 setOperationAction(ISD::FNEG , MVT::f64, Custom);
216 setOperationAction(ISD::FNEG , MVT::f32, Custom);
218 // We don't support sin/cos/fmod
219 setOperationAction(ISD::FSIN , MVT::f64, Expand);
220 setOperationAction(ISD::FCOS , MVT::f64, Expand);
221 setOperationAction(ISD::FREM , MVT::f64, Expand);
222 setOperationAction(ISD::FSIN , MVT::f32, Expand);
223 setOperationAction(ISD::FCOS , MVT::f32, Expand);
224 setOperationAction(ISD::FREM , MVT::f32, Expand);
226 // Expand FP immediates into loads from the stack, except for the special
228 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
229 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
230 addLegalFPImmediate(+0.0); // xorps / xorpd
232 // Set up the FP register classes.
233 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
235 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
238 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
239 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
242 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
243 addLegalFPImmediate(+0.0); // FLD0
244 addLegalFPImmediate(+1.0); // FLD1
245 addLegalFPImmediate(-0.0); // FLD0/FCHS
246 addLegalFPImmediate(-1.0); // FLD1/FCHS
249 // First set operation action for all vector types to expand. Then we
250 // will selectively turn on ones that can be effectively codegen'd.
251 for (unsigned VT = (unsigned)MVT::Vector + 1;
252 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
253 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
254 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
258 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
259 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
262 if (Subtarget->hasMMX()) {
263 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
264 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
267 // FIXME: add MMX packed arithmetics
268 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
273 if (Subtarget->hasSSE1()) {
274 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
276 setOperationAction(ISD::AND, MVT::v4f32, Legal);
277 setOperationAction(ISD::OR, MVT::v4f32, Legal);
278 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
279 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
280 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
281 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
282 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
283 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
284 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
285 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
286 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
289 if (Subtarget->hasSSE2()) {
290 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
291 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
292 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
293 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
294 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
296 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
297 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
298 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
299 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
300 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
301 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
302 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
303 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
304 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
305 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
307 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
309 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
310 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
311 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
312 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
314 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
315 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
316 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
317 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
318 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
320 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
321 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
322 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
323 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
324 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
325 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
327 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
328 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
329 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
330 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
331 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
332 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
333 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
334 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
335 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
336 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
337 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
338 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
341 // Custom lower v2i64 and v2f64 selects.
342 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
343 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
344 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
345 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
348 // We want to custom lower some of our intrinsics.
349 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
351 computeRegisterProperties();
353 // FIXME: These should be based on subtarget info. Plus, the values should
354 // be smaller when we are in optimizing for size mode.
355 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
356 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
357 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
358 allowUnalignedMemoryAccesses = true; // x86 supports it!
361 //===----------------------------------------------------------------------===//
362 // C Calling Convention implementation
363 //===----------------------------------------------------------------------===//
365 /// AddLiveIn - This helper function adds the specified physical register to the
366 /// MachineFunction as a live in value. It also creates a corresponding virtual
368 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
369 TargetRegisterClass *RC) {
370 assert(RC->contains(PReg) && "Not the correct regclass!");
371 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
372 MF.addLiveIn(PReg, VReg);
376 /// HowToPassCCCArgument - Returns how an formal argument of the specified type
377 /// should be passed. If it is through stack, returns the size of the stack
378 /// slot; if it is through XMM register, returns the number of XMM registers
381 HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
382 unsigned &ObjSize, unsigned &ObjXMMRegs) {
386 default: assert(0 && "Unhandled argument type!");
387 case MVT::i8: ObjSize = 1; break;
388 case MVT::i16: ObjSize = 2; break;
389 case MVT::i32: ObjSize = 4; break;
390 case MVT::i64: ObjSize = 8; break;
391 case MVT::f32: ObjSize = 4; break;
392 case MVT::f64: ObjSize = 8; break;
407 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
408 unsigned NumArgs = Op.Val->getNumValues() - 1;
409 MachineFunction &MF = DAG.getMachineFunction();
410 MachineFrameInfo *MFI = MF.getFrameInfo();
411 SDOperand Root = Op.getOperand(0);
412 std::vector<SDOperand> ArgValues;
414 // Add DAG nodes to load the arguments... On entry to a function on the X86,
415 // the stack frame looks like this:
417 // [ESP] -- return address
418 // [ESP + 4] -- first argument (leftmost lexically)
419 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
422 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
423 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
424 unsigned XMMArgRegs[] = { X86::XMM0, X86::XMM1, X86::XMM2 };
425 for (unsigned i = 0; i < NumArgs; ++i) {
426 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
427 unsigned ArgIncrement = 4;
428 unsigned ObjSize = 0;
429 unsigned ObjXMMRegs = 0;
430 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
432 ArgIncrement = ObjSize;
436 // Passed in a XMM register.
437 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
438 X86::VR128RegisterClass);
439 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
440 ArgValues.push_back(ArgValue);
441 NumXMMRegs += ObjXMMRegs;
443 // Create the frame index object for this incoming parameter...
444 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
445 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
446 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
447 DAG.getSrcValue(NULL));
448 ArgValues.push_back(ArgValue);
449 ArgOffset += ArgIncrement; // Move on to the next argument...
453 ArgValues.push_back(Root);
455 // If the function takes variable number of arguments, make a frame index for
456 // the start of the first vararg value... for expansion of llvm.va_start.
457 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
459 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
460 ReturnAddrIndex = 0; // No return address slot generated yet.
461 BytesToPopOnReturn = 0; // Callee pops nothing.
462 BytesCallerReserves = ArgOffset;
464 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
466 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
467 Subtarget->isTargetDarwin())
468 BytesToPopOnReturn = 4;
470 // Return the new list of results.
471 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
472 Op.Val->value_end());
473 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
477 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
478 SDOperand Chain = Op.getOperand(0);
479 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
480 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
481 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
482 SDOperand Callee = Op.getOperand(4);
483 MVT::ValueType RetVT= Op.Val->getValueType(0);
484 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
486 // Keep track of the number of XMM regs passed so far.
487 unsigned NumXMMRegs = 0;
488 static const unsigned XMMArgRegs[] = {
489 X86::XMM0, X86::XMM1, X86::XMM2
492 // Count how many bytes are to be pushed on the stack.
493 unsigned NumBytes = 0;
494 for (unsigned i = 0; i != NumOps; ++i) {
495 SDOperand Arg = Op.getOperand(5+2*i);
497 switch (Arg.getValueType()) {
498 default: assert(0 && "Unexpected ValueType for argument!");
523 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
525 // Arguments go on the stack in reverse order, as specified by the ABI.
526 unsigned ArgOffset = 0;
528 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
529 std::vector<SDOperand> MemOpChains;
530 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
531 for (unsigned i = 0; i != NumOps; ++i) {
532 SDOperand Arg = Op.getOperand(5+2*i);
534 switch (Arg.getValueType()) {
535 default: assert(0 && "Unexpected ValueType for argument!");
538 // Promote the integer to 32 bits. If the input type is signed use a
539 // sign extend, otherwise use a zero extend.
541 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
542 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
543 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
549 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
550 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
551 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
552 Arg, PtrOff, DAG.getSrcValue(NULL)));
558 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
559 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
560 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
561 Arg, PtrOff, DAG.getSrcValue(NULL)));
571 if (NumXMMRegs < 3) {
572 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
575 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
576 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
577 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
578 Arg, PtrOff, DAG.getSrcValue(NULL)));
584 if (!MemOpChains.empty())
585 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
587 // Build a sequence of copy-to-reg nodes chained together with token chain
588 // and flag operands which copy the outgoing args into registers.
590 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
591 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
593 InFlag = Chain.getValue(1);
596 // If the callee is a GlobalAddress node (quite common, every direct call is)
597 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
598 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
599 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
600 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
601 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
603 std::vector<MVT::ValueType> NodeTys;
604 NodeTys.push_back(MVT::Other); // Returns a chain
605 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
606 std::vector<SDOperand> Ops;
607 Ops.push_back(Chain);
608 Ops.push_back(Callee);
610 Ops.push_back(InFlag);
612 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
614 InFlag = Chain.getValue(1);
616 // Create the CALLSEQ_END node.
617 unsigned NumBytesForCalleeToPush = 0;
619 // If this is is a call to a struct-return function on Darwin/X86, the callee
620 // pops the hidden struct pointer, so we have to push it back.
621 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
622 NumBytesForCalleeToPush = 4;
625 NodeTys.push_back(MVT::Other); // Returns a chain
626 if (RetVT != MVT::Other)
627 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
629 Ops.push_back(Chain);
630 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
631 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
632 Ops.push_back(InFlag);
633 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
634 if (RetVT != MVT::Other)
635 InFlag = Chain.getValue(1);
637 std::vector<SDOperand> ResultVals;
640 default: assert(0 && "Unknown value type to return!");
641 case MVT::Other: break;
643 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
644 ResultVals.push_back(Chain.getValue(0));
645 NodeTys.push_back(MVT::i8);
648 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
649 ResultVals.push_back(Chain.getValue(0));
650 NodeTys.push_back(MVT::i16);
653 if (Op.Val->getValueType(1) == MVT::i32) {
654 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
655 ResultVals.push_back(Chain.getValue(0));
656 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
657 Chain.getValue(2)).getValue(1);
658 ResultVals.push_back(Chain.getValue(0));
659 NodeTys.push_back(MVT::i32);
661 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
662 ResultVals.push_back(Chain.getValue(0));
664 NodeTys.push_back(MVT::i32);
672 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
673 ResultVals.push_back(Chain.getValue(0));
674 NodeTys.push_back(RetVT);
678 std::vector<MVT::ValueType> Tys;
679 Tys.push_back(MVT::f64);
680 Tys.push_back(MVT::Other);
681 Tys.push_back(MVT::Flag);
682 std::vector<SDOperand> Ops;
683 Ops.push_back(Chain);
684 Ops.push_back(InFlag);
685 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
686 Chain = RetVal.getValue(1);
687 InFlag = RetVal.getValue(2);
689 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
690 // shouldn't be necessary except that RFP cannot be live across
691 // multiple blocks. When stackifier is fixed, they can be uncoupled.
692 MachineFunction &MF = DAG.getMachineFunction();
693 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
694 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
696 Tys.push_back(MVT::Other);
698 Ops.push_back(Chain);
699 Ops.push_back(RetVal);
700 Ops.push_back(StackSlot);
701 Ops.push_back(DAG.getValueType(RetVT));
702 Ops.push_back(InFlag);
703 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
704 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
705 DAG.getSrcValue(NULL));
706 Chain = RetVal.getValue(1);
709 if (RetVT == MVT::f32 && !X86ScalarSSE)
710 // FIXME: we would really like to remember that this FP_ROUND
711 // operation is okay to eliminate if we allow excess FP precision.
712 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
713 ResultVals.push_back(RetVal);
714 NodeTys.push_back(RetVT);
719 // If the function returns void, just return the chain.
720 if (ResultVals.empty())
723 // Otherwise, merge everything together with a MERGE_VALUES node.
724 NodeTys.push_back(MVT::Other);
725 ResultVals.push_back(Chain);
726 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
727 return Res.getValue(Op.ResNo);
730 //===----------------------------------------------------------------------===//
731 // Fast Calling Convention implementation
732 //===----------------------------------------------------------------------===//
734 // The X86 'fast' calling convention passes up to two integer arguments in
735 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
736 // and requires that the callee pop its arguments off the stack (allowing proper
737 // tail calls), and has the same return value conventions as C calling convs.
739 // This calling convention always arranges for the callee pop value to be 8n+4
740 // bytes, which is needed for tail recursion elimination and stack alignment
743 // Note that this can be enhanced in the future to pass fp vals in registers
744 // (when we have a global fp allocator) and do other tricks.
747 // FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
748 // to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
749 // EDX". Anything more is illegal.
751 // FIXME: The linscan register allocator currently has problem with
752 // coalescing. At the time of this writing, whenever it decides to coalesce
753 // a physreg with a virtreg, this increases the size of the physreg's live
754 // range, and the live range cannot ever be reduced. This causes problems if
755 // too many physregs are coaleced with virtregs, which can cause the register
756 // allocator to wedge itself.
758 // This code triggers this problem more often if we pass args in registers,
759 // so disable it until this is fixed.
761 // NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
762 // about code being dead.
764 static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
767 /// HowToPassFastCCArgument - Returns how an formal argument of the specified
768 /// type should be passed. If it is through stack, returns the size of the stack
769 /// slot; if it is through integer or XMM register, returns the number of
770 /// integer or XMM registers are needed.
772 HowToPassFastCCArgument(MVT::ValueType ObjectVT,
773 unsigned NumIntRegs, unsigned NumXMMRegs,
774 unsigned &ObjSize, unsigned &ObjIntRegs,
775 unsigned &ObjXMMRegs) {
781 default: assert(0 && "Unhandled argument type!");
783 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
789 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
795 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
801 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
803 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
829 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
830 unsigned NumArgs = Op.Val->getNumValues()-1;
831 MachineFunction &MF = DAG.getMachineFunction();
832 MachineFrameInfo *MFI = MF.getFrameInfo();
833 SDOperand Root = Op.getOperand(0);
834 std::vector<SDOperand> ArgValues;
836 // Add DAG nodes to load the arguments... On entry to a function the stack
837 // frame looks like this:
839 // [ESP] -- return address
840 // [ESP + 4] -- first nonreg argument (leftmost lexically)
841 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
843 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
845 // Keep track of the number of integer regs passed so far. This can be either
846 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
848 unsigned NumIntRegs = 0;
849 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
851 static const unsigned XMMArgRegs[] = {
852 X86::XMM0, X86::XMM1, X86::XMM2
855 for (unsigned i = 0; i < NumArgs; ++i) {
856 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
857 unsigned ArgIncrement = 4;
858 unsigned ObjSize = 0;
859 unsigned ObjIntRegs = 0;
860 unsigned ObjXMMRegs = 0;
862 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
863 ObjSize, ObjIntRegs, ObjXMMRegs);
865 ArgIncrement = ObjSize;
869 if (ObjIntRegs || ObjXMMRegs) {
871 default: assert(0 && "Unhandled argument type!");
873 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
874 X86::GR8RegisterClass);
875 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
878 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
879 X86::GR16RegisterClass);
880 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
883 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
884 X86::GR32RegisterClass);
885 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
888 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
889 X86::GR32RegisterClass);
890 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
891 if (ObjIntRegs == 2) {
892 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
893 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
894 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
903 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
904 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
907 NumIntRegs += ObjIntRegs;
908 NumXMMRegs += ObjXMMRegs;
912 // Create the SelectionDAG nodes corresponding to a load from this
914 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
915 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
916 if (ObjectVT == MVT::i64 && ObjIntRegs) {
917 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
918 DAG.getSrcValue(NULL));
919 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
921 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
922 DAG.getSrcValue(NULL));
923 ArgOffset += ArgIncrement; // Move on to the next argument.
926 ArgValues.push_back(ArgValue);
929 ArgValues.push_back(Root);
931 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
932 // arguments and the arguments after the retaddr has been pushed are aligned.
933 if ((ArgOffset & 7) == 0)
936 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
937 ReturnAddrIndex = 0; // No return address slot generated yet.
938 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
939 BytesCallerReserves = 0;
941 // Finally, inform the code generator which regs we return values in.
942 switch (getValueType(MF.getFunction()->getReturnType())) {
943 default: assert(0 && "Unknown type!");
944 case MVT::isVoid: break;
948 MF.addLiveOut(X86::EAX);
951 MF.addLiveOut(X86::EAX);
952 MF.addLiveOut(X86::EDX);
956 MF.addLiveOut(X86::ST0);
964 MF.addLiveOut(X86::XMM0);
968 // Return the new list of results.
969 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
970 Op.Val->value_end());
971 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
974 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG) {
975 SDOperand Chain = Op.getOperand(0);
976 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
977 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
978 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
979 SDOperand Callee = Op.getOperand(4);
980 MVT::ValueType RetVT= Op.Val->getValueType(0);
981 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
983 // Count how many bytes are to be pushed on the stack.
984 unsigned NumBytes = 0;
986 // Keep track of the number of integer regs passed so far. This can be either
987 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
989 unsigned NumIntRegs = 0;
990 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
992 static const unsigned GPRArgRegs[][2] = {
993 { X86::AL, X86::DL },
994 { X86::AX, X86::DX },
995 { X86::EAX, X86::EDX }
997 static const unsigned XMMArgRegs[] = {
998 X86::XMM0, X86::XMM1, X86::XMM2
1001 for (unsigned i = 0; i != NumOps; ++i) {
1002 SDOperand Arg = Op.getOperand(5+2*i);
1004 switch (Arg.getValueType()) {
1005 default: assert(0 && "Unknown value type!");
1009 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
1034 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1035 // arguments and the arguments after the retaddr has been pushed are aligned.
1036 if ((NumBytes & 7) == 0)
1039 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1041 // Arguments go on the stack in reverse order, as specified by the ABI.
1042 unsigned ArgOffset = 0;
1044 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1045 std::vector<SDOperand> MemOpChains;
1046 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
1047 for (unsigned i = 0; i != NumOps; ++i) {
1048 SDOperand Arg = Op.getOperand(5+2*i);
1050 switch (Arg.getValueType()) {
1051 default: assert(0 && "Unexpected ValueType for argument!");
1055 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
1056 RegsToPass.push_back(
1057 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1064 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1065 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1066 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1067 Arg, PtrOff, DAG.getSrcValue(NULL)));
1072 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1073 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1074 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1075 Arg, PtrOff, DAG.getSrcValue(NULL)));
1085 if (NumXMMRegs < 3) {
1086 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1089 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1090 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1091 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1092 Arg, PtrOff, DAG.getSrcValue(NULL)));
1098 if (!MemOpChains.empty())
1099 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
1101 // Build a sequence of copy-to-reg nodes chained together with token chain
1102 // and flag operands which copy the outgoing args into registers.
1104 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1105 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1107 InFlag = Chain.getValue(1);
1110 // If the callee is a GlobalAddress node (quite common, every direct call is)
1111 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1112 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1113 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1114 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1115 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1117 std::vector<MVT::ValueType> NodeTys;
1118 NodeTys.push_back(MVT::Other); // Returns a chain
1119 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1120 std::vector<SDOperand> Ops;
1121 Ops.push_back(Chain);
1122 Ops.push_back(Callee);
1124 Ops.push_back(InFlag);
1126 // FIXME: Do not generate X86ISD::TAILCALL for now.
1127 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1129 InFlag = Chain.getValue(1);
1132 NodeTys.push_back(MVT::Other); // Returns a chain
1133 if (RetVT != MVT::Other)
1134 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1136 Ops.push_back(Chain);
1137 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1138 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1139 Ops.push_back(InFlag);
1140 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1141 if (RetVT != MVT::Other)
1142 InFlag = Chain.getValue(1);
1144 std::vector<SDOperand> ResultVals;
1147 default: assert(0 && "Unknown value type to return!");
1148 case MVT::Other: break;
1150 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1151 ResultVals.push_back(Chain.getValue(0));
1152 NodeTys.push_back(MVT::i8);
1155 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1156 ResultVals.push_back(Chain.getValue(0));
1157 NodeTys.push_back(MVT::i16);
1160 if (Op.Val->getValueType(1) == MVT::i32) {
1161 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1162 ResultVals.push_back(Chain.getValue(0));
1163 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1164 Chain.getValue(2)).getValue(1);
1165 ResultVals.push_back(Chain.getValue(0));
1166 NodeTys.push_back(MVT::i32);
1168 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1169 ResultVals.push_back(Chain.getValue(0));
1171 NodeTys.push_back(MVT::i32);
1179 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1180 ResultVals.push_back(Chain.getValue(0));
1181 NodeTys.push_back(RetVT);
1185 std::vector<MVT::ValueType> Tys;
1186 Tys.push_back(MVT::f64);
1187 Tys.push_back(MVT::Other);
1188 Tys.push_back(MVT::Flag);
1189 std::vector<SDOperand> Ops;
1190 Ops.push_back(Chain);
1191 Ops.push_back(InFlag);
1192 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1193 Chain = RetVal.getValue(1);
1194 InFlag = RetVal.getValue(2);
1196 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1197 // shouldn't be necessary except that RFP cannot be live across
1198 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1199 MachineFunction &MF = DAG.getMachineFunction();
1200 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1201 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1203 Tys.push_back(MVT::Other);
1205 Ops.push_back(Chain);
1206 Ops.push_back(RetVal);
1207 Ops.push_back(StackSlot);
1208 Ops.push_back(DAG.getValueType(RetVT));
1209 Ops.push_back(InFlag);
1210 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1211 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1212 DAG.getSrcValue(NULL));
1213 Chain = RetVal.getValue(1);
1216 if (RetVT == MVT::f32 && !X86ScalarSSE)
1217 // FIXME: we would really like to remember that this FP_ROUND
1218 // operation is okay to eliminate if we allow excess FP precision.
1219 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1220 ResultVals.push_back(RetVal);
1221 NodeTys.push_back(RetVT);
1227 // If the function returns void, just return the chain.
1228 if (ResultVals.empty())
1231 // Otherwise, merge everything together with a MERGE_VALUES node.
1232 NodeTys.push_back(MVT::Other);
1233 ResultVals.push_back(Chain);
1234 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
1235 return Res.getValue(Op.ResNo);
1238 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1239 if (ReturnAddrIndex == 0) {
1240 // Set up a frame object for the return address.
1241 MachineFunction &MF = DAG.getMachineFunction();
1242 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1245 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1250 std::pair<SDOperand, SDOperand> X86TargetLowering::
1251 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1252 SelectionDAG &DAG) {
1254 if (Depth) // Depths > 0 not supported yet!
1255 Result = DAG.getConstant(0, getPointerTy());
1257 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1258 if (!isFrameAddress)
1259 // Just load the return address
1260 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1261 DAG.getSrcValue(NULL));
1263 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1264 DAG.getConstant(4, MVT::i32));
1266 return std::make_pair(Result, Chain);
1269 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1270 /// which corresponds to the condition code.
1271 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1273 default: assert(0 && "Unknown X86 conditional code!");
1274 case X86ISD::COND_A: return X86::JA;
1275 case X86ISD::COND_AE: return X86::JAE;
1276 case X86ISD::COND_B: return X86::JB;
1277 case X86ISD::COND_BE: return X86::JBE;
1278 case X86ISD::COND_E: return X86::JE;
1279 case X86ISD::COND_G: return X86::JG;
1280 case X86ISD::COND_GE: return X86::JGE;
1281 case X86ISD::COND_L: return X86::JL;
1282 case X86ISD::COND_LE: return X86::JLE;
1283 case X86ISD::COND_NE: return X86::JNE;
1284 case X86ISD::COND_NO: return X86::JNO;
1285 case X86ISD::COND_NP: return X86::JNP;
1286 case X86ISD::COND_NS: return X86::JNS;
1287 case X86ISD::COND_O: return X86::JO;
1288 case X86ISD::COND_P: return X86::JP;
1289 case X86ISD::COND_S: return X86::JS;
1293 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1294 /// specific condition code. It returns a false if it cannot do a direct
1295 /// translation. X86CC is the translated CondCode. Flip is set to true if the
1296 /// the order of comparison operands should be flipped.
1297 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1298 unsigned &X86CC, bool &Flip) {
1300 X86CC = X86ISD::COND_INVALID;
1302 switch (SetCCOpcode) {
1304 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1305 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1306 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1307 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1308 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1309 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1310 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1311 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1312 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1313 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1316 // On a floating point condition, the flags are set as follows:
1318 // 0 | 0 | 0 | X > Y
1319 // 0 | 0 | 1 | X < Y
1320 // 1 | 0 | 0 | X == Y
1321 // 1 | 1 | 1 | unordered
1322 switch (SetCCOpcode) {
1325 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1326 case ISD::SETOLT: Flip = true; // Fallthrough
1328 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1329 case ISD::SETOLE: Flip = true; // Fallthrough
1331 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1332 case ISD::SETUGT: Flip = true; // Fallthrough
1334 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1335 case ISD::SETUGE: Flip = true; // Fallthrough
1337 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1339 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1340 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1341 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1345 return X86CC != X86ISD::COND_INVALID;
1348 static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1350 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1353 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1354 /// code. Current x86 isa includes the following FP cmov instructions:
1355 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1356 static bool hasFPCMov(unsigned X86CC) {
1360 case X86ISD::COND_B:
1361 case X86ISD::COND_BE:
1362 case X86ISD::COND_E:
1363 case X86ISD::COND_P:
1364 case X86ISD::COND_A:
1365 case X86ISD::COND_AE:
1366 case X86ISD::COND_NE:
1367 case X86ISD::COND_NP:
1373 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1374 MachineBasicBlock *BB) {
1375 switch (MI->getOpcode()) {
1376 default: assert(false && "Unexpected instr type to insert");
1377 case X86::CMOV_FR32:
1378 case X86::CMOV_FR64:
1379 case X86::CMOV_V4F32:
1380 case X86::CMOV_V2F64:
1381 case X86::CMOV_V2I64: {
1382 // To "insert" a SELECT_CC instruction, we actually have to insert the
1383 // diamond control-flow pattern. The incoming instruction knows the
1384 // destination vreg to set, the condition code register to branch on, the
1385 // true/false values to select between, and a branch opcode to use.
1386 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1387 ilist<MachineBasicBlock>::iterator It = BB;
1393 // cmpTY ccX, r1, r2
1395 // fallthrough --> copy0MBB
1396 MachineBasicBlock *thisMBB = BB;
1397 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1398 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1399 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1400 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1401 MachineFunction *F = BB->getParent();
1402 F->getBasicBlockList().insert(It, copy0MBB);
1403 F->getBasicBlockList().insert(It, sinkMBB);
1404 // Update machine-CFG edges by first adding all successors of the current
1405 // block to the new block which will contain the Phi node for the select.
1406 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1407 e = BB->succ_end(); i != e; ++i)
1408 sinkMBB->addSuccessor(*i);
1409 // Next, remove all successors of the current block, and add the true
1410 // and fallthrough blocks as its successors.
1411 while(!BB->succ_empty())
1412 BB->removeSuccessor(BB->succ_begin());
1413 BB->addSuccessor(copy0MBB);
1414 BB->addSuccessor(sinkMBB);
1417 // %FalseValue = ...
1418 // # fallthrough to sinkMBB
1421 // Update machine-CFG edges
1422 BB->addSuccessor(sinkMBB);
1425 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1428 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1429 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1430 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1432 delete MI; // The pseudo instruction is gone now.
1436 case X86::FP_TO_INT16_IN_MEM:
1437 case X86::FP_TO_INT32_IN_MEM:
1438 case X86::FP_TO_INT64_IN_MEM: {
1439 // Change the floating point control register to use "round towards zero"
1440 // mode when truncating to an integer value.
1441 MachineFunction *F = BB->getParent();
1442 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1443 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1445 // Load the old value of the high byte of the control word...
1447 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
1448 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1450 // Set the high part to be round to zero...
1451 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1453 // Reload the modified control word now...
1454 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1456 // Restore the memory image of control word to original value
1457 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1459 // Get the X86 opcode to use.
1461 switch (MI->getOpcode()) {
1462 default: assert(0 && "illegal opcode!");
1463 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1464 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1465 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1469 MachineOperand &Op = MI->getOperand(0);
1470 if (Op.isRegister()) {
1471 AM.BaseType = X86AddressMode::RegBase;
1472 AM.Base.Reg = Op.getReg();
1474 AM.BaseType = X86AddressMode::FrameIndexBase;
1475 AM.Base.FrameIndex = Op.getFrameIndex();
1477 Op = MI->getOperand(1);
1478 if (Op.isImmediate())
1479 AM.Scale = Op.getImmedValue();
1480 Op = MI->getOperand(2);
1481 if (Op.isImmediate())
1482 AM.IndexReg = Op.getImmedValue();
1483 Op = MI->getOperand(3);
1484 if (Op.isGlobalAddress()) {
1485 AM.GV = Op.getGlobal();
1487 AM.Disp = Op.getImmedValue();
1489 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1491 // Reload the original control word now.
1492 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1494 delete MI; // The pseudo instruction is gone now.
1501 //===----------------------------------------------------------------------===//
1502 // X86 Custom Lowering Hooks
1503 //===----------------------------------------------------------------------===//
1505 /// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1506 /// load. For Darwin, external and weak symbols are indirect, loading the value
1507 /// at address GV rather then the value of GV itself. This means that the
1508 /// GlobalAddress must be in the base or index register of the address, not the
1509 /// GV offset field.
1510 static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1511 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1512 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1515 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1516 /// true if Op is undef or if its value falls within the specified range (L, H].
1517 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1518 if (Op.getOpcode() == ISD::UNDEF)
1521 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1522 return (Val >= Low && Val < Hi);
1525 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1526 /// true if Op is undef or if its value equal to the specified value.
1527 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1528 if (Op.getOpcode() == ISD::UNDEF)
1530 return cast<ConstantSDNode>(Op)->getValue() == Val;
1533 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1534 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1535 bool X86::isPSHUFDMask(SDNode *N) {
1536 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1538 if (N->getNumOperands() != 4)
1541 // Check if the value doesn't reference the second vector.
1542 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1543 SDOperand Arg = N->getOperand(i);
1544 if (Arg.getOpcode() == ISD::UNDEF) continue;
1545 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1546 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1553 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1554 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1555 bool X86::isPSHUFHWMask(SDNode *N) {
1556 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1558 if (N->getNumOperands() != 8)
1561 // Lower quadword copied in order.
1562 for (unsigned i = 0; i != 4; ++i) {
1563 SDOperand Arg = N->getOperand(i);
1564 if (Arg.getOpcode() == ISD::UNDEF) continue;
1565 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1566 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1570 // Upper quadword shuffled.
1571 for (unsigned i = 4; i != 8; ++i) {
1572 SDOperand Arg = N->getOperand(i);
1573 if (Arg.getOpcode() == ISD::UNDEF) continue;
1574 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1575 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1576 if (Val < 4 || Val > 7)
1583 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1584 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1585 bool X86::isPSHUFLWMask(SDNode *N) {
1586 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1588 if (N->getNumOperands() != 8)
1591 // Upper quadword copied in order.
1592 for (unsigned i = 4; i != 8; ++i)
1593 if (!isUndefOrEqual(N->getOperand(i), i))
1596 // Lower quadword shuffled.
1597 for (unsigned i = 0; i != 4; ++i)
1598 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1604 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1605 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1606 static bool isSHUFPMask(std::vector<SDOperand> &N) {
1607 unsigned NumElems = N.size();
1608 if (NumElems != 2 && NumElems != 4) return false;
1610 unsigned Half = NumElems / 2;
1611 for (unsigned i = 0; i < Half; ++i)
1612 if (!isUndefOrInRange(N[i], 0, NumElems))
1614 for (unsigned i = Half; i < NumElems; ++i)
1615 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1621 bool X86::isSHUFPMask(SDNode *N) {
1622 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1623 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1624 return ::isSHUFPMask(Ops);
1627 /// isCommutedSHUFP - Returns true if the shuffle mask is except
1628 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1629 /// half elements to come from vector 1 (which would equal the dest.) and
1630 /// the upper half to come from vector 2.
1631 static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1632 unsigned NumElems = Ops.size();
1633 if (NumElems != 2 && NumElems != 4) return false;
1635 unsigned Half = NumElems / 2;
1636 for (unsigned i = 0; i < Half; ++i)
1637 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1639 for (unsigned i = Half; i < NumElems; ++i)
1640 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1645 static bool isCommutedSHUFP(SDNode *N) {
1646 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1647 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1648 return isCommutedSHUFP(Ops);
1651 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1652 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1653 bool X86::isMOVHLPSMask(SDNode *N) {
1654 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1656 if (N->getNumOperands() != 4)
1659 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1660 return isUndefOrEqual(N->getOperand(0), 6) &&
1661 isUndefOrEqual(N->getOperand(1), 7) &&
1662 isUndefOrEqual(N->getOperand(2), 2) &&
1663 isUndefOrEqual(N->getOperand(3), 3);
1666 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1667 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1668 bool X86::isMOVLPMask(SDNode *N) {
1669 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1671 unsigned NumElems = N->getNumOperands();
1672 if (NumElems != 2 && NumElems != 4)
1675 for (unsigned i = 0; i < NumElems/2; ++i)
1676 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1679 for (unsigned i = NumElems/2; i < NumElems; ++i)
1680 if (!isUndefOrEqual(N->getOperand(i), i))
1686 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1687 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1689 bool X86::isMOVHPMask(SDNode *N) {
1690 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1692 unsigned NumElems = N->getNumOperands();
1693 if (NumElems != 2 && NumElems != 4)
1696 for (unsigned i = 0; i < NumElems/2; ++i)
1697 if (!isUndefOrEqual(N->getOperand(i), i))
1700 for (unsigned i = 0; i < NumElems/2; ++i) {
1701 SDOperand Arg = N->getOperand(i + NumElems/2);
1702 if (!isUndefOrEqual(Arg, i + NumElems))
1709 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1710 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1711 bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1712 unsigned NumElems = N.size();
1713 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1716 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1717 SDOperand BitI = N[i];
1718 SDOperand BitI1 = N[i+1];
1719 if (!isUndefOrEqual(BitI, j))
1722 if (isUndefOrEqual(BitI1, NumElems))
1725 if (!isUndefOrEqual(BitI1, j + NumElems))
1733 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1734 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1735 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1736 return ::isUNPCKLMask(Ops, V2IsSplat);
1739 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1740 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1741 bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1742 unsigned NumElems = N.size();
1743 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1746 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1747 SDOperand BitI = N[i];
1748 SDOperand BitI1 = N[i+1];
1749 if (!isUndefOrEqual(BitI, j + NumElems/2))
1752 if (isUndefOrEqual(BitI1, NumElems))
1755 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1763 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1764 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1765 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1766 return ::isUNPCKHMask(Ops, V2IsSplat);
1769 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1770 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1772 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1773 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1775 unsigned NumElems = N->getNumOperands();
1776 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1779 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1780 SDOperand BitI = N->getOperand(i);
1781 SDOperand BitI1 = N->getOperand(i+1);
1783 if (!isUndefOrEqual(BitI, j))
1785 if (!isUndefOrEqual(BitI1, j))
1792 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1793 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1794 /// MOVSD, and MOVD, i.e. setting the lowest element.
1795 static bool isMOVLMask(std::vector<SDOperand> &N) {
1796 unsigned NumElems = N.size();
1797 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1800 if (!isUndefOrEqual(N[0], NumElems))
1803 for (unsigned i = 1; i < NumElems; ++i) {
1804 SDOperand Arg = N[i];
1805 if (!isUndefOrEqual(Arg, i))
1812 bool X86::isMOVLMask(SDNode *N) {
1813 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1814 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1815 return ::isMOVLMask(Ops);
1818 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1819 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1820 /// element of vector 2 and the other elements to come from vector 1 in order.
1821 static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
1822 unsigned NumElems = Ops.size();
1823 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1826 if (!isUndefOrEqual(Ops[0], 0))
1829 for (unsigned i = 1; i < NumElems; ++i) {
1830 SDOperand Arg = Ops[i];
1832 if (!isUndefOrEqual(Arg, NumElems))
1835 if (!isUndefOrEqual(Arg, i+NumElems))
1843 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
1844 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1845 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1846 return isCommutedMOVL(Ops, V2IsSplat);
1849 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1850 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1851 bool X86::isMOVSHDUPMask(SDNode *N) {
1852 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1854 if (N->getNumOperands() != 4)
1857 // Expect 1, 1, 3, 3
1858 for (unsigned i = 0; i < 2; ++i) {
1859 SDOperand Arg = N->getOperand(i);
1860 if (Arg.getOpcode() == ISD::UNDEF) continue;
1861 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1862 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1863 if (Val != 1) return false;
1867 for (unsigned i = 2; i < 4; ++i) {
1868 SDOperand Arg = N->getOperand(i);
1869 if (Arg.getOpcode() == ISD::UNDEF) continue;
1870 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1871 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1872 if (Val != 3) return false;
1876 // Don't use movshdup if it can be done with a shufps.
1880 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1881 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1882 bool X86::isMOVSLDUPMask(SDNode *N) {
1883 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1885 if (N->getNumOperands() != 4)
1888 // Expect 0, 0, 2, 2
1889 for (unsigned i = 0; i < 2; ++i) {
1890 SDOperand Arg = N->getOperand(i);
1891 if (Arg.getOpcode() == ISD::UNDEF) continue;
1892 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1893 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1894 if (Val != 0) return false;
1898 for (unsigned i = 2; i < 4; ++i) {
1899 SDOperand Arg = N->getOperand(i);
1900 if (Arg.getOpcode() == ISD::UNDEF) continue;
1901 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1902 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1903 if (Val != 2) return false;
1907 // Don't use movshdup if it can be done with a shufps.
1911 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1912 /// a splat of a single element.
1913 static bool isSplatMask(SDNode *N) {
1914 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1916 // This is a splat operation if each element of the permute is the same, and
1917 // if the value doesn't reference the second vector.
1918 unsigned NumElems = N->getNumOperands();
1919 SDOperand ElementBase;
1921 for (; i != NumElems; ++i) {
1922 SDOperand Elt = N->getOperand(i);
1923 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1929 if (!ElementBase.Val)
1932 for (; i != NumElems; ++i) {
1933 SDOperand Arg = N->getOperand(i);
1934 if (Arg.getOpcode() == ISD::UNDEF) continue;
1935 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1936 if (Arg != ElementBase) return false;
1939 // Make sure it is a splat of the first vector operand.
1940 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
1943 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1944 /// a splat of a single element and it's a 2 or 4 element mask.
1945 bool X86::isSplatMask(SDNode *N) {
1946 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1948 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
1949 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1951 return ::isSplatMask(N);
1954 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1955 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1957 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
1958 unsigned NumOperands = N->getNumOperands();
1959 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1961 for (unsigned i = 0; i < NumOperands; ++i) {
1963 SDOperand Arg = N->getOperand(NumOperands-i-1);
1964 if (Arg.getOpcode() != ISD::UNDEF)
1965 Val = cast<ConstantSDNode>(Arg)->getValue();
1966 if (Val >= NumOperands) Val -= NumOperands;
1968 if (i != NumOperands - 1)
1975 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1976 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1978 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1980 // 8 nodes, but we only care about the last 4.
1981 for (unsigned i = 7; i >= 4; --i) {
1983 SDOperand Arg = N->getOperand(i);
1984 if (Arg.getOpcode() != ISD::UNDEF)
1985 Val = cast<ConstantSDNode>(Arg)->getValue();
1994 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1995 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1997 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1999 // 8 nodes, but we only care about the first 4.
2000 for (int i = 3; i >= 0; --i) {
2002 SDOperand Arg = N->getOperand(i);
2003 if (Arg.getOpcode() != ISD::UNDEF)
2004 Val = cast<ConstantSDNode>(Arg)->getValue();
2013 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2014 /// specifies a 8 element shuffle that can be broken into a pair of
2015 /// PSHUFHW and PSHUFLW.
2016 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2017 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2019 if (N->getNumOperands() != 8)
2022 // Lower quadword shuffled.
2023 for (unsigned i = 0; i != 4; ++i) {
2024 SDOperand Arg = N->getOperand(i);
2025 if (Arg.getOpcode() == ISD::UNDEF) continue;
2026 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2027 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2032 // Upper quadword shuffled.
2033 for (unsigned i = 4; i != 8; ++i) {
2034 SDOperand Arg = N->getOperand(i);
2035 if (Arg.getOpcode() == ISD::UNDEF) continue;
2036 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2037 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2038 if (Val < 4 || Val > 7)
2045 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2046 /// values in ther permute mask.
2047 static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2048 SDOperand V1 = Op.getOperand(0);
2049 SDOperand V2 = Op.getOperand(1);
2050 SDOperand Mask = Op.getOperand(2);
2051 MVT::ValueType VT = Op.getValueType();
2052 MVT::ValueType MaskVT = Mask.getValueType();
2053 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2054 unsigned NumElems = Mask.getNumOperands();
2055 std::vector<SDOperand> MaskVec;
2057 for (unsigned i = 0; i != NumElems; ++i) {
2058 SDOperand Arg = Mask.getOperand(i);
2059 if (Arg.getOpcode() == ISD::UNDEF) {
2060 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2063 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2064 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2066 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2068 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2071 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2072 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2075 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2076 /// match movhlps. The lower half elements should come from upper half of
2077 /// V1 (and in order), and the upper half elements should come from the upper
2078 /// half of V2 (and in order).
2079 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2080 unsigned NumElems = Mask->getNumOperands();
2083 for (unsigned i = 0, e = 2; i != e; ++i)
2084 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2086 for (unsigned i = 2; i != 4; ++i)
2087 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2092 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2093 /// is promoted to a vector.
2094 static inline bool isScalarLoadToVector(SDNode *N) {
2095 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2096 N = N->getOperand(0).Val;
2097 return (N->getOpcode() == ISD::LOAD);
2102 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2103 /// match movlp{s|d}. The lower half elements should come from lower half of
2104 /// V1 (and in order), and the upper half elements should come from the upper
2105 /// half of V2 (and in order). And since V1 will become the source of the
2106 /// MOVLP, it must be either a vector load or a scalar load to vector.
2107 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2108 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2111 unsigned NumElems = Mask->getNumOperands();
2112 if (NumElems != 2 && NumElems != 4)
2114 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2115 if (!isUndefOrEqual(Mask->getOperand(i), i))
2117 for (unsigned i = NumElems/2; i != NumElems; ++i)
2118 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2123 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2125 static bool isSplatVector(SDNode *N) {
2126 if (N->getOpcode() != ISD::BUILD_VECTOR)
2129 SDOperand SplatValue = N->getOperand(0);
2130 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2131 if (N->getOperand(i) != SplatValue)
2136 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2137 /// that point to V2 points to its first element.
2138 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2139 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2141 bool Changed = false;
2142 std::vector<SDOperand> MaskVec;
2143 unsigned NumElems = Mask.getNumOperands();
2144 for (unsigned i = 0; i != NumElems; ++i) {
2145 SDOperand Arg = Mask.getOperand(i);
2146 if (Arg.getOpcode() != ISD::UNDEF) {
2147 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2148 if (Val > NumElems) {
2149 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2153 MaskVec.push_back(Arg);
2157 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2161 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2162 /// operation of specified width.
2163 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2164 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2165 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2167 std::vector<SDOperand> MaskVec;
2168 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2169 for (unsigned i = 1; i != NumElems; ++i)
2170 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2171 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2174 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2175 /// of specified width.
2176 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2177 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2178 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2179 std::vector<SDOperand> MaskVec;
2180 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2181 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2182 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2184 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2187 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2188 /// of specified width.
2189 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2190 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2191 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2192 unsigned Half = NumElems/2;
2193 std::vector<SDOperand> MaskVec;
2194 for (unsigned i = 0; i != Half; ++i) {
2195 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2196 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2198 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2201 /// getZeroVector - Returns a vector of specified type with all zero elements.
2203 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2204 assert(MVT::isVector(VT) && "Expected a vector type");
2205 unsigned NumElems = getVectorNumElements(VT);
2206 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2207 bool isFP = MVT::isFloatingPoint(EVT);
2208 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2209 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2210 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2213 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2215 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2216 SDOperand V1 = Op.getOperand(0);
2217 SDOperand Mask = Op.getOperand(2);
2218 MVT::ValueType VT = Op.getValueType();
2219 unsigned NumElems = Mask.getNumOperands();
2220 Mask = getUnpacklMask(NumElems, DAG);
2221 while (NumElems != 4) {
2222 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2225 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2227 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2228 Mask = getZeroVector(MaskVT, DAG);
2229 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2230 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2231 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2234 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2236 static inline bool isZeroNode(SDOperand Elt) {
2237 return ((isa<ConstantSDNode>(Elt) &&
2238 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2239 (isa<ConstantFPSDNode>(Elt) &&
2240 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2243 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2244 /// vector and zero or undef vector.
2245 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2246 unsigned NumElems, unsigned Idx,
2247 bool isZero, SelectionDAG &DAG) {
2248 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2249 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2250 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2251 SDOperand Zero = DAG.getConstant(0, EVT);
2252 std::vector<SDOperand> MaskVec(NumElems, Zero);
2253 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2254 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2255 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2258 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2260 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2261 unsigned NumNonZero, unsigned NumZero,
2262 SelectionDAG &DAG) {
2268 for (unsigned i = 0; i < 16; ++i) {
2269 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2270 if (ThisIsNonZero && First) {
2272 V = getZeroVector(MVT::v8i16, DAG);
2274 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2279 SDOperand ThisElt(0, 0), LastElt(0, 0);
2280 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2281 if (LastIsNonZero) {
2282 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2284 if (ThisIsNonZero) {
2285 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2286 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2287 ThisElt, DAG.getConstant(8, MVT::i8));
2289 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2294 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2295 DAG.getConstant(i/2, MVT::i32));
2299 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2302 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2304 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2305 unsigned NumNonZero, unsigned NumZero,
2306 SelectionDAG &DAG) {
2312 for (unsigned i = 0; i < 8; ++i) {
2313 bool isNonZero = (NonZeros & (1 << i)) != 0;
2317 V = getZeroVector(MVT::v8i16, DAG);
2319 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2322 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2323 DAG.getConstant(i, MVT::i32));
2331 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2332 // All zero's are handled with pxor.
2333 if (ISD::isBuildVectorAllZeros(Op.Val))
2336 // All one's are handled with pcmpeqd.
2337 if (ISD::isBuildVectorAllOnes(Op.Val))
2340 MVT::ValueType VT = Op.getValueType();
2341 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2342 unsigned EVTBits = MVT::getSizeInBits(EVT);
2344 unsigned NumElems = Op.getNumOperands();
2345 unsigned NumZero = 0;
2346 unsigned NumNonZero = 0;
2347 unsigned NonZeros = 0;
2348 std::set<SDOperand> Values;
2349 for (unsigned i = 0; i < NumElems; ++i) {
2350 SDOperand Elt = Op.getOperand(i);
2351 if (Elt.getOpcode() != ISD::UNDEF) {
2353 if (isZeroNode(Elt))
2356 NonZeros |= (1 << i);
2362 if (NumNonZero == 0)
2363 // Must be a mix of zero and undef. Return a zero vector.
2364 return getZeroVector(VT, DAG);
2366 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2367 if (Values.size() == 1)
2370 // Special case for single non-zero element.
2371 if (NumNonZero == 1) {
2372 unsigned Idx = CountTrailingZeros_32(NonZeros);
2373 SDOperand Item = Op.getOperand(Idx);
2374 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2376 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2377 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2380 if (EVTBits == 32) {
2381 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2382 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2384 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2385 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2386 std::vector<SDOperand> MaskVec;
2387 for (unsigned i = 0; i < NumElems; i++)
2388 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2389 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2390 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2391 DAG.getNode(ISD::UNDEF, VT), Mask);
2395 // Let legalizer expand 2-widde build_vector's.
2399 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2401 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2402 if (V.Val) return V;
2405 if (EVTBits == 16) {
2406 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2407 if (V.Val) return V;
2410 // If element VT is == 32 bits, turn it into a number of shuffles.
2411 std::vector<SDOperand> V(NumElems);
2412 if (NumElems == 4 && NumZero > 0) {
2413 for (unsigned i = 0; i < 4; ++i) {
2414 bool isZero = !(NonZeros & (1 << i));
2416 V[i] = getZeroVector(VT, DAG);
2418 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2421 for (unsigned i = 0; i < 2; ++i) {
2422 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2425 V[i] = V[i*2]; // Must be a zero vector.
2428 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2429 getMOVLMask(NumElems, DAG));
2432 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2433 getMOVLMask(NumElems, DAG));
2436 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2437 getUnpacklMask(NumElems, DAG));
2442 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2443 // clears the upper bits.
2444 // FIXME: we can do the same for v4f32 case when we know both parts of
2445 // the lower half come from scalar_to_vector (loadf32). We should do
2446 // that in post legalizer dag combiner with target specific hooks.
2447 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2449 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2450 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2451 std::vector<SDOperand> MaskVec;
2452 bool Reverse = (NonZeros & 0x3) == 2;
2453 for (unsigned i = 0; i < 2; ++i)
2455 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2457 MaskVec.push_back(DAG.getConstant(i, EVT));
2458 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2459 for (unsigned i = 0; i < 2; ++i)
2461 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2463 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2464 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2465 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2468 if (Values.size() > 2) {
2469 // Expand into a number of unpckl*.
2471 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2472 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2473 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2474 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2475 for (unsigned i = 0; i < NumElems; ++i)
2476 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2478 while (NumElems != 0) {
2479 for (unsigned i = 0; i < NumElems; ++i)
2480 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2491 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2492 SDOperand V1 = Op.getOperand(0);
2493 SDOperand V2 = Op.getOperand(1);
2494 SDOperand PermMask = Op.getOperand(2);
2495 MVT::ValueType VT = Op.getValueType();
2496 unsigned NumElems = PermMask.getNumOperands();
2497 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2498 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2500 if (isSplatMask(PermMask.Val)) {
2501 if (NumElems <= 4) return Op;
2502 // Promote it to a v4i32 splat.
2503 return PromoteSplat(Op, DAG);
2506 if (X86::isMOVLMask(PermMask.Val))
2507 return (V1IsUndef) ? V2 : Op;
2509 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2510 X86::isMOVSLDUPMask(PermMask.Val) ||
2511 X86::isMOVHLPSMask(PermMask.Val) ||
2512 X86::isMOVHPMask(PermMask.Val) ||
2513 X86::isMOVLPMask(PermMask.Val))
2516 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2517 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2518 return CommuteVectorShuffle(Op, DAG);
2520 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2521 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2522 if (V1IsSplat && !V2IsSplat) {
2523 Op = CommuteVectorShuffle(Op, DAG);
2524 V1 = Op.getOperand(0);
2525 V2 = Op.getOperand(1);
2526 PermMask = Op.getOperand(2);
2530 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2531 if (V2IsUndef) return V1;
2532 Op = CommuteVectorShuffle(Op, DAG);
2533 V1 = Op.getOperand(0);
2534 V2 = Op.getOperand(1);
2535 PermMask = Op.getOperand(2);
2537 // V2 is a splat, so the mask may be malformed. That is, it may point
2538 // to any V2 element. The instruction selectior won't like this. Get
2539 // a corrected mask and commute to form a proper MOVS{S|D}.
2540 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2541 if (NewMask.Val != PermMask.Val)
2542 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2547 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2548 X86::isUNPCKLMask(PermMask.Val) ||
2549 X86::isUNPCKHMask(PermMask.Val))
2553 // Normalize mask so all entries that point to V2 points to its first
2554 // element then try to match unpck{h|l} again. If match, return a
2555 // new vector_shuffle with the corrected mask.
2556 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2557 if (NewMask.Val != PermMask.Val) {
2558 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2559 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2560 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2561 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2562 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2563 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2568 // Normalize the node to match x86 shuffle ops if needed
2569 if (V2.getOpcode() != ISD::UNDEF)
2570 if (isCommutedSHUFP(PermMask.Val)) {
2571 Op = CommuteVectorShuffle(Op, DAG);
2572 V1 = Op.getOperand(0);
2573 V2 = Op.getOperand(1);
2574 PermMask = Op.getOperand(2);
2577 // If VT is integer, try PSHUF* first, then SHUFP*.
2578 if (MVT::isInteger(VT)) {
2579 if (X86::isPSHUFDMask(PermMask.Val) ||
2580 X86::isPSHUFHWMask(PermMask.Val) ||
2581 X86::isPSHUFLWMask(PermMask.Val)) {
2582 if (V2.getOpcode() != ISD::UNDEF)
2583 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2584 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2588 if (X86::isSHUFPMask(PermMask.Val))
2591 // Handle v8i16 shuffle high / low shuffle node pair.
2592 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2593 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2594 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2595 std::vector<SDOperand> MaskVec;
2596 for (unsigned i = 0; i != 4; ++i)
2597 MaskVec.push_back(PermMask.getOperand(i));
2598 for (unsigned i = 4; i != 8; ++i)
2599 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2600 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2601 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2603 for (unsigned i = 0; i != 4; ++i)
2604 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2605 for (unsigned i = 4; i != 8; ++i)
2606 MaskVec.push_back(PermMask.getOperand(i));
2607 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2608 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2611 // Floating point cases in the other order.
2612 if (X86::isSHUFPMask(PermMask.Val))
2614 if (X86::isPSHUFDMask(PermMask.Val) ||
2615 X86::isPSHUFHWMask(PermMask.Val) ||
2616 X86::isPSHUFLWMask(PermMask.Val)) {
2617 if (V2.getOpcode() != ISD::UNDEF)
2618 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2619 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2624 if (NumElems == 4) {
2625 MVT::ValueType MaskVT = PermMask.getValueType();
2626 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2627 std::vector<std::pair<int, int> > Locs;
2628 Locs.reserve(NumElems);
2629 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2630 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2633 // If no more than two elements come from either vector. This can be
2634 // implemented with two shuffles. First shuffle gather the elements.
2635 // The second shuffle, which takes the first shuffle as both of its
2636 // vector operands, put the elements into the right order.
2637 for (unsigned i = 0; i != NumElems; ++i) {
2638 SDOperand Elt = PermMask.getOperand(i);
2639 if (Elt.getOpcode() == ISD::UNDEF) {
2640 Locs[i] = std::make_pair(-1, -1);
2642 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2643 if (Val < NumElems) {
2644 Locs[i] = std::make_pair(0, NumLo);
2648 Locs[i] = std::make_pair(1, NumHi);
2649 if (2+NumHi < NumElems)
2650 Mask1[2+NumHi] = Elt;
2655 if (NumLo <= 2 && NumHi <= 2) {
2656 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2657 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2658 for (unsigned i = 0; i != NumElems; ++i) {
2659 if (Locs[i].first == -1)
2662 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2663 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2664 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2668 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2669 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2672 // Break it into (shuffle shuffle_hi, shuffle_lo).
2674 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2675 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2676 std::vector<SDOperand> *MaskPtr = &LoMask;
2677 unsigned MaskIdx = 0;
2679 unsigned HiIdx = NumElems/2;
2680 for (unsigned i = 0; i != NumElems; ++i) {
2681 if (i == NumElems/2) {
2687 SDOperand Elt = PermMask.getOperand(i);
2688 if (Elt.getOpcode() == ISD::UNDEF) {
2689 Locs[i] = std::make_pair(-1, -1);
2690 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2691 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2692 (*MaskPtr)[LoIdx] = Elt;
2695 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2696 (*MaskPtr)[HiIdx] = Elt;
2701 SDOperand LoShuffle =
2702 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2703 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2704 SDOperand HiShuffle =
2705 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2706 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
2707 std::vector<SDOperand> MaskOps;
2708 for (unsigned i = 0; i != NumElems; ++i) {
2709 if (Locs[i].first == -1) {
2710 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2712 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2713 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2716 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2717 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2724 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2725 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2728 MVT::ValueType VT = Op.getValueType();
2729 // TODO: handle v16i8.
2730 if (MVT::getSizeInBits(VT) == 16) {
2731 // Transform it so it match pextrw which produces a 32-bit result.
2732 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2733 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2734 Op.getOperand(0), Op.getOperand(1));
2735 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2736 DAG.getValueType(VT));
2737 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2738 } else if (MVT::getSizeInBits(VT) == 32) {
2739 SDOperand Vec = Op.getOperand(0);
2740 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2744 // SHUFPS the element to the lowest double word, then movss.
2745 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2746 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2747 MVT::getVectorBaseType(MaskVT));
2748 std::vector<SDOperand> IdxVec;
2749 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2750 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2751 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2752 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2753 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2754 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2756 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2757 DAG.getConstant(0, MVT::i32));
2758 } else if (MVT::getSizeInBits(VT) == 64) {
2759 SDOperand Vec = Op.getOperand(0);
2760 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2764 // UNPCKHPD the element to the lowest double word, then movsd.
2765 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2766 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2767 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2768 std::vector<SDOperand> IdxVec;
2769 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2770 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2771 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2772 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2773 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2774 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2775 DAG.getConstant(0, MVT::i32));
2782 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2783 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2784 // as its second argument.
2785 MVT::ValueType VT = Op.getValueType();
2786 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2787 SDOperand N0 = Op.getOperand(0);
2788 SDOperand N1 = Op.getOperand(1);
2789 SDOperand N2 = Op.getOperand(2);
2790 if (MVT::getSizeInBits(BaseVT) == 16) {
2791 if (N1.getValueType() != MVT::i32)
2792 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2793 if (N2.getValueType() != MVT::i32)
2794 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2795 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2796 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2797 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2800 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2801 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2802 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2803 std::vector<SDOperand> MaskVec;
2804 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2805 for (unsigned i = 1; i <= 3; ++i)
2806 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2807 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2808 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2810 // Use two pinsrw instructions to insert a 32 bit value.
2812 if (MVT::isFloatingPoint(N1.getValueType())) {
2813 if (N1.getOpcode() == ISD::LOAD) {
2814 // Just load directly from f32mem to GR32.
2815 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2818 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2819 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2820 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2821 DAG.getConstant(0, MVT::i32));
2824 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2825 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2826 DAG.getConstant(Idx, MVT::i32));
2827 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2828 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2829 DAG.getConstant(Idx+1, MVT::i32));
2830 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2838 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2839 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2840 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2843 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2844 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2845 // one of the above mentioned nodes. It has to be wrapped because otherwise
2846 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2847 // be used to form addressing mode. These wrapped nodes will be selected
2850 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2851 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2852 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2853 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2854 CP->getAlignment()));
2855 if (Subtarget->isTargetDarwin()) {
2856 // With PIC, the address is actually $g + Offset.
2857 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2858 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2859 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2866 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2867 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2868 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2869 DAG.getTargetGlobalAddress(GV,
2871 if (Subtarget->isTargetDarwin()) {
2872 // With PIC, the address is actually $g + Offset.
2873 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2874 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2875 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2878 // For Darwin, external and weak symbols are indirect, so we want to load
2879 // the value at address GV, not the value of GV itself. This means that
2880 // the GlobalAddress must be in the base or index register of the address,
2881 // not the GV offset field.
2882 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2883 DarwinGVRequiresExtraLoad(GV))
2884 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2885 Result, DAG.getSrcValue(NULL));
2892 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2893 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2894 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2895 DAG.getTargetExternalSymbol(Sym,
2897 if (Subtarget->isTargetDarwin()) {
2898 // With PIC, the address is actually $g + Offset.
2899 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2900 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2901 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2908 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
2909 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2910 "Not an i64 shift!");
2911 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2912 SDOperand ShOpLo = Op.getOperand(0);
2913 SDOperand ShOpHi = Op.getOperand(1);
2914 SDOperand ShAmt = Op.getOperand(2);
2915 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
2916 DAG.getConstant(31, MVT::i8))
2917 : DAG.getConstant(0, MVT::i32);
2919 SDOperand Tmp2, Tmp3;
2920 if (Op.getOpcode() == ISD::SHL_PARTS) {
2921 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2922 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2924 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
2925 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
2928 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2929 ShAmt, DAG.getConstant(32, MVT::i8));
2932 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
2934 std::vector<MVT::ValueType> Tys;
2935 Tys.push_back(MVT::i32);
2936 Tys.push_back(MVT::Flag);
2937 std::vector<SDOperand> Ops;
2938 if (Op.getOpcode() == ISD::SHL_PARTS) {
2939 Ops.push_back(Tmp2);
2940 Ops.push_back(Tmp3);
2942 Ops.push_back(InFlag);
2943 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2944 InFlag = Hi.getValue(1);
2947 Ops.push_back(Tmp3);
2948 Ops.push_back(Tmp1);
2950 Ops.push_back(InFlag);
2951 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2953 Ops.push_back(Tmp2);
2954 Ops.push_back(Tmp3);
2956 Ops.push_back(InFlag);
2957 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2958 InFlag = Lo.getValue(1);
2961 Ops.push_back(Tmp3);
2962 Ops.push_back(Tmp1);
2964 Ops.push_back(InFlag);
2965 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2969 Tys.push_back(MVT::i32);
2970 Tys.push_back(MVT::i32);
2974 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
2977 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2978 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2979 Op.getOperand(0).getValueType() >= MVT::i16 &&
2980 "Unknown SINT_TO_FP to lower!");
2983 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2984 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
2985 MachineFunction &MF = DAG.getMachineFunction();
2986 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2987 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2988 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2989 DAG.getEntryNode(), Op.getOperand(0),
2990 StackSlot, DAG.getSrcValue(NULL));
2993 std::vector<MVT::ValueType> Tys;
2994 Tys.push_back(MVT::f64);
2995 Tys.push_back(MVT::Other);
2996 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
2997 std::vector<SDOperand> Ops;
2998 Ops.push_back(Chain);
2999 Ops.push_back(StackSlot);
3000 Ops.push_back(DAG.getValueType(SrcVT));
3001 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3005 Chain = Result.getValue(1);
3006 SDOperand InFlag = Result.getValue(2);
3008 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3009 // shouldn't be necessary except that RFP cannot be live across
3010 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3011 MachineFunction &MF = DAG.getMachineFunction();
3012 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3013 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3014 std::vector<MVT::ValueType> Tys;
3015 Tys.push_back(MVT::Other);
3016 std::vector<SDOperand> Ops;
3017 Ops.push_back(Chain);
3018 Ops.push_back(Result);
3019 Ops.push_back(StackSlot);
3020 Ops.push_back(DAG.getValueType(Op.getValueType()));
3021 Ops.push_back(InFlag);
3022 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3023 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3024 DAG.getSrcValue(NULL));
3030 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3031 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3032 "Unknown FP_TO_SINT to lower!");
3033 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3035 MachineFunction &MF = DAG.getMachineFunction();
3036 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3037 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3038 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3041 switch (Op.getValueType()) {
3042 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3043 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3044 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3045 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3048 SDOperand Chain = DAG.getEntryNode();
3049 SDOperand Value = Op.getOperand(0);
3051 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3052 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3053 DAG.getSrcValue(0));
3054 std::vector<MVT::ValueType> Tys;
3055 Tys.push_back(MVT::f64);
3056 Tys.push_back(MVT::Other);
3057 std::vector<SDOperand> Ops;
3058 Ops.push_back(Chain);
3059 Ops.push_back(StackSlot);
3060 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3061 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3062 Chain = Value.getValue(1);
3063 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3064 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3067 // Build the FP_TO_INT*_IN_MEM
3068 std::vector<SDOperand> Ops;
3069 Ops.push_back(Chain);
3070 Ops.push_back(Value);
3071 Ops.push_back(StackSlot);
3072 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
3075 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3076 DAG.getSrcValue(NULL));
3079 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3080 MVT::ValueType VT = Op.getValueType();
3081 const Type *OpNTy = MVT::getTypeForValueType(VT);
3082 std::vector<Constant*> CV;
3083 if (VT == MVT::f64) {
3084 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3085 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3087 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3088 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3089 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3090 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3092 Constant *CS = ConstantStruct::get(CV);
3093 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3095 = DAG.getNode(X86ISD::LOAD_PACK,
3096 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3097 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3100 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3101 MVT::ValueType VT = Op.getValueType();
3102 const Type *OpNTy = MVT::getTypeForValueType(VT);
3103 std::vector<Constant*> CV;
3104 if (VT == MVT::f64) {
3105 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3106 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3108 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3109 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3110 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3111 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3113 Constant *CS = ConstantStruct::get(CV);
3114 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3115 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3116 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3117 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3120 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3121 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3123 SDOperand CC = Op.getOperand(2);
3124 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3125 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3128 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3130 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3131 Op.getOperand(1), Op.getOperand(0));
3133 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3134 Op.getOperand(0), Op.getOperand(1));
3135 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3136 DAG.getConstant(X86CC, MVT::i8), Cond);
3138 assert(isFP && "Illegal integer SetCC!");
3140 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3141 Op.getOperand(0), Op.getOperand(1));
3142 std::vector<MVT::ValueType> Tys;
3143 std::vector<SDOperand> Ops;
3144 switch (SetCCOpcode) {
3145 default: assert(false && "Illegal floating point SetCC!");
3146 case ISD::SETOEQ: { // !PF & ZF
3147 Tys.push_back(MVT::i8);
3148 Tys.push_back(MVT::Flag);
3149 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3150 Ops.push_back(Cond);
3151 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3152 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3153 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3155 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3157 case ISD::SETUNE: { // PF | !ZF
3158 Tys.push_back(MVT::i8);
3159 Tys.push_back(MVT::Flag);
3160 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3161 Ops.push_back(Cond);
3162 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3163 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3164 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3166 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3172 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3173 MVT::ValueType VT = Op.getValueType();
3174 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3175 bool addTest = false;
3176 SDOperand Op0 = Op.getOperand(0);
3178 if (Op0.getOpcode() == ISD::SETCC)
3179 Op0 = LowerOperation(Op0, DAG);
3181 if (Op0.getOpcode() == X86ISD::SETCC) {
3182 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3183 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3184 // have another use it will be eliminated.
3185 // If the X86ISD::SETCC has more than one use, then it's probably better
3186 // to use a test instead of duplicating the X86ISD::CMP (for register
3187 // pressure reason).
3188 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3189 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3190 CmpOpc == X86ISD::UCOMI) {
3191 if (!Op0.hasOneUse()) {
3192 std::vector<MVT::ValueType> Tys;
3193 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3194 Tys.push_back(Op0.Val->getValueType(i));
3195 std::vector<SDOperand> Ops;
3196 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3197 Ops.push_back(Op0.getOperand(i));
3198 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3201 CC = Op0.getOperand(0);
3202 Cond = Op0.getOperand(1);
3203 // Make a copy as flag result cannot be used by more than one.
3204 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3205 Cond.getOperand(0), Cond.getOperand(1));
3207 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3214 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3215 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
3218 std::vector<MVT::ValueType> Tys;
3219 Tys.push_back(Op.getValueType());
3220 Tys.push_back(MVT::Flag);
3221 std::vector<SDOperand> Ops;
3222 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3223 // condition is true.
3224 Ops.push_back(Op.getOperand(2));
3225 Ops.push_back(Op.getOperand(1));
3227 Ops.push_back(Cond);
3228 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3231 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3232 bool addTest = false;
3233 SDOperand Cond = Op.getOperand(1);
3234 SDOperand Dest = Op.getOperand(2);
3236 if (Cond.getOpcode() == ISD::SETCC)
3237 Cond = LowerOperation(Cond, DAG);
3239 if (Cond.getOpcode() == X86ISD::SETCC) {
3240 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3241 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3242 // have another use it will be eliminated.
3243 // If the X86ISD::SETCC has more than one use, then it's probably better
3244 // to use a test instead of duplicating the X86ISD::CMP (for register
3245 // pressure reason).
3246 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3247 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3248 CmpOpc == X86ISD::UCOMI) {
3249 if (!Cond.hasOneUse()) {
3250 std::vector<MVT::ValueType> Tys;
3251 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3252 Tys.push_back(Cond.Val->getValueType(i));
3253 std::vector<SDOperand> Ops;
3254 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3255 Ops.push_back(Cond.getOperand(i));
3256 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3259 CC = Cond.getOperand(0);
3260 Cond = Cond.getOperand(1);
3261 // Make a copy as flag result cannot be used by more than one.
3262 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3263 Cond.getOperand(0), Cond.getOperand(1));
3270 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3271 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
3273 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3274 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3277 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3278 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3279 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3280 DAG.getTargetJumpTable(JT->getIndex(),
3282 if (Subtarget->isTargetDarwin()) {
3283 // With PIC, the address is actually $g + Offset.
3284 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3285 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3286 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3293 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3294 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3295 if (CallingConv == CallingConv::Fast && EnableFastCC)
3296 return LowerFastCCCallTo(Op, DAG);
3298 return LowerCCCCallTo(Op, DAG);
3301 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3304 switch(Op.getNumOperands()) {
3306 assert(0 && "Do not know how to return this many arguments!");
3308 case 1: // ret void.
3309 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
3310 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
3312 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
3314 if (MVT::isVector(ArgVT)) {
3315 // Integer or FP vector result -> XMM0.
3316 if (DAG.getMachineFunction().liveout_empty())
3317 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3318 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3320 } else if (MVT::isInteger(ArgVT)) {
3321 // Integer result -> EAX
3322 if (DAG.getMachineFunction().liveout_empty())
3323 DAG.getMachineFunction().addLiveOut(X86::EAX);
3325 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3327 } else if (!X86ScalarSSE) {
3328 // FP return with fp-stack value.
3329 if (DAG.getMachineFunction().liveout_empty())
3330 DAG.getMachineFunction().addLiveOut(X86::ST0);
3332 std::vector<MVT::ValueType> Tys;
3333 Tys.push_back(MVT::Other);
3334 Tys.push_back(MVT::Flag);
3335 std::vector<SDOperand> Ops;
3336 Ops.push_back(Op.getOperand(0));
3337 Ops.push_back(Op.getOperand(1));
3338 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3340 // FP return with ScalarSSE (return on fp-stack).
3341 if (DAG.getMachineFunction().liveout_empty())
3342 DAG.getMachineFunction().addLiveOut(X86::ST0);
3345 SDOperand Chain = Op.getOperand(0);
3346 SDOperand Value = Op.getOperand(1);
3348 if (Value.getOpcode() == ISD::LOAD &&
3349 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
3350 Chain = Value.getOperand(0);
3351 MemLoc = Value.getOperand(1);
3353 // Spill the value to memory and reload it into top of stack.
3354 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3355 MachineFunction &MF = DAG.getMachineFunction();
3356 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3357 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3358 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3359 Value, MemLoc, DAG.getSrcValue(0));
3361 std::vector<MVT::ValueType> Tys;
3362 Tys.push_back(MVT::f64);
3363 Tys.push_back(MVT::Other);
3364 std::vector<SDOperand> Ops;
3365 Ops.push_back(Chain);
3366 Ops.push_back(MemLoc);
3367 Ops.push_back(DAG.getValueType(ArgVT));
3368 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3370 Tys.push_back(MVT::Other);
3371 Tys.push_back(MVT::Flag);
3373 Ops.push_back(Copy.getValue(1));
3374 Ops.push_back(Copy);
3375 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3380 if (DAG.getMachineFunction().liveout_empty()) {
3381 DAG.getMachineFunction().addLiveOut(X86::EAX);
3382 DAG.getMachineFunction().addLiveOut(X86::EDX);
3385 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3387 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3390 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3391 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3396 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3397 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3398 if (CC == CallingConv::Fast && EnableFastCC)
3399 return LowerFastCCArguments(Op, DAG);
3401 return LowerCCCArguments(Op, DAG);
3404 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3405 SDOperand InFlag(0, 0);
3406 SDOperand Chain = Op.getOperand(0);
3408 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3409 if (Align == 0) Align = 1;
3411 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3412 // If not DWORD aligned, call memset if size is less than the threshold.
3413 // It knows how to align to the right boundary first.
3414 if ((Align & 3) != 0 ||
3415 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3416 MVT::ValueType IntPtr = getPointerTy();
3417 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3418 std::vector<std::pair<SDOperand, const Type*> > Args;
3419 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3420 // Extend the ubyte argument to be an int value for the call.
3421 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3422 Args.push_back(std::make_pair(Val, IntPtrTy));
3423 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3424 std::pair<SDOperand,SDOperand> CallResult =
3425 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3426 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3427 return CallResult.second;
3432 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3433 unsigned BytesLeft = 0;
3434 bool TwoRepStos = false;
3437 unsigned Val = ValC->getValue() & 255;
3439 // If the value is a constant, then we can potentially use larger sets.
3440 switch (Align & 3) {
3441 case 2: // WORD aligned
3443 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3444 BytesLeft = I->getValue() % 2;
3445 Val = (Val << 8) | Val;
3448 case 0: // DWORD aligned
3451 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3452 BytesLeft = I->getValue() % 4;
3454 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3455 DAG.getConstant(2, MVT::i8));
3458 Val = (Val << 8) | Val;
3459 Val = (Val << 16) | Val;
3462 default: // Byte aligned
3464 Count = Op.getOperand(3);
3469 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3471 InFlag = Chain.getValue(1);
3474 Count = Op.getOperand(3);
3475 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3476 InFlag = Chain.getValue(1);
3479 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3480 InFlag = Chain.getValue(1);
3481 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3482 InFlag = Chain.getValue(1);
3484 std::vector<MVT::ValueType> Tys;
3485 Tys.push_back(MVT::Other);
3486 Tys.push_back(MVT::Flag);
3487 std::vector<SDOperand> Ops;
3488 Ops.push_back(Chain);
3489 Ops.push_back(DAG.getValueType(AVT));
3490 Ops.push_back(InFlag);
3491 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3494 InFlag = Chain.getValue(1);
3495 Count = Op.getOperand(3);
3496 MVT::ValueType CVT = Count.getValueType();
3497 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3498 DAG.getConstant(3, CVT));
3499 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3500 InFlag = Chain.getValue(1);
3502 Tys.push_back(MVT::Other);
3503 Tys.push_back(MVT::Flag);
3505 Ops.push_back(Chain);
3506 Ops.push_back(DAG.getValueType(MVT::i8));
3507 Ops.push_back(InFlag);
3508 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3509 } else if (BytesLeft) {
3510 // Issue stores for the last 1 - 3 bytes.
3512 unsigned Val = ValC->getValue() & 255;
3513 unsigned Offset = I->getValue() - BytesLeft;
3514 SDOperand DstAddr = Op.getOperand(1);
3515 MVT::ValueType AddrVT = DstAddr.getValueType();
3516 if (BytesLeft >= 2) {
3517 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3518 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3519 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3520 DAG.getConstant(Offset, AddrVT)),
3521 DAG.getSrcValue(NULL));
3526 if (BytesLeft == 1) {
3527 Value = DAG.getConstant(Val, MVT::i8);
3528 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3529 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3530 DAG.getConstant(Offset, AddrVT)),
3531 DAG.getSrcValue(NULL));
3538 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3539 SDOperand Chain = Op.getOperand(0);
3541 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3542 if (Align == 0) Align = 1;
3544 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3545 // If not DWORD aligned, call memcpy if size is less than the threshold.
3546 // It knows how to align to the right boundary first.
3547 if ((Align & 3) != 0 ||
3548 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3549 MVT::ValueType IntPtr = getPointerTy();
3550 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3551 std::vector<std::pair<SDOperand, const Type*> > Args;
3552 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3553 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3554 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3555 std::pair<SDOperand,SDOperand> CallResult =
3556 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3557 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3558 return CallResult.second;
3563 unsigned BytesLeft = 0;
3564 bool TwoRepMovs = false;
3565 switch (Align & 3) {
3566 case 2: // WORD aligned
3568 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3569 BytesLeft = I->getValue() % 2;
3571 case 0: // DWORD aligned
3574 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3575 BytesLeft = I->getValue() % 4;
3577 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3578 DAG.getConstant(2, MVT::i8));
3582 default: // Byte aligned
3584 Count = Op.getOperand(3);
3588 SDOperand InFlag(0, 0);
3589 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3590 InFlag = Chain.getValue(1);
3591 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3592 InFlag = Chain.getValue(1);
3593 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3594 InFlag = Chain.getValue(1);
3596 std::vector<MVT::ValueType> Tys;
3597 Tys.push_back(MVT::Other);
3598 Tys.push_back(MVT::Flag);
3599 std::vector<SDOperand> Ops;
3600 Ops.push_back(Chain);
3601 Ops.push_back(DAG.getValueType(AVT));
3602 Ops.push_back(InFlag);
3603 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3606 InFlag = Chain.getValue(1);
3607 Count = Op.getOperand(3);
3608 MVT::ValueType CVT = Count.getValueType();
3609 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3610 DAG.getConstant(3, CVT));
3611 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3612 InFlag = Chain.getValue(1);
3614 Tys.push_back(MVT::Other);
3615 Tys.push_back(MVT::Flag);
3617 Ops.push_back(Chain);
3618 Ops.push_back(DAG.getValueType(MVT::i8));
3619 Ops.push_back(InFlag);
3620 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3621 } else if (BytesLeft) {
3622 // Issue loads and stores for the last 1 - 3 bytes.
3623 unsigned Offset = I->getValue() - BytesLeft;
3624 SDOperand DstAddr = Op.getOperand(1);
3625 MVT::ValueType DstVT = DstAddr.getValueType();
3626 SDOperand SrcAddr = Op.getOperand(2);
3627 MVT::ValueType SrcVT = SrcAddr.getValueType();
3629 if (BytesLeft >= 2) {
3630 Value = DAG.getLoad(MVT::i16, Chain,
3631 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3632 DAG.getConstant(Offset, SrcVT)),
3633 DAG.getSrcValue(NULL));
3634 Chain = Value.getValue(1);
3635 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3636 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3637 DAG.getConstant(Offset, DstVT)),
3638 DAG.getSrcValue(NULL));
3643 if (BytesLeft == 1) {
3644 Value = DAG.getLoad(MVT::i8, Chain,
3645 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3646 DAG.getConstant(Offset, SrcVT)),
3647 DAG.getSrcValue(NULL));
3648 Chain = Value.getValue(1);
3649 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3650 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3651 DAG.getConstant(Offset, DstVT)),
3652 DAG.getSrcValue(NULL));
3660 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3661 std::vector<MVT::ValueType> Tys;
3662 Tys.push_back(MVT::Other);
3663 Tys.push_back(MVT::Flag);
3664 std::vector<SDOperand> Ops;
3665 Ops.push_back(Op.getOperand(0));
3666 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3668 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3669 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3670 MVT::i32, Ops[0].getValue(2)));
3671 Ops.push_back(Ops[1].getValue(1));
3672 Tys[0] = Tys[1] = MVT::i32;
3673 Tys.push_back(MVT::Other);
3674 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3677 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3678 // vastart just stores the address of the VarArgsFrameIndex slot into the
3679 // memory location argument.
3680 // FIXME: Replace MVT::i32 with PointerTy
3681 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3682 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3683 Op.getOperand(1), Op.getOperand(2));
3687 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3688 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3690 default: return SDOperand(); // Don't custom lower most intrinsics.
3691 // Comparison intrinsics.
3692 case Intrinsic::x86_sse_comieq_ss:
3693 case Intrinsic::x86_sse_comilt_ss:
3694 case Intrinsic::x86_sse_comile_ss:
3695 case Intrinsic::x86_sse_comigt_ss:
3696 case Intrinsic::x86_sse_comige_ss:
3697 case Intrinsic::x86_sse_comineq_ss:
3698 case Intrinsic::x86_sse_ucomieq_ss:
3699 case Intrinsic::x86_sse_ucomilt_ss:
3700 case Intrinsic::x86_sse_ucomile_ss:
3701 case Intrinsic::x86_sse_ucomigt_ss:
3702 case Intrinsic::x86_sse_ucomige_ss:
3703 case Intrinsic::x86_sse_ucomineq_ss:
3704 case Intrinsic::x86_sse2_comieq_sd:
3705 case Intrinsic::x86_sse2_comilt_sd:
3706 case Intrinsic::x86_sse2_comile_sd:
3707 case Intrinsic::x86_sse2_comigt_sd:
3708 case Intrinsic::x86_sse2_comige_sd:
3709 case Intrinsic::x86_sse2_comineq_sd:
3710 case Intrinsic::x86_sse2_ucomieq_sd:
3711 case Intrinsic::x86_sse2_ucomilt_sd:
3712 case Intrinsic::x86_sse2_ucomile_sd:
3713 case Intrinsic::x86_sse2_ucomigt_sd:
3714 case Intrinsic::x86_sse2_ucomige_sd:
3715 case Intrinsic::x86_sse2_ucomineq_sd: {
3717 ISD::CondCode CC = ISD::SETCC_INVALID;
3720 case Intrinsic::x86_sse_comieq_ss:
3721 case Intrinsic::x86_sse2_comieq_sd:
3725 case Intrinsic::x86_sse_comilt_ss:
3726 case Intrinsic::x86_sse2_comilt_sd:
3730 case Intrinsic::x86_sse_comile_ss:
3731 case Intrinsic::x86_sse2_comile_sd:
3735 case Intrinsic::x86_sse_comigt_ss:
3736 case Intrinsic::x86_sse2_comigt_sd:
3740 case Intrinsic::x86_sse_comige_ss:
3741 case Intrinsic::x86_sse2_comige_sd:
3745 case Intrinsic::x86_sse_comineq_ss:
3746 case Intrinsic::x86_sse2_comineq_sd:
3750 case Intrinsic::x86_sse_ucomieq_ss:
3751 case Intrinsic::x86_sse2_ucomieq_sd:
3752 Opc = X86ISD::UCOMI;
3755 case Intrinsic::x86_sse_ucomilt_ss:
3756 case Intrinsic::x86_sse2_ucomilt_sd:
3757 Opc = X86ISD::UCOMI;
3760 case Intrinsic::x86_sse_ucomile_ss:
3761 case Intrinsic::x86_sse2_ucomile_sd:
3762 Opc = X86ISD::UCOMI;
3765 case Intrinsic::x86_sse_ucomigt_ss:
3766 case Intrinsic::x86_sse2_ucomigt_sd:
3767 Opc = X86ISD::UCOMI;
3770 case Intrinsic::x86_sse_ucomige_ss:
3771 case Intrinsic::x86_sse2_ucomige_sd:
3772 Opc = X86ISD::UCOMI;
3775 case Intrinsic::x86_sse_ucomineq_ss:
3776 case Intrinsic::x86_sse2_ucomineq_sd:
3777 Opc = X86ISD::UCOMI;
3783 translateX86CC(CC, true, X86CC, Flip);
3784 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3785 Op.getOperand(Flip?1:2));
3786 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3787 DAG.getConstant(X86CC, MVT::i8), Cond);
3788 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
3793 /// LowerOperation - Provide custom lowering hooks for some operations.
3795 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3796 switch (Op.getOpcode()) {
3797 default: assert(0 && "Should not custom lower this!");
3798 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3799 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3800 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3801 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3802 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3803 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3804 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3805 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3806 case ISD::SHL_PARTS:
3807 case ISD::SRA_PARTS:
3808 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3809 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3810 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3811 case ISD::FABS: return LowerFABS(Op, DAG);
3812 case ISD::FNEG: return LowerFNEG(Op, DAG);
3813 case ISD::SETCC: return LowerSETCC(Op, DAG);
3814 case ISD::SELECT: return LowerSELECT(Op, DAG);
3815 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3816 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3817 case ISD::CALL: return LowerCALL(Op, DAG);
3818 case ISD::RET: return LowerRET(Op, DAG);
3819 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
3820 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3821 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3822 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3823 case ISD::VASTART: return LowerVASTART(Op, DAG);
3824 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3828 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3830 default: return NULL;
3831 case X86ISD::SHLD: return "X86ISD::SHLD";
3832 case X86ISD::SHRD: return "X86ISD::SHRD";
3833 case X86ISD::FAND: return "X86ISD::FAND";
3834 case X86ISD::FXOR: return "X86ISD::FXOR";
3835 case X86ISD::FILD: return "X86ISD::FILD";
3836 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
3837 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3838 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3839 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
3840 case X86ISD::FLD: return "X86ISD::FLD";
3841 case X86ISD::FST: return "X86ISD::FST";
3842 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
3843 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
3844 case X86ISD::CALL: return "X86ISD::CALL";
3845 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3846 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3847 case X86ISD::CMP: return "X86ISD::CMP";
3848 case X86ISD::TEST: return "X86ISD::TEST";
3849 case X86ISD::COMI: return "X86ISD::COMI";
3850 case X86ISD::UCOMI: return "X86ISD::UCOMI";
3851 case X86ISD::SETCC: return "X86ISD::SETCC";
3852 case X86ISD::CMOV: return "X86ISD::CMOV";
3853 case X86ISD::BRCOND: return "X86ISD::BRCOND";
3854 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
3855 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3856 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
3857 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
3858 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
3859 case X86ISD::Wrapper: return "X86ISD::Wrapper";
3860 case X86ISD::S2VEC: return "X86ISD::S2VEC";
3861 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
3862 case X86ISD::PINSRW: return "X86ISD::PINSRW";
3866 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3868 uint64_t &KnownZero,
3870 unsigned Depth) const {
3871 unsigned Opc = Op.getOpcode();
3872 assert((Opc >= ISD::BUILTIN_OP_END ||
3873 Opc == ISD::INTRINSIC_WO_CHAIN ||
3874 Opc == ISD::INTRINSIC_W_CHAIN ||
3875 Opc == ISD::INTRINSIC_VOID) &&
3876 "Should use MaskedValueIsZero if you don't know whether Op"
3877 " is a target node!");
3879 KnownZero = KnownOne = 0; // Don't know anything.
3883 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3888 std::vector<unsigned> X86TargetLowering::
3889 getRegClassForInlineAsmConstraint(const std::string &Constraint,
3890 MVT::ValueType VT) const {
3891 if (Constraint.size() == 1) {
3892 // FIXME: not handling fp-stack yet!
3893 // FIXME: not handling MMX registers yet ('y' constraint).
3894 switch (Constraint[0]) { // GCC X86 Constraint Letters
3895 default: break; // Unknown constriant letter
3896 case 'r': // GENERAL_REGS
3897 case 'R': // LEGACY_REGS
3899 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3900 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3901 else if (VT == MVT::i16)
3902 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3903 X86::SI, X86::DI, X86::BP, X86::SP, 0);
3904 else if (VT == MVT::i8)
3905 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3907 case 'l': // INDEX_REGS
3909 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3910 X86::ESI, X86::EDI, X86::EBP, 0);
3911 else if (VT == MVT::i16)
3912 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3913 X86::SI, X86::DI, X86::BP, 0);
3914 else if (VT == MVT::i8)
3915 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3917 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3920 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
3921 else if (VT == MVT::i16)
3922 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
3923 else if (VT == MVT::i8)
3924 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3926 case 'x': // SSE_REGS if SSE1 allowed
3927 if (Subtarget->hasSSE1())
3928 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3929 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3931 return std::vector<unsigned>();
3932 case 'Y': // SSE_REGS if SSE2 allowed
3933 if (Subtarget->hasSSE2())
3934 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3935 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3937 return std::vector<unsigned>();
3941 return std::vector<unsigned>();
3944 /// isLegalAddressImmediate - Return true if the integer value or
3945 /// GlobalValue can be used as the offset of the target addressing mode.
3946 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3947 // X86 allows a sign-extended 32-bit immediate field.
3948 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3951 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
3952 if (Subtarget->isTargetDarwin()) {
3953 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3954 if (RModel == Reloc::Static)
3956 else if (RModel == Reloc::DynamicNoPIC)
3957 return !DarwinGVRequiresExtraLoad(GV);
3964 /// isShuffleMaskLegal - Targets can use this to indicate that they only
3965 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3966 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3967 /// are assumed to be legal.
3969 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3970 // Only do shuffles on 128-bit vector types for now.
3971 if (MVT::getSizeInBits(VT) == 64) return false;
3972 return (Mask.Val->getNumOperands() <= 4 ||
3973 isSplatMask(Mask.Val) ||
3974 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
3975 X86::isUNPCKLMask(Mask.Val) ||
3976 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
3977 X86::isUNPCKHMask(Mask.Val));
3980 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3982 SelectionDAG &DAG) const {
3983 unsigned NumElts = BVOps.size();
3984 // Only do shuffles on 128-bit vector types for now.
3985 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
3986 if (NumElts == 2) return true;
3988 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
3989 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));