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/SelectionDAGISel.h"
33 #include "llvm/CodeGen/SSARegMap.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include "llvm/ADT/StringExtras.h"
39 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
40 : TargetLowering(TM) {
41 Subtarget = &TM.getSubtarget<X86Subtarget>();
42 X86ScalarSSE = Subtarget->hasSSE2();
43 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
45 // Set up the TargetLowering object.
47 // X86 is weird, it always uses i8 for shift amounts and setcc results.
48 setShiftAmountType(MVT::i8);
49 setSetCCResultType(MVT::i8);
50 setSetCCResultContents(ZeroOrOneSetCCResult);
51 setSchedulingPreference(SchedulingForRegPressure);
52 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
53 setStackPointerRegisterToSaveRestore(X86StackPtr);
55 if (Subtarget->isTargetDarwin()) {
56 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
57 setUseUnderscoreSetJmp(false);
58 setUseUnderscoreLongJmp(false);
59 } else if (Subtarget->isTargetMingw()) {
60 // MS runtime is weird: it exports _setjmp, but longjmp!
61 setUseUnderscoreSetJmp(true);
62 setUseUnderscoreLongJmp(false);
64 setUseUnderscoreSetJmp(true);
65 setUseUnderscoreLongJmp(true);
68 // Add legal addressing mode scale values.
69 addLegalAddressScale(8);
70 addLegalAddressScale(4);
71 addLegalAddressScale(2);
72 // Enter the ones which require both scale + index last. These are more
74 addLegalAddressScale(9);
75 addLegalAddressScale(5);
76 addLegalAddressScale(3);
78 // Set up the register classes.
79 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
80 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
81 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
82 if (Subtarget->is64Bit())
83 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
85 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
87 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
89 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
90 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
91 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
93 if (Subtarget->is64Bit()) {
94 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
95 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
98 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
99 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
101 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
104 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
106 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
107 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
108 // SSE has no i16 to fp conversion, only i32
110 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
112 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
113 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
116 if (!Subtarget->is64Bit()) {
117 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
118 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
119 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
122 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
124 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
125 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
128 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
130 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
131 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
134 // Handle FP_TO_UINT by promoting the destination to a larger signed
136 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
137 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
138 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
140 if (Subtarget->is64Bit()) {
141 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
142 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
144 if (X86ScalarSSE && !Subtarget->hasSSE3())
145 // Expand FP_TO_UINT into a select.
146 // FIXME: We would like to use a Custom expander here eventually to do
147 // the optimal thing for SSE vs. the default expansion in the legalizer.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
150 // With SSE3 we can use fisttpll to convert to a signed i64.
151 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
154 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
156 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
157 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
160 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
161 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
162 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
163 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
164 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
165 if (Subtarget->is64Bit())
166 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
167 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
168 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
169 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
170 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
171 setOperationAction(ISD::FREM , MVT::f64 , Expand);
173 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
174 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
175 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
176 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
177 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
178 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
179 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
180 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
181 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
182 if (Subtarget->is64Bit()) {
183 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
184 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
185 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
188 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
189 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
191 // These should be promoted to a larger select which is supported.
192 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
193 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
194 // X86 wants to expand cmov itself.
195 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
196 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
197 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
198 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
199 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
200 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
201 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
202 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
203 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
204 if (Subtarget->is64Bit()) {
205 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
206 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
208 // X86 ret instruction may pop stack.
209 setOperationAction(ISD::RET , MVT::Other, Custom);
211 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
212 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
213 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
214 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
215 if (Subtarget->is64Bit()) {
216 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
217 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
218 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
219 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
221 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
222 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
223 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
224 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
225 // X86 wants to expand memset / memcpy itself.
226 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
227 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
229 // We don't have line number support yet.
230 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
231 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
232 // FIXME - use subtarget debug flags
233 if (!Subtarget->isTargetDarwin() &&
234 !Subtarget->isTargetELF() &&
235 !Subtarget->isTargetCygMing())
236 setOperationAction(ISD::LABEL, MVT::Other, Expand);
238 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
239 setOperationAction(ISD::VASTART , MVT::Other, Custom);
240 setOperationAction(ISD::VAARG , MVT::Other, Expand);
241 setOperationAction(ISD::VAEND , MVT::Other, Expand);
242 if (Subtarget->is64Bit())
243 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
245 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
247 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
248 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
249 if (Subtarget->is64Bit())
250 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
251 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
254 // Set up the FP register classes.
255 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
256 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
258 // Use ANDPD to simulate FABS.
259 setOperationAction(ISD::FABS , MVT::f64, Custom);
260 setOperationAction(ISD::FABS , MVT::f32, Custom);
262 // Use XORP to simulate FNEG.
263 setOperationAction(ISD::FNEG , MVT::f64, Custom);
264 setOperationAction(ISD::FNEG , MVT::f32, Custom);
266 // Use ANDPD and ORPD to simulate FCOPYSIGN.
267 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
268 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
270 // We don't support sin/cos/fmod
271 setOperationAction(ISD::FSIN , MVT::f64, Expand);
272 setOperationAction(ISD::FCOS , MVT::f64, Expand);
273 setOperationAction(ISD::FREM , MVT::f64, Expand);
274 setOperationAction(ISD::FSIN , MVT::f32, Expand);
275 setOperationAction(ISD::FCOS , MVT::f32, Expand);
276 setOperationAction(ISD::FREM , MVT::f32, Expand);
278 // Expand FP immediates into loads from the stack, except for the special
280 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
281 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
282 addLegalFPImmediate(+0.0); // xorps / xorpd
284 // Set up the FP register classes.
285 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
287 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
288 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
289 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
292 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
293 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
296 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
297 addLegalFPImmediate(+0.0); // FLD0
298 addLegalFPImmediate(+1.0); // FLD1
299 addLegalFPImmediate(-0.0); // FLD0/FCHS
300 addLegalFPImmediate(-1.0); // FLD1/FCHS
303 // First set operation action for all vector types to expand. Then we
304 // will selectively turn on ones that can be effectively codegen'd.
305 for (unsigned VT = (unsigned)MVT::Vector + 1;
306 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
307 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
308 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
309 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
310 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
311 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
312 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
313 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
314 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
315 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
316 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
317 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
318 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
319 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
320 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
321 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
324 if (Subtarget->hasMMX()) {
325 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
326 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
327 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
329 // FIXME: add MMX packed arithmetics
330 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
331 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v2i32);
332 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
333 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v2i32);
334 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
336 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
337 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
338 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
341 if (Subtarget->hasSSE1()) {
342 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
344 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
345 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
346 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
347 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
348 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
349 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
350 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
351 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
352 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
355 if (Subtarget->hasSSE2()) {
356 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
357 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
358 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
359 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
360 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
362 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
363 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
364 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
365 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
366 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
367 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
368 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
369 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
370 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
371 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
372 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
374 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
375 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
376 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
377 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
378 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
379 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
381 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
382 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
383 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
384 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
385 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
387 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
388 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
389 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
390 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
391 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
392 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
394 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
395 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
396 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
397 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
398 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
399 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
400 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
401 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
402 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
403 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
404 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
405 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
408 // Custom lower v2i64 and v2f64 selects.
409 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
410 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
411 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
412 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
415 // We want to custom lower some of our intrinsics.
416 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
418 // We have target-specific dag combine patterns for the following nodes:
419 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
420 setTargetDAGCombine(ISD::SELECT);
422 computeRegisterProperties();
424 // FIXME: These should be based on subtarget info. Plus, the values should
425 // be smaller when we are in optimizing for size mode.
426 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
427 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
428 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
429 allowUnalignedMemoryAccesses = true; // x86 supports it!
433 //===----------------------------------------------------------------------===//
434 // Return Value Calling Convention Implementation
435 //===----------------------------------------------------------------------===//
437 #include "X86GenCallingConv.inc"
439 /// LowerRET - Lower an ISD::RET node.
440 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
441 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
443 SmallVector<CCValAssign, 16> RVLocs;
444 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
445 CCState CCInfo(CC, getTargetMachine(), RVLocs);
446 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
449 // If this is the first return lowered for this function, add the regs to the
450 // liveout set for the function.
451 if (DAG.getMachineFunction().liveout_empty()) {
452 for (unsigned i = 0; i != RVLocs.size(); ++i)
453 if (RVLocs[i].isRegLoc())
454 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
457 SDOperand Chain = Op.getOperand(0);
460 // Copy the result values into the output registers.
461 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
462 RVLocs[0].getLocReg() != X86::ST0) {
463 for (unsigned i = 0; i != RVLocs.size(); ++i) {
464 CCValAssign &VA = RVLocs[i];
465 assert(VA.isRegLoc() && "Can only return in registers!");
466 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
468 Flag = Chain.getValue(1);
471 // We need to handle a destination of ST0 specially, because it isn't really
473 SDOperand Value = Op.getOperand(1);
475 // If this is an FP return with ScalarSSE, we need to move the value from
476 // an XMM register onto the fp-stack.
480 // If this is a load into a scalarsse value, don't store the loaded value
481 // back to the stack, only to reload it: just replace the scalar-sse load.
482 if (ISD::isNON_EXTLoad(Value.Val) &&
483 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
484 Chain = Value.getOperand(0);
485 MemLoc = Value.getOperand(1);
487 // Spill the value to memory and reload it into top of stack.
488 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
489 MachineFunction &MF = DAG.getMachineFunction();
490 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
491 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
492 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
494 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
495 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
496 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
497 Chain = Value.getValue(1);
500 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
501 SDOperand Ops[] = { Chain, Value };
502 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
503 Flag = Chain.getValue(1);
506 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
508 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
510 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
514 /// LowerCallResult - Lower the result values of an ISD::CALL into the
515 /// appropriate copies out of appropriate physical registers. This assumes that
516 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
517 /// being lowered. The returns a SDNode with the same number of values as the
519 SDNode *X86TargetLowering::
520 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
521 unsigned CallingConv, SelectionDAG &DAG) {
523 // Assign locations to each value returned by this call.
524 SmallVector<CCValAssign, 16> RVLocs;
525 CCState CCInfo(CallingConv, getTargetMachine(), RVLocs);
526 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
529 SmallVector<SDOperand, 8> ResultVals;
531 // Copy all of the result registers out of their specified physreg.
532 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
533 for (unsigned i = 0; i != RVLocs.size(); ++i) {
534 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
535 RVLocs[i].getValVT(), InFlag).getValue(1);
536 InFlag = Chain.getValue(2);
537 ResultVals.push_back(Chain.getValue(0));
540 // Copies from the FP stack are special, as ST0 isn't a valid register
541 // before the fp stackifier runs.
543 // Copy ST0 into an RFP register with FP_GET_RESULT.
544 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
545 SDOperand GROps[] = { Chain, InFlag };
546 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
547 Chain = RetVal.getValue(1);
548 InFlag = RetVal.getValue(2);
550 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
553 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
554 // shouldn't be necessary except that RFP cannot be live across
555 // multiple blocks. When stackifier is fixed, they can be uncoupled.
556 MachineFunction &MF = DAG.getMachineFunction();
557 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
558 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
560 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
562 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
563 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
564 Chain = RetVal.getValue(1);
567 if (RVLocs[0].getValVT() == MVT::f32 && !X86ScalarSSE)
568 // FIXME: we would really like to remember that this FP_ROUND
569 // operation is okay to eliminate if we allow excess FP precision.
570 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
571 ResultVals.push_back(RetVal);
574 // Merge everything together with a MERGE_VALUES node.
575 ResultVals.push_back(Chain);
576 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
577 &ResultVals[0], ResultVals.size()).Val;
581 //===----------------------------------------------------------------------===//
582 // C & StdCall Calling Convention implementation
583 //===----------------------------------------------------------------------===//
584 // StdCall calling convention seems to be standard for many Windows' API
585 // routines and around. It differs from C calling convention just a little:
586 // callee should clean up the stack, not caller. Symbols should be also
587 // decorated in some fancy way :) It doesn't support any vector arguments.
589 /// AddLiveIn - This helper function adds the specified physical register to the
590 /// MachineFunction as a live in value. It also creates a corresponding virtual
592 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
593 const TargetRegisterClass *RC) {
594 assert(RC->contains(PReg) && "Not the correct regclass!");
595 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
596 MF.addLiveIn(PReg, VReg);
600 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
602 unsigned NumArgs = Op.Val->getNumValues() - 1;
603 MachineFunction &MF = DAG.getMachineFunction();
604 MachineFrameInfo *MFI = MF.getFrameInfo();
605 SDOperand Root = Op.getOperand(0);
606 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
608 // Assign locations to all of the incoming arguments.
609 SmallVector<CCValAssign, 16> ArgLocs;
610 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
612 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
614 SmallVector<SDOperand, 8> ArgValues;
615 unsigned LastVal = ~0U;
616 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
617 CCValAssign &VA = ArgLocs[i];
618 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
620 assert(VA.getValNo() != LastVal &&
621 "Don't support value assigned to multiple locs yet");
622 LastVal = VA.getValNo();
625 MVT::ValueType RegVT = VA.getLocVT();
626 TargetRegisterClass *RC;
627 if (RegVT == MVT::i32)
628 RC = X86::GR32RegisterClass;
630 assert(MVT::isVector(RegVT));
631 RC = X86::VR128RegisterClass;
634 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
635 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
637 // If this is an 8 or 16-bit value, it is really passed promoted to 32
638 // bits. Insert an assert[sz]ext to capture this, then truncate to the
640 if (VA.getLocInfo() == CCValAssign::SExt)
641 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
642 DAG.getValueType(VA.getValVT()));
643 else if (VA.getLocInfo() == CCValAssign::ZExt)
644 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
645 DAG.getValueType(VA.getValVT()));
647 if (VA.getLocInfo() != CCValAssign::Full)
648 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
650 ArgValues.push_back(ArgValue);
652 assert(VA.isMemLoc());
654 // Create the nodes corresponding to a load from this parameter slot.
655 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
656 VA.getLocMemOffset());
657 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
658 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
662 unsigned StackSize = CCInfo.getNextStackOffset();
664 ArgValues.push_back(Root);
666 // If the function takes variable number of arguments, make a frame index for
667 // the start of the first vararg value... for expansion of llvm.va_start.
669 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
671 if (isStdCall && !isVarArg) {
672 BytesToPopOnReturn = StackSize; // Callee pops everything..
673 BytesCallerReserves = 0;
675 BytesToPopOnReturn = 0; // Callee pops nothing.
677 // If this is an sret function, the return should pop the hidden pointer.
679 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
680 SDISelParamFlags::StructReturn))
681 BytesToPopOnReturn = 4;
683 BytesCallerReserves = StackSize;
686 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
687 ReturnAddrIndex = 0; // No return address slot generated yet.
689 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
691 // Return the new list of results.
692 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
693 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
696 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
698 SDOperand Chain = Op.getOperand(0);
699 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
700 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
701 SDOperand Callee = Op.getOperand(4);
702 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
704 // Analyze operands of the call, assigning locations to each operand.
705 SmallVector<CCValAssign, 16> ArgLocs;
706 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
707 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
709 // Get a count of how many bytes are to be pushed on the stack.
710 unsigned NumBytes = CCInfo.getNextStackOffset();
712 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
714 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
715 SmallVector<SDOperand, 8> MemOpChains;
719 // Walk the register/memloc assignments, inserting copies/loads.
720 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
721 CCValAssign &VA = ArgLocs[i];
722 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
724 // Promote the value if needed.
725 switch (VA.getLocInfo()) {
726 default: assert(0 && "Unknown loc info!");
727 case CCValAssign::Full: break;
728 case CCValAssign::SExt:
729 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
731 case CCValAssign::ZExt:
732 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
734 case CCValAssign::AExt:
735 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
740 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
742 assert(VA.isMemLoc());
743 if (StackPtr.Val == 0)
744 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
745 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
746 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
747 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
751 // If the first argument is an sret pointer, remember it.
752 bool isSRet = NumOps &&
753 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
754 SDISelParamFlags::StructReturn);
756 if (!MemOpChains.empty())
757 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
758 &MemOpChains[0], MemOpChains.size());
760 // Build a sequence of copy-to-reg nodes chained together with token chain
761 // and flag operands which copy the outgoing args into registers.
763 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
764 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
766 InFlag = Chain.getValue(1);
769 // ELF / PIC requires GOT in the EBX register before function calls via PLT
771 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
772 Subtarget->isPICStyleGOT()) {
773 Chain = DAG.getCopyToReg(Chain, X86::EBX,
774 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
776 InFlag = Chain.getValue(1);
779 // If the callee is a GlobalAddress node (quite common, every direct call is)
780 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
781 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
782 // We should use extra load for direct calls to dllimported functions in
784 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
785 getTargetMachine(), true))
786 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
787 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
788 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
790 // Returns a chain & a flag for retval copy to use.
791 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
792 SmallVector<SDOperand, 8> Ops;
793 Ops.push_back(Chain);
794 Ops.push_back(Callee);
796 // Add argument registers to the end of the list so that they are known live
798 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
799 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
800 RegsToPass[i].second.getValueType()));
802 // Add an implicit use GOT pointer in EBX.
803 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
804 Subtarget->isPICStyleGOT())
805 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
808 Ops.push_back(InFlag);
810 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
811 NodeTys, &Ops[0], Ops.size());
812 InFlag = Chain.getValue(1);
814 // Create the CALLSEQ_END node.
815 unsigned NumBytesForCalleeToPush = 0;
817 if (CC == CallingConv::X86_StdCall) {
819 NumBytesForCalleeToPush = isSRet ? 4 : 0;
821 NumBytesForCalleeToPush = NumBytes;
823 // If this is is a call to a struct-return function, the callee
824 // pops the hidden struct pointer, so we have to push it back.
825 // This is common for Darwin/X86, Linux & Mingw32 targets.
826 NumBytesForCalleeToPush = isSRet ? 4 : 0;
829 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
831 Ops.push_back(Chain);
832 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
833 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
834 Ops.push_back(InFlag);
835 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
836 InFlag = Chain.getValue(1);
838 // Handle result values, copying them out of physregs into vregs that we
840 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
844 //===----------------------------------------------------------------------===//
845 // FastCall Calling Convention implementation
846 //===----------------------------------------------------------------------===//
848 // The X86 'fastcall' calling convention passes up to two integer arguments in
849 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
850 // and requires that the callee pop its arguments off the stack (allowing proper
851 // tail calls), and has the same return value conventions as C calling convs.
853 // This calling convention always arranges for the callee pop value to be 8n+4
854 // bytes, which is needed for tail recursion elimination and stack alignment
857 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
858 MachineFunction &MF = DAG.getMachineFunction();
859 MachineFrameInfo *MFI = MF.getFrameInfo();
860 SDOperand Root = Op.getOperand(0);
862 // Assign locations to all of the incoming arguments.
863 SmallVector<CCValAssign, 16> ArgLocs;
864 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
866 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
868 SmallVector<SDOperand, 8> ArgValues;
869 unsigned LastVal = ~0U;
870 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
871 CCValAssign &VA = ArgLocs[i];
872 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
874 assert(VA.getValNo() != LastVal &&
875 "Don't support value assigned to multiple locs yet");
876 LastVal = VA.getValNo();
879 MVT::ValueType RegVT = VA.getLocVT();
880 TargetRegisterClass *RC;
881 if (RegVT == MVT::i32)
882 RC = X86::GR32RegisterClass;
884 assert(MVT::isVector(RegVT));
885 RC = X86::VR128RegisterClass;
888 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
889 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
891 // If this is an 8 or 16-bit value, it is really passed promoted to 32
892 // bits. Insert an assert[sz]ext to capture this, then truncate to the
894 if (VA.getLocInfo() == CCValAssign::SExt)
895 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
896 DAG.getValueType(VA.getValVT()));
897 else if (VA.getLocInfo() == CCValAssign::ZExt)
898 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
899 DAG.getValueType(VA.getValVT()));
901 if (VA.getLocInfo() != CCValAssign::Full)
902 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
904 ArgValues.push_back(ArgValue);
906 assert(VA.isMemLoc());
908 // Create the nodes corresponding to a load from this parameter slot.
909 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
910 VA.getLocMemOffset());
911 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
912 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
916 ArgValues.push_back(Root);
918 unsigned StackSize = CCInfo.getNextStackOffset();
920 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
921 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
922 // arguments and the arguments after the retaddr has been pushed are aligned.
923 if ((StackSize & 7) == 0)
927 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
928 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
929 ReturnAddrIndex = 0; // No return address slot generated yet.
930 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
931 BytesCallerReserves = 0;
933 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
935 // Return the new list of results.
936 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
937 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
940 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
942 SDOperand Chain = Op.getOperand(0);
943 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
944 SDOperand Callee = Op.getOperand(4);
946 // Analyze operands of the call, assigning locations to each operand.
947 SmallVector<CCValAssign, 16> ArgLocs;
948 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
949 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
951 // Get a count of how many bytes are to be pushed on the stack.
952 unsigned NumBytes = CCInfo.getNextStackOffset();
954 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
955 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
956 // arguments and the arguments after the retaddr has been pushed are aligned.
957 if ((NumBytes & 7) == 0)
961 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
963 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
964 SmallVector<SDOperand, 8> MemOpChains;
968 // Walk the register/memloc assignments, inserting copies/loads.
969 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
970 CCValAssign &VA = ArgLocs[i];
971 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
973 // Promote the value if needed.
974 switch (VA.getLocInfo()) {
975 default: assert(0 && "Unknown loc info!");
976 case CCValAssign::Full: break;
977 case CCValAssign::SExt:
978 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
980 case CCValAssign::ZExt:
981 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
983 case CCValAssign::AExt:
984 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
989 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
991 assert(VA.isMemLoc());
992 if (StackPtr.Val == 0)
993 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
994 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
995 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
996 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1000 if (!MemOpChains.empty())
1001 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1002 &MemOpChains[0], MemOpChains.size());
1004 // Build a sequence of copy-to-reg nodes chained together with token chain
1005 // and flag operands which copy the outgoing args into registers.
1007 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1008 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1010 InFlag = Chain.getValue(1);
1013 // If the callee is a GlobalAddress node (quite common, every direct call is)
1014 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1015 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1016 // We should use extra load for direct calls to dllimported functions in
1018 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1019 getTargetMachine(), true))
1020 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1021 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1022 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1024 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1026 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1027 Subtarget->isPICStyleGOT()) {
1028 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1029 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1031 InFlag = Chain.getValue(1);
1034 // Returns a chain & a flag for retval copy to use.
1035 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1036 SmallVector<SDOperand, 8> Ops;
1037 Ops.push_back(Chain);
1038 Ops.push_back(Callee);
1040 // Add argument registers to the end of the list so that they are known live
1042 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1043 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1044 RegsToPass[i].second.getValueType()));
1046 // Add an implicit use GOT pointer in EBX.
1047 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1048 Subtarget->isPICStyleGOT())
1049 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1052 Ops.push_back(InFlag);
1054 // FIXME: Do not generate X86ISD::TAILCALL for now.
1055 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1056 NodeTys, &Ops[0], Ops.size());
1057 InFlag = Chain.getValue(1);
1059 // Returns a flag for retval copy to use.
1060 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1062 Ops.push_back(Chain);
1063 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1064 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1065 Ops.push_back(InFlag);
1066 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1067 InFlag = Chain.getValue(1);
1069 // Handle result values, copying them out of physregs into vregs that we
1071 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1075 //===----------------------------------------------------------------------===//
1076 // X86-64 C Calling Convention implementation
1077 //===----------------------------------------------------------------------===//
1080 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1081 MachineFunction &MF = DAG.getMachineFunction();
1082 MachineFrameInfo *MFI = MF.getFrameInfo();
1083 SDOperand Root = Op.getOperand(0);
1084 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1086 static const unsigned GPR64ArgRegs[] = {
1087 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1089 static const unsigned XMMArgRegs[] = {
1090 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1091 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1095 // Assign locations to all of the incoming arguments.
1096 SmallVector<CCValAssign, 16> ArgLocs;
1097 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
1099 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1101 SmallVector<SDOperand, 8> ArgValues;
1102 unsigned LastVal = ~0U;
1103 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1104 CCValAssign &VA = ArgLocs[i];
1105 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1107 assert(VA.getValNo() != LastVal &&
1108 "Don't support value assigned to multiple locs yet");
1109 LastVal = VA.getValNo();
1111 if (VA.isRegLoc()) {
1112 MVT::ValueType RegVT = VA.getLocVT();
1113 TargetRegisterClass *RC;
1114 if (RegVT == MVT::i32)
1115 RC = X86::GR32RegisterClass;
1116 else if (RegVT == MVT::i64)
1117 RC = X86::GR64RegisterClass;
1118 else if (RegVT == MVT::f32)
1119 RC = X86::FR32RegisterClass;
1120 else if (RegVT == MVT::f64)
1121 RC = X86::FR64RegisterClass;
1123 assert(MVT::isVector(RegVT));
1124 RC = X86::VR128RegisterClass;
1127 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1128 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1130 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1131 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1133 if (VA.getLocInfo() == CCValAssign::SExt)
1134 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1135 DAG.getValueType(VA.getValVT()));
1136 else if (VA.getLocInfo() == CCValAssign::ZExt)
1137 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1138 DAG.getValueType(VA.getValVT()));
1140 if (VA.getLocInfo() != CCValAssign::Full)
1141 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1143 ArgValues.push_back(ArgValue);
1145 assert(VA.isMemLoc());
1147 // Create the nodes corresponding to a load from this parameter slot.
1148 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1149 VA.getLocMemOffset());
1150 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1151 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1155 unsigned StackSize = CCInfo.getNextStackOffset();
1157 // If the function takes variable number of arguments, make a frame index for
1158 // the start of the first vararg value... for expansion of llvm.va_start.
1160 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1161 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1163 // For X86-64, if there are vararg parameters that are passed via
1164 // registers, then we must store them to their spots on the stack so they
1165 // may be loaded by deferencing the result of va_next.
1166 VarArgsGPOffset = NumIntRegs * 8;
1167 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1168 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1169 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1171 // Store the integer parameter registers.
1172 SmallVector<SDOperand, 8> MemOps;
1173 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1174 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1175 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1176 for (; NumIntRegs != 6; ++NumIntRegs) {
1177 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1178 X86::GR64RegisterClass);
1179 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1180 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1181 MemOps.push_back(Store);
1182 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1183 DAG.getConstant(8, getPointerTy()));
1186 // Now store the XMM (fp + vector) parameter registers.
1187 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1188 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1189 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1190 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1191 X86::VR128RegisterClass);
1192 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1193 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1194 MemOps.push_back(Store);
1195 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1196 DAG.getConstant(16, getPointerTy()));
1198 if (!MemOps.empty())
1199 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1200 &MemOps[0], MemOps.size());
1203 ArgValues.push_back(Root);
1205 ReturnAddrIndex = 0; // No return address slot generated yet.
1206 BytesToPopOnReturn = 0; // Callee pops nothing.
1207 BytesCallerReserves = StackSize;
1209 // Return the new list of results.
1210 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1211 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1215 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1217 SDOperand Chain = Op.getOperand(0);
1218 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1219 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1220 SDOperand Callee = Op.getOperand(4);
1222 // Analyze operands of the call, assigning locations to each operand.
1223 SmallVector<CCValAssign, 16> ArgLocs;
1224 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
1225 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1227 // Get a count of how many bytes are to be pushed on the stack.
1228 unsigned NumBytes = CCInfo.getNextStackOffset();
1229 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1231 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1232 SmallVector<SDOperand, 8> MemOpChains;
1236 // Walk the register/memloc assignments, inserting copies/loads.
1237 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1238 CCValAssign &VA = ArgLocs[i];
1239 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1241 // Promote the value if needed.
1242 switch (VA.getLocInfo()) {
1243 default: assert(0 && "Unknown loc info!");
1244 case CCValAssign::Full: break;
1245 case CCValAssign::SExt:
1246 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1248 case CCValAssign::ZExt:
1249 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1251 case CCValAssign::AExt:
1252 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1256 if (VA.isRegLoc()) {
1257 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1259 assert(VA.isMemLoc());
1260 if (StackPtr.Val == 0)
1261 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1262 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1263 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1264 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1268 if (!MemOpChains.empty())
1269 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1270 &MemOpChains[0], MemOpChains.size());
1272 // Build a sequence of copy-to-reg nodes chained together with token chain
1273 // and flag operands which copy the outgoing args into registers.
1275 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1276 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1278 InFlag = Chain.getValue(1);
1282 // From AMD64 ABI document:
1283 // For calls that may call functions that use varargs or stdargs
1284 // (prototype-less calls or calls to functions containing ellipsis (...) in
1285 // the declaration) %al is used as hidden argument to specify the number
1286 // of SSE registers used. The contents of %al do not need to match exactly
1287 // the number of registers, but must be an ubound on the number of SSE
1288 // registers used and is in the range 0 - 8 inclusive.
1290 // Count the number of XMM registers allocated.
1291 static const unsigned XMMArgRegs[] = {
1292 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1293 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1295 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1297 Chain = DAG.getCopyToReg(Chain, X86::AL,
1298 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1299 InFlag = Chain.getValue(1);
1302 // If the callee is a GlobalAddress node (quite common, every direct call is)
1303 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1304 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1305 // We should use extra load for direct calls to dllimported functions in
1307 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1308 getTargetMachine(), true))
1309 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1310 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1311 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1313 // Returns a chain & a flag for retval copy to use.
1314 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1315 SmallVector<SDOperand, 8> Ops;
1316 Ops.push_back(Chain);
1317 Ops.push_back(Callee);
1319 // Add argument registers to the end of the list so that they are known live
1321 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1322 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1323 RegsToPass[i].second.getValueType()));
1326 Ops.push_back(InFlag);
1328 // FIXME: Do not generate X86ISD::TAILCALL for now.
1329 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1330 NodeTys, &Ops[0], Ops.size());
1331 InFlag = Chain.getValue(1);
1333 // Returns a flag for retval copy to use.
1334 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1336 Ops.push_back(Chain);
1337 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1338 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1339 Ops.push_back(InFlag);
1340 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1341 InFlag = Chain.getValue(1);
1343 // Handle result values, copying them out of physregs into vregs that we
1345 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1349 //===----------------------------------------------------------------------===//
1350 // Other Lowering Hooks
1351 //===----------------------------------------------------------------------===//
1354 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1355 if (ReturnAddrIndex == 0) {
1356 // Set up a frame object for the return address.
1357 MachineFunction &MF = DAG.getMachineFunction();
1358 if (Subtarget->is64Bit())
1359 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1361 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1364 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1369 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1370 /// specific condition code. It returns a false if it cannot do a direct
1371 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1373 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1374 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1375 SelectionDAG &DAG) {
1376 X86CC = X86::COND_INVALID;
1378 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1379 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1380 // X > -1 -> X == 0, jump !sign.
1381 RHS = DAG.getConstant(0, RHS.getValueType());
1382 X86CC = X86::COND_NS;
1384 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1385 // X < 0 -> X == 0, jump on sign.
1386 X86CC = X86::COND_S;
1391 switch (SetCCOpcode) {
1393 case ISD::SETEQ: X86CC = X86::COND_E; break;
1394 case ISD::SETGT: X86CC = X86::COND_G; break;
1395 case ISD::SETGE: X86CC = X86::COND_GE; break;
1396 case ISD::SETLT: X86CC = X86::COND_L; break;
1397 case ISD::SETLE: X86CC = X86::COND_LE; break;
1398 case ISD::SETNE: X86CC = X86::COND_NE; break;
1399 case ISD::SETULT: X86CC = X86::COND_B; break;
1400 case ISD::SETUGT: X86CC = X86::COND_A; break;
1401 case ISD::SETULE: X86CC = X86::COND_BE; break;
1402 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1405 // On a floating point condition, the flags are set as follows:
1407 // 0 | 0 | 0 | X > Y
1408 // 0 | 0 | 1 | X < Y
1409 // 1 | 0 | 0 | X == Y
1410 // 1 | 1 | 1 | unordered
1412 switch (SetCCOpcode) {
1415 case ISD::SETEQ: X86CC = X86::COND_E; break;
1416 case ISD::SETOLT: Flip = true; // Fallthrough
1418 case ISD::SETGT: X86CC = X86::COND_A; break;
1419 case ISD::SETOLE: Flip = true; // Fallthrough
1421 case ISD::SETGE: X86CC = X86::COND_AE; break;
1422 case ISD::SETUGT: Flip = true; // Fallthrough
1424 case ISD::SETLT: X86CC = X86::COND_B; break;
1425 case ISD::SETUGE: Flip = true; // Fallthrough
1427 case ISD::SETLE: X86CC = X86::COND_BE; break;
1429 case ISD::SETNE: X86CC = X86::COND_NE; break;
1430 case ISD::SETUO: X86CC = X86::COND_P; break;
1431 case ISD::SETO: X86CC = X86::COND_NP; break;
1434 std::swap(LHS, RHS);
1437 return X86CC != X86::COND_INVALID;
1440 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1441 /// code. Current x86 isa includes the following FP cmov instructions:
1442 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1443 static bool hasFPCMov(unsigned X86CC) {
1459 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1460 /// true if Op is undef or if its value falls within the specified range (L, H].
1461 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1462 if (Op.getOpcode() == ISD::UNDEF)
1465 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1466 return (Val >= Low && Val < Hi);
1469 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1470 /// true if Op is undef or if its value equal to the specified value.
1471 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1472 if (Op.getOpcode() == ISD::UNDEF)
1474 return cast<ConstantSDNode>(Op)->getValue() == Val;
1477 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1478 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1479 bool X86::isPSHUFDMask(SDNode *N) {
1480 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1482 if (N->getNumOperands() != 4)
1485 // Check if the value doesn't reference the second vector.
1486 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1487 SDOperand Arg = N->getOperand(i);
1488 if (Arg.getOpcode() == ISD::UNDEF) continue;
1489 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1490 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1497 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1498 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1499 bool X86::isPSHUFHWMask(SDNode *N) {
1500 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1502 if (N->getNumOperands() != 8)
1505 // Lower quadword copied in order.
1506 for (unsigned i = 0; i != 4; ++i) {
1507 SDOperand Arg = N->getOperand(i);
1508 if (Arg.getOpcode() == ISD::UNDEF) continue;
1509 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1510 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1514 // Upper quadword shuffled.
1515 for (unsigned i = 4; i != 8; ++i) {
1516 SDOperand Arg = N->getOperand(i);
1517 if (Arg.getOpcode() == ISD::UNDEF) continue;
1518 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1519 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1520 if (Val < 4 || Val > 7)
1527 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1528 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1529 bool X86::isPSHUFLWMask(SDNode *N) {
1530 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1532 if (N->getNumOperands() != 8)
1535 // Upper quadword copied in order.
1536 for (unsigned i = 4; i != 8; ++i)
1537 if (!isUndefOrEqual(N->getOperand(i), i))
1540 // Lower quadword shuffled.
1541 for (unsigned i = 0; i != 4; ++i)
1542 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1548 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1549 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1550 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1551 if (NumElems != 2 && NumElems != 4) return false;
1553 unsigned Half = NumElems / 2;
1554 for (unsigned i = 0; i < Half; ++i)
1555 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1557 for (unsigned i = Half; i < NumElems; ++i)
1558 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1564 bool X86::isSHUFPMask(SDNode *N) {
1565 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1566 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1569 /// isCommutedSHUFP - Returns true if the shuffle mask is except
1570 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1571 /// half elements to come from vector 1 (which would equal the dest.) and
1572 /// the upper half to come from vector 2.
1573 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1574 if (NumOps != 2 && NumOps != 4) return false;
1576 unsigned Half = NumOps / 2;
1577 for (unsigned i = 0; i < Half; ++i)
1578 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1580 for (unsigned i = Half; i < NumOps; ++i)
1581 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1586 static bool isCommutedSHUFP(SDNode *N) {
1587 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1588 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1591 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1592 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1593 bool X86::isMOVHLPSMask(SDNode *N) {
1594 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1596 if (N->getNumOperands() != 4)
1599 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1600 return isUndefOrEqual(N->getOperand(0), 6) &&
1601 isUndefOrEqual(N->getOperand(1), 7) &&
1602 isUndefOrEqual(N->getOperand(2), 2) &&
1603 isUndefOrEqual(N->getOperand(3), 3);
1606 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1607 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1609 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1610 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1612 if (N->getNumOperands() != 4)
1615 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1616 return isUndefOrEqual(N->getOperand(0), 2) &&
1617 isUndefOrEqual(N->getOperand(1), 3) &&
1618 isUndefOrEqual(N->getOperand(2), 2) &&
1619 isUndefOrEqual(N->getOperand(3), 3);
1622 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1623 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1624 bool X86::isMOVLPMask(SDNode *N) {
1625 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1627 unsigned NumElems = N->getNumOperands();
1628 if (NumElems != 2 && NumElems != 4)
1631 for (unsigned i = 0; i < NumElems/2; ++i)
1632 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1635 for (unsigned i = NumElems/2; i < NumElems; ++i)
1636 if (!isUndefOrEqual(N->getOperand(i), i))
1642 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1643 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1645 bool X86::isMOVHPMask(SDNode *N) {
1646 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1648 unsigned NumElems = N->getNumOperands();
1649 if (NumElems != 2 && NumElems != 4)
1652 for (unsigned i = 0; i < NumElems/2; ++i)
1653 if (!isUndefOrEqual(N->getOperand(i), i))
1656 for (unsigned i = 0; i < NumElems/2; ++i) {
1657 SDOperand Arg = N->getOperand(i + NumElems/2);
1658 if (!isUndefOrEqual(Arg, i + NumElems))
1665 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1666 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1667 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1668 bool V2IsSplat = false) {
1669 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1672 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1673 SDOperand BitI = Elts[i];
1674 SDOperand BitI1 = Elts[i+1];
1675 if (!isUndefOrEqual(BitI, j))
1678 if (isUndefOrEqual(BitI1, NumElts))
1681 if (!isUndefOrEqual(BitI1, j + NumElts))
1689 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1690 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1691 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1694 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1695 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1696 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1697 bool V2IsSplat = false) {
1698 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1701 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1702 SDOperand BitI = Elts[i];
1703 SDOperand BitI1 = Elts[i+1];
1704 if (!isUndefOrEqual(BitI, j + NumElts/2))
1707 if (isUndefOrEqual(BitI1, NumElts))
1710 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1718 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1719 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1720 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1723 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1724 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1726 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1727 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1729 unsigned NumElems = N->getNumOperands();
1730 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1733 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1734 SDOperand BitI = N->getOperand(i);
1735 SDOperand BitI1 = N->getOperand(i+1);
1737 if (!isUndefOrEqual(BitI, j))
1739 if (!isUndefOrEqual(BitI1, j))
1746 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1747 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1748 /// MOVSD, and MOVD, i.e. setting the lowest element.
1749 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1750 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1753 if (!isUndefOrEqual(Elts[0], NumElts))
1756 for (unsigned i = 1; i < NumElts; ++i) {
1757 if (!isUndefOrEqual(Elts[i], i))
1764 bool X86::isMOVLMask(SDNode *N) {
1765 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1766 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
1769 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1770 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1771 /// element of vector 2 and the other elements to come from vector 1 in order.
1772 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1773 bool V2IsSplat = false,
1774 bool V2IsUndef = false) {
1775 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
1778 if (!isUndefOrEqual(Ops[0], 0))
1781 for (unsigned i = 1; i < NumOps; ++i) {
1782 SDOperand Arg = Ops[i];
1783 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1784 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1785 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
1792 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1793 bool V2IsUndef = false) {
1794 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1795 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1796 V2IsSplat, V2IsUndef);
1799 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1800 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1801 bool X86::isMOVSHDUPMask(SDNode *N) {
1802 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1804 if (N->getNumOperands() != 4)
1807 // Expect 1, 1, 3, 3
1808 for (unsigned i = 0; i < 2; ++i) {
1809 SDOperand Arg = N->getOperand(i);
1810 if (Arg.getOpcode() == ISD::UNDEF) continue;
1811 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1812 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1813 if (Val != 1) return false;
1817 for (unsigned i = 2; i < 4; ++i) {
1818 SDOperand Arg = N->getOperand(i);
1819 if (Arg.getOpcode() == ISD::UNDEF) continue;
1820 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1821 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1822 if (Val != 3) return false;
1826 // Don't use movshdup if it can be done with a shufps.
1830 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1831 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1832 bool X86::isMOVSLDUPMask(SDNode *N) {
1833 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1835 if (N->getNumOperands() != 4)
1838 // Expect 0, 0, 2, 2
1839 for (unsigned i = 0; i < 2; ++i) {
1840 SDOperand Arg = N->getOperand(i);
1841 if (Arg.getOpcode() == ISD::UNDEF) continue;
1842 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1843 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1844 if (Val != 0) return false;
1848 for (unsigned i = 2; i < 4; ++i) {
1849 SDOperand Arg = N->getOperand(i);
1850 if (Arg.getOpcode() == ISD::UNDEF) continue;
1851 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1852 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1853 if (Val != 2) return false;
1857 // Don't use movshdup if it can be done with a shufps.
1861 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1862 /// a splat of a single element.
1863 static bool isSplatMask(SDNode *N) {
1864 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1866 // This is a splat operation if each element of the permute is the same, and
1867 // if the value doesn't reference the second vector.
1868 unsigned NumElems = N->getNumOperands();
1869 SDOperand ElementBase;
1871 for (; i != NumElems; ++i) {
1872 SDOperand Elt = N->getOperand(i);
1873 if (isa<ConstantSDNode>(Elt)) {
1879 if (!ElementBase.Val)
1882 for (; i != NumElems; ++i) {
1883 SDOperand Arg = N->getOperand(i);
1884 if (Arg.getOpcode() == ISD::UNDEF) continue;
1885 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1886 if (Arg != ElementBase) return false;
1889 // Make sure it is a splat of the first vector operand.
1890 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
1893 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1894 /// a splat of a single element and it's a 2 or 4 element mask.
1895 bool X86::isSplatMask(SDNode *N) {
1896 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1898 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
1899 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1901 return ::isSplatMask(N);
1904 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
1905 /// specifies a splat of zero element.
1906 bool X86::isSplatLoMask(SDNode *N) {
1907 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1909 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
1910 if (!isUndefOrEqual(N->getOperand(i), 0))
1915 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1916 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1918 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
1919 unsigned NumOperands = N->getNumOperands();
1920 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1922 for (unsigned i = 0; i < NumOperands; ++i) {
1924 SDOperand Arg = N->getOperand(NumOperands-i-1);
1925 if (Arg.getOpcode() != ISD::UNDEF)
1926 Val = cast<ConstantSDNode>(Arg)->getValue();
1927 if (Val >= NumOperands) Val -= NumOperands;
1929 if (i != NumOperands - 1)
1936 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1937 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1939 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1941 // 8 nodes, but we only care about the last 4.
1942 for (unsigned i = 7; i >= 4; --i) {
1944 SDOperand Arg = N->getOperand(i);
1945 if (Arg.getOpcode() != ISD::UNDEF)
1946 Val = cast<ConstantSDNode>(Arg)->getValue();
1955 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1956 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1958 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1960 // 8 nodes, but we only care about the first 4.
1961 for (int i = 3; i >= 0; --i) {
1963 SDOperand Arg = N->getOperand(i);
1964 if (Arg.getOpcode() != ISD::UNDEF)
1965 Val = cast<ConstantSDNode>(Arg)->getValue();
1974 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1975 /// specifies a 8 element shuffle that can be broken into a pair of
1976 /// PSHUFHW and PSHUFLW.
1977 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1978 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1980 if (N->getNumOperands() != 8)
1983 // Lower quadword shuffled.
1984 for (unsigned i = 0; i != 4; ++i) {
1985 SDOperand Arg = N->getOperand(i);
1986 if (Arg.getOpcode() == ISD::UNDEF) continue;
1987 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1988 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1993 // Upper quadword shuffled.
1994 for (unsigned i = 4; i != 8; ++i) {
1995 SDOperand Arg = N->getOperand(i);
1996 if (Arg.getOpcode() == ISD::UNDEF) continue;
1997 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1998 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1999 if (Val < 4 || Val > 7)
2006 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2007 /// values in ther permute mask.
2008 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2009 SDOperand &V2, SDOperand &Mask,
2010 SelectionDAG &DAG) {
2011 MVT::ValueType VT = Op.getValueType();
2012 MVT::ValueType MaskVT = Mask.getValueType();
2013 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2014 unsigned NumElems = Mask.getNumOperands();
2015 SmallVector<SDOperand, 8> MaskVec;
2017 for (unsigned i = 0; i != NumElems; ++i) {
2018 SDOperand Arg = Mask.getOperand(i);
2019 if (Arg.getOpcode() == ISD::UNDEF) {
2020 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2023 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2024 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2026 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2028 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2032 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2033 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2036 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2037 /// match movhlps. The lower half elements should come from upper half of
2038 /// V1 (and in order), and the upper half elements should come from the upper
2039 /// half of V2 (and in order).
2040 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2041 unsigned NumElems = Mask->getNumOperands();
2044 for (unsigned i = 0, e = 2; i != e; ++i)
2045 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2047 for (unsigned i = 2; i != 4; ++i)
2048 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2053 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2054 /// is promoted to a vector.
2055 static inline bool isScalarLoadToVector(SDNode *N) {
2056 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2057 N = N->getOperand(0).Val;
2058 return ISD::isNON_EXTLoad(N);
2063 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2064 /// match movlp{s|d}. The lower half elements should come from lower half of
2065 /// V1 (and in order), and the upper half elements should come from the upper
2066 /// half of V2 (and in order). And since V1 will become the source of the
2067 /// MOVLP, it must be either a vector load or a scalar load to vector.
2068 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2069 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2071 // Is V2 is a vector load, don't do this transformation. We will try to use
2072 // load folding shufps op.
2073 if (ISD::isNON_EXTLoad(V2))
2076 unsigned NumElems = Mask->getNumOperands();
2077 if (NumElems != 2 && NumElems != 4)
2079 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2080 if (!isUndefOrEqual(Mask->getOperand(i), i))
2082 for (unsigned i = NumElems/2; i != NumElems; ++i)
2083 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2088 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2090 static bool isSplatVector(SDNode *N) {
2091 if (N->getOpcode() != ISD::BUILD_VECTOR)
2094 SDOperand SplatValue = N->getOperand(0);
2095 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2096 if (N->getOperand(i) != SplatValue)
2101 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2103 static bool isUndefShuffle(SDNode *N) {
2104 if (N->getOpcode() != ISD::BUILD_VECTOR)
2107 SDOperand V1 = N->getOperand(0);
2108 SDOperand V2 = N->getOperand(1);
2109 SDOperand Mask = N->getOperand(2);
2110 unsigned NumElems = Mask.getNumOperands();
2111 for (unsigned i = 0; i != NumElems; ++i) {
2112 SDOperand Arg = Mask.getOperand(i);
2113 if (Arg.getOpcode() != ISD::UNDEF) {
2114 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2115 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2117 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2124 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2125 /// that point to V2 points to its first element.
2126 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2127 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2129 bool Changed = false;
2130 SmallVector<SDOperand, 8> MaskVec;
2131 unsigned NumElems = Mask.getNumOperands();
2132 for (unsigned i = 0; i != NumElems; ++i) {
2133 SDOperand Arg = Mask.getOperand(i);
2134 if (Arg.getOpcode() != ISD::UNDEF) {
2135 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2136 if (Val > NumElems) {
2137 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2141 MaskVec.push_back(Arg);
2145 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2146 &MaskVec[0], MaskVec.size());
2150 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2151 /// operation of specified width.
2152 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2153 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2154 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2156 SmallVector<SDOperand, 8> MaskVec;
2157 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2158 for (unsigned i = 1; i != NumElems; ++i)
2159 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2160 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2163 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2164 /// of specified width.
2165 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2166 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2167 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2168 SmallVector<SDOperand, 8> MaskVec;
2169 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2170 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2171 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2173 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2176 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2177 /// of specified width.
2178 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2179 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2180 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2181 unsigned Half = NumElems/2;
2182 SmallVector<SDOperand, 8> MaskVec;
2183 for (unsigned i = 0; i != Half; ++i) {
2184 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2185 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2187 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2190 /// getZeroVector - Returns a vector of specified type with all zero elements.
2192 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2193 assert(MVT::isVector(VT) && "Expected a vector type");
2194 unsigned NumElems = getVectorNumElements(VT);
2195 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2196 bool isFP = MVT::isFloatingPoint(EVT);
2197 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2198 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2199 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2202 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2204 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2205 SDOperand V1 = Op.getOperand(0);
2206 SDOperand Mask = Op.getOperand(2);
2207 MVT::ValueType VT = Op.getValueType();
2208 unsigned NumElems = Mask.getNumOperands();
2209 Mask = getUnpacklMask(NumElems, DAG);
2210 while (NumElems != 4) {
2211 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2214 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2216 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2217 Mask = getZeroVector(MaskVT, DAG);
2218 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2219 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2220 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2223 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2225 static inline bool isZeroNode(SDOperand Elt) {
2226 return ((isa<ConstantSDNode>(Elt) &&
2227 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2228 (isa<ConstantFPSDNode>(Elt) &&
2229 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2232 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2233 /// vector and zero or undef vector.
2234 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2235 unsigned NumElems, unsigned Idx,
2236 bool isZero, SelectionDAG &DAG) {
2237 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2238 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2239 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2240 SDOperand Zero = DAG.getConstant(0, EVT);
2241 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2242 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2243 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2244 &MaskVec[0], MaskVec.size());
2245 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2248 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2250 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2251 unsigned NumNonZero, unsigned NumZero,
2252 SelectionDAG &DAG, TargetLowering &TLI) {
2258 for (unsigned i = 0; i < 16; ++i) {
2259 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2260 if (ThisIsNonZero && First) {
2262 V = getZeroVector(MVT::v8i16, DAG);
2264 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2269 SDOperand ThisElt(0, 0), LastElt(0, 0);
2270 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2271 if (LastIsNonZero) {
2272 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2274 if (ThisIsNonZero) {
2275 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2276 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2277 ThisElt, DAG.getConstant(8, MVT::i8));
2279 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2284 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2285 DAG.getConstant(i/2, TLI.getPointerTy()));
2289 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2292 /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2294 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2295 unsigned NumNonZero, unsigned NumZero,
2296 SelectionDAG &DAG, TargetLowering &TLI) {
2302 for (unsigned i = 0; i < 8; ++i) {
2303 bool isNonZero = (NonZeros & (1 << i)) != 0;
2307 V = getZeroVector(MVT::v8i16, DAG);
2309 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2312 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2313 DAG.getConstant(i, TLI.getPointerTy()));
2321 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2322 // All zero's are handled with pxor.
2323 if (ISD::isBuildVectorAllZeros(Op.Val))
2326 // All one's are handled with pcmpeqd.
2327 if (ISD::isBuildVectorAllOnes(Op.Val))
2330 MVT::ValueType VT = Op.getValueType();
2331 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2332 unsigned EVTBits = MVT::getSizeInBits(EVT);
2334 unsigned NumElems = Op.getNumOperands();
2335 unsigned NumZero = 0;
2336 unsigned NumNonZero = 0;
2337 unsigned NonZeros = 0;
2338 std::set<SDOperand> Values;
2339 for (unsigned i = 0; i < NumElems; ++i) {
2340 SDOperand Elt = Op.getOperand(i);
2341 if (Elt.getOpcode() != ISD::UNDEF) {
2343 if (isZeroNode(Elt))
2346 NonZeros |= (1 << i);
2352 if (NumNonZero == 0)
2353 // Must be a mix of zero and undef. Return a zero vector.
2354 return getZeroVector(VT, DAG);
2356 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2357 if (Values.size() == 1)
2360 // Special case for single non-zero element.
2361 if (NumNonZero == 1) {
2362 unsigned Idx = CountTrailingZeros_32(NonZeros);
2363 SDOperand Item = Op.getOperand(Idx);
2364 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2366 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2367 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2370 if (EVTBits == 32) {
2371 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2372 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2374 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2375 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2376 SmallVector<SDOperand, 8> MaskVec;
2377 for (unsigned i = 0; i < NumElems; i++)
2378 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2379 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2380 &MaskVec[0], MaskVec.size());
2381 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2382 DAG.getNode(ISD::UNDEF, VT), Mask);
2386 // Let legalizer expand 2-wide build_vector's.
2390 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2392 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2394 if (V.Val) return V;
2397 if (EVTBits == 16) {
2398 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2400 if (V.Val) return V;
2403 // If element VT is == 32 bits, turn it into a number of shuffles.
2404 SmallVector<SDOperand, 8> V;
2406 if (NumElems == 4 && NumZero > 0) {
2407 for (unsigned i = 0; i < 4; ++i) {
2408 bool isZero = !(NonZeros & (1 << i));
2410 V[i] = getZeroVector(VT, DAG);
2412 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2415 for (unsigned i = 0; i < 2; ++i) {
2416 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2419 V[i] = V[i*2]; // Must be a zero vector.
2422 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2423 getMOVLMask(NumElems, DAG));
2426 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2427 getMOVLMask(NumElems, DAG));
2430 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2431 getUnpacklMask(NumElems, DAG));
2436 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2437 // clears the upper bits.
2438 // FIXME: we can do the same for v4f32 case when we know both parts of
2439 // the lower half come from scalar_to_vector (loadf32). We should do
2440 // that in post legalizer dag combiner with target specific hooks.
2441 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2443 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2444 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2445 SmallVector<SDOperand, 8> MaskVec;
2446 bool Reverse = (NonZeros & 0x3) == 2;
2447 for (unsigned i = 0; i < 2; ++i)
2449 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2451 MaskVec.push_back(DAG.getConstant(i, EVT));
2452 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2453 for (unsigned i = 0; i < 2; ++i)
2455 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2457 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2458 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2459 &MaskVec[0], MaskVec.size());
2460 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2463 if (Values.size() > 2) {
2464 // Expand into a number of unpckl*.
2466 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2467 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2468 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2469 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2470 for (unsigned i = 0; i < NumElems; ++i)
2471 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2473 while (NumElems != 0) {
2474 for (unsigned i = 0; i < NumElems; ++i)
2475 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2486 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2487 SDOperand V1 = Op.getOperand(0);
2488 SDOperand V2 = Op.getOperand(1);
2489 SDOperand PermMask = Op.getOperand(2);
2490 MVT::ValueType VT = Op.getValueType();
2491 unsigned NumElems = PermMask.getNumOperands();
2492 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2493 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2494 bool V1IsSplat = false;
2495 bool V2IsSplat = false;
2497 if (isUndefShuffle(Op.Val))
2498 return DAG.getNode(ISD::UNDEF, VT);
2500 if (isSplatMask(PermMask.Val)) {
2501 if (NumElems <= 4) return Op;
2502 // Promote it to a v4i32 splat.
2503 return PromoteSplat(Op, DAG);
2506 if (X86::isMOVLMask(PermMask.Val))
2507 return (V1IsUndef) ? V2 : Op;
2509 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2510 X86::isMOVSLDUPMask(PermMask.Val) ||
2511 X86::isMOVHLPSMask(PermMask.Val) ||
2512 X86::isMOVHPMask(PermMask.Val) ||
2513 X86::isMOVLPMask(PermMask.Val))
2516 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2517 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2518 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2520 bool Commuted = false;
2521 V1IsSplat = isSplatVector(V1.Val);
2522 V2IsSplat = isSplatVector(V2.Val);
2523 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2524 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2525 std::swap(V1IsSplat, V2IsSplat);
2526 std::swap(V1IsUndef, V2IsUndef);
2530 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2531 if (V2IsUndef) return V1;
2532 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2534 // V2 is a splat, so the mask may be malformed. That is, it may point
2535 // to any V2 element. The instruction selectior won't like this. Get
2536 // a corrected mask and commute to form a proper MOVS{S|D}.
2537 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2538 if (NewMask.Val != PermMask.Val)
2539 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2544 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2545 X86::isUNPCKLMask(PermMask.Val) ||
2546 X86::isUNPCKHMask(PermMask.Val))
2550 // Normalize mask so all entries that point to V2 points to its first
2551 // element then try to match unpck{h|l} again. If match, return a
2552 // new vector_shuffle with the corrected mask.
2553 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2554 if (NewMask.Val != PermMask.Val) {
2555 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2556 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2557 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2558 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2559 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2560 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2565 // Normalize the node to match x86 shuffle ops if needed
2566 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2567 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2570 // Commute is back and try unpck* again.
2571 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2572 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2573 X86::isUNPCKLMask(PermMask.Val) ||
2574 X86::isUNPCKHMask(PermMask.Val))
2578 // If VT is integer, try PSHUF* first, then SHUFP*.
2579 if (MVT::isInteger(VT)) {
2580 if (X86::isPSHUFDMask(PermMask.Val) ||
2581 X86::isPSHUFHWMask(PermMask.Val) ||
2582 X86::isPSHUFLWMask(PermMask.Val)) {
2583 if (V2.getOpcode() != ISD::UNDEF)
2584 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2585 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2589 if (X86::isSHUFPMask(PermMask.Val))
2592 // Handle v8i16 shuffle high / low shuffle node pair.
2593 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2594 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2595 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2596 SmallVector<SDOperand, 8> MaskVec;
2597 for (unsigned i = 0; i != 4; ++i)
2598 MaskVec.push_back(PermMask.getOperand(i));
2599 for (unsigned i = 4; i != 8; ++i)
2600 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2601 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2602 &MaskVec[0], MaskVec.size());
2603 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2605 for (unsigned i = 0; i != 4; ++i)
2606 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2607 for (unsigned i = 4; i != 8; ++i)
2608 MaskVec.push_back(PermMask.getOperand(i));
2609 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2610 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2613 // Floating point cases in the other order.
2614 if (X86::isSHUFPMask(PermMask.Val))
2616 if (X86::isPSHUFDMask(PermMask.Val) ||
2617 X86::isPSHUFHWMask(PermMask.Val) ||
2618 X86::isPSHUFLWMask(PermMask.Val)) {
2619 if (V2.getOpcode() != ISD::UNDEF)
2620 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2621 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2626 if (NumElems == 4) {
2627 MVT::ValueType MaskVT = PermMask.getValueType();
2628 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2629 SmallVector<std::pair<int, int>, 8> Locs;
2630 Locs.reserve(NumElems);
2631 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2632 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2635 // If no more than two elements come from either vector. This can be
2636 // implemented with two shuffles. First shuffle gather the elements.
2637 // The second shuffle, which takes the first shuffle as both of its
2638 // vector operands, put the elements into the right order.
2639 for (unsigned i = 0; i != NumElems; ++i) {
2640 SDOperand Elt = PermMask.getOperand(i);
2641 if (Elt.getOpcode() == ISD::UNDEF) {
2642 Locs[i] = std::make_pair(-1, -1);
2644 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2645 if (Val < NumElems) {
2646 Locs[i] = std::make_pair(0, NumLo);
2650 Locs[i] = std::make_pair(1, NumHi);
2651 if (2+NumHi < NumElems)
2652 Mask1[2+NumHi] = Elt;
2657 if (NumLo <= 2 && NumHi <= 2) {
2658 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2659 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2660 &Mask1[0], Mask1.size()));
2661 for (unsigned i = 0; i != NumElems; ++i) {
2662 if (Locs[i].first == -1)
2665 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2666 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2667 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2671 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2672 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2673 &Mask2[0], Mask2.size()));
2676 // Break it into (shuffle shuffle_hi, shuffle_lo).
2678 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2679 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2680 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
2681 unsigned MaskIdx = 0;
2683 unsigned HiIdx = NumElems/2;
2684 for (unsigned i = 0; i != NumElems; ++i) {
2685 if (i == NumElems/2) {
2691 SDOperand Elt = PermMask.getOperand(i);
2692 if (Elt.getOpcode() == ISD::UNDEF) {
2693 Locs[i] = std::make_pair(-1, -1);
2694 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2695 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2696 (*MaskPtr)[LoIdx] = Elt;
2699 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2700 (*MaskPtr)[HiIdx] = Elt;
2705 SDOperand LoShuffle =
2706 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2707 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2708 &LoMask[0], LoMask.size()));
2709 SDOperand HiShuffle =
2710 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2711 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2712 &HiMask[0], HiMask.size()));
2713 SmallVector<SDOperand, 8> MaskOps;
2714 for (unsigned i = 0; i != NumElems; ++i) {
2715 if (Locs[i].first == -1) {
2716 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2718 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2719 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2722 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2723 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2724 &MaskOps[0], MaskOps.size()));
2731 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2732 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2735 MVT::ValueType VT = Op.getValueType();
2736 // TODO: handle v16i8.
2737 if (MVT::getSizeInBits(VT) == 16) {
2738 // Transform it so it match pextrw which produces a 32-bit result.
2739 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2740 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2741 Op.getOperand(0), Op.getOperand(1));
2742 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2743 DAG.getValueType(VT));
2744 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2745 } else if (MVT::getSizeInBits(VT) == 32) {
2746 SDOperand Vec = Op.getOperand(0);
2747 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2750 // SHUFPS the element to the lowest double word, then movss.
2751 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2752 SmallVector<SDOperand, 8> IdxVec;
2753 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2754 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2755 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2756 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2757 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2758 &IdxVec[0], IdxVec.size());
2759 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2760 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2761 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2762 DAG.getConstant(0, getPointerTy()));
2763 } else if (MVT::getSizeInBits(VT) == 64) {
2764 SDOperand Vec = Op.getOperand(0);
2765 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2769 // UNPCKHPD the element to the lowest double word, then movsd.
2770 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2771 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2772 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2773 SmallVector<SDOperand, 8> IdxVec;
2774 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2775 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2776 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2777 &IdxVec[0], IdxVec.size());
2778 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2779 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2780 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2781 DAG.getConstant(0, getPointerTy()));
2788 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2789 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2790 // as its second argument.
2791 MVT::ValueType VT = Op.getValueType();
2792 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2793 SDOperand N0 = Op.getOperand(0);
2794 SDOperand N1 = Op.getOperand(1);
2795 SDOperand N2 = Op.getOperand(2);
2796 if (MVT::getSizeInBits(BaseVT) == 16) {
2797 if (N1.getValueType() != MVT::i32)
2798 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2799 if (N2.getValueType() != MVT::i32)
2800 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2801 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2802 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2803 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2806 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2807 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2808 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2809 SmallVector<SDOperand, 8> MaskVec;
2810 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2811 for (unsigned i = 1; i <= 3; ++i)
2812 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2813 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2814 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2815 &MaskVec[0], MaskVec.size()));
2817 // Use two pinsrw instructions to insert a 32 bit value.
2819 if (MVT::isFloatingPoint(N1.getValueType())) {
2820 if (ISD::isNON_EXTLoad(N1.Val)) {
2821 // Just load directly from f32mem to GR32.
2822 LoadSDNode *LD = cast<LoadSDNode>(N1);
2823 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
2824 LD->getSrcValue(), LD->getSrcValueOffset());
2826 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2827 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2828 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2829 DAG.getConstant(0, getPointerTy()));
2832 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2833 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2834 DAG.getConstant(Idx, getPointerTy()));
2835 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2836 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2837 DAG.getConstant(Idx+1, getPointerTy()));
2838 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2846 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2847 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2848 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2851 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2852 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2853 // one of the above mentioned nodes. It has to be wrapped because otherwise
2854 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2855 // be used to form addressing mode. These wrapped nodes will be selected
2858 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2859 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2860 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
2862 CP->getAlignment());
2863 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2864 // With PIC, the address is actually $g + Offset.
2865 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2866 !Subtarget->isPICStyleRIPRel()) {
2867 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2868 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2876 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2877 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2878 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
2879 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2880 // With PIC, the address is actually $g + Offset.
2881 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2882 !Subtarget->isPICStyleRIPRel()) {
2883 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2884 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2888 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
2889 // load the value at address GV, not the value of GV itself. This means that
2890 // the GlobalAddress must be in the base or index register of the address, not
2891 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
2892 // The same applies for external symbols during PIC codegen
2893 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
2894 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
2900 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2901 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2902 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
2903 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2904 // With PIC, the address is actually $g + Offset.
2905 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2906 !Subtarget->isPICStyleRIPRel()) {
2907 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2908 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2915 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
2916 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
2917 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
2918 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
2919 // With PIC, the address is actually $g + Offset.
2920 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2921 !Subtarget->isPICStyleRIPRel()) {
2922 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2923 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2930 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
2931 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2932 "Not an i64 shift!");
2933 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2934 SDOperand ShOpLo = Op.getOperand(0);
2935 SDOperand ShOpHi = Op.getOperand(1);
2936 SDOperand ShAmt = Op.getOperand(2);
2937 SDOperand Tmp1 = isSRA ?
2938 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
2939 DAG.getConstant(0, MVT::i32);
2941 SDOperand Tmp2, Tmp3;
2942 if (Op.getOpcode() == ISD::SHL_PARTS) {
2943 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2944 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2946 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
2947 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
2950 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
2951 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
2952 DAG.getConstant(32, MVT::i8));
2953 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
2954 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
2957 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
2959 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
2960 SmallVector<SDOperand, 4> Ops;
2961 if (Op.getOpcode() == ISD::SHL_PARTS) {
2962 Ops.push_back(Tmp2);
2963 Ops.push_back(Tmp3);
2965 Ops.push_back(InFlag);
2966 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
2967 InFlag = Hi.getValue(1);
2970 Ops.push_back(Tmp3);
2971 Ops.push_back(Tmp1);
2973 Ops.push_back(InFlag);
2974 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
2976 Ops.push_back(Tmp2);
2977 Ops.push_back(Tmp3);
2979 Ops.push_back(InFlag);
2980 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
2981 InFlag = Lo.getValue(1);
2984 Ops.push_back(Tmp3);
2985 Ops.push_back(Tmp1);
2987 Ops.push_back(InFlag);
2988 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
2991 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
2995 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
2998 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2999 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3000 Op.getOperand(0).getValueType() >= MVT::i16 &&
3001 "Unknown SINT_TO_FP to lower!");
3004 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3005 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3006 MachineFunction &MF = DAG.getMachineFunction();
3007 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3008 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3009 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3010 StackSlot, NULL, 0);
3015 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3017 Tys = DAG.getVTList(MVT::f64, MVT::Other);
3018 SmallVector<SDOperand, 8> Ops;
3019 Ops.push_back(Chain);
3020 Ops.push_back(StackSlot);
3021 Ops.push_back(DAG.getValueType(SrcVT));
3022 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3023 Tys, &Ops[0], Ops.size());
3026 Chain = Result.getValue(1);
3027 SDOperand InFlag = Result.getValue(2);
3029 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3030 // shouldn't be necessary except that RFP cannot be live across
3031 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3032 MachineFunction &MF = DAG.getMachineFunction();
3033 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3034 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3035 Tys = DAG.getVTList(MVT::Other);
3036 SmallVector<SDOperand, 8> Ops;
3037 Ops.push_back(Chain);
3038 Ops.push_back(Result);
3039 Ops.push_back(StackSlot);
3040 Ops.push_back(DAG.getValueType(Op.getValueType()));
3041 Ops.push_back(InFlag);
3042 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3043 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3049 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3050 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3051 "Unknown FP_TO_SINT to lower!");
3052 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3054 MachineFunction &MF = DAG.getMachineFunction();
3055 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3056 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3057 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3060 switch (Op.getValueType()) {
3061 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3062 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3063 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3064 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3067 SDOperand Chain = DAG.getEntryNode();
3068 SDOperand Value = Op.getOperand(0);
3070 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3071 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3072 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
3074 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3076 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3077 Chain = Value.getValue(1);
3078 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3079 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3082 // Build the FP_TO_INT*_IN_MEM
3083 SDOperand Ops[] = { Chain, Value, StackSlot };
3084 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3087 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3090 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3091 MVT::ValueType VT = Op.getValueType();
3092 const Type *OpNTy = MVT::getTypeForValueType(VT);
3093 std::vector<Constant*> CV;
3094 if (VT == MVT::f64) {
3095 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3096 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3098 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3099 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3100 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3101 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3103 Constant *CS = ConstantStruct::get(CV);
3104 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3105 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
3106 SmallVector<SDOperand, 3> Ops;
3107 Ops.push_back(DAG.getEntryNode());
3108 Ops.push_back(CPIdx);
3109 Ops.push_back(DAG.getSrcValue(NULL));
3110 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3111 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3114 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3115 MVT::ValueType VT = Op.getValueType();
3116 const Type *OpNTy = MVT::getTypeForValueType(VT);
3117 std::vector<Constant*> CV;
3118 if (VT == MVT::f64) {
3119 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3120 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3122 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3123 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3124 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3125 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3127 Constant *CS = ConstantStruct::get(CV);
3128 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3129 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
3130 SmallVector<SDOperand, 3> Ops;
3131 Ops.push_back(DAG.getEntryNode());
3132 Ops.push_back(CPIdx);
3133 Ops.push_back(DAG.getSrcValue(NULL));
3134 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3135 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3138 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3139 SDOperand Op0 = Op.getOperand(0);
3140 SDOperand Op1 = Op.getOperand(1);
3141 MVT::ValueType VT = Op.getValueType();
3142 MVT::ValueType SrcVT = Op1.getValueType();
3143 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3145 // If second operand is smaller, extend it first.
3146 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3147 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3151 // First get the sign bit of second operand.
3152 std::vector<Constant*> CV;
3153 if (SrcVT == MVT::f64) {
3154 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3155 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3157 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3158 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3159 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3160 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3162 Constant *CS = ConstantStruct::get(CV);
3163 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3164 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other);
3165 SmallVector<SDOperand, 3> Ops;
3166 Ops.push_back(DAG.getEntryNode());
3167 Ops.push_back(CPIdx);
3168 Ops.push_back(DAG.getSrcValue(NULL));
3169 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3170 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3172 // Shift sign bit right or left if the two operands have different types.
3173 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3174 // Op0 is MVT::f32, Op1 is MVT::f64.
3175 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3176 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3177 DAG.getConstant(32, MVT::i32));
3178 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3179 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3180 DAG.getConstant(0, getPointerTy()));
3183 // Clear first operand sign bit.
3185 if (VT == MVT::f64) {
3186 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3187 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3189 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3190 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3191 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3192 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3194 CS = ConstantStruct::get(CV);
3195 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3196 Tys = DAG.getVTList(VT, MVT::Other);
3198 Ops.push_back(DAG.getEntryNode());
3199 Ops.push_back(CPIdx);
3200 Ops.push_back(DAG.getSrcValue(NULL));
3201 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3202 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3204 // Or the value with the sign bit.
3205 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3208 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3210 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3212 SDOperand Op0 = Op.getOperand(0);
3213 SDOperand Op1 = Op.getOperand(1);
3214 SDOperand CC = Op.getOperand(2);
3215 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3216 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3217 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3218 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3221 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3223 SDOperand Ops1[] = { Chain, Op0, Op1 };
3224 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3225 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3226 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3229 assert(isFP && "Illegal integer SetCC!");
3231 SDOperand COps[] = { Chain, Op0, Op1 };
3232 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3234 switch (SetCCOpcode) {
3235 default: assert(false && "Illegal floating point SetCC!");
3236 case ISD::SETOEQ: { // !PF & ZF
3237 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3238 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3239 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3241 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3242 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3244 case ISD::SETUNE: { // PF | !ZF
3245 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3246 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3247 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3249 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3250 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3255 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3256 bool addTest = true;
3257 SDOperand Chain = DAG.getEntryNode();
3258 SDOperand Cond = Op.getOperand(0);
3260 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3262 if (Cond.getOpcode() == ISD::SETCC)
3263 Cond = LowerSETCC(Cond, DAG, Chain);
3265 if (Cond.getOpcode() == X86ISD::SETCC) {
3266 CC = Cond.getOperand(0);
3268 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3269 // (since flag operand cannot be shared). Use it as the condition setting
3270 // operand in place of the X86ISD::SETCC.
3271 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3272 // to use a test instead of duplicating the X86ISD::CMP (for register
3273 // pressure reason)?
3274 SDOperand Cmp = Cond.getOperand(1);
3275 unsigned Opc = Cmp.getOpcode();
3276 bool IllegalFPCMov = !X86ScalarSSE &&
3277 MVT::isFloatingPoint(Op.getValueType()) &&
3278 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3279 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3281 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3282 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3288 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3289 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3290 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3293 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3294 SmallVector<SDOperand, 4> Ops;
3295 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3296 // condition is true.
3297 Ops.push_back(Op.getOperand(2));
3298 Ops.push_back(Op.getOperand(1));
3300 Ops.push_back(Cond.getValue(1));
3301 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3304 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3305 bool addTest = true;
3306 SDOperand Chain = Op.getOperand(0);
3307 SDOperand Cond = Op.getOperand(1);
3308 SDOperand Dest = Op.getOperand(2);
3310 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3312 if (Cond.getOpcode() == ISD::SETCC)
3313 Cond = LowerSETCC(Cond, DAG, Chain);
3315 if (Cond.getOpcode() == X86ISD::SETCC) {
3316 CC = Cond.getOperand(0);
3318 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3319 // (since flag operand cannot be shared). Use it as the condition setting
3320 // operand in place of the X86ISD::SETCC.
3321 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3322 // to use a test instead of duplicating the X86ISD::CMP (for register
3323 // pressure reason)?
3324 SDOperand Cmp = Cond.getOperand(1);
3325 unsigned Opc = Cmp.getOpcode();
3326 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3327 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3328 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3334 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3335 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3336 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3338 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3339 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3342 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3343 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3345 if (Subtarget->is64Bit())
3346 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3348 switch (CallingConv) {
3350 assert(0 && "Unsupported calling convention");
3351 case CallingConv::Fast:
3352 // TODO: Implement fastcc
3354 case CallingConv::C:
3355 case CallingConv::X86_StdCall:
3356 return LowerCCCCallTo(Op, DAG, CallingConv);
3357 case CallingConv::X86_FastCall:
3358 return LowerFastCCCallTo(Op, DAG, CallingConv);
3363 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3364 MachineFunction &MF = DAG.getMachineFunction();
3365 const Function* Fn = MF.getFunction();
3366 if (Fn->hasExternalLinkage() &&
3367 Subtarget->isTargetCygMing() &&
3368 Fn->getName() == "main")
3369 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3371 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3372 if (Subtarget->is64Bit())
3373 return LowerX86_64CCCArguments(Op, DAG);
3377 assert(0 && "Unsupported calling convention");
3378 case CallingConv::Fast:
3379 // TODO: implement fastcc.
3382 case CallingConv::C:
3383 return LowerCCCArguments(Op, DAG);
3384 case CallingConv::X86_StdCall:
3385 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
3386 return LowerCCCArguments(Op, DAG, true);
3387 case CallingConv::X86_FastCall:
3388 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
3389 return LowerFastCCArguments(Op, DAG);
3393 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3394 SDOperand InFlag(0, 0);
3395 SDOperand Chain = Op.getOperand(0);
3397 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3398 if (Align == 0) Align = 1;
3400 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3401 // If not DWORD aligned, call memset if size is less than the threshold.
3402 // It knows how to align to the right boundary first.
3403 if ((Align & 3) != 0 ||
3404 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3405 MVT::ValueType IntPtr = getPointerTy();
3406 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3407 TargetLowering::ArgListTy Args;
3408 TargetLowering::ArgListEntry Entry;
3409 Entry.Node = Op.getOperand(1);
3410 Entry.Ty = IntPtrTy;
3411 Entry.isSigned = false;
3412 Entry.isInReg = false;
3413 Entry.isSRet = false;
3414 Args.push_back(Entry);
3415 // Extend the unsigned i8 argument to be an int value for the call.
3416 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3417 Entry.Ty = IntPtrTy;
3418 Entry.isSigned = false;
3419 Entry.isInReg = false;
3420 Entry.isSRet = false;
3421 Args.push_back(Entry);
3422 Entry.Node = Op.getOperand(3);
3423 Args.push_back(Entry);
3424 std::pair<SDOperand,SDOperand> CallResult =
3425 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3426 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3427 return CallResult.second;
3432 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3433 unsigned BytesLeft = 0;
3434 bool TwoRepStos = false;
3437 uint64_t Val = ValC->getValue() & 255;
3439 // If the value is a constant, then we can potentially use larger sets.
3440 switch (Align & 3) {
3441 case 2: // WORD aligned
3444 Val = (Val << 8) | Val;
3446 case 0: // DWORD aligned
3449 Val = (Val << 8) | Val;
3450 Val = (Val << 16) | Val;
3451 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3454 Val = (Val << 32) | Val;
3457 default: // Byte aligned
3460 Count = Op.getOperand(3);
3464 if (AVT > MVT::i8) {
3466 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3467 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3468 BytesLeft = I->getValue() % UBytes;
3470 assert(AVT >= MVT::i32 &&
3471 "Do not use rep;stos if not at least DWORD aligned");
3472 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3473 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3478 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3480 InFlag = Chain.getValue(1);
3483 Count = Op.getOperand(3);
3484 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3485 InFlag = Chain.getValue(1);
3488 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3490 InFlag = Chain.getValue(1);
3491 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3492 Op.getOperand(1), InFlag);
3493 InFlag = Chain.getValue(1);
3495 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3496 SmallVector<SDOperand, 8> Ops;
3497 Ops.push_back(Chain);
3498 Ops.push_back(DAG.getValueType(AVT));
3499 Ops.push_back(InFlag);
3500 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3503 InFlag = Chain.getValue(1);
3504 Count = Op.getOperand(3);
3505 MVT::ValueType CVT = Count.getValueType();
3506 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3507 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3508 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3510 InFlag = Chain.getValue(1);
3511 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3513 Ops.push_back(Chain);
3514 Ops.push_back(DAG.getValueType(MVT::i8));
3515 Ops.push_back(InFlag);
3516 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3517 } else if (BytesLeft) {
3518 // Issue stores for the last 1 - 7 bytes.
3520 unsigned Val = ValC->getValue() & 255;
3521 unsigned Offset = I->getValue() - BytesLeft;
3522 SDOperand DstAddr = Op.getOperand(1);
3523 MVT::ValueType AddrVT = DstAddr.getValueType();
3524 if (BytesLeft >= 4) {
3525 Val = (Val << 8) | Val;
3526 Val = (Val << 16) | Val;
3527 Value = DAG.getConstant(Val, MVT::i32);
3528 Chain = DAG.getStore(Chain, Value,
3529 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3530 DAG.getConstant(Offset, AddrVT)),
3535 if (BytesLeft >= 2) {
3536 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3537 Chain = DAG.getStore(Chain, Value,
3538 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3539 DAG.getConstant(Offset, AddrVT)),
3544 if (BytesLeft == 1) {
3545 Value = DAG.getConstant(Val, MVT::i8);
3546 Chain = DAG.getStore(Chain, Value,
3547 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3548 DAG.getConstant(Offset, AddrVT)),
3556 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3557 SDOperand Chain = Op.getOperand(0);
3559 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3560 if (Align == 0) Align = 1;
3562 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3563 // If not DWORD aligned, call memcpy if size is less than the threshold.
3564 // It knows how to align to the right boundary first.
3565 if ((Align & 3) != 0 ||
3566 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3567 MVT::ValueType IntPtr = getPointerTy();
3568 TargetLowering::ArgListTy Args;
3569 TargetLowering::ArgListEntry Entry;
3570 Entry.Ty = getTargetData()->getIntPtrType();
3571 Entry.isSigned = false;
3572 Entry.isInReg = false;
3573 Entry.isSRet = false;
3574 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3575 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3576 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
3577 std::pair<SDOperand,SDOperand> CallResult =
3578 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3579 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3580 return CallResult.second;
3585 unsigned BytesLeft = 0;
3586 bool TwoRepMovs = false;
3587 switch (Align & 3) {
3588 case 2: // WORD aligned
3591 case 0: // DWORD aligned
3593 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3596 default: // Byte aligned
3598 Count = Op.getOperand(3);
3602 if (AVT > MVT::i8) {
3604 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3605 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3606 BytesLeft = I->getValue() % UBytes;
3608 assert(AVT >= MVT::i32 &&
3609 "Do not use rep;movs if not at least DWORD aligned");
3610 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3611 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3616 SDOperand InFlag(0, 0);
3617 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3619 InFlag = Chain.getValue(1);
3620 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3621 Op.getOperand(1), InFlag);
3622 InFlag = Chain.getValue(1);
3623 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3624 Op.getOperand(2), InFlag);
3625 InFlag = Chain.getValue(1);
3627 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3628 SmallVector<SDOperand, 8> Ops;
3629 Ops.push_back(Chain);
3630 Ops.push_back(DAG.getValueType(AVT));
3631 Ops.push_back(InFlag);
3632 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3635 InFlag = Chain.getValue(1);
3636 Count = Op.getOperand(3);
3637 MVT::ValueType CVT = Count.getValueType();
3638 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3639 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3640 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3642 InFlag = Chain.getValue(1);
3643 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3645 Ops.push_back(Chain);
3646 Ops.push_back(DAG.getValueType(MVT::i8));
3647 Ops.push_back(InFlag);
3648 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3649 } else if (BytesLeft) {
3650 // Issue loads and stores for the last 1 - 7 bytes.
3651 unsigned Offset = I->getValue() - BytesLeft;
3652 SDOperand DstAddr = Op.getOperand(1);
3653 MVT::ValueType DstVT = DstAddr.getValueType();
3654 SDOperand SrcAddr = Op.getOperand(2);
3655 MVT::ValueType SrcVT = SrcAddr.getValueType();
3657 if (BytesLeft >= 4) {
3658 Value = DAG.getLoad(MVT::i32, Chain,
3659 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3660 DAG.getConstant(Offset, SrcVT)),
3662 Chain = Value.getValue(1);
3663 Chain = DAG.getStore(Chain, Value,
3664 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3665 DAG.getConstant(Offset, DstVT)),
3670 if (BytesLeft >= 2) {
3671 Value = DAG.getLoad(MVT::i16, Chain,
3672 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3673 DAG.getConstant(Offset, SrcVT)),
3675 Chain = Value.getValue(1);
3676 Chain = DAG.getStore(Chain, Value,
3677 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3678 DAG.getConstant(Offset, DstVT)),
3684 if (BytesLeft == 1) {
3685 Value = DAG.getLoad(MVT::i8, Chain,
3686 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3687 DAG.getConstant(Offset, SrcVT)),
3689 Chain = Value.getValue(1);
3690 Chain = DAG.getStore(Chain, Value,
3691 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3692 DAG.getConstant(Offset, DstVT)),
3701 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3702 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3703 SDOperand TheOp = Op.getOperand(0);
3704 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
3705 if (Subtarget->is64Bit()) {
3706 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
3707 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
3708 MVT::i64, Copy1.getValue(2));
3709 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
3710 DAG.getConstant(32, MVT::i8));
3712 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
3715 Tys = DAG.getVTList(MVT::i64, MVT::Other);
3716 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
3719 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
3720 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
3721 MVT::i32, Copy1.getValue(2));
3722 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
3723 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
3724 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
3727 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3728 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
3730 if (!Subtarget->is64Bit()) {
3731 // vastart just stores the address of the VarArgsFrameIndex slot into the
3732 // memory location argument.
3733 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
3734 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
3739 // gp_offset (0 - 6 * 8)
3740 // fp_offset (48 - 48 + 8 * 16)
3741 // overflow_arg_area (point to parameters coming in memory).
3743 SmallVector<SDOperand, 8> MemOps;
3744 SDOperand FIN = Op.getOperand(1);
3746 SDOperand Store = DAG.getStore(Op.getOperand(0),
3747 DAG.getConstant(VarArgsGPOffset, MVT::i32),
3748 FIN, SV->getValue(), SV->getOffset());
3749 MemOps.push_back(Store);
3752 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3753 DAG.getConstant(4, getPointerTy()));
3754 Store = DAG.getStore(Op.getOperand(0),
3755 DAG.getConstant(VarArgsFPOffset, MVT::i32),
3756 FIN, SV->getValue(), SV->getOffset());
3757 MemOps.push_back(Store);
3759 // Store ptr to overflow_arg_area
3760 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3761 DAG.getConstant(4, getPointerTy()));
3762 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
3763 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
3765 MemOps.push_back(Store);
3767 // Store ptr to reg_save_area.
3768 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3769 DAG.getConstant(8, getPointerTy()));
3770 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
3771 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
3773 MemOps.push_back(Store);
3774 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
3777 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
3778 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
3779 SDOperand Chain = Op.getOperand(0);
3780 SDOperand DstPtr = Op.getOperand(1);
3781 SDOperand SrcPtr = Op.getOperand(2);
3782 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
3783 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
3785 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
3786 SrcSV->getValue(), SrcSV->getOffset());
3787 Chain = SrcPtr.getValue(1);
3788 for (unsigned i = 0; i < 3; ++i) {
3789 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
3790 SrcSV->getValue(), SrcSV->getOffset());
3791 Chain = Val.getValue(1);
3792 Chain = DAG.getStore(Chain, Val, DstPtr,
3793 DstSV->getValue(), DstSV->getOffset());
3796 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
3797 DAG.getConstant(8, getPointerTy()));
3798 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
3799 DAG.getConstant(8, getPointerTy()));
3805 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3806 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3808 default: return SDOperand(); // Don't custom lower most intrinsics.
3809 // Comparison intrinsics.
3810 case Intrinsic::x86_sse_comieq_ss:
3811 case Intrinsic::x86_sse_comilt_ss:
3812 case Intrinsic::x86_sse_comile_ss:
3813 case Intrinsic::x86_sse_comigt_ss:
3814 case Intrinsic::x86_sse_comige_ss:
3815 case Intrinsic::x86_sse_comineq_ss:
3816 case Intrinsic::x86_sse_ucomieq_ss:
3817 case Intrinsic::x86_sse_ucomilt_ss:
3818 case Intrinsic::x86_sse_ucomile_ss:
3819 case Intrinsic::x86_sse_ucomigt_ss:
3820 case Intrinsic::x86_sse_ucomige_ss:
3821 case Intrinsic::x86_sse_ucomineq_ss:
3822 case Intrinsic::x86_sse2_comieq_sd:
3823 case Intrinsic::x86_sse2_comilt_sd:
3824 case Intrinsic::x86_sse2_comile_sd:
3825 case Intrinsic::x86_sse2_comigt_sd:
3826 case Intrinsic::x86_sse2_comige_sd:
3827 case Intrinsic::x86_sse2_comineq_sd:
3828 case Intrinsic::x86_sse2_ucomieq_sd:
3829 case Intrinsic::x86_sse2_ucomilt_sd:
3830 case Intrinsic::x86_sse2_ucomile_sd:
3831 case Intrinsic::x86_sse2_ucomigt_sd:
3832 case Intrinsic::x86_sse2_ucomige_sd:
3833 case Intrinsic::x86_sse2_ucomineq_sd: {
3835 ISD::CondCode CC = ISD::SETCC_INVALID;
3838 case Intrinsic::x86_sse_comieq_ss:
3839 case Intrinsic::x86_sse2_comieq_sd:
3843 case Intrinsic::x86_sse_comilt_ss:
3844 case Intrinsic::x86_sse2_comilt_sd:
3848 case Intrinsic::x86_sse_comile_ss:
3849 case Intrinsic::x86_sse2_comile_sd:
3853 case Intrinsic::x86_sse_comigt_ss:
3854 case Intrinsic::x86_sse2_comigt_sd:
3858 case Intrinsic::x86_sse_comige_ss:
3859 case Intrinsic::x86_sse2_comige_sd:
3863 case Intrinsic::x86_sse_comineq_ss:
3864 case Intrinsic::x86_sse2_comineq_sd:
3868 case Intrinsic::x86_sse_ucomieq_ss:
3869 case Intrinsic::x86_sse2_ucomieq_sd:
3870 Opc = X86ISD::UCOMI;
3873 case Intrinsic::x86_sse_ucomilt_ss:
3874 case Intrinsic::x86_sse2_ucomilt_sd:
3875 Opc = X86ISD::UCOMI;
3878 case Intrinsic::x86_sse_ucomile_ss:
3879 case Intrinsic::x86_sse2_ucomile_sd:
3880 Opc = X86ISD::UCOMI;
3883 case Intrinsic::x86_sse_ucomigt_ss:
3884 case Intrinsic::x86_sse2_ucomigt_sd:
3885 Opc = X86ISD::UCOMI;
3888 case Intrinsic::x86_sse_ucomige_ss:
3889 case Intrinsic::x86_sse2_ucomige_sd:
3890 Opc = X86ISD::UCOMI;
3893 case Intrinsic::x86_sse_ucomineq_ss:
3894 case Intrinsic::x86_sse2_ucomineq_sd:
3895 Opc = X86ISD::UCOMI;
3901 SDOperand LHS = Op.getOperand(1);
3902 SDOperand RHS = Op.getOperand(2);
3903 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
3905 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3906 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
3907 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
3908 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3909 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3910 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
3911 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
3916 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
3917 // Depths > 0 not supported yet!
3918 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3921 // Just load the return address
3922 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
3923 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
3926 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
3927 // Depths > 0 not supported yet!
3928 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3931 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
3932 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
3933 DAG.getConstant(4, getPointerTy()));
3936 /// LowerOperation - Provide custom lowering hooks for some operations.
3938 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3939 switch (Op.getOpcode()) {
3940 default: assert(0 && "Should not custom lower this!");
3941 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3942 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3943 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3944 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3945 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3946 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3947 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3948 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3949 case ISD::SHL_PARTS:
3950 case ISD::SRA_PARTS:
3951 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3952 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3953 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3954 case ISD::FABS: return LowerFABS(Op, DAG);
3955 case ISD::FNEG: return LowerFNEG(Op, DAG);
3956 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
3957 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
3958 case ISD::SELECT: return LowerSELECT(Op, DAG);
3959 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3960 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3961 case ISD::CALL: return LowerCALL(Op, DAG);
3962 case ISD::RET: return LowerRET(Op, DAG);
3963 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
3964 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3965 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3966 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3967 case ISD::VASTART: return LowerVASTART(Op, DAG);
3968 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
3969 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3970 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
3971 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
3976 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3978 default: return NULL;
3979 case X86ISD::SHLD: return "X86ISD::SHLD";
3980 case X86ISD::SHRD: return "X86ISD::SHRD";
3981 case X86ISD::FAND: return "X86ISD::FAND";
3982 case X86ISD::FOR: return "X86ISD::FOR";
3983 case X86ISD::FXOR: return "X86ISD::FXOR";
3984 case X86ISD::FSRL: return "X86ISD::FSRL";
3985 case X86ISD::FILD: return "X86ISD::FILD";
3986 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
3987 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3988 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3989 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
3990 case X86ISD::FLD: return "X86ISD::FLD";
3991 case X86ISD::FST: return "X86ISD::FST";
3992 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
3993 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
3994 case X86ISD::CALL: return "X86ISD::CALL";
3995 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3996 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3997 case X86ISD::CMP: return "X86ISD::CMP";
3998 case X86ISD::COMI: return "X86ISD::COMI";
3999 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4000 case X86ISD::SETCC: return "X86ISD::SETCC";
4001 case X86ISD::CMOV: return "X86ISD::CMOV";
4002 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4003 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4004 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4005 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4006 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
4007 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
4008 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4009 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4010 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4011 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4012 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4013 case X86ISD::FMAX: return "X86ISD::FMAX";
4014 case X86ISD::FMIN: return "X86ISD::FMIN";
4018 /// isLegalAddressImmediate - Return true if the integer value or
4019 /// GlobalValue can be used as the offset of the target addressing mode.
4020 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
4021 // X86 allows a sign-extended 32-bit immediate field.
4022 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4025 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
4026 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement
4027 // field unless we are in small code model.
4028 if (Subtarget->is64Bit() &&
4029 getTargetMachine().getCodeModel() != CodeModel::Small)
4032 return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false));
4035 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4036 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4037 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4038 /// are assumed to be legal.
4040 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4041 // Only do shuffles on 128-bit vector types for now.
4042 if (MVT::getSizeInBits(VT) == 64) return false;
4043 return (Mask.Val->getNumOperands() <= 4 ||
4044 isSplatMask(Mask.Val) ||
4045 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4046 X86::isUNPCKLMask(Mask.Val) ||
4047 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4048 X86::isUNPCKHMask(Mask.Val));
4051 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4053 SelectionDAG &DAG) const {
4054 unsigned NumElts = BVOps.size();
4055 // Only do shuffles on 128-bit vector types for now.
4056 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4057 if (NumElts == 2) return true;
4059 return (isMOVLMask(&BVOps[0], 4) ||
4060 isCommutedMOVL(&BVOps[0], 4, true) ||
4061 isSHUFPMask(&BVOps[0], 4) ||
4062 isCommutedSHUFP(&BVOps[0], 4));
4067 //===----------------------------------------------------------------------===//
4068 // X86 Scheduler Hooks
4069 //===----------------------------------------------------------------------===//
4072 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4073 MachineBasicBlock *BB) {
4074 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4075 switch (MI->getOpcode()) {
4076 default: assert(false && "Unexpected instr type to insert");
4077 case X86::CMOV_FR32:
4078 case X86::CMOV_FR64:
4079 case X86::CMOV_V4F32:
4080 case X86::CMOV_V2F64:
4081 case X86::CMOV_V2I64: {
4082 // To "insert" a SELECT_CC instruction, we actually have to insert the
4083 // diamond control-flow pattern. The incoming instruction knows the
4084 // destination vreg to set, the condition code register to branch on, the
4085 // true/false values to select between, and a branch opcode to use.
4086 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4087 ilist<MachineBasicBlock>::iterator It = BB;
4093 // cmpTY ccX, r1, r2
4095 // fallthrough --> copy0MBB
4096 MachineBasicBlock *thisMBB = BB;
4097 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4098 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4100 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4101 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4102 MachineFunction *F = BB->getParent();
4103 F->getBasicBlockList().insert(It, copy0MBB);
4104 F->getBasicBlockList().insert(It, sinkMBB);
4105 // Update machine-CFG edges by first adding all successors of the current
4106 // block to the new block which will contain the Phi node for the select.
4107 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4108 e = BB->succ_end(); i != e; ++i)
4109 sinkMBB->addSuccessor(*i);
4110 // Next, remove all successors of the current block, and add the true
4111 // and fallthrough blocks as its successors.
4112 while(!BB->succ_empty())
4113 BB->removeSuccessor(BB->succ_begin());
4114 BB->addSuccessor(copy0MBB);
4115 BB->addSuccessor(sinkMBB);
4118 // %FalseValue = ...
4119 // # fallthrough to sinkMBB
4122 // Update machine-CFG edges
4123 BB->addSuccessor(sinkMBB);
4126 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4129 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4130 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4131 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4133 delete MI; // The pseudo instruction is gone now.
4137 case X86::FP_TO_INT16_IN_MEM:
4138 case X86::FP_TO_INT32_IN_MEM:
4139 case X86::FP_TO_INT64_IN_MEM: {
4140 // Change the floating point control register to use "round towards zero"
4141 // mode when truncating to an integer value.
4142 MachineFunction *F = BB->getParent();
4143 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4144 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4146 // Load the old value of the high byte of the control word...
4148 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4149 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4151 // Set the high part to be round to zero...
4152 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4155 // Reload the modified control word now...
4156 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4158 // Restore the memory image of control word to original value
4159 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4162 // Get the X86 opcode to use.
4164 switch (MI->getOpcode()) {
4165 default: assert(0 && "illegal opcode!");
4166 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4167 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4168 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4172 MachineOperand &Op = MI->getOperand(0);
4173 if (Op.isRegister()) {
4174 AM.BaseType = X86AddressMode::RegBase;
4175 AM.Base.Reg = Op.getReg();
4177 AM.BaseType = X86AddressMode::FrameIndexBase;
4178 AM.Base.FrameIndex = Op.getFrameIndex();
4180 Op = MI->getOperand(1);
4181 if (Op.isImmediate())
4182 AM.Scale = Op.getImm();
4183 Op = MI->getOperand(2);
4184 if (Op.isImmediate())
4185 AM.IndexReg = Op.getImm();
4186 Op = MI->getOperand(3);
4187 if (Op.isGlobalAddress()) {
4188 AM.GV = Op.getGlobal();
4190 AM.Disp = Op.getImm();
4192 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4193 .addReg(MI->getOperand(4).getReg());
4195 // Reload the original control word now.
4196 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4198 delete MI; // The pseudo instruction is gone now.
4204 //===----------------------------------------------------------------------===//
4205 // X86 Optimization Hooks
4206 //===----------------------------------------------------------------------===//
4208 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4210 uint64_t &KnownZero,
4212 unsigned Depth) const {
4213 unsigned Opc = Op.getOpcode();
4214 assert((Opc >= ISD::BUILTIN_OP_END ||
4215 Opc == ISD::INTRINSIC_WO_CHAIN ||
4216 Opc == ISD::INTRINSIC_W_CHAIN ||
4217 Opc == ISD::INTRINSIC_VOID) &&
4218 "Should use MaskedValueIsZero if you don't know whether Op"
4219 " is a target node!");
4221 KnownZero = KnownOne = 0; // Don't know anything.
4225 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4230 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
4231 /// element of the result of the vector shuffle.
4232 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4233 MVT::ValueType VT = N->getValueType(0);
4234 SDOperand PermMask = N->getOperand(2);
4235 unsigned NumElems = PermMask.getNumOperands();
4236 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4238 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4240 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4241 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4242 SDOperand Idx = PermMask.getOperand(i);
4243 if (Idx.getOpcode() == ISD::UNDEF)
4244 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4245 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4250 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4251 /// node is a GlobalAddress + an offset.
4252 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4253 unsigned Opc = N->getOpcode();
4254 if (Opc == X86ISD::Wrapper) {
4255 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4256 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4259 } else if (Opc == ISD::ADD) {
4260 SDOperand N1 = N->getOperand(0);
4261 SDOperand N2 = N->getOperand(1);
4262 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4263 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4265 Offset += V->getSignExtended();
4268 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4269 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4271 Offset += V->getSignExtended();
4279 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4281 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4282 MachineFrameInfo *MFI) {
4283 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4286 SDOperand Loc = N->getOperand(1);
4287 SDOperand BaseLoc = Base->getOperand(1);
4288 if (Loc.getOpcode() == ISD::FrameIndex) {
4289 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4291 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4292 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4293 int FS = MFI->getObjectSize(FI);
4294 int BFS = MFI->getObjectSize(BFI);
4295 if (FS != BFS || FS != Size) return false;
4296 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4298 GlobalValue *GV1 = NULL;
4299 GlobalValue *GV2 = NULL;
4300 int64_t Offset1 = 0;
4301 int64_t Offset2 = 0;
4302 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4303 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4304 if (isGA1 && isGA2 && GV1 == GV2)
4305 return Offset1 == (Offset2 + Dist*Size);
4311 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4312 const X86Subtarget *Subtarget) {
4315 if (isGAPlusOffset(Base, GV, Offset))
4316 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4318 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4319 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
4321 // Fixed objects do not specify alignment, however the offsets are known.
4322 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4323 (MFI->getObjectOffset(BFI) % 16) == 0);
4325 return MFI->getObjectAlignment(BFI) >= 16;
4331 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4332 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4333 /// if the load addresses are consecutive, non-overlapping, and in the right
4335 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4336 const X86Subtarget *Subtarget) {
4337 MachineFunction &MF = DAG.getMachineFunction();
4338 MachineFrameInfo *MFI = MF.getFrameInfo();
4339 MVT::ValueType VT = N->getValueType(0);
4340 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4341 SDOperand PermMask = N->getOperand(2);
4342 int NumElems = (int)PermMask.getNumOperands();
4343 SDNode *Base = NULL;
4344 for (int i = 0; i < NumElems; ++i) {
4345 SDOperand Idx = PermMask.getOperand(i);
4346 if (Idx.getOpcode() == ISD::UNDEF) {
4347 if (!Base) return SDOperand();
4350 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4351 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
4355 else if (!isConsecutiveLoad(Arg.Val, Base,
4356 i, MVT::getSizeInBits(EVT)/8,MFI))
4361 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
4363 LoadSDNode *LD = cast<LoadSDNode>(Base);
4364 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4365 LD->getSrcValueOffset());
4367 // Just use movups, it's shorter.
4368 SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other);
4369 SmallVector<SDOperand, 3> Ops;
4370 Ops.push_back(Base->getOperand(0));
4371 Ops.push_back(Base->getOperand(1));
4372 Ops.push_back(Base->getOperand(2));
4373 return DAG.getNode(ISD::BIT_CONVERT, VT,
4374 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
4378 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4379 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4380 const X86Subtarget *Subtarget) {
4381 SDOperand Cond = N->getOperand(0);
4383 // If we have SSE[12] support, try to form min/max nodes.
4384 if (Subtarget->hasSSE2() &&
4385 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4386 if (Cond.getOpcode() == ISD::SETCC) {
4387 // Get the LHS/RHS of the select.
4388 SDOperand LHS = N->getOperand(1);
4389 SDOperand RHS = N->getOperand(2);
4390 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
4392 unsigned Opcode = 0;
4393 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
4396 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4399 if (!UnsafeFPMath) break;
4401 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4403 Opcode = X86ISD::FMIN;
4406 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4409 if (!UnsafeFPMath) break;
4411 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4413 Opcode = X86ISD::FMAX;
4416 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
4419 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4422 if (!UnsafeFPMath) break;
4424 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4426 Opcode = X86ISD::FMIN;
4429 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4432 if (!UnsafeFPMath) break;
4434 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4436 Opcode = X86ISD::FMAX;
4442 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
4451 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4452 DAGCombinerInfo &DCI) const {
4453 SelectionDAG &DAG = DCI.DAG;
4454 switch (N->getOpcode()) {
4456 case ISD::VECTOR_SHUFFLE:
4457 return PerformShuffleCombine(N, DAG, Subtarget);
4459 return PerformSELECTCombine(N, DAG, Subtarget);
4465 //===----------------------------------------------------------------------===//
4466 // X86 Inline Assembly Support
4467 //===----------------------------------------------------------------------===//
4469 /// getConstraintType - Given a constraint letter, return the type of
4470 /// constraint it is for this target.
4471 X86TargetLowering::ConstraintType
4472 X86TargetLowering::getConstraintType(char ConstraintLetter) const {
4473 switch (ConstraintLetter) {
4482 return C_RegisterClass;
4483 default: return TargetLowering::getConstraintType(ConstraintLetter);
4487 /// isOperandValidForConstraint - Return the specified operand (possibly
4488 /// modified) if the specified SDOperand is valid for the specified target
4489 /// constraint letter, otherwise return null.
4490 SDOperand X86TargetLowering::
4491 isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4492 switch (Constraint) {
4495 // Literal immediates are always ok.
4496 if (isa<ConstantSDNode>(Op)) return Op;
4498 // If we are in non-pic codegen mode, we allow the address of a global to
4499 // be used with 'i'.
4500 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
4501 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4502 return SDOperand(0, 0);
4504 if (GA->getOpcode() != ISD::TargetGlobalAddress)
4505 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4510 // Otherwise, not valid for this mode.
4511 return SDOperand(0, 0);
4513 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
4517 std::vector<unsigned> X86TargetLowering::
4518 getRegClassForInlineAsmConstraint(const std::string &Constraint,
4519 MVT::ValueType VT) const {
4520 if (Constraint.size() == 1) {
4521 // FIXME: not handling fp-stack yet!
4522 // FIXME: not handling MMX registers yet ('y' constraint).
4523 switch (Constraint[0]) { // GCC X86 Constraint Letters
4524 default: break; // Unknown constraint letter
4525 case 'A': // EAX/EDX
4526 if (VT == MVT::i32 || VT == MVT::i64)
4527 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4529 case 'r': // GENERAL_REGS
4530 case 'R': // LEGACY_REGS
4531 if (VT == MVT::i64 && Subtarget->is64Bit())
4532 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
4533 X86::RSI, X86::RDI, X86::RBP, X86::RSP,
4534 X86::R8, X86::R9, X86::R10, X86::R11,
4535 X86::R12, X86::R13, X86::R14, X86::R15, 0);
4537 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4538 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
4539 else if (VT == MVT::i16)
4540 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4541 X86::SI, X86::DI, X86::BP, X86::SP, 0);
4542 else if (VT == MVT::i8)
4543 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
4545 case 'l': // INDEX_REGS
4547 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
4548 X86::ESI, X86::EDI, X86::EBP, 0);
4549 else if (VT == MVT::i16)
4550 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
4551 X86::SI, X86::DI, X86::BP, 0);
4552 else if (VT == MVT::i8)
4553 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4555 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4558 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4559 else if (VT == MVT::i16)
4560 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4561 else if (VT == MVT::i8)
4562 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4564 case 'x': // SSE_REGS if SSE1 allowed
4565 if (Subtarget->hasSSE1())
4566 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4567 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4569 return std::vector<unsigned>();
4570 case 'Y': // SSE_REGS if SSE2 allowed
4571 if (Subtarget->hasSSE2())
4572 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
4573 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
4575 return std::vector<unsigned>();
4579 return std::vector<unsigned>();
4582 std::pair<unsigned, const TargetRegisterClass*>
4583 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4584 MVT::ValueType VT) const {
4585 // Use the default implementation in TargetLowering to convert the register
4586 // constraint into a member of a register class.
4587 std::pair<unsigned, const TargetRegisterClass*> Res;
4588 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4590 // Not found as a standard register?
4591 if (Res.second == 0) {
4592 // GCC calls "st(0)" just plain "st".
4593 if (StringsEqualNoCase("{st}", Constraint)) {
4594 Res.first = X86::ST0;
4595 Res.second = X86::RSTRegisterClass;
4601 // Otherwise, check to see if this is a register class of the wrong value
4602 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4603 // turn into {ax},{dx}.
4604 if (Res.second->hasType(VT))
4605 return Res; // Correct type already, nothing to do.
4607 // All of the single-register GCC register classes map their values onto
4608 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4609 // really want an 8-bit or 32-bit register, map to the appropriate register
4610 // class and return the appropriate register.
4611 if (Res.second != X86::GR16RegisterClass)
4614 if (VT == MVT::i8) {
4615 unsigned DestReg = 0;
4616 switch (Res.first) {
4618 case X86::AX: DestReg = X86::AL; break;
4619 case X86::DX: DestReg = X86::DL; break;
4620 case X86::CX: DestReg = X86::CL; break;
4621 case X86::BX: DestReg = X86::BL; break;
4624 Res.first = DestReg;
4625 Res.second = Res.second = X86::GR8RegisterClass;
4627 } else if (VT == MVT::i32) {
4628 unsigned DestReg = 0;
4629 switch (Res.first) {
4631 case X86::AX: DestReg = X86::EAX; break;
4632 case X86::DX: DestReg = X86::EDX; break;
4633 case X86::CX: DestReg = X86::ECX; break;
4634 case X86::BX: DestReg = X86::EBX; break;
4635 case X86::SI: DestReg = X86::ESI; break;
4636 case X86::DI: DestReg = X86::EDI; break;
4637 case X86::BP: DestReg = X86::EBP; break;
4638 case X86::SP: DestReg = X86::ESP; break;
4641 Res.first = DestReg;
4642 Res.second = Res.second = X86::GR32RegisterClass;
4644 } else if (VT == MVT::i64) {
4645 unsigned DestReg = 0;
4646 switch (Res.first) {
4648 case X86::AX: DestReg = X86::RAX; break;
4649 case X86::DX: DestReg = X86::RDX; break;
4650 case X86::CX: DestReg = X86::RCX; break;
4651 case X86::BX: DestReg = X86::RBX; break;
4652 case X86::SI: DestReg = X86::RSI; break;
4653 case X86::DI: DestReg = X86::RDI; break;
4654 case X86::BP: DestReg = X86::RBP; break;
4655 case X86::SP: DestReg = X86::RSP; break;
4658 Res.first = DestReg;
4659 Res.second = Res.second = X86::GR64RegisterClass;