1 //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that X86 uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
16 #include "X86InstrBuilder.h"
17 #include "X86ISelLowering.h"
18 #include "X86MachineFunctionInfo.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/CallingConv.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/ADT/VectorExtras.h"
26 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/SelectionDAG.h"
31 #include "llvm/CodeGen/SSARegMap.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/ADT/StringExtras.h"
39 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
40 cl::desc("Enable fastcc on X86"));
41 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
43 Subtarget = &TM.getSubtarget<X86Subtarget>();
44 X86ScalarSSE = Subtarget->hasSSE2();
45 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
47 // Set up the TargetLowering object.
49 // X86 is weird, it always uses i8 for shift amounts and setcc results.
50 setShiftAmountType(MVT::i8);
51 setSetCCResultType(MVT::i8);
52 setSetCCResultContents(ZeroOrOneSetCCResult);
53 setSchedulingPreference(SchedulingForRegPressure);
54 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
55 setStackPointerRegisterToSaveRestore(X86StackPtr);
57 if (!Subtarget->isTargetDarwin())
58 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
59 setUseUnderscoreSetJmpLongJmp(true);
61 // Add legal addressing mode scale values.
62 addLegalAddressScale(8);
63 addLegalAddressScale(4);
64 addLegalAddressScale(2);
65 // Enter the ones which require both scale + index last. These are more
67 addLegalAddressScale(9);
68 addLegalAddressScale(5);
69 addLegalAddressScale(3);
71 // Set up the register classes.
72 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
73 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
74 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
75 if (Subtarget->is64Bit())
76 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
78 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
80 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
82 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
83 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
84 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
86 if (Subtarget->is64Bit()) {
87 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
88 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
91 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
92 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
94 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
97 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
99 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
100 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
101 // SSE has no i16 to fp conversion, only i32
103 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
105 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
106 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
109 if (!Subtarget->is64Bit()) {
110 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
111 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
112 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
115 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
117 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
118 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
121 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
123 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
124 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
127 // Handle FP_TO_UINT by promoting the destination to a larger signed
129 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
130 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
131 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
133 if (Subtarget->is64Bit()) {
134 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
135 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
137 if (X86ScalarSSE && !Subtarget->hasSSE3())
138 // Expand FP_TO_UINT into a select.
139 // FIXME: We would like to use a Custom expander here eventually to do
140 // the optimal thing for SSE vs. the default expansion in the legalizer.
141 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
143 // With SSE3 we can use fisttpll to convert to a signed i64.
144 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
147 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
148 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
150 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
151 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
152 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
153 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
154 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
155 if (Subtarget->is64Bit())
156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
160 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
161 setOperationAction(ISD::FREM , MVT::f64 , Expand);
163 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
164 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
165 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
166 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
167 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
168 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
169 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
170 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
171 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
172 if (Subtarget->is64Bit()) {
173 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
174 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
175 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
178 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
179 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
181 // These should be promoted to a larger select which is supported.
182 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
183 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
184 // X86 wants to expand cmov itself.
185 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
186 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
187 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
188 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
189 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
190 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
191 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
192 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
193 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
194 if (Subtarget->is64Bit()) {
195 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
196 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
198 // X86 ret instruction may pop stack.
199 setOperationAction(ISD::RET , MVT::Other, Custom);
201 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
202 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
203 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
204 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
205 if (Subtarget->is64Bit()) {
206 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
207 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
208 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
209 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
211 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
212 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
213 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
214 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
215 // X86 wants to expand memset / memcpy itself.
216 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
217 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
219 // We don't have line number support yet.
220 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
221 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
222 // FIXME - use subtarget debug flags
223 if (!Subtarget->isTargetDarwin() &&
224 !Subtarget->isTargetELF() &&
225 !Subtarget->isTargetCygwin())
226 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
228 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
229 setOperationAction(ISD::VASTART , MVT::Other, Custom);
231 // Use the default implementation.
232 setOperationAction(ISD::VAARG , MVT::Other, Expand);
233 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
234 setOperationAction(ISD::VAEND , MVT::Other, Expand);
235 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
236 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
237 if (Subtarget->is64Bit())
238 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
239 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
241 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
242 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
245 // Set up the FP register classes.
246 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
247 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
249 // Use ANDPD to simulate FABS.
250 setOperationAction(ISD::FABS , MVT::f64, Custom);
251 setOperationAction(ISD::FABS , MVT::f32, Custom);
253 // Use XORP to simulate FNEG.
254 setOperationAction(ISD::FNEG , MVT::f64, Custom);
255 setOperationAction(ISD::FNEG , MVT::f32, Custom);
257 // We don't support sin/cos/fmod
258 setOperationAction(ISD::FSIN , MVT::f64, Expand);
259 setOperationAction(ISD::FCOS , MVT::f64, Expand);
260 setOperationAction(ISD::FREM , MVT::f64, Expand);
261 setOperationAction(ISD::FSIN , MVT::f32, Expand);
262 setOperationAction(ISD::FCOS , MVT::f32, Expand);
263 setOperationAction(ISD::FREM , MVT::f32, Expand);
265 // Expand FP immediates into loads from the stack, except for the special
267 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
268 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
269 addLegalFPImmediate(+0.0); // xorps / xorpd
271 // Set up the FP register classes.
272 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
274 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
277 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
278 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
281 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
282 addLegalFPImmediate(+0.0); // FLD0
283 addLegalFPImmediate(+1.0); // FLD1
284 addLegalFPImmediate(-0.0); // FLD0/FCHS
285 addLegalFPImmediate(-1.0); // FLD1/FCHS
288 // First set operation action for all vector types to expand. Then we
289 // will selectively turn on ones that can be effectively codegen'd.
290 for (unsigned VT = (unsigned)MVT::Vector + 1;
291 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
292 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
293 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
294 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
295 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
296 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
297 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
298 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
299 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
300 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
301 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
302 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
303 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
304 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
305 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
306 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
309 if (Subtarget->hasMMX()) {
310 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
311 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
312 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
314 // FIXME: add MMX packed arithmetics
315 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
316 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
317 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
320 if (Subtarget->hasSSE1()) {
321 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
323 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
324 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
325 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
326 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
327 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
328 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
329 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
330 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
331 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
334 if (Subtarget->hasSSE2()) {
335 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
336 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
337 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
338 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
339 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
341 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
342 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
343 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
344 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
345 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
346 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
347 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
348 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
349 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
350 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
351 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
353 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
354 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
355 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
356 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
357 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
358 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
360 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
361 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
362 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
363 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
364 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
366 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
367 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
368 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
369 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
370 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
371 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
373 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
374 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
375 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
376 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
377 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
378 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
379 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
380 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
381 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
382 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
383 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
384 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
387 // Custom lower v2i64 and v2f64 selects.
388 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
389 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
390 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
391 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
394 // We want to custom lower some of our intrinsics.
395 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
397 // We have target-specific dag combine patterns for the following nodes:
398 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
399 setTargetDAGCombine(ISD::SELECT);
401 computeRegisterProperties();
403 // FIXME: These should be based on subtarget info. Plus, the values should
404 // be smaller when we are in optimizing for size mode.
405 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
406 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
407 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
408 allowUnalignedMemoryAccesses = true; // x86 supports it!
411 //===----------------------------------------------------------------------===//
412 // C Calling Convention implementation
413 //===----------------------------------------------------------------------===//
415 /// AddLiveIn - This helper function adds the specified physical register to the
416 /// MachineFunction as a live in value. It also creates a corresponding virtual
418 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
419 TargetRegisterClass *RC) {
420 assert(RC->contains(PReg) && "Not the correct regclass!");
421 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
422 MF.addLiveIn(PReg, VReg);
426 /// HowToPassCCCArgument - Returns how an formal argument of the specified type
427 /// should be passed. If it is through stack, returns the size of the stack
428 /// slot; if it is through XMM register, returns the number of XMM registers
431 HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
432 unsigned &ObjSize, unsigned &ObjXMMRegs) {
436 default: assert(0 && "Unhandled argument type!");
437 case MVT::i8: ObjSize = 1; break;
438 case MVT::i16: ObjSize = 2; break;
439 case MVT::i32: ObjSize = 4; break;
440 case MVT::i64: ObjSize = 8; break;
441 case MVT::f32: ObjSize = 4; break;
442 case MVT::f64: ObjSize = 8; break;
457 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
458 unsigned NumArgs = Op.Val->getNumValues() - 1;
459 MachineFunction &MF = DAG.getMachineFunction();
460 MachineFrameInfo *MFI = MF.getFrameInfo();
461 SDOperand Root = Op.getOperand(0);
462 std::vector<SDOperand> ArgValues;
464 // Add DAG nodes to load the arguments... On entry to a function on the X86,
465 // the stack frame looks like this:
467 // [ESP] -- return address
468 // [ESP + 4] -- first argument (leftmost lexically)
469 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
472 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
473 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
474 static const unsigned XMMArgRegs[] = {
475 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
477 for (unsigned i = 0; i < NumArgs; ++i) {
478 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
479 unsigned ArgIncrement = 4;
480 unsigned ObjSize = 0;
481 unsigned ObjXMMRegs = 0;
482 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
484 ArgIncrement = ObjSize;
488 // Passed in a XMM register.
489 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
490 X86::VR128RegisterClass);
491 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
492 ArgValues.push_back(ArgValue);
493 NumXMMRegs += ObjXMMRegs;
495 // XMM arguments have to be aligned on 16-byte boundary.
497 ArgOffset = ((ArgOffset + 15) / 16) * 16;
498 // Create the frame index object for this incoming parameter...
499 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
500 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
501 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
502 ArgValues.push_back(ArgValue);
503 ArgOffset += ArgIncrement; // Move on to the next argument...
507 ArgValues.push_back(Root);
509 // If the function takes variable number of arguments, make a frame index for
510 // the start of the first vararg value... for expansion of llvm.va_start.
511 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
513 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
514 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
515 ReturnAddrIndex = 0; // No return address slot generated yet.
516 BytesToPopOnReturn = 0; // Callee pops nothing.
517 BytesCallerReserves = ArgOffset;
519 // If this is a struct return on, the callee pops the hidden struct
520 // pointer. This is common for Darwin/X86, Linux & Mingw32 targets.
521 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet)
522 BytesToPopOnReturn = 4;
524 // Return the new list of results.
525 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
526 Op.Val->value_end());
527 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
531 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
532 SDOperand Chain = Op.getOperand(0);
533 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
534 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
535 SDOperand Callee = Op.getOperand(4);
536 MVT::ValueType RetVT= Op.Val->getValueType(0);
537 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
539 // Keep track of the number of XMM regs passed so far.
540 unsigned NumXMMRegs = 0;
541 static const unsigned XMMArgRegs[] = {
542 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
545 // Count how many bytes are to be pushed on the stack.
546 unsigned NumBytes = 0;
547 for (unsigned i = 0; i != NumOps; ++i) {
548 SDOperand Arg = Op.getOperand(5+2*i);
550 switch (Arg.getValueType()) {
551 default: assert(0 && "Unexpected ValueType for argument!");
571 // XMM arguments have to be aligned on 16-byte boundary.
572 NumBytes = ((NumBytes + 15) / 16) * 16;
579 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
581 // Arguments go on the stack in reverse order, as specified by the ABI.
582 unsigned ArgOffset = 0;
584 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
585 std::vector<SDOperand> MemOpChains;
586 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
587 for (unsigned i = 0; i != NumOps; ++i) {
588 SDOperand Arg = Op.getOperand(5+2*i);
590 switch (Arg.getValueType()) {
591 default: assert(0 && "Unexpected ValueType for argument!");
594 // Promote the integer to 32 bits. If the input type is signed use a
595 // sign extend, otherwise use a zero extend.
597 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
598 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
599 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
605 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
606 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
607 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
613 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
614 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
615 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
625 if (NumXMMRegs < 4) {
626 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
629 // XMM arguments have to be aligned on 16-byte boundary.
630 ArgOffset = ((ArgOffset + 15) / 16) * 16;
631 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
632 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
633 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
639 if (!MemOpChains.empty())
640 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
641 &MemOpChains[0], MemOpChains.size());
643 // Build a sequence of copy-to-reg nodes chained together with token chain
644 // and flag operands which copy the outgoing args into registers.
646 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
647 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
649 InFlag = Chain.getValue(1);
652 // If the callee is a GlobalAddress node (quite common, every direct call is)
653 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
654 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
655 // We should use extra load for direct calls to dllimported functions
656 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), true))
657 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
658 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
659 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
661 std::vector<MVT::ValueType> NodeTys;
662 NodeTys.push_back(MVT::Other); // Returns a chain
663 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
664 std::vector<SDOperand> Ops;
665 Ops.push_back(Chain);
666 Ops.push_back(Callee);
668 // Add argument registers to the end of the list so that they are known live
670 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
671 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
672 RegsToPass[i].second.getValueType()));
675 Ops.push_back(InFlag);
677 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
678 NodeTys, &Ops[0], Ops.size());
679 InFlag = Chain.getValue(1);
681 // Create the CALLSEQ_END node.
682 unsigned NumBytesForCalleeToPush = 0;
684 // If this is is a call to a struct-return function, the callee
685 // pops the hidden struct pointer, so we have to push it back.
686 // This is common for Darwin/X86, Linux & Mingw32 targets.
687 if (CallingConv == CallingConv::CSRet)
688 NumBytesForCalleeToPush = 4;
691 NodeTys.push_back(MVT::Other); // Returns a chain
692 if (RetVT != MVT::Other)
693 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
695 Ops.push_back(Chain);
696 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
697 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
698 Ops.push_back(InFlag);
699 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
700 if (RetVT != MVT::Other)
701 InFlag = Chain.getValue(1);
703 std::vector<SDOperand> ResultVals;
706 default: assert(0 && "Unknown value type to return!");
707 case MVT::Other: break;
709 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
710 ResultVals.push_back(Chain.getValue(0));
711 NodeTys.push_back(MVT::i8);
714 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
715 ResultVals.push_back(Chain.getValue(0));
716 NodeTys.push_back(MVT::i16);
719 if (Op.Val->getValueType(1) == MVT::i32) {
720 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
721 ResultVals.push_back(Chain.getValue(0));
722 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
723 Chain.getValue(2)).getValue(1);
724 ResultVals.push_back(Chain.getValue(0));
725 NodeTys.push_back(MVT::i32);
727 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
728 ResultVals.push_back(Chain.getValue(0));
730 NodeTys.push_back(MVT::i32);
738 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
739 ResultVals.push_back(Chain.getValue(0));
740 NodeTys.push_back(RetVT);
744 std::vector<MVT::ValueType> Tys;
745 Tys.push_back(MVT::f64);
746 Tys.push_back(MVT::Other);
747 Tys.push_back(MVT::Flag);
748 std::vector<SDOperand> Ops;
749 Ops.push_back(Chain);
750 Ops.push_back(InFlag);
751 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
752 &Ops[0], Ops.size());
753 Chain = RetVal.getValue(1);
754 InFlag = RetVal.getValue(2);
756 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
757 // shouldn't be necessary except that RFP cannot be live across
758 // multiple blocks. When stackifier is fixed, they can be uncoupled.
759 MachineFunction &MF = DAG.getMachineFunction();
760 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
761 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
763 Tys.push_back(MVT::Other);
765 Ops.push_back(Chain);
766 Ops.push_back(RetVal);
767 Ops.push_back(StackSlot);
768 Ops.push_back(DAG.getValueType(RetVT));
769 Ops.push_back(InFlag);
770 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
771 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
772 Chain = RetVal.getValue(1);
775 if (RetVT == MVT::f32 && !X86ScalarSSE)
776 // FIXME: we would really like to remember that this FP_ROUND
777 // operation is okay to eliminate if we allow excess FP precision.
778 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
779 ResultVals.push_back(RetVal);
780 NodeTys.push_back(RetVT);
785 // If the function returns void, just return the chain.
786 if (ResultVals.empty())
789 // Otherwise, merge everything together with a MERGE_VALUES node.
790 NodeTys.push_back(MVT::Other);
791 ResultVals.push_back(Chain);
792 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
793 &ResultVals[0], ResultVals.size());
794 return Res.getValue(Op.ResNo);
798 //===----------------------------------------------------------------------===//
799 // X86-64 C Calling Convention implementation
800 //===----------------------------------------------------------------------===//
802 /// HowToPassX86_64CCCArgument - Returns how an formal argument of the specified
803 /// type should be passed. If it is through stack, returns the size of the stack
804 /// slot; if it is through integer or XMM register, returns the number of
805 /// integer or XMM registers are needed.
807 HowToPassX86_64CCCArgument(MVT::ValueType ObjectVT,
808 unsigned NumIntRegs, unsigned NumXMMRegs,
809 unsigned &ObjSize, unsigned &ObjIntRegs,
810 unsigned &ObjXMMRegs) {
816 default: assert(0 && "Unhandled argument type!");
826 case MVT::i8: ObjSize = 1; break;
827 case MVT::i16: ObjSize = 2; break;
828 case MVT::i32: ObjSize = 4; break;
829 case MVT::i64: ObjSize = 8; break;
846 case MVT::f32: ObjSize = 4; break;
847 case MVT::f64: ObjSize = 8; break;
853 case MVT::v2f64: ObjSize = 16; break;
861 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
862 unsigned NumArgs = Op.Val->getNumValues() - 1;
863 MachineFunction &MF = DAG.getMachineFunction();
864 MachineFrameInfo *MFI = MF.getFrameInfo();
865 SDOperand Root = Op.getOperand(0);
866 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
867 std::vector<SDOperand> ArgValues;
869 // Add DAG nodes to load the arguments... On entry to a function on the X86,
870 // the stack frame looks like this:
872 // [RSP] -- return address
873 // [RSP + 8] -- first nonreg argument (leftmost lexically)
874 // [RSP +16] -- second nonreg argument, if 1st argument is <= 8 bytes in size
877 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
878 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
879 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
881 static const unsigned GPR8ArgRegs[] = {
882 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
884 static const unsigned GPR16ArgRegs[] = {
885 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
887 static const unsigned GPR32ArgRegs[] = {
888 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
890 static const unsigned GPR64ArgRegs[] = {
891 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
893 static const unsigned XMMArgRegs[] = {
894 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
895 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
898 for (unsigned i = 0; i < NumArgs; ++i) {
899 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
900 unsigned ArgIncrement = 8;
901 unsigned ObjSize = 0;
902 unsigned ObjIntRegs = 0;
903 unsigned ObjXMMRegs = 0;
905 // FIXME: __int128 and long double support?
906 HowToPassX86_64CCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
907 ObjSize, ObjIntRegs, ObjXMMRegs);
909 ArgIncrement = ObjSize;
913 if (ObjIntRegs || ObjXMMRegs) {
915 default: assert(0 && "Unhandled argument type!");
920 TargetRegisterClass *RC = NULL;
924 RC = X86::GR8RegisterClass;
925 Reg = GPR8ArgRegs[NumIntRegs];
928 RC = X86::GR16RegisterClass;
929 Reg = GPR16ArgRegs[NumIntRegs];
932 RC = X86::GR32RegisterClass;
933 Reg = GPR32ArgRegs[NumIntRegs];
936 RC = X86::GR64RegisterClass;
937 Reg = GPR64ArgRegs[NumIntRegs];
940 Reg = AddLiveIn(MF, Reg, RC);
941 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
952 TargetRegisterClass *RC= (ObjectVT == MVT::f32) ?
953 X86::FR32RegisterClass : ((ObjectVT == MVT::f64) ?
954 X86::FR64RegisterClass : X86::VR128RegisterClass);
955 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], RC);
956 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
960 NumIntRegs += ObjIntRegs;
961 NumXMMRegs += ObjXMMRegs;
962 } else if (ObjSize) {
963 // XMM arguments have to be aligned on 16-byte boundary.
965 ArgOffset = ((ArgOffset + 15) / 16) * 16;
966 // Create the SelectionDAG nodes corresponding to a load from this
968 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
969 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
970 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
971 ArgOffset += ArgIncrement; // Move on to the next argument.
974 ArgValues.push_back(ArgValue);
977 // If the function takes variable number of arguments, make a frame index for
978 // the start of the first vararg value... for expansion of llvm.va_start.
980 // For X86-64, if there are vararg parameters that are passed via
981 // registers, then we must store them to their spots on the stack so they
982 // may be loaded by deferencing the result of va_next.
983 VarArgsGPOffset = NumIntRegs * 8;
984 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
985 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
986 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
988 // Store the integer parameter registers.
989 std::vector<SDOperand> MemOps;
990 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
991 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
992 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
993 for (; NumIntRegs != 6; ++NumIntRegs) {
994 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
995 X86::GR64RegisterClass);
996 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
997 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
998 MemOps.push_back(Store);
999 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1000 DAG.getConstant(8, getPointerTy()));
1003 // Now store the XMM (fp + vector) parameter registers.
1004 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1005 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1006 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1007 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1008 X86::VR128RegisterClass);
1009 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1010 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1011 MemOps.push_back(Store);
1012 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1013 DAG.getConstant(16, getPointerTy()));
1015 if (!MemOps.empty())
1016 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1017 &MemOps[0], MemOps.size());
1020 ArgValues.push_back(Root);
1022 ReturnAddrIndex = 0; // No return address slot generated yet.
1023 BytesToPopOnReturn = 0; // Callee pops nothing.
1024 BytesCallerReserves = ArgOffset;
1026 // Return the new list of results.
1027 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1028 Op.Val->value_end());
1029 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1033 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG) {
1034 SDOperand Chain = Op.getOperand(0);
1035 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1036 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1037 SDOperand Callee = Op.getOperand(4);
1038 MVT::ValueType RetVT= Op.Val->getValueType(0);
1039 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1041 // Count how many bytes are to be pushed on the stack.
1042 unsigned NumBytes = 0;
1043 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
1044 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1046 static const unsigned GPR8ArgRegs[] = {
1047 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
1049 static const unsigned GPR16ArgRegs[] = {
1050 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
1052 static const unsigned GPR32ArgRegs[] = {
1053 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1055 static const unsigned GPR64ArgRegs[] = {
1056 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1058 static const unsigned XMMArgRegs[] = {
1059 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1060 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1063 for (unsigned i = 0; i != NumOps; ++i) {
1064 SDOperand Arg = Op.getOperand(5+2*i);
1065 MVT::ValueType ArgVT = Arg.getValueType();
1068 default: assert(0 && "Unknown value type!");
1088 else if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1091 // XMM arguments have to be aligned on 16-byte boundary.
1092 NumBytes = ((NumBytes + 15) / 16) * 16;
1099 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1101 // Arguments go on the stack in reverse order, as specified by the ABI.
1102 unsigned ArgOffset = 0;
1105 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1106 std::vector<SDOperand> MemOpChains;
1107 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1108 for (unsigned i = 0; i != NumOps; ++i) {
1109 SDOperand Arg = Op.getOperand(5+2*i);
1110 MVT::ValueType ArgVT = Arg.getValueType();
1113 default: assert(0 && "Unexpected ValueType for argument!");
1118 if (NumIntRegs < 6) {
1122 case MVT::i8: Reg = GPR8ArgRegs[NumIntRegs]; break;
1123 case MVT::i16: Reg = GPR16ArgRegs[NumIntRegs]; break;
1124 case MVT::i32: Reg = GPR32ArgRegs[NumIntRegs]; break;
1125 case MVT::i64: Reg = GPR64ArgRegs[NumIntRegs]; break;
1127 RegsToPass.push_back(std::make_pair(Reg, Arg));
1130 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1131 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1132 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1144 if (NumXMMRegs < 8) {
1145 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1148 if (ArgVT != MVT::f32 && ArgVT != MVT::f64) {
1149 // XMM arguments have to be aligned on 16-byte boundary.
1150 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1152 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1153 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1154 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1155 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1163 if (!MemOpChains.empty())
1164 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1165 &MemOpChains[0], MemOpChains.size());
1167 // Build a sequence of copy-to-reg nodes chained together with token chain
1168 // and flag operands which copy the outgoing args into registers.
1170 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1171 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1173 InFlag = Chain.getValue(1);
1177 // From AMD64 ABI document:
1178 // For calls that may call functions that use varargs or stdargs
1179 // (prototype-less calls or calls to functions containing ellipsis (...) in
1180 // the declaration) %al is used as hidden argument to specify the number
1181 // of SSE registers used. The contents of %al do not need to match exactly
1182 // the number of registers, but must be an ubound on the number of SSE
1183 // registers used and is in the range 0 - 8 inclusive.
1184 Chain = DAG.getCopyToReg(Chain, X86::AL,
1185 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1186 InFlag = Chain.getValue(1);
1189 // If the callee is a GlobalAddress node (quite common, every direct call is)
1190 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1191 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1192 // We should use extra load for direct calls to dllimported functions
1193 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), true))
1194 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1195 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1196 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1198 std::vector<MVT::ValueType> NodeTys;
1199 NodeTys.push_back(MVT::Other); // Returns a chain
1200 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1201 std::vector<SDOperand> Ops;
1202 Ops.push_back(Chain);
1203 Ops.push_back(Callee);
1205 // Add argument registers to the end of the list so that they are known live
1207 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1208 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1209 RegsToPass[i].second.getValueType()));
1212 Ops.push_back(InFlag);
1214 // FIXME: Do not generate X86ISD::TAILCALL for now.
1215 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1216 NodeTys, &Ops[0], Ops.size());
1217 InFlag = Chain.getValue(1);
1220 NodeTys.push_back(MVT::Other); // Returns a chain
1221 if (RetVT != MVT::Other)
1222 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1224 Ops.push_back(Chain);
1225 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1226 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1227 Ops.push_back(InFlag);
1228 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1229 if (RetVT != MVT::Other)
1230 InFlag = Chain.getValue(1);
1232 std::vector<SDOperand> ResultVals;
1235 default: assert(0 && "Unknown value type to return!");
1236 case MVT::Other: break;
1238 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1239 ResultVals.push_back(Chain.getValue(0));
1240 NodeTys.push_back(MVT::i8);
1243 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1244 ResultVals.push_back(Chain.getValue(0));
1245 NodeTys.push_back(MVT::i16);
1248 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1249 ResultVals.push_back(Chain.getValue(0));
1250 NodeTys.push_back(MVT::i32);
1253 if (Op.Val->getValueType(1) == MVT::i64) {
1254 // FIXME: __int128 support?
1255 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1256 ResultVals.push_back(Chain.getValue(0));
1257 Chain = DAG.getCopyFromReg(Chain, X86::RDX, MVT::i64,
1258 Chain.getValue(2)).getValue(1);
1259 ResultVals.push_back(Chain.getValue(0));
1260 NodeTys.push_back(MVT::i64);
1262 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1263 ResultVals.push_back(Chain.getValue(0));
1265 NodeTys.push_back(MVT::i64);
1275 // FIXME: long double support?
1276 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1277 ResultVals.push_back(Chain.getValue(0));
1278 NodeTys.push_back(RetVT);
1282 // If the function returns void, just return the chain.
1283 if (ResultVals.empty())
1286 // Otherwise, merge everything together with a MERGE_VALUES node.
1287 NodeTys.push_back(MVT::Other);
1288 ResultVals.push_back(Chain);
1289 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1290 &ResultVals[0], ResultVals.size());
1291 return Res.getValue(Op.ResNo);
1294 //===----------------------------------------------------------------------===//
1295 // Fast Calling Convention implementation
1296 //===----------------------------------------------------------------------===//
1298 // The X86 'fast' calling convention passes up to two integer arguments in
1299 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
1300 // and requires that the callee pop its arguments off the stack (allowing proper
1301 // tail calls), and has the same return value conventions as C calling convs.
1303 // This calling convention always arranges for the callee pop value to be 8n+4
1304 // bytes, which is needed for tail recursion elimination and stack alignment
1307 // Note that this can be enhanced in the future to pass fp vals in registers
1308 // (when we have a global fp allocator) and do other tricks.
1311 /// HowToPassFastCCArgument - Returns how an formal argument of the specified
1312 /// type should be passed. If it is through stack, returns the size of the stack
1313 /// slot; if it is through integer or XMM register, returns the number of
1314 /// integer or XMM registers are needed.
1316 HowToPassFastCCArgument(MVT::ValueType ObjectVT,
1317 unsigned NumIntRegs, unsigned NumXMMRegs,
1318 unsigned &ObjSize, unsigned &ObjIntRegs,
1319 unsigned &ObjXMMRegs) {
1325 default: assert(0 && "Unhandled argument type!");
1327 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1328 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1335 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1336 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1343 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1344 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
1351 #if FASTCC_NUM_INT_ARGS_INREGS > 0
1352 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
1354 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
1381 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1382 unsigned NumArgs = Op.Val->getNumValues()-1;
1383 MachineFunction &MF = DAG.getMachineFunction();
1384 MachineFrameInfo *MFI = MF.getFrameInfo();
1385 SDOperand Root = Op.getOperand(0);
1386 std::vector<SDOperand> ArgValues;
1388 // Add DAG nodes to load the arguments... On entry to a function the stack
1389 // frame looks like this:
1391 // [ESP] -- return address
1392 // [ESP + 4] -- first nonreg argument (leftmost lexically)
1393 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
1395 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1397 // Keep track of the number of integer regs passed so far. This can be either
1398 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1400 unsigned NumIntRegs = 0;
1401 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1403 static const unsigned XMMArgRegs[] = {
1404 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1407 for (unsigned i = 0; i < NumArgs; ++i) {
1408 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1409 unsigned ArgIncrement = 4;
1410 unsigned ObjSize = 0;
1411 unsigned ObjIntRegs = 0;
1412 unsigned ObjXMMRegs = 0;
1414 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
1415 ObjSize, ObjIntRegs, ObjXMMRegs);
1417 ArgIncrement = ObjSize;
1421 if (ObjIntRegs || ObjXMMRegs) {
1423 default: assert(0 && "Unhandled argument type!");
1425 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
1426 X86::GR8RegisterClass);
1427 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
1430 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
1431 X86::GR16RegisterClass);
1432 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
1435 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
1436 X86::GR32RegisterClass);
1437 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1440 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
1441 X86::GR32RegisterClass);
1442 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1443 if (ObjIntRegs == 2) {
1444 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
1445 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
1446 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
1455 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
1456 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1459 NumIntRegs += ObjIntRegs;
1460 NumXMMRegs += ObjXMMRegs;
1464 // XMM arguments have to be aligned on 16-byte boundary.
1466 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1467 // Create the SelectionDAG nodes corresponding to a load from this
1469 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1470 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1471 if (ObjectVT == MVT::i64 && ObjIntRegs) {
1472 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
1474 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
1476 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1477 ArgOffset += ArgIncrement; // Move on to the next argument.
1480 ArgValues.push_back(ArgValue);
1483 ArgValues.push_back(Root);
1485 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1486 // arguments and the arguments after the retaddr has been pushed are aligned.
1487 if ((ArgOffset & 7) == 0)
1490 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1491 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1492 ReturnAddrIndex = 0; // No return address slot generated yet.
1493 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1494 BytesCallerReserves = 0;
1496 // Finally, inform the code generator which regs we return values in.
1497 switch (getValueType(MF.getFunction()->getReturnType())) {
1498 default: assert(0 && "Unknown type!");
1499 case MVT::isVoid: break;
1504 MF.addLiveOut(X86::EAX);
1507 MF.addLiveOut(X86::EAX);
1508 MF.addLiveOut(X86::EDX);
1512 MF.addLiveOut(X86::ST0);
1520 MF.addLiveOut(X86::XMM0);
1524 // Return the new list of results.
1525 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1526 Op.Val->value_end());
1527 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1530 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1532 SDOperand Chain = Op.getOperand(0);
1533 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1534 SDOperand Callee = Op.getOperand(4);
1535 MVT::ValueType RetVT= Op.Val->getValueType(0);
1536 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1538 // Count how many bytes are to be pushed on the stack.
1539 unsigned NumBytes = 0;
1541 // Keep track of the number of integer regs passed so far. This can be either
1542 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
1544 unsigned NumIntRegs = 0;
1545 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1547 static const unsigned GPRArgRegs[][2] = {
1548 { X86::AL, X86::DL },
1549 { X86::AX, X86::DX },
1550 { X86::EAX, X86::EDX }
1553 static const unsigned FastCallGPRArgRegs[][2] = {
1554 { X86::CL, X86::DL },
1555 { X86::CX, X86::DX },
1556 { X86::ECX, X86::EDX }
1559 static const unsigned XMMArgRegs[] = {
1560 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1563 for (unsigned i = 0; i != NumOps; ++i) {
1564 SDOperand Arg = Op.getOperand(5+2*i);
1566 switch (Arg.getValueType()) {
1567 default: assert(0 && "Unknown value type!");
1571 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1572 if (NumIntRegs < MaxNumIntRegs) {
1590 assert(0 && "Unknown value type!");
1595 // XMM arguments have to be aligned on 16-byte boundary.
1596 NumBytes = ((NumBytes + 15) / 16) * 16;
1604 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1605 // arguments and the arguments after the retaddr has been pushed are aligned.
1606 if ((NumBytes & 7) == 0)
1609 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1611 // Arguments go on the stack in reverse order, as specified by the ABI.
1612 unsigned ArgOffset = 0;
1614 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1615 std::vector<SDOperand> MemOpChains;
1616 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1617 for (unsigned i = 0; i != NumOps; ++i) {
1618 SDOperand Arg = Op.getOperand(5+2*i);
1620 switch (Arg.getValueType()) {
1621 default: assert(0 && "Unexpected ValueType for argument!");
1625 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1626 if (NumIntRegs < MaxNumIntRegs) {
1627 RegsToPass.push_back(
1628 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1635 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1636 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1637 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1642 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1643 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1644 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1655 assert(0 && "Unexpected ValueType for argument!");
1657 if (NumXMMRegs < 4) {
1658 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1661 // XMM arguments have to be aligned on 16-byte boundary.
1662 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1663 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1664 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1665 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1673 if (!MemOpChains.empty())
1674 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1675 &MemOpChains[0], MemOpChains.size());
1677 // Build a sequence of copy-to-reg nodes chained together with token chain
1678 // and flag operands which copy the outgoing args into registers.
1680 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1681 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1683 InFlag = Chain.getValue(1);
1686 // If the callee is a GlobalAddress node (quite common, every direct call is)
1687 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1688 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1689 // We should use extra load for direct calls to dllimported functions
1690 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), true))
1691 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1692 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1693 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1695 std::vector<MVT::ValueType> NodeTys;
1696 NodeTys.push_back(MVT::Other); // Returns a chain
1697 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1698 std::vector<SDOperand> Ops;
1699 Ops.push_back(Chain);
1700 Ops.push_back(Callee);
1702 // Add argument registers to the end of the list so that they are known live
1704 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1705 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1706 RegsToPass[i].second.getValueType()));
1709 Ops.push_back(InFlag);
1711 // FIXME: Do not generate X86ISD::TAILCALL for now.
1712 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1713 NodeTys, &Ops[0], Ops.size());
1714 InFlag = Chain.getValue(1);
1717 NodeTys.push_back(MVT::Other); // Returns a chain
1718 if (RetVT != MVT::Other)
1719 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1721 Ops.push_back(Chain);
1722 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1723 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1724 Ops.push_back(InFlag);
1725 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1726 if (RetVT != MVT::Other)
1727 InFlag = Chain.getValue(1);
1729 std::vector<SDOperand> ResultVals;
1732 default: assert(0 && "Unknown value type to return!");
1733 case MVT::Other: break;
1735 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1736 ResultVals.push_back(Chain.getValue(0));
1737 NodeTys.push_back(MVT::i8);
1740 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1741 ResultVals.push_back(Chain.getValue(0));
1742 NodeTys.push_back(MVT::i16);
1745 if (Op.Val->getValueType(1) == MVT::i32) {
1746 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1747 ResultVals.push_back(Chain.getValue(0));
1748 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1749 Chain.getValue(2)).getValue(1);
1750 ResultVals.push_back(Chain.getValue(0));
1751 NodeTys.push_back(MVT::i32);
1753 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1754 ResultVals.push_back(Chain.getValue(0));
1756 NodeTys.push_back(MVT::i32);
1765 assert(0 && "Unknown value type to return!");
1767 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1768 ResultVals.push_back(Chain.getValue(0));
1769 NodeTys.push_back(RetVT);
1774 std::vector<MVT::ValueType> Tys;
1775 Tys.push_back(MVT::f64);
1776 Tys.push_back(MVT::Other);
1777 Tys.push_back(MVT::Flag);
1778 std::vector<SDOperand> Ops;
1779 Ops.push_back(Chain);
1780 Ops.push_back(InFlag);
1781 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
1782 &Ops[0], Ops.size());
1783 Chain = RetVal.getValue(1);
1784 InFlag = RetVal.getValue(2);
1786 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1787 // shouldn't be necessary except that RFP cannot be live across
1788 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1789 MachineFunction &MF = DAG.getMachineFunction();
1790 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1791 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1793 Tys.push_back(MVT::Other);
1795 Ops.push_back(Chain);
1796 Ops.push_back(RetVal);
1797 Ops.push_back(StackSlot);
1798 Ops.push_back(DAG.getValueType(RetVT));
1799 Ops.push_back(InFlag);
1800 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
1801 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
1802 Chain = RetVal.getValue(1);
1805 if (RetVT == MVT::f32 && !X86ScalarSSE)
1806 // FIXME: we would really like to remember that this FP_ROUND
1807 // operation is okay to eliminate if we allow excess FP precision.
1808 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1809 ResultVals.push_back(RetVal);
1810 NodeTys.push_back(RetVT);
1816 // If the function returns void, just return the chain.
1817 if (ResultVals.empty())
1820 // Otherwise, merge everything together with a MERGE_VALUES node.
1821 NodeTys.push_back(MVT::Other);
1822 ResultVals.push_back(Chain);
1823 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1824 &ResultVals[0], ResultVals.size());
1825 return Res.getValue(Op.ResNo);
1828 //===----------------------------------------------------------------------===//
1829 // StdCall Calling Convention implementation
1830 //===----------------------------------------------------------------------===//
1831 // StdCall calling convention seems to be standard for many Windows' API
1832 // routines and around. It differs from C calling convention just a little:
1833 // callee should clean up the stack, not caller. Symbols should be also
1834 // decorated in some fancy way :) It doesn't support any vector arguments.
1836 /// HowToPassStdCallCCArgument - Returns how an formal argument of the specified
1837 /// type should be passed. Returns the size of the stack slot
1839 HowToPassStdCallCCArgument(MVT::ValueType ObjectVT, unsigned &ObjSize) {
1841 default: assert(0 && "Unhandled argument type!");
1842 case MVT::i8: ObjSize = 1; break;
1843 case MVT::i16: ObjSize = 2; break;
1844 case MVT::i32: ObjSize = 4; break;
1845 case MVT::i64: ObjSize = 8; break;
1846 case MVT::f32: ObjSize = 4; break;
1847 case MVT::f64: ObjSize = 8; break;
1851 SDOperand X86TargetLowering::LowerStdCallCCArguments(SDOperand Op,
1852 SelectionDAG &DAG) {
1853 unsigned NumArgs = Op.Val->getNumValues() - 1;
1854 MachineFunction &MF = DAG.getMachineFunction();
1855 MachineFrameInfo *MFI = MF.getFrameInfo();
1856 SDOperand Root = Op.getOperand(0);
1857 std::vector<SDOperand> ArgValues;
1859 // Add DAG nodes to load the arguments... On entry to a function on the X86,
1860 // the stack frame looks like this:
1862 // [ESP] -- return address
1863 // [ESP + 4] -- first argument (leftmost lexically)
1864 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
1867 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1868 for (unsigned i = 0; i < NumArgs; ++i) {
1869 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1870 unsigned ArgIncrement = 4;
1871 unsigned ObjSize = 0;
1872 HowToPassStdCallCCArgument(ObjectVT, ObjSize);
1874 ArgIncrement = ObjSize;
1877 // Create the frame index object for this incoming parameter...
1878 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1879 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1880 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1881 ArgValues.push_back(ArgValue);
1882 ArgOffset += ArgIncrement; // Move on to the next argument...
1885 ArgValues.push_back(Root);
1887 // If the function takes variable number of arguments, make a frame index for
1888 // the start of the first vararg value... for expansion of llvm.va_start.
1889 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1891 BytesToPopOnReturn = 0; // Callee pops nothing.
1892 BytesCallerReserves = ArgOffset;
1893 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
1895 BytesToPopOnReturn = ArgOffset; // Callee pops everything..
1896 BytesCallerReserves = 0;
1898 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1899 ReturnAddrIndex = 0; // No return address slot generated yet.
1901 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
1903 // Return the new list of results.
1904 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1905 Op.Val->value_end());
1906 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1910 SDOperand X86TargetLowering::LowerStdCallCCCallTo(SDOperand Op,
1911 SelectionDAG &DAG) {
1912 SDOperand Chain = Op.getOperand(0);
1913 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1914 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1915 SDOperand Callee = Op.getOperand(4);
1916 MVT::ValueType RetVT= Op.Val->getValueType(0);
1917 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1919 // Count how many bytes are to be pushed on the stack.
1920 unsigned NumBytes = 0;
1921 for (unsigned i = 0; i != NumOps; ++i) {
1922 SDOperand Arg = Op.getOperand(5+2*i);
1924 switch (Arg.getValueType()) {
1925 default: assert(0 && "Unexpected ValueType for argument!");
1939 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1941 // Arguments go on the stack in reverse order, as specified by the ABI.
1942 unsigned ArgOffset = 0;
1943 std::vector<SDOperand> MemOpChains;
1944 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1945 for (unsigned i = 0; i != NumOps; ++i) {
1946 SDOperand Arg = Op.getOperand(5+2*i);
1948 switch (Arg.getValueType()) {
1949 default: assert(0 && "Unexpected ValueType for argument!");
1952 // Promote the integer to 32 bits. If the input type is signed use a
1953 // sign extend, otherwise use a zero extend.
1955 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
1956 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1957 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
1963 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1964 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1965 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1971 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1972 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1973 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1980 if (!MemOpChains.empty())
1981 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1982 &MemOpChains[0], MemOpChains.size());
1984 // If the callee is a GlobalAddress node (quite common, every direct call is)
1985 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1986 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1987 // We should use extra load for direct calls to dllimported functions
1988 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), true))
1989 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1990 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1991 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1993 std::vector<MVT::ValueType> NodeTys;
1994 NodeTys.push_back(MVT::Other); // Returns a chain
1995 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1996 std::vector<SDOperand> Ops;
1997 Ops.push_back(Chain);
1998 Ops.push_back(Callee);
2000 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
2001 NodeTys, &Ops[0], Ops.size());
2002 SDOperand InFlag = Chain.getValue(1);
2004 // Create the CALLSEQ_END node.
2005 unsigned NumBytesForCalleeToPush;
2008 NumBytesForCalleeToPush = 0;
2010 NumBytesForCalleeToPush = NumBytes;
2014 NodeTys.push_back(MVT::Other); // Returns a chain
2015 if (RetVT != MVT::Other)
2016 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2018 Ops.push_back(Chain);
2019 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
2020 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
2021 Ops.push_back(InFlag);
2022 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2023 if (RetVT != MVT::Other)
2024 InFlag = Chain.getValue(1);
2026 std::vector<SDOperand> ResultVals;
2029 default: assert(0 && "Unknown value type to return!");
2030 case MVT::Other: break;
2032 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
2033 ResultVals.push_back(Chain.getValue(0));
2034 NodeTys.push_back(MVT::i8);
2037 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
2038 ResultVals.push_back(Chain.getValue(0));
2039 NodeTys.push_back(MVT::i16);
2042 if (Op.Val->getValueType(1) == MVT::i32) {
2043 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
2044 ResultVals.push_back(Chain.getValue(0));
2045 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
2046 Chain.getValue(2)).getValue(1);
2047 ResultVals.push_back(Chain.getValue(0));
2048 NodeTys.push_back(MVT::i32);
2050 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
2051 ResultVals.push_back(Chain.getValue(0));
2053 NodeTys.push_back(MVT::i32);
2057 std::vector<MVT::ValueType> Tys;
2058 Tys.push_back(MVT::f64);
2059 Tys.push_back(MVT::Other);
2060 Tys.push_back(MVT::Flag);
2061 std::vector<SDOperand> Ops;
2062 Ops.push_back(Chain);
2063 Ops.push_back(InFlag);
2064 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
2065 &Ops[0], Ops.size());
2066 Chain = RetVal.getValue(1);
2067 InFlag = RetVal.getValue(2);
2069 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
2070 // shouldn't be necessary except that RFP cannot be live across
2071 // multiple blocks. When stackifier is fixed, they can be uncoupled.
2072 MachineFunction &MF = DAG.getMachineFunction();
2073 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2074 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2076 Tys.push_back(MVT::Other);
2078 Ops.push_back(Chain);
2079 Ops.push_back(RetVal);
2080 Ops.push_back(StackSlot);
2081 Ops.push_back(DAG.getValueType(RetVT));
2082 Ops.push_back(InFlag);
2083 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
2084 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
2085 Chain = RetVal.getValue(1);
2088 if (RetVT == MVT::f32 && !X86ScalarSSE)
2089 // FIXME: we would really like to remember that this FP_ROUND
2090 // operation is okay to eliminate if we allow excess FP precision.
2091 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
2092 ResultVals.push_back(RetVal);
2093 NodeTys.push_back(RetVT);
2098 // If the function returns void, just return the chain.
2099 if (ResultVals.empty())
2102 // Otherwise, merge everything together with a MERGE_VALUES node.
2103 NodeTys.push_back(MVT::Other);
2104 ResultVals.push_back(Chain);
2105 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
2106 &ResultVals[0], ResultVals.size());
2107 return Res.getValue(Op.ResNo);
2110 //===----------------------------------------------------------------------===//
2111 // FastCall Calling Convention implementation
2112 //===----------------------------------------------------------------------===//
2114 // The X86 'fastcall' calling convention passes up to two integer arguments in
2115 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
2116 // and requires that the callee pop its arguments off the stack (allowing proper
2117 // tail calls), and has the same return value conventions as C calling convs.
2119 // This calling convention always arranges for the callee pop value to be 8n+4
2120 // bytes, which is needed for tail recursion elimination and stack alignment
2124 /// HowToPassFastCallCCArgument - Returns how an formal argument of the
2125 /// specified type should be passed. If it is through stack, returns the size of
2126 /// the stack slot; if it is through integer register, returns the number of
2127 /// integer registers are needed.
2129 HowToPassFastCallCCArgument(MVT::ValueType ObjectVT,
2130 unsigned NumIntRegs,
2132 unsigned &ObjIntRegs)
2138 default: assert(0 && "Unhandled argument type!");
2158 if (NumIntRegs+2 <= 2) {
2160 } else if (NumIntRegs+1 <= 2) {
2175 X86TargetLowering::LowerFastCallCCArguments(SDOperand Op, SelectionDAG &DAG) {
2176 unsigned NumArgs = Op.Val->getNumValues()-1;
2177 MachineFunction &MF = DAG.getMachineFunction();
2178 MachineFrameInfo *MFI = MF.getFrameInfo();
2179 SDOperand Root = Op.getOperand(0);
2180 std::vector<SDOperand> ArgValues;
2182 // Add DAG nodes to load the arguments... On entry to a function the stack
2183 // frame looks like this:
2185 // [ESP] -- return address
2186 // [ESP + 4] -- first nonreg argument (leftmost lexically)
2187 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
2189 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
2191 // Keep track of the number of integer regs passed so far. This can be either
2192 // 0 (neither ECX or EDX used), 1 (ECX is used) or 2 (ECX and EDX are both
2194 unsigned NumIntRegs = 0;
2196 for (unsigned i = 0; i < NumArgs; ++i) {
2197 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
2198 unsigned ArgIncrement = 4;
2199 unsigned ObjSize = 0;
2200 unsigned ObjIntRegs = 0;
2202 HowToPassFastCallCCArgument(ObjectVT, NumIntRegs, ObjSize, ObjIntRegs);
2204 ArgIncrement = ObjSize;
2210 default: assert(0 && "Unhandled argument type!");
2212 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::CL,
2213 X86::GR8RegisterClass);
2214 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
2217 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::CX,
2218 X86::GR16RegisterClass);
2219 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
2222 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::ECX,
2223 X86::GR32RegisterClass);
2224 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2227 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::ECX,
2228 X86::GR32RegisterClass);
2229 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2230 if (ObjIntRegs == 2) {
2231 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
2232 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
2233 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
2238 NumIntRegs += ObjIntRegs;
2242 // Create the SelectionDAG nodes corresponding to a load from this
2244 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
2245 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
2246 if (ObjectVT == MVT::i64 && ObjIntRegs) {
2247 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
2249 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
2251 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
2252 ArgOffset += ArgIncrement; // Move on to the next argument.
2255 ArgValues.push_back(ArgValue);
2258 ArgValues.push_back(Root);
2260 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
2261 // arguments and the arguments after the retaddr has been pushed are aligned.
2262 if ((ArgOffset & 7) == 0)
2265 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
2266 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
2267 ReturnAddrIndex = 0; // No return address slot generated yet.
2268 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
2269 BytesCallerReserves = 0;
2271 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
2273 // Finally, inform the code generator which regs we return values in.
2274 switch (getValueType(MF.getFunction()->getReturnType())) {
2275 default: assert(0 && "Unknown type!");
2276 case MVT::isVoid: break;
2281 MF.addLiveOut(X86::ECX);
2284 MF.addLiveOut(X86::ECX);
2285 MF.addLiveOut(X86::EDX);
2289 MF.addLiveOut(X86::ST0);
2293 // Return the new list of results.
2294 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
2295 Op.Val->value_end());
2296 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
2299 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
2300 if (ReturnAddrIndex == 0) {
2301 // Set up a frame object for the return address.
2302 MachineFunction &MF = DAG.getMachineFunction();
2303 if (Subtarget->is64Bit())
2304 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2306 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
2309 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2314 std::pair<SDOperand, SDOperand> X86TargetLowering::
2315 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
2316 SelectionDAG &DAG) {
2318 if (Depth) // Depths > 0 not supported yet!
2319 Result = DAG.getConstant(0, getPointerTy());
2321 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
2322 if (!isFrameAddress)
2323 // Just load the return address
2324 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI,
2327 Result = DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
2328 DAG.getConstant(4, getPointerTy()));
2330 return std::make_pair(Result, Chain);
2333 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2334 /// specific condition code. It returns a false if it cannot do a direct
2335 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2337 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2338 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2339 SelectionDAG &DAG) {
2340 X86CC = X86::COND_INVALID;
2342 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2343 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2344 // X > -1 -> X == 0, jump !sign.
2345 RHS = DAG.getConstant(0, RHS.getValueType());
2346 X86CC = X86::COND_NS;
2348 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2349 // X < 0 -> X == 0, jump on sign.
2350 X86CC = X86::COND_S;
2355 switch (SetCCOpcode) {
2357 case ISD::SETEQ: X86CC = X86::COND_E; break;
2358 case ISD::SETGT: X86CC = X86::COND_G; break;
2359 case ISD::SETGE: X86CC = X86::COND_GE; break;
2360 case ISD::SETLT: X86CC = X86::COND_L; break;
2361 case ISD::SETLE: X86CC = X86::COND_LE; break;
2362 case ISD::SETNE: X86CC = X86::COND_NE; break;
2363 case ISD::SETULT: X86CC = X86::COND_B; break;
2364 case ISD::SETUGT: X86CC = X86::COND_A; break;
2365 case ISD::SETULE: X86CC = X86::COND_BE; break;
2366 case ISD::SETUGE: X86CC = X86::COND_AE; break;
2369 // On a floating point condition, the flags are set as follows:
2371 // 0 | 0 | 0 | X > Y
2372 // 0 | 0 | 1 | X < Y
2373 // 1 | 0 | 0 | X == Y
2374 // 1 | 1 | 1 | unordered
2376 switch (SetCCOpcode) {
2379 case ISD::SETEQ: X86CC = X86::COND_E; break;
2380 case ISD::SETOLT: Flip = true; // Fallthrough
2382 case ISD::SETGT: X86CC = X86::COND_A; break;
2383 case ISD::SETOLE: Flip = true; // Fallthrough
2385 case ISD::SETGE: X86CC = X86::COND_AE; break;
2386 case ISD::SETUGT: Flip = true; // Fallthrough
2388 case ISD::SETLT: X86CC = X86::COND_B; break;
2389 case ISD::SETUGE: Flip = true; // Fallthrough
2391 case ISD::SETLE: X86CC = X86::COND_BE; break;
2393 case ISD::SETNE: X86CC = X86::COND_NE; break;
2394 case ISD::SETUO: X86CC = X86::COND_P; break;
2395 case ISD::SETO: X86CC = X86::COND_NP; break;
2398 std::swap(LHS, RHS);
2401 return X86CC != X86::COND_INVALID;
2404 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
2405 /// code. Current x86 isa includes the following FP cmov instructions:
2406 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2407 static bool hasFPCMov(unsigned X86CC) {
2423 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2424 /// true if Op is undef or if its value falls within the specified range (L, H].
2425 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2426 if (Op.getOpcode() == ISD::UNDEF)
2429 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2430 return (Val >= Low && Val < Hi);
2433 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2434 /// true if Op is undef or if its value equal to the specified value.
2435 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2436 if (Op.getOpcode() == ISD::UNDEF)
2438 return cast<ConstantSDNode>(Op)->getValue() == Val;
2441 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2442 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
2443 bool X86::isPSHUFDMask(SDNode *N) {
2444 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2446 if (N->getNumOperands() != 4)
2449 // Check if the value doesn't reference the second vector.
2450 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2451 SDOperand Arg = N->getOperand(i);
2452 if (Arg.getOpcode() == ISD::UNDEF) continue;
2453 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2454 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
2461 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2462 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2463 bool X86::isPSHUFHWMask(SDNode *N) {
2464 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2466 if (N->getNumOperands() != 8)
2469 // Lower quadword copied in order.
2470 for (unsigned i = 0; i != 4; ++i) {
2471 SDOperand Arg = N->getOperand(i);
2472 if (Arg.getOpcode() == ISD::UNDEF) continue;
2473 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2474 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2478 // Upper quadword shuffled.
2479 for (unsigned i = 4; i != 8; ++i) {
2480 SDOperand Arg = N->getOperand(i);
2481 if (Arg.getOpcode() == ISD::UNDEF) continue;
2482 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2483 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2484 if (Val < 4 || Val > 7)
2491 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2492 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2493 bool X86::isPSHUFLWMask(SDNode *N) {
2494 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2496 if (N->getNumOperands() != 8)
2499 // Upper quadword copied in order.
2500 for (unsigned i = 4; i != 8; ++i)
2501 if (!isUndefOrEqual(N->getOperand(i), i))
2504 // Lower quadword shuffled.
2505 for (unsigned i = 0; i != 4; ++i)
2506 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2512 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2513 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
2514 static bool isSHUFPMask(std::vector<SDOperand> &N) {
2515 unsigned NumElems = N.size();
2516 if (NumElems != 2 && NumElems != 4) return false;
2518 unsigned Half = NumElems / 2;
2519 for (unsigned i = 0; i < Half; ++i)
2520 if (!isUndefOrInRange(N[i], 0, NumElems))
2522 for (unsigned i = Half; i < NumElems; ++i)
2523 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
2529 bool X86::isSHUFPMask(SDNode *N) {
2530 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2531 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2532 return ::isSHUFPMask(Ops);
2535 /// isCommutedSHUFP - Returns true if the shuffle mask is except
2536 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2537 /// half elements to come from vector 1 (which would equal the dest.) and
2538 /// the upper half to come from vector 2.
2539 static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
2540 unsigned NumElems = Ops.size();
2541 if (NumElems != 2 && NumElems != 4) return false;
2543 unsigned Half = NumElems / 2;
2544 for (unsigned i = 0; i < Half; ++i)
2545 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
2547 for (unsigned i = Half; i < NumElems; ++i)
2548 if (!isUndefOrInRange(Ops[i], 0, NumElems))
2553 static bool isCommutedSHUFP(SDNode *N) {
2554 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2555 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2556 return isCommutedSHUFP(Ops);
2559 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2560 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2561 bool X86::isMOVHLPSMask(SDNode *N) {
2562 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2564 if (N->getNumOperands() != 4)
2567 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2568 return isUndefOrEqual(N->getOperand(0), 6) &&
2569 isUndefOrEqual(N->getOperand(1), 7) &&
2570 isUndefOrEqual(N->getOperand(2), 2) &&
2571 isUndefOrEqual(N->getOperand(3), 3);
2574 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2575 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2577 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2578 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2580 if (N->getNumOperands() != 4)
2583 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2584 return isUndefOrEqual(N->getOperand(0), 2) &&
2585 isUndefOrEqual(N->getOperand(1), 3) &&
2586 isUndefOrEqual(N->getOperand(2), 2) &&
2587 isUndefOrEqual(N->getOperand(3), 3);
2590 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2591 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2592 bool X86::isMOVLPMask(SDNode *N) {
2593 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2595 unsigned NumElems = N->getNumOperands();
2596 if (NumElems != 2 && NumElems != 4)
2599 for (unsigned i = 0; i < NumElems/2; ++i)
2600 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2603 for (unsigned i = NumElems/2; i < NumElems; ++i)
2604 if (!isUndefOrEqual(N->getOperand(i), i))
2610 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2611 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2613 bool X86::isMOVHPMask(SDNode *N) {
2614 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2616 unsigned NumElems = N->getNumOperands();
2617 if (NumElems != 2 && NumElems != 4)
2620 for (unsigned i = 0; i < NumElems/2; ++i)
2621 if (!isUndefOrEqual(N->getOperand(i), i))
2624 for (unsigned i = 0; i < NumElems/2; ++i) {
2625 SDOperand Arg = N->getOperand(i + NumElems/2);
2626 if (!isUndefOrEqual(Arg, i + NumElems))
2633 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2634 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
2635 bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2636 unsigned NumElems = N.size();
2637 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2640 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2641 SDOperand BitI = N[i];
2642 SDOperand BitI1 = N[i+1];
2643 if (!isUndefOrEqual(BitI, j))
2646 if (isUndefOrEqual(BitI1, NumElems))
2649 if (!isUndefOrEqual(BitI1, j + NumElems))
2657 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2658 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2659 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2660 return ::isUNPCKLMask(Ops, V2IsSplat);
2663 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2664 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
2665 bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2666 unsigned NumElems = N.size();
2667 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2670 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2671 SDOperand BitI = N[i];
2672 SDOperand BitI1 = N[i+1];
2673 if (!isUndefOrEqual(BitI, j + NumElems/2))
2676 if (isUndefOrEqual(BitI1, NumElems))
2679 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
2687 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2688 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2689 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2690 return ::isUNPCKHMask(Ops, V2IsSplat);
2693 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2694 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2696 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2697 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2699 unsigned NumElems = N->getNumOperands();
2700 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
2703 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2704 SDOperand BitI = N->getOperand(i);
2705 SDOperand BitI1 = N->getOperand(i+1);
2707 if (!isUndefOrEqual(BitI, j))
2709 if (!isUndefOrEqual(BitI1, j))
2716 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2717 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2718 /// MOVSD, and MOVD, i.e. setting the lowest element.
2719 static bool isMOVLMask(std::vector<SDOperand> &N) {
2720 unsigned NumElems = N.size();
2721 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2724 if (!isUndefOrEqual(N[0], NumElems))
2727 for (unsigned i = 1; i < NumElems; ++i) {
2728 SDOperand Arg = N[i];
2729 if (!isUndefOrEqual(Arg, i))
2736 bool X86::isMOVLMask(SDNode *N) {
2737 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2738 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2739 return ::isMOVLMask(Ops);
2742 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2743 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2744 /// element of vector 2 and the other elements to come from vector 1 in order.
2745 static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false,
2746 bool V2IsUndef = false) {
2747 unsigned NumElems = Ops.size();
2748 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2751 if (!isUndefOrEqual(Ops[0], 0))
2754 for (unsigned i = 1; i < NumElems; ++i) {
2755 SDOperand Arg = Ops[i];
2756 if (!(isUndefOrEqual(Arg, i+NumElems) ||
2757 (V2IsUndef && isUndefOrInRange(Arg, NumElems, NumElems*2)) ||
2758 (V2IsSplat && isUndefOrEqual(Arg, NumElems))))
2765 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2766 bool V2IsUndef = false) {
2767 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2768 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2769 return isCommutedMOVL(Ops, V2IsSplat, V2IsUndef);
2772 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2773 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2774 bool X86::isMOVSHDUPMask(SDNode *N) {
2775 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2777 if (N->getNumOperands() != 4)
2780 // Expect 1, 1, 3, 3
2781 for (unsigned i = 0; i < 2; ++i) {
2782 SDOperand Arg = N->getOperand(i);
2783 if (Arg.getOpcode() == ISD::UNDEF) continue;
2784 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2785 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2786 if (Val != 1) return false;
2790 for (unsigned i = 2; i < 4; ++i) {
2791 SDOperand Arg = N->getOperand(i);
2792 if (Arg.getOpcode() == ISD::UNDEF) continue;
2793 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2794 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2795 if (Val != 3) return false;
2799 // Don't use movshdup if it can be done with a shufps.
2803 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2804 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2805 bool X86::isMOVSLDUPMask(SDNode *N) {
2806 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2808 if (N->getNumOperands() != 4)
2811 // Expect 0, 0, 2, 2
2812 for (unsigned i = 0; i < 2; ++i) {
2813 SDOperand Arg = N->getOperand(i);
2814 if (Arg.getOpcode() == ISD::UNDEF) continue;
2815 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2816 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2817 if (Val != 0) return false;
2821 for (unsigned i = 2; i < 4; ++i) {
2822 SDOperand Arg = N->getOperand(i);
2823 if (Arg.getOpcode() == ISD::UNDEF) continue;
2824 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2825 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2826 if (Val != 2) return false;
2830 // Don't use movshdup if it can be done with a shufps.
2834 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2835 /// a splat of a single element.
2836 static bool isSplatMask(SDNode *N) {
2837 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2839 // This is a splat operation if each element of the permute is the same, and
2840 // if the value doesn't reference the second vector.
2841 unsigned NumElems = N->getNumOperands();
2842 SDOperand ElementBase;
2844 for (; i != NumElems; ++i) {
2845 SDOperand Elt = N->getOperand(i);
2846 if (isa<ConstantSDNode>(Elt)) {
2852 if (!ElementBase.Val)
2855 for (; i != NumElems; ++i) {
2856 SDOperand Arg = N->getOperand(i);
2857 if (Arg.getOpcode() == ISD::UNDEF) continue;
2858 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2859 if (Arg != ElementBase) return false;
2862 // Make sure it is a splat of the first vector operand.
2863 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2866 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2867 /// a splat of a single element and it's a 2 or 4 element mask.
2868 bool X86::isSplatMask(SDNode *N) {
2869 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2871 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2872 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2874 return ::isSplatMask(N);
2877 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2878 /// specifies a splat of zero element.
2879 bool X86::isSplatLoMask(SDNode *N) {
2880 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2882 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2883 if (!isUndefOrEqual(N->getOperand(i), 0))
2888 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2889 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2891 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2892 unsigned NumOperands = N->getNumOperands();
2893 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2895 for (unsigned i = 0; i < NumOperands; ++i) {
2897 SDOperand Arg = N->getOperand(NumOperands-i-1);
2898 if (Arg.getOpcode() != ISD::UNDEF)
2899 Val = cast<ConstantSDNode>(Arg)->getValue();
2900 if (Val >= NumOperands) Val -= NumOperands;
2902 if (i != NumOperands - 1)
2909 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2910 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2912 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2914 // 8 nodes, but we only care about the last 4.
2915 for (unsigned i = 7; i >= 4; --i) {
2917 SDOperand Arg = N->getOperand(i);
2918 if (Arg.getOpcode() != ISD::UNDEF)
2919 Val = cast<ConstantSDNode>(Arg)->getValue();
2928 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2929 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2931 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2933 // 8 nodes, but we only care about the first 4.
2934 for (int i = 3; i >= 0; --i) {
2936 SDOperand Arg = N->getOperand(i);
2937 if (Arg.getOpcode() != ISD::UNDEF)
2938 Val = cast<ConstantSDNode>(Arg)->getValue();
2947 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2948 /// specifies a 8 element shuffle that can be broken into a pair of
2949 /// PSHUFHW and PSHUFLW.
2950 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2951 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2953 if (N->getNumOperands() != 8)
2956 // Lower quadword shuffled.
2957 for (unsigned i = 0; i != 4; ++i) {
2958 SDOperand Arg = N->getOperand(i);
2959 if (Arg.getOpcode() == ISD::UNDEF) continue;
2960 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2961 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2966 // Upper quadword shuffled.
2967 for (unsigned i = 4; i != 8; ++i) {
2968 SDOperand Arg = N->getOperand(i);
2969 if (Arg.getOpcode() == ISD::UNDEF) continue;
2970 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2971 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2972 if (Val < 4 || Val > 7)
2979 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2980 /// values in ther permute mask.
2981 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2982 SDOperand &V2, SDOperand &Mask,
2983 SelectionDAG &DAG) {
2984 MVT::ValueType VT = Op.getValueType();
2985 MVT::ValueType MaskVT = Mask.getValueType();
2986 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2987 unsigned NumElems = Mask.getNumOperands();
2988 std::vector<SDOperand> MaskVec;
2990 for (unsigned i = 0; i != NumElems; ++i) {
2991 SDOperand Arg = Mask.getOperand(i);
2992 if (Arg.getOpcode() == ISD::UNDEF) {
2993 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2996 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2997 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2999 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
3001 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
3005 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3006 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3009 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3010 /// match movhlps. The lower half elements should come from upper half of
3011 /// V1 (and in order), and the upper half elements should come from the upper
3012 /// half of V2 (and in order).
3013 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
3014 unsigned NumElems = Mask->getNumOperands();
3017 for (unsigned i = 0, e = 2; i != e; ++i)
3018 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
3020 for (unsigned i = 2; i != 4; ++i)
3021 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
3026 /// isScalarLoadToVector - Returns true if the node is a scalar load that
3027 /// is promoted to a vector.
3028 static inline bool isScalarLoadToVector(SDNode *N) {
3029 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
3030 N = N->getOperand(0).Val;
3031 return ISD::isNON_EXTLoad(N);
3036 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3037 /// match movlp{s|d}. The lower half elements should come from lower half of
3038 /// V1 (and in order), and the upper half elements should come from the upper
3039 /// half of V2 (and in order). And since V1 will become the source of the
3040 /// MOVLP, it must be either a vector load or a scalar load to vector.
3041 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
3042 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
3044 // Is V2 is a vector load, don't do this transformation. We will try to use
3045 // load folding shufps op.
3046 if (ISD::isNON_EXTLoad(V2))
3049 unsigned NumElems = Mask->getNumOperands();
3050 if (NumElems != 2 && NumElems != 4)
3052 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3053 if (!isUndefOrEqual(Mask->getOperand(i), i))
3055 for (unsigned i = NumElems/2; i != NumElems; ++i)
3056 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
3061 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3063 static bool isSplatVector(SDNode *N) {
3064 if (N->getOpcode() != ISD::BUILD_VECTOR)
3067 SDOperand SplatValue = N->getOperand(0);
3068 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3069 if (N->getOperand(i) != SplatValue)
3074 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
3076 static bool isUndefShuffle(SDNode *N) {
3077 if (N->getOpcode() != ISD::BUILD_VECTOR)
3080 SDOperand V1 = N->getOperand(0);
3081 SDOperand V2 = N->getOperand(1);
3082 SDOperand Mask = N->getOperand(2);
3083 unsigned NumElems = Mask.getNumOperands();
3084 for (unsigned i = 0; i != NumElems; ++i) {
3085 SDOperand Arg = Mask.getOperand(i);
3086 if (Arg.getOpcode() != ISD::UNDEF) {
3087 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3088 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
3090 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
3097 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3098 /// that point to V2 points to its first element.
3099 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
3100 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
3102 bool Changed = false;
3103 std::vector<SDOperand> MaskVec;
3104 unsigned NumElems = Mask.getNumOperands();
3105 for (unsigned i = 0; i != NumElems; ++i) {
3106 SDOperand Arg = Mask.getOperand(i);
3107 if (Arg.getOpcode() != ISD::UNDEF) {
3108 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3109 if (Val > NumElems) {
3110 Arg = DAG.getConstant(NumElems, Arg.getValueType());
3114 MaskVec.push_back(Arg);
3118 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
3119 &MaskVec[0], MaskVec.size());
3123 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3124 /// operation of specified width.
3125 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
3126 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3127 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3129 std::vector<SDOperand> MaskVec;
3130 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
3131 for (unsigned i = 1; i != NumElems; ++i)
3132 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3133 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3136 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
3137 /// of specified width.
3138 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
3139 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3140 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3141 std::vector<SDOperand> MaskVec;
3142 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
3143 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3144 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
3146 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3149 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3150 /// of specified width.
3151 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
3152 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3153 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3154 unsigned Half = NumElems/2;
3155 std::vector<SDOperand> MaskVec;
3156 for (unsigned i = 0; i != Half; ++i) {
3157 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3158 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3160 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3163 /// getZeroVector - Returns a vector of specified type with all zero elements.
3165 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
3166 assert(MVT::isVector(VT) && "Expected a vector type");
3167 unsigned NumElems = getVectorNumElements(VT);
3168 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
3169 bool isFP = MVT::isFloatingPoint(EVT);
3170 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
3171 std::vector<SDOperand> ZeroVec(NumElems, Zero);
3172 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
3175 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
3177 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
3178 SDOperand V1 = Op.getOperand(0);
3179 SDOperand Mask = Op.getOperand(2);
3180 MVT::ValueType VT = Op.getValueType();
3181 unsigned NumElems = Mask.getNumOperands();
3182 Mask = getUnpacklMask(NumElems, DAG);
3183 while (NumElems != 4) {
3184 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3187 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3189 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3190 Mask = getZeroVector(MaskVT, DAG);
3191 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
3192 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
3193 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3196 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
3198 static inline bool isZeroNode(SDOperand Elt) {
3199 return ((isa<ConstantSDNode>(Elt) &&
3200 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
3201 (isa<ConstantFPSDNode>(Elt) &&
3202 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
3205 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
3206 /// vector and zero or undef vector.
3207 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
3208 unsigned NumElems, unsigned Idx,
3209 bool isZero, SelectionDAG &DAG) {
3210 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
3211 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3212 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
3213 SDOperand Zero = DAG.getConstant(0, EVT);
3214 std::vector<SDOperand> MaskVec(NumElems, Zero);
3215 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
3216 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3217 &MaskVec[0], MaskVec.size());
3218 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3221 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3223 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3224 unsigned NumNonZero, unsigned NumZero,
3225 SelectionDAG &DAG, TargetLowering &TLI) {
3231 for (unsigned i = 0; i < 16; ++i) {
3232 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3233 if (ThisIsNonZero && First) {
3235 V = getZeroVector(MVT::v8i16, DAG);
3237 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3242 SDOperand ThisElt(0, 0), LastElt(0, 0);
3243 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3244 if (LastIsNonZero) {
3245 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3247 if (ThisIsNonZero) {
3248 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3249 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3250 ThisElt, DAG.getConstant(8, MVT::i8));
3252 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3257 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
3258 DAG.getConstant(i/2, TLI.getPointerTy()));
3262 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3265 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
3267 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3268 unsigned NumNonZero, unsigned NumZero,
3269 SelectionDAG &DAG, TargetLowering &TLI) {
3275 for (unsigned i = 0; i < 8; ++i) {
3276 bool isNonZero = (NonZeros & (1 << i)) != 0;
3280 V = getZeroVector(MVT::v8i16, DAG);
3282 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3285 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
3286 DAG.getConstant(i, TLI.getPointerTy()));
3294 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3295 // All zero's are handled with pxor.
3296 if (ISD::isBuildVectorAllZeros(Op.Val))
3299 // All one's are handled with pcmpeqd.
3300 if (ISD::isBuildVectorAllOnes(Op.Val))
3303 MVT::ValueType VT = Op.getValueType();
3304 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
3305 unsigned EVTBits = MVT::getSizeInBits(EVT);
3307 unsigned NumElems = Op.getNumOperands();
3308 unsigned NumZero = 0;
3309 unsigned NumNonZero = 0;
3310 unsigned NonZeros = 0;
3311 std::set<SDOperand> Values;
3312 for (unsigned i = 0; i < NumElems; ++i) {
3313 SDOperand Elt = Op.getOperand(i);
3314 if (Elt.getOpcode() != ISD::UNDEF) {
3316 if (isZeroNode(Elt))
3319 NonZeros |= (1 << i);
3325 if (NumNonZero == 0)
3326 // Must be a mix of zero and undef. Return a zero vector.
3327 return getZeroVector(VT, DAG);
3329 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3330 if (Values.size() == 1)
3333 // Special case for single non-zero element.
3334 if (NumNonZero == 1) {
3335 unsigned Idx = CountTrailingZeros_32(NonZeros);
3336 SDOperand Item = Op.getOperand(Idx);
3337 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3339 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3340 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3343 if (EVTBits == 32) {
3344 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3345 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3347 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3348 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
3349 std::vector<SDOperand> MaskVec;
3350 for (unsigned i = 0; i < NumElems; i++)
3351 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3352 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3353 &MaskVec[0], MaskVec.size());
3354 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3355 DAG.getNode(ISD::UNDEF, VT), Mask);
3359 // Let legalizer expand 2-wide build_vector's.
3363 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3365 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3367 if (V.Val) return V;
3370 if (EVTBits == 16) {
3371 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3373 if (V.Val) return V;
3376 // If element VT is == 32 bits, turn it into a number of shuffles.
3377 std::vector<SDOperand> V(NumElems);
3378 if (NumElems == 4 && NumZero > 0) {
3379 for (unsigned i = 0; i < 4; ++i) {
3380 bool isZero = !(NonZeros & (1 << i));
3382 V[i] = getZeroVector(VT, DAG);
3384 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3387 for (unsigned i = 0; i < 2; ++i) {
3388 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3391 V[i] = V[i*2]; // Must be a zero vector.
3394 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3395 getMOVLMask(NumElems, DAG));
3398 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3399 getMOVLMask(NumElems, DAG));
3402 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3403 getUnpacklMask(NumElems, DAG));
3408 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3409 // clears the upper bits.
3410 // FIXME: we can do the same for v4f32 case when we know both parts of
3411 // the lower half come from scalar_to_vector (loadf32). We should do
3412 // that in post legalizer dag combiner with target specific hooks.
3413 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3415 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3416 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
3417 std::vector<SDOperand> MaskVec;
3418 bool Reverse = (NonZeros & 0x3) == 2;
3419 for (unsigned i = 0; i < 2; ++i)
3421 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3423 MaskVec.push_back(DAG.getConstant(i, EVT));
3424 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3425 for (unsigned i = 0; i < 2; ++i)
3427 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3429 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3430 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3431 &MaskVec[0], MaskVec.size());
3432 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3435 if (Values.size() > 2) {
3436 // Expand into a number of unpckl*.
3438 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3439 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3440 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3441 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3442 for (unsigned i = 0; i < NumElems; ++i)
3443 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3445 while (NumElems != 0) {
3446 for (unsigned i = 0; i < NumElems; ++i)
3447 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3458 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3459 SDOperand V1 = Op.getOperand(0);
3460 SDOperand V2 = Op.getOperand(1);
3461 SDOperand PermMask = Op.getOperand(2);
3462 MVT::ValueType VT = Op.getValueType();
3463 unsigned NumElems = PermMask.getNumOperands();
3464 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3465 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3466 bool V1IsSplat = false;
3467 bool V2IsSplat = false;
3469 if (isUndefShuffle(Op.Val))
3470 return DAG.getNode(ISD::UNDEF, VT);
3472 if (isSplatMask(PermMask.Val)) {
3473 if (NumElems <= 4) return Op;
3474 // Promote it to a v4i32 splat.
3475 return PromoteSplat(Op, DAG);
3478 if (X86::isMOVLMask(PermMask.Val))
3479 return (V1IsUndef) ? V2 : Op;
3481 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3482 X86::isMOVSLDUPMask(PermMask.Val) ||
3483 X86::isMOVHLPSMask(PermMask.Val) ||
3484 X86::isMOVHPMask(PermMask.Val) ||
3485 X86::isMOVLPMask(PermMask.Val))
3488 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3489 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3490 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3492 bool Commuted = false;
3493 V1IsSplat = isSplatVector(V1.Val);
3494 V2IsSplat = isSplatVector(V2.Val);
3495 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3496 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3497 std::swap(V1IsSplat, V2IsSplat);
3498 std::swap(V1IsUndef, V2IsUndef);
3502 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3503 if (V2IsUndef) return V1;
3504 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3506 // V2 is a splat, so the mask may be malformed. That is, it may point
3507 // to any V2 element. The instruction selectior won't like this. Get
3508 // a corrected mask and commute to form a proper MOVS{S|D}.
3509 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3510 if (NewMask.Val != PermMask.Val)
3511 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3516 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3517 X86::isUNPCKLMask(PermMask.Val) ||
3518 X86::isUNPCKHMask(PermMask.Val))
3522 // Normalize mask so all entries that point to V2 points to its first
3523 // element then try to match unpck{h|l} again. If match, return a
3524 // new vector_shuffle with the corrected mask.
3525 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3526 if (NewMask.Val != PermMask.Val) {
3527 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3528 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3529 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3530 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3531 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3532 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3537 // Normalize the node to match x86 shuffle ops if needed
3538 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3539 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3542 // Commute is back and try unpck* again.
3543 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3544 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3545 X86::isUNPCKLMask(PermMask.Val) ||
3546 X86::isUNPCKHMask(PermMask.Val))
3550 // If VT is integer, try PSHUF* first, then SHUFP*.
3551 if (MVT::isInteger(VT)) {
3552 if (X86::isPSHUFDMask(PermMask.Val) ||
3553 X86::isPSHUFHWMask(PermMask.Val) ||
3554 X86::isPSHUFLWMask(PermMask.Val)) {
3555 if (V2.getOpcode() != ISD::UNDEF)
3556 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3557 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3561 if (X86::isSHUFPMask(PermMask.Val))
3564 // Handle v8i16 shuffle high / low shuffle node pair.
3565 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3566 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3567 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3568 std::vector<SDOperand> MaskVec;
3569 for (unsigned i = 0; i != 4; ++i)
3570 MaskVec.push_back(PermMask.getOperand(i));
3571 for (unsigned i = 4; i != 8; ++i)
3572 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3573 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3574 &MaskVec[0], MaskVec.size());
3575 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3577 for (unsigned i = 0; i != 4; ++i)
3578 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3579 for (unsigned i = 4; i != 8; ++i)
3580 MaskVec.push_back(PermMask.getOperand(i));
3581 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
3582 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3585 // Floating point cases in the other order.
3586 if (X86::isSHUFPMask(PermMask.Val))
3588 if (X86::isPSHUFDMask(PermMask.Val) ||
3589 X86::isPSHUFHWMask(PermMask.Val) ||
3590 X86::isPSHUFLWMask(PermMask.Val)) {
3591 if (V2.getOpcode() != ISD::UNDEF)
3592 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3593 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3598 if (NumElems == 4) {
3599 MVT::ValueType MaskVT = PermMask.getValueType();
3600 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
3601 std::vector<std::pair<int, int> > Locs;
3602 Locs.reserve(NumElems);
3603 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3604 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3607 // If no more than two elements come from either vector. This can be
3608 // implemented with two shuffles. First shuffle gather the elements.
3609 // The second shuffle, which takes the first shuffle as both of its
3610 // vector operands, put the elements into the right order.
3611 for (unsigned i = 0; i != NumElems; ++i) {
3612 SDOperand Elt = PermMask.getOperand(i);
3613 if (Elt.getOpcode() == ISD::UNDEF) {
3614 Locs[i] = std::make_pair(-1, -1);
3616 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3617 if (Val < NumElems) {
3618 Locs[i] = std::make_pair(0, NumLo);
3622 Locs[i] = std::make_pair(1, NumHi);
3623 if (2+NumHi < NumElems)
3624 Mask1[2+NumHi] = Elt;
3629 if (NumLo <= 2 && NumHi <= 2) {
3630 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3631 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3632 &Mask1[0], Mask1.size()));
3633 for (unsigned i = 0; i != NumElems; ++i) {
3634 if (Locs[i].first == -1)
3637 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3638 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3639 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3643 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3644 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3645 &Mask2[0], Mask2.size()));
3648 // Break it into (shuffle shuffle_hi, shuffle_lo).
3650 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3651 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3652 std::vector<SDOperand> *MaskPtr = &LoMask;
3653 unsigned MaskIdx = 0;
3655 unsigned HiIdx = NumElems/2;
3656 for (unsigned i = 0; i != NumElems; ++i) {
3657 if (i == NumElems/2) {
3663 SDOperand Elt = PermMask.getOperand(i);
3664 if (Elt.getOpcode() == ISD::UNDEF) {
3665 Locs[i] = std::make_pair(-1, -1);
3666 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3667 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3668 (*MaskPtr)[LoIdx] = Elt;
3671 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3672 (*MaskPtr)[HiIdx] = Elt;
3677 SDOperand LoShuffle =
3678 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3679 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3680 &LoMask[0], LoMask.size()));
3681 SDOperand HiShuffle =
3682 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3683 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3684 &HiMask[0], HiMask.size()));
3685 std::vector<SDOperand> MaskOps;
3686 for (unsigned i = 0; i != NumElems; ++i) {
3687 if (Locs[i].first == -1) {
3688 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3690 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3691 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3694 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3695 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3696 &MaskOps[0], MaskOps.size()));
3703 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3704 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3707 MVT::ValueType VT = Op.getValueType();
3708 // TODO: handle v16i8.
3709 if (MVT::getSizeInBits(VT) == 16) {
3710 // Transform it so it match pextrw which produces a 32-bit result.
3711 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3712 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3713 Op.getOperand(0), Op.getOperand(1));
3714 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3715 DAG.getValueType(VT));
3716 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3717 } else if (MVT::getSizeInBits(VT) == 32) {
3718 SDOperand Vec = Op.getOperand(0);
3719 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3722 // SHUFPS the element to the lowest double word, then movss.
3723 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3724 std::vector<SDOperand> IdxVec;
3725 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
3726 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3727 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3728 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3729 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3730 &IdxVec[0], IdxVec.size());
3731 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3732 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3733 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3734 DAG.getConstant(0, getPointerTy()));
3735 } else if (MVT::getSizeInBits(VT) == 64) {
3736 SDOperand Vec = Op.getOperand(0);
3737 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3741 // UNPCKHPD the element to the lowest double word, then movsd.
3742 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3743 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3744 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3745 std::vector<SDOperand> IdxVec;
3746 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
3747 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3748 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3749 &IdxVec[0], IdxVec.size());
3750 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3751 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3752 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3753 DAG.getConstant(0, getPointerTy()));
3760 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3761 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3762 // as its second argument.
3763 MVT::ValueType VT = Op.getValueType();
3764 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
3765 SDOperand N0 = Op.getOperand(0);
3766 SDOperand N1 = Op.getOperand(1);
3767 SDOperand N2 = Op.getOperand(2);
3768 if (MVT::getSizeInBits(BaseVT) == 16) {
3769 if (N1.getValueType() != MVT::i32)
3770 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3771 if (N2.getValueType() != MVT::i32)
3772 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
3773 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3774 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3775 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3778 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3779 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3780 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3781 std::vector<SDOperand> MaskVec;
3782 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3783 for (unsigned i = 1; i <= 3; ++i)
3784 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3785 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3786 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3787 &MaskVec[0], MaskVec.size()));
3789 // Use two pinsrw instructions to insert a 32 bit value.
3791 if (MVT::isFloatingPoint(N1.getValueType())) {
3792 if (ISD::isNON_EXTLoad(N1.Val)) {
3793 // Just load directly from f32mem to GR32.
3794 LoadSDNode *LD = cast<LoadSDNode>(N1);
3795 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3796 LD->getSrcValue(), LD->getSrcValueOffset());
3798 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3799 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3800 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3801 DAG.getConstant(0, getPointerTy()));
3804 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3805 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3806 DAG.getConstant(Idx, getPointerTy()));
3807 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3808 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3809 DAG.getConstant(Idx+1, getPointerTy()));
3810 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3818 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3819 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3820 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3823 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3824 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3825 // one of the above mentioned nodes. It has to be wrapped because otherwise
3826 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3827 // be used to form addressing mode. These wrapped nodes will be selected
3830 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3831 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3832 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3834 CP->getAlignment());
3835 // Use X86ISD::WrapperRIP if we are in X86-64 small / medium PIC mode.
3836 TargetMachine &tm = getTargetMachine();
3837 unsigned WrapperOpcode = (Subtarget->is64Bit() &&
3838 (tm.getCodeModel() == CodeModel::Small ||
3839 tm.getCodeModel() == CodeModel::Medium) &&
3840 tm.getRelocationModel() == Reloc::PIC_)
3841 ? X86ISD::WrapperRIP : X86ISD::Wrapper;
3842 Result = DAG.getNode(WrapperOpcode, getPointerTy(), Result);
3843 if (Subtarget->isTargetDarwin()) {
3844 // With PIC, the address is actually $g + Offset.
3845 if (!Subtarget->is64Bit() &&
3846 getTargetMachine().getRelocationModel() == Reloc::PIC_)
3847 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3848 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
3855 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3856 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3857 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3858 // Use X86ISD::WrapperRIP if we are in X86-64 small / medium PIC mode.
3859 TargetMachine &tm = getTargetMachine();
3860 unsigned WrapperOpcode = (Subtarget->is64Bit() &&
3861 (tm.getCodeModel() == CodeModel::Small ||
3862 tm.getCodeModel() == CodeModel::Medium) &&
3863 tm.getRelocationModel() == Reloc::PIC_)
3864 ? X86ISD::WrapperRIP : X86ISD::Wrapper;
3865 Result = DAG.getNode(WrapperOpcode, getPointerTy(), Result);
3866 if (Subtarget->isTargetDarwin()) {
3867 // With PIC, the address is actually $g + Offset.
3868 if (!Subtarget->is64Bit() &&
3869 getTargetMachine().getRelocationModel() == Reloc::PIC_)
3870 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3871 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3874 // For Darwin, external and weak symbols are indirect, so we want to load
3875 // the value at address GV, not the value of GV itself. This means that
3876 // the GlobalAddress must be in the base or index register of the address,
3877 // not the GV offset field.
3878 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
3879 Subtarget->GVRequiresExtraLoad(GV, false))
3880 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3881 } else if (Subtarget->GVRequiresExtraLoad(GV, false)) {
3882 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3889 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3890 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3891 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3892 // Use X86ISD::WrapperRIP if we are in X86-64 small / medium PIC mode.
3893 TargetMachine &tm = getTargetMachine();
3894 unsigned WrapperOpcode = (Subtarget->is64Bit() &&
3895 (tm.getCodeModel() == CodeModel::Small ||
3896 tm.getCodeModel() == CodeModel::Medium) &&
3897 tm.getRelocationModel() == Reloc::PIC_)
3898 ? X86ISD::WrapperRIP : X86ISD::Wrapper;
3899 Result = DAG.getNode(WrapperOpcode, getPointerTy(), Result);
3900 if (Subtarget->isTargetDarwin()) {
3901 // With PIC, the address is actually $g + Offset.
3902 if (!Subtarget->is64Bit() &&
3903 getTargetMachine().getRelocationModel() == Reloc::PIC_)
3904 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3905 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3912 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3913 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3914 "Not an i64 shift!");
3915 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3916 SDOperand ShOpLo = Op.getOperand(0);
3917 SDOperand ShOpHi = Op.getOperand(1);
3918 SDOperand ShAmt = Op.getOperand(2);
3919 SDOperand Tmp1 = isSRA ?
3920 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3921 DAG.getConstant(0, MVT::i32);
3923 SDOperand Tmp2, Tmp3;
3924 if (Op.getOpcode() == ISD::SHL_PARTS) {
3925 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3926 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3928 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3929 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3932 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3933 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3934 DAG.getConstant(32, MVT::i8));
3935 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3936 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3939 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3941 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3942 SmallVector<SDOperand, 4> Ops;
3943 if (Op.getOpcode() == ISD::SHL_PARTS) {
3944 Ops.push_back(Tmp2);
3945 Ops.push_back(Tmp3);
3947 Ops.push_back(InFlag);
3948 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3949 InFlag = Hi.getValue(1);
3952 Ops.push_back(Tmp3);
3953 Ops.push_back(Tmp1);
3955 Ops.push_back(InFlag);
3956 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3958 Ops.push_back(Tmp2);
3959 Ops.push_back(Tmp3);
3961 Ops.push_back(InFlag);
3962 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3963 InFlag = Lo.getValue(1);
3966 Ops.push_back(Tmp3);
3967 Ops.push_back(Tmp1);
3969 Ops.push_back(InFlag);
3970 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3973 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3977 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3980 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3981 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3982 Op.getOperand(0).getValueType() >= MVT::i16 &&
3983 "Unknown SINT_TO_FP to lower!");
3986 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3987 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3988 MachineFunction &MF = DAG.getMachineFunction();
3989 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3990 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3991 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3992 StackSlot, NULL, 0);
3995 std::vector<MVT::ValueType> Tys;
3996 Tys.push_back(MVT::f64);
3997 Tys.push_back(MVT::Other);
3998 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3999 std::vector<SDOperand> Ops;
4000 Ops.push_back(Chain);
4001 Ops.push_back(StackSlot);
4002 Ops.push_back(DAG.getValueType(SrcVT));
4003 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
4004 Tys, &Ops[0], Ops.size());
4007 Chain = Result.getValue(1);
4008 SDOperand InFlag = Result.getValue(2);
4010 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4011 // shouldn't be necessary except that RFP cannot be live across
4012 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4013 MachineFunction &MF = DAG.getMachineFunction();
4014 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4015 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4016 std::vector<MVT::ValueType> Tys;
4017 Tys.push_back(MVT::Other);
4018 std::vector<SDOperand> Ops;
4019 Ops.push_back(Chain);
4020 Ops.push_back(Result);
4021 Ops.push_back(StackSlot);
4022 Ops.push_back(DAG.getValueType(Op.getValueType()));
4023 Ops.push_back(InFlag);
4024 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
4025 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
4031 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
4032 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4033 "Unknown FP_TO_SINT to lower!");
4034 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4036 MachineFunction &MF = DAG.getMachineFunction();
4037 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4038 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4039 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4042 switch (Op.getValueType()) {
4043 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4044 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4045 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4046 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
4049 SDOperand Chain = DAG.getEntryNode();
4050 SDOperand Value = Op.getOperand(0);
4052 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
4053 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
4054 std::vector<MVT::ValueType> Tys;
4055 Tys.push_back(MVT::f64);
4056 Tys.push_back(MVT::Other);
4057 std::vector<SDOperand> Ops;
4058 Ops.push_back(Chain);
4059 Ops.push_back(StackSlot);
4060 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
4061 Value = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
4062 Chain = Value.getValue(1);
4063 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4064 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4067 // Build the FP_TO_INT*_IN_MEM
4068 std::vector<SDOperand> Ops;
4069 Ops.push_back(Chain);
4070 Ops.push_back(Value);
4071 Ops.push_back(StackSlot);
4072 SDOperand FIST = DAG.getNode(Opc, MVT::Other, &Ops[0], Ops.size());
4075 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4078 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4079 MVT::ValueType VT = Op.getValueType();
4080 const Type *OpNTy = MVT::getTypeForValueType(VT);
4081 std::vector<Constant*> CV;
4082 if (VT == MVT::f64) {
4083 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
4084 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4086 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
4087 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4088 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4089 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4091 Constant *CS = ConstantStruct::get(CV);
4092 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
4093 std::vector<MVT::ValueType> Tys;
4095 Tys.push_back(MVT::Other);
4096 SmallVector<SDOperand, 3> Ops;
4097 Ops.push_back(DAG.getEntryNode());
4098 Ops.push_back(CPIdx);
4099 Ops.push_back(DAG.getSrcValue(NULL));
4100 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
4101 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4104 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4105 MVT::ValueType VT = Op.getValueType();
4106 const Type *OpNTy = MVT::getTypeForValueType(VT);
4107 std::vector<Constant*> CV;
4108 if (VT == MVT::f64) {
4109 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
4110 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4112 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
4113 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4114 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4115 CV.push_back(ConstantFP::get(OpNTy, 0.0));
4117 Constant *CS = ConstantStruct::get(CV);
4118 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
4119 std::vector<MVT::ValueType> Tys;
4121 Tys.push_back(MVT::Other);
4122 SmallVector<SDOperand, 3> Ops;
4123 Ops.push_back(DAG.getEntryNode());
4124 Ops.push_back(CPIdx);
4125 Ops.push_back(DAG.getSrcValue(NULL));
4126 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
4127 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4130 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
4132 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
4134 SDOperand Op0 = Op.getOperand(0);
4135 SDOperand Op1 = Op.getOperand(1);
4136 SDOperand CC = Op.getOperand(2);
4137 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4138 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4139 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4140 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4143 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
4145 SDOperand Ops1[] = { Chain, Op0, Op1 };
4146 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
4147 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4148 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
4151 assert(isFP && "Illegal integer SetCC!");
4153 SDOperand COps[] = { Chain, Op0, Op1 };
4154 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
4156 switch (SetCCOpcode) {
4157 default: assert(false && "Illegal floating point SetCC!");
4158 case ISD::SETOEQ: { // !PF & ZF
4159 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
4160 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
4161 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
4163 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
4164 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4166 case ISD::SETUNE: { // PF | !ZF
4167 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
4168 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
4169 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
4171 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
4172 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4177 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4178 bool addTest = true;
4179 SDOperand Chain = DAG.getEntryNode();
4180 SDOperand Cond = Op.getOperand(0);
4182 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4184 if (Cond.getOpcode() == ISD::SETCC)
4185 Cond = LowerSETCC(Cond, DAG, Chain);
4187 if (Cond.getOpcode() == X86ISD::SETCC) {
4188 CC = Cond.getOperand(0);
4190 // If condition flag is set by a X86ISD::CMP, then make a copy of it
4191 // (since flag operand cannot be shared). Use it as the condition setting
4192 // operand in place of the X86ISD::SETCC.
4193 // If the X86ISD::SETCC has more than one use, then perhaps it's better
4194 // to use a test instead of duplicating the X86ISD::CMP (for register
4195 // pressure reason)?
4196 SDOperand Cmp = Cond.getOperand(1);
4197 unsigned Opc = Cmp.getOpcode();
4198 bool IllegalFPCMov = !X86ScalarSSE &&
4199 MVT::isFloatingPoint(Op.getValueType()) &&
4200 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4201 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
4203 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
4204 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
4210 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4211 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
4212 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
4215 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
4216 SmallVector<SDOperand, 4> Ops;
4217 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4218 // condition is true.
4219 Ops.push_back(Op.getOperand(2));
4220 Ops.push_back(Op.getOperand(1));
4222 Ops.push_back(Cond.getValue(1));
4223 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
4226 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4227 bool addTest = true;
4228 SDOperand Chain = Op.getOperand(0);
4229 SDOperand Cond = Op.getOperand(1);
4230 SDOperand Dest = Op.getOperand(2);
4232 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4234 if (Cond.getOpcode() == ISD::SETCC)
4235 Cond = LowerSETCC(Cond, DAG, Chain);
4237 if (Cond.getOpcode() == X86ISD::SETCC) {
4238 CC = Cond.getOperand(0);
4240 // If condition flag is set by a X86ISD::CMP, then make a copy of it
4241 // (since flag operand cannot be shared). Use it as the condition setting
4242 // operand in place of the X86ISD::SETCC.
4243 // If the X86ISD::SETCC has more than one use, then perhaps it's better
4244 // to use a test instead of duplicating the X86ISD::CMP (for register
4245 // pressure reason)?
4246 SDOperand Cmp = Cond.getOperand(1);
4247 unsigned Opc = Cmp.getOpcode();
4248 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
4249 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
4250 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
4256 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4257 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
4258 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
4260 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
4261 Cond, Op.getOperand(2), CC, Cond.getValue(1));
4264 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4265 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4266 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4267 // Use X86ISD::WrapperRIP if we are in X86-64 small / medium PIC mode.
4268 TargetMachine &tm = getTargetMachine();
4269 unsigned WrapperOpcode = (Subtarget->is64Bit() &&
4270 (tm.getCodeModel() == CodeModel::Small ||
4271 tm.getCodeModel() == CodeModel::Medium) &&
4272 tm.getRelocationModel() == Reloc::PIC_)
4273 ? X86ISD::WrapperRIP : X86ISD::Wrapper;
4274 Result = DAG.getNode(WrapperOpcode, getPointerTy(), Result);
4275 if (Subtarget->isTargetDarwin()) {
4276 // With PIC, the address is actually $g + Offset.
4277 if (!Subtarget->is64Bit() &&
4278 getTargetMachine().getRelocationModel() == Reloc::PIC_)
4279 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4280 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4287 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
4288 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4290 if (Subtarget->is64Bit())
4291 return LowerX86_64CCCCallTo(Op, DAG);
4293 switch (CallingConv) {
4295 assert(0 && "Unsupported calling convention");
4296 case CallingConv::Fast:
4298 return LowerFastCCCallTo(Op, DAG, false);
4301 case CallingConv::C:
4302 case CallingConv::CSRet:
4303 return LowerCCCCallTo(Op, DAG);
4304 case CallingConv::X86_StdCall:
4305 return LowerStdCallCCCallTo(Op, DAG);
4306 case CallingConv::X86_FastCall:
4307 return LowerFastCCCallTo(Op, DAG, true);
4311 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
4314 switch(Op.getNumOperands()) {
4316 assert(0 && "Do not know how to return this many arguments!");
4318 case 1: // ret void.
4319 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
4320 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
4322 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
4324 if (MVT::isVector(ArgVT) ||
4325 (Subtarget->is64Bit() && MVT::isFloatingPoint(ArgVT))) {
4326 // Integer or FP vector result -> XMM0.
4327 if (DAG.getMachineFunction().liveout_empty())
4328 DAG.getMachineFunction().addLiveOut(X86::XMM0);
4329 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
4331 } else if (MVT::isInteger(ArgVT)) {
4332 // Integer result -> EAX / RAX.
4333 // The C calling convention guarantees the return value has been
4334 // promoted to at least MVT::i32. The X86-64 ABI doesn't require the
4335 // value to be promoted MVT::i64. So we don't have to extend it to
4336 // 64-bit. Return the value in EAX, but mark RAX as liveout.
4337 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
4338 if (DAG.getMachineFunction().liveout_empty())
4339 DAG.getMachineFunction().addLiveOut(Reg);
4341 Reg = (ArgVT == MVT::i64) ? X86::RAX : X86::EAX;
4342 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg, Op.getOperand(1),
4344 } else if (!X86ScalarSSE) {
4345 // FP return with fp-stack value.
4346 if (DAG.getMachineFunction().liveout_empty())
4347 DAG.getMachineFunction().addLiveOut(X86::ST0);
4349 std::vector<MVT::ValueType> Tys;
4350 Tys.push_back(MVT::Other);
4351 Tys.push_back(MVT::Flag);
4352 std::vector<SDOperand> Ops;
4353 Ops.push_back(Op.getOperand(0));
4354 Ops.push_back(Op.getOperand(1));
4355 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
4357 // FP return with ScalarSSE (return on fp-stack).
4358 if (DAG.getMachineFunction().liveout_empty())
4359 DAG.getMachineFunction().addLiveOut(X86::ST0);
4362 SDOperand Chain = Op.getOperand(0);
4363 SDOperand Value = Op.getOperand(1);
4365 if (ISD::isNON_EXTLoad(Value.Val) &&
4366 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
4367 Chain = Value.getOperand(0);
4368 MemLoc = Value.getOperand(1);
4370 // Spill the value to memory and reload it into top of stack.
4371 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
4372 MachineFunction &MF = DAG.getMachineFunction();
4373 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4374 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
4375 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
4377 std::vector<MVT::ValueType> Tys;
4378 Tys.push_back(MVT::f64);
4379 Tys.push_back(MVT::Other);
4380 std::vector<SDOperand> Ops;
4381 Ops.push_back(Chain);
4382 Ops.push_back(MemLoc);
4383 Ops.push_back(DAG.getValueType(ArgVT));
4384 Copy = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
4386 Tys.push_back(MVT::Other);
4387 Tys.push_back(MVT::Flag);
4389 Ops.push_back(Copy.getValue(1));
4390 Ops.push_back(Copy);
4391 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
4396 unsigned Reg1 = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
4397 unsigned Reg2 = Subtarget->is64Bit() ? X86::RDX : X86::EDX;
4398 if (DAG.getMachineFunction().liveout_empty()) {
4399 DAG.getMachineFunction().addLiveOut(Reg1);
4400 DAG.getMachineFunction().addLiveOut(Reg2);
4403 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg2, Op.getOperand(3),
4405 Copy = DAG.getCopyToReg(Copy, Reg1, Op.getOperand(1), Copy.getValue(1));
4409 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
4410 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
4415 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
4416 MachineFunction &MF = DAG.getMachineFunction();
4417 const Function* Fn = MF.getFunction();
4418 if (Fn->hasExternalLinkage() &&
4419 Subtarget->isTargetCygwin() &&
4420 Fn->getName() == "main")
4421 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
4423 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4424 if (Subtarget->is64Bit())
4425 return LowerX86_64CCCArguments(Op, DAG);
4429 assert(0 && "Unsupported calling convention");
4430 case CallingConv::Fast:
4432 return LowerFastCCArguments(Op, DAG);
4435 case CallingConv::C:
4436 case CallingConv::CSRet:
4437 return LowerCCCArguments(Op, DAG);
4438 case CallingConv::X86_StdCall:
4439 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
4440 return LowerStdCallCCArguments(Op, DAG);
4441 case CallingConv::X86_FastCall:
4442 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
4443 return LowerFastCallCCArguments(Op, DAG);
4447 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4448 SDOperand InFlag(0, 0);
4449 SDOperand Chain = Op.getOperand(0);
4451 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4452 if (Align == 0) Align = 1;
4454 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4455 // If not DWORD aligned, call memset if size is less than the threshold.
4456 // It knows how to align to the right boundary first.
4457 if ((Align & 3) != 0 ||
4458 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4459 MVT::ValueType IntPtr = getPointerTy();
4460 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4461 std::vector<std::pair<SDOperand, const Type*> > Args;
4462 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
4463 // Extend the ubyte argument to be an int value for the call.
4464 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4465 Args.push_back(std::make_pair(Val, IntPtrTy));
4466 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
4467 std::pair<SDOperand,SDOperand> CallResult =
4468 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
4469 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4470 return CallResult.second;
4475 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4476 unsigned BytesLeft = 0;
4477 bool TwoRepStos = false;
4480 uint64_t Val = ValC->getValue() & 255;
4482 // If the value is a constant, then we can potentially use larger sets.
4483 switch (Align & 3) {
4484 case 2: // WORD aligned
4487 Val = (Val << 8) | Val;
4489 case 0: // DWORD aligned
4492 Val = (Val << 8) | Val;
4493 Val = (Val << 16) | Val;
4494 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4497 Val = (Val << 32) | Val;
4500 default: // Byte aligned
4503 Count = Op.getOperand(3);
4507 if (AVT > MVT::i8) {
4509 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4510 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4511 BytesLeft = I->getValue() % UBytes;
4513 assert(AVT >= MVT::i32 &&
4514 "Do not use rep;stos if not at least DWORD aligned");
4515 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4516 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4521 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4523 InFlag = Chain.getValue(1);
4526 Count = Op.getOperand(3);
4527 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4528 InFlag = Chain.getValue(1);
4531 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4533 InFlag = Chain.getValue(1);
4534 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4535 Op.getOperand(1), InFlag);
4536 InFlag = Chain.getValue(1);
4538 std::vector<MVT::ValueType> Tys;
4539 Tys.push_back(MVT::Other);
4540 Tys.push_back(MVT::Flag);
4541 std::vector<SDOperand> Ops;
4542 Ops.push_back(Chain);
4543 Ops.push_back(DAG.getValueType(AVT));
4544 Ops.push_back(InFlag);
4545 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4548 InFlag = Chain.getValue(1);
4549 Count = Op.getOperand(3);
4550 MVT::ValueType CVT = Count.getValueType();
4551 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4552 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4553 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4555 InFlag = Chain.getValue(1);
4557 Tys.push_back(MVT::Other);
4558 Tys.push_back(MVT::Flag);
4560 Ops.push_back(Chain);
4561 Ops.push_back(DAG.getValueType(MVT::i8));
4562 Ops.push_back(InFlag);
4563 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4564 } else if (BytesLeft) {
4565 // Issue stores for the last 1 - 7 bytes.
4567 unsigned Val = ValC->getValue() & 255;
4568 unsigned Offset = I->getValue() - BytesLeft;
4569 SDOperand DstAddr = Op.getOperand(1);
4570 MVT::ValueType AddrVT = DstAddr.getValueType();
4571 if (BytesLeft >= 4) {
4572 Val = (Val << 8) | Val;
4573 Val = (Val << 16) | Val;
4574 Value = DAG.getConstant(Val, MVT::i32);
4575 Chain = DAG.getStore(Chain, Value,
4576 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4577 DAG.getConstant(Offset, AddrVT)),
4582 if (BytesLeft >= 2) {
4583 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4584 Chain = DAG.getStore(Chain, Value,
4585 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4586 DAG.getConstant(Offset, AddrVT)),
4591 if (BytesLeft == 1) {
4592 Value = DAG.getConstant(Val, MVT::i8);
4593 Chain = DAG.getStore(Chain, Value,
4594 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4595 DAG.getConstant(Offset, AddrVT)),
4603 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4604 SDOperand Chain = Op.getOperand(0);
4606 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4607 if (Align == 0) Align = 1;
4609 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4610 // If not DWORD aligned, call memcpy if size is less than the threshold.
4611 // It knows how to align to the right boundary first.
4612 if ((Align & 3) != 0 ||
4613 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4614 MVT::ValueType IntPtr = getPointerTy();
4615 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4616 std::vector<std::pair<SDOperand, const Type*> > Args;
4617 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
4618 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
4619 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
4620 std::pair<SDOperand,SDOperand> CallResult =
4621 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
4622 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4623 return CallResult.second;
4628 unsigned BytesLeft = 0;
4629 bool TwoRepMovs = false;
4630 switch (Align & 3) {
4631 case 2: // WORD aligned
4634 case 0: // DWORD aligned
4636 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4639 default: // Byte aligned
4641 Count = Op.getOperand(3);
4645 if (AVT > MVT::i8) {
4647 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4648 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4649 BytesLeft = I->getValue() % UBytes;
4651 assert(AVT >= MVT::i32 &&
4652 "Do not use rep;movs if not at least DWORD aligned");
4653 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4654 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4659 SDOperand InFlag(0, 0);
4660 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4662 InFlag = Chain.getValue(1);
4663 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4664 Op.getOperand(1), InFlag);
4665 InFlag = Chain.getValue(1);
4666 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4667 Op.getOperand(2), InFlag);
4668 InFlag = Chain.getValue(1);
4670 std::vector<MVT::ValueType> Tys;
4671 Tys.push_back(MVT::Other);
4672 Tys.push_back(MVT::Flag);
4673 std::vector<SDOperand> Ops;
4674 Ops.push_back(Chain);
4675 Ops.push_back(DAG.getValueType(AVT));
4676 Ops.push_back(InFlag);
4677 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4680 InFlag = Chain.getValue(1);
4681 Count = Op.getOperand(3);
4682 MVT::ValueType CVT = Count.getValueType();
4683 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4684 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4685 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4687 InFlag = Chain.getValue(1);
4689 Tys.push_back(MVT::Other);
4690 Tys.push_back(MVT::Flag);
4692 Ops.push_back(Chain);
4693 Ops.push_back(DAG.getValueType(MVT::i8));
4694 Ops.push_back(InFlag);
4695 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4696 } else if (BytesLeft) {
4697 // Issue loads and stores for the last 1 - 7 bytes.
4698 unsigned Offset = I->getValue() - BytesLeft;
4699 SDOperand DstAddr = Op.getOperand(1);
4700 MVT::ValueType DstVT = DstAddr.getValueType();
4701 SDOperand SrcAddr = Op.getOperand(2);
4702 MVT::ValueType SrcVT = SrcAddr.getValueType();
4704 if (BytesLeft >= 4) {
4705 Value = DAG.getLoad(MVT::i32, Chain,
4706 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4707 DAG.getConstant(Offset, SrcVT)),
4709 Chain = Value.getValue(1);
4710 Chain = DAG.getStore(Chain, Value,
4711 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4712 DAG.getConstant(Offset, DstVT)),
4717 if (BytesLeft >= 2) {
4718 Value = DAG.getLoad(MVT::i16, Chain,
4719 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4720 DAG.getConstant(Offset, SrcVT)),
4722 Chain = Value.getValue(1);
4723 Chain = DAG.getStore(Chain, Value,
4724 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4725 DAG.getConstant(Offset, DstVT)),
4731 if (BytesLeft == 1) {
4732 Value = DAG.getLoad(MVT::i8, Chain,
4733 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4734 DAG.getConstant(Offset, SrcVT)),
4736 Chain = Value.getValue(1);
4737 Chain = DAG.getStore(Chain, Value,
4738 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4739 DAG.getConstant(Offset, DstVT)),
4748 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4749 std::vector<MVT::ValueType> Tys;
4750 Tys.push_back(MVT::Other);
4751 Tys.push_back(MVT::Flag);
4752 std::vector<SDOperand> Ops;
4753 Ops.push_back(Op.getOperand(0));
4754 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &Ops[0], Ops.size());
4756 if (Subtarget->is64Bit()) {
4757 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4758 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4759 MVT::i64, Copy1.getValue(2));
4760 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4761 DAG.getConstant(32, MVT::i8));
4762 Ops.push_back(DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp));
4763 Ops.push_back(Copy2.getValue(1));
4765 Tys[1] = MVT::Other;
4767 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4768 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4769 MVT::i32, Copy1.getValue(2));
4770 Ops.push_back(Copy1);
4771 Ops.push_back(Copy2);
4772 Ops.push_back(Copy2.getValue(1));
4773 Tys[0] = Tys[1] = MVT::i32;
4774 Tys.push_back(MVT::Other);
4776 return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size());
4779 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4780 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4782 if (!Subtarget->is64Bit()) {
4783 // vastart just stores the address of the VarArgsFrameIndex slot into the
4784 // memory location argument.
4785 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4786 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4791 // gp_offset (0 - 6 * 8)
4792 // fp_offset (48 - 48 + 8 * 16)
4793 // overflow_arg_area (point to parameters coming in memory).
4795 std::vector<SDOperand> MemOps;
4796 SDOperand FIN = Op.getOperand(1);
4798 SDOperand Store = DAG.getStore(Op.getOperand(0),
4799 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4800 FIN, SV->getValue(), SV->getOffset());
4801 MemOps.push_back(Store);
4804 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4805 DAG.getConstant(4, getPointerTy()));
4806 Store = DAG.getStore(Op.getOperand(0),
4807 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4808 FIN, SV->getValue(), SV->getOffset());
4809 MemOps.push_back(Store);
4811 // Store ptr to overflow_arg_area
4812 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4813 DAG.getConstant(4, getPointerTy()));
4814 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4815 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4817 MemOps.push_back(Store);
4819 // Store ptr to reg_save_area.
4820 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4821 DAG.getConstant(8, getPointerTy()));
4822 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4823 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4825 MemOps.push_back(Store);
4826 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4830 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4831 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4833 default: return SDOperand(); // Don't custom lower most intrinsics.
4834 // Comparison intrinsics.
4835 case Intrinsic::x86_sse_comieq_ss:
4836 case Intrinsic::x86_sse_comilt_ss:
4837 case Intrinsic::x86_sse_comile_ss:
4838 case Intrinsic::x86_sse_comigt_ss:
4839 case Intrinsic::x86_sse_comige_ss:
4840 case Intrinsic::x86_sse_comineq_ss:
4841 case Intrinsic::x86_sse_ucomieq_ss:
4842 case Intrinsic::x86_sse_ucomilt_ss:
4843 case Intrinsic::x86_sse_ucomile_ss:
4844 case Intrinsic::x86_sse_ucomigt_ss:
4845 case Intrinsic::x86_sse_ucomige_ss:
4846 case Intrinsic::x86_sse_ucomineq_ss:
4847 case Intrinsic::x86_sse2_comieq_sd:
4848 case Intrinsic::x86_sse2_comilt_sd:
4849 case Intrinsic::x86_sse2_comile_sd:
4850 case Intrinsic::x86_sse2_comigt_sd:
4851 case Intrinsic::x86_sse2_comige_sd:
4852 case Intrinsic::x86_sse2_comineq_sd:
4853 case Intrinsic::x86_sse2_ucomieq_sd:
4854 case Intrinsic::x86_sse2_ucomilt_sd:
4855 case Intrinsic::x86_sse2_ucomile_sd:
4856 case Intrinsic::x86_sse2_ucomigt_sd:
4857 case Intrinsic::x86_sse2_ucomige_sd:
4858 case Intrinsic::x86_sse2_ucomineq_sd: {
4860 ISD::CondCode CC = ISD::SETCC_INVALID;
4863 case Intrinsic::x86_sse_comieq_ss:
4864 case Intrinsic::x86_sse2_comieq_sd:
4868 case Intrinsic::x86_sse_comilt_ss:
4869 case Intrinsic::x86_sse2_comilt_sd:
4873 case Intrinsic::x86_sse_comile_ss:
4874 case Intrinsic::x86_sse2_comile_sd:
4878 case Intrinsic::x86_sse_comigt_ss:
4879 case Intrinsic::x86_sse2_comigt_sd:
4883 case Intrinsic::x86_sse_comige_ss:
4884 case Intrinsic::x86_sse2_comige_sd:
4888 case Intrinsic::x86_sse_comineq_ss:
4889 case Intrinsic::x86_sse2_comineq_sd:
4893 case Intrinsic::x86_sse_ucomieq_ss:
4894 case Intrinsic::x86_sse2_ucomieq_sd:
4895 Opc = X86ISD::UCOMI;
4898 case Intrinsic::x86_sse_ucomilt_ss:
4899 case Intrinsic::x86_sse2_ucomilt_sd:
4900 Opc = X86ISD::UCOMI;
4903 case Intrinsic::x86_sse_ucomile_ss:
4904 case Intrinsic::x86_sse2_ucomile_sd:
4905 Opc = X86ISD::UCOMI;
4908 case Intrinsic::x86_sse_ucomigt_ss:
4909 case Intrinsic::x86_sse2_ucomigt_sd:
4910 Opc = X86ISD::UCOMI;
4913 case Intrinsic::x86_sse_ucomige_ss:
4914 case Intrinsic::x86_sse2_ucomige_sd:
4915 Opc = X86ISD::UCOMI;
4918 case Intrinsic::x86_sse_ucomineq_ss:
4919 case Intrinsic::x86_sse2_ucomineq_sd:
4920 Opc = X86ISD::UCOMI;
4926 SDOperand LHS = Op.getOperand(1);
4927 SDOperand RHS = Op.getOperand(2);
4928 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4930 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4931 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4932 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4933 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4934 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4935 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4936 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4941 /// LowerOperation - Provide custom lowering hooks for some operations.
4943 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4944 switch (Op.getOpcode()) {
4945 default: assert(0 && "Should not custom lower this!");
4946 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4947 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4948 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4949 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4950 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4951 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4952 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4953 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4954 case ISD::SHL_PARTS:
4955 case ISD::SRA_PARTS:
4956 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4957 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4958 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4959 case ISD::FABS: return LowerFABS(Op, DAG);
4960 case ISD::FNEG: return LowerFNEG(Op, DAG);
4961 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
4962 case ISD::SELECT: return LowerSELECT(Op, DAG);
4963 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4964 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4965 case ISD::CALL: return LowerCALL(Op, DAG);
4966 case ISD::RET: return LowerRET(Op, DAG);
4967 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4968 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4969 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4970 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4971 case ISD::VASTART: return LowerVASTART(Op, DAG);
4972 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4976 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4978 default: return NULL;
4979 case X86ISD::SHLD: return "X86ISD::SHLD";
4980 case X86ISD::SHRD: return "X86ISD::SHRD";
4981 case X86ISD::FAND: return "X86ISD::FAND";
4982 case X86ISD::FXOR: return "X86ISD::FXOR";
4983 case X86ISD::FILD: return "X86ISD::FILD";
4984 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4985 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4986 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4987 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4988 case X86ISD::FLD: return "X86ISD::FLD";
4989 case X86ISD::FST: return "X86ISD::FST";
4990 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4991 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4992 case X86ISD::CALL: return "X86ISD::CALL";
4993 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4994 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4995 case X86ISD::CMP: return "X86ISD::CMP";
4996 case X86ISD::COMI: return "X86ISD::COMI";
4997 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4998 case X86ISD::SETCC: return "X86ISD::SETCC";
4999 case X86ISD::CMOV: return "X86ISD::CMOV";
5000 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5001 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5002 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5003 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
5004 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
5005 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
5006 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5007 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5008 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
5009 case X86ISD::S2VEC: return "X86ISD::S2VEC";
5010 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5011 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5012 case X86ISD::FMAX: return "X86ISD::FMAX";
5013 case X86ISD::FMIN: return "X86ISD::FMIN";
5017 /// isLegalAddressImmediate - Return true if the integer value or
5018 /// GlobalValue can be used as the offset of the target addressing mode.
5019 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
5020 // X86 allows a sign-extended 32-bit immediate field.
5021 return (V > -(1LL << 32) && V < (1LL << 32)-1);
5024 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
5025 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement
5026 // field unless we are in small code model.
5027 if (Subtarget->is64Bit() &&
5028 getTargetMachine().getCodeModel() != CodeModel::Small)
5030 Reloc::Model RModel = getTargetMachine().getRelocationModel();
5031 return (RModel == Reloc::Static) ||
5032 !Subtarget->GVRequiresExtraLoad(GV, false);
5035 /// isShuffleMaskLegal - Targets can use this to indicate that they only
5036 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5037 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5038 /// are assumed to be legal.
5040 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5041 // Only do shuffles on 128-bit vector types for now.
5042 if (MVT::getSizeInBits(VT) == 64) return false;
5043 return (Mask.Val->getNumOperands() <= 4 ||
5044 isSplatMask(Mask.Val) ||
5045 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5046 X86::isUNPCKLMask(Mask.Val) ||
5047 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5048 X86::isUNPCKHMask(Mask.Val));
5051 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5053 SelectionDAG &DAG) const {
5054 unsigned NumElts = BVOps.size();
5055 // Only do shuffles on 128-bit vector types for now.
5056 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5057 if (NumElts == 2) return true;
5059 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
5060 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
5065 //===----------------------------------------------------------------------===//
5066 // X86 Scheduler Hooks
5067 //===----------------------------------------------------------------------===//
5070 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5071 MachineBasicBlock *BB) {
5072 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5073 switch (MI->getOpcode()) {
5074 default: assert(false && "Unexpected instr type to insert");
5075 case X86::CMOV_FR32:
5076 case X86::CMOV_FR64:
5077 case X86::CMOV_V4F32:
5078 case X86::CMOV_V2F64:
5079 case X86::CMOV_V2I64: {
5080 // To "insert" a SELECT_CC instruction, we actually have to insert the
5081 // diamond control-flow pattern. The incoming instruction knows the
5082 // destination vreg to set, the condition code register to branch on, the
5083 // true/false values to select between, and a branch opcode to use.
5084 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5085 ilist<MachineBasicBlock>::iterator It = BB;
5091 // cmpTY ccX, r1, r2
5093 // fallthrough --> copy0MBB
5094 MachineBasicBlock *thisMBB = BB;
5095 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5096 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5098 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5099 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5100 MachineFunction *F = BB->getParent();
5101 F->getBasicBlockList().insert(It, copy0MBB);
5102 F->getBasicBlockList().insert(It, sinkMBB);
5103 // Update machine-CFG edges by first adding all successors of the current
5104 // block to the new block which will contain the Phi node for the select.
5105 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5106 e = BB->succ_end(); i != e; ++i)
5107 sinkMBB->addSuccessor(*i);
5108 // Next, remove all successors of the current block, and add the true
5109 // and fallthrough blocks as its successors.
5110 while(!BB->succ_empty())
5111 BB->removeSuccessor(BB->succ_begin());
5112 BB->addSuccessor(copy0MBB);
5113 BB->addSuccessor(sinkMBB);
5116 // %FalseValue = ...
5117 // # fallthrough to sinkMBB
5120 // Update machine-CFG edges
5121 BB->addSuccessor(sinkMBB);
5124 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5127 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5128 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5129 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5131 delete MI; // The pseudo instruction is gone now.
5135 case X86::FP_TO_INT16_IN_MEM:
5136 case X86::FP_TO_INT32_IN_MEM:
5137 case X86::FP_TO_INT64_IN_MEM: {
5138 // Change the floating point control register to use "round towards zero"
5139 // mode when truncating to an integer value.
5140 MachineFunction *F = BB->getParent();
5141 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5142 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5144 // Load the old value of the high byte of the control word...
5146 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
5147 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5149 // Set the high part to be round to zero...
5150 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5153 // Reload the modified control word now...
5154 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5156 // Restore the memory image of control word to original value
5157 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5160 // Get the X86 opcode to use.
5162 switch (MI->getOpcode()) {
5163 default: assert(0 && "illegal opcode!");
5164 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
5165 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
5166 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
5170 MachineOperand &Op = MI->getOperand(0);
5171 if (Op.isRegister()) {
5172 AM.BaseType = X86AddressMode::RegBase;
5173 AM.Base.Reg = Op.getReg();
5175 AM.BaseType = X86AddressMode::FrameIndexBase;
5176 AM.Base.FrameIndex = Op.getFrameIndex();
5178 Op = MI->getOperand(1);
5179 if (Op.isImmediate())
5180 AM.Scale = Op.getImm();
5181 Op = MI->getOperand(2);
5182 if (Op.isImmediate())
5183 AM.IndexReg = Op.getImm();
5184 Op = MI->getOperand(3);
5185 if (Op.isGlobalAddress()) {
5186 AM.GV = Op.getGlobal();
5188 AM.Disp = Op.getImm();
5190 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5191 .addReg(MI->getOperand(4).getReg());
5193 // Reload the original control word now.
5194 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5196 delete MI; // The pseudo instruction is gone now.
5202 //===----------------------------------------------------------------------===//
5203 // X86 Optimization Hooks
5204 //===----------------------------------------------------------------------===//
5206 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5208 uint64_t &KnownZero,
5210 unsigned Depth) const {
5211 unsigned Opc = Op.getOpcode();
5212 assert((Opc >= ISD::BUILTIN_OP_END ||
5213 Opc == ISD::INTRINSIC_WO_CHAIN ||
5214 Opc == ISD::INTRINSIC_W_CHAIN ||
5215 Opc == ISD::INTRINSIC_VOID) &&
5216 "Should use MaskedValueIsZero if you don't know whether Op"
5217 " is a target node!");
5219 KnownZero = KnownOne = 0; // Don't know anything.
5223 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5228 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
5229 /// element of the result of the vector shuffle.
5230 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5231 MVT::ValueType VT = N->getValueType(0);
5232 SDOperand PermMask = N->getOperand(2);
5233 unsigned NumElems = PermMask.getNumOperands();
5234 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5236 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5238 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
5239 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5240 SDOperand Idx = PermMask.getOperand(i);
5241 if (Idx.getOpcode() == ISD::UNDEF)
5242 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
5243 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5248 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5249 /// node is a GlobalAddress + an offset.
5250 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5251 unsigned Opc = N->getOpcode();
5252 if (Opc == X86ISD::Wrapper || Opc == X86ISD::WrapperRIP) {
5253 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5254 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5257 } else if (Opc == ISD::ADD) {
5258 SDOperand N1 = N->getOperand(0);
5259 SDOperand N2 = N->getOperand(1);
5260 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5261 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5263 Offset += V->getSignExtended();
5266 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5267 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5269 Offset += V->getSignExtended();
5277 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
5279 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5280 MachineFrameInfo *MFI) {
5281 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5284 SDOperand Loc = N->getOperand(1);
5285 SDOperand BaseLoc = Base->getOperand(1);
5286 if (Loc.getOpcode() == ISD::FrameIndex) {
5287 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5289 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
5290 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5291 int FS = MFI->getObjectSize(FI);
5292 int BFS = MFI->getObjectSize(BFI);
5293 if (FS != BFS || FS != Size) return false;
5294 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5296 GlobalValue *GV1 = NULL;
5297 GlobalValue *GV2 = NULL;
5298 int64_t Offset1 = 0;
5299 int64_t Offset2 = 0;
5300 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5301 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5302 if (isGA1 && isGA2 && GV1 == GV2)
5303 return Offset1 == (Offset2 + Dist*Size);
5309 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5310 const X86Subtarget *Subtarget) {
5313 if (isGAPlusOffset(Base, GV, Offset))
5314 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5316 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
5317 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
5319 // Fixed objects do not specify alignment, however the offsets are known.
5320 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5321 (MFI->getObjectOffset(BFI) % 16) == 0);
5323 return MFI->getObjectAlignment(BFI) >= 16;
5329 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5330 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5331 /// if the load addresses are consecutive, non-overlapping, and in the right
5333 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5334 const X86Subtarget *Subtarget) {
5335 MachineFunction &MF = DAG.getMachineFunction();
5336 MachineFrameInfo *MFI = MF.getFrameInfo();
5337 MVT::ValueType VT = N->getValueType(0);
5338 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
5339 SDOperand PermMask = N->getOperand(2);
5340 int NumElems = (int)PermMask.getNumOperands();
5341 SDNode *Base = NULL;
5342 for (int i = 0; i < NumElems; ++i) {
5343 SDOperand Idx = PermMask.getOperand(i);
5344 if (Idx.getOpcode() == ISD::UNDEF) {
5345 if (!Base) return SDOperand();
5348 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5349 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5353 else if (!isConsecutiveLoad(Arg.Val, Base,
5354 i, MVT::getSizeInBits(EVT)/8,MFI))
5359 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
5361 LoadSDNode *LD = cast<LoadSDNode>(Base);
5362 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5363 LD->getSrcValueOffset());
5365 // Just use movups, it's shorter.
5366 std::vector<MVT::ValueType> Tys;
5367 Tys.push_back(MVT::v4f32);
5368 Tys.push_back(MVT::Other);
5369 SmallVector<SDOperand, 3> Ops;
5370 Ops.push_back(Base->getOperand(0));
5371 Ops.push_back(Base->getOperand(1));
5372 Ops.push_back(Base->getOperand(2));
5373 return DAG.getNode(ISD::BIT_CONVERT, VT,
5374 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
5378 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5379 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5380 const X86Subtarget *Subtarget) {
5381 SDOperand Cond = N->getOperand(0);
5383 // If we have SSE[12] support, try to form min/max nodes.
5384 if (Subtarget->hasSSE2() &&
5385 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5386 if (Cond.getOpcode() == ISD::SETCC) {
5387 // Get the LHS/RHS of the select.
5388 SDOperand LHS = N->getOperand(1);
5389 SDOperand RHS = N->getOperand(2);
5390 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5392 unsigned Opcode = 0;
5393 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5396 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5399 if (!UnsafeFPMath) break;
5401 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5403 Opcode = X86ISD::FMIN;
5406 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5409 if (!UnsafeFPMath) break;
5411 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5413 Opcode = X86ISD::FMAX;
5416 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5419 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5422 if (!UnsafeFPMath) break;
5424 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5426 Opcode = X86ISD::FMIN;
5429 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5432 if (!UnsafeFPMath) break;
5434 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5436 Opcode = X86ISD::FMAX;
5442 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5451 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5452 DAGCombinerInfo &DCI) const {
5453 SelectionDAG &DAG = DCI.DAG;
5454 switch (N->getOpcode()) {
5456 case ISD::VECTOR_SHUFFLE:
5457 return PerformShuffleCombine(N, DAG, Subtarget);
5459 return PerformSELECTCombine(N, DAG, Subtarget);
5465 //===----------------------------------------------------------------------===//
5466 // X86 Inline Assembly Support
5467 //===----------------------------------------------------------------------===//
5469 /// getConstraintType - Given a constraint letter, return the type of
5470 /// constraint it is for this target.
5471 X86TargetLowering::ConstraintType
5472 X86TargetLowering::getConstraintType(char ConstraintLetter) const {
5473 switch (ConstraintLetter) {
5482 return C_RegisterClass;
5483 default: return TargetLowering::getConstraintType(ConstraintLetter);
5487 /// isOperandValidForConstraint - Return the specified operand (possibly
5488 /// modified) if the specified SDOperand is valid for the specified target
5489 /// constraint letter, otherwise return null.
5490 SDOperand X86TargetLowering::
5491 isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
5492 switch (Constraint) {
5495 // Literal immediates are always ok.
5496 if (isa<ConstantSDNode>(Op)) return Op;
5498 // If we are in non-pic codegen mode, we allow the address of a global to
5499 // be used with 'i'.
5500 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
5501 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
5502 return SDOperand(0, 0);
5504 if (GA->getOpcode() != ISD::TargetGlobalAddress)
5505 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5510 // Otherwise, not valid for this mode.
5511 return SDOperand(0, 0);
5513 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
5517 std::vector<unsigned> X86TargetLowering::
5518 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5519 MVT::ValueType VT) const {
5520 if (Constraint.size() == 1) {
5521 // FIXME: not handling fp-stack yet!
5522 // FIXME: not handling MMX registers yet ('y' constraint).
5523 switch (Constraint[0]) { // GCC X86 Constraint Letters
5524 default: break; // Unknown constraint letter
5525 case 'A': // EAX/EDX
5526 if (VT == MVT::i32 || VT == MVT::i64)
5527 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5529 case 'r': // GENERAL_REGS
5530 case 'R': // LEGACY_REGS
5531 if (VT == MVT::i64 && Subtarget->is64Bit())
5532 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
5533 X86::RSI, X86::RDI, X86::RBP, X86::RSP,
5534 X86::R8, X86::R9, X86::R10, X86::R11,
5535 X86::R12, X86::R13, X86::R14, X86::R15, 0);
5537 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5538 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
5539 else if (VT == MVT::i16)
5540 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
5541 X86::SI, X86::DI, X86::BP, X86::SP, 0);
5542 else if (VT == MVT::i8)
5543 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5545 case 'l': // INDEX_REGS
5547 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5548 X86::ESI, X86::EDI, X86::EBP, 0);
5549 else if (VT == MVT::i16)
5550 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
5551 X86::SI, X86::DI, X86::BP, 0);
5552 else if (VT == MVT::i8)
5553 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5555 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5558 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5559 else if (VT == MVT::i16)
5560 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5561 else if (VT == MVT::i8)
5562 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5564 case 'x': // SSE_REGS if SSE1 allowed
5565 if (Subtarget->hasSSE1())
5566 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5567 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5569 return std::vector<unsigned>();
5570 case 'Y': // SSE_REGS if SSE2 allowed
5571 if (Subtarget->hasSSE2())
5572 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5573 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5575 return std::vector<unsigned>();
5579 return std::vector<unsigned>();
5582 std::pair<unsigned, const TargetRegisterClass*>
5583 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5584 MVT::ValueType VT) const {
5585 // Use the default implementation in TargetLowering to convert the register
5586 // constraint into a member of a register class.
5587 std::pair<unsigned, const TargetRegisterClass*> Res;
5588 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5590 // Not found as a standard register?
5591 if (Res.second == 0) {
5592 // GCC calls "st(0)" just plain "st".
5593 if (StringsEqualNoCase("{st}", Constraint)) {
5594 Res.first = X86::ST0;
5595 Res.second = X86::RSTRegisterClass;
5601 // Otherwise, check to see if this is a register class of the wrong value
5602 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5603 // turn into {ax},{dx}.
5604 if (Res.second->hasType(VT))
5605 return Res; // Correct type already, nothing to do.
5607 // All of the single-register GCC register classes map their values onto
5608 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5609 // really want an 8-bit or 32-bit register, map to the appropriate register
5610 // class and return the appropriate register.
5611 if (Res.second != X86::GR16RegisterClass)
5614 if (VT == MVT::i8) {
5615 unsigned DestReg = 0;
5616 switch (Res.first) {
5618 case X86::AX: DestReg = X86::AL; break;
5619 case X86::DX: DestReg = X86::DL; break;
5620 case X86::CX: DestReg = X86::CL; break;
5621 case X86::BX: DestReg = X86::BL; break;
5624 Res.first = DestReg;
5625 Res.second = Res.second = X86::GR8RegisterClass;
5627 } else if (VT == MVT::i32) {
5628 unsigned DestReg = 0;
5629 switch (Res.first) {
5631 case X86::AX: DestReg = X86::EAX; break;
5632 case X86::DX: DestReg = X86::EDX; break;
5633 case X86::CX: DestReg = X86::ECX; break;
5634 case X86::BX: DestReg = X86::EBX; break;
5635 case X86::SI: DestReg = X86::ESI; break;
5636 case X86::DI: DestReg = X86::EDI; break;
5637 case X86::BP: DestReg = X86::EBP; break;
5638 case X86::SP: DestReg = X86::ESP; break;
5641 Res.first = DestReg;
5642 Res.second = Res.second = X86::GR32RegisterClass;
5644 } else if (VT == MVT::i64) {
5645 unsigned DestReg = 0;
5646 switch (Res.first) {
5648 case X86::AX: DestReg = X86::RAX; break;
5649 case X86::DX: DestReg = X86::RDX; break;
5650 case X86::CX: DestReg = X86::RCX; break;
5651 case X86::BX: DestReg = X86::RBX; break;
5652 case X86::SI: DestReg = X86::RSI; break;
5653 case X86::DI: DestReg = X86::RDI; break;
5654 case X86::BP: DestReg = X86::RBP; break;
5655 case X86::SP: DestReg = X86::RSP; break;
5658 Res.first = DestReg;
5659 Res.second = Res.second = X86::GR64RegisterClass;