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[0],ArgValues.size());
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,
600 &MemOpChains[0], MemOpChains.size());
602 // Build a sequence of copy-to-reg nodes chained together with token chain
603 // and flag operands which copy the outgoing args into registers.
605 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
606 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
608 InFlag = Chain.getValue(1);
611 // If the callee is a GlobalAddress node (quite common, every direct call is)
612 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
613 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
614 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
615 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
616 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
618 std::vector<MVT::ValueType> NodeTys;
619 NodeTys.push_back(MVT::Other); // Returns a chain
620 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
621 std::vector<SDOperand> Ops;
622 Ops.push_back(Chain);
623 Ops.push_back(Callee);
625 // Add argument registers to the end of the list so that they are known live
627 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
628 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
629 RegsToPass[i].second.getValueType()));
632 Ops.push_back(InFlag);
634 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
635 NodeTys, &Ops[0], Ops.size());
636 InFlag = Chain.getValue(1);
638 // Create the CALLSEQ_END node.
639 unsigned NumBytesForCalleeToPush = 0;
641 // If this is is a call to a struct-return function on Darwin/X86, the callee
642 // pops the hidden struct pointer, so we have to push it back.
643 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
644 NumBytesForCalleeToPush = 4;
647 NodeTys.push_back(MVT::Other); // Returns a chain
648 if (RetVT != MVT::Other)
649 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
651 Ops.push_back(Chain);
652 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
653 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
654 Ops.push_back(InFlag);
655 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
656 if (RetVT != MVT::Other)
657 InFlag = Chain.getValue(1);
659 std::vector<SDOperand> ResultVals;
662 default: assert(0 && "Unknown value type to return!");
663 case MVT::Other: break;
665 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
666 ResultVals.push_back(Chain.getValue(0));
667 NodeTys.push_back(MVT::i8);
670 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
671 ResultVals.push_back(Chain.getValue(0));
672 NodeTys.push_back(MVT::i16);
675 if (Op.Val->getValueType(1) == MVT::i32) {
676 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
677 ResultVals.push_back(Chain.getValue(0));
678 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
679 Chain.getValue(2)).getValue(1);
680 ResultVals.push_back(Chain.getValue(0));
681 NodeTys.push_back(MVT::i32);
683 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
684 ResultVals.push_back(Chain.getValue(0));
686 NodeTys.push_back(MVT::i32);
694 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
695 ResultVals.push_back(Chain.getValue(0));
696 NodeTys.push_back(RetVT);
700 std::vector<MVT::ValueType> Tys;
701 Tys.push_back(MVT::f64);
702 Tys.push_back(MVT::Other);
703 Tys.push_back(MVT::Flag);
704 std::vector<SDOperand> Ops;
705 Ops.push_back(Chain);
706 Ops.push_back(InFlag);
707 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
708 &Ops[0], Ops.size());
709 Chain = RetVal.getValue(1);
710 InFlag = RetVal.getValue(2);
712 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
713 // shouldn't be necessary except that RFP cannot be live across
714 // multiple blocks. When stackifier is fixed, they can be uncoupled.
715 MachineFunction &MF = DAG.getMachineFunction();
716 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
717 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
719 Tys.push_back(MVT::Other);
721 Ops.push_back(Chain);
722 Ops.push_back(RetVal);
723 Ops.push_back(StackSlot);
724 Ops.push_back(DAG.getValueType(RetVT));
725 Ops.push_back(InFlag);
726 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
727 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
728 DAG.getSrcValue(NULL));
729 Chain = RetVal.getValue(1);
732 if (RetVT == MVT::f32 && !X86ScalarSSE)
733 // FIXME: we would really like to remember that this FP_ROUND
734 // operation is okay to eliminate if we allow excess FP precision.
735 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
736 ResultVals.push_back(RetVal);
737 NodeTys.push_back(RetVT);
742 // If the function returns void, just return the chain.
743 if (ResultVals.empty())
746 // Otherwise, merge everything together with a MERGE_VALUES node.
747 NodeTys.push_back(MVT::Other);
748 ResultVals.push_back(Chain);
749 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
750 &ResultVals[0], ResultVals.size());
751 return Res.getValue(Op.ResNo);
754 //===----------------------------------------------------------------------===//
755 // Fast Calling Convention implementation
756 //===----------------------------------------------------------------------===//
758 // The X86 'fast' calling convention passes up to two integer arguments in
759 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
760 // and requires that the callee pop its arguments off the stack (allowing proper
761 // tail calls), and has the same return value conventions as C calling convs.
763 // This calling convention always arranges for the callee pop value to be 8n+4
764 // bytes, which is needed for tail recursion elimination and stack alignment
767 // Note that this can be enhanced in the future to pass fp vals in registers
768 // (when we have a global fp allocator) and do other tricks.
771 /// HowToPassFastCCArgument - Returns how an formal argument of the specified
772 /// type should be passed. If it is through stack, returns the size of the stack
773 /// slot; if it is through integer or XMM register, returns the number of
774 /// integer or XMM registers are needed.
776 HowToPassFastCCArgument(MVT::ValueType ObjectVT,
777 unsigned NumIntRegs, unsigned NumXMMRegs,
778 unsigned &ObjSize, unsigned &ObjIntRegs,
779 unsigned &ObjXMMRegs) {
785 default: assert(0 && "Unhandled argument type!");
787 #if FASTCC_NUM_INT_ARGS_INREGS > 0
788 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
795 #if FASTCC_NUM_INT_ARGS_INREGS > 0
796 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
803 #if FASTCC_NUM_INT_ARGS_INREGS > 0
804 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
811 #if FASTCC_NUM_INT_ARGS_INREGS > 0
812 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
814 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
841 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
842 unsigned NumArgs = Op.Val->getNumValues()-1;
843 MachineFunction &MF = DAG.getMachineFunction();
844 MachineFrameInfo *MFI = MF.getFrameInfo();
845 SDOperand Root = Op.getOperand(0);
846 std::vector<SDOperand> ArgValues;
848 // Add DAG nodes to load the arguments... On entry to a function the stack
849 // frame looks like this:
851 // [ESP] -- return address
852 // [ESP + 4] -- first nonreg argument (leftmost lexically)
853 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
855 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
857 // Keep track of the number of integer regs passed so far. This can be either
858 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
860 unsigned NumIntRegs = 0;
861 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
863 static const unsigned XMMArgRegs[] = {
864 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
867 for (unsigned i = 0; i < NumArgs; ++i) {
868 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
869 unsigned ArgIncrement = 4;
870 unsigned ObjSize = 0;
871 unsigned ObjIntRegs = 0;
872 unsigned ObjXMMRegs = 0;
874 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
875 ObjSize, ObjIntRegs, ObjXMMRegs);
877 ArgIncrement = ObjSize;
881 if (ObjIntRegs || ObjXMMRegs) {
883 default: assert(0 && "Unhandled argument type!");
885 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
886 X86::GR8RegisterClass);
887 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
890 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
891 X86::GR16RegisterClass);
892 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
895 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
896 X86::GR32RegisterClass);
897 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
900 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
901 X86::GR32RegisterClass);
902 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
903 if (ObjIntRegs == 2) {
904 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
905 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
906 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
915 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
916 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
919 NumIntRegs += ObjIntRegs;
920 NumXMMRegs += ObjXMMRegs;
924 // XMM arguments have to be aligned on 16-byte boundary.
926 ArgOffset = ((ArgOffset + 15) / 16) * 16;
927 // Create the SelectionDAG nodes corresponding to a load from this
929 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
930 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
931 if (ObjectVT == MVT::i64 && ObjIntRegs) {
932 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
933 DAG.getSrcValue(NULL));
934 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
936 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
937 DAG.getSrcValue(NULL));
938 ArgOffset += ArgIncrement; // Move on to the next argument.
941 ArgValues.push_back(ArgValue);
944 ArgValues.push_back(Root);
946 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
947 // arguments and the arguments after the retaddr has been pushed are aligned.
948 if ((ArgOffset & 7) == 0)
951 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
952 ReturnAddrIndex = 0; // No return address slot generated yet.
953 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
954 BytesCallerReserves = 0;
956 // Finally, inform the code generator which regs we return values in.
957 switch (getValueType(MF.getFunction()->getReturnType())) {
958 default: assert(0 && "Unknown type!");
959 case MVT::isVoid: break;
963 MF.addLiveOut(X86::EAX);
966 MF.addLiveOut(X86::EAX);
967 MF.addLiveOut(X86::EDX);
971 MF.addLiveOut(X86::ST0);
979 MF.addLiveOut(X86::XMM0);
983 // Return the new list of results.
984 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
985 Op.Val->value_end());
986 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
989 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG){
990 SDOperand Chain = Op.getOperand(0);
991 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
992 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
993 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
994 SDOperand Callee = Op.getOperand(4);
995 MVT::ValueType RetVT= Op.Val->getValueType(0);
996 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
998 // Count how many bytes are to be pushed on the stack.
999 unsigned NumBytes = 0;
1001 // Keep track of the number of integer regs passed so far. This can be either
1002 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1004 unsigned NumIntRegs = 0;
1005 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1007 static const unsigned GPRArgRegs[][2] = {
1008 { X86::AL, X86::DL },
1009 { X86::AX, X86::DX },
1010 { X86::EAX, X86::EDX }
1012 static const unsigned XMMArgRegs[] = {
1013 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1016 for (unsigned i = 0; i != NumOps; ++i) {
1017 SDOperand Arg = Op.getOperand(5+2*i);
1019 switch (Arg.getValueType()) {
1020 default: assert(0 && "Unknown value type!");
1024 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1025 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
1046 // XMM arguments have to be aligned on 16-byte boundary.
1047 NumBytes = ((NumBytes + 15) / 16) * 16;
1054 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1055 // arguments and the arguments after the retaddr has been pushed are aligned.
1056 if ((NumBytes & 7) == 0)
1059 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1061 // Arguments go on the stack in reverse order, as specified by the ABI.
1062 unsigned ArgOffset = 0;
1064 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1065 std::vector<SDOperand> MemOpChains;
1066 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
1067 for (unsigned i = 0; i != NumOps; ++i) {
1068 SDOperand Arg = Op.getOperand(5+2*i);
1070 switch (Arg.getValueType()) {
1071 default: assert(0 && "Unexpected ValueType for argument!");
1075 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1076 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
1077 RegsToPass.push_back(
1078 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1086 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1087 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1088 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1089 Arg, PtrOff, DAG.getSrcValue(NULL)));
1094 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1095 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1096 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1097 Arg, PtrOff, DAG.getSrcValue(NULL)));
1107 if (NumXMMRegs < 4) {
1108 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1111 // XMM arguments have to be aligned on 16-byte boundary.
1112 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1113 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1114 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1115 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1116 Arg, PtrOff, DAG.getSrcValue(NULL)));
1122 if (!MemOpChains.empty())
1123 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1124 &MemOpChains[0], MemOpChains.size());
1126 // Build a sequence of copy-to-reg nodes chained together with token chain
1127 // and flag operands which copy the outgoing args into registers.
1129 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1130 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1132 InFlag = Chain.getValue(1);
1135 // If the callee is a GlobalAddress node (quite common, every direct call is)
1136 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1137 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1138 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1139 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1140 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1142 std::vector<MVT::ValueType> NodeTys;
1143 NodeTys.push_back(MVT::Other); // Returns a chain
1144 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1145 std::vector<SDOperand> Ops;
1146 Ops.push_back(Chain);
1147 Ops.push_back(Callee);
1149 // Add argument registers to the end of the list so that they are known live
1151 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1152 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1153 RegsToPass[i].second.getValueType()));
1156 Ops.push_back(InFlag);
1158 // FIXME: Do not generate X86ISD::TAILCALL for now.
1159 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1160 NodeTys, &Ops[0], Ops.size());
1161 InFlag = Chain.getValue(1);
1164 NodeTys.push_back(MVT::Other); // Returns a chain
1165 if (RetVT != MVT::Other)
1166 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1168 Ops.push_back(Chain);
1169 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1170 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1171 Ops.push_back(InFlag);
1172 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1173 if (RetVT != MVT::Other)
1174 InFlag = Chain.getValue(1);
1176 std::vector<SDOperand> ResultVals;
1179 default: assert(0 && "Unknown value type to return!");
1180 case MVT::Other: break;
1182 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1183 ResultVals.push_back(Chain.getValue(0));
1184 NodeTys.push_back(MVT::i8);
1187 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1188 ResultVals.push_back(Chain.getValue(0));
1189 NodeTys.push_back(MVT::i16);
1192 if (Op.Val->getValueType(1) == MVT::i32) {
1193 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1194 ResultVals.push_back(Chain.getValue(0));
1195 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1196 Chain.getValue(2)).getValue(1);
1197 ResultVals.push_back(Chain.getValue(0));
1198 NodeTys.push_back(MVT::i32);
1200 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1201 ResultVals.push_back(Chain.getValue(0));
1203 NodeTys.push_back(MVT::i32);
1211 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1212 ResultVals.push_back(Chain.getValue(0));
1213 NodeTys.push_back(RetVT);
1217 std::vector<MVT::ValueType> Tys;
1218 Tys.push_back(MVT::f64);
1219 Tys.push_back(MVT::Other);
1220 Tys.push_back(MVT::Flag);
1221 std::vector<SDOperand> Ops;
1222 Ops.push_back(Chain);
1223 Ops.push_back(InFlag);
1224 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
1225 &Ops[0], Ops.size());
1226 Chain = RetVal.getValue(1);
1227 InFlag = RetVal.getValue(2);
1229 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1230 // shouldn't be necessary except that RFP cannot be live across
1231 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1232 MachineFunction &MF = DAG.getMachineFunction();
1233 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1234 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1236 Tys.push_back(MVT::Other);
1238 Ops.push_back(Chain);
1239 Ops.push_back(RetVal);
1240 Ops.push_back(StackSlot);
1241 Ops.push_back(DAG.getValueType(RetVT));
1242 Ops.push_back(InFlag);
1243 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
1244 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1245 DAG.getSrcValue(NULL));
1246 Chain = RetVal.getValue(1);
1249 if (RetVT == MVT::f32 && !X86ScalarSSE)
1250 // FIXME: we would really like to remember that this FP_ROUND
1251 // operation is okay to eliminate if we allow excess FP precision.
1252 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1253 ResultVals.push_back(RetVal);
1254 NodeTys.push_back(RetVT);
1260 // If the function returns void, just return the chain.
1261 if (ResultVals.empty())
1264 // Otherwise, merge everything together with a MERGE_VALUES node.
1265 NodeTys.push_back(MVT::Other);
1266 ResultVals.push_back(Chain);
1267 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1268 &ResultVals[0], ResultVals.size());
1269 return Res.getValue(Op.ResNo);
1272 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1273 if (ReturnAddrIndex == 0) {
1274 // Set up a frame object for the return address.
1275 MachineFunction &MF = DAG.getMachineFunction();
1276 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1279 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1284 std::pair<SDOperand, SDOperand> X86TargetLowering::
1285 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1286 SelectionDAG &DAG) {
1288 if (Depth) // Depths > 0 not supported yet!
1289 Result = DAG.getConstant(0, getPointerTy());
1291 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1292 if (!isFrameAddress)
1293 // Just load the return address
1294 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1295 DAG.getSrcValue(NULL));
1297 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1298 DAG.getConstant(4, MVT::i32));
1300 return std::make_pair(Result, Chain);
1303 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1304 /// which corresponds to the condition code.
1305 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1307 default: assert(0 && "Unknown X86 conditional code!");
1308 case X86ISD::COND_A: return X86::JA;
1309 case X86ISD::COND_AE: return X86::JAE;
1310 case X86ISD::COND_B: return X86::JB;
1311 case X86ISD::COND_BE: return X86::JBE;
1312 case X86ISD::COND_E: return X86::JE;
1313 case X86ISD::COND_G: return X86::JG;
1314 case X86ISD::COND_GE: return X86::JGE;
1315 case X86ISD::COND_L: return X86::JL;
1316 case X86ISD::COND_LE: return X86::JLE;
1317 case X86ISD::COND_NE: return X86::JNE;
1318 case X86ISD::COND_NO: return X86::JNO;
1319 case X86ISD::COND_NP: return X86::JNP;
1320 case X86ISD::COND_NS: return X86::JNS;
1321 case X86ISD::COND_O: return X86::JO;
1322 case X86ISD::COND_P: return X86::JP;
1323 case X86ISD::COND_S: return X86::JS;
1327 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1328 /// specific condition code. It returns a false if it cannot do a direct
1329 /// translation. X86CC is the translated CondCode. Flip is set to true if the
1330 /// the order of comparison operands should be flipped.
1331 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1332 unsigned &X86CC, bool &Flip) {
1334 X86CC = X86ISD::COND_INVALID;
1336 switch (SetCCOpcode) {
1338 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1339 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1340 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1341 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1342 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1343 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1344 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1345 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1346 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1347 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1350 // On a floating point condition, the flags are set as follows:
1352 // 0 | 0 | 0 | X > Y
1353 // 0 | 0 | 1 | X < Y
1354 // 1 | 0 | 0 | X == Y
1355 // 1 | 1 | 1 | unordered
1356 switch (SetCCOpcode) {
1359 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1360 case ISD::SETOLT: Flip = true; // Fallthrough
1362 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
1363 case ISD::SETOLE: Flip = true; // Fallthrough
1365 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1366 case ISD::SETUGT: Flip = true; // Fallthrough
1368 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
1369 case ISD::SETUGE: Flip = true; // Fallthrough
1371 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1373 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1374 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1375 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1379 return X86CC != X86ISD::COND_INVALID;
1382 static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1384 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1387 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1388 /// code. Current x86 isa includes the following FP cmov instructions:
1389 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1390 static bool hasFPCMov(unsigned X86CC) {
1394 case X86ISD::COND_B:
1395 case X86ISD::COND_BE:
1396 case X86ISD::COND_E:
1397 case X86ISD::COND_P:
1398 case X86ISD::COND_A:
1399 case X86ISD::COND_AE:
1400 case X86ISD::COND_NE:
1401 case X86ISD::COND_NP:
1406 /// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1407 /// load. For Darwin, external and weak symbols are indirect, loading the value
1408 /// at address GV rather then the value of GV itself. This means that the
1409 /// GlobalAddress must be in the base or index register of the address, not the
1410 /// GV offset field.
1411 static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1412 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1413 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1416 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1417 /// true if Op is undef or if its value falls within the specified range (L, H].
1418 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1419 if (Op.getOpcode() == ISD::UNDEF)
1422 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1423 return (Val >= Low && Val < Hi);
1426 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1427 /// true if Op is undef or if its value equal to the specified value.
1428 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1429 if (Op.getOpcode() == ISD::UNDEF)
1431 return cast<ConstantSDNode>(Op)->getValue() == Val;
1434 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1435 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1436 bool X86::isPSHUFDMask(SDNode *N) {
1437 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1439 if (N->getNumOperands() != 4)
1442 // Check if the value doesn't reference the second vector.
1443 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1444 SDOperand Arg = N->getOperand(i);
1445 if (Arg.getOpcode() == ISD::UNDEF) continue;
1446 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1447 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1454 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1455 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1456 bool X86::isPSHUFHWMask(SDNode *N) {
1457 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1459 if (N->getNumOperands() != 8)
1462 // Lower quadword copied in order.
1463 for (unsigned i = 0; i != 4; ++i) {
1464 SDOperand Arg = N->getOperand(i);
1465 if (Arg.getOpcode() == ISD::UNDEF) continue;
1466 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1467 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1471 // Upper quadword shuffled.
1472 for (unsigned i = 4; i != 8; ++i) {
1473 SDOperand Arg = N->getOperand(i);
1474 if (Arg.getOpcode() == ISD::UNDEF) continue;
1475 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1476 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1477 if (Val < 4 || Val > 7)
1484 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1485 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1486 bool X86::isPSHUFLWMask(SDNode *N) {
1487 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1489 if (N->getNumOperands() != 8)
1492 // Upper quadword copied in order.
1493 for (unsigned i = 4; i != 8; ++i)
1494 if (!isUndefOrEqual(N->getOperand(i), i))
1497 // Lower quadword shuffled.
1498 for (unsigned i = 0; i != 4; ++i)
1499 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1505 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1506 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1507 static bool isSHUFPMask(std::vector<SDOperand> &N) {
1508 unsigned NumElems = N.size();
1509 if (NumElems != 2 && NumElems != 4) return false;
1511 unsigned Half = NumElems / 2;
1512 for (unsigned i = 0; i < Half; ++i)
1513 if (!isUndefOrInRange(N[i], 0, NumElems))
1515 for (unsigned i = Half; i < NumElems; ++i)
1516 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1522 bool X86::isSHUFPMask(SDNode *N) {
1523 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1524 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1525 return ::isSHUFPMask(Ops);
1528 /// isCommutedSHUFP - Returns true if the shuffle mask is except
1529 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1530 /// half elements to come from vector 1 (which would equal the dest.) and
1531 /// the upper half to come from vector 2.
1532 static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1533 unsigned NumElems = Ops.size();
1534 if (NumElems != 2 && NumElems != 4) return false;
1536 unsigned Half = NumElems / 2;
1537 for (unsigned i = 0; i < Half; ++i)
1538 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1540 for (unsigned i = Half; i < NumElems; ++i)
1541 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1546 static bool isCommutedSHUFP(SDNode *N) {
1547 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1548 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1549 return isCommutedSHUFP(Ops);
1552 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1553 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1554 bool X86::isMOVHLPSMask(SDNode *N) {
1555 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1557 if (N->getNumOperands() != 4)
1560 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1561 return isUndefOrEqual(N->getOperand(0), 6) &&
1562 isUndefOrEqual(N->getOperand(1), 7) &&
1563 isUndefOrEqual(N->getOperand(2), 2) &&
1564 isUndefOrEqual(N->getOperand(3), 3);
1567 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1568 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1569 bool X86::isMOVLPMask(SDNode *N) {
1570 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1572 unsigned NumElems = N->getNumOperands();
1573 if (NumElems != 2 && NumElems != 4)
1576 for (unsigned i = 0; i < NumElems/2; ++i)
1577 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1580 for (unsigned i = NumElems/2; i < NumElems; ++i)
1581 if (!isUndefOrEqual(N->getOperand(i), i))
1587 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1588 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1590 bool X86::isMOVHPMask(SDNode *N) {
1591 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1593 unsigned NumElems = N->getNumOperands();
1594 if (NumElems != 2 && NumElems != 4)
1597 for (unsigned i = 0; i < NumElems/2; ++i)
1598 if (!isUndefOrEqual(N->getOperand(i), i))
1601 for (unsigned i = 0; i < NumElems/2; ++i) {
1602 SDOperand Arg = N->getOperand(i + NumElems/2);
1603 if (!isUndefOrEqual(Arg, i + NumElems))
1610 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1611 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1612 bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1613 unsigned NumElems = N.size();
1614 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1617 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1618 SDOperand BitI = N[i];
1619 SDOperand BitI1 = N[i+1];
1620 if (!isUndefOrEqual(BitI, j))
1623 if (isUndefOrEqual(BitI1, NumElems))
1626 if (!isUndefOrEqual(BitI1, j + NumElems))
1634 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1635 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1636 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1637 return ::isUNPCKLMask(Ops, V2IsSplat);
1640 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1641 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1642 bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1643 unsigned NumElems = N.size();
1644 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1647 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1648 SDOperand BitI = N[i];
1649 SDOperand BitI1 = N[i+1];
1650 if (!isUndefOrEqual(BitI, j + NumElems/2))
1653 if (isUndefOrEqual(BitI1, NumElems))
1656 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1664 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1665 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1666 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1667 return ::isUNPCKHMask(Ops, V2IsSplat);
1670 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1671 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1673 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1674 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1676 unsigned NumElems = N->getNumOperands();
1677 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1680 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1681 SDOperand BitI = N->getOperand(i);
1682 SDOperand BitI1 = N->getOperand(i+1);
1684 if (!isUndefOrEqual(BitI, j))
1686 if (!isUndefOrEqual(BitI1, j))
1693 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1694 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1695 /// MOVSD, and MOVD, i.e. setting the lowest element.
1696 static bool isMOVLMask(std::vector<SDOperand> &N) {
1697 unsigned NumElems = N.size();
1698 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1701 if (!isUndefOrEqual(N[0], NumElems))
1704 for (unsigned i = 1; i < NumElems; ++i) {
1705 SDOperand Arg = N[i];
1706 if (!isUndefOrEqual(Arg, i))
1713 bool X86::isMOVLMask(SDNode *N) {
1714 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1715 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1716 return ::isMOVLMask(Ops);
1719 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1720 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1721 /// element of vector 2 and the other elements to come from vector 1 in order.
1722 static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
1723 unsigned NumElems = Ops.size();
1724 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1727 if (!isUndefOrEqual(Ops[0], 0))
1730 for (unsigned i = 1; i < NumElems; ++i) {
1731 SDOperand Arg = Ops[i];
1733 if (!isUndefOrEqual(Arg, NumElems))
1736 if (!isUndefOrEqual(Arg, i+NumElems))
1744 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
1745 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1746 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1747 return isCommutedMOVL(Ops, V2IsSplat);
1750 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1751 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1752 bool X86::isMOVSHDUPMask(SDNode *N) {
1753 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1755 if (N->getNumOperands() != 4)
1758 // Expect 1, 1, 3, 3
1759 for (unsigned i = 0; i < 2; ++i) {
1760 SDOperand Arg = N->getOperand(i);
1761 if (Arg.getOpcode() == ISD::UNDEF) continue;
1762 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1763 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1764 if (Val != 1) return false;
1768 for (unsigned i = 2; i < 4; ++i) {
1769 SDOperand Arg = N->getOperand(i);
1770 if (Arg.getOpcode() == ISD::UNDEF) continue;
1771 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1772 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1773 if (Val != 3) return false;
1777 // Don't use movshdup if it can be done with a shufps.
1781 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1782 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1783 bool X86::isMOVSLDUPMask(SDNode *N) {
1784 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1786 if (N->getNumOperands() != 4)
1789 // Expect 0, 0, 2, 2
1790 for (unsigned i = 0; i < 2; ++i) {
1791 SDOperand Arg = N->getOperand(i);
1792 if (Arg.getOpcode() == ISD::UNDEF) continue;
1793 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1794 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1795 if (Val != 0) return false;
1799 for (unsigned i = 2; i < 4; ++i) {
1800 SDOperand Arg = N->getOperand(i);
1801 if (Arg.getOpcode() == ISD::UNDEF) continue;
1802 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1803 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1804 if (Val != 2) return false;
1808 // Don't use movshdup if it can be done with a shufps.
1812 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1813 /// a splat of a single element.
1814 static bool isSplatMask(SDNode *N) {
1815 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1817 // This is a splat operation if each element of the permute is the same, and
1818 // if the value doesn't reference the second vector.
1819 unsigned NumElems = N->getNumOperands();
1820 SDOperand ElementBase;
1822 for (; i != NumElems; ++i) {
1823 SDOperand Elt = N->getOperand(i);
1824 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1830 if (!ElementBase.Val)
1833 for (; i != NumElems; ++i) {
1834 SDOperand Arg = N->getOperand(i);
1835 if (Arg.getOpcode() == ISD::UNDEF) continue;
1836 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1837 if (Arg != ElementBase) return false;
1840 // Make sure it is a splat of the first vector operand.
1841 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
1844 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1845 /// a splat of a single element and it's a 2 or 4 element mask.
1846 bool X86::isSplatMask(SDNode *N) {
1847 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1849 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
1850 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1852 return ::isSplatMask(N);
1855 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1856 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1858 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
1859 unsigned NumOperands = N->getNumOperands();
1860 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1862 for (unsigned i = 0; i < NumOperands; ++i) {
1864 SDOperand Arg = N->getOperand(NumOperands-i-1);
1865 if (Arg.getOpcode() != ISD::UNDEF)
1866 Val = cast<ConstantSDNode>(Arg)->getValue();
1867 if (Val >= NumOperands) Val -= NumOperands;
1869 if (i != NumOperands - 1)
1876 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1877 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1879 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1881 // 8 nodes, but we only care about the last 4.
1882 for (unsigned i = 7; i >= 4; --i) {
1884 SDOperand Arg = N->getOperand(i);
1885 if (Arg.getOpcode() != ISD::UNDEF)
1886 Val = cast<ConstantSDNode>(Arg)->getValue();
1895 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1896 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1898 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1900 // 8 nodes, but we only care about the first 4.
1901 for (int i = 3; i >= 0; --i) {
1903 SDOperand Arg = N->getOperand(i);
1904 if (Arg.getOpcode() != ISD::UNDEF)
1905 Val = cast<ConstantSDNode>(Arg)->getValue();
1914 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1915 /// specifies a 8 element shuffle that can be broken into a pair of
1916 /// PSHUFHW and PSHUFLW.
1917 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1918 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1920 if (N->getNumOperands() != 8)
1923 // Lower quadword shuffled.
1924 for (unsigned i = 0; i != 4; ++i) {
1925 SDOperand Arg = N->getOperand(i);
1926 if (Arg.getOpcode() == ISD::UNDEF) continue;
1927 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1928 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1933 // Upper quadword shuffled.
1934 for (unsigned i = 4; i != 8; ++i) {
1935 SDOperand Arg = N->getOperand(i);
1936 if (Arg.getOpcode() == ISD::UNDEF) continue;
1937 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1938 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1939 if (Val < 4 || Val > 7)
1946 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
1947 /// values in ther permute mask.
1948 static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
1949 SDOperand V1 = Op.getOperand(0);
1950 SDOperand V2 = Op.getOperand(1);
1951 SDOperand Mask = Op.getOperand(2);
1952 MVT::ValueType VT = Op.getValueType();
1953 MVT::ValueType MaskVT = Mask.getValueType();
1954 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
1955 unsigned NumElems = Mask.getNumOperands();
1956 std::vector<SDOperand> MaskVec;
1958 for (unsigned i = 0; i != NumElems; ++i) {
1959 SDOperand Arg = Mask.getOperand(i);
1960 if (Arg.getOpcode() == ISD::UNDEF) {
1961 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1964 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1965 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1967 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
1969 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
1972 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
1973 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1976 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
1977 /// match movhlps. The lower half elements should come from upper half of
1978 /// V1 (and in order), and the upper half elements should come from the upper
1979 /// half of V2 (and in order).
1980 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
1981 unsigned NumElems = Mask->getNumOperands();
1984 for (unsigned i = 0, e = 2; i != e; ++i)
1985 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
1987 for (unsigned i = 2; i != 4; ++i)
1988 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
1993 /// isScalarLoadToVector - Returns true if the node is a scalar load that
1994 /// is promoted to a vector.
1995 static inline bool isScalarLoadToVector(SDNode *N) {
1996 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
1997 N = N->getOperand(0).Val;
1998 return (N->getOpcode() == ISD::LOAD);
2003 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2004 /// match movlp{s|d}. The lower half elements should come from lower half of
2005 /// V1 (and in order), and the upper half elements should come from the upper
2006 /// half of V2 (and in order). And since V1 will become the source of the
2007 /// MOVLP, it must be either a vector load or a scalar load to vector.
2008 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2009 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2012 unsigned NumElems = Mask->getNumOperands();
2013 if (NumElems != 2 && NumElems != 4)
2015 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2016 if (!isUndefOrEqual(Mask->getOperand(i), i))
2018 for (unsigned i = NumElems/2; i != NumElems; ++i)
2019 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2024 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2026 static bool isSplatVector(SDNode *N) {
2027 if (N->getOpcode() != ISD::BUILD_VECTOR)
2030 SDOperand SplatValue = N->getOperand(0);
2031 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2032 if (N->getOperand(i) != SplatValue)
2037 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2038 /// that point to V2 points to its first element.
2039 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2040 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2042 bool Changed = false;
2043 std::vector<SDOperand> MaskVec;
2044 unsigned NumElems = Mask.getNumOperands();
2045 for (unsigned i = 0; i != NumElems; ++i) {
2046 SDOperand Arg = Mask.getOperand(i);
2047 if (Arg.getOpcode() != ISD::UNDEF) {
2048 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2049 if (Val > NumElems) {
2050 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2054 MaskVec.push_back(Arg);
2058 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2059 &MaskVec[0], MaskVec.size());
2063 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2064 /// operation of specified width.
2065 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2066 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2067 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2069 std::vector<SDOperand> MaskVec;
2070 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2071 for (unsigned i = 1; i != NumElems; ++i)
2072 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2073 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2076 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2077 /// of specified width.
2078 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2079 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2080 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2081 std::vector<SDOperand> MaskVec;
2082 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2083 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2084 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2086 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2089 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2090 /// of specified width.
2091 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2092 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2093 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2094 unsigned Half = NumElems/2;
2095 std::vector<SDOperand> MaskVec;
2096 for (unsigned i = 0; i != Half; ++i) {
2097 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2098 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2100 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2103 /// getZeroVector - Returns a vector of specified type with all zero elements.
2105 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2106 assert(MVT::isVector(VT) && "Expected a vector type");
2107 unsigned NumElems = getVectorNumElements(VT);
2108 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2109 bool isFP = MVT::isFloatingPoint(EVT);
2110 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2111 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2112 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2115 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2117 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2118 SDOperand V1 = Op.getOperand(0);
2119 SDOperand Mask = Op.getOperand(2);
2120 MVT::ValueType VT = Op.getValueType();
2121 unsigned NumElems = Mask.getNumOperands();
2122 Mask = getUnpacklMask(NumElems, DAG);
2123 while (NumElems != 4) {
2124 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2127 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2129 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2130 Mask = getZeroVector(MaskVT, DAG);
2131 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2132 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2133 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2136 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2138 static inline bool isZeroNode(SDOperand Elt) {
2139 return ((isa<ConstantSDNode>(Elt) &&
2140 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2141 (isa<ConstantFPSDNode>(Elt) &&
2142 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2145 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2146 /// vector and zero or undef vector.
2147 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2148 unsigned NumElems, unsigned Idx,
2149 bool isZero, SelectionDAG &DAG) {
2150 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2151 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2152 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2153 SDOperand Zero = DAG.getConstant(0, EVT);
2154 std::vector<SDOperand> MaskVec(NumElems, Zero);
2155 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2156 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2157 &MaskVec[0], MaskVec.size());
2158 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2161 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2163 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2164 unsigned NumNonZero, unsigned NumZero,
2165 SelectionDAG &DAG) {
2171 for (unsigned i = 0; i < 16; ++i) {
2172 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2173 if (ThisIsNonZero && First) {
2175 V = getZeroVector(MVT::v8i16, DAG);
2177 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2182 SDOperand ThisElt(0, 0), LastElt(0, 0);
2183 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2184 if (LastIsNonZero) {
2185 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2187 if (ThisIsNonZero) {
2188 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2189 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2190 ThisElt, DAG.getConstant(8, MVT::i8));
2192 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2197 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2198 DAG.getConstant(i/2, MVT::i32));
2202 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2205 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2207 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2208 unsigned NumNonZero, unsigned NumZero,
2209 SelectionDAG &DAG) {
2215 for (unsigned i = 0; i < 8; ++i) {
2216 bool isNonZero = (NonZeros & (1 << i)) != 0;
2220 V = getZeroVector(MVT::v8i16, DAG);
2222 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2225 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2226 DAG.getConstant(i, MVT::i32));
2234 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2235 // All zero's are handled with pxor.
2236 if (ISD::isBuildVectorAllZeros(Op.Val))
2239 // All one's are handled with pcmpeqd.
2240 if (ISD::isBuildVectorAllOnes(Op.Val))
2243 MVT::ValueType VT = Op.getValueType();
2244 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2245 unsigned EVTBits = MVT::getSizeInBits(EVT);
2247 unsigned NumElems = Op.getNumOperands();
2248 unsigned NumZero = 0;
2249 unsigned NumNonZero = 0;
2250 unsigned NonZeros = 0;
2251 std::set<SDOperand> Values;
2252 for (unsigned i = 0; i < NumElems; ++i) {
2253 SDOperand Elt = Op.getOperand(i);
2254 if (Elt.getOpcode() != ISD::UNDEF) {
2256 if (isZeroNode(Elt))
2259 NonZeros |= (1 << i);
2265 if (NumNonZero == 0)
2266 // Must be a mix of zero and undef. Return a zero vector.
2267 return getZeroVector(VT, DAG);
2269 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2270 if (Values.size() == 1)
2273 // Special case for single non-zero element.
2274 if (NumNonZero == 1) {
2275 unsigned Idx = CountTrailingZeros_32(NonZeros);
2276 SDOperand Item = Op.getOperand(Idx);
2277 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2279 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2280 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2283 if (EVTBits == 32) {
2284 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2285 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2287 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2288 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2289 std::vector<SDOperand> MaskVec;
2290 for (unsigned i = 0; i < NumElems; i++)
2291 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2292 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2293 &MaskVec[0], MaskVec.size());
2294 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2295 DAG.getNode(ISD::UNDEF, VT), Mask);
2299 // Let legalizer expand 2-widde build_vector's.
2303 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2305 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2306 if (V.Val) return V;
2309 if (EVTBits == 16) {
2310 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2311 if (V.Val) return V;
2314 // If element VT is == 32 bits, turn it into a number of shuffles.
2315 std::vector<SDOperand> V(NumElems);
2316 if (NumElems == 4 && NumZero > 0) {
2317 for (unsigned i = 0; i < 4; ++i) {
2318 bool isZero = !(NonZeros & (1 << i));
2320 V[i] = getZeroVector(VT, DAG);
2322 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2325 for (unsigned i = 0; i < 2; ++i) {
2326 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2329 V[i] = V[i*2]; // Must be a zero vector.
2332 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2333 getMOVLMask(NumElems, DAG));
2336 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2337 getMOVLMask(NumElems, DAG));
2340 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2341 getUnpacklMask(NumElems, DAG));
2346 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2347 // clears the upper bits.
2348 // FIXME: we can do the same for v4f32 case when we know both parts of
2349 // the lower half come from scalar_to_vector (loadf32). We should do
2350 // that in post legalizer dag combiner with target specific hooks.
2351 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2353 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2354 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2355 std::vector<SDOperand> MaskVec;
2356 bool Reverse = (NonZeros & 0x3) == 2;
2357 for (unsigned i = 0; i < 2; ++i)
2359 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2361 MaskVec.push_back(DAG.getConstant(i, EVT));
2362 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2363 for (unsigned i = 0; i < 2; ++i)
2365 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2367 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2368 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2369 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2372 if (Values.size() > 2) {
2373 // Expand into a number of unpckl*.
2375 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2376 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2377 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2378 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2379 for (unsigned i = 0; i < NumElems; ++i)
2380 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2382 while (NumElems != 0) {
2383 for (unsigned i = 0; i < NumElems; ++i)
2384 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2395 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2396 SDOperand V1 = Op.getOperand(0);
2397 SDOperand V2 = Op.getOperand(1);
2398 SDOperand PermMask = Op.getOperand(2);
2399 MVT::ValueType VT = Op.getValueType();
2400 unsigned NumElems = PermMask.getNumOperands();
2401 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2402 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2404 if (isSplatMask(PermMask.Val)) {
2405 if (NumElems <= 4) return Op;
2406 // Promote it to a v4i32 splat.
2407 return PromoteSplat(Op, DAG);
2410 if (X86::isMOVLMask(PermMask.Val))
2411 return (V1IsUndef) ? V2 : Op;
2413 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2414 X86::isMOVSLDUPMask(PermMask.Val) ||
2415 X86::isMOVHLPSMask(PermMask.Val) ||
2416 X86::isMOVHPMask(PermMask.Val) ||
2417 X86::isMOVLPMask(PermMask.Val))
2420 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2421 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2422 return CommuteVectorShuffle(Op, DAG);
2424 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2425 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2426 if (V1IsSplat && !V2IsSplat) {
2427 Op = CommuteVectorShuffle(Op, DAG);
2428 V1 = Op.getOperand(0);
2429 V2 = Op.getOperand(1);
2430 PermMask = Op.getOperand(2);
2434 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2435 if (V2IsUndef) return V1;
2436 Op = CommuteVectorShuffle(Op, DAG);
2437 V1 = Op.getOperand(0);
2438 V2 = Op.getOperand(1);
2439 PermMask = Op.getOperand(2);
2441 // V2 is a splat, so the mask may be malformed. That is, it may point
2442 // to any V2 element. The instruction selectior won't like this. Get
2443 // a corrected mask and commute to form a proper MOVS{S|D}.
2444 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2445 if (NewMask.Val != PermMask.Val)
2446 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2451 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2452 X86::isUNPCKLMask(PermMask.Val) ||
2453 X86::isUNPCKHMask(PermMask.Val))
2457 // Normalize mask so all entries that point to V2 points to its first
2458 // element then try to match unpck{h|l} again. If match, return a
2459 // new vector_shuffle with the corrected mask.
2460 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2461 if (NewMask.Val != PermMask.Val) {
2462 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2463 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2464 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2465 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2466 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2467 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2472 // Normalize the node to match x86 shuffle ops if needed
2473 if (V2.getOpcode() != ISD::UNDEF)
2474 if (isCommutedSHUFP(PermMask.Val)) {
2475 Op = CommuteVectorShuffle(Op, DAG);
2476 V1 = Op.getOperand(0);
2477 V2 = Op.getOperand(1);
2478 PermMask = Op.getOperand(2);
2481 // If VT is integer, try PSHUF* first, then SHUFP*.
2482 if (MVT::isInteger(VT)) {
2483 if (X86::isPSHUFDMask(PermMask.Val) ||
2484 X86::isPSHUFHWMask(PermMask.Val) ||
2485 X86::isPSHUFLWMask(PermMask.Val)) {
2486 if (V2.getOpcode() != ISD::UNDEF)
2487 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2488 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2492 if (X86::isSHUFPMask(PermMask.Val))
2495 // Handle v8i16 shuffle high / low shuffle node pair.
2496 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2497 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2498 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2499 std::vector<SDOperand> MaskVec;
2500 for (unsigned i = 0; i != 4; ++i)
2501 MaskVec.push_back(PermMask.getOperand(i));
2502 for (unsigned i = 4; i != 8; ++i)
2503 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2504 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2505 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2507 for (unsigned i = 0; i != 4; ++i)
2508 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2509 for (unsigned i = 4; i != 8; ++i)
2510 MaskVec.push_back(PermMask.getOperand(i));
2511 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2512 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2515 // Floating point cases in the other order.
2516 if (X86::isSHUFPMask(PermMask.Val))
2518 if (X86::isPSHUFDMask(PermMask.Val) ||
2519 X86::isPSHUFHWMask(PermMask.Val) ||
2520 X86::isPSHUFLWMask(PermMask.Val)) {
2521 if (V2.getOpcode() != ISD::UNDEF)
2522 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2523 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2528 if (NumElems == 4) {
2529 MVT::ValueType MaskVT = PermMask.getValueType();
2530 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2531 std::vector<std::pair<int, int> > Locs;
2532 Locs.reserve(NumElems);
2533 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2534 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2537 // If no more than two elements come from either vector. This can be
2538 // implemented with two shuffles. First shuffle gather the elements.
2539 // The second shuffle, which takes the first shuffle as both of its
2540 // vector operands, put the elements into the right order.
2541 for (unsigned i = 0; i != NumElems; ++i) {
2542 SDOperand Elt = PermMask.getOperand(i);
2543 if (Elt.getOpcode() == ISD::UNDEF) {
2544 Locs[i] = std::make_pair(-1, -1);
2546 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2547 if (Val < NumElems) {
2548 Locs[i] = std::make_pair(0, NumLo);
2552 Locs[i] = std::make_pair(1, NumHi);
2553 if (2+NumHi < NumElems)
2554 Mask1[2+NumHi] = Elt;
2559 if (NumLo <= 2 && NumHi <= 2) {
2560 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2561 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2562 for (unsigned i = 0; i != NumElems; ++i) {
2563 if (Locs[i].first == -1)
2566 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2567 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2568 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2572 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2573 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2576 // Break it into (shuffle shuffle_hi, shuffle_lo).
2578 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2579 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2580 std::vector<SDOperand> *MaskPtr = &LoMask;
2581 unsigned MaskIdx = 0;
2583 unsigned HiIdx = NumElems/2;
2584 for (unsigned i = 0; i != NumElems; ++i) {
2585 if (i == NumElems/2) {
2591 SDOperand Elt = PermMask.getOperand(i);
2592 if (Elt.getOpcode() == ISD::UNDEF) {
2593 Locs[i] = std::make_pair(-1, -1);
2594 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2595 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2596 (*MaskPtr)[LoIdx] = Elt;
2599 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2600 (*MaskPtr)[HiIdx] = Elt;
2605 SDOperand LoShuffle =
2606 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2607 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2608 SDOperand HiShuffle =
2609 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2610 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
2611 std::vector<SDOperand> MaskOps;
2612 for (unsigned i = 0; i != NumElems; ++i) {
2613 if (Locs[i].first == -1) {
2614 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2616 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2617 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2620 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2621 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2628 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2629 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2632 MVT::ValueType VT = Op.getValueType();
2633 // TODO: handle v16i8.
2634 if (MVT::getSizeInBits(VT) == 16) {
2635 // Transform it so it match pextrw which produces a 32-bit result.
2636 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2637 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2638 Op.getOperand(0), Op.getOperand(1));
2639 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2640 DAG.getValueType(VT));
2641 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2642 } else if (MVT::getSizeInBits(VT) == 32) {
2643 SDOperand Vec = Op.getOperand(0);
2644 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2647 // SHUFPS the element to the lowest double word, then movss.
2648 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2649 std::vector<SDOperand> IdxVec;
2650 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2651 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2652 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2653 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2654 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2655 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2657 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2658 DAG.getConstant(0, getPointerTy()));
2659 } else if (MVT::getSizeInBits(VT) == 64) {
2660 SDOperand Vec = Op.getOperand(0);
2661 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2665 // UNPCKHPD the element to the lowest double word, then movsd.
2666 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2667 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2668 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2669 std::vector<SDOperand> IdxVec;
2670 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2671 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2672 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2673 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2674 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2675 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2676 DAG.getConstant(0, getPointerTy()));
2683 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2684 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2685 // as its second argument.
2686 MVT::ValueType VT = Op.getValueType();
2687 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2688 SDOperand N0 = Op.getOperand(0);
2689 SDOperand N1 = Op.getOperand(1);
2690 SDOperand N2 = Op.getOperand(2);
2691 if (MVT::getSizeInBits(BaseVT) == 16) {
2692 if (N1.getValueType() != MVT::i32)
2693 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2694 if (N2.getValueType() != MVT::i32)
2695 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2696 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2697 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2698 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2701 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2702 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2703 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2704 std::vector<SDOperand> MaskVec;
2705 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2706 for (unsigned i = 1; i <= 3; ++i)
2707 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2708 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2709 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2711 // Use two pinsrw instructions to insert a 32 bit value.
2713 if (MVT::isFloatingPoint(N1.getValueType())) {
2714 if (N1.getOpcode() == ISD::LOAD) {
2715 // Just load directly from f32mem to GR32.
2716 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2719 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2720 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2721 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2722 DAG.getConstant(0, getPointerTy()));
2725 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2726 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2727 DAG.getConstant(Idx, getPointerTy()));
2728 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2729 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2730 DAG.getConstant(Idx+1, getPointerTy()));
2731 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2739 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2740 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2741 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2744 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2745 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2746 // one of the above mentioned nodes. It has to be wrapped because otherwise
2747 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2748 // be used to form addressing mode. These wrapped nodes will be selected
2751 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2752 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2753 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2754 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2755 CP->getAlignment()));
2756 if (Subtarget->isTargetDarwin()) {
2757 // With PIC, the address is actually $g + Offset.
2758 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2759 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2760 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2767 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2768 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2769 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2770 DAG.getTargetGlobalAddress(GV,
2772 if (Subtarget->isTargetDarwin()) {
2773 // With PIC, the address is actually $g + Offset.
2774 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2775 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2776 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2779 // For Darwin, external and weak symbols are indirect, so we want to load
2780 // the value at address GV, not the value of GV itself. This means that
2781 // the GlobalAddress must be in the base or index register of the address,
2782 // not the GV offset field.
2783 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2784 DarwinGVRequiresExtraLoad(GV))
2785 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2786 Result, DAG.getSrcValue(NULL));
2793 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2794 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2795 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2796 DAG.getTargetExternalSymbol(Sym,
2798 if (Subtarget->isTargetDarwin()) {
2799 // With PIC, the address is actually $g + Offset.
2800 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2801 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2802 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2809 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
2810 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2811 "Not an i64 shift!");
2812 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2813 SDOperand ShOpLo = Op.getOperand(0);
2814 SDOperand ShOpHi = Op.getOperand(1);
2815 SDOperand ShAmt = Op.getOperand(2);
2816 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
2817 DAG.getConstant(31, MVT::i8))
2818 : DAG.getConstant(0, MVT::i32);
2820 SDOperand Tmp2, Tmp3;
2821 if (Op.getOpcode() == ISD::SHL_PARTS) {
2822 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2823 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2825 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
2826 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
2829 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2830 ShAmt, DAG.getConstant(32, MVT::i8));
2833 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
2835 std::vector<MVT::ValueType> Tys;
2836 Tys.push_back(MVT::i32);
2837 Tys.push_back(MVT::Flag);
2838 std::vector<SDOperand> Ops;
2839 if (Op.getOpcode() == ISD::SHL_PARTS) {
2840 Ops.push_back(Tmp2);
2841 Ops.push_back(Tmp3);
2843 Ops.push_back(InFlag);
2844 Hi = DAG.getNode(X86ISD::CMOV, Tys, &Ops[0], Ops.size());
2845 InFlag = Hi.getValue(1);
2848 Ops.push_back(Tmp3);
2849 Ops.push_back(Tmp1);
2851 Ops.push_back(InFlag);
2852 Lo = DAG.getNode(X86ISD::CMOV, Tys, &Ops[0], Ops.size());
2854 Ops.push_back(Tmp2);
2855 Ops.push_back(Tmp3);
2857 Ops.push_back(InFlag);
2858 Lo = DAG.getNode(X86ISD::CMOV, Tys, &Ops[0], Ops.size());
2859 InFlag = Lo.getValue(1);
2862 Ops.push_back(Tmp3);
2863 Ops.push_back(Tmp1);
2865 Ops.push_back(InFlag);
2866 Hi = DAG.getNode(X86ISD::CMOV, Tys, &Ops[0], Ops.size());
2870 Tys.push_back(MVT::i32);
2871 Tys.push_back(MVT::i32);
2875 return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size());
2878 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2879 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2880 Op.getOperand(0).getValueType() >= MVT::i16 &&
2881 "Unknown SINT_TO_FP to lower!");
2884 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2885 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
2886 MachineFunction &MF = DAG.getMachineFunction();
2887 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2888 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2889 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2890 DAG.getEntryNode(), Op.getOperand(0),
2891 StackSlot, DAG.getSrcValue(NULL));
2894 std::vector<MVT::ValueType> Tys;
2895 Tys.push_back(MVT::f64);
2896 Tys.push_back(MVT::Other);
2897 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
2898 std::vector<SDOperand> Ops;
2899 Ops.push_back(Chain);
2900 Ops.push_back(StackSlot);
2901 Ops.push_back(DAG.getValueType(SrcVT));
2902 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
2903 Tys, &Ops[0], Ops.size());
2906 Chain = Result.getValue(1);
2907 SDOperand InFlag = Result.getValue(2);
2909 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
2910 // shouldn't be necessary except that RFP cannot be live across
2911 // multiple blocks. When stackifier is fixed, they can be uncoupled.
2912 MachineFunction &MF = DAG.getMachineFunction();
2913 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2914 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2915 std::vector<MVT::ValueType> Tys;
2916 Tys.push_back(MVT::Other);
2917 std::vector<SDOperand> Ops;
2918 Ops.push_back(Chain);
2919 Ops.push_back(Result);
2920 Ops.push_back(StackSlot);
2921 Ops.push_back(DAG.getValueType(Op.getValueType()));
2922 Ops.push_back(InFlag);
2923 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
2924 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
2925 DAG.getSrcValue(NULL));
2931 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
2932 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
2933 "Unknown FP_TO_SINT to lower!");
2934 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
2936 MachineFunction &MF = DAG.getMachineFunction();
2937 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
2938 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2939 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2942 switch (Op.getValueType()) {
2943 default: assert(0 && "Invalid FP_TO_SINT to lower!");
2944 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
2945 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
2946 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
2949 SDOperand Chain = DAG.getEntryNode();
2950 SDOperand Value = Op.getOperand(0);
2952 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
2953 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
2954 DAG.getSrcValue(0));
2955 std::vector<MVT::ValueType> Tys;
2956 Tys.push_back(MVT::f64);
2957 Tys.push_back(MVT::Other);
2958 std::vector<SDOperand> Ops;
2959 Ops.push_back(Chain);
2960 Ops.push_back(StackSlot);
2961 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
2962 Value = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
2963 Chain = Value.getValue(1);
2964 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2965 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2968 // Build the FP_TO_INT*_IN_MEM
2969 std::vector<SDOperand> Ops;
2970 Ops.push_back(Chain);
2971 Ops.push_back(Value);
2972 Ops.push_back(StackSlot);
2973 SDOperand FIST = DAG.getNode(Opc, MVT::Other, &Ops[0], Ops.size());
2976 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
2977 DAG.getSrcValue(NULL));
2980 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
2981 MVT::ValueType VT = Op.getValueType();
2982 const Type *OpNTy = MVT::getTypeForValueType(VT);
2983 std::vector<Constant*> CV;
2984 if (VT == MVT::f64) {
2985 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
2986 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2988 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
2989 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2990 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2991 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2993 Constant *CS = ConstantStruct::get(CV);
2994 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2996 = DAG.getNode(X86ISD::LOAD_PACK,
2997 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2998 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3001 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3002 MVT::ValueType VT = Op.getValueType();
3003 const Type *OpNTy = MVT::getTypeForValueType(VT);
3004 std::vector<Constant*> CV;
3005 if (VT == MVT::f64) {
3006 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3007 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3009 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3010 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3011 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3012 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3014 Constant *CS = ConstantStruct::get(CV);
3015 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3016 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3017 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3018 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3021 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3022 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3024 SDOperand CC = Op.getOperand(2);
3025 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3026 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3029 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3031 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3032 Op.getOperand(1), Op.getOperand(0));
3034 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3035 Op.getOperand(0), Op.getOperand(1));
3036 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3037 DAG.getConstant(X86CC, MVT::i8), Cond);
3039 assert(isFP && "Illegal integer SetCC!");
3041 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3042 Op.getOperand(0), Op.getOperand(1));
3043 std::vector<MVT::ValueType> Tys;
3044 std::vector<SDOperand> Ops;
3045 switch (SetCCOpcode) {
3046 default: assert(false && "Illegal floating point SetCC!");
3047 case ISD::SETOEQ: { // !PF & ZF
3048 Tys.push_back(MVT::i8);
3049 Tys.push_back(MVT::Flag);
3050 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3051 Ops.push_back(Cond);
3052 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, &Ops[0], Ops.size());
3053 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3054 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3056 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3058 case ISD::SETUNE: { // PF | !ZF
3059 Tys.push_back(MVT::i8);
3060 Tys.push_back(MVT::Flag);
3061 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3062 Ops.push_back(Cond);
3063 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, &Ops[0], Ops.size());
3064 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3065 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3067 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3073 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3074 MVT::ValueType VT = Op.getValueType();
3075 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3076 bool addTest = false;
3077 SDOperand Op0 = Op.getOperand(0);
3079 if (Op0.getOpcode() == ISD::SETCC)
3080 Op0 = LowerOperation(Op0, DAG);
3082 if (Op0.getOpcode() == X86ISD::SETCC) {
3083 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3084 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3085 // have another use it will be eliminated.
3086 // If the X86ISD::SETCC has more than one use, then it's probably better
3087 // to use a test instead of duplicating the X86ISD::CMP (for register
3088 // pressure reason).
3089 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3090 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3091 CmpOpc == X86ISD::UCOMI) {
3092 if (!Op0.hasOneUse()) {
3093 std::vector<MVT::ValueType> Tys;
3094 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3095 Tys.push_back(Op0.Val->getValueType(i));
3096 std::vector<SDOperand> Ops;
3097 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3098 Ops.push_back(Op0.getOperand(i));
3099 Op0 = DAG.getNode(X86ISD::SETCC, Tys, &Ops[0], Ops.size());
3102 CC = Op0.getOperand(0);
3103 Cond = Op0.getOperand(1);
3104 // Make a copy as flag result cannot be used by more than one.
3105 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3106 Cond.getOperand(0), Cond.getOperand(1));
3108 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3115 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3116 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
3119 std::vector<MVT::ValueType> Tys;
3120 Tys.push_back(Op.getValueType());
3121 Tys.push_back(MVT::Flag);
3122 std::vector<SDOperand> Ops;
3123 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3124 // condition is true.
3125 Ops.push_back(Op.getOperand(2));
3126 Ops.push_back(Op.getOperand(1));
3128 Ops.push_back(Cond);
3129 return DAG.getNode(X86ISD::CMOV, Tys, &Ops[0], Ops.size());
3132 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3133 bool addTest = false;
3134 SDOperand Cond = Op.getOperand(1);
3135 SDOperand Dest = Op.getOperand(2);
3137 if (Cond.getOpcode() == ISD::SETCC)
3138 Cond = LowerOperation(Cond, DAG);
3140 if (Cond.getOpcode() == X86ISD::SETCC) {
3141 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3142 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3143 // have another use it will be eliminated.
3144 // If the X86ISD::SETCC has more than one use, then it's probably better
3145 // to use a test instead of duplicating the X86ISD::CMP (for register
3146 // pressure reason).
3147 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3148 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3149 CmpOpc == X86ISD::UCOMI) {
3150 if (!Cond.hasOneUse()) {
3151 std::vector<MVT::ValueType> Tys;
3152 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3153 Tys.push_back(Cond.Val->getValueType(i));
3154 std::vector<SDOperand> Ops;
3155 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3156 Ops.push_back(Cond.getOperand(i));
3157 Cond = DAG.getNode(X86ISD::SETCC, Tys, &Ops[0], Ops.size());
3160 CC = Cond.getOperand(0);
3161 Cond = Cond.getOperand(1);
3162 // Make a copy as flag result cannot be used by more than one.
3163 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3164 Cond.getOperand(0), Cond.getOperand(1));
3171 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3172 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
3174 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3175 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3178 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3179 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3180 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3181 DAG.getTargetJumpTable(JT->getIndex(),
3183 if (Subtarget->isTargetDarwin()) {
3184 // With PIC, the address is actually $g + Offset.
3185 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3186 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3187 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3194 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3195 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3196 if (CallingConv == CallingConv::Fast && EnableFastCC)
3197 return LowerFastCCCallTo(Op, DAG);
3199 return LowerCCCCallTo(Op, DAG);
3202 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3205 switch(Op.getNumOperands()) {
3207 assert(0 && "Do not know how to return this many arguments!");
3209 case 1: // ret void.
3210 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
3211 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
3213 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
3215 if (MVT::isVector(ArgVT)) {
3216 // Integer or FP vector result -> XMM0.
3217 if (DAG.getMachineFunction().liveout_empty())
3218 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3219 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3221 } else if (MVT::isInteger(ArgVT)) {
3222 // Integer result -> EAX
3223 if (DAG.getMachineFunction().liveout_empty())
3224 DAG.getMachineFunction().addLiveOut(X86::EAX);
3226 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3228 } else if (!X86ScalarSSE) {
3229 // FP return with fp-stack value.
3230 if (DAG.getMachineFunction().liveout_empty())
3231 DAG.getMachineFunction().addLiveOut(X86::ST0);
3233 std::vector<MVT::ValueType> Tys;
3234 Tys.push_back(MVT::Other);
3235 Tys.push_back(MVT::Flag);
3236 std::vector<SDOperand> Ops;
3237 Ops.push_back(Op.getOperand(0));
3238 Ops.push_back(Op.getOperand(1));
3239 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
3241 // FP return with ScalarSSE (return on fp-stack).
3242 if (DAG.getMachineFunction().liveout_empty())
3243 DAG.getMachineFunction().addLiveOut(X86::ST0);
3246 SDOperand Chain = Op.getOperand(0);
3247 SDOperand Value = Op.getOperand(1);
3249 if (Value.getOpcode() == ISD::LOAD &&
3250 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
3251 Chain = Value.getOperand(0);
3252 MemLoc = Value.getOperand(1);
3254 // Spill the value to memory and reload it into top of stack.
3255 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3256 MachineFunction &MF = DAG.getMachineFunction();
3257 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3258 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3259 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3260 Value, MemLoc, DAG.getSrcValue(0));
3262 std::vector<MVT::ValueType> Tys;
3263 Tys.push_back(MVT::f64);
3264 Tys.push_back(MVT::Other);
3265 std::vector<SDOperand> Ops;
3266 Ops.push_back(Chain);
3267 Ops.push_back(MemLoc);
3268 Ops.push_back(DAG.getValueType(ArgVT));
3269 Copy = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
3271 Tys.push_back(MVT::Other);
3272 Tys.push_back(MVT::Flag);
3274 Ops.push_back(Copy.getValue(1));
3275 Ops.push_back(Copy);
3276 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
3281 if (DAG.getMachineFunction().liveout_empty()) {
3282 DAG.getMachineFunction().addLiveOut(X86::EAX);
3283 DAG.getMachineFunction().addLiveOut(X86::EDX);
3286 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(3),
3288 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3291 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3292 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3297 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3298 MachineFunction &MF = DAG.getMachineFunction();
3299 const Function* Fn = MF.getFunction();
3300 if (Fn->hasExternalLinkage() &&
3301 Subtarget->TargetType == X86Subtarget::isCygwin &&
3302 Fn->getName() == "main")
3303 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3305 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3306 if (CC == CallingConv::Fast && EnableFastCC)
3307 return LowerFastCCArguments(Op, DAG);
3309 return LowerCCCArguments(Op, DAG);
3312 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3313 SDOperand InFlag(0, 0);
3314 SDOperand Chain = Op.getOperand(0);
3316 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3317 if (Align == 0) Align = 1;
3319 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3320 // If not DWORD aligned, call memset if size is less than the threshold.
3321 // It knows how to align to the right boundary first.
3322 if ((Align & 3) != 0 ||
3323 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3324 MVT::ValueType IntPtr = getPointerTy();
3325 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3326 std::vector<std::pair<SDOperand, const Type*> > Args;
3327 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3328 // Extend the ubyte argument to be an int value for the call.
3329 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3330 Args.push_back(std::make_pair(Val, IntPtrTy));
3331 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3332 std::pair<SDOperand,SDOperand> CallResult =
3333 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3334 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3335 return CallResult.second;
3340 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3341 unsigned BytesLeft = 0;
3342 bool TwoRepStos = false;
3345 unsigned Val = ValC->getValue() & 255;
3347 // If the value is a constant, then we can potentially use larger sets.
3348 switch (Align & 3) {
3349 case 2: // WORD aligned
3351 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3352 BytesLeft = I->getValue() % 2;
3353 Val = (Val << 8) | Val;
3356 case 0: // DWORD aligned
3359 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3360 BytesLeft = I->getValue() % 4;
3362 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3363 DAG.getConstant(2, MVT::i8));
3366 Val = (Val << 8) | Val;
3367 Val = (Val << 16) | Val;
3370 default: // Byte aligned
3372 Count = Op.getOperand(3);
3377 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3379 InFlag = Chain.getValue(1);
3382 Count = Op.getOperand(3);
3383 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3384 InFlag = Chain.getValue(1);
3387 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3388 InFlag = Chain.getValue(1);
3389 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3390 InFlag = Chain.getValue(1);
3392 std::vector<MVT::ValueType> Tys;
3393 Tys.push_back(MVT::Other);
3394 Tys.push_back(MVT::Flag);
3395 std::vector<SDOperand> Ops;
3396 Ops.push_back(Chain);
3397 Ops.push_back(DAG.getValueType(AVT));
3398 Ops.push_back(InFlag);
3399 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3402 InFlag = Chain.getValue(1);
3403 Count = Op.getOperand(3);
3404 MVT::ValueType CVT = Count.getValueType();
3405 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3406 DAG.getConstant(3, CVT));
3407 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3408 InFlag = Chain.getValue(1);
3410 Tys.push_back(MVT::Other);
3411 Tys.push_back(MVT::Flag);
3413 Ops.push_back(Chain);
3414 Ops.push_back(DAG.getValueType(MVT::i8));
3415 Ops.push_back(InFlag);
3416 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3417 } else if (BytesLeft) {
3418 // Issue stores for the last 1 - 3 bytes.
3420 unsigned Val = ValC->getValue() & 255;
3421 unsigned Offset = I->getValue() - BytesLeft;
3422 SDOperand DstAddr = Op.getOperand(1);
3423 MVT::ValueType AddrVT = DstAddr.getValueType();
3424 if (BytesLeft >= 2) {
3425 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3426 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3427 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3428 DAG.getConstant(Offset, AddrVT)),
3429 DAG.getSrcValue(NULL));
3434 if (BytesLeft == 1) {
3435 Value = DAG.getConstant(Val, MVT::i8);
3436 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3437 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3438 DAG.getConstant(Offset, AddrVT)),
3439 DAG.getSrcValue(NULL));
3446 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3447 SDOperand Chain = Op.getOperand(0);
3449 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3450 if (Align == 0) Align = 1;
3452 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3453 // If not DWORD aligned, call memcpy if size is less than the threshold.
3454 // It knows how to align to the right boundary first.
3455 if ((Align & 3) != 0 ||
3456 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3457 MVT::ValueType IntPtr = getPointerTy();
3458 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3459 std::vector<std::pair<SDOperand, const Type*> > Args;
3460 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3461 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3462 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3463 std::pair<SDOperand,SDOperand> CallResult =
3464 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3465 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3466 return CallResult.second;
3471 unsigned BytesLeft = 0;
3472 bool TwoRepMovs = false;
3473 switch (Align & 3) {
3474 case 2: // WORD aligned
3476 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3477 BytesLeft = I->getValue() % 2;
3479 case 0: // DWORD aligned
3482 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3483 BytesLeft = I->getValue() % 4;
3485 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3486 DAG.getConstant(2, MVT::i8));
3490 default: // Byte aligned
3492 Count = Op.getOperand(3);
3496 SDOperand InFlag(0, 0);
3497 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3498 InFlag = Chain.getValue(1);
3499 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3500 InFlag = Chain.getValue(1);
3501 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3502 InFlag = Chain.getValue(1);
3504 std::vector<MVT::ValueType> Tys;
3505 Tys.push_back(MVT::Other);
3506 Tys.push_back(MVT::Flag);
3507 std::vector<SDOperand> Ops;
3508 Ops.push_back(Chain);
3509 Ops.push_back(DAG.getValueType(AVT));
3510 Ops.push_back(InFlag);
3511 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3514 InFlag = Chain.getValue(1);
3515 Count = Op.getOperand(3);
3516 MVT::ValueType CVT = Count.getValueType();
3517 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3518 DAG.getConstant(3, CVT));
3519 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3520 InFlag = Chain.getValue(1);
3522 Tys.push_back(MVT::Other);
3523 Tys.push_back(MVT::Flag);
3525 Ops.push_back(Chain);
3526 Ops.push_back(DAG.getValueType(MVT::i8));
3527 Ops.push_back(InFlag);
3528 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3529 } else if (BytesLeft) {
3530 // Issue loads and stores for the last 1 - 3 bytes.
3531 unsigned Offset = I->getValue() - BytesLeft;
3532 SDOperand DstAddr = Op.getOperand(1);
3533 MVT::ValueType DstVT = DstAddr.getValueType();
3534 SDOperand SrcAddr = Op.getOperand(2);
3535 MVT::ValueType SrcVT = SrcAddr.getValueType();
3537 if (BytesLeft >= 2) {
3538 Value = DAG.getLoad(MVT::i16, Chain,
3539 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3540 DAG.getConstant(Offset, SrcVT)),
3541 DAG.getSrcValue(NULL));
3542 Chain = Value.getValue(1);
3543 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3544 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3545 DAG.getConstant(Offset, DstVT)),
3546 DAG.getSrcValue(NULL));
3551 if (BytesLeft == 1) {
3552 Value = DAG.getLoad(MVT::i8, Chain,
3553 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3554 DAG.getConstant(Offset, SrcVT)),
3555 DAG.getSrcValue(NULL));
3556 Chain = Value.getValue(1);
3557 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3558 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3559 DAG.getConstant(Offset, DstVT)),
3560 DAG.getSrcValue(NULL));
3568 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3569 std::vector<MVT::ValueType> Tys;
3570 Tys.push_back(MVT::Other);
3571 Tys.push_back(MVT::Flag);
3572 std::vector<SDOperand> Ops;
3573 Ops.push_back(Op.getOperand(0));
3574 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &Ops[0], Ops.size());
3576 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3577 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3578 MVT::i32, Ops[0].getValue(2)));
3579 Ops.push_back(Ops[1].getValue(1));
3580 Tys[0] = Tys[1] = MVT::i32;
3581 Tys.push_back(MVT::Other);
3582 return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size());
3585 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3586 // vastart just stores the address of the VarArgsFrameIndex slot into the
3587 // memory location argument.
3588 // FIXME: Replace MVT::i32 with PointerTy
3589 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3590 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3591 Op.getOperand(1), Op.getOperand(2));
3595 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3596 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3598 default: return SDOperand(); // Don't custom lower most intrinsics.
3599 // Comparison intrinsics.
3600 case Intrinsic::x86_sse_comieq_ss:
3601 case Intrinsic::x86_sse_comilt_ss:
3602 case Intrinsic::x86_sse_comile_ss:
3603 case Intrinsic::x86_sse_comigt_ss:
3604 case Intrinsic::x86_sse_comige_ss:
3605 case Intrinsic::x86_sse_comineq_ss:
3606 case Intrinsic::x86_sse_ucomieq_ss:
3607 case Intrinsic::x86_sse_ucomilt_ss:
3608 case Intrinsic::x86_sse_ucomile_ss:
3609 case Intrinsic::x86_sse_ucomigt_ss:
3610 case Intrinsic::x86_sse_ucomige_ss:
3611 case Intrinsic::x86_sse_ucomineq_ss:
3612 case Intrinsic::x86_sse2_comieq_sd:
3613 case Intrinsic::x86_sse2_comilt_sd:
3614 case Intrinsic::x86_sse2_comile_sd:
3615 case Intrinsic::x86_sse2_comigt_sd:
3616 case Intrinsic::x86_sse2_comige_sd:
3617 case Intrinsic::x86_sse2_comineq_sd:
3618 case Intrinsic::x86_sse2_ucomieq_sd:
3619 case Intrinsic::x86_sse2_ucomilt_sd:
3620 case Intrinsic::x86_sse2_ucomile_sd:
3621 case Intrinsic::x86_sse2_ucomigt_sd:
3622 case Intrinsic::x86_sse2_ucomige_sd:
3623 case Intrinsic::x86_sse2_ucomineq_sd: {
3625 ISD::CondCode CC = ISD::SETCC_INVALID;
3628 case Intrinsic::x86_sse_comieq_ss:
3629 case Intrinsic::x86_sse2_comieq_sd:
3633 case Intrinsic::x86_sse_comilt_ss:
3634 case Intrinsic::x86_sse2_comilt_sd:
3638 case Intrinsic::x86_sse_comile_ss:
3639 case Intrinsic::x86_sse2_comile_sd:
3643 case Intrinsic::x86_sse_comigt_ss:
3644 case Intrinsic::x86_sse2_comigt_sd:
3648 case Intrinsic::x86_sse_comige_ss:
3649 case Intrinsic::x86_sse2_comige_sd:
3653 case Intrinsic::x86_sse_comineq_ss:
3654 case Intrinsic::x86_sse2_comineq_sd:
3658 case Intrinsic::x86_sse_ucomieq_ss:
3659 case Intrinsic::x86_sse2_ucomieq_sd:
3660 Opc = X86ISD::UCOMI;
3663 case Intrinsic::x86_sse_ucomilt_ss:
3664 case Intrinsic::x86_sse2_ucomilt_sd:
3665 Opc = X86ISD::UCOMI;
3668 case Intrinsic::x86_sse_ucomile_ss:
3669 case Intrinsic::x86_sse2_ucomile_sd:
3670 Opc = X86ISD::UCOMI;
3673 case Intrinsic::x86_sse_ucomigt_ss:
3674 case Intrinsic::x86_sse2_ucomigt_sd:
3675 Opc = X86ISD::UCOMI;
3678 case Intrinsic::x86_sse_ucomige_ss:
3679 case Intrinsic::x86_sse2_ucomige_sd:
3680 Opc = X86ISD::UCOMI;
3683 case Intrinsic::x86_sse_ucomineq_ss:
3684 case Intrinsic::x86_sse2_ucomineq_sd:
3685 Opc = X86ISD::UCOMI;
3691 translateX86CC(CC, true, X86CC, Flip);
3692 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3693 Op.getOperand(Flip?1:2));
3694 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3695 DAG.getConstant(X86CC, MVT::i8), Cond);
3696 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
3701 /// LowerOperation - Provide custom lowering hooks for some operations.
3703 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3704 switch (Op.getOpcode()) {
3705 default: assert(0 && "Should not custom lower this!");
3706 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3707 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3708 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3709 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3710 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3711 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3712 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3713 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3714 case ISD::SHL_PARTS:
3715 case ISD::SRA_PARTS:
3716 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3717 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3718 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3719 case ISD::FABS: return LowerFABS(Op, DAG);
3720 case ISD::FNEG: return LowerFNEG(Op, DAG);
3721 case ISD::SETCC: return LowerSETCC(Op, DAG);
3722 case ISD::SELECT: return LowerSELECT(Op, DAG);
3723 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3724 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3725 case ISD::CALL: return LowerCALL(Op, DAG);
3726 case ISD::RET: return LowerRET(Op, DAG);
3727 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
3728 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3729 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3730 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3731 case ISD::VASTART: return LowerVASTART(Op, DAG);
3732 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3736 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3738 default: return NULL;
3739 case X86ISD::SHLD: return "X86ISD::SHLD";
3740 case X86ISD::SHRD: return "X86ISD::SHRD";
3741 case X86ISD::FAND: return "X86ISD::FAND";
3742 case X86ISD::FXOR: return "X86ISD::FXOR";
3743 case X86ISD::FILD: return "X86ISD::FILD";
3744 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
3745 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3746 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3747 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
3748 case X86ISD::FLD: return "X86ISD::FLD";
3749 case X86ISD::FST: return "X86ISD::FST";
3750 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
3751 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
3752 case X86ISD::CALL: return "X86ISD::CALL";
3753 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3754 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3755 case X86ISD::CMP: return "X86ISD::CMP";
3756 case X86ISD::TEST: return "X86ISD::TEST";
3757 case X86ISD::COMI: return "X86ISD::COMI";
3758 case X86ISD::UCOMI: return "X86ISD::UCOMI";
3759 case X86ISD::SETCC: return "X86ISD::SETCC";
3760 case X86ISD::CMOV: return "X86ISD::CMOV";
3761 case X86ISD::BRCOND: return "X86ISD::BRCOND";
3762 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
3763 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3764 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
3765 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
3766 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
3767 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
3768 case X86ISD::Wrapper: return "X86ISD::Wrapper";
3769 case X86ISD::S2VEC: return "X86ISD::S2VEC";
3770 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
3771 case X86ISD::PINSRW: return "X86ISD::PINSRW";
3775 /// isLegalAddressImmediate - Return true if the integer value or
3776 /// GlobalValue can be used as the offset of the target addressing mode.
3777 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3778 // X86 allows a sign-extended 32-bit immediate field.
3779 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3782 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
3783 // GV is 64-bit but displacement field is 32-bit unless we are in small code
3784 // model. Mac OS X happens to support only small PIC code model.
3785 // FIXME: better support for other OS's.
3786 if (Subtarget->is64Bit() && !Subtarget->isTargetDarwin())
3788 if (Subtarget->isTargetDarwin()) {
3789 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3790 if (RModel == Reloc::Static)
3792 else if (RModel == Reloc::DynamicNoPIC)
3793 return !DarwinGVRequiresExtraLoad(GV);
3800 /// isShuffleMaskLegal - Targets can use this to indicate that they only
3801 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3802 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3803 /// are assumed to be legal.
3805 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3806 // Only do shuffles on 128-bit vector types for now.
3807 if (MVT::getSizeInBits(VT) == 64) return false;
3808 return (Mask.Val->getNumOperands() <= 4 ||
3809 isSplatMask(Mask.Val) ||
3810 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
3811 X86::isUNPCKLMask(Mask.Val) ||
3812 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
3813 X86::isUNPCKHMask(Mask.Val));
3816 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3818 SelectionDAG &DAG) const {
3819 unsigned NumElts = BVOps.size();
3820 // Only do shuffles on 128-bit vector types for now.
3821 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
3822 if (NumElts == 2) return true;
3824 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
3825 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
3830 //===----------------------------------------------------------------------===//
3831 // X86 Scheduler Hooks
3832 //===----------------------------------------------------------------------===//
3835 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
3836 MachineBasicBlock *BB) {
3837 switch (MI->getOpcode()) {
3838 default: assert(false && "Unexpected instr type to insert");
3839 case X86::CMOV_FR32:
3840 case X86::CMOV_FR64:
3841 case X86::CMOV_V4F32:
3842 case X86::CMOV_V2F64:
3843 case X86::CMOV_V2I64: {
3844 // To "insert" a SELECT_CC instruction, we actually have to insert the
3845 // diamond control-flow pattern. The incoming instruction knows the
3846 // destination vreg to set, the condition code register to branch on, the
3847 // true/false values to select between, and a branch opcode to use.
3848 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3849 ilist<MachineBasicBlock>::iterator It = BB;
3855 // cmpTY ccX, r1, r2
3857 // fallthrough --> copy0MBB
3858 MachineBasicBlock *thisMBB = BB;
3859 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
3860 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
3861 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
3862 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
3863 MachineFunction *F = BB->getParent();
3864 F->getBasicBlockList().insert(It, copy0MBB);
3865 F->getBasicBlockList().insert(It, sinkMBB);
3866 // Update machine-CFG edges by first adding all successors of the current
3867 // block to the new block which will contain the Phi node for the select.
3868 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
3869 e = BB->succ_end(); i != e; ++i)
3870 sinkMBB->addSuccessor(*i);
3871 // Next, remove all successors of the current block, and add the true
3872 // and fallthrough blocks as its successors.
3873 while(!BB->succ_empty())
3874 BB->removeSuccessor(BB->succ_begin());
3875 BB->addSuccessor(copy0MBB);
3876 BB->addSuccessor(sinkMBB);
3879 // %FalseValue = ...
3880 // # fallthrough to sinkMBB
3883 // Update machine-CFG edges
3884 BB->addSuccessor(sinkMBB);
3887 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3890 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
3891 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3892 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3894 delete MI; // The pseudo instruction is gone now.
3898 case X86::FP_TO_INT16_IN_MEM:
3899 case X86::FP_TO_INT32_IN_MEM:
3900 case X86::FP_TO_INT64_IN_MEM: {
3901 // Change the floating point control register to use "round towards zero"
3902 // mode when truncating to an integer value.
3903 MachineFunction *F = BB->getParent();
3904 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
3905 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
3907 // Load the old value of the high byte of the control word...
3909 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
3910 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
3912 // Set the high part to be round to zero...
3913 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
3915 // Reload the modified control word now...
3916 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
3918 // Restore the memory image of control word to original value
3919 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
3921 // Get the X86 opcode to use.
3923 switch (MI->getOpcode()) {
3924 default: assert(0 && "illegal opcode!");
3925 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
3926 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
3927 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
3931 MachineOperand &Op = MI->getOperand(0);
3932 if (Op.isRegister()) {
3933 AM.BaseType = X86AddressMode::RegBase;
3934 AM.Base.Reg = Op.getReg();
3936 AM.BaseType = X86AddressMode::FrameIndexBase;
3937 AM.Base.FrameIndex = Op.getFrameIndex();
3939 Op = MI->getOperand(1);
3940 if (Op.isImmediate())
3941 AM.Scale = Op.getImmedValue();
3942 Op = MI->getOperand(2);
3943 if (Op.isImmediate())
3944 AM.IndexReg = Op.getImmedValue();
3945 Op = MI->getOperand(3);
3946 if (Op.isGlobalAddress()) {
3947 AM.GV = Op.getGlobal();
3949 AM.Disp = Op.getImmedValue();
3951 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
3953 // Reload the original control word now.
3954 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
3956 delete MI; // The pseudo instruction is gone now.
3962 //===----------------------------------------------------------------------===//
3963 // X86 Optimization Hooks
3964 //===----------------------------------------------------------------------===//
3966 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3968 uint64_t &KnownZero,
3970 unsigned Depth) const {
3971 unsigned Opc = Op.getOpcode();
3972 assert((Opc >= ISD::BUILTIN_OP_END ||
3973 Opc == ISD::INTRINSIC_WO_CHAIN ||
3974 Opc == ISD::INTRINSIC_W_CHAIN ||
3975 Opc == ISD::INTRINSIC_VOID) &&
3976 "Should use MaskedValueIsZero if you don't know whether Op"
3977 " is a target node!");
3979 KnownZero = KnownOne = 0; // Don't know anything.
3983 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3988 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
3989 /// element of the result of the vector shuffle.
3990 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
3991 MVT::ValueType VT = N->getValueType(0);
3992 SDOperand PermMask = N->getOperand(2);
3993 unsigned NumElems = PermMask.getNumOperands();
3994 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
3996 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3998 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
3999 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4000 SDOperand Idx = PermMask.getOperand(i);
4001 if (Idx.getOpcode() == ISD::UNDEF)
4002 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4003 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4008 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4009 /// node is a GlobalAddress + an offset.
4010 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4011 if (N->getOpcode() == X86ISD::Wrapper) {
4012 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4013 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4016 } else if (N->getOpcode() == ISD::ADD) {
4017 SDOperand N1 = N->getOperand(0);
4018 SDOperand N2 = N->getOperand(1);
4019 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4020 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4022 Offset += V->getSignExtended();
4025 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4026 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4028 Offset += V->getSignExtended();
4036 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4038 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4039 MachineFrameInfo *MFI) {
4040 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4043 SDOperand Loc = N->getOperand(1);
4044 SDOperand BaseLoc = Base->getOperand(1);
4045 if (Loc.getOpcode() == ISD::FrameIndex) {
4046 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4048 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4049 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4050 int FS = MFI->getObjectSize(FI);
4051 int BFS = MFI->getObjectSize(BFI);
4052 if (FS != BFS || FS != Size) return false;
4053 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4055 GlobalValue *GV1 = NULL;
4056 GlobalValue *GV2 = NULL;
4057 int64_t Offset1 = 0;
4058 int64_t Offset2 = 0;
4059 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4060 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4061 if (isGA1 && isGA2 && GV1 == GV2)
4062 return Offset1 == (Offset2 + Dist*Size);
4068 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4069 const X86Subtarget *Subtarget) {
4072 if (isGAPlusOffset(Base, GV, Offset))
4073 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4075 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4076 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
4078 // Fixed objects do not specify alignment, however the offsets are known.
4079 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4080 (MFI->getObjectOffset(BFI) % 16) == 0);
4082 return MFI->getObjectAlignment(BFI) >= 16;
4088 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4089 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4090 /// if the load addresses are consecutive, non-overlapping, and in the right
4092 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4093 const X86Subtarget *Subtarget) {
4094 MachineFunction &MF = DAG.getMachineFunction();
4095 MachineFrameInfo *MFI = MF.getFrameInfo();
4096 MVT::ValueType VT = N->getValueType(0);
4097 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4098 SDOperand PermMask = N->getOperand(2);
4099 int NumElems = (int)PermMask.getNumOperands();
4100 SDNode *Base = NULL;
4101 for (int i = 0; i < NumElems; ++i) {
4102 SDOperand Idx = PermMask.getOperand(i);
4103 if (Idx.getOpcode() == ISD::UNDEF) {
4104 if (!Base) return SDOperand();
4107 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4108 if (!Arg.Val || Arg.getOpcode() != ISD::LOAD)
4112 else if (!isConsecutiveLoad(Arg.Val, Base,
4113 i, MVT::getSizeInBits(EVT)/8,MFI))
4118 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
4120 return DAG.getLoad(VT, Base->getOperand(0), Base->getOperand(1),
4121 Base->getOperand(2));
4123 // Just use movups, it's shorter.
4124 return DAG.getNode(ISD::BIT_CONVERT, VT,
4125 DAG.getNode(X86ISD::LOAD_UA, MVT::v4f32,
4126 Base->getOperand(0), Base->getOperand(1),
4127 Base->getOperand(2)));
4131 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4132 DAGCombinerInfo &DCI) const {
4133 TargetMachine &TM = getTargetMachine();
4134 SelectionDAG &DAG = DCI.DAG;
4135 switch (N->getOpcode()) {
4137 case ISD::VECTOR_SHUFFLE:
4138 return PerformShuffleCombine(N, DAG, Subtarget);
4144 //===----------------------------------------------------------------------===//
4145 // X86 Inline Assembly Support
4146 //===----------------------------------------------------------------------===//
4148 /// getConstraintType - Given a constraint letter, return the type of
4149 /// constraint it is for this target.
4150 X86TargetLowering::ConstraintType
4151 X86TargetLowering::getConstraintType(char ConstraintLetter) const {
4152 switch (ConstraintLetter) {
4161 return C_RegisterClass;
4162 default: return TargetLowering::getConstraintType(ConstraintLetter);
4166 std::vector<unsigned> X86TargetLowering::
4167 getRegClassForInlineAsmConstraint(const std::string &Constraint,
4168 MVT::ValueType VT) const {
4169 if (Constraint.size() == 1) {
4170 // FIXME: not handling fp-stack yet!
4171 // FIXME: not handling MMX registers yet ('y' constraint).
4172 switch (Constraint[0]) { // GCC X86 Constraint Letters
4173 default: break; // Unknown constraint letter
4174 case 'A': // EAX/EDX
4175 if (VT == MVT::i32 || VT == MVT::i64)
4176 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4178 case 'r': // GENERAL_REGS
4179 case 'R': // LEGACY_REGS
4181 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4182 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4183 else if (VT == MVT::i16)
4184 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4185 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4186 else if (VT == MVT::i8)
4187 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4189 case 'l': // INDEX_REGS
4191 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4192 X86::ESI, X86::EDI, X86::EBP, 0);
4193 else if (VT == MVT::i16)
4194 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4195 X86::SI, X86::DI, X86::BP, 0);
4196 else if (VT == MVT::i8)
4197 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4199 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4202 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4203 else if (VT == MVT::i16)
4204 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4205 else if (VT == MVT::i8)
4206 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4208 case 'x': // SSE_REGS if SSE1 allowed
4209 if (Subtarget->hasSSE1())
4210 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4211 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4213 return std::vector<unsigned>();
4214 case 'Y': // SSE_REGS if SSE2 allowed
4215 if (Subtarget->hasSSE2())
4216 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4217 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4219 return std::vector<unsigned>();
4223 return std::vector<unsigned>();
4226 std::pair<unsigned, const TargetRegisterClass*>
4227 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4228 MVT::ValueType VT) const {
4229 // Use the default implementation in TargetLowering to convert the register
4230 // constraint into a member of a register class.
4231 std::pair<unsigned, const TargetRegisterClass*> Res;
4232 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4234 // Not found? Bail out.
4235 if (Res.second == 0) return Res;
4237 // Otherwise, check to see if this is a register class of the wrong value
4238 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4239 // turn into {ax},{dx}.
4240 if (Res.second->hasType(VT))
4241 return Res; // Correct type already, nothing to do.
4243 // All of the single-register GCC register classes map their values onto
4244 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4245 // really want an 8-bit or 32-bit register, map to the appropriate register
4246 // class and return the appropriate register.
4247 if (Res.second != X86::GR16RegisterClass)
4250 if (VT == MVT::i8) {
4251 unsigned DestReg = 0;
4252 switch (Res.first) {
4254 case X86::AX: DestReg = X86::AL; break;
4255 case X86::DX: DestReg = X86::DL; break;
4256 case X86::CX: DestReg = X86::CL; break;
4257 case X86::BX: DestReg = X86::BL; break;
4260 Res.first = DestReg;
4261 Res.second = Res.second = X86::GR8RegisterClass;
4263 } else if (VT == MVT::i32) {
4264 unsigned DestReg = 0;
4265 switch (Res.first) {
4267 case X86::AX: DestReg = X86::EAX; break;
4268 case X86::DX: DestReg = X86::EDX; break;
4269 case X86::CX: DestReg = X86::ECX; break;
4270 case X86::BX: DestReg = X86::EBX; break;
4271 case X86::SI: DestReg = X86::ESI; break;
4272 case X86::DI: DestReg = X86::EDI; break;
4273 case X86::BP: DestReg = X86::EBP; break;
4274 case X86::SP: DestReg = X86::ESP; break;
4277 Res.first = DestReg;
4278 Res.second = Res.second = X86::GR32RegisterClass;