1 //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that X86 uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
16 #include "X86InstrBuilder.h"
17 #include "X86ISelLowering.h"
18 #include "X86MachineFunctionInfo.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/CallingConv.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/ADT/VectorExtras.h"
26 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/SelectionDAG.h"
31 #include "llvm/CodeGen/SSARegMap.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Target/TargetOptions.h"
37 #include "llvm/Support/CommandLine.h"
38 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
39 cl::desc("Enable fastcc on X86"));
41 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
43 Subtarget = &TM.getSubtarget<X86Subtarget>();
44 X86ScalarSSE = Subtarget->hasSSE2();
46 // Set up the TargetLowering object.
48 // X86 is weird, it always uses i8 for shift amounts and setcc results.
49 setShiftAmountType(MVT::i8);
50 setSetCCResultType(MVT::i8);
51 setSetCCResultContents(ZeroOrOneSetCCResult);
52 setSchedulingPreference(SchedulingForRegPressure);
53 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
54 setStackPointerRegisterToSaveRestore(X86::ESP);
56 if (!Subtarget->isTargetDarwin())
57 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
58 setUseUnderscoreSetJmpLongJmp(true);
60 // Add legal addressing mode scale values.
61 addLegalAddressScale(8);
62 addLegalAddressScale(4);
63 addLegalAddressScale(2);
64 // Enter the ones which require both scale + index last. These are more
66 addLegalAddressScale(9);
67 addLegalAddressScale(5);
68 addLegalAddressScale(3);
70 // Set up the register classes.
71 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
72 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
73 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
75 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
77 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
78 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
79 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
82 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
83 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
85 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
87 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
89 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
90 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
91 // SSE has no i16 to fp conversion, only i32
93 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
95 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
96 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
99 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
101 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
102 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
104 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
106 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
107 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
110 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
112 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
113 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
116 // Handle FP_TO_UINT by promoting the destination to a larger signed
118 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
119 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
120 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
122 if (X86ScalarSSE && !Subtarget->hasSSE3())
123 // Expand FP_TO_UINT into a select.
124 // FIXME: We would like to use a Custom expander here eventually to do
125 // the optimal thing for SSE vs. the default expansion in the legalizer.
126 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
128 // With SSE3 we can use fisttpll to convert to a signed i64.
129 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
131 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
132 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
134 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
135 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
136 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
137 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
141 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
142 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
143 setOperationAction(ISD::FREM , MVT::f64 , Expand);
144 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
145 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
146 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
147 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
148 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
149 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
150 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
151 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
152 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
153 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
154 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
156 // These should be promoted to a larger select which is supported.
157 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
158 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
160 // X86 wants to expand cmov itself.
161 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
162 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
163 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
164 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
166 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
167 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
168 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
169 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
170 // X86 ret instruction may pop stack.
171 setOperationAction(ISD::RET , MVT::Other, Custom);
173 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
174 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
175 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
176 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
177 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
178 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
179 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
180 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
181 // X86 wants to expand memset / memcpy itself.
182 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
183 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
185 // We don't have line number support yet.
186 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
187 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
188 // FIXME - use subtarget debug flags
189 if (!Subtarget->isTargetDarwin())
190 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
192 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
193 setOperationAction(ISD::VASTART , MVT::Other, Custom);
195 // Use the default implementation.
196 setOperationAction(ISD::VAARG , MVT::Other, Expand);
197 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
198 setOperationAction(ISD::VAEND , MVT::Other, Expand);
199 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
200 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
201 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
203 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
204 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
207 // Set up the FP register classes.
208 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
209 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
211 // Use ANDPD to simulate FABS.
212 setOperationAction(ISD::FABS , MVT::f64, Custom);
213 setOperationAction(ISD::FABS , MVT::f32, Custom);
215 // Use XORP to simulate FNEG.
216 setOperationAction(ISD::FNEG , MVT::f64, Custom);
217 setOperationAction(ISD::FNEG , MVT::f32, Custom);
219 // We don't support sin/cos/fmod
220 setOperationAction(ISD::FSIN , MVT::f64, Expand);
221 setOperationAction(ISD::FCOS , MVT::f64, Expand);
222 setOperationAction(ISD::FREM , MVT::f64, Expand);
223 setOperationAction(ISD::FSIN , MVT::f32, Expand);
224 setOperationAction(ISD::FCOS , MVT::f32, Expand);
225 setOperationAction(ISD::FREM , MVT::f32, Expand);
227 // Expand FP immediates into loads from the stack, except for the special
229 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
230 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
231 addLegalFPImmediate(+0.0); // xorps / xorpd
233 // Set up the FP register classes.
234 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
236 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
239 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
240 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
243 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
244 addLegalFPImmediate(+0.0); // FLD0
245 addLegalFPImmediate(+1.0); // FLD1
246 addLegalFPImmediate(-0.0); // FLD0/FCHS
247 addLegalFPImmediate(-1.0); // FLD1/FCHS
250 // First set operation action for all vector types to expand. Then we
251 // will selectively turn on ones that can be effectively codegen'd.
252 for (unsigned VT = (unsigned)MVT::Vector + 1;
253 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
254 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
258 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
259 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
260 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
263 if (Subtarget->hasMMX()) {
264 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
266 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
268 // FIXME: add MMX packed arithmetics
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
271 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
274 if (Subtarget->hasSSE1()) {
275 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
277 setOperationAction(ISD::AND, MVT::v4f32, Legal);
278 setOperationAction(ISD::OR, MVT::v4f32, Legal);
279 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
280 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
281 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
282 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
283 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
284 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
285 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
286 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
287 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
290 if (Subtarget->hasSSE2()) {
291 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
292 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
293 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
294 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
295 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
297 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
298 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
299 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
300 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
301 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
302 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
303 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
304 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
305 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
306 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
309 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
310 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
311 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
312 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
313 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
315 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
316 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
317 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
318 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
319 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
321 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
322 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
323 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
324 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
325 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
326 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
328 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
329 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
330 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
331 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
332 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
333 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
334 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
335 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
336 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
337 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
338 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
339 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
342 // Custom lower v2i64 and v2f64 selects.
343 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
344 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
345 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
346 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
349 // We want to custom lower some of our intrinsics.
350 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
352 // We have target-specific dag combine patterns for the following nodes:
353 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
355 computeRegisterProperties();
357 // FIXME: These should be based on subtarget info. Plus, the values should
358 // be smaller when we are in optimizing for size mode.
359 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
360 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
361 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
362 allowUnalignedMemoryAccesses = true; // x86 supports it!
365 //===----------------------------------------------------------------------===//
366 // C Calling Convention implementation
367 //===----------------------------------------------------------------------===//
369 /// AddLiveIn - This helper function adds the specified physical register to the
370 /// MachineFunction as a live in value. It also creates a corresponding virtual
372 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
373 TargetRegisterClass *RC) {
374 assert(RC->contains(PReg) && "Not the correct regclass!");
375 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
376 MF.addLiveIn(PReg, VReg);
380 /// HowToPassCCCArgument - Returns how an formal argument of the specified type
381 /// should be passed. If it is through stack, returns the size of the stack
382 /// slot; if it is through XMM register, returns the number of XMM registers
385 HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
386 unsigned &ObjSize, unsigned &ObjXMMRegs) {
390 default: assert(0 && "Unhandled argument type!");
391 case MVT::i8: ObjSize = 1; break;
392 case MVT::i16: ObjSize = 2; break;
393 case MVT::i32: ObjSize = 4; break;
394 case MVT::i64: ObjSize = 8; break;
395 case MVT::f32: ObjSize = 4; break;
396 case MVT::f64: ObjSize = 8; break;
411 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
412 unsigned NumArgs = Op.Val->getNumValues() - 1;
413 MachineFunction &MF = DAG.getMachineFunction();
414 MachineFrameInfo *MFI = MF.getFrameInfo();
415 SDOperand Root = Op.getOperand(0);
416 std::vector<SDOperand> ArgValues;
418 // Add DAG nodes to load the arguments... On entry to a function on the X86,
419 // the stack frame looks like this:
421 // [ESP] -- return address
422 // [ESP + 4] -- first argument (leftmost lexically)
423 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
426 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
427 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
428 static const unsigned XMMArgRegs[] = {
429 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
431 for (unsigned i = 0; i < NumArgs; ++i) {
432 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
433 unsigned ArgIncrement = 4;
434 unsigned ObjSize = 0;
435 unsigned ObjXMMRegs = 0;
436 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
438 ArgIncrement = ObjSize;
442 // Passed in a XMM register.
443 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
444 X86::VR128RegisterClass);
445 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
446 ArgValues.push_back(ArgValue);
447 NumXMMRegs += ObjXMMRegs;
449 // XMM arguments have to be aligned on 16-byte boundary.
451 ArgOffset = ((ArgOffset + 15) / 16) * 16;
452 // Create the frame index object for this incoming parameter...
453 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
454 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
455 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
456 DAG.getSrcValue(NULL));
457 ArgValues.push_back(ArgValue);
458 ArgOffset += ArgIncrement; // Move on to the next argument...
462 ArgValues.push_back(Root);
464 // If the function takes variable number of arguments, make a frame index for
465 // the start of the first vararg value... for expansion of llvm.va_start.
466 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
468 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
469 ReturnAddrIndex = 0; // No return address slot generated yet.
470 BytesToPopOnReturn = 0; // Callee pops nothing.
471 BytesCallerReserves = ArgOffset;
473 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
475 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
476 Subtarget->isTargetDarwin())
477 BytesToPopOnReturn = 4;
479 // Return the new list of results.
480 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
481 Op.Val->value_end());
482 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
486 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
487 SDOperand Chain = Op.getOperand(0);
488 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
489 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
490 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
491 SDOperand Callee = Op.getOperand(4);
492 MVT::ValueType RetVT= Op.Val->getValueType(0);
493 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
495 // Keep track of the number of XMM regs passed so far.
496 unsigned NumXMMRegs = 0;
497 static const unsigned XMMArgRegs[] = {
498 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
501 // Count how many bytes are to be pushed on the stack.
502 unsigned NumBytes = 0;
503 for (unsigned i = 0; i != NumOps; ++i) {
504 SDOperand Arg = Op.getOperand(5+2*i);
506 switch (Arg.getValueType()) {
507 default: assert(0 && "Unexpected ValueType for argument!");
527 // XMM arguments have to be aligned on 16-byte boundary.
528 NumBytes = ((NumBytes + 15) / 16) * 16;
535 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
537 // Arguments go on the stack in reverse order, as specified by the ABI.
538 unsigned ArgOffset = 0;
540 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
541 std::vector<SDOperand> MemOpChains;
542 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
543 for (unsigned i = 0; i != NumOps; ++i) {
544 SDOperand Arg = Op.getOperand(5+2*i);
546 switch (Arg.getValueType()) {
547 default: assert(0 && "Unexpected ValueType for argument!");
550 // Promote the integer to 32 bits. If the input type is signed use a
551 // sign extend, otherwise use a zero extend.
553 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
554 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
555 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
561 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
562 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
563 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
564 Arg, PtrOff, DAG.getSrcValue(NULL)));
570 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
571 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
572 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
573 Arg, PtrOff, DAG.getSrcValue(NULL)));
583 if (NumXMMRegs < 4) {
584 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
587 // XMM arguments have to be aligned on 16-byte boundary.
588 ArgOffset = ((ArgOffset + 15) / 16) * 16;
589 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
590 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
591 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
592 Arg, PtrOff, DAG.getSrcValue(NULL)));
598 if (!MemOpChains.empty())
599 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
601 // Build a sequence of copy-to-reg nodes chained together with token chain
602 // and flag operands which copy the outgoing args into registers.
604 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
605 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
607 InFlag = Chain.getValue(1);
610 // If the callee is a GlobalAddress node (quite common, every direct call is)
611 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
612 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
613 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
614 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
615 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
617 std::vector<MVT::ValueType> NodeTys;
618 NodeTys.push_back(MVT::Other); // Returns a chain
619 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
620 std::vector<SDOperand> Ops;
621 Ops.push_back(Chain);
622 Ops.push_back(Callee);
624 // Add argument registers to the end of the list so that they are known live
626 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
627 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
628 RegsToPass[i].second.getValueType()));
631 Ops.push_back(InFlag);
633 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
635 InFlag = Chain.getValue(1);
637 // Create the CALLSEQ_END node.
638 unsigned NumBytesForCalleeToPush = 0;
640 // If this is is a call to a struct-return function on Darwin/X86, the callee
641 // pops the hidden struct pointer, so we have to push it back.
642 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
643 NumBytesForCalleeToPush = 4;
646 NodeTys.push_back(MVT::Other); // Returns a chain
647 if (RetVT != MVT::Other)
648 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
650 Ops.push_back(Chain);
651 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
652 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
653 Ops.push_back(InFlag);
654 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
655 if (RetVT != MVT::Other)
656 InFlag = Chain.getValue(1);
658 std::vector<SDOperand> ResultVals;
661 default: assert(0 && "Unknown value type to return!");
662 case MVT::Other: break;
664 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
665 ResultVals.push_back(Chain.getValue(0));
666 NodeTys.push_back(MVT::i8);
669 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
670 ResultVals.push_back(Chain.getValue(0));
671 NodeTys.push_back(MVT::i16);
674 if (Op.Val->getValueType(1) == MVT::i32) {
675 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
676 ResultVals.push_back(Chain.getValue(0));
677 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
678 Chain.getValue(2)).getValue(1);
679 ResultVals.push_back(Chain.getValue(0));
680 NodeTys.push_back(MVT::i32);
682 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
683 ResultVals.push_back(Chain.getValue(0));
685 NodeTys.push_back(MVT::i32);
693 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
694 ResultVals.push_back(Chain.getValue(0));
695 NodeTys.push_back(RetVT);
699 std::vector<MVT::ValueType> Tys;
700 Tys.push_back(MVT::f64);
701 Tys.push_back(MVT::Other);
702 Tys.push_back(MVT::Flag);
703 std::vector<SDOperand> Ops;
704 Ops.push_back(Chain);
705 Ops.push_back(InFlag);
706 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
707 Chain = RetVal.getValue(1);
708 InFlag = RetVal.getValue(2);
710 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
711 // shouldn't be necessary except that RFP cannot be live across
712 // multiple blocks. When stackifier is fixed, they can be uncoupled.
713 MachineFunction &MF = DAG.getMachineFunction();
714 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
715 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
717 Tys.push_back(MVT::Other);
719 Ops.push_back(Chain);
720 Ops.push_back(RetVal);
721 Ops.push_back(StackSlot);
722 Ops.push_back(DAG.getValueType(RetVT));
723 Ops.push_back(InFlag);
724 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
725 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
726 DAG.getSrcValue(NULL));
727 Chain = RetVal.getValue(1);
730 if (RetVT == MVT::f32 && !X86ScalarSSE)
731 // FIXME: we would really like to remember that this FP_ROUND
732 // operation is okay to eliminate if we allow excess FP precision.
733 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
734 ResultVals.push_back(RetVal);
735 NodeTys.push_back(RetVT);
740 // If the function returns void, just return the chain.
741 if (ResultVals.empty())
744 // Otherwise, merge everything together with a MERGE_VALUES node.
745 NodeTys.push_back(MVT::Other);
746 ResultVals.push_back(Chain);
747 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
748 return Res.getValue(Op.ResNo);
751 //===----------------------------------------------------------------------===//
752 // Fast Calling Convention implementation
753 //===----------------------------------------------------------------------===//
755 // The X86 'fast' calling convention passes up to two integer arguments in
756 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
757 // and requires that the callee pop its arguments off the stack (allowing proper
758 // tail calls), and has the same return value conventions as C calling convs.
760 // This calling convention always arranges for the callee pop value to be 8n+4
761 // bytes, which is needed for tail recursion elimination and stack alignment
764 // Note that this can be enhanced in the future to pass fp vals in registers
765 // (when we have a global fp allocator) and do other tricks.
768 /// HowToPassFastCCArgument - Returns how an formal argument of the specified
769 /// type should be passed. If it is through stack, returns the size of the stack
770 /// slot; if it is through integer or XMM register, returns the number of
771 /// integer or XMM registers are needed.
773 HowToPassFastCCArgument(MVT::ValueType ObjectVT,
774 unsigned NumIntRegs, unsigned NumXMMRegs,
775 unsigned &ObjSize, unsigned &ObjIntRegs,
776 unsigned &ObjXMMRegs) {
782 default: assert(0 && "Unhandled argument type!");
784 #if FASTCC_NUM_INT_ARGS_INREGS > 0
785 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
792 #if FASTCC_NUM_INT_ARGS_INREGS > 0
793 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
800 #if FASTCC_NUM_INT_ARGS_INREGS > 0
801 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
808 #if FASTCC_NUM_INT_ARGS_INREGS > 0
809 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
811 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
838 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
839 unsigned NumArgs = Op.Val->getNumValues()-1;
840 MachineFunction &MF = DAG.getMachineFunction();
841 MachineFrameInfo *MFI = MF.getFrameInfo();
842 SDOperand Root = Op.getOperand(0);
843 std::vector<SDOperand> ArgValues;
845 // Add DAG nodes to load the arguments... On entry to a function the stack
846 // frame looks like this:
848 // [ESP] -- return address
849 // [ESP + 4] -- first nonreg argument (leftmost lexically)
850 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
852 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
854 // Keep track of the number of integer regs passed so far. This can be either
855 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
857 unsigned NumIntRegs = 0;
858 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
860 static const unsigned XMMArgRegs[] = {
861 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
864 for (unsigned i = 0; i < NumArgs; ++i) {
865 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
866 unsigned ArgIncrement = 4;
867 unsigned ObjSize = 0;
868 unsigned ObjIntRegs = 0;
869 unsigned ObjXMMRegs = 0;
871 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
872 ObjSize, ObjIntRegs, ObjXMMRegs);
874 ArgIncrement = ObjSize;
878 if (ObjIntRegs || ObjXMMRegs) {
880 default: assert(0 && "Unhandled argument type!");
882 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
883 X86::GR8RegisterClass);
884 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
887 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
888 X86::GR16RegisterClass);
889 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
892 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
893 X86::GR32RegisterClass);
894 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
897 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
898 X86::GR32RegisterClass);
899 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
900 if (ObjIntRegs == 2) {
901 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
902 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
903 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
912 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
913 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
916 NumIntRegs += ObjIntRegs;
917 NumXMMRegs += ObjXMMRegs;
921 // XMM arguments have to be aligned on 16-byte boundary.
923 ArgOffset = ((ArgOffset + 15) / 16) * 16;
924 // Create the SelectionDAG nodes corresponding to a load from this
926 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
927 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
928 if (ObjectVT == MVT::i64 && ObjIntRegs) {
929 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
930 DAG.getSrcValue(NULL));
931 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
933 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
934 DAG.getSrcValue(NULL));
935 ArgOffset += ArgIncrement; // Move on to the next argument.
938 ArgValues.push_back(ArgValue);
941 ArgValues.push_back(Root);
943 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
944 // arguments and the arguments after the retaddr has been pushed are aligned.
945 if ((ArgOffset & 7) == 0)
948 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
949 ReturnAddrIndex = 0; // No return address slot generated yet.
950 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
951 BytesCallerReserves = 0;
953 // Finally, inform the code generator which regs we return values in.
954 switch (getValueType(MF.getFunction()->getReturnType())) {
955 default: assert(0 && "Unknown type!");
956 case MVT::isVoid: break;
960 MF.addLiveOut(X86::EAX);
963 MF.addLiveOut(X86::EAX);
964 MF.addLiveOut(X86::EDX);
968 MF.addLiveOut(X86::ST0);
976 MF.addLiveOut(X86::XMM0);
980 // Return the new list of results.
981 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
982 Op.Val->value_end());
983 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
986 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG) {
987 SDOperand Chain = Op.getOperand(0);
988 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
989 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
990 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
991 SDOperand Callee = Op.getOperand(4);
992 MVT::ValueType RetVT= Op.Val->getValueType(0);
993 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
995 // Count how many bytes are to be pushed on the stack.
996 unsigned NumBytes = 0;
998 // Keep track of the number of integer regs passed so far. This can be either
999 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1001 unsigned NumIntRegs = 0;
1002 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1004 static const unsigned GPRArgRegs[][2] = {
1005 { X86::AL, X86::DL },
1006 { X86::AX, X86::DX },
1007 { X86::EAX, X86::EDX }
1009 static const unsigned XMMArgRegs[] = {
1010 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1013 for (unsigned i = 0; i != NumOps; ++i) {
1014 SDOperand Arg = Op.getOperand(5+2*i);
1016 switch (Arg.getValueType()) {
1017 default: assert(0 && "Unknown value type!");
1021 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1022 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
1043 // XMM arguments have to be aligned on 16-byte boundary.
1044 NumBytes = ((NumBytes + 15) / 16) * 16;
1051 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1052 // arguments and the arguments after the retaddr has been pushed are aligned.
1053 if ((NumBytes & 7) == 0)
1056 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1058 // Arguments go on the stack in reverse order, as specified by the ABI.
1059 unsigned ArgOffset = 0;
1061 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1062 std::vector<SDOperand> MemOpChains;
1063 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
1064 for (unsigned i = 0; i != NumOps; ++i) {
1065 SDOperand Arg = Op.getOperand(5+2*i);
1067 switch (Arg.getValueType()) {
1068 default: assert(0 && "Unexpected ValueType for argument!");
1072 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1073 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
1074 RegsToPass.push_back(
1075 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1083 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1084 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1085 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1086 Arg, PtrOff, DAG.getSrcValue(NULL)));
1091 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1092 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1093 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1094 Arg, PtrOff, DAG.getSrcValue(NULL)));
1104 if (NumXMMRegs < 4) {
1105 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1108 // XMM arguments have to be aligned on 16-byte boundary.
1109 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1110 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1111 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1112 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1113 Arg, PtrOff, DAG.getSrcValue(NULL)));
1119 if (!MemOpChains.empty())
1120 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
1122 // Build a sequence of copy-to-reg nodes chained together with token chain
1123 // and flag operands which copy the outgoing args into registers.
1125 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1126 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1128 InFlag = Chain.getValue(1);
1131 // If the callee is a GlobalAddress node (quite common, every direct call is)
1132 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1133 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1134 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1135 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1136 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1138 std::vector<MVT::ValueType> NodeTys;
1139 NodeTys.push_back(MVT::Other); // Returns a chain
1140 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1141 std::vector<SDOperand> Ops;
1142 Ops.push_back(Chain);
1143 Ops.push_back(Callee);
1145 // Add argument registers to the end of the list so that they are known live
1147 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1148 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1149 RegsToPass[i].second.getValueType()));
1152 Ops.push_back(InFlag);
1154 // FIXME: Do not generate X86ISD::TAILCALL for now.
1155 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1157 InFlag = Chain.getValue(1);
1160 NodeTys.push_back(MVT::Other); // Returns a chain
1161 if (RetVT != MVT::Other)
1162 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1164 Ops.push_back(Chain);
1165 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1166 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1167 Ops.push_back(InFlag);
1168 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1169 if (RetVT != MVT::Other)
1170 InFlag = Chain.getValue(1);
1172 std::vector<SDOperand> ResultVals;
1175 default: assert(0 && "Unknown value type to return!");
1176 case MVT::Other: break;
1178 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1179 ResultVals.push_back(Chain.getValue(0));
1180 NodeTys.push_back(MVT::i8);
1183 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1184 ResultVals.push_back(Chain.getValue(0));
1185 NodeTys.push_back(MVT::i16);
1188 if (Op.Val->getValueType(1) == MVT::i32) {
1189 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1190 ResultVals.push_back(Chain.getValue(0));
1191 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1192 Chain.getValue(2)).getValue(1);
1193 ResultVals.push_back(Chain.getValue(0));
1194 NodeTys.push_back(MVT::i32);
1196 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1197 ResultVals.push_back(Chain.getValue(0));
1199 NodeTys.push_back(MVT::i32);
1207 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1208 ResultVals.push_back(Chain.getValue(0));
1209 NodeTys.push_back(RetVT);
1213 std::vector<MVT::ValueType> Tys;
1214 Tys.push_back(MVT::f64);
1215 Tys.push_back(MVT::Other);
1216 Tys.push_back(MVT::Flag);
1217 std::vector<SDOperand> Ops;
1218 Ops.push_back(Chain);
1219 Ops.push_back(InFlag);
1220 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1221 Chain = RetVal.getValue(1);
1222 InFlag = RetVal.getValue(2);
1224 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1225 // shouldn't be necessary except that RFP cannot be live across
1226 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1227 MachineFunction &MF = DAG.getMachineFunction();
1228 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1229 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1231 Tys.push_back(MVT::Other);
1233 Ops.push_back(Chain);
1234 Ops.push_back(RetVal);
1235 Ops.push_back(StackSlot);
1236 Ops.push_back(DAG.getValueType(RetVT));
1237 Ops.push_back(InFlag);
1238 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1239 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1240 DAG.getSrcValue(NULL));
1241 Chain = RetVal.getValue(1);
1244 if (RetVT == MVT::f32 && !X86ScalarSSE)
1245 // FIXME: we would really like to remember that this FP_ROUND
1246 // operation is okay to eliminate if we allow excess FP precision.
1247 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1248 ResultVals.push_back(RetVal);
1249 NodeTys.push_back(RetVT);
1255 // If the function returns void, just return the chain.
1256 if (ResultVals.empty())
1259 // Otherwise, merge everything together with a MERGE_VALUES node.
1260 NodeTys.push_back(MVT::Other);
1261 ResultVals.push_back(Chain);
1262 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
1263 return Res.getValue(Op.ResNo);
1266 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1267 if (ReturnAddrIndex == 0) {
1268 // Set up a frame object for the return address.
1269 MachineFunction &MF = DAG.getMachineFunction();
1270 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1273 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1278 std::pair<SDOperand, SDOperand> X86TargetLowering::
1279 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1280 SelectionDAG &DAG) {
1282 if (Depth) // Depths > 0 not supported yet!
1283 Result = DAG.getConstant(0, getPointerTy());
1285 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1286 if (!isFrameAddress)
1287 // Just load the return address
1288 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1289 DAG.getSrcValue(NULL));
1291 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1292 DAG.getConstant(4, MVT::i32));
1294 return std::make_pair(Result, Chain);
1297 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1298 /// which corresponds to the condition code.
1299 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1301 default: assert(0 && "Unknown X86 conditional code!");
1302 case X86ISD::COND_A: return X86::JA;
1303 case X86ISD::COND_AE: return X86::JAE;
1304 case X86ISD::COND_B: return X86::JB;
1305 case X86ISD::COND_BE: return X86::JBE;
1306 case X86ISD::COND_E: return X86::JE;
1307 case X86ISD::COND_G: return X86::JG;
1308 case X86ISD::COND_GE: return X86::JGE;
1309 case X86ISD::COND_L: return X86::JL;
1310 case X86ISD::COND_LE: return X86::JLE;
1311 case X86ISD::COND_NE: return X86::JNE;
1312 case X86ISD::COND_NO: return X86::JNO;
1313 case X86ISD::COND_NP: return X86::JNP;
1314 case X86ISD::COND_NS: return X86::JNS;
1315 case X86ISD::COND_O: return X86::JO;
1316 case X86ISD::COND_P: return X86::JP;
1317 case X86ISD::COND_S: return X86::JS;
1321 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1322 /// specific condition code. It returns a false if it cannot do a direct
1323 /// translation. X86CC is the translated CondCode. Flip is set to true if the
1324 /// the order of comparison operands should be flipped.
1325 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1326 unsigned &X86CC, bool &Flip) {
1328 X86CC = X86ISD::COND_INVALID;
1330 switch (SetCCOpcode) {
1332 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1333 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1334 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1335 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1336 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1337 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1338 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1339 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1340 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1341 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1344 // On a floating point condition, the flags are set as follows:
1346 // 0 | 0 | 0 | X > Y
1347 // 0 | 0 | 1 | X < Y
1348 // 1 | 0 | 0 | X == Y
1349 // 1 | 1 | 1 | unordered
1350 switch (SetCCOpcode) {
1353 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1354 case ISD::SETOLT: Flip = true; // Fallthrough
1356 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1357 case ISD::SETOLE: Flip = true; // Fallthrough
1359 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1360 case ISD::SETUGT: Flip = true; // Fallthrough
1362 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1363 case ISD::SETUGE: Flip = true; // Fallthrough
1365 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1367 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1368 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1369 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1373 return X86CC != X86ISD::COND_INVALID;
1376 static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1378 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1381 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1382 /// code. Current x86 isa includes the following FP cmov instructions:
1383 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1384 static bool hasFPCMov(unsigned X86CC) {
1388 case X86ISD::COND_B:
1389 case X86ISD::COND_BE:
1390 case X86ISD::COND_E:
1391 case X86ISD::COND_P:
1392 case X86ISD::COND_A:
1393 case X86ISD::COND_AE:
1394 case X86ISD::COND_NE:
1395 case X86ISD::COND_NP:
1400 /// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1401 /// load. For Darwin, external and weak symbols are indirect, loading the value
1402 /// at address GV rather then the value of GV itself. This means that the
1403 /// GlobalAddress must be in the base or index register of the address, not the
1404 /// GV offset field.
1405 static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1406 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1407 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1410 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1411 /// true if Op is undef or if its value falls within the specified range (L, H].
1412 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1413 if (Op.getOpcode() == ISD::UNDEF)
1416 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1417 return (Val >= Low && Val < Hi);
1420 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1421 /// true if Op is undef or if its value equal to the specified value.
1422 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1423 if (Op.getOpcode() == ISD::UNDEF)
1425 return cast<ConstantSDNode>(Op)->getValue() == Val;
1428 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1429 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1430 bool X86::isPSHUFDMask(SDNode *N) {
1431 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1433 if (N->getNumOperands() != 4)
1436 // Check if the value doesn't reference the second vector.
1437 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1438 SDOperand Arg = N->getOperand(i);
1439 if (Arg.getOpcode() == ISD::UNDEF) continue;
1440 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1441 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1448 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1449 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1450 bool X86::isPSHUFHWMask(SDNode *N) {
1451 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1453 if (N->getNumOperands() != 8)
1456 // Lower quadword copied in order.
1457 for (unsigned i = 0; i != 4; ++i) {
1458 SDOperand Arg = N->getOperand(i);
1459 if (Arg.getOpcode() == ISD::UNDEF) continue;
1460 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1461 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1465 // Upper quadword shuffled.
1466 for (unsigned i = 4; i != 8; ++i) {
1467 SDOperand Arg = N->getOperand(i);
1468 if (Arg.getOpcode() == ISD::UNDEF) continue;
1469 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1470 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1471 if (Val < 4 || Val > 7)
1478 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1479 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1480 bool X86::isPSHUFLWMask(SDNode *N) {
1481 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1483 if (N->getNumOperands() != 8)
1486 // Upper quadword copied in order.
1487 for (unsigned i = 4; i != 8; ++i)
1488 if (!isUndefOrEqual(N->getOperand(i), i))
1491 // Lower quadword shuffled.
1492 for (unsigned i = 0; i != 4; ++i)
1493 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1499 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1500 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1501 static bool isSHUFPMask(std::vector<SDOperand> &N) {
1502 unsigned NumElems = N.size();
1503 if (NumElems != 2 && NumElems != 4) return false;
1505 unsigned Half = NumElems / 2;
1506 for (unsigned i = 0; i < Half; ++i)
1507 if (!isUndefOrInRange(N[i], 0, NumElems))
1509 for (unsigned i = Half; i < NumElems; ++i)
1510 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1516 bool X86::isSHUFPMask(SDNode *N) {
1517 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1518 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1519 return ::isSHUFPMask(Ops);
1522 /// isCommutedSHUFP - Returns true if the shuffle mask is except
1523 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1524 /// half elements to come from vector 1 (which would equal the dest.) and
1525 /// the upper half to come from vector 2.
1526 static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1527 unsigned NumElems = Ops.size();
1528 if (NumElems != 2 && NumElems != 4) return false;
1530 unsigned Half = NumElems / 2;
1531 for (unsigned i = 0; i < Half; ++i)
1532 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1534 for (unsigned i = Half; i < NumElems; ++i)
1535 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1540 static bool isCommutedSHUFP(SDNode *N) {
1541 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1542 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1543 return isCommutedSHUFP(Ops);
1546 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1547 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1548 bool X86::isMOVHLPSMask(SDNode *N) {
1549 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1551 if (N->getNumOperands() != 4)
1554 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1555 return isUndefOrEqual(N->getOperand(0), 6) &&
1556 isUndefOrEqual(N->getOperand(1), 7) &&
1557 isUndefOrEqual(N->getOperand(2), 2) &&
1558 isUndefOrEqual(N->getOperand(3), 3);
1561 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1562 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1563 bool X86::isMOVLPMask(SDNode *N) {
1564 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1566 unsigned NumElems = N->getNumOperands();
1567 if (NumElems != 2 && NumElems != 4)
1570 for (unsigned i = 0; i < NumElems/2; ++i)
1571 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1574 for (unsigned i = NumElems/2; i < NumElems; ++i)
1575 if (!isUndefOrEqual(N->getOperand(i), i))
1581 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1582 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1584 bool X86::isMOVHPMask(SDNode *N) {
1585 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1587 unsigned NumElems = N->getNumOperands();
1588 if (NumElems != 2 && NumElems != 4)
1591 for (unsigned i = 0; i < NumElems/2; ++i)
1592 if (!isUndefOrEqual(N->getOperand(i), i))
1595 for (unsigned i = 0; i < NumElems/2; ++i) {
1596 SDOperand Arg = N->getOperand(i + NumElems/2);
1597 if (!isUndefOrEqual(Arg, i + NumElems))
1604 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1605 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1606 bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1607 unsigned NumElems = N.size();
1608 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1611 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1612 SDOperand BitI = N[i];
1613 SDOperand BitI1 = N[i+1];
1614 if (!isUndefOrEqual(BitI, j))
1617 if (isUndefOrEqual(BitI1, NumElems))
1620 if (!isUndefOrEqual(BitI1, j + NumElems))
1628 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1629 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1630 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1631 return ::isUNPCKLMask(Ops, V2IsSplat);
1634 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1635 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1636 bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1637 unsigned NumElems = N.size();
1638 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1641 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1642 SDOperand BitI = N[i];
1643 SDOperand BitI1 = N[i+1];
1644 if (!isUndefOrEqual(BitI, j + NumElems/2))
1647 if (isUndefOrEqual(BitI1, NumElems))
1650 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1658 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1659 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1660 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1661 return ::isUNPCKHMask(Ops, V2IsSplat);
1664 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1665 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1667 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1668 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1670 unsigned NumElems = N->getNumOperands();
1671 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1674 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1675 SDOperand BitI = N->getOperand(i);
1676 SDOperand BitI1 = N->getOperand(i+1);
1678 if (!isUndefOrEqual(BitI, j))
1680 if (!isUndefOrEqual(BitI1, j))
1687 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1688 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1689 /// MOVSD, and MOVD, i.e. setting the lowest element.
1690 static bool isMOVLMask(std::vector<SDOperand> &N) {
1691 unsigned NumElems = N.size();
1692 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1695 if (!isUndefOrEqual(N[0], NumElems))
1698 for (unsigned i = 1; i < NumElems; ++i) {
1699 SDOperand Arg = N[i];
1700 if (!isUndefOrEqual(Arg, i))
1707 bool X86::isMOVLMask(SDNode *N) {
1708 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1709 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1710 return ::isMOVLMask(Ops);
1713 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1714 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1715 /// element of vector 2 and the other elements to come from vector 1 in order.
1716 static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
1717 unsigned NumElems = Ops.size();
1718 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1721 if (!isUndefOrEqual(Ops[0], 0))
1724 for (unsigned i = 1; i < NumElems; ++i) {
1725 SDOperand Arg = Ops[i];
1727 if (!isUndefOrEqual(Arg, NumElems))
1730 if (!isUndefOrEqual(Arg, i+NumElems))
1738 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
1739 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1740 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1741 return isCommutedMOVL(Ops, V2IsSplat);
1744 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1745 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1746 bool X86::isMOVSHDUPMask(SDNode *N) {
1747 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1749 if (N->getNumOperands() != 4)
1752 // Expect 1, 1, 3, 3
1753 for (unsigned i = 0; i < 2; ++i) {
1754 SDOperand Arg = N->getOperand(i);
1755 if (Arg.getOpcode() == ISD::UNDEF) continue;
1756 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1757 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1758 if (Val != 1) return false;
1762 for (unsigned i = 2; i < 4; ++i) {
1763 SDOperand Arg = N->getOperand(i);
1764 if (Arg.getOpcode() == ISD::UNDEF) continue;
1765 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1766 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1767 if (Val != 3) return false;
1771 // Don't use movshdup if it can be done with a shufps.
1775 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1776 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1777 bool X86::isMOVSLDUPMask(SDNode *N) {
1778 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1780 if (N->getNumOperands() != 4)
1783 // Expect 0, 0, 2, 2
1784 for (unsigned i = 0; i < 2; ++i) {
1785 SDOperand Arg = N->getOperand(i);
1786 if (Arg.getOpcode() == ISD::UNDEF) continue;
1787 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1788 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1789 if (Val != 0) return false;
1793 for (unsigned i = 2; i < 4; ++i) {
1794 SDOperand Arg = N->getOperand(i);
1795 if (Arg.getOpcode() == ISD::UNDEF) continue;
1796 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1797 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1798 if (Val != 2) return false;
1802 // Don't use movshdup if it can be done with a shufps.
1806 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1807 /// a splat of a single element.
1808 static bool isSplatMask(SDNode *N) {
1809 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1811 // This is a splat operation if each element of the permute is the same, and
1812 // if the value doesn't reference the second vector.
1813 unsigned NumElems = N->getNumOperands();
1814 SDOperand ElementBase;
1816 for (; i != NumElems; ++i) {
1817 SDOperand Elt = N->getOperand(i);
1818 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1824 if (!ElementBase.Val)
1827 for (; i != NumElems; ++i) {
1828 SDOperand Arg = N->getOperand(i);
1829 if (Arg.getOpcode() == ISD::UNDEF) continue;
1830 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1831 if (Arg != ElementBase) return false;
1834 // Make sure it is a splat of the first vector operand.
1835 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
1838 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1839 /// a splat of a single element and it's a 2 or 4 element mask.
1840 bool X86::isSplatMask(SDNode *N) {
1841 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1843 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
1844 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1846 return ::isSplatMask(N);
1849 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1850 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1852 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
1853 unsigned NumOperands = N->getNumOperands();
1854 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1856 for (unsigned i = 0; i < NumOperands; ++i) {
1858 SDOperand Arg = N->getOperand(NumOperands-i-1);
1859 if (Arg.getOpcode() != ISD::UNDEF)
1860 Val = cast<ConstantSDNode>(Arg)->getValue();
1861 if (Val >= NumOperands) Val -= NumOperands;
1863 if (i != NumOperands - 1)
1870 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1871 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1873 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1875 // 8 nodes, but we only care about the last 4.
1876 for (unsigned i = 7; i >= 4; --i) {
1878 SDOperand Arg = N->getOperand(i);
1879 if (Arg.getOpcode() != ISD::UNDEF)
1880 Val = cast<ConstantSDNode>(Arg)->getValue();
1889 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1890 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1892 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1894 // 8 nodes, but we only care about the first 4.
1895 for (int i = 3; i >= 0; --i) {
1897 SDOperand Arg = N->getOperand(i);
1898 if (Arg.getOpcode() != ISD::UNDEF)
1899 Val = cast<ConstantSDNode>(Arg)->getValue();
1908 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1909 /// specifies a 8 element shuffle that can be broken into a pair of
1910 /// PSHUFHW and PSHUFLW.
1911 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1912 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1914 if (N->getNumOperands() != 8)
1917 // Lower quadword shuffled.
1918 for (unsigned i = 0; i != 4; ++i) {
1919 SDOperand Arg = N->getOperand(i);
1920 if (Arg.getOpcode() == ISD::UNDEF) continue;
1921 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1922 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1927 // Upper quadword shuffled.
1928 for (unsigned i = 4; i != 8; ++i) {
1929 SDOperand Arg = N->getOperand(i);
1930 if (Arg.getOpcode() == ISD::UNDEF) continue;
1931 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1932 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1933 if (Val < 4 || Val > 7)
1940 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
1941 /// values in ther permute mask.
1942 static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
1943 SDOperand V1 = Op.getOperand(0);
1944 SDOperand V2 = Op.getOperand(1);
1945 SDOperand Mask = Op.getOperand(2);
1946 MVT::ValueType VT = Op.getValueType();
1947 MVT::ValueType MaskVT = Mask.getValueType();
1948 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
1949 unsigned NumElems = Mask.getNumOperands();
1950 std::vector<SDOperand> MaskVec;
1952 for (unsigned i = 0; i != NumElems; ++i) {
1953 SDOperand Arg = Mask.getOperand(i);
1954 if (Arg.getOpcode() == ISD::UNDEF) {
1955 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1958 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1959 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1961 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
1963 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
1966 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
1967 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1970 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
1971 /// match movhlps. The lower half elements should come from upper half of
1972 /// V1 (and in order), and the upper half elements should come from the upper
1973 /// half of V2 (and in order).
1974 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
1975 unsigned NumElems = Mask->getNumOperands();
1978 for (unsigned i = 0, e = 2; i != e; ++i)
1979 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
1981 for (unsigned i = 2; i != 4; ++i)
1982 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
1987 /// isScalarLoadToVector - Returns true if the node is a scalar load that
1988 /// is promoted to a vector.
1989 static inline bool isScalarLoadToVector(SDNode *N) {
1990 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
1991 N = N->getOperand(0).Val;
1992 return (N->getOpcode() == ISD::LOAD);
1997 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
1998 /// match movlp{s|d}. The lower half elements should come from lower half of
1999 /// V1 (and in order), and the upper half elements should come from the upper
2000 /// half of V2 (and in order). And since V1 will become the source of the
2001 /// MOVLP, it must be either a vector load or a scalar load to vector.
2002 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2003 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2006 unsigned NumElems = Mask->getNumOperands();
2007 if (NumElems != 2 && NumElems != 4)
2009 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2010 if (!isUndefOrEqual(Mask->getOperand(i), i))
2012 for (unsigned i = NumElems/2; i != NumElems; ++i)
2013 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2018 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2020 static bool isSplatVector(SDNode *N) {
2021 if (N->getOpcode() != ISD::BUILD_VECTOR)
2024 SDOperand SplatValue = N->getOperand(0);
2025 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2026 if (N->getOperand(i) != SplatValue)
2031 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2032 /// that point to V2 points to its first element.
2033 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2034 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2036 bool Changed = false;
2037 std::vector<SDOperand> MaskVec;
2038 unsigned NumElems = Mask.getNumOperands();
2039 for (unsigned i = 0; i != NumElems; ++i) {
2040 SDOperand Arg = Mask.getOperand(i);
2041 if (Arg.getOpcode() != ISD::UNDEF) {
2042 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2043 if (Val > NumElems) {
2044 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2048 MaskVec.push_back(Arg);
2052 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2056 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2057 /// operation of specified width.
2058 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2059 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2060 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2062 std::vector<SDOperand> MaskVec;
2063 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2064 for (unsigned i = 1; i != NumElems; ++i)
2065 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2066 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2069 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2070 /// of specified width.
2071 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2072 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2073 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2074 std::vector<SDOperand> MaskVec;
2075 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2076 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2077 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2079 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2082 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2083 /// of specified width.
2084 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2085 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2086 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2087 unsigned Half = NumElems/2;
2088 std::vector<SDOperand> MaskVec;
2089 for (unsigned i = 0; i != Half; ++i) {
2090 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2091 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2093 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2096 /// getZeroVector - Returns a vector of specified type with all zero elements.
2098 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2099 assert(MVT::isVector(VT) && "Expected a vector type");
2100 unsigned NumElems = getVectorNumElements(VT);
2101 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2102 bool isFP = MVT::isFloatingPoint(EVT);
2103 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2104 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2105 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2108 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2110 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2111 SDOperand V1 = Op.getOperand(0);
2112 SDOperand Mask = Op.getOperand(2);
2113 MVT::ValueType VT = Op.getValueType();
2114 unsigned NumElems = Mask.getNumOperands();
2115 Mask = getUnpacklMask(NumElems, DAG);
2116 while (NumElems != 4) {
2117 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2120 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2122 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2123 Mask = getZeroVector(MaskVT, DAG);
2124 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2125 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2126 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2129 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2131 static inline bool isZeroNode(SDOperand Elt) {
2132 return ((isa<ConstantSDNode>(Elt) &&
2133 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2134 (isa<ConstantFPSDNode>(Elt) &&
2135 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2138 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2139 /// vector and zero or undef vector.
2140 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2141 unsigned NumElems, unsigned Idx,
2142 bool isZero, SelectionDAG &DAG) {
2143 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2144 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2145 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2146 SDOperand Zero = DAG.getConstant(0, EVT);
2147 std::vector<SDOperand> MaskVec(NumElems, Zero);
2148 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2149 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2150 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2153 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2155 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2156 unsigned NumNonZero, unsigned NumZero,
2157 SelectionDAG &DAG) {
2163 for (unsigned i = 0; i < 16; ++i) {
2164 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2165 if (ThisIsNonZero && First) {
2167 V = getZeroVector(MVT::v8i16, DAG);
2169 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2174 SDOperand ThisElt(0, 0), LastElt(0, 0);
2175 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2176 if (LastIsNonZero) {
2177 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2179 if (ThisIsNonZero) {
2180 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2181 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2182 ThisElt, DAG.getConstant(8, MVT::i8));
2184 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2189 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2190 DAG.getConstant(i/2, MVT::i32));
2194 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2197 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2199 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2200 unsigned NumNonZero, unsigned NumZero,
2201 SelectionDAG &DAG) {
2207 for (unsigned i = 0; i < 8; ++i) {
2208 bool isNonZero = (NonZeros & (1 << i)) != 0;
2212 V = getZeroVector(MVT::v8i16, DAG);
2214 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2217 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2218 DAG.getConstant(i, MVT::i32));
2226 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2227 // All zero's are handled with pxor.
2228 if (ISD::isBuildVectorAllZeros(Op.Val))
2231 // All one's are handled with pcmpeqd.
2232 if (ISD::isBuildVectorAllOnes(Op.Val))
2235 MVT::ValueType VT = Op.getValueType();
2236 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2237 unsigned EVTBits = MVT::getSizeInBits(EVT);
2239 unsigned NumElems = Op.getNumOperands();
2240 unsigned NumZero = 0;
2241 unsigned NumNonZero = 0;
2242 unsigned NonZeros = 0;
2243 std::set<SDOperand> Values;
2244 for (unsigned i = 0; i < NumElems; ++i) {
2245 SDOperand Elt = Op.getOperand(i);
2246 if (Elt.getOpcode() != ISD::UNDEF) {
2248 if (isZeroNode(Elt))
2251 NonZeros |= (1 << i);
2257 if (NumNonZero == 0)
2258 // Must be a mix of zero and undef. Return a zero vector.
2259 return getZeroVector(VT, DAG);
2261 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2262 if (Values.size() == 1)
2265 // Special case for single non-zero element.
2266 if (NumNonZero == 1) {
2267 unsigned Idx = CountTrailingZeros_32(NonZeros);
2268 SDOperand Item = Op.getOperand(Idx);
2269 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2271 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2272 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2275 if (EVTBits == 32) {
2276 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2277 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2279 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2280 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2281 std::vector<SDOperand> MaskVec;
2282 for (unsigned i = 0; i < NumElems; i++)
2283 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2284 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2285 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2286 DAG.getNode(ISD::UNDEF, VT), Mask);
2290 // Let legalizer expand 2-widde build_vector's.
2294 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2296 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2297 if (V.Val) return V;
2300 if (EVTBits == 16) {
2301 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2302 if (V.Val) return V;
2305 // If element VT is == 32 bits, turn it into a number of shuffles.
2306 std::vector<SDOperand> V(NumElems);
2307 if (NumElems == 4 && NumZero > 0) {
2308 for (unsigned i = 0; i < 4; ++i) {
2309 bool isZero = !(NonZeros & (1 << i));
2311 V[i] = getZeroVector(VT, DAG);
2313 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2316 for (unsigned i = 0; i < 2; ++i) {
2317 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2320 V[i] = V[i*2]; // Must be a zero vector.
2323 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2324 getMOVLMask(NumElems, DAG));
2327 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2328 getMOVLMask(NumElems, DAG));
2331 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2332 getUnpacklMask(NumElems, DAG));
2337 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2338 // clears the upper bits.
2339 // FIXME: we can do the same for v4f32 case when we know both parts of
2340 // the lower half come from scalar_to_vector (loadf32). We should do
2341 // that in post legalizer dag combiner with target specific hooks.
2342 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2344 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2345 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2346 std::vector<SDOperand> MaskVec;
2347 bool Reverse = (NonZeros & 0x3) == 2;
2348 for (unsigned i = 0; i < 2; ++i)
2350 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2352 MaskVec.push_back(DAG.getConstant(i, EVT));
2353 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2354 for (unsigned i = 0; i < 2; ++i)
2356 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2358 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2359 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2360 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2363 if (Values.size() > 2) {
2364 // Expand into a number of unpckl*.
2366 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2367 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2368 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2369 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2370 for (unsigned i = 0; i < NumElems; ++i)
2371 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2373 while (NumElems != 0) {
2374 for (unsigned i = 0; i < NumElems; ++i)
2375 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2386 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2387 SDOperand V1 = Op.getOperand(0);
2388 SDOperand V2 = Op.getOperand(1);
2389 SDOperand PermMask = Op.getOperand(2);
2390 MVT::ValueType VT = Op.getValueType();
2391 unsigned NumElems = PermMask.getNumOperands();
2392 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2393 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2395 if (isSplatMask(PermMask.Val)) {
2396 if (NumElems <= 4) return Op;
2397 // Promote it to a v4i32 splat.
2398 return PromoteSplat(Op, DAG);
2401 if (X86::isMOVLMask(PermMask.Val))
2402 return (V1IsUndef) ? V2 : Op;
2404 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2405 X86::isMOVSLDUPMask(PermMask.Val) ||
2406 X86::isMOVHLPSMask(PermMask.Val) ||
2407 X86::isMOVHPMask(PermMask.Val) ||
2408 X86::isMOVLPMask(PermMask.Val))
2411 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2412 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2413 return CommuteVectorShuffle(Op, DAG);
2415 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2416 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2417 if (V1IsSplat && !V2IsSplat) {
2418 Op = CommuteVectorShuffle(Op, DAG);
2419 V1 = Op.getOperand(0);
2420 V2 = Op.getOperand(1);
2421 PermMask = Op.getOperand(2);
2425 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2426 if (V2IsUndef) return V1;
2427 Op = CommuteVectorShuffle(Op, DAG);
2428 V1 = Op.getOperand(0);
2429 V2 = Op.getOperand(1);
2430 PermMask = Op.getOperand(2);
2432 // V2 is a splat, so the mask may be malformed. That is, it may point
2433 // to any V2 element. The instruction selectior won't like this. Get
2434 // a corrected mask and commute to form a proper MOVS{S|D}.
2435 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2436 if (NewMask.Val != PermMask.Val)
2437 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2442 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2443 X86::isUNPCKLMask(PermMask.Val) ||
2444 X86::isUNPCKHMask(PermMask.Val))
2448 // Normalize mask so all entries that point to V2 points to its first
2449 // element then try to match unpck{h|l} again. If match, return a
2450 // new vector_shuffle with the corrected mask.
2451 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2452 if (NewMask.Val != PermMask.Val) {
2453 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2454 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2455 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2456 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2457 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2458 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2463 // Normalize the node to match x86 shuffle ops if needed
2464 if (V2.getOpcode() != ISD::UNDEF)
2465 if (isCommutedSHUFP(PermMask.Val)) {
2466 Op = CommuteVectorShuffle(Op, DAG);
2467 V1 = Op.getOperand(0);
2468 V2 = Op.getOperand(1);
2469 PermMask = Op.getOperand(2);
2472 // If VT is integer, try PSHUF* first, then SHUFP*.
2473 if (MVT::isInteger(VT)) {
2474 if (X86::isPSHUFDMask(PermMask.Val) ||
2475 X86::isPSHUFHWMask(PermMask.Val) ||
2476 X86::isPSHUFLWMask(PermMask.Val)) {
2477 if (V2.getOpcode() != ISD::UNDEF)
2478 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2479 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2483 if (X86::isSHUFPMask(PermMask.Val))
2486 // Handle v8i16 shuffle high / low shuffle node pair.
2487 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2488 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2489 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2490 std::vector<SDOperand> MaskVec;
2491 for (unsigned i = 0; i != 4; ++i)
2492 MaskVec.push_back(PermMask.getOperand(i));
2493 for (unsigned i = 4; i != 8; ++i)
2494 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2495 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2496 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2498 for (unsigned i = 0; i != 4; ++i)
2499 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2500 for (unsigned i = 4; i != 8; ++i)
2501 MaskVec.push_back(PermMask.getOperand(i));
2502 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2503 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2506 // Floating point cases in the other order.
2507 if (X86::isSHUFPMask(PermMask.Val))
2509 if (X86::isPSHUFDMask(PermMask.Val) ||
2510 X86::isPSHUFHWMask(PermMask.Val) ||
2511 X86::isPSHUFLWMask(PermMask.Val)) {
2512 if (V2.getOpcode() != ISD::UNDEF)
2513 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2514 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2519 if (NumElems == 4) {
2520 MVT::ValueType MaskVT = PermMask.getValueType();
2521 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2522 std::vector<std::pair<int, int> > Locs;
2523 Locs.reserve(NumElems);
2524 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2525 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2528 // If no more than two elements come from either vector. This can be
2529 // implemented with two shuffles. First shuffle gather the elements.
2530 // The second shuffle, which takes the first shuffle as both of its
2531 // vector operands, put the elements into the right order.
2532 for (unsigned i = 0; i != NumElems; ++i) {
2533 SDOperand Elt = PermMask.getOperand(i);
2534 if (Elt.getOpcode() == ISD::UNDEF) {
2535 Locs[i] = std::make_pair(-1, -1);
2537 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2538 if (Val < NumElems) {
2539 Locs[i] = std::make_pair(0, NumLo);
2543 Locs[i] = std::make_pair(1, NumHi);
2544 if (2+NumHi < NumElems)
2545 Mask1[2+NumHi] = Elt;
2550 if (NumLo <= 2 && NumHi <= 2) {
2551 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2552 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2553 for (unsigned i = 0; i != NumElems; ++i) {
2554 if (Locs[i].first == -1)
2557 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2558 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2559 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2563 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2564 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2567 // Break it into (shuffle shuffle_hi, shuffle_lo).
2569 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2570 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2571 std::vector<SDOperand> *MaskPtr = &LoMask;
2572 unsigned MaskIdx = 0;
2574 unsigned HiIdx = NumElems/2;
2575 for (unsigned i = 0; i != NumElems; ++i) {
2576 if (i == NumElems/2) {
2582 SDOperand Elt = PermMask.getOperand(i);
2583 if (Elt.getOpcode() == ISD::UNDEF) {
2584 Locs[i] = std::make_pair(-1, -1);
2585 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2586 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2587 (*MaskPtr)[LoIdx] = Elt;
2590 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2591 (*MaskPtr)[HiIdx] = Elt;
2596 SDOperand LoShuffle =
2597 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2598 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2599 SDOperand HiShuffle =
2600 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2601 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
2602 std::vector<SDOperand> MaskOps;
2603 for (unsigned i = 0; i != NumElems; ++i) {
2604 if (Locs[i].first == -1) {
2605 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2607 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2608 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2611 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2612 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2619 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2620 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2623 MVT::ValueType VT = Op.getValueType();
2624 // TODO: handle v16i8.
2625 if (MVT::getSizeInBits(VT) == 16) {
2626 // Transform it so it match pextrw which produces a 32-bit result.
2627 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2628 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2629 Op.getOperand(0), Op.getOperand(1));
2630 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2631 DAG.getValueType(VT));
2632 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2633 } else if (MVT::getSizeInBits(VT) == 32) {
2634 SDOperand Vec = Op.getOperand(0);
2635 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2638 // SHUFPS the element to the lowest double word, then movss.
2639 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2640 std::vector<SDOperand> IdxVec;
2641 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2642 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2643 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2644 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2645 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2646 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2648 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2649 DAG.getConstant(0, getPointerTy()));
2650 } else if (MVT::getSizeInBits(VT) == 64) {
2651 SDOperand Vec = Op.getOperand(0);
2652 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2656 // UNPCKHPD the element to the lowest double word, then movsd.
2657 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2658 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2659 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2660 std::vector<SDOperand> IdxVec;
2661 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2662 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2663 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2664 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2665 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2666 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2667 DAG.getConstant(0, getPointerTy()));
2674 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2675 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2676 // as its second argument.
2677 MVT::ValueType VT = Op.getValueType();
2678 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2679 SDOperand N0 = Op.getOperand(0);
2680 SDOperand N1 = Op.getOperand(1);
2681 SDOperand N2 = Op.getOperand(2);
2682 if (MVT::getSizeInBits(BaseVT) == 16) {
2683 if (N1.getValueType() != MVT::i32)
2684 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2685 if (N2.getValueType() != MVT::i32)
2686 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2687 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2688 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2689 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2692 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2693 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2694 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2695 std::vector<SDOperand> MaskVec;
2696 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2697 for (unsigned i = 1; i <= 3; ++i)
2698 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2699 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2700 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2702 // Use two pinsrw instructions to insert a 32 bit value.
2704 if (MVT::isFloatingPoint(N1.getValueType())) {
2705 if (N1.getOpcode() == ISD::LOAD) {
2706 // Just load directly from f32mem to GR32.
2707 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2710 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2711 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2712 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2713 DAG.getConstant(0, getPointerTy()));
2716 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2717 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2718 DAG.getConstant(Idx, getPointerTy()));
2719 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2720 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2721 DAG.getConstant(Idx+1, getPointerTy()));
2722 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2730 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2731 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2732 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2735 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2736 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2737 // one of the above mentioned nodes. It has to be wrapped because otherwise
2738 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2739 // be used to form addressing mode. These wrapped nodes will be selected
2742 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2743 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2744 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2745 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2746 CP->getAlignment()));
2747 if (Subtarget->isTargetDarwin()) {
2748 // With PIC, the address is actually $g + Offset.
2749 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2750 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2751 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2758 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2759 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2760 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2761 DAG.getTargetGlobalAddress(GV,
2763 if (Subtarget->isTargetDarwin()) {
2764 // With PIC, the address is actually $g + Offset.
2765 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2766 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2767 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2770 // For Darwin, external and weak symbols are indirect, so we want to load
2771 // the value at address GV, not the value of GV itself. This means that
2772 // the GlobalAddress must be in the base or index register of the address,
2773 // not the GV offset field.
2774 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2775 DarwinGVRequiresExtraLoad(GV))
2776 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2777 Result, DAG.getSrcValue(NULL));
2784 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2785 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2786 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2787 DAG.getTargetExternalSymbol(Sym,
2789 if (Subtarget->isTargetDarwin()) {
2790 // With PIC, the address is actually $g + Offset.
2791 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2792 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2793 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2800 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
2801 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2802 "Not an i64 shift!");
2803 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2804 SDOperand ShOpLo = Op.getOperand(0);
2805 SDOperand ShOpHi = Op.getOperand(1);
2806 SDOperand ShAmt = Op.getOperand(2);
2807 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
2808 DAG.getConstant(31, MVT::i8))
2809 : DAG.getConstant(0, MVT::i32);
2811 SDOperand Tmp2, Tmp3;
2812 if (Op.getOpcode() == ISD::SHL_PARTS) {
2813 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2814 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2816 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
2817 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
2820 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2821 ShAmt, DAG.getConstant(32, MVT::i8));
2824 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
2826 std::vector<MVT::ValueType> Tys;
2827 Tys.push_back(MVT::i32);
2828 Tys.push_back(MVT::Flag);
2829 std::vector<SDOperand> Ops;
2830 if (Op.getOpcode() == ISD::SHL_PARTS) {
2831 Ops.push_back(Tmp2);
2832 Ops.push_back(Tmp3);
2834 Ops.push_back(InFlag);
2835 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2836 InFlag = Hi.getValue(1);
2839 Ops.push_back(Tmp3);
2840 Ops.push_back(Tmp1);
2842 Ops.push_back(InFlag);
2843 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2845 Ops.push_back(Tmp2);
2846 Ops.push_back(Tmp3);
2848 Ops.push_back(InFlag);
2849 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2850 InFlag = Lo.getValue(1);
2853 Ops.push_back(Tmp3);
2854 Ops.push_back(Tmp1);
2856 Ops.push_back(InFlag);
2857 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2861 Tys.push_back(MVT::i32);
2862 Tys.push_back(MVT::i32);
2866 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
2869 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2870 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2871 Op.getOperand(0).getValueType() >= MVT::i16 &&
2872 "Unknown SINT_TO_FP to lower!");
2875 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2876 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
2877 MachineFunction &MF = DAG.getMachineFunction();
2878 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2879 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2880 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2881 DAG.getEntryNode(), Op.getOperand(0),
2882 StackSlot, DAG.getSrcValue(NULL));
2885 std::vector<MVT::ValueType> Tys;
2886 Tys.push_back(MVT::f64);
2887 Tys.push_back(MVT::Other);
2888 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
2889 std::vector<SDOperand> Ops;
2890 Ops.push_back(Chain);
2891 Ops.push_back(StackSlot);
2892 Ops.push_back(DAG.getValueType(SrcVT));
2893 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
2897 Chain = Result.getValue(1);
2898 SDOperand InFlag = Result.getValue(2);
2900 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
2901 // shouldn't be necessary except that RFP cannot be live across
2902 // multiple blocks. When stackifier is fixed, they can be uncoupled.
2903 MachineFunction &MF = DAG.getMachineFunction();
2904 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2905 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2906 std::vector<MVT::ValueType> Tys;
2907 Tys.push_back(MVT::Other);
2908 std::vector<SDOperand> Ops;
2909 Ops.push_back(Chain);
2910 Ops.push_back(Result);
2911 Ops.push_back(StackSlot);
2912 Ops.push_back(DAG.getValueType(Op.getValueType()));
2913 Ops.push_back(InFlag);
2914 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
2915 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
2916 DAG.getSrcValue(NULL));
2922 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
2923 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
2924 "Unknown FP_TO_SINT to lower!");
2925 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
2927 MachineFunction &MF = DAG.getMachineFunction();
2928 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
2929 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2930 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2933 switch (Op.getValueType()) {
2934 default: assert(0 && "Invalid FP_TO_SINT to lower!");
2935 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
2936 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
2937 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
2940 SDOperand Chain = DAG.getEntryNode();
2941 SDOperand Value = Op.getOperand(0);
2943 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
2944 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
2945 DAG.getSrcValue(0));
2946 std::vector<MVT::ValueType> Tys;
2947 Tys.push_back(MVT::f64);
2948 Tys.push_back(MVT::Other);
2949 std::vector<SDOperand> Ops;
2950 Ops.push_back(Chain);
2951 Ops.push_back(StackSlot);
2952 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
2953 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
2954 Chain = Value.getValue(1);
2955 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2956 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2959 // Build the FP_TO_INT*_IN_MEM
2960 std::vector<SDOperand> Ops;
2961 Ops.push_back(Chain);
2962 Ops.push_back(Value);
2963 Ops.push_back(StackSlot);
2964 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
2967 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
2968 DAG.getSrcValue(NULL));
2971 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
2972 MVT::ValueType VT = Op.getValueType();
2973 const Type *OpNTy = MVT::getTypeForValueType(VT);
2974 std::vector<Constant*> CV;
2975 if (VT == MVT::f64) {
2976 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
2977 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2979 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
2980 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2981 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2982 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2984 Constant *CS = ConstantStruct::get(CV);
2985 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2987 = DAG.getNode(X86ISD::LOAD_PACK,
2988 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2989 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
2992 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
2993 MVT::ValueType VT = Op.getValueType();
2994 const Type *OpNTy = MVT::getTypeForValueType(VT);
2995 std::vector<Constant*> CV;
2996 if (VT == MVT::f64) {
2997 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
2998 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3000 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3001 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3002 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3003 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3005 Constant *CS = ConstantStruct::get(CV);
3006 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3007 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3008 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3009 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3012 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3013 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3015 SDOperand CC = Op.getOperand(2);
3016 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3017 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3020 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3022 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3023 Op.getOperand(1), Op.getOperand(0));
3025 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3026 Op.getOperand(0), Op.getOperand(1));
3027 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3028 DAG.getConstant(X86CC, MVT::i8), Cond);
3030 assert(isFP && "Illegal integer SetCC!");
3032 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3033 Op.getOperand(0), Op.getOperand(1));
3034 std::vector<MVT::ValueType> Tys;
3035 std::vector<SDOperand> Ops;
3036 switch (SetCCOpcode) {
3037 default: assert(false && "Illegal floating point SetCC!");
3038 case ISD::SETOEQ: { // !PF & ZF
3039 Tys.push_back(MVT::i8);
3040 Tys.push_back(MVT::Flag);
3041 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3042 Ops.push_back(Cond);
3043 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3044 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3045 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3047 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3049 case ISD::SETUNE: { // PF | !ZF
3050 Tys.push_back(MVT::i8);
3051 Tys.push_back(MVT::Flag);
3052 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3053 Ops.push_back(Cond);
3054 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3055 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3056 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3058 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3064 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3065 MVT::ValueType VT = Op.getValueType();
3066 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3067 bool addTest = false;
3068 SDOperand Op0 = Op.getOperand(0);
3070 if (Op0.getOpcode() == ISD::SETCC)
3071 Op0 = LowerOperation(Op0, DAG);
3073 if (Op0.getOpcode() == X86ISD::SETCC) {
3074 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3075 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3076 // have another use it will be eliminated.
3077 // If the X86ISD::SETCC has more than one use, then it's probably better
3078 // to use a test instead of duplicating the X86ISD::CMP (for register
3079 // pressure reason).
3080 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3081 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3082 CmpOpc == X86ISD::UCOMI) {
3083 if (!Op0.hasOneUse()) {
3084 std::vector<MVT::ValueType> Tys;
3085 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3086 Tys.push_back(Op0.Val->getValueType(i));
3087 std::vector<SDOperand> Ops;
3088 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3089 Ops.push_back(Op0.getOperand(i));
3090 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3093 CC = Op0.getOperand(0);
3094 Cond = Op0.getOperand(1);
3095 // Make a copy as flag result cannot be used by more than one.
3096 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3097 Cond.getOperand(0), Cond.getOperand(1));
3099 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3106 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3107 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
3110 std::vector<MVT::ValueType> Tys;
3111 Tys.push_back(Op.getValueType());
3112 Tys.push_back(MVT::Flag);
3113 std::vector<SDOperand> Ops;
3114 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3115 // condition is true.
3116 Ops.push_back(Op.getOperand(2));
3117 Ops.push_back(Op.getOperand(1));
3119 Ops.push_back(Cond);
3120 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3123 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3124 bool addTest = false;
3125 SDOperand Cond = Op.getOperand(1);
3126 SDOperand Dest = Op.getOperand(2);
3128 if (Cond.getOpcode() == ISD::SETCC)
3129 Cond = LowerOperation(Cond, DAG);
3131 if (Cond.getOpcode() == X86ISD::SETCC) {
3132 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3133 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3134 // have another use it will be eliminated.
3135 // If the X86ISD::SETCC has more than one use, then it's probably better
3136 // to use a test instead of duplicating the X86ISD::CMP (for register
3137 // pressure reason).
3138 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3139 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3140 CmpOpc == X86ISD::UCOMI) {
3141 if (!Cond.hasOneUse()) {
3142 std::vector<MVT::ValueType> Tys;
3143 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3144 Tys.push_back(Cond.Val->getValueType(i));
3145 std::vector<SDOperand> Ops;
3146 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3147 Ops.push_back(Cond.getOperand(i));
3148 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3151 CC = Cond.getOperand(0);
3152 Cond = Cond.getOperand(1);
3153 // Make a copy as flag result cannot be used by more than one.
3154 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3155 Cond.getOperand(0), Cond.getOperand(1));
3162 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3163 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
3165 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3166 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3169 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3170 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3171 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3172 DAG.getTargetJumpTable(JT->getIndex(),
3174 if (Subtarget->isTargetDarwin()) {
3175 // With PIC, the address is actually $g + Offset.
3176 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3177 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3178 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3185 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3186 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3187 if (CallingConv == CallingConv::Fast && EnableFastCC)
3188 return LowerFastCCCallTo(Op, DAG);
3190 return LowerCCCCallTo(Op, DAG);
3193 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3196 switch(Op.getNumOperands()) {
3198 assert(0 && "Do not know how to return this many arguments!");
3200 case 1: // ret void.
3201 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
3202 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
3204 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
3206 if (MVT::isVector(ArgVT)) {
3207 // Integer or FP vector result -> XMM0.
3208 if (DAG.getMachineFunction().liveout_empty())
3209 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3210 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3212 } else if (MVT::isInteger(ArgVT)) {
3213 // Integer result -> EAX
3214 if (DAG.getMachineFunction().liveout_empty())
3215 DAG.getMachineFunction().addLiveOut(X86::EAX);
3217 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3219 } else if (!X86ScalarSSE) {
3220 // FP return with fp-stack value.
3221 if (DAG.getMachineFunction().liveout_empty())
3222 DAG.getMachineFunction().addLiveOut(X86::ST0);
3224 std::vector<MVT::ValueType> Tys;
3225 Tys.push_back(MVT::Other);
3226 Tys.push_back(MVT::Flag);
3227 std::vector<SDOperand> Ops;
3228 Ops.push_back(Op.getOperand(0));
3229 Ops.push_back(Op.getOperand(1));
3230 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3232 // FP return with ScalarSSE (return on fp-stack).
3233 if (DAG.getMachineFunction().liveout_empty())
3234 DAG.getMachineFunction().addLiveOut(X86::ST0);
3237 SDOperand Chain = Op.getOperand(0);
3238 SDOperand Value = Op.getOperand(1);
3240 if (Value.getOpcode() == ISD::LOAD &&
3241 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
3242 Chain = Value.getOperand(0);
3243 MemLoc = Value.getOperand(1);
3245 // Spill the value to memory and reload it into top of stack.
3246 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3247 MachineFunction &MF = DAG.getMachineFunction();
3248 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3249 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3250 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3251 Value, MemLoc, DAG.getSrcValue(0));
3253 std::vector<MVT::ValueType> Tys;
3254 Tys.push_back(MVT::f64);
3255 Tys.push_back(MVT::Other);
3256 std::vector<SDOperand> Ops;
3257 Ops.push_back(Chain);
3258 Ops.push_back(MemLoc);
3259 Ops.push_back(DAG.getValueType(ArgVT));
3260 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3262 Tys.push_back(MVT::Other);
3263 Tys.push_back(MVT::Flag);
3265 Ops.push_back(Copy.getValue(1));
3266 Ops.push_back(Copy);
3267 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3272 if (DAG.getMachineFunction().liveout_empty()) {
3273 DAG.getMachineFunction().addLiveOut(X86::EAX);
3274 DAG.getMachineFunction().addLiveOut(X86::EDX);
3277 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(3),
3279 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3282 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3283 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3288 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3289 MachineFunction &MF = DAG.getMachineFunction();
3290 const Function* Fn = MF.getFunction();
3291 if (Fn->hasExternalLinkage() &&
3292 Subtarget->TargetType == X86Subtarget::isCygwin &&
3293 Fn->getName() == "main")
3294 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3296 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3297 if (CC == CallingConv::Fast && EnableFastCC)
3298 return LowerFastCCArguments(Op, DAG);
3300 return LowerCCCArguments(Op, DAG);
3303 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3304 SDOperand InFlag(0, 0);
3305 SDOperand Chain = Op.getOperand(0);
3307 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3308 if (Align == 0) Align = 1;
3310 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3311 // If not DWORD aligned, call memset if size is less than the threshold.
3312 // It knows how to align to the right boundary first.
3313 if ((Align & 3) != 0 ||
3314 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3315 MVT::ValueType IntPtr = getPointerTy();
3316 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3317 std::vector<std::pair<SDOperand, const Type*> > Args;
3318 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3319 // Extend the ubyte argument to be an int value for the call.
3320 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3321 Args.push_back(std::make_pair(Val, IntPtrTy));
3322 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3323 std::pair<SDOperand,SDOperand> CallResult =
3324 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3325 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3326 return CallResult.second;
3331 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3332 unsigned BytesLeft = 0;
3333 bool TwoRepStos = false;
3336 unsigned Val = ValC->getValue() & 255;
3338 // If the value is a constant, then we can potentially use larger sets.
3339 switch (Align & 3) {
3340 case 2: // WORD aligned
3342 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3343 BytesLeft = I->getValue() % 2;
3344 Val = (Val << 8) | Val;
3347 case 0: // DWORD aligned
3350 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3351 BytesLeft = I->getValue() % 4;
3353 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3354 DAG.getConstant(2, MVT::i8));
3357 Val = (Val << 8) | Val;
3358 Val = (Val << 16) | Val;
3361 default: // Byte aligned
3363 Count = Op.getOperand(3);
3368 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3370 InFlag = Chain.getValue(1);
3373 Count = Op.getOperand(3);
3374 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3375 InFlag = Chain.getValue(1);
3378 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3379 InFlag = Chain.getValue(1);
3380 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3381 InFlag = Chain.getValue(1);
3383 std::vector<MVT::ValueType> Tys;
3384 Tys.push_back(MVT::Other);
3385 Tys.push_back(MVT::Flag);
3386 std::vector<SDOperand> Ops;
3387 Ops.push_back(Chain);
3388 Ops.push_back(DAG.getValueType(AVT));
3389 Ops.push_back(InFlag);
3390 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3393 InFlag = Chain.getValue(1);
3394 Count = Op.getOperand(3);
3395 MVT::ValueType CVT = Count.getValueType();
3396 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3397 DAG.getConstant(3, CVT));
3398 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3399 InFlag = Chain.getValue(1);
3401 Tys.push_back(MVT::Other);
3402 Tys.push_back(MVT::Flag);
3404 Ops.push_back(Chain);
3405 Ops.push_back(DAG.getValueType(MVT::i8));
3406 Ops.push_back(InFlag);
3407 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3408 } else if (BytesLeft) {
3409 // Issue stores for the last 1 - 3 bytes.
3411 unsigned Val = ValC->getValue() & 255;
3412 unsigned Offset = I->getValue() - BytesLeft;
3413 SDOperand DstAddr = Op.getOperand(1);
3414 MVT::ValueType AddrVT = DstAddr.getValueType();
3415 if (BytesLeft >= 2) {
3416 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3417 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3418 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3419 DAG.getConstant(Offset, AddrVT)),
3420 DAG.getSrcValue(NULL));
3425 if (BytesLeft == 1) {
3426 Value = DAG.getConstant(Val, MVT::i8);
3427 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3428 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3429 DAG.getConstant(Offset, AddrVT)),
3430 DAG.getSrcValue(NULL));
3437 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3438 SDOperand Chain = Op.getOperand(0);
3440 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3441 if (Align == 0) Align = 1;
3443 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3444 // If not DWORD aligned, call memcpy if size is less than the threshold.
3445 // It knows how to align to the right boundary first.
3446 if ((Align & 3) != 0 ||
3447 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3448 MVT::ValueType IntPtr = getPointerTy();
3449 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3450 std::vector<std::pair<SDOperand, const Type*> > Args;
3451 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3452 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3453 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3454 std::pair<SDOperand,SDOperand> CallResult =
3455 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3456 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3457 return CallResult.second;
3462 unsigned BytesLeft = 0;
3463 bool TwoRepMovs = false;
3464 switch (Align & 3) {
3465 case 2: // WORD aligned
3467 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3468 BytesLeft = I->getValue() % 2;
3470 case 0: // DWORD aligned
3473 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3474 BytesLeft = I->getValue() % 4;
3476 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3477 DAG.getConstant(2, MVT::i8));
3481 default: // Byte aligned
3483 Count = Op.getOperand(3);
3487 SDOperand InFlag(0, 0);
3488 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3489 InFlag = Chain.getValue(1);
3490 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3491 InFlag = Chain.getValue(1);
3492 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3493 InFlag = Chain.getValue(1);
3495 std::vector<MVT::ValueType> Tys;
3496 Tys.push_back(MVT::Other);
3497 Tys.push_back(MVT::Flag);
3498 std::vector<SDOperand> Ops;
3499 Ops.push_back(Chain);
3500 Ops.push_back(DAG.getValueType(AVT));
3501 Ops.push_back(InFlag);
3502 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3505 InFlag = Chain.getValue(1);
3506 Count = Op.getOperand(3);
3507 MVT::ValueType CVT = Count.getValueType();
3508 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3509 DAG.getConstant(3, CVT));
3510 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3511 InFlag = Chain.getValue(1);
3513 Tys.push_back(MVT::Other);
3514 Tys.push_back(MVT::Flag);
3516 Ops.push_back(Chain);
3517 Ops.push_back(DAG.getValueType(MVT::i8));
3518 Ops.push_back(InFlag);
3519 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3520 } else if (BytesLeft) {
3521 // Issue loads and stores for the last 1 - 3 bytes.
3522 unsigned Offset = I->getValue() - BytesLeft;
3523 SDOperand DstAddr = Op.getOperand(1);
3524 MVT::ValueType DstVT = DstAddr.getValueType();
3525 SDOperand SrcAddr = Op.getOperand(2);
3526 MVT::ValueType SrcVT = SrcAddr.getValueType();
3528 if (BytesLeft >= 2) {
3529 Value = DAG.getLoad(MVT::i16, Chain,
3530 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3531 DAG.getConstant(Offset, SrcVT)),
3532 DAG.getSrcValue(NULL));
3533 Chain = Value.getValue(1);
3534 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3535 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3536 DAG.getConstant(Offset, DstVT)),
3537 DAG.getSrcValue(NULL));
3542 if (BytesLeft == 1) {
3543 Value = DAG.getLoad(MVT::i8, Chain,
3544 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3545 DAG.getConstant(Offset, SrcVT)),
3546 DAG.getSrcValue(NULL));
3547 Chain = Value.getValue(1);
3548 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3549 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3550 DAG.getConstant(Offset, DstVT)),
3551 DAG.getSrcValue(NULL));
3559 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3560 std::vector<MVT::ValueType> Tys;
3561 Tys.push_back(MVT::Other);
3562 Tys.push_back(MVT::Flag);
3563 std::vector<SDOperand> Ops;
3564 Ops.push_back(Op.getOperand(0));
3565 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3567 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3568 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3569 MVT::i32, Ops[0].getValue(2)));
3570 Ops.push_back(Ops[1].getValue(1));
3571 Tys[0] = Tys[1] = MVT::i32;
3572 Tys.push_back(MVT::Other);
3573 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3576 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3577 // vastart just stores the address of the VarArgsFrameIndex slot into the
3578 // memory location argument.
3579 // FIXME: Replace MVT::i32 with PointerTy
3580 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3581 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3582 Op.getOperand(1), Op.getOperand(2));
3586 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3587 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3589 default: return SDOperand(); // Don't custom lower most intrinsics.
3590 // Comparison intrinsics.
3591 case Intrinsic::x86_sse_comieq_ss:
3592 case Intrinsic::x86_sse_comilt_ss:
3593 case Intrinsic::x86_sse_comile_ss:
3594 case Intrinsic::x86_sse_comigt_ss:
3595 case Intrinsic::x86_sse_comige_ss:
3596 case Intrinsic::x86_sse_comineq_ss:
3597 case Intrinsic::x86_sse_ucomieq_ss:
3598 case Intrinsic::x86_sse_ucomilt_ss:
3599 case Intrinsic::x86_sse_ucomile_ss:
3600 case Intrinsic::x86_sse_ucomigt_ss:
3601 case Intrinsic::x86_sse_ucomige_ss:
3602 case Intrinsic::x86_sse_ucomineq_ss:
3603 case Intrinsic::x86_sse2_comieq_sd:
3604 case Intrinsic::x86_sse2_comilt_sd:
3605 case Intrinsic::x86_sse2_comile_sd:
3606 case Intrinsic::x86_sse2_comigt_sd:
3607 case Intrinsic::x86_sse2_comige_sd:
3608 case Intrinsic::x86_sse2_comineq_sd:
3609 case Intrinsic::x86_sse2_ucomieq_sd:
3610 case Intrinsic::x86_sse2_ucomilt_sd:
3611 case Intrinsic::x86_sse2_ucomile_sd:
3612 case Intrinsic::x86_sse2_ucomigt_sd:
3613 case Intrinsic::x86_sse2_ucomige_sd:
3614 case Intrinsic::x86_sse2_ucomineq_sd: {
3616 ISD::CondCode CC = ISD::SETCC_INVALID;
3619 case Intrinsic::x86_sse_comieq_ss:
3620 case Intrinsic::x86_sse2_comieq_sd:
3624 case Intrinsic::x86_sse_comilt_ss:
3625 case Intrinsic::x86_sse2_comilt_sd:
3629 case Intrinsic::x86_sse_comile_ss:
3630 case Intrinsic::x86_sse2_comile_sd:
3634 case Intrinsic::x86_sse_comigt_ss:
3635 case Intrinsic::x86_sse2_comigt_sd:
3639 case Intrinsic::x86_sse_comige_ss:
3640 case Intrinsic::x86_sse2_comige_sd:
3644 case Intrinsic::x86_sse_comineq_ss:
3645 case Intrinsic::x86_sse2_comineq_sd:
3649 case Intrinsic::x86_sse_ucomieq_ss:
3650 case Intrinsic::x86_sse2_ucomieq_sd:
3651 Opc = X86ISD::UCOMI;
3654 case Intrinsic::x86_sse_ucomilt_ss:
3655 case Intrinsic::x86_sse2_ucomilt_sd:
3656 Opc = X86ISD::UCOMI;
3659 case Intrinsic::x86_sse_ucomile_ss:
3660 case Intrinsic::x86_sse2_ucomile_sd:
3661 Opc = X86ISD::UCOMI;
3664 case Intrinsic::x86_sse_ucomigt_ss:
3665 case Intrinsic::x86_sse2_ucomigt_sd:
3666 Opc = X86ISD::UCOMI;
3669 case Intrinsic::x86_sse_ucomige_ss:
3670 case Intrinsic::x86_sse2_ucomige_sd:
3671 Opc = X86ISD::UCOMI;
3674 case Intrinsic::x86_sse_ucomineq_ss:
3675 case Intrinsic::x86_sse2_ucomineq_sd:
3676 Opc = X86ISD::UCOMI;
3682 translateX86CC(CC, true, X86CC, Flip);
3683 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3684 Op.getOperand(Flip?1:2));
3685 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3686 DAG.getConstant(X86CC, MVT::i8), Cond);
3687 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
3692 /// LowerOperation - Provide custom lowering hooks for some operations.
3694 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3695 switch (Op.getOpcode()) {
3696 default: assert(0 && "Should not custom lower this!");
3697 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3698 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3699 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3700 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3701 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3702 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3703 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3704 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3705 case ISD::SHL_PARTS:
3706 case ISD::SRA_PARTS:
3707 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3708 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3709 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3710 case ISD::FABS: return LowerFABS(Op, DAG);
3711 case ISD::FNEG: return LowerFNEG(Op, DAG);
3712 case ISD::SETCC: return LowerSETCC(Op, DAG);
3713 case ISD::SELECT: return LowerSELECT(Op, DAG);
3714 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3715 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3716 case ISD::CALL: return LowerCALL(Op, DAG);
3717 case ISD::RET: return LowerRET(Op, DAG);
3718 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
3719 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3720 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3721 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3722 case ISD::VASTART: return LowerVASTART(Op, DAG);
3723 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3727 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3729 default: return NULL;
3730 case X86ISD::SHLD: return "X86ISD::SHLD";
3731 case X86ISD::SHRD: return "X86ISD::SHRD";
3732 case X86ISD::FAND: return "X86ISD::FAND";
3733 case X86ISD::FXOR: return "X86ISD::FXOR";
3734 case X86ISD::FILD: return "X86ISD::FILD";
3735 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
3736 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3737 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3738 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
3739 case X86ISD::FLD: return "X86ISD::FLD";
3740 case X86ISD::FST: return "X86ISD::FST";
3741 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
3742 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
3743 case X86ISD::CALL: return "X86ISD::CALL";
3744 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3745 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3746 case X86ISD::CMP: return "X86ISD::CMP";
3747 case X86ISD::TEST: return "X86ISD::TEST";
3748 case X86ISD::COMI: return "X86ISD::COMI";
3749 case X86ISD::UCOMI: return "X86ISD::UCOMI";
3750 case X86ISD::SETCC: return "X86ISD::SETCC";
3751 case X86ISD::CMOV: return "X86ISD::CMOV";
3752 case X86ISD::BRCOND: return "X86ISD::BRCOND";
3753 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
3754 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3755 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
3756 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
3757 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
3758 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
3759 case X86ISD::Wrapper: return "X86ISD::Wrapper";
3760 case X86ISD::S2VEC: return "X86ISD::S2VEC";
3761 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
3762 case X86ISD::PINSRW: return "X86ISD::PINSRW";
3766 /// isLegalAddressImmediate - Return true if the integer value or
3767 /// GlobalValue can be used as the offset of the target addressing mode.
3768 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3769 // X86 allows a sign-extended 32-bit immediate field.
3770 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3773 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
3774 // GV is 64-bit but displacement field is 32-bit unless we are in small code
3775 // model. Mac OS X happens to support only small PIC code model.
3776 // FIXME: better support for other OS's.
3777 if (Subtarget->is64Bit() && !Subtarget->isTargetDarwin())
3779 if (Subtarget->isTargetDarwin()) {
3780 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3781 if (RModel == Reloc::Static)
3783 else if (RModel == Reloc::DynamicNoPIC)
3784 return !DarwinGVRequiresExtraLoad(GV);
3791 /// isShuffleMaskLegal - Targets can use this to indicate that they only
3792 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3793 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3794 /// are assumed to be legal.
3796 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3797 // Only do shuffles on 128-bit vector types for now.
3798 if (MVT::getSizeInBits(VT) == 64) return false;
3799 return (Mask.Val->getNumOperands() <= 4 ||
3800 isSplatMask(Mask.Val) ||
3801 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
3802 X86::isUNPCKLMask(Mask.Val) ||
3803 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
3804 X86::isUNPCKHMask(Mask.Val));
3807 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3809 SelectionDAG &DAG) const {
3810 unsigned NumElts = BVOps.size();
3811 // Only do shuffles on 128-bit vector types for now.
3812 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
3813 if (NumElts == 2) return true;
3815 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
3816 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
3821 //===----------------------------------------------------------------------===//
3822 // X86 Scheduler Hooks
3823 //===----------------------------------------------------------------------===//
3826 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
3827 MachineBasicBlock *BB) {
3828 switch (MI->getOpcode()) {
3829 default: assert(false && "Unexpected instr type to insert");
3830 case X86::CMOV_FR32:
3831 case X86::CMOV_FR64:
3832 case X86::CMOV_V4F32:
3833 case X86::CMOV_V2F64:
3834 case X86::CMOV_V2I64: {
3835 // To "insert" a SELECT_CC instruction, we actually have to insert the
3836 // diamond control-flow pattern. The incoming instruction knows the
3837 // destination vreg to set, the condition code register to branch on, the
3838 // true/false values to select between, and a branch opcode to use.
3839 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3840 ilist<MachineBasicBlock>::iterator It = BB;
3846 // cmpTY ccX, r1, r2
3848 // fallthrough --> copy0MBB
3849 MachineBasicBlock *thisMBB = BB;
3850 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
3851 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
3852 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
3853 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
3854 MachineFunction *F = BB->getParent();
3855 F->getBasicBlockList().insert(It, copy0MBB);
3856 F->getBasicBlockList().insert(It, sinkMBB);
3857 // Update machine-CFG edges by first adding all successors of the current
3858 // block to the new block which will contain the Phi node for the select.
3859 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
3860 e = BB->succ_end(); i != e; ++i)
3861 sinkMBB->addSuccessor(*i);
3862 // Next, remove all successors of the current block, and add the true
3863 // and fallthrough blocks as its successors.
3864 while(!BB->succ_empty())
3865 BB->removeSuccessor(BB->succ_begin());
3866 BB->addSuccessor(copy0MBB);
3867 BB->addSuccessor(sinkMBB);
3870 // %FalseValue = ...
3871 // # fallthrough to sinkMBB
3874 // Update machine-CFG edges
3875 BB->addSuccessor(sinkMBB);
3878 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3881 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
3882 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3883 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3885 delete MI; // The pseudo instruction is gone now.
3889 case X86::FP_TO_INT16_IN_MEM:
3890 case X86::FP_TO_INT32_IN_MEM:
3891 case X86::FP_TO_INT64_IN_MEM: {
3892 // Change the floating point control register to use "round towards zero"
3893 // mode when truncating to an integer value.
3894 MachineFunction *F = BB->getParent();
3895 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
3896 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
3898 // Load the old value of the high byte of the control word...
3900 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
3901 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
3903 // Set the high part to be round to zero...
3904 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
3906 // Reload the modified control word now...
3907 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
3909 // Restore the memory image of control word to original value
3910 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
3912 // Get the X86 opcode to use.
3914 switch (MI->getOpcode()) {
3915 default: assert(0 && "illegal opcode!");
3916 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
3917 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
3918 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
3922 MachineOperand &Op = MI->getOperand(0);
3923 if (Op.isRegister()) {
3924 AM.BaseType = X86AddressMode::RegBase;
3925 AM.Base.Reg = Op.getReg();
3927 AM.BaseType = X86AddressMode::FrameIndexBase;
3928 AM.Base.FrameIndex = Op.getFrameIndex();
3930 Op = MI->getOperand(1);
3931 if (Op.isImmediate())
3932 AM.Scale = Op.getImmedValue();
3933 Op = MI->getOperand(2);
3934 if (Op.isImmediate())
3935 AM.IndexReg = Op.getImmedValue();
3936 Op = MI->getOperand(3);
3937 if (Op.isGlobalAddress()) {
3938 AM.GV = Op.getGlobal();
3940 AM.Disp = Op.getImmedValue();
3942 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
3944 // Reload the original control word now.
3945 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
3947 delete MI; // The pseudo instruction is gone now.
3953 //===----------------------------------------------------------------------===//
3954 // X86 Optimization Hooks
3955 //===----------------------------------------------------------------------===//
3957 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3959 uint64_t &KnownZero,
3961 unsigned Depth) const {
3962 unsigned Opc = Op.getOpcode();
3963 assert((Opc >= ISD::BUILTIN_OP_END ||
3964 Opc == ISD::INTRINSIC_WO_CHAIN ||
3965 Opc == ISD::INTRINSIC_W_CHAIN ||
3966 Opc == ISD::INTRINSIC_VOID) &&
3967 "Should use MaskedValueIsZero if you don't know whether Op"
3968 " is a target node!");
3970 KnownZero = KnownOne = 0; // Don't know anything.
3974 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3979 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
3980 /// element of the result of the vector shuffle.
3981 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
3982 MVT::ValueType VT = N->getValueType(0);
3983 SDOperand PermMask = N->getOperand(2);
3984 unsigned NumElems = PermMask.getNumOperands();
3985 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
3987 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3989 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
3990 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
3991 SDOperand Idx = PermMask.getOperand(i);
3992 if (Idx.getOpcode() == ISD::UNDEF)
3993 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
3994 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
3999 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4000 /// node is a GlobalAddress + an offset.
4001 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4002 if (N->getOpcode() == X86ISD::Wrapper) {
4003 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4004 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4007 } else if (N->getOpcode() == ISD::ADD) {
4008 SDOperand N1 = N->getOperand(0);
4009 SDOperand N2 = N->getOperand(1);
4010 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4011 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4013 Offset += V->getSignExtended();
4016 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4017 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4019 Offset += V->getSignExtended();
4027 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4029 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4030 MachineFrameInfo *MFI) {
4031 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4034 SDOperand Loc = N->getOperand(1);
4035 SDOperand BaseLoc = Base->getOperand(1);
4036 if (Loc.getOpcode() == ISD::FrameIndex) {
4037 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4039 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4040 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4041 int FS = MFI->getObjectSize(FI);
4042 int BFS = MFI->getObjectSize(BFI);
4043 if (FS != BFS || FS != Size) return false;
4044 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4046 GlobalValue *GV1 = NULL;
4047 GlobalValue *GV2 = NULL;
4048 int64_t Offset1 = 0;
4049 int64_t Offset2 = 0;
4050 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4051 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4052 if (isGA1 && isGA2 && GV1 == GV2)
4053 return Offset1 == (Offset2 + Dist*Size);
4059 bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI) {
4062 if (isGAPlusOffset(Base, GV, Offset))
4063 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4065 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4066 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
4067 return MFI->getObjectAlignment(BFI) >= 16;
4073 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4074 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4075 /// if the load addresses are consecutive, non-overlapping, and in the right
4077 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG) {
4078 MachineFunction &MF = DAG.getMachineFunction();
4079 MachineFrameInfo *MFI = MF.getFrameInfo();
4080 MVT::ValueType VT = N->getValueType(0);
4081 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4082 SDOperand PermMask = N->getOperand(2);
4083 int NumElems = (int)PermMask.getNumOperands();
4084 SDNode *Base = NULL;
4085 for (int i = 0; i < NumElems; ++i) {
4086 SDOperand Idx = PermMask.getOperand(i);
4087 if (Idx.getOpcode() == ISD::UNDEF) {
4088 if (!Base) return SDOperand();
4091 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4092 if (!Arg.Val || Arg.getOpcode() != ISD::LOAD)
4096 else if (!isConsecutiveLoad(Arg.Val, Base,
4097 i, MVT::getSizeInBits(EVT)/8,MFI))
4102 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI);
4104 return DAG.getLoad(VT, Base->getOperand(0), Base->getOperand(1),
4105 Base->getOperand(2));
4107 // Just use movups, it's shorter.
4108 return DAG.getNode(ISD::BIT_CONVERT, VT,
4109 DAG.getNode(X86ISD::LOAD_UA, MVT::v4f32,
4110 Base->getOperand(0), Base->getOperand(1),
4111 Base->getOperand(2)));
4114 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4115 DAGCombinerInfo &DCI) const {
4116 TargetMachine &TM = getTargetMachine();
4117 SelectionDAG &DAG = DCI.DAG;
4118 switch (N->getOpcode()) {
4120 case ISD::VECTOR_SHUFFLE:
4121 return PerformShuffleCombine(N, DAG);
4127 //===----------------------------------------------------------------------===//
4128 // X86 Inline Assembly Support
4129 //===----------------------------------------------------------------------===//
4131 std::vector<unsigned> X86TargetLowering::
4132 getRegClassForInlineAsmConstraint(const std::string &Constraint,
4133 MVT::ValueType VT) const {
4134 if (Constraint.size() == 1) {
4135 // FIXME: not handling fp-stack yet!
4136 // FIXME: not handling MMX registers yet ('y' constraint).
4137 switch (Constraint[0]) { // GCC X86 Constraint Letters
4138 default: break; // Unknown constriant letter
4139 case 'r': // GENERAL_REGS
4140 case 'R': // LEGACY_REGS
4142 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4143 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4144 else if (VT == MVT::i16)
4145 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4146 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4147 else if (VT == MVT::i8)
4148 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4150 case 'l': // INDEX_REGS
4152 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4153 X86::ESI, X86::EDI, X86::EBP, 0);
4154 else if (VT == MVT::i16)
4155 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4156 X86::SI, X86::DI, X86::BP, 0);
4157 else if (VT == MVT::i8)
4158 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4160 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4163 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4164 else if (VT == MVT::i16)
4165 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4166 else if (VT == MVT::i8)
4167 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4169 case 'x': // SSE_REGS if SSE1 allowed
4170 if (Subtarget->hasSSE1())
4171 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4172 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4174 return std::vector<unsigned>();
4175 case 'Y': // SSE_REGS if SSE2 allowed
4176 if (Subtarget->hasSSE2())
4177 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4178 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4180 return std::vector<unsigned>();
4184 return std::vector<unsigned>();