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/CallingConvLower.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/SelectionDAG.h"
32 #include "llvm/CodeGen/SSARegMap.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Target/TargetOptions.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/ADT/StringExtras.h"
40 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
41 cl::desc("Enable fastcc on X86"));
42 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
43 : TargetLowering(TM) {
44 Subtarget = &TM.getSubtarget<X86Subtarget>();
45 X86ScalarSSE = Subtarget->hasSSE2();
46 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
48 // Set up the TargetLowering object.
50 // X86 is weird, it always uses i8 for shift amounts and setcc results.
51 setShiftAmountType(MVT::i8);
52 setSetCCResultType(MVT::i8);
53 setSetCCResultContents(ZeroOrOneSetCCResult);
54 setSchedulingPreference(SchedulingForRegPressure);
55 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
56 setStackPointerRegisterToSaveRestore(X86StackPtr);
58 if (Subtarget->isTargetDarwin()) {
59 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
60 setUseUnderscoreSetJmp(false);
61 setUseUnderscoreLongJmp(false);
62 } else if (Subtarget->isTargetMingw()) {
63 // MS runtime is weird: it exports _setjmp, but longjmp!
64 setUseUnderscoreSetJmp(true);
65 setUseUnderscoreLongJmp(false);
67 setUseUnderscoreSetJmp(true);
68 setUseUnderscoreLongJmp(true);
71 // Add legal addressing mode scale values.
72 addLegalAddressScale(8);
73 addLegalAddressScale(4);
74 addLegalAddressScale(2);
75 // Enter the ones which require both scale + index last. These are more
77 addLegalAddressScale(9);
78 addLegalAddressScale(5);
79 addLegalAddressScale(3);
81 // Set up the register classes.
82 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
83 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
84 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
85 if (Subtarget->is64Bit())
86 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
88 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
90 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
92 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
93 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
94 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
96 if (Subtarget->is64Bit()) {
97 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
98 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
101 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
102 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
107 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
109 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
110 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
111 // SSE has no i16 to fp conversion, only i32
113 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
115 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
119 if (!Subtarget->is64Bit()) {
120 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
121 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
122 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
125 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
127 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
128 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
131 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
133 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
134 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
137 // Handle FP_TO_UINT by promoting the destination to a larger signed
139 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
140 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
141 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
143 if (Subtarget->is64Bit()) {
144 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
145 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
147 if (X86ScalarSSE && !Subtarget->hasSSE3())
148 // Expand FP_TO_UINT into a select.
149 // FIXME: We would like to use a Custom expander here eventually to do
150 // the optimal thing for SSE vs. the default expansion in the legalizer.
151 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
153 // With SSE3 we can use fisttpll to convert to a signed i64.
154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
157 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
159 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
160 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
163 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
164 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
165 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
166 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
167 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
168 if (Subtarget->is64Bit())
169 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
170 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
171 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
172 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
173 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
174 setOperationAction(ISD::FREM , MVT::f64 , Expand);
176 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
177 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
178 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
179 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
180 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
181 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
182 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
183 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
184 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
185 if (Subtarget->is64Bit()) {
186 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
187 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
188 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
191 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
192 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
194 // These should be promoted to a larger select which is supported.
195 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
196 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
197 // X86 wants to expand cmov itself.
198 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
199 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
200 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
201 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
202 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
203 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
204 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
205 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
206 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
207 if (Subtarget->is64Bit()) {
208 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
209 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
211 // X86 ret instruction may pop stack.
212 setOperationAction(ISD::RET , MVT::Other, Custom);
214 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
215 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
216 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
217 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
218 if (Subtarget->is64Bit()) {
219 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
220 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
221 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
222 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
224 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
225 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
226 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
227 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
228 // X86 wants to expand memset / memcpy itself.
229 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
230 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
232 // We don't have line number support yet.
233 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
234 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
235 // FIXME - use subtarget debug flags
236 if (!Subtarget->isTargetDarwin() &&
237 !Subtarget->isTargetELF() &&
238 !Subtarget->isTargetCygMing())
239 setOperationAction(ISD::LABEL, MVT::Other, Expand);
241 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
242 setOperationAction(ISD::VASTART , MVT::Other, Custom);
244 // Use the default implementation.
245 setOperationAction(ISD::VAARG , MVT::Other, Expand);
246 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
247 setOperationAction(ISD::VAEND , MVT::Other, Expand);
248 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
249 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
250 if (Subtarget->is64Bit())
251 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
252 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
255 // Set up the FP register classes.
256 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
257 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
259 // Use ANDPD to simulate FABS.
260 setOperationAction(ISD::FABS , MVT::f64, Custom);
261 setOperationAction(ISD::FABS , MVT::f32, Custom);
263 // Use XORP to simulate FNEG.
264 setOperationAction(ISD::FNEG , MVT::f64, Custom);
265 setOperationAction(ISD::FNEG , MVT::f32, Custom);
267 // Use ANDPD and ORPD to simulate FCOPYSIGN.
268 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
269 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
271 // We don't support sin/cos/fmod
272 setOperationAction(ISD::FSIN , MVT::f64, Expand);
273 setOperationAction(ISD::FCOS , MVT::f64, Expand);
274 setOperationAction(ISD::FREM , MVT::f64, Expand);
275 setOperationAction(ISD::FSIN , MVT::f32, Expand);
276 setOperationAction(ISD::FCOS , MVT::f32, Expand);
277 setOperationAction(ISD::FREM , MVT::f32, Expand);
279 // Expand FP immediates into loads from the stack, except for the special
281 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
282 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
283 addLegalFPImmediate(+0.0); // xorps / xorpd
285 // Set up the FP register classes.
286 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
288 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
289 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
290 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
293 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
294 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
297 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
298 addLegalFPImmediate(+0.0); // FLD0
299 addLegalFPImmediate(+1.0); // FLD1
300 addLegalFPImmediate(-0.0); // FLD0/FCHS
301 addLegalFPImmediate(-1.0); // FLD1/FCHS
304 // First set operation action for all vector types to expand. Then we
305 // will selectively turn on ones that can be effectively codegen'd.
306 for (unsigned VT = (unsigned)MVT::Vector + 1;
307 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
308 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
309 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
310 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
311 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
312 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
313 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
314 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
315 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
316 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
317 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
318 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
319 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
320 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
321 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
322 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
325 if (Subtarget->hasMMX()) {
326 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
327 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
328 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
330 // FIXME: add MMX packed arithmetics
331 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
332 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
333 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
336 if (Subtarget->hasSSE1()) {
337 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
339 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
340 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
341 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
342 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
343 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
344 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
345 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
346 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
347 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
350 if (Subtarget->hasSSE2()) {
351 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
352 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
353 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
354 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
355 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
357 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
358 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
359 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
360 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
361 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
362 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
363 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
364 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
365 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
366 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
367 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
369 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
370 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
371 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
372 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
373 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
374 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
376 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
377 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
378 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
379 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
380 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
382 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
383 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
384 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
385 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
386 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
387 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
389 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
390 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
391 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
392 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
393 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
394 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
395 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
396 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
397 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
398 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
399 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
400 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
403 // Custom lower v2i64 and v2f64 selects.
404 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
405 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
406 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
407 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
410 // We want to custom lower some of our intrinsics.
411 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
413 // We have target-specific dag combine patterns for the following nodes:
414 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
415 setTargetDAGCombine(ISD::SELECT);
417 computeRegisterProperties();
419 // FIXME: These should be based on subtarget info. Plus, the values should
420 // be smaller when we are in optimizing for size mode.
421 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
422 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
423 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
424 allowUnalignedMemoryAccesses = true; // x86 supports it!
428 //===----------------------------------------------------------------------===//
429 // Return Value Calling Convention Implementation
430 //===----------------------------------------------------------------------===//
432 #include "X86GenCallingConv.inc"
434 /// LowerRET - Lower an ISD::RET node.
435 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
436 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
438 SmallVector<CCValAssign, 16> RVLocs;
439 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
440 CCState CCInfo(CC, getTargetMachine(), RVLocs);
442 // Determine which register each value should be copied into.
443 for (unsigned i = 0; i != Op.getNumOperands() / 2; ++i) {
444 MVT::ValueType VT = Op.getOperand(i*2+1).getValueType();
445 if (RetCC_X86(i, VT, VT, CCValAssign::Full,
446 cast<ConstantSDNode>(Op.getOperand(i*2+2))->getValue(),
448 assert(0 && "Unhandled result type!");
451 // If this is the first return lowered for this function, add the regs to the
452 // liveout set for the function.
453 if (DAG.getMachineFunction().liveout_empty()) {
454 for (unsigned i = 0; i != RVLocs.size(); ++i)
455 if (RVLocs[i].isRegLoc())
456 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
459 SDOperand Chain = Op.getOperand(0);
462 // Copy the result values into the output registers.
463 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
464 RVLocs[0].getLocReg() != X86::ST0) {
465 for (unsigned i = 0; i != RVLocs.size(); ++i) {
466 CCValAssign &VA = RVLocs[i];
467 assert(VA.isRegLoc() && "Can only return in registers!");
468 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
470 Flag = Chain.getValue(1);
473 // We need to handle a destination of ST0 specially, because it isn't really
475 SDOperand Value = Op.getOperand(1);
477 // If this is an FP return with ScalarSSE, we need to move the value from
478 // an XMM register onto the fp-stack.
482 // If this is a load into a scalarsse value, don't store the loaded value
483 // back to the stack, only to reload it: just replace the scalar-sse load.
484 if (ISD::isNON_EXTLoad(Value.Val) &&
485 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
486 Chain = Value.getOperand(0);
487 MemLoc = Value.getOperand(1);
489 // Spill the value to memory and reload it into top of stack.
490 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
491 MachineFunction &MF = DAG.getMachineFunction();
492 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
493 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
494 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
496 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
497 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
498 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
499 Chain = Value.getValue(1);
502 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
503 SDOperand Ops[] = { Chain, Value };
504 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
505 Flag = Chain.getValue(1);
508 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
510 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
512 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
516 /// LowerCallResult - Lower the result values of an ISD::CALL into the
517 /// appropriate copies out of appropriate physical registers. This assumes that
518 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
519 /// being lowered. The returns a SDNode with the same number of values as the
521 SDNode *X86TargetLowering::
522 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
523 unsigned CallingConv, SelectionDAG &DAG) {
524 SmallVector<SDOperand, 8> ResultVals;
526 SmallVector<CCValAssign, 16> RVLocs;
527 CCState CCInfo(CallingConv, getTargetMachine(), RVLocs);
529 for (unsigned i = 0, e = TheCall->getNumValues() - 1; i != e; ++i) {
530 MVT::ValueType VT = TheCall->getValueType(i);
531 if (RetCC_X86(i, VT, VT, CCValAssign::Full, 0, CCInfo))
532 assert(0 && "Unhandled result type!");
535 // Copy all of the result registers out of their specified physreg.
536 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
537 for (unsigned i = 0; i != RVLocs.size(); ++i) {
538 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
539 RVLocs[i].getValVT(), InFlag).getValue(1);
540 InFlag = Chain.getValue(2);
541 ResultVals.push_back(Chain.getValue(0));
544 // Copies from the FP stack are special, as ST0 isn't a valid register
545 // before the fp stackifier runs.
547 // Copy ST0 into an RFP register with FP_GET_RESULT.
548 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
549 SDOperand GROps[] = { Chain, InFlag };
550 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
551 Chain = RetVal.getValue(1);
552 InFlag = RetVal.getValue(2);
554 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
557 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
558 // shouldn't be necessary except that RFP cannot be live across
559 // multiple blocks. When stackifier is fixed, they can be uncoupled.
560 MachineFunction &MF = DAG.getMachineFunction();
561 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
562 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
564 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
566 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
567 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
568 Chain = RetVal.getValue(1);
571 if (RVLocs[0].getValVT() == MVT::f32 && !X86ScalarSSE)
572 // FIXME: we would really like to remember that this FP_ROUND
573 // operation is okay to eliminate if we allow excess FP precision.
574 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
575 ResultVals.push_back(RetVal);
578 // Merge everything together with a MERGE_VALUES node.
579 ResultVals.push_back(Chain);
580 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
581 &ResultVals[0], ResultVals.size()).Val;
585 //===----------------------------------------------------------------------===//
586 // C & StdCall Calling Convention implementation
587 //===----------------------------------------------------------------------===//
588 // StdCall calling convention seems to be standard for many Windows' API
589 // routines and around. It differs from C calling convention just a little:
590 // callee should clean up the stack, not caller. Symbols should be also
591 // decorated in some fancy way :) It doesn't support any vector arguments.
593 /// AddLiveIn - This helper function adds the specified physical register to the
594 /// MachineFunction as a live in value. It also creates a corresponding virtual
596 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
597 const TargetRegisterClass *RC) {
598 assert(RC->contains(PReg) && "Not the correct regclass!");
599 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
600 MF.addLiveIn(PReg, VReg);
604 /// HowToPassArgument - Returns how an formal argument of the specified type
605 /// should be passed. If it is through stack, returns the size of the stack
606 /// slot; if it is through integer or XMM register, returns the number of
607 /// integer or XMM registers are needed.
609 HowToPassCallArgument(MVT::ValueType ObjectVT,
611 unsigned NumIntRegs, unsigned NumXMMRegs,
612 unsigned MaxNumIntRegs,
613 unsigned &ObjSize, unsigned &ObjIntRegs,
614 unsigned &ObjXMMRegs) {
619 if (MaxNumIntRegs>3) {
620 // We don't have too much registers on ia32! :)
625 default: assert(0 && "Unhandled argument type!");
627 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
633 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
639 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
664 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
666 unsigned NumArgs = Op.Val->getNumValues() - 1;
667 MachineFunction &MF = DAG.getMachineFunction();
668 MachineFrameInfo *MFI = MF.getFrameInfo();
669 SDOperand Root = Op.getOperand(0);
670 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
672 SmallVector<CCValAssign, 16> ArgLocs;
673 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
676 for (unsigned i = 0; i != NumArgs; ++i) {
677 MVT::ValueType ArgVT = Op.getValue(i).getValueType();
678 unsigned ArgFlags = cast<ConstantSDNode>(Op.getOperand(3+i))->getValue();
679 if (CC_X86_32_C(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo))
680 assert(0 && "Unhandled argument type!");
683 SmallVector<SDOperand, 8> ArgValues;
684 unsigned LastVal = ~0U;
685 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
686 CCValAssign &VA = ArgLocs[i];
687 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
689 assert(VA.getValNo() != LastVal &&
690 "Don't support value assigned to multiple locs yet");
691 LastVal = VA.getValNo();
694 MVT::ValueType RegVT = VA.getLocVT();
695 TargetRegisterClass *RC;
696 if (RegVT == MVT::i32)
697 RC = X86::GR32RegisterClass;
699 assert(MVT::isVector(RegVT));
700 RC = X86::VR128RegisterClass;
703 SDOperand ArgValue = DAG.getCopyFromReg(Root, VA.getLocReg(), RegVT);
704 AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
706 // If this is an 8 or 16-bit value, it is really passed promoted to 32
707 // bits. Insert an assert[sz]ext to capture this, then truncate to the
709 if (VA.getLocInfo() == CCValAssign::SExt)
710 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
711 DAG.getValueType(VA.getValVT()));
712 else if (VA.getLocInfo() == CCValAssign::ZExt)
713 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
714 DAG.getValueType(VA.getValVT()));
716 if (VA.getLocInfo() != CCValAssign::Full)
717 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
719 ArgValues.push_back(ArgValue);
721 assert(VA.isMemLoc());
723 // Create the nodes corresponding to a load from this parameter slot.
724 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
725 VA.getLocMemOffset());
726 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
727 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
731 unsigned StackSize = CCInfo.getNextStackOffset();
733 ArgValues.push_back(Root);
735 // If the function takes variable number of arguments, make a frame index for
736 // the start of the first vararg value... for expansion of llvm.va_start.
738 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
740 if (isStdCall && !isVarArg) {
741 BytesToPopOnReturn = StackSize; // Callee pops everything..
742 BytesCallerReserves = 0;
744 BytesToPopOnReturn = 0; // Callee pops hidden struct pointer.
746 // If this is an sret function, the return should pop the hidden pointer.
747 if (NumArgs && (cast<ConstantSDNode>(Op.getOperand(3))->getValue() & 4))
748 BytesToPopOnReturn = 4;
750 BytesCallerReserves = StackSize;
753 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
754 ReturnAddrIndex = 0; // No return address slot generated yet.
756 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
758 // Return the new list of results.
759 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
760 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
763 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
765 SDOperand Chain = Op.getOperand(0);
766 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
767 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
768 SDOperand Callee = Op.getOperand(4);
769 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
771 SmallVector<CCValAssign, 16> ArgLocs;
772 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
774 for (unsigned i = 0; i != NumOps; ++i) {
775 MVT::ValueType ArgVT = Op.getOperand(5+2*i).getValueType();
776 unsigned ArgFlags =cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue();
777 if (CC_X86_32_C(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo))
778 assert(0 && "Unhandled argument type!");
781 // Get a count of how many bytes are to be pushed on the stack.
782 unsigned NumBytes = CCInfo.getNextStackOffset();
784 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
786 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
787 SmallVector<SDOperand, 8> MemOpChains;
791 // Walk the register/memloc assignments, inserting copies/loads.
792 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
793 CCValAssign &VA = ArgLocs[i];
794 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
796 // Promote the value if needed.
797 switch (VA.getLocInfo()) {
798 default: assert(0 && "Unknown loc info!");
799 case CCValAssign::Full: break;
800 case CCValAssign::SExt:
801 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
803 case CCValAssign::ZExt:
804 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
806 case CCValAssign::AExt:
807 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
812 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
814 assert(VA.isMemLoc());
815 if (StackPtr.Val == 0)
816 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
817 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
818 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
819 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
823 // If the first argument is an sret pointer, remember it.
824 bool isSRet = NumOps &&(cast<ConstantSDNode>(Op.getOperand(6))->getValue()&4);
826 if (!MemOpChains.empty())
827 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
828 &MemOpChains[0], MemOpChains.size());
830 // Build a sequence of copy-to-reg nodes chained together with token chain
831 // and flag operands which copy the outgoing args into registers.
833 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
834 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
836 InFlag = Chain.getValue(1);
839 // ELF / PIC requires GOT in the EBX register before function calls via PLT
841 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
842 Subtarget->isPICStyleGOT()) {
843 Chain = DAG.getCopyToReg(Chain, X86::EBX,
844 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
846 InFlag = Chain.getValue(1);
849 // If the callee is a GlobalAddress node (quite common, every direct call is)
850 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
851 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
852 // We should use extra load for direct calls to dllimported functions in
854 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
855 getTargetMachine(), true))
856 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
857 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
858 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
860 // Returns a chain & a flag for retval copy to use.
861 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
862 SmallVector<SDOperand, 8> Ops;
863 Ops.push_back(Chain);
864 Ops.push_back(Callee);
866 // Add argument registers to the end of the list so that they are known live
868 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
869 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
870 RegsToPass[i].second.getValueType()));
872 // Add an implicit use GOT pointer in EBX.
873 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
874 Subtarget->isPICStyleGOT())
875 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
878 Ops.push_back(InFlag);
880 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
881 NodeTys, &Ops[0], Ops.size());
882 InFlag = Chain.getValue(1);
884 // Create the CALLSEQ_END node.
885 unsigned NumBytesForCalleeToPush = 0;
887 if (CC == CallingConv::X86_StdCall) {
889 NumBytesForCalleeToPush = isSRet ? 4 : 0;
891 NumBytesForCalleeToPush = NumBytes;
893 // If this is is a call to a struct-return function, the callee
894 // pops the hidden struct pointer, so we have to push it back.
895 // This is common for Darwin/X86, Linux & Mingw32 targets.
896 NumBytesForCalleeToPush = isSRet ? 4 : 0;
899 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
901 Ops.push_back(Chain);
902 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
903 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
904 Ops.push_back(InFlag);
905 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
906 InFlag = Chain.getValue(1);
908 // Handle result values, copying them out of physregs into vregs that we
910 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
914 //===----------------------------------------------------------------------===//
915 // X86-64 C Calling Convention implementation
916 //===----------------------------------------------------------------------===//
921 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
922 unsigned NumArgs = Op.Val->getNumValues() - 1;
923 MachineFunction &MF = DAG.getMachineFunction();
924 MachineFrameInfo *MFI = MF.getFrameInfo();
925 SDOperand Root = Op.getOperand(0);
926 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
928 static const unsigned GPR64ArgRegs[] = {
929 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
931 static const unsigned XMMArgRegs[] = {
932 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
933 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
936 SmallVector<CCValAssign, 16> ArgLocs;
937 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
940 for (unsigned i = 0; i != NumArgs; ++i) {
941 MVT::ValueType ArgVT = Op.getValue(i).getValueType();
942 unsigned ArgFlags = cast<ConstantSDNode>(Op.getOperand(3+i))->getValue();
943 if (CC_X86_64_C(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo))
944 assert(0 && "Unhandled argument type!");
947 SmallVector<SDOperand, 8> ArgValues;
948 unsigned LastVal = ~0U;
949 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
950 CCValAssign &VA = ArgLocs[i];
951 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
953 assert(VA.getValNo() != LastVal &&
954 "Don't support value assigned to multiple locs yet");
955 LastVal = VA.getValNo();
958 MVT::ValueType RegVT = VA.getLocVT();
959 TargetRegisterClass *RC;
960 if (RegVT == MVT::i32)
961 RC = X86::GR32RegisterClass;
962 else if (RegVT == MVT::i64)
963 RC = X86::GR64RegisterClass;
964 else if (RegVT == MVT::f32)
965 RC = X86::FR32RegisterClass;
966 else if (RegVT == MVT::f64)
967 RC = X86::FR64RegisterClass;
969 assert(MVT::isVector(RegVT));
970 RC = X86::VR128RegisterClass;
973 SDOperand ArgValue = DAG.getCopyFromReg(Root, VA.getLocReg(), RegVT);
974 AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
976 // If this is an 8 or 16-bit value, it is really passed promoted to 32
977 // bits. Insert an assert[sz]ext to capture this, then truncate to the
979 if (VA.getLocInfo() == CCValAssign::SExt)
980 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
981 DAG.getValueType(VA.getValVT()));
982 else if (VA.getLocInfo() == CCValAssign::ZExt)
983 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
984 DAG.getValueType(VA.getValVT()));
986 if (VA.getLocInfo() != CCValAssign::Full)
987 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
989 ArgValues.push_back(ArgValue);
991 assert(VA.isMemLoc());
993 // Create the nodes corresponding to a load from this parameter slot.
994 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
995 VA.getLocMemOffset());
996 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
997 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1001 unsigned StackSize = CCInfo.getNextStackOffset();
1003 // If the function takes variable number of arguments, make a frame index for
1004 // the start of the first vararg value... for expansion of llvm.va_start.
1006 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1007 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1009 // For X86-64, if there are vararg parameters that are passed via
1010 // registers, then we must store them to their spots on the stack so they
1011 // may be loaded by deferencing the result of va_next.
1012 VarArgsGPOffset = NumIntRegs * 8;
1013 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1014 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1015 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1017 // Store the integer parameter registers.
1018 SmallVector<SDOperand, 8> MemOps;
1019 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1020 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1021 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1022 for (; NumIntRegs != 6; ++NumIntRegs) {
1023 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1024 X86::GR64RegisterClass);
1025 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1026 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1027 MemOps.push_back(Store);
1028 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1029 DAG.getConstant(8, getPointerTy()));
1032 // Now store the XMM (fp + vector) parameter registers.
1033 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1034 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1035 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1036 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1037 X86::VR128RegisterClass);
1038 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1039 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1040 MemOps.push_back(Store);
1041 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1042 DAG.getConstant(16, getPointerTy()));
1044 if (!MemOps.empty())
1045 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1046 &MemOps[0], MemOps.size());
1049 ArgValues.push_back(Root);
1051 ReturnAddrIndex = 0; // No return address slot generated yet.
1052 BytesToPopOnReturn = 0; // Callee pops nothing.
1053 BytesCallerReserves = StackSize;
1055 // Return the new list of results.
1056 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1057 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1061 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1063 SDOperand Chain = Op.getOperand(0);
1064 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1065 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1066 SDOperand Callee = Op.getOperand(4);
1067 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1069 SmallVector<CCValAssign, 16> ArgLocs;
1070 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
1072 for (unsigned i = 0; i != NumOps; ++i) {
1073 MVT::ValueType ArgVT = Op.getOperand(5+2*i).getValueType();
1074 unsigned ArgFlags =cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue();
1075 if (CC_X86_64_C(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo))
1076 assert(0 && "Unhandled argument type!");
1079 // Get a count of how many bytes are to be pushed on the stack.
1080 unsigned NumBytes = CCInfo.getNextStackOffset();
1081 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1083 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1084 SmallVector<SDOperand, 8> MemOpChains;
1088 // Walk the register/memloc assignments, inserting copies/loads.
1089 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1090 CCValAssign &VA = ArgLocs[i];
1091 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1093 // Promote the value if needed.
1094 switch (VA.getLocInfo()) {
1095 default: assert(0 && "Unknown loc info!");
1096 case CCValAssign::Full: break;
1097 case CCValAssign::SExt:
1098 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1100 case CCValAssign::ZExt:
1101 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1103 case CCValAssign::AExt:
1104 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1108 if (VA.isRegLoc()) {
1109 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1111 assert(VA.isMemLoc());
1112 if (StackPtr.Val == 0)
1113 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1114 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1115 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1116 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1120 if (!MemOpChains.empty())
1121 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1122 &MemOpChains[0], MemOpChains.size());
1124 // Build a sequence of copy-to-reg nodes chained together with token chain
1125 // and flag operands which copy the outgoing args into registers.
1127 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1128 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1130 InFlag = Chain.getValue(1);
1134 // From AMD64 ABI document:
1135 // For calls that may call functions that use varargs or stdargs
1136 // (prototype-less calls or calls to functions containing ellipsis (...) in
1137 // the declaration) %al is used as hidden argument to specify the number
1138 // of SSE registers used. The contents of %al do not need to match exactly
1139 // the number of registers, but must be an ubound on the number of SSE
1140 // registers used and is in the range 0 - 8 inclusive.
1142 // Count the number of XMM registers allocated.
1143 static const unsigned XMMArgRegs[] = {
1144 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1145 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1147 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1149 Chain = DAG.getCopyToReg(Chain, X86::AL,
1150 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1151 InFlag = Chain.getValue(1);
1154 // If the callee is a GlobalAddress node (quite common, every direct call is)
1155 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1156 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1157 // We should use extra load for direct calls to dllimported functions in
1159 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1160 getTargetMachine(), true))
1161 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1162 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1163 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1165 // Returns a chain & a flag for retval copy to use.
1166 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1167 SmallVector<SDOperand, 8> Ops;
1168 Ops.push_back(Chain);
1169 Ops.push_back(Callee);
1171 // Add argument registers to the end of the list so that they are known live
1173 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1174 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1175 RegsToPass[i].second.getValueType()));
1178 Ops.push_back(InFlag);
1180 // FIXME: Do not generate X86ISD::TAILCALL for now.
1181 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1182 NodeTys, &Ops[0], Ops.size());
1183 InFlag = Chain.getValue(1);
1185 // Returns a flag for retval copy to use.
1186 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1188 Ops.push_back(Chain);
1189 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1190 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1191 Ops.push_back(InFlag);
1192 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1193 InFlag = Chain.getValue(1);
1195 // Handle result values, copying them out of physregs into vregs that we
1197 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1200 //===----------------------------------------------------------------------===//
1201 // Fast & FastCall Calling Convention implementation
1202 //===----------------------------------------------------------------------===//
1204 // The X86 'fast' calling convention passes up to two integer arguments in
1205 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
1206 // and requires that the callee pop its arguments off the stack (allowing proper
1207 // tail calls), and has the same return value conventions as C calling convs.
1209 // This calling convention always arranges for the callee pop value to be 8n+4
1210 // bytes, which is needed for tail recursion elimination and stack alignment
1213 // Note that this can be enhanced in the future to pass fp vals in registers
1214 // (when we have a global fp allocator) and do other tricks.
1216 //===----------------------------------------------------------------------===//
1217 // The X86 'fastcall' calling convention passes up to two integer arguments in
1218 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1219 // and requires that the callee pop its arguments off the stack (allowing proper
1220 // tail calls), and has the same return value conventions as C calling convs.
1222 // This calling convention always arranges for the callee pop value to be 8n+4
1223 // bytes, which is needed for tail recursion elimination and stack alignment
1226 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1227 unsigned NumArgs = Op.Val->getNumValues()-1;
1228 MachineFunction &MF = DAG.getMachineFunction();
1229 MachineFrameInfo *MFI = MF.getFrameInfo();
1230 SDOperand Root = Op.getOperand(0);
1231 SmallVector<SDOperand, 8> ArgValues;
1233 // Add DAG nodes to load the arguments... On entry to a function the stack
1234 // frame looks like this:
1236 // [ESP] -- return address
1237 // [ESP + 4] -- first nonreg argument (leftmost lexically)
1238 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
1240 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1242 // Keep track of the number of integer regs passed so far. This can be either
1243 // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX
1245 unsigned NumIntRegs = 0;
1246 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1248 static const unsigned XMMArgRegs[] = {
1249 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1252 static const unsigned GPRArgRegs[][2] = {
1253 { X86::CL, X86::DL },
1254 { X86::CX, X86::DX },
1255 { X86::ECX, X86::EDX }
1258 static const TargetRegisterClass* GPRClasses[3] = {
1259 X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass
1262 for (unsigned i = 0; i < NumArgs; ++i) {
1263 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1264 unsigned ArgIncrement = 4;
1265 unsigned ObjSize = 0;
1266 unsigned ObjXMMRegs = 0;
1267 unsigned ObjIntRegs = 0;
1271 HowToPassCallArgument(ObjectVT,
1272 true, // Use as much registers as possible
1273 NumIntRegs, NumXMMRegs, 2,
1274 ObjSize, ObjIntRegs, ObjXMMRegs);
1277 ArgIncrement = ObjSize;
1279 if (ObjIntRegs || ObjXMMRegs) {
1281 default: assert(0 && "Unhandled argument type!");
1285 unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][NumIntRegs];
1286 Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]);
1287 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1296 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
1297 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1301 NumIntRegs += ObjIntRegs;
1302 NumXMMRegs += ObjXMMRegs;
1305 // XMM arguments have to be aligned on 16-byte boundary.
1307 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1308 // Create the SelectionDAG nodes corresponding to a load from this
1310 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1311 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1312 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1314 ArgOffset += ArgIncrement; // Move on to the next argument.
1317 ArgValues.push_back(ArgValue);
1320 ArgValues.push_back(Root);
1322 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1323 // arguments and the arguments after the retaddr has been pushed are aligned.
1324 if ((ArgOffset & 7) == 0)
1327 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1328 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1329 ReturnAddrIndex = 0; // No return address slot generated yet.
1330 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1331 BytesCallerReserves = 0;
1333 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
1335 // Finally, inform the code generator which regs we return values in.
1336 switch (getValueType(MF.getFunction()->getReturnType())) {
1337 default: assert(0 && "Unknown type!");
1338 case MVT::isVoid: break;
1343 MF.addLiveOut(X86::EAX);
1346 MF.addLiveOut(X86::EAX);
1347 MF.addLiveOut(X86::EDX);
1351 MF.addLiveOut(X86::ST0);
1359 MF.addLiveOut(X86::XMM0);
1363 // Return the new list of results.
1364 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1365 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1368 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1370 SDOperand Chain = Op.getOperand(0);
1371 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1372 SDOperand Callee = Op.getOperand(4);
1373 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1375 // Count how many bytes are to be pushed on the stack.
1376 unsigned NumBytes = 0;
1378 // Keep track of the number of integer regs passed so far. This can be either
1379 // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX
1381 unsigned NumIntRegs = 0;
1382 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1384 static const unsigned GPRArgRegs[][2] = {
1385 { X86::CL, X86::DL },
1386 { X86::CX, X86::DX },
1387 { X86::ECX, X86::EDX }
1389 static const unsigned XMMArgRegs[] = {
1390 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1393 for (unsigned i = 0; i != NumOps; ++i) {
1394 SDOperand Arg = Op.getOperand(5+2*i);
1396 switch (Arg.getValueType()) {
1397 default: assert(0 && "Unknown value type!");
1401 if (NumIntRegs < 2) {
1420 // XMM arguments have to be aligned on 16-byte boundary.
1421 NumBytes = ((NumBytes + 15) / 16) * 16;
1428 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1429 // arguments and the arguments after the retaddr has been pushed are aligned.
1430 if ((NumBytes & 7) == 0)
1433 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1435 // Arguments go on the stack in reverse order, as specified by the ABI.
1436 unsigned ArgOffset = 0;
1438 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1439 SmallVector<SDOperand, 8> MemOpChains;
1440 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1441 for (unsigned i = 0; i != NumOps; ++i) {
1442 SDOperand Arg = Op.getOperand(5+2*i);
1444 switch (Arg.getValueType()) {
1445 default: assert(0 && "Unexpected ValueType for argument!");
1449 if (NumIntRegs < 2) {
1451 GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs];
1452 RegsToPass.push_back(std::make_pair(RegToUse, Arg));
1457 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1458 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1459 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1464 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1465 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1466 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1476 if (NumXMMRegs < 4) {
1477 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1480 // XMM arguments have to be aligned on 16-byte boundary.
1481 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1482 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1483 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1484 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1491 if (!MemOpChains.empty())
1492 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1493 &MemOpChains[0], MemOpChains.size());
1495 // Build a sequence of copy-to-reg nodes chained together with token chain
1496 // and flag operands which copy the outgoing args into registers.
1498 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1499 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1501 InFlag = Chain.getValue(1);
1504 // If the callee is a GlobalAddress node (quite common, every direct call is)
1505 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1506 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1507 // We should use extra load for direct calls to dllimported functions in
1509 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1510 getTargetMachine(), true))
1511 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1512 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1513 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1515 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1517 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1518 Subtarget->isPICStyleGOT()) {
1519 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1520 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1522 InFlag = Chain.getValue(1);
1525 // Returns a chain & a flag for retval copy to use.
1526 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1527 SmallVector<SDOperand, 8> Ops;
1528 Ops.push_back(Chain);
1529 Ops.push_back(Callee);
1531 // Add argument registers to the end of the list so that they are known live
1533 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1534 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1535 RegsToPass[i].second.getValueType()));
1537 // Add an implicit use GOT pointer in EBX.
1538 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1539 Subtarget->isPICStyleGOT())
1540 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1543 Ops.push_back(InFlag);
1545 // FIXME: Do not generate X86ISD::TAILCALL for now.
1546 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1547 NodeTys, &Ops[0], Ops.size());
1548 InFlag = Chain.getValue(1);
1550 // Returns a flag for retval copy to use.
1551 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1553 Ops.push_back(Chain);
1554 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1555 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1556 Ops.push_back(InFlag);
1557 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1558 InFlag = Chain.getValue(1);
1560 // Handle result values, copying them out of physregs into vregs that we
1562 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1565 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1566 if (ReturnAddrIndex == 0) {
1567 // Set up a frame object for the return address.
1568 MachineFunction &MF = DAG.getMachineFunction();
1569 if (Subtarget->is64Bit())
1570 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1572 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1575 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1580 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1581 /// specific condition code. It returns a false if it cannot do a direct
1582 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1584 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1585 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1586 SelectionDAG &DAG) {
1587 X86CC = X86::COND_INVALID;
1589 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1590 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1591 // X > -1 -> X == 0, jump !sign.
1592 RHS = DAG.getConstant(0, RHS.getValueType());
1593 X86CC = X86::COND_NS;
1595 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1596 // X < 0 -> X == 0, jump on sign.
1597 X86CC = X86::COND_S;
1602 switch (SetCCOpcode) {
1604 case ISD::SETEQ: X86CC = X86::COND_E; break;
1605 case ISD::SETGT: X86CC = X86::COND_G; break;
1606 case ISD::SETGE: X86CC = X86::COND_GE; break;
1607 case ISD::SETLT: X86CC = X86::COND_L; break;
1608 case ISD::SETLE: X86CC = X86::COND_LE; break;
1609 case ISD::SETNE: X86CC = X86::COND_NE; break;
1610 case ISD::SETULT: X86CC = X86::COND_B; break;
1611 case ISD::SETUGT: X86CC = X86::COND_A; break;
1612 case ISD::SETULE: X86CC = X86::COND_BE; break;
1613 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1616 // On a floating point condition, the flags are set as follows:
1618 // 0 | 0 | 0 | X > Y
1619 // 0 | 0 | 1 | X < Y
1620 // 1 | 0 | 0 | X == Y
1621 // 1 | 1 | 1 | unordered
1623 switch (SetCCOpcode) {
1626 case ISD::SETEQ: X86CC = X86::COND_E; break;
1627 case ISD::SETOLT: Flip = true; // Fallthrough
1629 case ISD::SETGT: X86CC = X86::COND_A; break;
1630 case ISD::SETOLE: Flip = true; // Fallthrough
1632 case ISD::SETGE: X86CC = X86::COND_AE; break;
1633 case ISD::SETUGT: Flip = true; // Fallthrough
1635 case ISD::SETLT: X86CC = X86::COND_B; break;
1636 case ISD::SETUGE: Flip = true; // Fallthrough
1638 case ISD::SETLE: X86CC = X86::COND_BE; break;
1640 case ISD::SETNE: X86CC = X86::COND_NE; break;
1641 case ISD::SETUO: X86CC = X86::COND_P; break;
1642 case ISD::SETO: X86CC = X86::COND_NP; break;
1645 std::swap(LHS, RHS);
1648 return X86CC != X86::COND_INVALID;
1651 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1652 /// code. Current x86 isa includes the following FP cmov instructions:
1653 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1654 static bool hasFPCMov(unsigned X86CC) {
1670 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1671 /// true if Op is undef or if its value falls within the specified range (L, H].
1672 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1673 if (Op.getOpcode() == ISD::UNDEF)
1676 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1677 return (Val >= Low && Val < Hi);
1680 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1681 /// true if Op is undef or if its value equal to the specified value.
1682 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1683 if (Op.getOpcode() == ISD::UNDEF)
1685 return cast<ConstantSDNode>(Op)->getValue() == Val;
1688 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1689 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1690 bool X86::isPSHUFDMask(SDNode *N) {
1691 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1693 if (N->getNumOperands() != 4)
1696 // Check if the value doesn't reference the second vector.
1697 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1698 SDOperand Arg = N->getOperand(i);
1699 if (Arg.getOpcode() == ISD::UNDEF) continue;
1700 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1701 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1708 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1709 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1710 bool X86::isPSHUFHWMask(SDNode *N) {
1711 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1713 if (N->getNumOperands() != 8)
1716 // Lower quadword copied in order.
1717 for (unsigned i = 0; i != 4; ++i) {
1718 SDOperand Arg = N->getOperand(i);
1719 if (Arg.getOpcode() == ISD::UNDEF) continue;
1720 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1721 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1725 // Upper quadword shuffled.
1726 for (unsigned i = 4; i != 8; ++i) {
1727 SDOperand Arg = N->getOperand(i);
1728 if (Arg.getOpcode() == ISD::UNDEF) continue;
1729 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1730 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1731 if (Val < 4 || Val > 7)
1738 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1739 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1740 bool X86::isPSHUFLWMask(SDNode *N) {
1741 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1743 if (N->getNumOperands() != 8)
1746 // Upper quadword copied in order.
1747 for (unsigned i = 4; i != 8; ++i)
1748 if (!isUndefOrEqual(N->getOperand(i), i))
1751 // Lower quadword shuffled.
1752 for (unsigned i = 0; i != 4; ++i)
1753 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1759 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1760 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1761 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1762 if (NumElems != 2 && NumElems != 4) return false;
1764 unsigned Half = NumElems / 2;
1765 for (unsigned i = 0; i < Half; ++i)
1766 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1768 for (unsigned i = Half; i < NumElems; ++i)
1769 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1775 bool X86::isSHUFPMask(SDNode *N) {
1776 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1777 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1780 /// isCommutedSHUFP - Returns true if the shuffle mask is except
1781 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1782 /// half elements to come from vector 1 (which would equal the dest.) and
1783 /// the upper half to come from vector 2.
1784 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1785 if (NumOps != 2 && NumOps != 4) return false;
1787 unsigned Half = NumOps / 2;
1788 for (unsigned i = 0; i < Half; ++i)
1789 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1791 for (unsigned i = Half; i < NumOps; ++i)
1792 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1797 static bool isCommutedSHUFP(SDNode *N) {
1798 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1799 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1802 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1803 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1804 bool X86::isMOVHLPSMask(SDNode *N) {
1805 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1807 if (N->getNumOperands() != 4)
1810 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1811 return isUndefOrEqual(N->getOperand(0), 6) &&
1812 isUndefOrEqual(N->getOperand(1), 7) &&
1813 isUndefOrEqual(N->getOperand(2), 2) &&
1814 isUndefOrEqual(N->getOperand(3), 3);
1817 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1818 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1820 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1821 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1823 if (N->getNumOperands() != 4)
1826 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1827 return isUndefOrEqual(N->getOperand(0), 2) &&
1828 isUndefOrEqual(N->getOperand(1), 3) &&
1829 isUndefOrEqual(N->getOperand(2), 2) &&
1830 isUndefOrEqual(N->getOperand(3), 3);
1833 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1834 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1835 bool X86::isMOVLPMask(SDNode *N) {
1836 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1838 unsigned NumElems = N->getNumOperands();
1839 if (NumElems != 2 && NumElems != 4)
1842 for (unsigned i = 0; i < NumElems/2; ++i)
1843 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1846 for (unsigned i = NumElems/2; i < NumElems; ++i)
1847 if (!isUndefOrEqual(N->getOperand(i), i))
1853 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1854 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1856 bool X86::isMOVHPMask(SDNode *N) {
1857 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1859 unsigned NumElems = N->getNumOperands();
1860 if (NumElems != 2 && NumElems != 4)
1863 for (unsigned i = 0; i < NumElems/2; ++i)
1864 if (!isUndefOrEqual(N->getOperand(i), i))
1867 for (unsigned i = 0; i < NumElems/2; ++i) {
1868 SDOperand Arg = N->getOperand(i + NumElems/2);
1869 if (!isUndefOrEqual(Arg, i + NumElems))
1876 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1877 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1878 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1879 bool V2IsSplat = false) {
1880 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1883 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1884 SDOperand BitI = Elts[i];
1885 SDOperand BitI1 = Elts[i+1];
1886 if (!isUndefOrEqual(BitI, j))
1889 if (isUndefOrEqual(BitI1, NumElts))
1892 if (!isUndefOrEqual(BitI1, j + NumElts))
1900 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1901 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1902 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1905 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1906 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1907 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1908 bool V2IsSplat = false) {
1909 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1912 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1913 SDOperand BitI = Elts[i];
1914 SDOperand BitI1 = Elts[i+1];
1915 if (!isUndefOrEqual(BitI, j + NumElts/2))
1918 if (isUndefOrEqual(BitI1, NumElts))
1921 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1929 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1930 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1931 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1934 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1935 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1937 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1938 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1940 unsigned NumElems = N->getNumOperands();
1941 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1944 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1945 SDOperand BitI = N->getOperand(i);
1946 SDOperand BitI1 = N->getOperand(i+1);
1948 if (!isUndefOrEqual(BitI, j))
1950 if (!isUndefOrEqual(BitI1, j))
1957 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1958 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1959 /// MOVSD, and MOVD, i.e. setting the lowest element.
1960 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1961 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1964 if (!isUndefOrEqual(Elts[0], NumElts))
1967 for (unsigned i = 1; i < NumElts; ++i) {
1968 if (!isUndefOrEqual(Elts[i], i))
1975 bool X86::isMOVLMask(SDNode *N) {
1976 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1977 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
1980 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1981 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1982 /// element of vector 2 and the other elements to come from vector 1 in order.
1983 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1984 bool V2IsSplat = false,
1985 bool V2IsUndef = false) {
1986 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
1989 if (!isUndefOrEqual(Ops[0], 0))
1992 for (unsigned i = 1; i < NumOps; ++i) {
1993 SDOperand Arg = Ops[i];
1994 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1995 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1996 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2003 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2004 bool V2IsUndef = false) {
2005 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2006 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2007 V2IsSplat, V2IsUndef);
2010 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2011 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2012 bool X86::isMOVSHDUPMask(SDNode *N) {
2013 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2015 if (N->getNumOperands() != 4)
2018 // Expect 1, 1, 3, 3
2019 for (unsigned i = 0; i < 2; ++i) {
2020 SDOperand Arg = N->getOperand(i);
2021 if (Arg.getOpcode() == ISD::UNDEF) continue;
2022 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2023 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2024 if (Val != 1) return false;
2028 for (unsigned i = 2; i < 4; ++i) {
2029 SDOperand Arg = N->getOperand(i);
2030 if (Arg.getOpcode() == ISD::UNDEF) continue;
2031 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2032 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2033 if (Val != 3) return false;
2037 // Don't use movshdup if it can be done with a shufps.
2041 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2042 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2043 bool X86::isMOVSLDUPMask(SDNode *N) {
2044 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2046 if (N->getNumOperands() != 4)
2049 // Expect 0, 0, 2, 2
2050 for (unsigned i = 0; i < 2; ++i) {
2051 SDOperand Arg = N->getOperand(i);
2052 if (Arg.getOpcode() == ISD::UNDEF) continue;
2053 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2054 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2055 if (Val != 0) return false;
2059 for (unsigned i = 2; i < 4; ++i) {
2060 SDOperand Arg = N->getOperand(i);
2061 if (Arg.getOpcode() == ISD::UNDEF) continue;
2062 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2063 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2064 if (Val != 2) return false;
2068 // Don't use movshdup if it can be done with a shufps.
2072 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2073 /// a splat of a single element.
2074 static bool isSplatMask(SDNode *N) {
2075 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2077 // This is a splat operation if each element of the permute is the same, and
2078 // if the value doesn't reference the second vector.
2079 unsigned NumElems = N->getNumOperands();
2080 SDOperand ElementBase;
2082 for (; i != NumElems; ++i) {
2083 SDOperand Elt = N->getOperand(i);
2084 if (isa<ConstantSDNode>(Elt)) {
2090 if (!ElementBase.Val)
2093 for (; i != NumElems; ++i) {
2094 SDOperand Arg = N->getOperand(i);
2095 if (Arg.getOpcode() == ISD::UNDEF) continue;
2096 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2097 if (Arg != ElementBase) return false;
2100 // Make sure it is a splat of the first vector operand.
2101 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2104 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2105 /// a splat of a single element and it's a 2 or 4 element mask.
2106 bool X86::isSplatMask(SDNode *N) {
2107 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2109 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2110 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2112 return ::isSplatMask(N);
2115 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2116 /// specifies a splat of zero element.
2117 bool X86::isSplatLoMask(SDNode *N) {
2118 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2120 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2121 if (!isUndefOrEqual(N->getOperand(i), 0))
2126 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2127 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2129 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2130 unsigned NumOperands = N->getNumOperands();
2131 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2133 for (unsigned i = 0; i < NumOperands; ++i) {
2135 SDOperand Arg = N->getOperand(NumOperands-i-1);
2136 if (Arg.getOpcode() != ISD::UNDEF)
2137 Val = cast<ConstantSDNode>(Arg)->getValue();
2138 if (Val >= NumOperands) Val -= NumOperands;
2140 if (i != NumOperands - 1)
2147 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2148 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2150 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2152 // 8 nodes, but we only care about the last 4.
2153 for (unsigned i = 7; i >= 4; --i) {
2155 SDOperand Arg = N->getOperand(i);
2156 if (Arg.getOpcode() != ISD::UNDEF)
2157 Val = cast<ConstantSDNode>(Arg)->getValue();
2166 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2167 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2169 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2171 // 8 nodes, but we only care about the first 4.
2172 for (int i = 3; i >= 0; --i) {
2174 SDOperand Arg = N->getOperand(i);
2175 if (Arg.getOpcode() != ISD::UNDEF)
2176 Val = cast<ConstantSDNode>(Arg)->getValue();
2185 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2186 /// specifies a 8 element shuffle that can be broken into a pair of
2187 /// PSHUFHW and PSHUFLW.
2188 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2189 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2191 if (N->getNumOperands() != 8)
2194 // Lower quadword shuffled.
2195 for (unsigned i = 0; i != 4; ++i) {
2196 SDOperand Arg = N->getOperand(i);
2197 if (Arg.getOpcode() == ISD::UNDEF) continue;
2198 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2199 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2204 // Upper quadword shuffled.
2205 for (unsigned i = 4; i != 8; ++i) {
2206 SDOperand Arg = N->getOperand(i);
2207 if (Arg.getOpcode() == ISD::UNDEF) continue;
2208 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2209 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2210 if (Val < 4 || Val > 7)
2217 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2218 /// values in ther permute mask.
2219 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2220 SDOperand &V2, SDOperand &Mask,
2221 SelectionDAG &DAG) {
2222 MVT::ValueType VT = Op.getValueType();
2223 MVT::ValueType MaskVT = Mask.getValueType();
2224 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2225 unsigned NumElems = Mask.getNumOperands();
2226 SmallVector<SDOperand, 8> MaskVec;
2228 for (unsigned i = 0; i != NumElems; ++i) {
2229 SDOperand Arg = Mask.getOperand(i);
2230 if (Arg.getOpcode() == ISD::UNDEF) {
2231 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2234 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2235 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2237 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2239 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2243 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2244 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2247 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2248 /// match movhlps. The lower half elements should come from upper half of
2249 /// V1 (and in order), and the upper half elements should come from the upper
2250 /// half of V2 (and in order).
2251 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2252 unsigned NumElems = Mask->getNumOperands();
2255 for (unsigned i = 0, e = 2; i != e; ++i)
2256 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2258 for (unsigned i = 2; i != 4; ++i)
2259 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2264 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2265 /// is promoted to a vector.
2266 static inline bool isScalarLoadToVector(SDNode *N) {
2267 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2268 N = N->getOperand(0).Val;
2269 return ISD::isNON_EXTLoad(N);
2274 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2275 /// match movlp{s|d}. The lower half elements should come from lower half of
2276 /// V1 (and in order), and the upper half elements should come from the upper
2277 /// half of V2 (and in order). And since V1 will become the source of the
2278 /// MOVLP, it must be either a vector load or a scalar load to vector.
2279 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2280 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2282 // Is V2 is a vector load, don't do this transformation. We will try to use
2283 // load folding shufps op.
2284 if (ISD::isNON_EXTLoad(V2))
2287 unsigned NumElems = Mask->getNumOperands();
2288 if (NumElems != 2 && NumElems != 4)
2290 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2291 if (!isUndefOrEqual(Mask->getOperand(i), i))
2293 for (unsigned i = NumElems/2; i != NumElems; ++i)
2294 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2299 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2301 static bool isSplatVector(SDNode *N) {
2302 if (N->getOpcode() != ISD::BUILD_VECTOR)
2305 SDOperand SplatValue = N->getOperand(0);
2306 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2307 if (N->getOperand(i) != SplatValue)
2312 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2314 static bool isUndefShuffle(SDNode *N) {
2315 if (N->getOpcode() != ISD::BUILD_VECTOR)
2318 SDOperand V1 = N->getOperand(0);
2319 SDOperand V2 = N->getOperand(1);
2320 SDOperand Mask = N->getOperand(2);
2321 unsigned NumElems = Mask.getNumOperands();
2322 for (unsigned i = 0; i != NumElems; ++i) {
2323 SDOperand Arg = Mask.getOperand(i);
2324 if (Arg.getOpcode() != ISD::UNDEF) {
2325 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2326 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2328 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2335 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2336 /// that point to V2 points to its first element.
2337 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2338 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2340 bool Changed = false;
2341 SmallVector<SDOperand, 8> MaskVec;
2342 unsigned NumElems = Mask.getNumOperands();
2343 for (unsigned i = 0; i != NumElems; ++i) {
2344 SDOperand Arg = Mask.getOperand(i);
2345 if (Arg.getOpcode() != ISD::UNDEF) {
2346 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2347 if (Val > NumElems) {
2348 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2352 MaskVec.push_back(Arg);
2356 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2357 &MaskVec[0], MaskVec.size());
2361 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2362 /// operation of specified width.
2363 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2364 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2365 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2367 SmallVector<SDOperand, 8> MaskVec;
2368 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2369 for (unsigned i = 1; i != NumElems; ++i)
2370 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2371 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2374 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2375 /// of specified width.
2376 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2377 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2378 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2379 SmallVector<SDOperand, 8> MaskVec;
2380 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2381 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2382 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2384 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2387 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2388 /// of specified width.
2389 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2390 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2391 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2392 unsigned Half = NumElems/2;
2393 SmallVector<SDOperand, 8> MaskVec;
2394 for (unsigned i = 0; i != Half; ++i) {
2395 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2396 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2398 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2401 /// getZeroVector - Returns a vector of specified type with all zero elements.
2403 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2404 assert(MVT::isVector(VT) && "Expected a vector type");
2405 unsigned NumElems = getVectorNumElements(VT);
2406 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2407 bool isFP = MVT::isFloatingPoint(EVT);
2408 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2409 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2410 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2413 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2415 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2416 SDOperand V1 = Op.getOperand(0);
2417 SDOperand Mask = Op.getOperand(2);
2418 MVT::ValueType VT = Op.getValueType();
2419 unsigned NumElems = Mask.getNumOperands();
2420 Mask = getUnpacklMask(NumElems, DAG);
2421 while (NumElems != 4) {
2422 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2425 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2427 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2428 Mask = getZeroVector(MaskVT, DAG);
2429 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2430 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2431 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2434 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2436 static inline bool isZeroNode(SDOperand Elt) {
2437 return ((isa<ConstantSDNode>(Elt) &&
2438 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2439 (isa<ConstantFPSDNode>(Elt) &&
2440 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2443 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2444 /// vector and zero or undef vector.
2445 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2446 unsigned NumElems, unsigned Idx,
2447 bool isZero, SelectionDAG &DAG) {
2448 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2449 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2450 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2451 SDOperand Zero = DAG.getConstant(0, EVT);
2452 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2453 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2454 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2455 &MaskVec[0], MaskVec.size());
2456 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2459 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2461 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2462 unsigned NumNonZero, unsigned NumZero,
2463 SelectionDAG &DAG, TargetLowering &TLI) {
2469 for (unsigned i = 0; i < 16; ++i) {
2470 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2471 if (ThisIsNonZero && First) {
2473 V = getZeroVector(MVT::v8i16, DAG);
2475 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2480 SDOperand ThisElt(0, 0), LastElt(0, 0);
2481 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2482 if (LastIsNonZero) {
2483 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2485 if (ThisIsNonZero) {
2486 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2487 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2488 ThisElt, DAG.getConstant(8, MVT::i8));
2490 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2495 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2496 DAG.getConstant(i/2, TLI.getPointerTy()));
2500 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2503 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2505 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2506 unsigned NumNonZero, unsigned NumZero,
2507 SelectionDAG &DAG, TargetLowering &TLI) {
2513 for (unsigned i = 0; i < 8; ++i) {
2514 bool isNonZero = (NonZeros & (1 << i)) != 0;
2518 V = getZeroVector(MVT::v8i16, DAG);
2520 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2523 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2524 DAG.getConstant(i, TLI.getPointerTy()));
2532 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2533 // All zero's are handled with pxor.
2534 if (ISD::isBuildVectorAllZeros(Op.Val))
2537 // All one's are handled with pcmpeqd.
2538 if (ISD::isBuildVectorAllOnes(Op.Val))
2541 MVT::ValueType VT = Op.getValueType();
2542 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2543 unsigned EVTBits = MVT::getSizeInBits(EVT);
2545 unsigned NumElems = Op.getNumOperands();
2546 unsigned NumZero = 0;
2547 unsigned NumNonZero = 0;
2548 unsigned NonZeros = 0;
2549 std::set<SDOperand> Values;
2550 for (unsigned i = 0; i < NumElems; ++i) {
2551 SDOperand Elt = Op.getOperand(i);
2552 if (Elt.getOpcode() != ISD::UNDEF) {
2554 if (isZeroNode(Elt))
2557 NonZeros |= (1 << i);
2563 if (NumNonZero == 0)
2564 // Must be a mix of zero and undef. Return a zero vector.
2565 return getZeroVector(VT, DAG);
2567 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2568 if (Values.size() == 1)
2571 // Special case for single non-zero element.
2572 if (NumNonZero == 1) {
2573 unsigned Idx = CountTrailingZeros_32(NonZeros);
2574 SDOperand Item = Op.getOperand(Idx);
2575 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2577 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2578 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2581 if (EVTBits == 32) {
2582 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2583 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2585 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2586 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2587 SmallVector<SDOperand, 8> MaskVec;
2588 for (unsigned i = 0; i < NumElems; i++)
2589 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2590 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2591 &MaskVec[0], MaskVec.size());
2592 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2593 DAG.getNode(ISD::UNDEF, VT), Mask);
2597 // Let legalizer expand 2-wide build_vector's.
2601 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2603 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2605 if (V.Val) return V;
2608 if (EVTBits == 16) {
2609 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2611 if (V.Val) return V;
2614 // If element VT is == 32 bits, turn it into a number of shuffles.
2615 SmallVector<SDOperand, 8> V;
2617 if (NumElems == 4 && NumZero > 0) {
2618 for (unsigned i = 0; i < 4; ++i) {
2619 bool isZero = !(NonZeros & (1 << i));
2621 V[i] = getZeroVector(VT, DAG);
2623 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2626 for (unsigned i = 0; i < 2; ++i) {
2627 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2630 V[i] = V[i*2]; // Must be a zero vector.
2633 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2634 getMOVLMask(NumElems, DAG));
2637 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2638 getMOVLMask(NumElems, DAG));
2641 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2642 getUnpacklMask(NumElems, DAG));
2647 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2648 // clears the upper bits.
2649 // FIXME: we can do the same for v4f32 case when we know both parts of
2650 // the lower half come from scalar_to_vector (loadf32). We should do
2651 // that in post legalizer dag combiner with target specific hooks.
2652 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2654 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2655 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2656 SmallVector<SDOperand, 8> MaskVec;
2657 bool Reverse = (NonZeros & 0x3) == 2;
2658 for (unsigned i = 0; i < 2; ++i)
2660 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2662 MaskVec.push_back(DAG.getConstant(i, EVT));
2663 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2664 for (unsigned i = 0; i < 2; ++i)
2666 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2668 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2669 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2670 &MaskVec[0], MaskVec.size());
2671 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2674 if (Values.size() > 2) {
2675 // Expand into a number of unpckl*.
2677 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2678 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2679 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2680 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2681 for (unsigned i = 0; i < NumElems; ++i)
2682 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2684 while (NumElems != 0) {
2685 for (unsigned i = 0; i < NumElems; ++i)
2686 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2697 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2698 SDOperand V1 = Op.getOperand(0);
2699 SDOperand V2 = Op.getOperand(1);
2700 SDOperand PermMask = Op.getOperand(2);
2701 MVT::ValueType VT = Op.getValueType();
2702 unsigned NumElems = PermMask.getNumOperands();
2703 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2704 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2705 bool V1IsSplat = false;
2706 bool V2IsSplat = false;
2708 if (isUndefShuffle(Op.Val))
2709 return DAG.getNode(ISD::UNDEF, VT);
2711 if (isSplatMask(PermMask.Val)) {
2712 if (NumElems <= 4) return Op;
2713 // Promote it to a v4i32 splat.
2714 return PromoteSplat(Op, DAG);
2717 if (X86::isMOVLMask(PermMask.Val))
2718 return (V1IsUndef) ? V2 : Op;
2720 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2721 X86::isMOVSLDUPMask(PermMask.Val) ||
2722 X86::isMOVHLPSMask(PermMask.Val) ||
2723 X86::isMOVHPMask(PermMask.Val) ||
2724 X86::isMOVLPMask(PermMask.Val))
2727 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2728 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2729 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2731 bool Commuted = false;
2732 V1IsSplat = isSplatVector(V1.Val);
2733 V2IsSplat = isSplatVector(V2.Val);
2734 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2735 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2736 std::swap(V1IsSplat, V2IsSplat);
2737 std::swap(V1IsUndef, V2IsUndef);
2741 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2742 if (V2IsUndef) return V1;
2743 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2745 // V2 is a splat, so the mask may be malformed. That is, it may point
2746 // to any V2 element. The instruction selectior won't like this. Get
2747 // a corrected mask and commute to form a proper MOVS{S|D}.
2748 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2749 if (NewMask.Val != PermMask.Val)
2750 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2755 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2756 X86::isUNPCKLMask(PermMask.Val) ||
2757 X86::isUNPCKHMask(PermMask.Val))
2761 // Normalize mask so all entries that point to V2 points to its first
2762 // element then try to match unpck{h|l} again. If match, return a
2763 // new vector_shuffle with the corrected mask.
2764 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2765 if (NewMask.Val != PermMask.Val) {
2766 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2767 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2768 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2769 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2770 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2771 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2776 // Normalize the node to match x86 shuffle ops if needed
2777 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2778 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2781 // Commute is back and try unpck* again.
2782 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2783 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2784 X86::isUNPCKLMask(PermMask.Val) ||
2785 X86::isUNPCKHMask(PermMask.Val))
2789 // If VT is integer, try PSHUF* first, then SHUFP*.
2790 if (MVT::isInteger(VT)) {
2791 if (X86::isPSHUFDMask(PermMask.Val) ||
2792 X86::isPSHUFHWMask(PermMask.Val) ||
2793 X86::isPSHUFLWMask(PermMask.Val)) {
2794 if (V2.getOpcode() != ISD::UNDEF)
2795 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2796 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2800 if (X86::isSHUFPMask(PermMask.Val))
2803 // Handle v8i16 shuffle high / low shuffle node pair.
2804 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2805 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2806 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2807 SmallVector<SDOperand, 8> MaskVec;
2808 for (unsigned i = 0; i != 4; ++i)
2809 MaskVec.push_back(PermMask.getOperand(i));
2810 for (unsigned i = 4; i != 8; ++i)
2811 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2812 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2813 &MaskVec[0], MaskVec.size());
2814 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2816 for (unsigned i = 0; i != 4; ++i)
2817 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2818 for (unsigned i = 4; i != 8; ++i)
2819 MaskVec.push_back(PermMask.getOperand(i));
2820 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2821 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2824 // Floating point cases in the other order.
2825 if (X86::isSHUFPMask(PermMask.Val))
2827 if (X86::isPSHUFDMask(PermMask.Val) ||
2828 X86::isPSHUFHWMask(PermMask.Val) ||
2829 X86::isPSHUFLWMask(PermMask.Val)) {
2830 if (V2.getOpcode() != ISD::UNDEF)
2831 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2832 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2837 if (NumElems == 4) {
2838 MVT::ValueType MaskVT = PermMask.getValueType();
2839 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2840 SmallVector<std::pair<int, int>, 8> Locs;
2841 Locs.reserve(NumElems);
2842 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2843 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2846 // If no more than two elements come from either vector. This can be
2847 // implemented with two shuffles. First shuffle gather the elements.
2848 // The second shuffle, which takes the first shuffle as both of its
2849 // vector operands, put the elements into the right order.
2850 for (unsigned i = 0; i != NumElems; ++i) {
2851 SDOperand Elt = PermMask.getOperand(i);
2852 if (Elt.getOpcode() == ISD::UNDEF) {
2853 Locs[i] = std::make_pair(-1, -1);
2855 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2856 if (Val < NumElems) {
2857 Locs[i] = std::make_pair(0, NumLo);
2861 Locs[i] = std::make_pair(1, NumHi);
2862 if (2+NumHi < NumElems)
2863 Mask1[2+NumHi] = Elt;
2868 if (NumLo <= 2 && NumHi <= 2) {
2869 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2870 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2871 &Mask1[0], Mask1.size()));
2872 for (unsigned i = 0; i != NumElems; ++i) {
2873 if (Locs[i].first == -1)
2876 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2877 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2878 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2882 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2883 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2884 &Mask2[0], Mask2.size()));
2887 // Break it into (shuffle shuffle_hi, shuffle_lo).
2889 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2890 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2891 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
2892 unsigned MaskIdx = 0;
2894 unsigned HiIdx = NumElems/2;
2895 for (unsigned i = 0; i != NumElems; ++i) {
2896 if (i == NumElems/2) {
2902 SDOperand Elt = PermMask.getOperand(i);
2903 if (Elt.getOpcode() == ISD::UNDEF) {
2904 Locs[i] = std::make_pair(-1, -1);
2905 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2906 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2907 (*MaskPtr)[LoIdx] = Elt;
2910 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2911 (*MaskPtr)[HiIdx] = Elt;
2916 SDOperand LoShuffle =
2917 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2918 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2919 &LoMask[0], LoMask.size()));
2920 SDOperand HiShuffle =
2921 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2922 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2923 &HiMask[0], HiMask.size()));
2924 SmallVector<SDOperand, 8> MaskOps;
2925 for (unsigned i = 0; i != NumElems; ++i) {
2926 if (Locs[i].first == -1) {
2927 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2929 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2930 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2933 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2934 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2935 &MaskOps[0], MaskOps.size()));
2942 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2943 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2946 MVT::ValueType VT = Op.getValueType();
2947 // TODO: handle v16i8.
2948 if (MVT::getSizeInBits(VT) == 16) {
2949 // Transform it so it match pextrw which produces a 32-bit result.
2950 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2951 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2952 Op.getOperand(0), Op.getOperand(1));
2953 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2954 DAG.getValueType(VT));
2955 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2956 } else if (MVT::getSizeInBits(VT) == 32) {
2957 SDOperand Vec = Op.getOperand(0);
2958 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2961 // SHUFPS the element to the lowest double word, then movss.
2962 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2963 SmallVector<SDOperand, 8> IdxVec;
2964 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2965 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2966 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2967 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2968 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2969 &IdxVec[0], IdxVec.size());
2970 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2971 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2972 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2973 DAG.getConstant(0, getPointerTy()));
2974 } else if (MVT::getSizeInBits(VT) == 64) {
2975 SDOperand Vec = Op.getOperand(0);
2976 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2980 // UNPCKHPD the element to the lowest double word, then movsd.
2981 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2982 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2983 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2984 SmallVector<SDOperand, 8> IdxVec;
2985 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2986 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2987 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2988 &IdxVec[0], IdxVec.size());
2989 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2990 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2991 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2992 DAG.getConstant(0, getPointerTy()));
2999 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3000 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3001 // as its second argument.
3002 MVT::ValueType VT = Op.getValueType();
3003 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
3004 SDOperand N0 = Op.getOperand(0);
3005 SDOperand N1 = Op.getOperand(1);
3006 SDOperand N2 = Op.getOperand(2);
3007 if (MVT::getSizeInBits(BaseVT) == 16) {
3008 if (N1.getValueType() != MVT::i32)
3009 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3010 if (N2.getValueType() != MVT::i32)
3011 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
3012 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3013 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3014 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3017 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3018 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3019 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3020 SmallVector<SDOperand, 8> MaskVec;
3021 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3022 for (unsigned i = 1; i <= 3; ++i)
3023 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3024 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3025 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3026 &MaskVec[0], MaskVec.size()));
3028 // Use two pinsrw instructions to insert a 32 bit value.
3030 if (MVT::isFloatingPoint(N1.getValueType())) {
3031 if (ISD::isNON_EXTLoad(N1.Val)) {
3032 // Just load directly from f32mem to GR32.
3033 LoadSDNode *LD = cast<LoadSDNode>(N1);
3034 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3035 LD->getSrcValue(), LD->getSrcValueOffset());
3037 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3038 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3039 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3040 DAG.getConstant(0, getPointerTy()));
3043 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3044 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3045 DAG.getConstant(Idx, getPointerTy()));
3046 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3047 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3048 DAG.getConstant(Idx+1, getPointerTy()));
3049 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3057 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3058 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3059 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3062 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3063 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3064 // one of the above mentioned nodes. It has to be wrapped because otherwise
3065 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3066 // be used to form addressing mode. These wrapped nodes will be selected
3069 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3070 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3071 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3073 CP->getAlignment());
3074 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3075 // With PIC, the address is actually $g + Offset.
3076 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3077 !Subtarget->isPICStyleRIPRel()) {
3078 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3079 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3087 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3088 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3089 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3090 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3091 // With PIC, the address is actually $g + Offset.
3092 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3093 !Subtarget->isPICStyleRIPRel()) {
3094 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3095 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3099 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3100 // load the value at address GV, not the value of GV itself. This means that
3101 // the GlobalAddress must be in the base or index register of the address, not
3102 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3103 // The same applies for external symbols during PIC codegen
3104 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3105 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3111 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3112 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3113 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3114 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3115 // With PIC, the address is actually $g + Offset.
3116 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3117 !Subtarget->isPICStyleRIPRel()) {
3118 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3119 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3126 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3127 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3128 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3129 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3130 // With PIC, the address is actually $g + Offset.
3131 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3132 !Subtarget->isPICStyleRIPRel()) {
3133 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3134 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3141 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3142 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3143 "Not an i64 shift!");
3144 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3145 SDOperand ShOpLo = Op.getOperand(0);
3146 SDOperand ShOpHi = Op.getOperand(1);
3147 SDOperand ShAmt = Op.getOperand(2);
3148 SDOperand Tmp1 = isSRA ?
3149 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3150 DAG.getConstant(0, MVT::i32);
3152 SDOperand Tmp2, Tmp3;
3153 if (Op.getOpcode() == ISD::SHL_PARTS) {
3154 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3155 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3157 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3158 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3161 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3162 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3163 DAG.getConstant(32, MVT::i8));
3164 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3165 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3168 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3170 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3171 SmallVector<SDOperand, 4> Ops;
3172 if (Op.getOpcode() == ISD::SHL_PARTS) {
3173 Ops.push_back(Tmp2);
3174 Ops.push_back(Tmp3);
3176 Ops.push_back(InFlag);
3177 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3178 InFlag = Hi.getValue(1);
3181 Ops.push_back(Tmp3);
3182 Ops.push_back(Tmp1);
3184 Ops.push_back(InFlag);
3185 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3187 Ops.push_back(Tmp2);
3188 Ops.push_back(Tmp3);
3190 Ops.push_back(InFlag);
3191 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3192 InFlag = Lo.getValue(1);
3195 Ops.push_back(Tmp3);
3196 Ops.push_back(Tmp1);
3198 Ops.push_back(InFlag);
3199 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3202 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3206 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3209 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3210 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3211 Op.getOperand(0).getValueType() >= MVT::i16 &&
3212 "Unknown SINT_TO_FP to lower!");
3215 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3216 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3217 MachineFunction &MF = DAG.getMachineFunction();
3218 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3219 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3220 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3221 StackSlot, NULL, 0);
3226 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3228 Tys = DAG.getVTList(MVT::f64, MVT::Other);
3229 SmallVector<SDOperand, 8> Ops;
3230 Ops.push_back(Chain);
3231 Ops.push_back(StackSlot);
3232 Ops.push_back(DAG.getValueType(SrcVT));
3233 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3234 Tys, &Ops[0], Ops.size());
3237 Chain = Result.getValue(1);
3238 SDOperand InFlag = Result.getValue(2);
3240 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3241 // shouldn't be necessary except that RFP cannot be live across
3242 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3243 MachineFunction &MF = DAG.getMachineFunction();
3244 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3245 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3246 Tys = DAG.getVTList(MVT::Other);
3247 SmallVector<SDOperand, 8> Ops;
3248 Ops.push_back(Chain);
3249 Ops.push_back(Result);
3250 Ops.push_back(StackSlot);
3251 Ops.push_back(DAG.getValueType(Op.getValueType()));
3252 Ops.push_back(InFlag);
3253 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3254 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3260 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3261 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3262 "Unknown FP_TO_SINT to lower!");
3263 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3265 MachineFunction &MF = DAG.getMachineFunction();
3266 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3267 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3268 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3271 switch (Op.getValueType()) {
3272 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3273 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3274 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3275 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3278 SDOperand Chain = DAG.getEntryNode();
3279 SDOperand Value = Op.getOperand(0);
3281 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3282 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3283 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
3285 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3287 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3288 Chain = Value.getValue(1);
3289 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3290 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3293 // Build the FP_TO_INT*_IN_MEM
3294 SDOperand Ops[] = { Chain, Value, StackSlot };
3295 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3298 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3301 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3302 MVT::ValueType VT = Op.getValueType();
3303 const Type *OpNTy = MVT::getTypeForValueType(VT);
3304 std::vector<Constant*> CV;
3305 if (VT == MVT::f64) {
3306 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3307 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3309 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3310 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3311 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3312 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3314 Constant *CS = ConstantStruct::get(CV);
3315 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3316 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
3317 SmallVector<SDOperand, 3> Ops;
3318 Ops.push_back(DAG.getEntryNode());
3319 Ops.push_back(CPIdx);
3320 Ops.push_back(DAG.getSrcValue(NULL));
3321 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3322 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3325 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3326 MVT::ValueType VT = Op.getValueType();
3327 const Type *OpNTy = MVT::getTypeForValueType(VT);
3328 std::vector<Constant*> CV;
3329 if (VT == MVT::f64) {
3330 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3331 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3333 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3334 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3335 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3336 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3338 Constant *CS = ConstantStruct::get(CV);
3339 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3340 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
3341 SmallVector<SDOperand, 3> Ops;
3342 Ops.push_back(DAG.getEntryNode());
3343 Ops.push_back(CPIdx);
3344 Ops.push_back(DAG.getSrcValue(NULL));
3345 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3346 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3349 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3350 SDOperand Op0 = Op.getOperand(0);
3351 SDOperand Op1 = Op.getOperand(1);
3352 MVT::ValueType VT = Op.getValueType();
3353 MVT::ValueType SrcVT = Op1.getValueType();
3354 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3356 // If second operand is smaller, extend it first.
3357 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3358 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3362 // First get the sign bit of second operand.
3363 std::vector<Constant*> CV;
3364 if (SrcVT == MVT::f64) {
3365 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3366 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3368 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3369 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3370 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3371 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3373 Constant *CS = ConstantStruct::get(CV);
3374 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3375 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other);
3376 SmallVector<SDOperand, 3> Ops;
3377 Ops.push_back(DAG.getEntryNode());
3378 Ops.push_back(CPIdx);
3379 Ops.push_back(DAG.getSrcValue(NULL));
3380 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3381 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3383 // Shift sign bit right or left if the two operands have different types.
3384 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3385 // Op0 is MVT::f32, Op1 is MVT::f64.
3386 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3387 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3388 DAG.getConstant(32, MVT::i32));
3389 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3390 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3391 DAG.getConstant(0, getPointerTy()));
3394 // Clear first operand sign bit.
3396 if (VT == MVT::f64) {
3397 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3398 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3400 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3401 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3402 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3403 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3405 CS = ConstantStruct::get(CV);
3406 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3407 Tys = DAG.getVTList(VT, MVT::Other);
3409 Ops.push_back(DAG.getEntryNode());
3410 Ops.push_back(CPIdx);
3411 Ops.push_back(DAG.getSrcValue(NULL));
3412 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3413 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3415 // Or the value with the sign bit.
3416 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3419 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3421 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3423 SDOperand Op0 = Op.getOperand(0);
3424 SDOperand Op1 = Op.getOperand(1);
3425 SDOperand CC = Op.getOperand(2);
3426 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3427 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3428 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3429 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3432 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3434 SDOperand Ops1[] = { Chain, Op0, Op1 };
3435 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3436 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3437 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3440 assert(isFP && "Illegal integer SetCC!");
3442 SDOperand COps[] = { Chain, Op0, Op1 };
3443 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3445 switch (SetCCOpcode) {
3446 default: assert(false && "Illegal floating point SetCC!");
3447 case ISD::SETOEQ: { // !PF & ZF
3448 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3449 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3450 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3452 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3453 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3455 case ISD::SETUNE: { // PF | !ZF
3456 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3457 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3458 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3460 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3461 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3466 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3467 bool addTest = true;
3468 SDOperand Chain = DAG.getEntryNode();
3469 SDOperand Cond = Op.getOperand(0);
3471 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3473 if (Cond.getOpcode() == ISD::SETCC)
3474 Cond = LowerSETCC(Cond, DAG, Chain);
3476 if (Cond.getOpcode() == X86ISD::SETCC) {
3477 CC = Cond.getOperand(0);
3479 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3480 // (since flag operand cannot be shared). Use it as the condition setting
3481 // operand in place of the X86ISD::SETCC.
3482 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3483 // to use a test instead of duplicating the X86ISD::CMP (for register
3484 // pressure reason)?
3485 SDOperand Cmp = Cond.getOperand(1);
3486 unsigned Opc = Cmp.getOpcode();
3487 bool IllegalFPCMov = !X86ScalarSSE &&
3488 MVT::isFloatingPoint(Op.getValueType()) &&
3489 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3490 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3492 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3493 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3499 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3500 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3501 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3504 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3505 SmallVector<SDOperand, 4> Ops;
3506 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3507 // condition is true.
3508 Ops.push_back(Op.getOperand(2));
3509 Ops.push_back(Op.getOperand(1));
3511 Ops.push_back(Cond.getValue(1));
3512 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3515 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3516 bool addTest = true;
3517 SDOperand Chain = Op.getOperand(0);
3518 SDOperand Cond = Op.getOperand(1);
3519 SDOperand Dest = Op.getOperand(2);
3521 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3523 if (Cond.getOpcode() == ISD::SETCC)
3524 Cond = LowerSETCC(Cond, DAG, Chain);
3526 if (Cond.getOpcode() == X86ISD::SETCC) {
3527 CC = Cond.getOperand(0);
3529 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3530 // (since flag operand cannot be shared). Use it as the condition setting
3531 // operand in place of the X86ISD::SETCC.
3532 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3533 // to use a test instead of duplicating the X86ISD::CMP (for register
3534 // pressure reason)?
3535 SDOperand Cmp = Cond.getOperand(1);
3536 unsigned Opc = Cmp.getOpcode();
3537 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3538 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3539 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3545 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3546 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3547 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3549 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3550 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3553 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3554 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3556 if (Subtarget->is64Bit())
3557 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3559 switch (CallingConv) {
3561 assert(0 && "Unsupported calling convention");
3562 case CallingConv::Fast:
3563 // TODO: Implement fastcc
3565 case CallingConv::C:
3566 case CallingConv::X86_StdCall:
3567 return LowerCCCCallTo(Op, DAG, CallingConv);
3568 case CallingConv::X86_FastCall:
3569 return LowerFastCCCallTo(Op, DAG, CallingConv);
3574 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3575 MachineFunction &MF = DAG.getMachineFunction();
3576 const Function* Fn = MF.getFunction();
3577 if (Fn->hasExternalLinkage() &&
3578 Subtarget->isTargetCygMing() &&
3579 Fn->getName() == "main")
3580 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3582 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3583 if (Subtarget->is64Bit())
3584 return LowerX86_64CCCArguments(Op, DAG);
3588 assert(0 && "Unsupported calling convention");
3589 case CallingConv::Fast:
3590 // TODO: implement fastcc.
3593 case CallingConv::C:
3594 return LowerCCCArguments(Op, DAG);
3595 case CallingConv::X86_StdCall:
3596 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
3597 return LowerCCCArguments(Op, DAG, true);
3598 case CallingConv::X86_FastCall:
3599 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
3600 return LowerFastCCArguments(Op, DAG);
3604 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3605 SDOperand InFlag(0, 0);
3606 SDOperand Chain = Op.getOperand(0);
3608 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3609 if (Align == 0) Align = 1;
3611 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3612 // If not DWORD aligned, call memset if size is less than the threshold.
3613 // It knows how to align to the right boundary first.
3614 if ((Align & 3) != 0 ||
3615 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3616 MVT::ValueType IntPtr = getPointerTy();
3617 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3618 TargetLowering::ArgListTy Args;
3619 TargetLowering::ArgListEntry Entry;
3620 Entry.Node = Op.getOperand(1);
3621 Entry.Ty = IntPtrTy;
3622 Entry.isSigned = false;
3623 Entry.isInReg = false;
3624 Entry.isSRet = false;
3625 Args.push_back(Entry);
3626 // Extend the unsigned i8 argument to be an int value for the call.
3627 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3628 Entry.Ty = IntPtrTy;
3629 Entry.isSigned = false;
3630 Entry.isInReg = false;
3631 Entry.isSRet = false;
3632 Args.push_back(Entry);
3633 Entry.Node = Op.getOperand(3);
3634 Args.push_back(Entry);
3635 std::pair<SDOperand,SDOperand> CallResult =
3636 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3637 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3638 return CallResult.second;
3643 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3644 unsigned BytesLeft = 0;
3645 bool TwoRepStos = false;
3648 uint64_t Val = ValC->getValue() & 255;
3650 // If the value is a constant, then we can potentially use larger sets.
3651 switch (Align & 3) {
3652 case 2: // WORD aligned
3655 Val = (Val << 8) | Val;
3657 case 0: // DWORD aligned
3660 Val = (Val << 8) | Val;
3661 Val = (Val << 16) | Val;
3662 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3665 Val = (Val << 32) | Val;
3668 default: // Byte aligned
3671 Count = Op.getOperand(3);
3675 if (AVT > MVT::i8) {
3677 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3678 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3679 BytesLeft = I->getValue() % UBytes;
3681 assert(AVT >= MVT::i32 &&
3682 "Do not use rep;stos if not at least DWORD aligned");
3683 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3684 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3689 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3691 InFlag = Chain.getValue(1);
3694 Count = Op.getOperand(3);
3695 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3696 InFlag = Chain.getValue(1);
3699 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3701 InFlag = Chain.getValue(1);
3702 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3703 Op.getOperand(1), InFlag);
3704 InFlag = Chain.getValue(1);
3706 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3707 SmallVector<SDOperand, 8> Ops;
3708 Ops.push_back(Chain);
3709 Ops.push_back(DAG.getValueType(AVT));
3710 Ops.push_back(InFlag);
3711 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3714 InFlag = Chain.getValue(1);
3715 Count = Op.getOperand(3);
3716 MVT::ValueType CVT = Count.getValueType();
3717 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3718 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3719 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3721 InFlag = Chain.getValue(1);
3722 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3724 Ops.push_back(Chain);
3725 Ops.push_back(DAG.getValueType(MVT::i8));
3726 Ops.push_back(InFlag);
3727 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3728 } else if (BytesLeft) {
3729 // Issue stores for the last 1 - 7 bytes.
3731 unsigned Val = ValC->getValue() & 255;
3732 unsigned Offset = I->getValue() - BytesLeft;
3733 SDOperand DstAddr = Op.getOperand(1);
3734 MVT::ValueType AddrVT = DstAddr.getValueType();
3735 if (BytesLeft >= 4) {
3736 Val = (Val << 8) | Val;
3737 Val = (Val << 16) | Val;
3738 Value = DAG.getConstant(Val, MVT::i32);
3739 Chain = DAG.getStore(Chain, Value,
3740 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3741 DAG.getConstant(Offset, AddrVT)),
3746 if (BytesLeft >= 2) {
3747 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3748 Chain = DAG.getStore(Chain, Value,
3749 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3750 DAG.getConstant(Offset, AddrVT)),
3755 if (BytesLeft == 1) {
3756 Value = DAG.getConstant(Val, MVT::i8);
3757 Chain = DAG.getStore(Chain, Value,
3758 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3759 DAG.getConstant(Offset, AddrVT)),
3767 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3768 SDOperand Chain = Op.getOperand(0);
3770 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3771 if (Align == 0) Align = 1;
3773 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3774 // If not DWORD aligned, call memcpy if size is less than the threshold.
3775 // It knows how to align to the right boundary first.
3776 if ((Align & 3) != 0 ||
3777 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3778 MVT::ValueType IntPtr = getPointerTy();
3779 TargetLowering::ArgListTy Args;
3780 TargetLowering::ArgListEntry Entry;
3781 Entry.Ty = getTargetData()->getIntPtrType();
3782 Entry.isSigned = false;
3783 Entry.isInReg = false;
3784 Entry.isSRet = false;
3785 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3786 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3787 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
3788 std::pair<SDOperand,SDOperand> CallResult =
3789 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3790 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3791 return CallResult.second;
3796 unsigned BytesLeft = 0;
3797 bool TwoRepMovs = false;
3798 switch (Align & 3) {
3799 case 2: // WORD aligned
3802 case 0: // DWORD aligned
3804 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3807 default: // Byte aligned
3809 Count = Op.getOperand(3);
3813 if (AVT > MVT::i8) {
3815 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3816 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3817 BytesLeft = I->getValue() % UBytes;
3819 assert(AVT >= MVT::i32 &&
3820 "Do not use rep;movs if not at least DWORD aligned");
3821 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3822 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3827 SDOperand InFlag(0, 0);
3828 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3830 InFlag = Chain.getValue(1);
3831 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3832 Op.getOperand(1), InFlag);
3833 InFlag = Chain.getValue(1);
3834 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3835 Op.getOperand(2), InFlag);
3836 InFlag = Chain.getValue(1);
3838 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3839 SmallVector<SDOperand, 8> Ops;
3840 Ops.push_back(Chain);
3841 Ops.push_back(DAG.getValueType(AVT));
3842 Ops.push_back(InFlag);
3843 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3846 InFlag = Chain.getValue(1);
3847 Count = Op.getOperand(3);
3848 MVT::ValueType CVT = Count.getValueType();
3849 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3850 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3851 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3853 InFlag = Chain.getValue(1);
3854 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3856 Ops.push_back(Chain);
3857 Ops.push_back(DAG.getValueType(MVT::i8));
3858 Ops.push_back(InFlag);
3859 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3860 } else if (BytesLeft) {
3861 // Issue loads and stores for the last 1 - 7 bytes.
3862 unsigned Offset = I->getValue() - BytesLeft;
3863 SDOperand DstAddr = Op.getOperand(1);
3864 MVT::ValueType DstVT = DstAddr.getValueType();
3865 SDOperand SrcAddr = Op.getOperand(2);
3866 MVT::ValueType SrcVT = SrcAddr.getValueType();
3868 if (BytesLeft >= 4) {
3869 Value = DAG.getLoad(MVT::i32, Chain,
3870 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3871 DAG.getConstant(Offset, SrcVT)),
3873 Chain = Value.getValue(1);
3874 Chain = DAG.getStore(Chain, Value,
3875 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3876 DAG.getConstant(Offset, DstVT)),
3881 if (BytesLeft >= 2) {
3882 Value = DAG.getLoad(MVT::i16, Chain,
3883 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3884 DAG.getConstant(Offset, SrcVT)),
3886 Chain = Value.getValue(1);
3887 Chain = DAG.getStore(Chain, Value,
3888 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3889 DAG.getConstant(Offset, DstVT)),
3895 if (BytesLeft == 1) {
3896 Value = DAG.getLoad(MVT::i8, Chain,
3897 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3898 DAG.getConstant(Offset, SrcVT)),
3900 Chain = Value.getValue(1);
3901 Chain = DAG.getStore(Chain, Value,
3902 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3903 DAG.getConstant(Offset, DstVT)),
3912 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3913 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3914 SDOperand TheOp = Op.getOperand(0);
3915 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
3916 if (Subtarget->is64Bit()) {
3917 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
3918 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
3919 MVT::i64, Copy1.getValue(2));
3920 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
3921 DAG.getConstant(32, MVT::i8));
3923 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
3926 Tys = DAG.getVTList(MVT::i64, MVT::Other);
3927 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
3930 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
3931 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
3932 MVT::i32, Copy1.getValue(2));
3933 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
3934 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
3935 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
3938 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3939 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
3941 if (!Subtarget->is64Bit()) {
3942 // vastart just stores the address of the VarArgsFrameIndex slot into the
3943 // memory location argument.
3944 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
3945 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
3950 // gp_offset (0 - 6 * 8)
3951 // fp_offset (48 - 48 + 8 * 16)
3952 // overflow_arg_area (point to parameters coming in memory).
3954 SmallVector<SDOperand, 8> MemOps;
3955 SDOperand FIN = Op.getOperand(1);
3957 SDOperand Store = DAG.getStore(Op.getOperand(0),
3958 DAG.getConstant(VarArgsGPOffset, MVT::i32),
3959 FIN, SV->getValue(), SV->getOffset());
3960 MemOps.push_back(Store);
3963 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3964 DAG.getConstant(4, getPointerTy()));
3965 Store = DAG.getStore(Op.getOperand(0),
3966 DAG.getConstant(VarArgsFPOffset, MVT::i32),
3967 FIN, SV->getValue(), SV->getOffset());
3968 MemOps.push_back(Store);
3970 // Store ptr to overflow_arg_area
3971 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3972 DAG.getConstant(4, getPointerTy()));
3973 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
3974 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
3976 MemOps.push_back(Store);
3978 // Store ptr to reg_save_area.
3979 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3980 DAG.getConstant(8, getPointerTy()));
3981 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
3982 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
3984 MemOps.push_back(Store);
3985 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
3989 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3990 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3992 default: return SDOperand(); // Don't custom lower most intrinsics.
3993 // Comparison intrinsics.
3994 case Intrinsic::x86_sse_comieq_ss:
3995 case Intrinsic::x86_sse_comilt_ss:
3996 case Intrinsic::x86_sse_comile_ss:
3997 case Intrinsic::x86_sse_comigt_ss:
3998 case Intrinsic::x86_sse_comige_ss:
3999 case Intrinsic::x86_sse_comineq_ss:
4000 case Intrinsic::x86_sse_ucomieq_ss:
4001 case Intrinsic::x86_sse_ucomilt_ss:
4002 case Intrinsic::x86_sse_ucomile_ss:
4003 case Intrinsic::x86_sse_ucomigt_ss:
4004 case Intrinsic::x86_sse_ucomige_ss:
4005 case Intrinsic::x86_sse_ucomineq_ss:
4006 case Intrinsic::x86_sse2_comieq_sd:
4007 case Intrinsic::x86_sse2_comilt_sd:
4008 case Intrinsic::x86_sse2_comile_sd:
4009 case Intrinsic::x86_sse2_comigt_sd:
4010 case Intrinsic::x86_sse2_comige_sd:
4011 case Intrinsic::x86_sse2_comineq_sd:
4012 case Intrinsic::x86_sse2_ucomieq_sd:
4013 case Intrinsic::x86_sse2_ucomilt_sd:
4014 case Intrinsic::x86_sse2_ucomile_sd:
4015 case Intrinsic::x86_sse2_ucomigt_sd:
4016 case Intrinsic::x86_sse2_ucomige_sd:
4017 case Intrinsic::x86_sse2_ucomineq_sd: {
4019 ISD::CondCode CC = ISD::SETCC_INVALID;
4022 case Intrinsic::x86_sse_comieq_ss:
4023 case Intrinsic::x86_sse2_comieq_sd:
4027 case Intrinsic::x86_sse_comilt_ss:
4028 case Intrinsic::x86_sse2_comilt_sd:
4032 case Intrinsic::x86_sse_comile_ss:
4033 case Intrinsic::x86_sse2_comile_sd:
4037 case Intrinsic::x86_sse_comigt_ss:
4038 case Intrinsic::x86_sse2_comigt_sd:
4042 case Intrinsic::x86_sse_comige_ss:
4043 case Intrinsic::x86_sse2_comige_sd:
4047 case Intrinsic::x86_sse_comineq_ss:
4048 case Intrinsic::x86_sse2_comineq_sd:
4052 case Intrinsic::x86_sse_ucomieq_ss:
4053 case Intrinsic::x86_sse2_ucomieq_sd:
4054 Opc = X86ISD::UCOMI;
4057 case Intrinsic::x86_sse_ucomilt_ss:
4058 case Intrinsic::x86_sse2_ucomilt_sd:
4059 Opc = X86ISD::UCOMI;
4062 case Intrinsic::x86_sse_ucomile_ss:
4063 case Intrinsic::x86_sse2_ucomile_sd:
4064 Opc = X86ISD::UCOMI;
4067 case Intrinsic::x86_sse_ucomigt_ss:
4068 case Intrinsic::x86_sse2_ucomigt_sd:
4069 Opc = X86ISD::UCOMI;
4072 case Intrinsic::x86_sse_ucomige_ss:
4073 case Intrinsic::x86_sse2_ucomige_sd:
4074 Opc = X86ISD::UCOMI;
4077 case Intrinsic::x86_sse_ucomineq_ss:
4078 case Intrinsic::x86_sse2_ucomineq_sd:
4079 Opc = X86ISD::UCOMI;
4085 SDOperand LHS = Op.getOperand(1);
4086 SDOperand RHS = Op.getOperand(2);
4087 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4089 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4090 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4091 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4092 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4093 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4094 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4095 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4100 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4101 // Depths > 0 not supported yet!
4102 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4105 // Just load the return address
4106 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4107 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4110 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4111 // Depths > 0 not supported yet!
4112 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4115 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4116 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4117 DAG.getConstant(4, getPointerTy()));
4120 /// LowerOperation - Provide custom lowering hooks for some operations.
4122 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4123 switch (Op.getOpcode()) {
4124 default: assert(0 && "Should not custom lower this!");
4125 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4126 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4127 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4128 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4129 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4130 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4131 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4132 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4133 case ISD::SHL_PARTS:
4134 case ISD::SRA_PARTS:
4135 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4136 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4137 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4138 case ISD::FABS: return LowerFABS(Op, DAG);
4139 case ISD::FNEG: return LowerFNEG(Op, DAG);
4140 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
4141 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
4142 case ISD::SELECT: return LowerSELECT(Op, DAG);
4143 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4144 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4145 case ISD::CALL: return LowerCALL(Op, DAG);
4146 case ISD::RET: return LowerRET(Op, DAG);
4147 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4148 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4149 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4150 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4151 case ISD::VASTART: return LowerVASTART(Op, DAG);
4152 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4153 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4154 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4159 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4161 default: return NULL;
4162 case X86ISD::SHLD: return "X86ISD::SHLD";
4163 case X86ISD::SHRD: return "X86ISD::SHRD";
4164 case X86ISD::FAND: return "X86ISD::FAND";
4165 case X86ISD::FOR: return "X86ISD::FOR";
4166 case X86ISD::FXOR: return "X86ISD::FXOR";
4167 case X86ISD::FSRL: return "X86ISD::FSRL";
4168 case X86ISD::FILD: return "X86ISD::FILD";
4169 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4170 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4171 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4172 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4173 case X86ISD::FLD: return "X86ISD::FLD";
4174 case X86ISD::FST: return "X86ISD::FST";
4175 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4176 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4177 case X86ISD::CALL: return "X86ISD::CALL";
4178 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4179 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4180 case X86ISD::CMP: return "X86ISD::CMP";
4181 case X86ISD::COMI: return "X86ISD::COMI";
4182 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4183 case X86ISD::SETCC: return "X86ISD::SETCC";
4184 case X86ISD::CMOV: return "X86ISD::CMOV";
4185 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4186 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4187 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4188 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4189 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
4190 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
4191 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4192 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4193 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4194 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4195 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4196 case X86ISD::FMAX: return "X86ISD::FMAX";
4197 case X86ISD::FMIN: return "X86ISD::FMIN";
4201 /// isLegalAddressImmediate - Return true if the integer value or
4202 /// GlobalValue can be used as the offset of the target addressing mode.
4203 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
4204 // X86 allows a sign-extended 32-bit immediate field.
4205 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4208 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
4209 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement
4210 // field unless we are in small code model.
4211 if (Subtarget->is64Bit() &&
4212 getTargetMachine().getCodeModel() != CodeModel::Small)
4215 return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false));
4218 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4219 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4220 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4221 /// are assumed to be legal.
4223 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4224 // Only do shuffles on 128-bit vector types for now.
4225 if (MVT::getSizeInBits(VT) == 64) return false;
4226 return (Mask.Val->getNumOperands() <= 4 ||
4227 isSplatMask(Mask.Val) ||
4228 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4229 X86::isUNPCKLMask(Mask.Val) ||
4230 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4231 X86::isUNPCKHMask(Mask.Val));
4234 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4236 SelectionDAG &DAG) const {
4237 unsigned NumElts = BVOps.size();
4238 // Only do shuffles on 128-bit vector types for now.
4239 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4240 if (NumElts == 2) return true;
4242 return (isMOVLMask(&BVOps[0], 4) ||
4243 isCommutedMOVL(&BVOps[0], 4, true) ||
4244 isSHUFPMask(&BVOps[0], 4) ||
4245 isCommutedSHUFP(&BVOps[0], 4));
4250 //===----------------------------------------------------------------------===//
4251 // X86 Scheduler Hooks
4252 //===----------------------------------------------------------------------===//
4255 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4256 MachineBasicBlock *BB) {
4257 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4258 switch (MI->getOpcode()) {
4259 default: assert(false && "Unexpected instr type to insert");
4260 case X86::CMOV_FR32:
4261 case X86::CMOV_FR64:
4262 case X86::CMOV_V4F32:
4263 case X86::CMOV_V2F64:
4264 case X86::CMOV_V2I64: {
4265 // To "insert" a SELECT_CC instruction, we actually have to insert the
4266 // diamond control-flow pattern. The incoming instruction knows the
4267 // destination vreg to set, the condition code register to branch on, the
4268 // true/false values to select between, and a branch opcode to use.
4269 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4270 ilist<MachineBasicBlock>::iterator It = BB;
4276 // cmpTY ccX, r1, r2
4278 // fallthrough --> copy0MBB
4279 MachineBasicBlock *thisMBB = BB;
4280 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4281 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4283 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4284 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4285 MachineFunction *F = BB->getParent();
4286 F->getBasicBlockList().insert(It, copy0MBB);
4287 F->getBasicBlockList().insert(It, sinkMBB);
4288 // Update machine-CFG edges by first adding all successors of the current
4289 // block to the new block which will contain the Phi node for the select.
4290 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4291 e = BB->succ_end(); i != e; ++i)
4292 sinkMBB->addSuccessor(*i);
4293 // Next, remove all successors of the current block, and add the true
4294 // and fallthrough blocks as its successors.
4295 while(!BB->succ_empty())
4296 BB->removeSuccessor(BB->succ_begin());
4297 BB->addSuccessor(copy0MBB);
4298 BB->addSuccessor(sinkMBB);
4301 // %FalseValue = ...
4302 // # fallthrough to sinkMBB
4305 // Update machine-CFG edges
4306 BB->addSuccessor(sinkMBB);
4309 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4312 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4313 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4314 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4316 delete MI; // The pseudo instruction is gone now.
4320 case X86::FP_TO_INT16_IN_MEM:
4321 case X86::FP_TO_INT32_IN_MEM:
4322 case X86::FP_TO_INT64_IN_MEM: {
4323 // Change the floating point control register to use "round towards zero"
4324 // mode when truncating to an integer value.
4325 MachineFunction *F = BB->getParent();
4326 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4327 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4329 // Load the old value of the high byte of the control word...
4331 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4332 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4334 // Set the high part to be round to zero...
4335 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4338 // Reload the modified control word now...
4339 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4341 // Restore the memory image of control word to original value
4342 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4345 // Get the X86 opcode to use.
4347 switch (MI->getOpcode()) {
4348 default: assert(0 && "illegal opcode!");
4349 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4350 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4351 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4355 MachineOperand &Op = MI->getOperand(0);
4356 if (Op.isRegister()) {
4357 AM.BaseType = X86AddressMode::RegBase;
4358 AM.Base.Reg = Op.getReg();
4360 AM.BaseType = X86AddressMode::FrameIndexBase;
4361 AM.Base.FrameIndex = Op.getFrameIndex();
4363 Op = MI->getOperand(1);
4364 if (Op.isImmediate())
4365 AM.Scale = Op.getImm();
4366 Op = MI->getOperand(2);
4367 if (Op.isImmediate())
4368 AM.IndexReg = Op.getImm();
4369 Op = MI->getOperand(3);
4370 if (Op.isGlobalAddress()) {
4371 AM.GV = Op.getGlobal();
4373 AM.Disp = Op.getImm();
4375 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4376 .addReg(MI->getOperand(4).getReg());
4378 // Reload the original control word now.
4379 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4381 delete MI; // The pseudo instruction is gone now.
4387 //===----------------------------------------------------------------------===//
4388 // X86 Optimization Hooks
4389 //===----------------------------------------------------------------------===//
4391 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4393 uint64_t &KnownZero,
4395 unsigned Depth) const {
4396 unsigned Opc = Op.getOpcode();
4397 assert((Opc >= ISD::BUILTIN_OP_END ||
4398 Opc == ISD::INTRINSIC_WO_CHAIN ||
4399 Opc == ISD::INTRINSIC_W_CHAIN ||
4400 Opc == ISD::INTRINSIC_VOID) &&
4401 "Should use MaskedValueIsZero if you don't know whether Op"
4402 " is a target node!");
4404 KnownZero = KnownOne = 0; // Don't know anything.
4408 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4413 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
4414 /// element of the result of the vector shuffle.
4415 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4416 MVT::ValueType VT = N->getValueType(0);
4417 SDOperand PermMask = N->getOperand(2);
4418 unsigned NumElems = PermMask.getNumOperands();
4419 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4421 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4423 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4424 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4425 SDOperand Idx = PermMask.getOperand(i);
4426 if (Idx.getOpcode() == ISD::UNDEF)
4427 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4428 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4433 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4434 /// node is a GlobalAddress + an offset.
4435 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4436 unsigned Opc = N->getOpcode();
4437 if (Opc == X86ISD::Wrapper) {
4438 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4439 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4442 } else if (Opc == ISD::ADD) {
4443 SDOperand N1 = N->getOperand(0);
4444 SDOperand N2 = N->getOperand(1);
4445 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4446 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4448 Offset += V->getSignExtended();
4451 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4452 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4454 Offset += V->getSignExtended();
4462 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4464 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4465 MachineFrameInfo *MFI) {
4466 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4469 SDOperand Loc = N->getOperand(1);
4470 SDOperand BaseLoc = Base->getOperand(1);
4471 if (Loc.getOpcode() == ISD::FrameIndex) {
4472 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4474 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4475 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4476 int FS = MFI->getObjectSize(FI);
4477 int BFS = MFI->getObjectSize(BFI);
4478 if (FS != BFS || FS != Size) return false;
4479 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4481 GlobalValue *GV1 = NULL;
4482 GlobalValue *GV2 = NULL;
4483 int64_t Offset1 = 0;
4484 int64_t Offset2 = 0;
4485 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4486 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4487 if (isGA1 && isGA2 && GV1 == GV2)
4488 return Offset1 == (Offset2 + Dist*Size);
4494 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4495 const X86Subtarget *Subtarget) {
4498 if (isGAPlusOffset(Base, GV, Offset))
4499 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4501 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4502 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
4504 // Fixed objects do not specify alignment, however the offsets are known.
4505 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4506 (MFI->getObjectOffset(BFI) % 16) == 0);
4508 return MFI->getObjectAlignment(BFI) >= 16;
4514 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4515 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4516 /// if the load addresses are consecutive, non-overlapping, and in the right
4518 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4519 const X86Subtarget *Subtarget) {
4520 MachineFunction &MF = DAG.getMachineFunction();
4521 MachineFrameInfo *MFI = MF.getFrameInfo();
4522 MVT::ValueType VT = N->getValueType(0);
4523 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4524 SDOperand PermMask = N->getOperand(2);
4525 int NumElems = (int)PermMask.getNumOperands();
4526 SDNode *Base = NULL;
4527 for (int i = 0; i < NumElems; ++i) {
4528 SDOperand Idx = PermMask.getOperand(i);
4529 if (Idx.getOpcode() == ISD::UNDEF) {
4530 if (!Base) return SDOperand();
4533 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4534 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
4538 else if (!isConsecutiveLoad(Arg.Val, Base,
4539 i, MVT::getSizeInBits(EVT)/8,MFI))
4544 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
4546 LoadSDNode *LD = cast<LoadSDNode>(Base);
4547 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4548 LD->getSrcValueOffset());
4550 // Just use movups, it's shorter.
4551 SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other);
4552 SmallVector<SDOperand, 3> Ops;
4553 Ops.push_back(Base->getOperand(0));
4554 Ops.push_back(Base->getOperand(1));
4555 Ops.push_back(Base->getOperand(2));
4556 return DAG.getNode(ISD::BIT_CONVERT, VT,
4557 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
4561 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4562 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4563 const X86Subtarget *Subtarget) {
4564 SDOperand Cond = N->getOperand(0);
4566 // If we have SSE[12] support, try to form min/max nodes.
4567 if (Subtarget->hasSSE2() &&
4568 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4569 if (Cond.getOpcode() == ISD::SETCC) {
4570 // Get the LHS/RHS of the select.
4571 SDOperand LHS = N->getOperand(1);
4572 SDOperand RHS = N->getOperand(2);
4573 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
4575 unsigned Opcode = 0;
4576 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
4579 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4582 if (!UnsafeFPMath) break;
4584 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4586 Opcode = X86ISD::FMIN;
4589 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4592 if (!UnsafeFPMath) break;
4594 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4596 Opcode = X86ISD::FMAX;
4599 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
4602 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4605 if (!UnsafeFPMath) break;
4607 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4609 Opcode = X86ISD::FMIN;
4612 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4615 if (!UnsafeFPMath) break;
4617 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4619 Opcode = X86ISD::FMAX;
4625 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
4634 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4635 DAGCombinerInfo &DCI) const {
4636 SelectionDAG &DAG = DCI.DAG;
4637 switch (N->getOpcode()) {
4639 case ISD::VECTOR_SHUFFLE:
4640 return PerformShuffleCombine(N, DAG, Subtarget);
4642 return PerformSELECTCombine(N, DAG, Subtarget);
4648 //===----------------------------------------------------------------------===//
4649 // X86 Inline Assembly Support
4650 //===----------------------------------------------------------------------===//
4652 /// getConstraintType - Given a constraint letter, return the type of
4653 /// constraint it is for this target.
4654 X86TargetLowering::ConstraintType
4655 X86TargetLowering::getConstraintType(char ConstraintLetter) const {
4656 switch (ConstraintLetter) {
4665 return C_RegisterClass;
4666 default: return TargetLowering::getConstraintType(ConstraintLetter);
4670 /// isOperandValidForConstraint - Return the specified operand (possibly
4671 /// modified) if the specified SDOperand is valid for the specified target
4672 /// constraint letter, otherwise return null.
4673 SDOperand X86TargetLowering::
4674 isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4675 switch (Constraint) {
4678 // Literal immediates are always ok.
4679 if (isa<ConstantSDNode>(Op)) return Op;
4681 // If we are in non-pic codegen mode, we allow the address of a global to
4682 // be used with 'i'.
4683 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
4684 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4685 return SDOperand(0, 0);
4687 if (GA->getOpcode() != ISD::TargetGlobalAddress)
4688 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4693 // Otherwise, not valid for this mode.
4694 return SDOperand(0, 0);
4696 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
4700 std::vector<unsigned> X86TargetLowering::
4701 getRegClassForInlineAsmConstraint(const std::string &Constraint,
4702 MVT::ValueType VT) const {
4703 if (Constraint.size() == 1) {
4704 // FIXME: not handling fp-stack yet!
4705 // FIXME: not handling MMX registers yet ('y' constraint).
4706 switch (Constraint[0]) { // GCC X86 Constraint Letters
4707 default: break; // Unknown constraint letter
4708 case 'A': // EAX/EDX
4709 if (VT == MVT::i32 || VT == MVT::i64)
4710 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4712 case 'r': // GENERAL_REGS
4713 case 'R': // LEGACY_REGS
4714 if (VT == MVT::i64 && Subtarget->is64Bit())
4715 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
4716 X86::RSI, X86::RDI, X86::RBP, X86::RSP,
4717 X86::R8, X86::R9, X86::R10, X86::R11,
4718 X86::R12, X86::R13, X86::R14, X86::R15, 0);
4720 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4721 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4722 else if (VT == MVT::i16)
4723 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4724 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4725 else if (VT == MVT::i8)
4726 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
4728 case 'l': // INDEX_REGS
4730 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4731 X86::ESI, X86::EDI, X86::EBP, 0);
4732 else if (VT == MVT::i16)
4733 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4734 X86::SI, X86::DI, X86::BP, 0);
4735 else if (VT == MVT::i8)
4736 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4738 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4741 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4742 else if (VT == MVT::i16)
4743 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4744 else if (VT == MVT::i8)
4745 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4747 case 'x': // SSE_REGS if SSE1 allowed
4748 if (Subtarget->hasSSE1())
4749 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4750 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4752 return std::vector<unsigned>();
4753 case 'Y': // SSE_REGS if SSE2 allowed
4754 if (Subtarget->hasSSE2())
4755 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4756 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4758 return std::vector<unsigned>();
4762 return std::vector<unsigned>();
4765 std::pair<unsigned, const TargetRegisterClass*>
4766 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4767 MVT::ValueType VT) const {
4768 // Use the default implementation in TargetLowering to convert the register
4769 // constraint into a member of a register class.
4770 std::pair<unsigned, const TargetRegisterClass*> Res;
4771 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4773 // Not found as a standard register?
4774 if (Res.second == 0) {
4775 // GCC calls "st(0)" just plain "st".
4776 if (StringsEqualNoCase("{st}", Constraint)) {
4777 Res.first = X86::ST0;
4778 Res.second = X86::RSTRegisterClass;
4784 // Otherwise, check to see if this is a register class of the wrong value
4785 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4786 // turn into {ax},{dx}.
4787 if (Res.second->hasType(VT))
4788 return Res; // Correct type already, nothing to do.
4790 // All of the single-register GCC register classes map their values onto
4791 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4792 // really want an 8-bit or 32-bit register, map to the appropriate register
4793 // class and return the appropriate register.
4794 if (Res.second != X86::GR16RegisterClass)
4797 if (VT == MVT::i8) {
4798 unsigned DestReg = 0;
4799 switch (Res.first) {
4801 case X86::AX: DestReg = X86::AL; break;
4802 case X86::DX: DestReg = X86::DL; break;
4803 case X86::CX: DestReg = X86::CL; break;
4804 case X86::BX: DestReg = X86::BL; break;
4807 Res.first = DestReg;
4808 Res.second = Res.second = X86::GR8RegisterClass;
4810 } else if (VT == MVT::i32) {
4811 unsigned DestReg = 0;
4812 switch (Res.first) {
4814 case X86::AX: DestReg = X86::EAX; break;
4815 case X86::DX: DestReg = X86::EDX; break;
4816 case X86::CX: DestReg = X86::ECX; break;
4817 case X86::BX: DestReg = X86::EBX; break;
4818 case X86::SI: DestReg = X86::ESI; break;
4819 case X86::DI: DestReg = X86::EDI; break;
4820 case X86::BP: DestReg = X86::EBP; break;
4821 case X86::SP: DestReg = X86::ESP; break;
4824 Res.first = DestReg;
4825 Res.second = Res.second = X86::GR32RegisterClass;
4827 } else if (VT == MVT::i64) {
4828 unsigned DestReg = 0;
4829 switch (Res.first) {
4831 case X86::AX: DestReg = X86::RAX; break;
4832 case X86::DX: DestReg = X86::RDX; break;
4833 case X86::CX: DestReg = X86::RCX; break;
4834 case X86::BX: DestReg = X86::RBX; break;
4835 case X86::SI: DestReg = X86::RSI; break;
4836 case X86::DI: DestReg = X86::RDI; break;
4837 case X86::BP: DestReg = X86::RBP; break;
4838 case X86::SP: DestReg = X86::RSP; break;
4841 Res.first = DestReg;
4842 Res.second = Res.second = X86::GR64RegisterClass;