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 "X86CodeEmitter.h"
17 #include "X86InstrBuilder.h"
18 #include "X86ISelLowering.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "X86TargetMachine.h"
21 #include "llvm/CallingConv.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/Function.h"
26 #include "llvm/Intrinsics.h"
27 #include "llvm/ADT/VectorExtras.h"
28 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
29 #include "llvm/CodeGen/CallingConvLower.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/SelectionDAG.h"
34 #include "llvm/CodeGen/SSARegMap.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Target/TargetOptions.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/ParameterAttributes.h"
41 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
43 Subtarget = &TM.getSubtarget<X86Subtarget>();
44 X86ScalarSSE = Subtarget->hasSSE2();
45 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
47 RegInfo = TM.getRegisterInfo();
49 // Set up the TargetLowering object.
51 // X86 is weird, it always uses i8 for shift amounts and setcc results.
52 setShiftAmountType(MVT::i8);
53 setSetCCResultType(MVT::i8);
54 setSetCCResultContents(ZeroOrOneSetCCResult);
55 setSchedulingPreference(SchedulingForRegPressure);
56 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
57 setStackPointerRegisterToSaveRestore(X86StackPtr);
59 if (Subtarget->isTargetDarwin()) {
60 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
61 setUseUnderscoreSetJmp(false);
62 setUseUnderscoreLongJmp(false);
63 } else if (Subtarget->isTargetMingw()) {
64 // MS runtime is weird: it exports _setjmp, but longjmp!
65 setUseUnderscoreSetJmp(true);
66 setUseUnderscoreLongJmp(false);
68 setUseUnderscoreSetJmp(true);
69 setUseUnderscoreLongJmp(true);
72 // Set up the register classes.
73 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
74 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
75 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
76 if (Subtarget->is64Bit())
77 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
79 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
81 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
83 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
84 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
85 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
87 if (Subtarget->is64Bit()) {
88 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
89 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
92 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
93 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
95 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
98 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
100 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
101 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
102 // SSE has no i16 to fp conversion, only i32
104 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
106 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
107 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
110 if (!Subtarget->is64Bit()) {
111 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
112 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
113 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
116 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
118 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
119 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
122 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
124 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
125 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
128 // Handle FP_TO_UINT by promoting the destination to a larger signed
130 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
131 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
132 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
134 if (Subtarget->is64Bit()) {
135 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
136 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
138 if (X86ScalarSSE && !Subtarget->hasSSE3())
139 // Expand FP_TO_UINT into a select.
140 // FIXME: We would like to use a Custom expander here eventually to do
141 // the optimal thing for SSE vs. the default expansion in the legalizer.
142 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
144 // With SSE3 we can use fisttpll to convert to a signed i64.
145 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
148 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
150 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
151 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
154 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
155 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
156 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
157 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
158 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
159 if (Subtarget->is64Bit())
160 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
161 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
162 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
163 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
164 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
165 setOperationAction(ISD::FREM , MVT::f64 , Expand);
167 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
168 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
169 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
170 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
171 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
172 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
173 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
174 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
175 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
176 if (Subtarget->is64Bit()) {
177 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
178 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
179 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
182 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
183 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
185 // These should be promoted to a larger select which is supported.
186 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
187 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
188 // X86 wants to expand cmov itself.
189 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
190 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
191 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
192 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
193 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
194 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
195 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
196 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
197 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
198 if (Subtarget->is64Bit()) {
199 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
200 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
202 // X86 ret instruction may pop stack.
203 setOperationAction(ISD::RET , MVT::Other, Custom);
204 if (!Subtarget->is64Bit())
205 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
208 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
209 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
210 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
211 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
212 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
213 if (Subtarget->is64Bit()) {
214 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
215 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
216 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
217 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
219 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
220 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
221 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
222 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
223 // X86 wants to expand memset / memcpy itself.
224 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
225 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
227 // We don't have line number support yet.
228 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
229 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
230 // FIXME - use subtarget debug flags
231 if (!Subtarget->isTargetDarwin() &&
232 !Subtarget->isTargetELF() &&
233 !Subtarget->isTargetCygMing())
234 setOperationAction(ISD::LABEL, MVT::Other, Expand);
236 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
237 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
238 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
239 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
240 if (Subtarget->is64Bit()) {
242 setExceptionPointerRegister(X86::RAX);
243 setExceptionSelectorRegister(X86::RDX);
245 setExceptionPointerRegister(X86::EAX);
246 setExceptionSelectorRegister(X86::EDX);
249 setOperationAction(ISD::ADJUST_TRAMP, MVT::i32, Expand);
250 setOperationAction(ISD::ADJUST_TRAMP, MVT::i64, Expand);
251 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
253 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
254 setOperationAction(ISD::VASTART , MVT::Other, Custom);
255 setOperationAction(ISD::VAARG , MVT::Other, Expand);
256 setOperationAction(ISD::VAEND , MVT::Other, Expand);
257 if (Subtarget->is64Bit())
258 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
260 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
262 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
263 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
264 if (Subtarget->is64Bit())
265 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
266 if (Subtarget->isTargetCygMing())
267 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
269 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
272 // Set up the FP register classes.
273 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
274 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
276 // Use ANDPD to simulate FABS.
277 setOperationAction(ISD::FABS , MVT::f64, Custom);
278 setOperationAction(ISD::FABS , MVT::f32, Custom);
280 // Use XORP to simulate FNEG.
281 setOperationAction(ISD::FNEG , MVT::f64, Custom);
282 setOperationAction(ISD::FNEG , MVT::f32, Custom);
284 // Use ANDPD and ORPD to simulate FCOPYSIGN.
285 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
286 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
288 // We don't support sin/cos/fmod
289 setOperationAction(ISD::FSIN , MVT::f64, Expand);
290 setOperationAction(ISD::FCOS , MVT::f64, Expand);
291 setOperationAction(ISD::FREM , MVT::f64, Expand);
292 setOperationAction(ISD::FSIN , MVT::f32, Expand);
293 setOperationAction(ISD::FCOS , MVT::f32, Expand);
294 setOperationAction(ISD::FREM , MVT::f32, Expand);
296 // Expand FP immediates into loads from the stack, except for the special
298 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
299 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
300 addLegalFPImmediate(+0.0); // xorps / xorpd
302 // Set up the FP register classes.
303 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
304 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
306 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
307 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
308 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
309 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
310 setOperationAction(ISD::FP_ROUND, MVT::f32, Expand);
313 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
314 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
317 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
318 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
319 addLegalFPImmediate(+0.0); // FLD0
320 addLegalFPImmediate(+1.0); // FLD1
321 addLegalFPImmediate(-0.0); // FLD0/FCHS
322 addLegalFPImmediate(-1.0); // FLD1/FCHS
325 // First set operation action for all vector types to expand. Then we
326 // will selectively turn on ones that can be effectively codegen'd.
327 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
328 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
329 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
330 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
331 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
332 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
333 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
334 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
335 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
336 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
337 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
338 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
339 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
340 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
341 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
342 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
343 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
344 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
345 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
346 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
347 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
348 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
349 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
350 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
351 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
354 if (Subtarget->hasMMX()) {
355 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
356 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
357 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
358 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
360 // FIXME: add MMX packed arithmetics
362 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
363 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
364 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
365 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
367 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
368 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
369 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
371 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
372 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
374 setOperationAction(ISD::AND, MVT::v8i8, Promote);
375 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
376 setOperationAction(ISD::AND, MVT::v4i16, Promote);
377 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
378 setOperationAction(ISD::AND, MVT::v2i32, Promote);
379 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
380 setOperationAction(ISD::AND, MVT::v1i64, Legal);
382 setOperationAction(ISD::OR, MVT::v8i8, Promote);
383 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
384 setOperationAction(ISD::OR, MVT::v4i16, Promote);
385 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
386 setOperationAction(ISD::OR, MVT::v2i32, Promote);
387 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
388 setOperationAction(ISD::OR, MVT::v1i64, Legal);
390 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
391 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
392 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
393 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
394 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
395 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
396 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
398 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
399 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
400 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
401 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
402 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
403 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
404 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
406 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
407 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
408 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
409 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
411 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
412 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
413 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
414 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
416 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
417 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
418 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
419 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
422 if (Subtarget->hasSSE1()) {
423 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
425 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
426 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
427 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
428 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
429 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
430 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
431 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
432 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
433 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
434 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
435 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
438 if (Subtarget->hasSSE2()) {
439 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
440 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
441 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
442 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
443 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
445 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
446 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
447 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
448 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
449 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
450 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
451 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
452 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
453 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
454 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
455 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
456 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
457 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
458 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
459 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
461 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
462 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
463 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
464 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
465 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
466 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
468 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
469 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
470 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
471 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
472 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
474 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
475 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
476 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
477 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
478 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
479 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
481 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
482 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
483 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
484 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
485 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
486 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
487 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
488 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
489 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
490 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
491 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
492 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
495 // Custom lower v2i64 and v2f64 selects.
496 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
497 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
498 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
499 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
502 // We want to custom lower some of our intrinsics.
503 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
505 // We have target-specific dag combine patterns for the following nodes:
506 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
507 setTargetDAGCombine(ISD::SELECT);
509 computeRegisterProperties();
511 // FIXME: These should be based on subtarget info. Plus, the values should
512 // be smaller when we are in optimizing for size mode.
513 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
514 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
515 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
516 allowUnalignedMemoryAccesses = true; // x86 supports it!
520 //===----------------------------------------------------------------------===//
521 // Return Value Calling Convention Implementation
522 //===----------------------------------------------------------------------===//
524 #include "X86GenCallingConv.inc"
526 /// LowerRET - Lower an ISD::RET node.
527 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
528 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
530 SmallVector<CCValAssign, 16> RVLocs;
531 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
532 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
533 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
534 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
537 // If this is the first return lowered for this function, add the regs to the
538 // liveout set for the function.
539 if (DAG.getMachineFunction().liveout_empty()) {
540 for (unsigned i = 0; i != RVLocs.size(); ++i)
541 if (RVLocs[i].isRegLoc())
542 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
545 SDOperand Chain = Op.getOperand(0);
548 // Copy the result values into the output registers.
549 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
550 RVLocs[0].getLocReg() != X86::ST0) {
551 for (unsigned i = 0; i != RVLocs.size(); ++i) {
552 CCValAssign &VA = RVLocs[i];
553 assert(VA.isRegLoc() && "Can only return in registers!");
554 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
556 Flag = Chain.getValue(1);
559 // We need to handle a destination of ST0 specially, because it isn't really
561 SDOperand Value = Op.getOperand(1);
563 // If this is an FP return with ScalarSSE, we need to move the value from
564 // an XMM register onto the fp-stack.
568 // If this is a load into a scalarsse value, don't store the loaded value
569 // back to the stack, only to reload it: just replace the scalar-sse load.
570 if (ISD::isNON_EXTLoad(Value.Val) &&
571 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
572 Chain = Value.getOperand(0);
573 MemLoc = Value.getOperand(1);
575 // Spill the value to memory and reload it into top of stack.
576 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
577 MachineFunction &MF = DAG.getMachineFunction();
578 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
579 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
580 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
582 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
583 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
584 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
585 Chain = Value.getValue(1);
588 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
589 SDOperand Ops[] = { Chain, Value };
590 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
591 Flag = Chain.getValue(1);
594 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
596 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
598 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
602 /// LowerCallResult - Lower the result values of an ISD::CALL into the
603 /// appropriate copies out of appropriate physical registers. This assumes that
604 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
605 /// being lowered. The returns a SDNode with the same number of values as the
607 SDNode *X86TargetLowering::
608 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
609 unsigned CallingConv, SelectionDAG &DAG) {
611 // Assign locations to each value returned by this call.
612 SmallVector<CCValAssign, 16> RVLocs;
613 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
614 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
615 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
618 SmallVector<SDOperand, 8> ResultVals;
620 // Copy all of the result registers out of their specified physreg.
621 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
622 for (unsigned i = 0; i != RVLocs.size(); ++i) {
623 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
624 RVLocs[i].getValVT(), InFlag).getValue(1);
625 InFlag = Chain.getValue(2);
626 ResultVals.push_back(Chain.getValue(0));
629 // Copies from the FP stack are special, as ST0 isn't a valid register
630 // before the fp stackifier runs.
632 // Copy ST0 into an RFP register with FP_GET_RESULT.
633 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
634 SDOperand GROps[] = { Chain, InFlag };
635 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
636 Chain = RetVal.getValue(1);
637 InFlag = RetVal.getValue(2);
639 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
642 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
643 // shouldn't be necessary except that RFP cannot be live across
644 // multiple blocks. When stackifier is fixed, they can be uncoupled.
645 MachineFunction &MF = DAG.getMachineFunction();
646 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
647 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
649 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
651 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
652 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
653 Chain = RetVal.getValue(1);
655 ResultVals.push_back(RetVal);
658 // Merge everything together with a MERGE_VALUES node.
659 ResultVals.push_back(Chain);
660 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
661 &ResultVals[0], ResultVals.size()).Val;
665 //===----------------------------------------------------------------------===//
666 // C & StdCall Calling Convention implementation
667 //===----------------------------------------------------------------------===//
668 // StdCall calling convention seems to be standard for many Windows' API
669 // routines and around. It differs from C calling convention just a little:
670 // callee should clean up the stack, not caller. Symbols should be also
671 // decorated in some fancy way :) It doesn't support any vector arguments.
673 /// AddLiveIn - This helper function adds the specified physical register to the
674 /// MachineFunction as a live in value. It also creates a corresponding virtual
676 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
677 const TargetRegisterClass *RC) {
678 assert(RC->contains(PReg) && "Not the correct regclass!");
679 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
680 MF.addLiveIn(PReg, VReg);
684 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
686 unsigned NumArgs = Op.Val->getNumValues() - 1;
687 MachineFunction &MF = DAG.getMachineFunction();
688 MachineFrameInfo *MFI = MF.getFrameInfo();
689 SDOperand Root = Op.getOperand(0);
690 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
692 // Assign locations to all of the incoming arguments.
693 SmallVector<CCValAssign, 16> ArgLocs;
694 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
695 getTargetMachine(), ArgLocs);
696 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
698 SmallVector<SDOperand, 8> ArgValues;
699 unsigned LastVal = ~0U;
700 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
701 CCValAssign &VA = ArgLocs[i];
702 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
704 assert(VA.getValNo() != LastVal &&
705 "Don't support value assigned to multiple locs yet");
706 LastVal = VA.getValNo();
709 MVT::ValueType RegVT = VA.getLocVT();
710 TargetRegisterClass *RC;
711 if (RegVT == MVT::i32)
712 RC = X86::GR32RegisterClass;
714 assert(MVT::isVector(RegVT));
715 RC = X86::VR128RegisterClass;
718 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
719 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
721 // If this is an 8 or 16-bit value, it is really passed promoted to 32
722 // bits. Insert an assert[sz]ext to capture this, then truncate to the
724 if (VA.getLocInfo() == CCValAssign::SExt)
725 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
726 DAG.getValueType(VA.getValVT()));
727 else if (VA.getLocInfo() == CCValAssign::ZExt)
728 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
729 DAG.getValueType(VA.getValVT()));
731 if (VA.getLocInfo() != CCValAssign::Full)
732 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
734 ArgValues.push_back(ArgValue);
736 assert(VA.isMemLoc());
738 // Create the nodes corresponding to a load from this parameter slot.
739 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
740 VA.getLocMemOffset());
741 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
742 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
746 unsigned StackSize = CCInfo.getNextStackOffset();
748 ArgValues.push_back(Root);
750 // If the function takes variable number of arguments, make a frame index for
751 // the start of the first vararg value... for expansion of llvm.va_start.
753 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
755 if (isStdCall && !isVarArg) {
756 BytesToPopOnReturn = StackSize; // Callee pops everything..
757 BytesCallerReserves = 0;
759 BytesToPopOnReturn = 0; // Callee pops nothing.
761 // If this is an sret function, the return should pop the hidden pointer.
763 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
764 ISD::ParamFlags::StructReturn))
765 BytesToPopOnReturn = 4;
767 BytesCallerReserves = StackSize;
770 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
771 ReturnAddrIndex = 0; // No return address slot generated yet.
773 MF.getInfo<X86MachineFunctionInfo>()
774 ->setBytesToPopOnReturn(BytesToPopOnReturn);
776 // Return the new list of results.
777 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
778 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
781 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
783 SDOperand Chain = Op.getOperand(0);
784 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
785 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
786 SDOperand Callee = Op.getOperand(4);
787 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
789 // Analyze operands of the call, assigning locations to each operand.
790 SmallVector<CCValAssign, 16> ArgLocs;
791 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
792 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
794 // Get a count of how many bytes are to be pushed on the stack.
795 unsigned NumBytes = CCInfo.getNextStackOffset();
797 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
799 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
800 SmallVector<SDOperand, 8> MemOpChains;
804 // Walk the register/memloc assignments, inserting copies/loads.
805 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
806 CCValAssign &VA = ArgLocs[i];
807 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
809 // Promote the value if needed.
810 switch (VA.getLocInfo()) {
811 default: assert(0 && "Unknown loc info!");
812 case CCValAssign::Full: break;
813 case CCValAssign::SExt:
814 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
816 case CCValAssign::ZExt:
817 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
819 case CCValAssign::AExt:
820 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
825 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
827 assert(VA.isMemLoc());
828 if (StackPtr.Val == 0)
829 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
830 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
831 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
832 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
836 // If the first argument is an sret pointer, remember it.
837 bool isSRet = NumOps &&
838 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
839 ISD::ParamFlags::StructReturn);
841 if (!MemOpChains.empty())
842 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
843 &MemOpChains[0], MemOpChains.size());
845 // Build a sequence of copy-to-reg nodes chained together with token chain
846 // and flag operands which copy the outgoing args into registers.
848 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
849 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
851 InFlag = Chain.getValue(1);
854 // ELF / PIC requires GOT in the EBX register before function calls via PLT
856 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
857 Subtarget->isPICStyleGOT()) {
858 Chain = DAG.getCopyToReg(Chain, X86::EBX,
859 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
861 InFlag = Chain.getValue(1);
864 // If the callee is a GlobalAddress node (quite common, every direct call is)
865 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
866 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
867 // We should use extra load for direct calls to dllimported functions in
869 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
870 getTargetMachine(), true))
871 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
872 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
873 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
875 // Returns a chain & a flag for retval copy to use.
876 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
877 SmallVector<SDOperand, 8> Ops;
878 Ops.push_back(Chain);
879 Ops.push_back(Callee);
881 // Add argument registers to the end of the list so that they are known live
883 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
884 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
885 RegsToPass[i].second.getValueType()));
887 // Add an implicit use GOT pointer in EBX.
888 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
889 Subtarget->isPICStyleGOT())
890 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
893 Ops.push_back(InFlag);
895 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
896 NodeTys, &Ops[0], Ops.size());
897 InFlag = Chain.getValue(1);
899 // Create the CALLSEQ_END node.
900 unsigned NumBytesForCalleeToPush = 0;
902 if (CC == CallingConv::X86_StdCall) {
904 NumBytesForCalleeToPush = isSRet ? 4 : 0;
906 NumBytesForCalleeToPush = NumBytes;
908 // If this is is a call to a struct-return function, the callee
909 // pops the hidden struct pointer, so we have to push it back.
910 // This is common for Darwin/X86, Linux & Mingw32 targets.
911 NumBytesForCalleeToPush = isSRet ? 4 : 0;
914 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
916 Ops.push_back(Chain);
917 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
918 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
919 Ops.push_back(InFlag);
920 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
921 InFlag = Chain.getValue(1);
923 // Handle result values, copying them out of physregs into vregs that we
925 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
929 //===----------------------------------------------------------------------===//
930 // FastCall Calling Convention implementation
931 //===----------------------------------------------------------------------===//
933 // The X86 'fastcall' calling convention passes up to two integer arguments in
934 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
935 // and requires that the callee pop its arguments off the stack (allowing proper
936 // tail calls), and has the same return value conventions as C calling convs.
938 // This calling convention always arranges for the callee pop value to be 8n+4
939 // bytes, which is needed for tail recursion elimination and stack alignment
942 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
943 MachineFunction &MF = DAG.getMachineFunction();
944 MachineFrameInfo *MFI = MF.getFrameInfo();
945 SDOperand Root = Op.getOperand(0);
946 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
948 // Assign locations to all of the incoming arguments.
949 SmallVector<CCValAssign, 16> ArgLocs;
950 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
951 getTargetMachine(), ArgLocs);
952 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
954 SmallVector<SDOperand, 8> ArgValues;
955 unsigned LastVal = ~0U;
956 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
957 CCValAssign &VA = ArgLocs[i];
958 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
960 assert(VA.getValNo() != LastVal &&
961 "Don't support value assigned to multiple locs yet");
962 LastVal = VA.getValNo();
965 MVT::ValueType RegVT = VA.getLocVT();
966 TargetRegisterClass *RC;
967 if (RegVT == MVT::i32)
968 RC = X86::GR32RegisterClass;
970 assert(MVT::isVector(RegVT));
971 RC = X86::VR128RegisterClass;
974 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
975 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
977 // If this is an 8 or 16-bit value, it is really passed promoted to 32
978 // bits. Insert an assert[sz]ext to capture this, then truncate to the
980 if (VA.getLocInfo() == CCValAssign::SExt)
981 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
982 DAG.getValueType(VA.getValVT()));
983 else if (VA.getLocInfo() == CCValAssign::ZExt)
984 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
985 DAG.getValueType(VA.getValVT()));
987 if (VA.getLocInfo() != CCValAssign::Full)
988 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
990 ArgValues.push_back(ArgValue);
992 assert(VA.isMemLoc());
994 // Create the nodes corresponding to a load from this parameter slot.
995 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
996 VA.getLocMemOffset());
997 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
998 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1002 ArgValues.push_back(Root);
1004 unsigned StackSize = CCInfo.getNextStackOffset();
1006 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1007 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1008 // arguments and the arguments after the retaddr has been pushed are aligned.
1009 if ((StackSize & 7) == 0)
1013 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1014 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1015 ReturnAddrIndex = 0; // No return address slot generated yet.
1016 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
1017 BytesCallerReserves = 0;
1019 MF.getInfo<X86MachineFunctionInfo>()
1020 ->setBytesToPopOnReturn(BytesToPopOnReturn);
1022 // Return the new list of results.
1023 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1024 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1027 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1029 SDOperand Chain = Op.getOperand(0);
1030 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1031 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1032 SDOperand Callee = Op.getOperand(4);
1034 // Analyze operands of the call, assigning locations to each operand.
1035 SmallVector<CCValAssign, 16> ArgLocs;
1036 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1037 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
1039 // Get a count of how many bytes are to be pushed on the stack.
1040 unsigned NumBytes = CCInfo.getNextStackOffset();
1042 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1043 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1044 // arguments and the arguments after the retaddr has been pushed are aligned.
1045 if ((NumBytes & 7) == 0)
1049 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1051 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1052 SmallVector<SDOperand, 8> MemOpChains;
1056 // Walk the register/memloc assignments, inserting copies/loads.
1057 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1058 CCValAssign &VA = ArgLocs[i];
1059 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1061 // Promote the value if needed.
1062 switch (VA.getLocInfo()) {
1063 default: assert(0 && "Unknown loc info!");
1064 case CCValAssign::Full: break;
1065 case CCValAssign::SExt:
1066 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1068 case CCValAssign::ZExt:
1069 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1071 case CCValAssign::AExt:
1072 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1076 if (VA.isRegLoc()) {
1077 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1079 assert(VA.isMemLoc());
1080 if (StackPtr.Val == 0)
1081 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1082 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1083 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1084 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1088 if (!MemOpChains.empty())
1089 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1090 &MemOpChains[0], MemOpChains.size());
1092 // Build a sequence of copy-to-reg nodes chained together with token chain
1093 // and flag operands which copy the outgoing args into registers.
1095 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1096 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1098 InFlag = Chain.getValue(1);
1101 // If the callee is a GlobalAddress node (quite common, every direct call is)
1102 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1103 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1104 // We should use extra load for direct calls to dllimported functions in
1106 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1107 getTargetMachine(), true))
1108 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1109 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1110 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1112 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1114 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1115 Subtarget->isPICStyleGOT()) {
1116 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1117 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1119 InFlag = Chain.getValue(1);
1122 // Returns a chain & a flag for retval copy to use.
1123 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1124 SmallVector<SDOperand, 8> Ops;
1125 Ops.push_back(Chain);
1126 Ops.push_back(Callee);
1128 // Add argument registers to the end of the list so that they are known live
1130 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1131 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1132 RegsToPass[i].second.getValueType()));
1134 // Add an implicit use GOT pointer in EBX.
1135 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1136 Subtarget->isPICStyleGOT())
1137 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1140 Ops.push_back(InFlag);
1142 // FIXME: Do not generate X86ISD::TAILCALL for now.
1143 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1144 NodeTys, &Ops[0], Ops.size());
1145 InFlag = Chain.getValue(1);
1147 // Returns a flag for retval copy to use.
1148 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1150 Ops.push_back(Chain);
1151 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1152 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1153 Ops.push_back(InFlag);
1154 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1155 InFlag = Chain.getValue(1);
1157 // Handle result values, copying them out of physregs into vregs that we
1159 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1163 //===----------------------------------------------------------------------===//
1164 // X86-64 C Calling Convention implementation
1165 //===----------------------------------------------------------------------===//
1168 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1169 MachineFunction &MF = DAG.getMachineFunction();
1170 MachineFrameInfo *MFI = MF.getFrameInfo();
1171 SDOperand Root = Op.getOperand(0);
1172 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1174 static const unsigned GPR64ArgRegs[] = {
1175 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1177 static const unsigned XMMArgRegs[] = {
1178 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1179 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1183 // Assign locations to all of the incoming arguments.
1184 SmallVector<CCValAssign, 16> ArgLocs;
1185 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1186 getTargetMachine(), ArgLocs);
1187 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1189 SmallVector<SDOperand, 8> ArgValues;
1190 unsigned LastVal = ~0U;
1191 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1192 CCValAssign &VA = ArgLocs[i];
1193 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1195 assert(VA.getValNo() != LastVal &&
1196 "Don't support value assigned to multiple locs yet");
1197 LastVal = VA.getValNo();
1199 if (VA.isRegLoc()) {
1200 MVT::ValueType RegVT = VA.getLocVT();
1201 TargetRegisterClass *RC;
1202 if (RegVT == MVT::i32)
1203 RC = X86::GR32RegisterClass;
1204 else if (RegVT == MVT::i64)
1205 RC = X86::GR64RegisterClass;
1206 else if (RegVT == MVT::f32)
1207 RC = X86::FR32RegisterClass;
1208 else if (RegVT == MVT::f64)
1209 RC = X86::FR64RegisterClass;
1211 assert(MVT::isVector(RegVT));
1212 if (MVT::getSizeInBits(RegVT) == 64) {
1213 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1216 RC = X86::VR128RegisterClass;
1219 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1220 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1222 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1223 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1225 if (VA.getLocInfo() == CCValAssign::SExt)
1226 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1227 DAG.getValueType(VA.getValVT()));
1228 else if (VA.getLocInfo() == CCValAssign::ZExt)
1229 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1230 DAG.getValueType(VA.getValVT()));
1232 if (VA.getLocInfo() != CCValAssign::Full)
1233 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1235 // Handle MMX values passed in GPRs.
1236 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1237 MVT::getSizeInBits(RegVT) == 64)
1238 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1240 ArgValues.push_back(ArgValue);
1242 assert(VA.isMemLoc());
1244 // Create the nodes corresponding to a load from this parameter slot.
1245 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1246 VA.getLocMemOffset());
1247 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1248 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1252 unsigned StackSize = CCInfo.getNextStackOffset();
1254 // If the function takes variable number of arguments, make a frame index for
1255 // the start of the first vararg value... for expansion of llvm.va_start.
1257 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1258 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1260 // For X86-64, if there are vararg parameters that are passed via
1261 // registers, then we must store them to their spots on the stack so they
1262 // may be loaded by deferencing the result of va_next.
1263 VarArgsGPOffset = NumIntRegs * 8;
1264 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1265 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1266 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1268 // Store the integer parameter registers.
1269 SmallVector<SDOperand, 8> MemOps;
1270 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1271 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1272 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1273 for (; NumIntRegs != 6; ++NumIntRegs) {
1274 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1275 X86::GR64RegisterClass);
1276 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1277 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1278 MemOps.push_back(Store);
1279 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1280 DAG.getConstant(8, getPointerTy()));
1283 // Now store the XMM (fp + vector) parameter registers.
1284 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1285 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1286 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1287 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1288 X86::VR128RegisterClass);
1289 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1290 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1291 MemOps.push_back(Store);
1292 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1293 DAG.getConstant(16, getPointerTy()));
1295 if (!MemOps.empty())
1296 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1297 &MemOps[0], MemOps.size());
1300 ArgValues.push_back(Root);
1302 ReturnAddrIndex = 0; // No return address slot generated yet.
1303 BytesToPopOnReturn = 0; // Callee pops nothing.
1304 BytesCallerReserves = StackSize;
1306 // Return the new list of results.
1307 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1308 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1312 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1314 SDOperand Chain = Op.getOperand(0);
1315 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1316 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1317 SDOperand Callee = Op.getOperand(4);
1319 // Analyze operands of the call, assigning locations to each operand.
1320 SmallVector<CCValAssign, 16> ArgLocs;
1321 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1322 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1324 // Get a count of how many bytes are to be pushed on the stack.
1325 unsigned NumBytes = CCInfo.getNextStackOffset();
1326 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1328 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1329 SmallVector<SDOperand, 8> MemOpChains;
1333 // Walk the register/memloc assignments, inserting copies/loads.
1334 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1335 CCValAssign &VA = ArgLocs[i];
1336 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1338 // Promote the value if needed.
1339 switch (VA.getLocInfo()) {
1340 default: assert(0 && "Unknown loc info!");
1341 case CCValAssign::Full: break;
1342 case CCValAssign::SExt:
1343 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1345 case CCValAssign::ZExt:
1346 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1348 case CCValAssign::AExt:
1349 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1353 if (VA.isRegLoc()) {
1354 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1356 assert(VA.isMemLoc());
1357 if (StackPtr.Val == 0)
1358 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1359 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1360 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1361 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1365 if (!MemOpChains.empty())
1366 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1367 &MemOpChains[0], MemOpChains.size());
1369 // Build a sequence of copy-to-reg nodes chained together with token chain
1370 // and flag operands which copy the outgoing args into registers.
1372 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1373 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1375 InFlag = Chain.getValue(1);
1379 // From AMD64 ABI document:
1380 // For calls that may call functions that use varargs or stdargs
1381 // (prototype-less calls or calls to functions containing ellipsis (...) in
1382 // the declaration) %al is used as hidden argument to specify the number
1383 // of SSE registers used. The contents of %al do not need to match exactly
1384 // the number of registers, but must be an ubound on the number of SSE
1385 // registers used and is in the range 0 - 8 inclusive.
1387 // Count the number of XMM registers allocated.
1388 static const unsigned XMMArgRegs[] = {
1389 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1390 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1392 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1394 Chain = DAG.getCopyToReg(Chain, X86::AL,
1395 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1396 InFlag = Chain.getValue(1);
1399 // If the callee is a GlobalAddress node (quite common, every direct call is)
1400 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1401 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1402 // We should use extra load for direct calls to dllimported functions in
1404 if (getTargetMachine().getCodeModel() != CodeModel::Large
1405 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1406 getTargetMachine(), true))
1407 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1408 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1409 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1410 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1412 // Returns a chain & a flag for retval copy to use.
1413 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1414 SmallVector<SDOperand, 8> Ops;
1415 Ops.push_back(Chain);
1416 Ops.push_back(Callee);
1418 // Add argument registers to the end of the list so that they are known live
1420 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1421 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1422 RegsToPass[i].second.getValueType()));
1425 Ops.push_back(InFlag);
1427 // FIXME: Do not generate X86ISD::TAILCALL for now.
1428 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1429 NodeTys, &Ops[0], Ops.size());
1430 InFlag = Chain.getValue(1);
1432 // Returns a flag for retval copy to use.
1433 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1435 Ops.push_back(Chain);
1436 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1437 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1438 Ops.push_back(InFlag);
1439 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1440 InFlag = Chain.getValue(1);
1442 // Handle result values, copying them out of physregs into vregs that we
1444 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1448 //===----------------------------------------------------------------------===//
1449 // Other Lowering Hooks
1450 //===----------------------------------------------------------------------===//
1453 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1454 if (ReturnAddrIndex == 0) {
1455 // Set up a frame object for the return address.
1456 MachineFunction &MF = DAG.getMachineFunction();
1457 if (Subtarget->is64Bit())
1458 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1460 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1463 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1468 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1469 /// specific condition code. It returns a false if it cannot do a direct
1470 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1472 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1473 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1474 SelectionDAG &DAG) {
1475 X86CC = X86::COND_INVALID;
1477 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1478 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1479 // X > -1 -> X == 0, jump !sign.
1480 RHS = DAG.getConstant(0, RHS.getValueType());
1481 X86CC = X86::COND_NS;
1483 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1484 // X < 0 -> X == 0, jump on sign.
1485 X86CC = X86::COND_S;
1490 switch (SetCCOpcode) {
1492 case ISD::SETEQ: X86CC = X86::COND_E; break;
1493 case ISD::SETGT: X86CC = X86::COND_G; break;
1494 case ISD::SETGE: X86CC = X86::COND_GE; break;
1495 case ISD::SETLT: X86CC = X86::COND_L; break;
1496 case ISD::SETLE: X86CC = X86::COND_LE; break;
1497 case ISD::SETNE: X86CC = X86::COND_NE; break;
1498 case ISD::SETULT: X86CC = X86::COND_B; break;
1499 case ISD::SETUGT: X86CC = X86::COND_A; break;
1500 case ISD::SETULE: X86CC = X86::COND_BE; break;
1501 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1504 // On a floating point condition, the flags are set as follows:
1506 // 0 | 0 | 0 | X > Y
1507 // 0 | 0 | 1 | X < Y
1508 // 1 | 0 | 0 | X == Y
1509 // 1 | 1 | 1 | unordered
1511 switch (SetCCOpcode) {
1514 case ISD::SETEQ: X86CC = X86::COND_E; break;
1515 case ISD::SETOLT: Flip = true; // Fallthrough
1517 case ISD::SETGT: X86CC = X86::COND_A; break;
1518 case ISD::SETOLE: Flip = true; // Fallthrough
1520 case ISD::SETGE: X86CC = X86::COND_AE; break;
1521 case ISD::SETUGT: Flip = true; // Fallthrough
1523 case ISD::SETLT: X86CC = X86::COND_B; break;
1524 case ISD::SETUGE: Flip = true; // Fallthrough
1526 case ISD::SETLE: X86CC = X86::COND_BE; break;
1528 case ISD::SETNE: X86CC = X86::COND_NE; break;
1529 case ISD::SETUO: X86CC = X86::COND_P; break;
1530 case ISD::SETO: X86CC = X86::COND_NP; break;
1533 std::swap(LHS, RHS);
1536 return X86CC != X86::COND_INVALID;
1539 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1540 /// code. Current x86 isa includes the following FP cmov instructions:
1541 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1542 static bool hasFPCMov(unsigned X86CC) {
1558 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1559 /// true if Op is undef or if its value falls within the specified range (L, H].
1560 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1561 if (Op.getOpcode() == ISD::UNDEF)
1564 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1565 return (Val >= Low && Val < Hi);
1568 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1569 /// true if Op is undef or if its value equal to the specified value.
1570 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1571 if (Op.getOpcode() == ISD::UNDEF)
1573 return cast<ConstantSDNode>(Op)->getValue() == Val;
1576 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1577 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1578 bool X86::isPSHUFDMask(SDNode *N) {
1579 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1581 if (N->getNumOperands() != 4)
1584 // Check if the value doesn't reference the second vector.
1585 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1586 SDOperand Arg = N->getOperand(i);
1587 if (Arg.getOpcode() == ISD::UNDEF) continue;
1588 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1589 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1596 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1597 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1598 bool X86::isPSHUFHWMask(SDNode *N) {
1599 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1601 if (N->getNumOperands() != 8)
1604 // Lower quadword copied in order.
1605 for (unsigned i = 0; i != 4; ++i) {
1606 SDOperand Arg = N->getOperand(i);
1607 if (Arg.getOpcode() == ISD::UNDEF) continue;
1608 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1609 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1613 // Upper quadword shuffled.
1614 for (unsigned i = 4; i != 8; ++i) {
1615 SDOperand Arg = N->getOperand(i);
1616 if (Arg.getOpcode() == ISD::UNDEF) continue;
1617 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1618 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1619 if (Val < 4 || Val > 7)
1626 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1627 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1628 bool X86::isPSHUFLWMask(SDNode *N) {
1629 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1631 if (N->getNumOperands() != 8)
1634 // Upper quadword copied in order.
1635 for (unsigned i = 4; i != 8; ++i)
1636 if (!isUndefOrEqual(N->getOperand(i), i))
1639 // Lower quadword shuffled.
1640 for (unsigned i = 0; i != 4; ++i)
1641 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1647 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1648 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1649 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1650 if (NumElems != 2 && NumElems != 4) return false;
1652 unsigned Half = NumElems / 2;
1653 for (unsigned i = 0; i < Half; ++i)
1654 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1656 for (unsigned i = Half; i < NumElems; ++i)
1657 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1663 bool X86::isSHUFPMask(SDNode *N) {
1664 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1665 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1668 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly
1669 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1670 /// half elements to come from vector 1 (which would equal the dest.) and
1671 /// the upper half to come from vector 2.
1672 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1673 if (NumOps != 2 && NumOps != 4) return false;
1675 unsigned Half = NumOps / 2;
1676 for (unsigned i = 0; i < Half; ++i)
1677 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1679 for (unsigned i = Half; i < NumOps; ++i)
1680 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1685 static bool isCommutedSHUFP(SDNode *N) {
1686 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1687 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1690 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1691 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1692 bool X86::isMOVHLPSMask(SDNode *N) {
1693 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1695 if (N->getNumOperands() != 4)
1698 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1699 return isUndefOrEqual(N->getOperand(0), 6) &&
1700 isUndefOrEqual(N->getOperand(1), 7) &&
1701 isUndefOrEqual(N->getOperand(2), 2) &&
1702 isUndefOrEqual(N->getOperand(3), 3);
1705 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1706 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1708 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1709 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1711 if (N->getNumOperands() != 4)
1714 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1715 return isUndefOrEqual(N->getOperand(0), 2) &&
1716 isUndefOrEqual(N->getOperand(1), 3) &&
1717 isUndefOrEqual(N->getOperand(2), 2) &&
1718 isUndefOrEqual(N->getOperand(3), 3);
1721 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1722 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1723 bool X86::isMOVLPMask(SDNode *N) {
1724 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1726 unsigned NumElems = N->getNumOperands();
1727 if (NumElems != 2 && NumElems != 4)
1730 for (unsigned i = 0; i < NumElems/2; ++i)
1731 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1734 for (unsigned i = NumElems/2; i < NumElems; ++i)
1735 if (!isUndefOrEqual(N->getOperand(i), i))
1741 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1742 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1744 bool X86::isMOVHPMask(SDNode *N) {
1745 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1747 unsigned NumElems = N->getNumOperands();
1748 if (NumElems != 2 && NumElems != 4)
1751 for (unsigned i = 0; i < NumElems/2; ++i)
1752 if (!isUndefOrEqual(N->getOperand(i), i))
1755 for (unsigned i = 0; i < NumElems/2; ++i) {
1756 SDOperand Arg = N->getOperand(i + NumElems/2);
1757 if (!isUndefOrEqual(Arg, i + NumElems))
1764 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1765 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1766 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1767 bool V2IsSplat = false) {
1768 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1771 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1772 SDOperand BitI = Elts[i];
1773 SDOperand BitI1 = Elts[i+1];
1774 if (!isUndefOrEqual(BitI, j))
1777 if (isUndefOrEqual(BitI1, NumElts))
1780 if (!isUndefOrEqual(BitI1, j + NumElts))
1788 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1789 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1790 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1793 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1794 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1795 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1796 bool V2IsSplat = false) {
1797 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1800 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1801 SDOperand BitI = Elts[i];
1802 SDOperand BitI1 = Elts[i+1];
1803 if (!isUndefOrEqual(BitI, j + NumElts/2))
1806 if (isUndefOrEqual(BitI1, NumElts))
1809 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1817 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1818 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1819 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1822 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1823 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1825 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1826 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1828 unsigned NumElems = N->getNumOperands();
1829 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1832 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1833 SDOperand BitI = N->getOperand(i);
1834 SDOperand BitI1 = N->getOperand(i+1);
1836 if (!isUndefOrEqual(BitI, j))
1838 if (!isUndefOrEqual(BitI1, j))
1845 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1846 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1848 bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1849 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1851 unsigned NumElems = N->getNumOperands();
1852 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1855 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1856 SDOperand BitI = N->getOperand(i);
1857 SDOperand BitI1 = N->getOperand(i + 1);
1859 if (!isUndefOrEqual(BitI, j))
1861 if (!isUndefOrEqual(BitI1, j))
1868 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1869 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1870 /// MOVSD, and MOVD, i.e. setting the lowest element.
1871 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1872 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1875 if (!isUndefOrEqual(Elts[0], NumElts))
1878 for (unsigned i = 1; i < NumElts; ++i) {
1879 if (!isUndefOrEqual(Elts[i], i))
1886 bool X86::isMOVLMask(SDNode *N) {
1887 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1888 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
1891 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1892 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1893 /// element of vector 2 and the other elements to come from vector 1 in order.
1894 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1895 bool V2IsSplat = false,
1896 bool V2IsUndef = false) {
1897 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
1900 if (!isUndefOrEqual(Ops[0], 0))
1903 for (unsigned i = 1; i < NumOps; ++i) {
1904 SDOperand Arg = Ops[i];
1905 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1906 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1907 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
1914 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1915 bool V2IsUndef = false) {
1916 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1917 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1918 V2IsSplat, V2IsUndef);
1921 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1922 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1923 bool X86::isMOVSHDUPMask(SDNode *N) {
1924 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1926 if (N->getNumOperands() != 4)
1929 // Expect 1, 1, 3, 3
1930 for (unsigned i = 0; i < 2; ++i) {
1931 SDOperand Arg = N->getOperand(i);
1932 if (Arg.getOpcode() == ISD::UNDEF) continue;
1933 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1934 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1935 if (Val != 1) return false;
1939 for (unsigned i = 2; i < 4; ++i) {
1940 SDOperand Arg = N->getOperand(i);
1941 if (Arg.getOpcode() == ISD::UNDEF) continue;
1942 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1943 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1944 if (Val != 3) return false;
1948 // Don't use movshdup if it can be done with a shufps.
1952 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1953 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1954 bool X86::isMOVSLDUPMask(SDNode *N) {
1955 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1957 if (N->getNumOperands() != 4)
1960 // Expect 0, 0, 2, 2
1961 for (unsigned i = 0; i < 2; ++i) {
1962 SDOperand Arg = N->getOperand(i);
1963 if (Arg.getOpcode() == ISD::UNDEF) continue;
1964 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1965 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1966 if (Val != 0) return false;
1970 for (unsigned i = 2; i < 4; ++i) {
1971 SDOperand Arg = N->getOperand(i);
1972 if (Arg.getOpcode() == ISD::UNDEF) continue;
1973 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1974 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1975 if (Val != 2) return false;
1979 // Don't use movshdup if it can be done with a shufps.
1983 /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
1984 /// specifies a identity operation on the LHS or RHS.
1985 static bool isIdentityMask(SDNode *N, bool RHS = false) {
1986 unsigned NumElems = N->getNumOperands();
1987 for (unsigned i = 0; i < NumElems; ++i)
1988 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
1993 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1994 /// a splat of a single element.
1995 static bool isSplatMask(SDNode *N) {
1996 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1998 // This is a splat operation if each element of the permute is the same, and
1999 // if the value doesn't reference the second vector.
2000 unsigned NumElems = N->getNumOperands();
2001 SDOperand ElementBase;
2003 for (; i != NumElems; ++i) {
2004 SDOperand Elt = N->getOperand(i);
2005 if (isa<ConstantSDNode>(Elt)) {
2011 if (!ElementBase.Val)
2014 for (; i != NumElems; ++i) {
2015 SDOperand Arg = N->getOperand(i);
2016 if (Arg.getOpcode() == ISD::UNDEF) continue;
2017 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2018 if (Arg != ElementBase) return false;
2021 // Make sure it is a splat of the first vector operand.
2022 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2025 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2026 /// a splat of a single element and it's a 2 or 4 element mask.
2027 bool X86::isSplatMask(SDNode *N) {
2028 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2030 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2031 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2033 return ::isSplatMask(N);
2036 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2037 /// specifies a splat of zero element.
2038 bool X86::isSplatLoMask(SDNode *N) {
2039 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2041 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2042 if (!isUndefOrEqual(N->getOperand(i), 0))
2047 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2048 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2050 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2051 unsigned NumOperands = N->getNumOperands();
2052 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2054 for (unsigned i = 0; i < NumOperands; ++i) {
2056 SDOperand Arg = N->getOperand(NumOperands-i-1);
2057 if (Arg.getOpcode() != ISD::UNDEF)
2058 Val = cast<ConstantSDNode>(Arg)->getValue();
2059 if (Val >= NumOperands) Val -= NumOperands;
2061 if (i != NumOperands - 1)
2068 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2069 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2071 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2073 // 8 nodes, but we only care about the last 4.
2074 for (unsigned i = 7; i >= 4; --i) {
2076 SDOperand Arg = N->getOperand(i);
2077 if (Arg.getOpcode() != ISD::UNDEF)
2078 Val = cast<ConstantSDNode>(Arg)->getValue();
2087 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2088 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2090 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2092 // 8 nodes, but we only care about the first 4.
2093 for (int i = 3; i >= 0; --i) {
2095 SDOperand Arg = N->getOperand(i);
2096 if (Arg.getOpcode() != ISD::UNDEF)
2097 Val = cast<ConstantSDNode>(Arg)->getValue();
2106 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2107 /// specifies a 8 element shuffle that can be broken into a pair of
2108 /// PSHUFHW and PSHUFLW.
2109 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2110 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2112 if (N->getNumOperands() != 8)
2115 // Lower quadword shuffled.
2116 for (unsigned i = 0; i != 4; ++i) {
2117 SDOperand Arg = N->getOperand(i);
2118 if (Arg.getOpcode() == ISD::UNDEF) continue;
2119 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2120 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2125 // Upper quadword shuffled.
2126 for (unsigned i = 4; i != 8; ++i) {
2127 SDOperand Arg = N->getOperand(i);
2128 if (Arg.getOpcode() == ISD::UNDEF) continue;
2129 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2130 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2131 if (Val < 4 || Val > 7)
2138 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2139 /// values in ther permute mask.
2140 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2141 SDOperand &V2, SDOperand &Mask,
2142 SelectionDAG &DAG) {
2143 MVT::ValueType VT = Op.getValueType();
2144 MVT::ValueType MaskVT = Mask.getValueType();
2145 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2146 unsigned NumElems = Mask.getNumOperands();
2147 SmallVector<SDOperand, 8> MaskVec;
2149 for (unsigned i = 0; i != NumElems; ++i) {
2150 SDOperand Arg = Mask.getOperand(i);
2151 if (Arg.getOpcode() == ISD::UNDEF) {
2152 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2155 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2156 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2158 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2160 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2164 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2165 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2168 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2169 /// match movhlps. The lower half elements should come from upper half of
2170 /// V1 (and in order), and the upper half elements should come from the upper
2171 /// half of V2 (and in order).
2172 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2173 unsigned NumElems = Mask->getNumOperands();
2176 for (unsigned i = 0, e = 2; i != e; ++i)
2177 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2179 for (unsigned i = 2; i != 4; ++i)
2180 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2185 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2186 /// is promoted to a vector.
2187 static inline bool isScalarLoadToVector(SDNode *N) {
2188 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2189 N = N->getOperand(0).Val;
2190 return ISD::isNON_EXTLoad(N);
2195 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2196 /// match movlp{s|d}. The lower half elements should come from lower half of
2197 /// V1 (and in order), and the upper half elements should come from the upper
2198 /// half of V2 (and in order). And since V1 will become the source of the
2199 /// MOVLP, it must be either a vector load or a scalar load to vector.
2200 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2201 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2203 // Is V2 is a vector load, don't do this transformation. We will try to use
2204 // load folding shufps op.
2205 if (ISD::isNON_EXTLoad(V2))
2208 unsigned NumElems = Mask->getNumOperands();
2209 if (NumElems != 2 && NumElems != 4)
2211 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2212 if (!isUndefOrEqual(Mask->getOperand(i), i))
2214 for (unsigned i = NumElems/2; i != NumElems; ++i)
2215 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2220 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2222 static bool isSplatVector(SDNode *N) {
2223 if (N->getOpcode() != ISD::BUILD_VECTOR)
2226 SDOperand SplatValue = N->getOperand(0);
2227 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2228 if (N->getOperand(i) != SplatValue)
2233 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2235 static bool isUndefShuffle(SDNode *N) {
2236 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2239 SDOperand V1 = N->getOperand(0);
2240 SDOperand V2 = N->getOperand(1);
2241 SDOperand Mask = N->getOperand(2);
2242 unsigned NumElems = Mask.getNumOperands();
2243 for (unsigned i = 0; i != NumElems; ++i) {
2244 SDOperand Arg = Mask.getOperand(i);
2245 if (Arg.getOpcode() != ISD::UNDEF) {
2246 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2247 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2249 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2256 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2258 static inline bool isZeroNode(SDOperand Elt) {
2259 return ((isa<ConstantSDNode>(Elt) &&
2260 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2261 (isa<ConstantFPSDNode>(Elt) &&
2262 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2265 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2266 /// to an zero vector.
2267 static bool isZeroShuffle(SDNode *N) {
2268 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2271 SDOperand V1 = N->getOperand(0);
2272 SDOperand V2 = N->getOperand(1);
2273 SDOperand Mask = N->getOperand(2);
2274 unsigned NumElems = Mask.getNumOperands();
2275 for (unsigned i = 0; i != NumElems; ++i) {
2276 SDOperand Arg = Mask.getOperand(i);
2277 if (Arg.getOpcode() != ISD::UNDEF) {
2278 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2279 if (Idx < NumElems) {
2280 unsigned Opc = V1.Val->getOpcode();
2281 if (Opc == ISD::UNDEF)
2283 if (Opc != ISD::BUILD_VECTOR ||
2284 !isZeroNode(V1.Val->getOperand(Idx)))
2286 } else if (Idx >= NumElems) {
2287 unsigned Opc = V2.Val->getOpcode();
2288 if (Opc == ISD::UNDEF)
2290 if (Opc != ISD::BUILD_VECTOR ||
2291 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2299 /// getZeroVector - Returns a vector of specified type with all zero elements.
2301 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2302 assert(MVT::isVector(VT) && "Expected a vector type");
2303 unsigned NumElems = MVT::getVectorNumElements(VT);
2304 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2305 bool isFP = MVT::isFloatingPoint(EVT);
2306 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2307 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2308 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2311 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2312 /// that point to V2 points to its first element.
2313 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2314 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2316 bool Changed = false;
2317 SmallVector<SDOperand, 8> MaskVec;
2318 unsigned NumElems = Mask.getNumOperands();
2319 for (unsigned i = 0; i != NumElems; ++i) {
2320 SDOperand Arg = Mask.getOperand(i);
2321 if (Arg.getOpcode() != ISD::UNDEF) {
2322 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2323 if (Val > NumElems) {
2324 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2328 MaskVec.push_back(Arg);
2332 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2333 &MaskVec[0], MaskVec.size());
2337 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2338 /// operation of specified width.
2339 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2340 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2341 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2343 SmallVector<SDOperand, 8> MaskVec;
2344 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2345 for (unsigned i = 1; i != NumElems; ++i)
2346 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2347 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2350 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2351 /// of specified width.
2352 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2353 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2354 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2355 SmallVector<SDOperand, 8> MaskVec;
2356 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2357 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2358 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2360 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2363 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2364 /// of specified width.
2365 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2366 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2367 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2368 unsigned Half = NumElems/2;
2369 SmallVector<SDOperand, 8> MaskVec;
2370 for (unsigned i = 0; i != Half; ++i) {
2371 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2372 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2374 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2377 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2379 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2380 SDOperand V1 = Op.getOperand(0);
2381 SDOperand Mask = Op.getOperand(2);
2382 MVT::ValueType VT = Op.getValueType();
2383 unsigned NumElems = Mask.getNumOperands();
2384 Mask = getUnpacklMask(NumElems, DAG);
2385 while (NumElems != 4) {
2386 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2389 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2391 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2392 Mask = getZeroVector(MaskVT, DAG);
2393 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2394 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2395 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2398 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2399 /// vector of zero or undef vector.
2400 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2401 unsigned NumElems, unsigned Idx,
2402 bool isZero, SelectionDAG &DAG) {
2403 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2404 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2405 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2406 SDOperand Zero = DAG.getConstant(0, EVT);
2407 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2408 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2409 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2410 &MaskVec[0], MaskVec.size());
2411 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2414 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2416 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2417 unsigned NumNonZero, unsigned NumZero,
2418 SelectionDAG &DAG, TargetLowering &TLI) {
2424 for (unsigned i = 0; i < 16; ++i) {
2425 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2426 if (ThisIsNonZero && First) {
2428 V = getZeroVector(MVT::v8i16, DAG);
2430 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2435 SDOperand ThisElt(0, 0), LastElt(0, 0);
2436 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2437 if (LastIsNonZero) {
2438 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2440 if (ThisIsNonZero) {
2441 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2442 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2443 ThisElt, DAG.getConstant(8, MVT::i8));
2445 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2450 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2451 DAG.getConstant(i/2, TLI.getPointerTy()));
2455 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2458 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2460 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2461 unsigned NumNonZero, unsigned NumZero,
2462 SelectionDAG &DAG, TargetLowering &TLI) {
2468 for (unsigned i = 0; i < 8; ++i) {
2469 bool isNonZero = (NonZeros & (1 << i)) != 0;
2473 V = getZeroVector(MVT::v8i16, DAG);
2475 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2478 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2479 DAG.getConstant(i, TLI.getPointerTy()));
2487 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2488 // All zero's are handled with pxor.
2489 if (ISD::isBuildVectorAllZeros(Op.Val))
2492 // All one's are handled with pcmpeqd.
2493 if (ISD::isBuildVectorAllOnes(Op.Val))
2496 MVT::ValueType VT = Op.getValueType();
2497 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2498 unsigned EVTBits = MVT::getSizeInBits(EVT);
2500 unsigned NumElems = Op.getNumOperands();
2501 unsigned NumZero = 0;
2502 unsigned NumNonZero = 0;
2503 unsigned NonZeros = 0;
2504 unsigned NumNonZeroImms = 0;
2505 std::set<SDOperand> Values;
2506 for (unsigned i = 0; i < NumElems; ++i) {
2507 SDOperand Elt = Op.getOperand(i);
2508 if (Elt.getOpcode() != ISD::UNDEF) {
2510 if (isZeroNode(Elt))
2513 NonZeros |= (1 << i);
2515 if (Elt.getOpcode() == ISD::Constant ||
2516 Elt.getOpcode() == ISD::ConstantFP)
2522 if (NumNonZero == 0) {
2524 // All undef vector. Return an UNDEF.
2525 return DAG.getNode(ISD::UNDEF, VT);
2527 // A mix of zero and undef. Return a zero vector.
2528 return getZeroVector(VT, DAG);
2531 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2532 if (Values.size() == 1)
2535 // Special case for single non-zero element.
2536 if (NumNonZero == 1) {
2537 unsigned Idx = CountTrailingZeros_32(NonZeros);
2538 SDOperand Item = Op.getOperand(Idx);
2539 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2541 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2542 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2545 if (EVTBits == 32) {
2546 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2547 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2549 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2550 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2551 SmallVector<SDOperand, 8> MaskVec;
2552 for (unsigned i = 0; i < NumElems; i++)
2553 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2554 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2555 &MaskVec[0], MaskVec.size());
2556 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2557 DAG.getNode(ISD::UNDEF, VT), Mask);
2561 // A vector full of immediates; various special cases are already
2562 // handled, so this is best done with a single constant-pool load.
2563 if (NumNonZero == NumNonZeroImms)
2566 // Let legalizer expand 2-wide build_vectors.
2570 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2571 if (EVTBits == 8 && NumElems == 16) {
2572 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2574 if (V.Val) return V;
2577 if (EVTBits == 16 && NumElems == 8) {
2578 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2580 if (V.Val) return V;
2583 // If element VT is == 32 bits, turn it into a number of shuffles.
2584 SmallVector<SDOperand, 8> V;
2586 if (NumElems == 4 && NumZero > 0) {
2587 for (unsigned i = 0; i < 4; ++i) {
2588 bool isZero = !(NonZeros & (1 << i));
2590 V[i] = getZeroVector(VT, DAG);
2592 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2595 for (unsigned i = 0; i < 2; ++i) {
2596 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2599 V[i] = V[i*2]; // Must be a zero vector.
2602 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2603 getMOVLMask(NumElems, DAG));
2606 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2607 getMOVLMask(NumElems, DAG));
2610 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2611 getUnpacklMask(NumElems, DAG));
2616 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2617 // clears the upper bits.
2618 // FIXME: we can do the same for v4f32 case when we know both parts of
2619 // the lower half come from scalar_to_vector (loadf32). We should do
2620 // that in post legalizer dag combiner with target specific hooks.
2621 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2623 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2624 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2625 SmallVector<SDOperand, 8> MaskVec;
2626 bool Reverse = (NonZeros & 0x3) == 2;
2627 for (unsigned i = 0; i < 2; ++i)
2629 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2631 MaskVec.push_back(DAG.getConstant(i, EVT));
2632 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2633 for (unsigned i = 0; i < 2; ++i)
2635 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2637 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2638 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2639 &MaskVec[0], MaskVec.size());
2640 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2643 if (Values.size() > 2) {
2644 // Expand into a number of unpckl*.
2646 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2647 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2648 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2649 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2650 for (unsigned i = 0; i < NumElems; ++i)
2651 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2653 while (NumElems != 0) {
2654 for (unsigned i = 0; i < NumElems; ++i)
2655 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2666 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2667 SDOperand V1 = Op.getOperand(0);
2668 SDOperand V2 = Op.getOperand(1);
2669 SDOperand PermMask = Op.getOperand(2);
2670 MVT::ValueType VT = Op.getValueType();
2671 unsigned NumElems = PermMask.getNumOperands();
2672 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2673 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2674 bool V1IsSplat = false;
2675 bool V2IsSplat = false;
2677 if (isUndefShuffle(Op.Val))
2678 return DAG.getNode(ISD::UNDEF, VT);
2680 if (isZeroShuffle(Op.Val))
2681 return getZeroVector(VT, DAG);
2683 if (isIdentityMask(PermMask.Val))
2685 else if (isIdentityMask(PermMask.Val, true))
2688 if (isSplatMask(PermMask.Val)) {
2689 if (NumElems <= 4) return Op;
2690 // Promote it to a v4i32 splat.
2691 return PromoteSplat(Op, DAG);
2694 if (X86::isMOVLMask(PermMask.Val))
2695 return (V1IsUndef) ? V2 : Op;
2697 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2698 X86::isMOVSLDUPMask(PermMask.Val) ||
2699 X86::isMOVHLPSMask(PermMask.Val) ||
2700 X86::isMOVHPMask(PermMask.Val) ||
2701 X86::isMOVLPMask(PermMask.Val))
2704 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2705 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2706 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2708 bool Commuted = false;
2709 V1IsSplat = isSplatVector(V1.Val);
2710 V2IsSplat = isSplatVector(V2.Val);
2711 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2712 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2713 std::swap(V1IsSplat, V2IsSplat);
2714 std::swap(V1IsUndef, V2IsUndef);
2718 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2719 if (V2IsUndef) return V1;
2720 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2722 // V2 is a splat, so the mask may be malformed. That is, it may point
2723 // to any V2 element. The instruction selectior won't like this. Get
2724 // a corrected mask and commute to form a proper MOVS{S|D}.
2725 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2726 if (NewMask.Val != PermMask.Val)
2727 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2732 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2733 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2734 X86::isUNPCKLMask(PermMask.Val) ||
2735 X86::isUNPCKHMask(PermMask.Val))
2739 // Normalize mask so all entries that point to V2 points to its first
2740 // element then try to match unpck{h|l} again. If match, return a
2741 // new vector_shuffle with the corrected mask.
2742 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2743 if (NewMask.Val != PermMask.Val) {
2744 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2745 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2746 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2747 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2748 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2749 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2754 // Normalize the node to match x86 shuffle ops if needed
2755 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2756 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2759 // Commute is back and try unpck* again.
2760 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2761 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2762 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2763 X86::isUNPCKLMask(PermMask.Val) ||
2764 X86::isUNPCKHMask(PermMask.Val))
2768 // If VT is integer, try PSHUF* first, then SHUFP*.
2769 if (MVT::isInteger(VT)) {
2770 if (X86::isPSHUFDMask(PermMask.Val) ||
2771 X86::isPSHUFHWMask(PermMask.Val) ||
2772 X86::isPSHUFLWMask(PermMask.Val)) {
2773 if (V2.getOpcode() != ISD::UNDEF)
2774 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2775 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2779 if (X86::isSHUFPMask(PermMask.Val) &&
2780 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
2783 // Handle v8i16 shuffle high / low shuffle node pair.
2784 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2785 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2786 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2787 SmallVector<SDOperand, 8> MaskVec;
2788 for (unsigned i = 0; i != 4; ++i)
2789 MaskVec.push_back(PermMask.getOperand(i));
2790 for (unsigned i = 4; i != 8; ++i)
2791 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2792 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2793 &MaskVec[0], MaskVec.size());
2794 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2796 for (unsigned i = 0; i != 4; ++i)
2797 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2798 for (unsigned i = 4; i != 8; ++i)
2799 MaskVec.push_back(PermMask.getOperand(i));
2800 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2801 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2804 // Floating point cases in the other order.
2805 if (X86::isSHUFPMask(PermMask.Val))
2807 if (X86::isPSHUFDMask(PermMask.Val) ||
2808 X86::isPSHUFHWMask(PermMask.Val) ||
2809 X86::isPSHUFLWMask(PermMask.Val)) {
2810 if (V2.getOpcode() != ISD::UNDEF)
2811 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2812 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2817 if (NumElems == 4 &&
2818 // Don't do this for MMX.
2819 MVT::getSizeInBits(VT) != 64) {
2820 MVT::ValueType MaskVT = PermMask.getValueType();
2821 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2822 SmallVector<std::pair<int, int>, 8> Locs;
2823 Locs.reserve(NumElems);
2824 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2825 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2828 // If no more than two elements come from either vector. This can be
2829 // implemented with two shuffles. First shuffle gather the elements.
2830 // The second shuffle, which takes the first shuffle as both of its
2831 // vector operands, put the elements into the right order.
2832 for (unsigned i = 0; i != NumElems; ++i) {
2833 SDOperand Elt = PermMask.getOperand(i);
2834 if (Elt.getOpcode() == ISD::UNDEF) {
2835 Locs[i] = std::make_pair(-1, -1);
2837 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2838 if (Val < NumElems) {
2839 Locs[i] = std::make_pair(0, NumLo);
2843 Locs[i] = std::make_pair(1, NumHi);
2844 if (2+NumHi < NumElems)
2845 Mask1[2+NumHi] = Elt;
2850 if (NumLo <= 2 && NumHi <= 2) {
2851 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2852 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2853 &Mask1[0], Mask1.size()));
2854 for (unsigned i = 0; i != NumElems; ++i) {
2855 if (Locs[i].first == -1)
2858 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2859 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2860 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2864 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2865 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2866 &Mask2[0], Mask2.size()));
2869 // Break it into (shuffle shuffle_hi, shuffle_lo).
2871 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2872 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2873 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
2874 unsigned MaskIdx = 0;
2876 unsigned HiIdx = NumElems/2;
2877 for (unsigned i = 0; i != NumElems; ++i) {
2878 if (i == NumElems/2) {
2884 SDOperand Elt = PermMask.getOperand(i);
2885 if (Elt.getOpcode() == ISD::UNDEF) {
2886 Locs[i] = std::make_pair(-1, -1);
2887 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2888 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2889 (*MaskPtr)[LoIdx] = Elt;
2892 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2893 (*MaskPtr)[HiIdx] = Elt;
2898 SDOperand LoShuffle =
2899 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2900 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2901 &LoMask[0], LoMask.size()));
2902 SDOperand HiShuffle =
2903 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2904 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2905 &HiMask[0], HiMask.size()));
2906 SmallVector<SDOperand, 8> MaskOps;
2907 for (unsigned i = 0; i != NumElems; ++i) {
2908 if (Locs[i].first == -1) {
2909 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2911 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2912 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2915 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2916 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2917 &MaskOps[0], MaskOps.size()));
2924 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2925 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2928 MVT::ValueType VT = Op.getValueType();
2929 // TODO: handle v16i8.
2930 if (MVT::getSizeInBits(VT) == 16) {
2931 // Transform it so it match pextrw which produces a 32-bit result.
2932 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2933 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2934 Op.getOperand(0), Op.getOperand(1));
2935 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2936 DAG.getValueType(VT));
2937 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2938 } else if (MVT::getSizeInBits(VT) == 32) {
2939 SDOperand Vec = Op.getOperand(0);
2940 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2943 // SHUFPS the element to the lowest double word, then movss.
2944 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2945 SmallVector<SDOperand, 8> IdxVec;
2946 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
2947 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2948 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2949 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2950 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2951 &IdxVec[0], IdxVec.size());
2952 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2953 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2954 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2955 DAG.getConstant(0, getPointerTy()));
2956 } else if (MVT::getSizeInBits(VT) == 64) {
2957 SDOperand Vec = Op.getOperand(0);
2958 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2962 // UNPCKHPD the element to the lowest double word, then movsd.
2963 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2964 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2965 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2966 SmallVector<SDOperand, 8> IdxVec;
2967 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
2968 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2969 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2970 &IdxVec[0], IdxVec.size());
2971 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2972 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2973 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2974 DAG.getConstant(0, getPointerTy()));
2981 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2982 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2983 // as its second argument.
2984 MVT::ValueType VT = Op.getValueType();
2985 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
2986 SDOperand N0 = Op.getOperand(0);
2987 SDOperand N1 = Op.getOperand(1);
2988 SDOperand N2 = Op.getOperand(2);
2989 if (MVT::getSizeInBits(BaseVT) == 16) {
2990 if (N1.getValueType() != MVT::i32)
2991 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2992 if (N2.getValueType() != MVT::i32)
2993 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
2994 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2995 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2996 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2999 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3000 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3001 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3002 SmallVector<SDOperand, 8> MaskVec;
3003 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3004 for (unsigned i = 1; i <= 3; ++i)
3005 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3006 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3007 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3008 &MaskVec[0], MaskVec.size()));
3010 // Use two pinsrw instructions to insert a 32 bit value.
3012 if (MVT::isFloatingPoint(N1.getValueType())) {
3013 if (ISD::isNON_EXTLoad(N1.Val)) {
3014 // Just load directly from f32mem to GR32.
3015 LoadSDNode *LD = cast<LoadSDNode>(N1);
3016 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3017 LD->getSrcValue(), LD->getSrcValueOffset());
3019 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3020 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3021 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3022 DAG.getConstant(0, getPointerTy()));
3025 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3026 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3027 DAG.getConstant(Idx, getPointerTy()));
3028 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3029 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3030 DAG.getConstant(Idx+1, getPointerTy()));
3031 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3039 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3040 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3041 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3044 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3045 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3046 // one of the above mentioned nodes. It has to be wrapped because otherwise
3047 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3048 // be used to form addressing mode. These wrapped nodes will be selected
3051 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3052 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3053 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3055 CP->getAlignment());
3056 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3057 // With PIC, the address is actually $g + Offset.
3058 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3059 !Subtarget->isPICStyleRIPRel()) {
3060 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3061 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3069 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3070 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3071 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3072 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3073 // With PIC, the address is actually $g + Offset.
3074 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3075 !Subtarget->isPICStyleRIPRel()) {
3076 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3077 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3081 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3082 // load the value at address GV, not the value of GV itself. This means that
3083 // the GlobalAddress must be in the base or index register of the address, not
3084 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3085 // The same applies for external symbols during PIC codegen
3086 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3087 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3092 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
3094 LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3095 const MVT::ValueType PtrVT) {
3097 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3098 DAG.getNode(X86ISD::GlobalBaseReg,
3100 InFlag = Chain.getValue(1);
3102 // emit leal symbol@TLSGD(,%ebx,1), %eax
3103 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3104 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3105 GA->getValueType(0),
3107 SDOperand Ops[] = { Chain, TGA, InFlag };
3108 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3109 InFlag = Result.getValue(2);
3110 Chain = Result.getValue(1);
3112 // call ___tls_get_addr. This function receives its argument in
3113 // the register EAX.
3114 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3115 InFlag = Chain.getValue(1);
3117 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3118 SDOperand Ops1[] = { Chain,
3119 DAG.getTargetExternalSymbol("___tls_get_addr",
3121 DAG.getRegister(X86::EAX, PtrVT),
3122 DAG.getRegister(X86::EBX, PtrVT),
3124 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3125 InFlag = Chain.getValue(1);
3127 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3130 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3131 // "local exec" model.
3133 LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3134 const MVT::ValueType PtrVT) {
3135 // Get the Thread Pointer
3136 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3137 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3139 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3140 GA->getValueType(0),
3142 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3144 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3145 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3147 // The address of the thread local variable is the add of the thread
3148 // pointer with the offset of the variable.
3149 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3153 X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3154 // TODO: implement the "local dynamic" model
3155 // TODO: implement the "initial exec"model for pic executables
3156 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3157 "TLS not implemented for non-ELF and 64-bit targets");
3158 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3159 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3160 // otherwise use the "Local Exec"TLS Model
3161 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3162 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3164 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3168 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3169 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3170 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3171 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3172 // With PIC, the address is actually $g + Offset.
3173 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3174 !Subtarget->isPICStyleRIPRel()) {
3175 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3176 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3183 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3184 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3185 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3186 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3187 // With PIC, the address is actually $g + Offset.
3188 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3189 !Subtarget->isPICStyleRIPRel()) {
3190 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3191 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3198 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3199 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3200 "Not an i64 shift!");
3201 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3202 SDOperand ShOpLo = Op.getOperand(0);
3203 SDOperand ShOpHi = Op.getOperand(1);
3204 SDOperand ShAmt = Op.getOperand(2);
3205 SDOperand Tmp1 = isSRA ?
3206 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3207 DAG.getConstant(0, MVT::i32);
3209 SDOperand Tmp2, Tmp3;
3210 if (Op.getOpcode() == ISD::SHL_PARTS) {
3211 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3212 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3214 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3215 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3218 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3219 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3220 DAG.getConstant(32, MVT::i8));
3221 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3222 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3225 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3227 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3228 SmallVector<SDOperand, 4> Ops;
3229 if (Op.getOpcode() == ISD::SHL_PARTS) {
3230 Ops.push_back(Tmp2);
3231 Ops.push_back(Tmp3);
3233 Ops.push_back(InFlag);
3234 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3235 InFlag = Hi.getValue(1);
3238 Ops.push_back(Tmp3);
3239 Ops.push_back(Tmp1);
3241 Ops.push_back(InFlag);
3242 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3244 Ops.push_back(Tmp2);
3245 Ops.push_back(Tmp3);
3247 Ops.push_back(InFlag);
3248 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3249 InFlag = Lo.getValue(1);
3252 Ops.push_back(Tmp3);
3253 Ops.push_back(Tmp1);
3255 Ops.push_back(InFlag);
3256 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3259 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3263 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3266 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3267 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3268 Op.getOperand(0).getValueType() >= MVT::i16 &&
3269 "Unknown SINT_TO_FP to lower!");
3272 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3273 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3274 MachineFunction &MF = DAG.getMachineFunction();
3275 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3276 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3277 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3278 StackSlot, NULL, 0);
3283 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3285 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
3286 SmallVector<SDOperand, 8> Ops;
3287 Ops.push_back(Chain);
3288 Ops.push_back(StackSlot);
3289 Ops.push_back(DAG.getValueType(SrcVT));
3290 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3291 Tys, &Ops[0], Ops.size());
3294 Chain = Result.getValue(1);
3295 SDOperand InFlag = Result.getValue(2);
3297 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3298 // shouldn't be necessary except that RFP cannot be live across
3299 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3300 MachineFunction &MF = DAG.getMachineFunction();
3301 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3302 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3303 Tys = DAG.getVTList(MVT::Other);
3304 SmallVector<SDOperand, 8> Ops;
3305 Ops.push_back(Chain);
3306 Ops.push_back(Result);
3307 Ops.push_back(StackSlot);
3308 Ops.push_back(DAG.getValueType(Op.getValueType()));
3309 Ops.push_back(InFlag);
3310 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3311 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3317 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3318 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3319 "Unknown FP_TO_SINT to lower!");
3320 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3322 MachineFunction &MF = DAG.getMachineFunction();
3323 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3324 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3325 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3328 switch (Op.getValueType()) {
3329 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3330 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3331 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3332 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3335 SDOperand Chain = DAG.getEntryNode();
3336 SDOperand Value = Op.getOperand(0);
3338 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3339 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3340 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
3342 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3344 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3345 Chain = Value.getValue(1);
3346 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3347 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3350 // Build the FP_TO_INT*_IN_MEM
3351 SDOperand Ops[] = { Chain, Value, StackSlot };
3352 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3355 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3358 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3359 MVT::ValueType VT = Op.getValueType();
3360 MVT::ValueType EltVT = VT;
3361 if (MVT::isVector(VT))
3362 EltVT = MVT::getVectorElementType(VT);
3363 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3364 std::vector<Constant*> CV;
3365 if (EltVT == MVT::f64) {
3366 Constant *C = ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)));
3370 Constant *C = ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)));
3376 Constant *C = ConstantVector::get(CV);
3377 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3378 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3380 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3383 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3384 MVT::ValueType VT = Op.getValueType();
3385 MVT::ValueType EltVT = VT;
3386 unsigned EltNum = 1;
3387 if (MVT::isVector(VT)) {
3388 EltVT = MVT::getVectorElementType(VT);
3389 EltNum = MVT::getVectorNumElements(VT);
3391 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3392 std::vector<Constant*> CV;
3393 if (EltVT == MVT::f64) {
3394 Constant *C = ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63));
3398 Constant *C = ConstantFP::get(OpNTy, BitsToFloat(1U << 31));
3404 Constant *C = ConstantVector::get(CV);
3405 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3406 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3408 if (MVT::isVector(VT)) {
3409 return DAG.getNode(ISD::BIT_CONVERT, VT,
3410 DAG.getNode(ISD::XOR, MVT::v2i64,
3411 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
3412 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
3414 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3418 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3419 SDOperand Op0 = Op.getOperand(0);
3420 SDOperand Op1 = Op.getOperand(1);
3421 MVT::ValueType VT = Op.getValueType();
3422 MVT::ValueType SrcVT = Op1.getValueType();
3423 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3425 // If second operand is smaller, extend it first.
3426 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3427 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3431 // First get the sign bit of second operand.
3432 std::vector<Constant*> CV;
3433 if (SrcVT == MVT::f64) {
3434 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3435 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3437 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3438 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3439 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3440 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3442 Constant *C = ConstantVector::get(CV);
3443 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3444 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
3446 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3448 // Shift sign bit right or left if the two operands have different types.
3449 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3450 // Op0 is MVT::f32, Op1 is MVT::f64.
3451 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3452 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3453 DAG.getConstant(32, MVT::i32));
3454 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3455 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3456 DAG.getConstant(0, getPointerTy()));
3459 // Clear first operand sign bit.
3461 if (VT == MVT::f64) {
3462 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3463 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3465 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3466 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3467 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3468 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3470 C = ConstantVector::get(CV);
3471 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3472 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3474 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3476 // Or the value with the sign bit.
3477 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3480 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3482 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3484 SDOperand Op0 = Op.getOperand(0);
3485 SDOperand Op1 = Op.getOperand(1);
3486 SDOperand CC = Op.getOperand(2);
3487 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3488 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3489 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3490 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3493 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3495 SDOperand Ops1[] = { Chain, Op0, Op1 };
3496 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3497 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3498 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3501 assert(isFP && "Illegal integer SetCC!");
3503 SDOperand COps[] = { Chain, Op0, Op1 };
3504 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3506 switch (SetCCOpcode) {
3507 default: assert(false && "Illegal floating point SetCC!");
3508 case ISD::SETOEQ: { // !PF & ZF
3509 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3510 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3511 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3513 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3514 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3516 case ISD::SETUNE: { // PF | !ZF
3517 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3518 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3519 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3521 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3522 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3527 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3528 bool addTest = true;
3529 SDOperand Chain = DAG.getEntryNode();
3530 SDOperand Cond = Op.getOperand(0);
3532 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3534 if (Cond.getOpcode() == ISD::SETCC)
3535 Cond = LowerSETCC(Cond, DAG, Chain);
3537 if (Cond.getOpcode() == X86ISD::SETCC) {
3538 CC = Cond.getOperand(0);
3540 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3541 // (since flag operand cannot be shared). Use it as the condition setting
3542 // operand in place of the X86ISD::SETCC.
3543 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3544 // to use a test instead of duplicating the X86ISD::CMP (for register
3545 // pressure reason)?
3546 SDOperand Cmp = Cond.getOperand(1);
3547 unsigned Opc = Cmp.getOpcode();
3548 bool IllegalFPCMov = !X86ScalarSSE &&
3549 MVT::isFloatingPoint(Op.getValueType()) &&
3550 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3551 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3553 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3554 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3560 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3561 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3562 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3565 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3566 SmallVector<SDOperand, 4> Ops;
3567 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3568 // condition is true.
3569 Ops.push_back(Op.getOperand(2));
3570 Ops.push_back(Op.getOperand(1));
3572 Ops.push_back(Cond.getValue(1));
3573 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3576 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3577 bool addTest = true;
3578 SDOperand Chain = Op.getOperand(0);
3579 SDOperand Cond = Op.getOperand(1);
3580 SDOperand Dest = Op.getOperand(2);
3582 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3584 if (Cond.getOpcode() == ISD::SETCC)
3585 Cond = LowerSETCC(Cond, DAG, Chain);
3587 if (Cond.getOpcode() == X86ISD::SETCC) {
3588 CC = Cond.getOperand(0);
3590 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3591 // (since flag operand cannot be shared). Use it as the condition setting
3592 // operand in place of the X86ISD::SETCC.
3593 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3594 // to use a test instead of duplicating the X86ISD::CMP (for register
3595 // pressure reason)?
3596 SDOperand Cmp = Cond.getOperand(1);
3597 unsigned Opc = Cmp.getOpcode();
3598 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3599 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3600 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3606 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3607 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3608 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3610 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3611 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3614 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3615 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3617 if (Subtarget->is64Bit())
3618 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3620 switch (CallingConv) {
3622 assert(0 && "Unsupported calling convention");
3623 case CallingConv::Fast:
3624 // TODO: Implement fastcc
3626 case CallingConv::C:
3627 case CallingConv::X86_StdCall:
3628 return LowerCCCCallTo(Op, DAG, CallingConv);
3629 case CallingConv::X86_FastCall:
3630 return LowerFastCCCallTo(Op, DAG, CallingConv);
3635 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3636 // Calls to _alloca is needed to probe the stack when allocating more than 4k
3637 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
3638 // that the guard pages used by the OS virtual memory manager are allocated in
3639 // correct sequence.
3641 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3642 SelectionDAG &DAG) {
3643 assert(Subtarget->isTargetCygMing() &&
3644 "This should be used only on Cygwin/Mingw targets");
3647 SDOperand Chain = Op.getOperand(0);
3648 SDOperand Size = Op.getOperand(1);
3649 // FIXME: Ensure alignment here
3653 MVT::ValueType IntPtr = getPointerTy();
3654 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
3656 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
3657 Flag = Chain.getValue(1);
3659 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3660 SDOperand Ops[] = { Chain,
3661 DAG.getTargetExternalSymbol("_alloca", IntPtr),
3662 DAG.getRegister(X86::EAX, IntPtr),
3664 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
3665 Flag = Chain.getValue(1);
3667 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
3669 std::vector<MVT::ValueType> Tys;
3670 Tys.push_back(SPTy);
3671 Tys.push_back(MVT::Other);
3672 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
3673 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
3677 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3678 MachineFunction &MF = DAG.getMachineFunction();
3679 const Function* Fn = MF.getFunction();
3680 if (Fn->hasExternalLinkage() &&
3681 Subtarget->isTargetCygMing() &&
3682 Fn->getName() == "main")
3683 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
3685 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3686 if (Subtarget->is64Bit())
3687 return LowerX86_64CCCArguments(Op, DAG);
3691 assert(0 && "Unsupported calling convention");
3692 case CallingConv::Fast:
3693 // TODO: implement fastcc.
3696 case CallingConv::C:
3697 return LowerCCCArguments(Op, DAG);
3698 case CallingConv::X86_StdCall:
3699 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
3700 return LowerCCCArguments(Op, DAG, true);
3701 case CallingConv::X86_FastCall:
3702 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
3703 return LowerFastCCArguments(Op, DAG);
3707 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3708 SDOperand InFlag(0, 0);
3709 SDOperand Chain = Op.getOperand(0);
3711 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3712 if (Align == 0) Align = 1;
3714 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3715 // If not DWORD aligned, call memset if size is less than the threshold.
3716 // It knows how to align to the right boundary first.
3717 if ((Align & 3) != 0 ||
3718 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3719 MVT::ValueType IntPtr = getPointerTy();
3720 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3721 TargetLowering::ArgListTy Args;
3722 TargetLowering::ArgListEntry Entry;
3723 Entry.Node = Op.getOperand(1);
3724 Entry.Ty = IntPtrTy;
3725 Args.push_back(Entry);
3726 // Extend the unsigned i8 argument to be an int value for the call.
3727 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3728 Entry.Ty = IntPtrTy;
3729 Args.push_back(Entry);
3730 Entry.Node = Op.getOperand(3);
3731 Args.push_back(Entry);
3732 std::pair<SDOperand,SDOperand> CallResult =
3733 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3734 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3735 return CallResult.second;
3740 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3741 unsigned BytesLeft = 0;
3742 bool TwoRepStos = false;
3745 uint64_t Val = ValC->getValue() & 255;
3747 // If the value is a constant, then we can potentially use larger sets.
3748 switch (Align & 3) {
3749 case 2: // WORD aligned
3752 Val = (Val << 8) | Val;
3754 case 0: // DWORD aligned
3757 Val = (Val << 8) | Val;
3758 Val = (Val << 16) | Val;
3759 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3762 Val = (Val << 32) | Val;
3765 default: // Byte aligned
3768 Count = Op.getOperand(3);
3772 if (AVT > MVT::i8) {
3774 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3775 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3776 BytesLeft = I->getValue() % UBytes;
3778 assert(AVT >= MVT::i32 &&
3779 "Do not use rep;stos if not at least DWORD aligned");
3780 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3781 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3786 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3788 InFlag = Chain.getValue(1);
3791 Count = Op.getOperand(3);
3792 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3793 InFlag = Chain.getValue(1);
3796 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3798 InFlag = Chain.getValue(1);
3799 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3800 Op.getOperand(1), InFlag);
3801 InFlag = Chain.getValue(1);
3803 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3804 SmallVector<SDOperand, 8> Ops;
3805 Ops.push_back(Chain);
3806 Ops.push_back(DAG.getValueType(AVT));
3807 Ops.push_back(InFlag);
3808 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3811 InFlag = Chain.getValue(1);
3812 Count = Op.getOperand(3);
3813 MVT::ValueType CVT = Count.getValueType();
3814 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3815 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3816 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3818 InFlag = Chain.getValue(1);
3819 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3821 Ops.push_back(Chain);
3822 Ops.push_back(DAG.getValueType(MVT::i8));
3823 Ops.push_back(InFlag);
3824 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3825 } else if (BytesLeft) {
3826 // Issue stores for the last 1 - 7 bytes.
3828 unsigned Val = ValC->getValue() & 255;
3829 unsigned Offset = I->getValue() - BytesLeft;
3830 SDOperand DstAddr = Op.getOperand(1);
3831 MVT::ValueType AddrVT = DstAddr.getValueType();
3832 if (BytesLeft >= 4) {
3833 Val = (Val << 8) | Val;
3834 Val = (Val << 16) | Val;
3835 Value = DAG.getConstant(Val, MVT::i32);
3836 Chain = DAG.getStore(Chain, Value,
3837 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3838 DAG.getConstant(Offset, AddrVT)),
3843 if (BytesLeft >= 2) {
3844 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3845 Chain = DAG.getStore(Chain, Value,
3846 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3847 DAG.getConstant(Offset, AddrVT)),
3852 if (BytesLeft == 1) {
3853 Value = DAG.getConstant(Val, MVT::i8);
3854 Chain = DAG.getStore(Chain, Value,
3855 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3856 DAG.getConstant(Offset, AddrVT)),
3864 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3865 SDOperand Chain = Op.getOperand(0);
3867 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3868 if (Align == 0) Align = 1;
3870 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3871 // If not DWORD aligned, call memcpy if size is less than the threshold.
3872 // It knows how to align to the right boundary first.
3873 if ((Align & 3) != 0 ||
3874 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3875 MVT::ValueType IntPtr = getPointerTy();
3876 TargetLowering::ArgListTy Args;
3877 TargetLowering::ArgListEntry Entry;
3878 Entry.Ty = getTargetData()->getIntPtrType();
3879 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3880 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3881 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
3882 std::pair<SDOperand,SDOperand> CallResult =
3883 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3884 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3885 return CallResult.second;
3890 unsigned BytesLeft = 0;
3891 bool TwoRepMovs = false;
3892 switch (Align & 3) {
3893 case 2: // WORD aligned
3896 case 0: // DWORD aligned
3898 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3901 default: // Byte aligned
3903 Count = Op.getOperand(3);
3907 if (AVT > MVT::i8) {
3909 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3910 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3911 BytesLeft = I->getValue() % UBytes;
3913 assert(AVT >= MVT::i32 &&
3914 "Do not use rep;movs if not at least DWORD aligned");
3915 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3916 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3921 SDOperand InFlag(0, 0);
3922 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3924 InFlag = Chain.getValue(1);
3925 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3926 Op.getOperand(1), InFlag);
3927 InFlag = Chain.getValue(1);
3928 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3929 Op.getOperand(2), InFlag);
3930 InFlag = Chain.getValue(1);
3932 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3933 SmallVector<SDOperand, 8> Ops;
3934 Ops.push_back(Chain);
3935 Ops.push_back(DAG.getValueType(AVT));
3936 Ops.push_back(InFlag);
3937 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3940 InFlag = Chain.getValue(1);
3941 Count = Op.getOperand(3);
3942 MVT::ValueType CVT = Count.getValueType();
3943 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3944 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3945 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3947 InFlag = Chain.getValue(1);
3948 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3950 Ops.push_back(Chain);
3951 Ops.push_back(DAG.getValueType(MVT::i8));
3952 Ops.push_back(InFlag);
3953 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3954 } else if (BytesLeft) {
3955 // Issue loads and stores for the last 1 - 7 bytes.
3956 unsigned Offset = I->getValue() - BytesLeft;
3957 SDOperand DstAddr = Op.getOperand(1);
3958 MVT::ValueType DstVT = DstAddr.getValueType();
3959 SDOperand SrcAddr = Op.getOperand(2);
3960 MVT::ValueType SrcVT = SrcAddr.getValueType();
3962 if (BytesLeft >= 4) {
3963 Value = DAG.getLoad(MVT::i32, Chain,
3964 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3965 DAG.getConstant(Offset, SrcVT)),
3967 Chain = Value.getValue(1);
3968 Chain = DAG.getStore(Chain, Value,
3969 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3970 DAG.getConstant(Offset, DstVT)),
3975 if (BytesLeft >= 2) {
3976 Value = DAG.getLoad(MVT::i16, Chain,
3977 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3978 DAG.getConstant(Offset, SrcVT)),
3980 Chain = Value.getValue(1);
3981 Chain = DAG.getStore(Chain, Value,
3982 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3983 DAG.getConstant(Offset, DstVT)),
3989 if (BytesLeft == 1) {
3990 Value = DAG.getLoad(MVT::i8, Chain,
3991 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3992 DAG.getConstant(Offset, SrcVT)),
3994 Chain = Value.getValue(1);
3995 Chain = DAG.getStore(Chain, Value,
3996 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3997 DAG.getConstant(Offset, DstVT)),
4006 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4007 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4008 SDOperand TheOp = Op.getOperand(0);
4009 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
4010 if (Subtarget->is64Bit()) {
4011 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4012 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4013 MVT::i64, Copy1.getValue(2));
4014 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4015 DAG.getConstant(32, MVT::i8));
4017 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4020 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4021 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
4024 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4025 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4026 MVT::i32, Copy1.getValue(2));
4027 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4028 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4029 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
4032 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4033 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4035 if (!Subtarget->is64Bit()) {
4036 // vastart just stores the address of the VarArgsFrameIndex slot into the
4037 // memory location argument.
4038 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4039 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4044 // gp_offset (0 - 6 * 8)
4045 // fp_offset (48 - 48 + 8 * 16)
4046 // overflow_arg_area (point to parameters coming in memory).
4048 SmallVector<SDOperand, 8> MemOps;
4049 SDOperand FIN = Op.getOperand(1);
4051 SDOperand Store = DAG.getStore(Op.getOperand(0),
4052 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4053 FIN, SV->getValue(), SV->getOffset());
4054 MemOps.push_back(Store);
4057 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4058 DAG.getConstant(4, getPointerTy()));
4059 Store = DAG.getStore(Op.getOperand(0),
4060 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4061 FIN, SV->getValue(), SV->getOffset());
4062 MemOps.push_back(Store);
4064 // Store ptr to overflow_arg_area
4065 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4066 DAG.getConstant(4, getPointerTy()));
4067 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4068 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4070 MemOps.push_back(Store);
4072 // Store ptr to reg_save_area.
4073 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4074 DAG.getConstant(8, getPointerTy()));
4075 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4076 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4078 MemOps.push_back(Store);
4079 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4082 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4083 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4084 SDOperand Chain = Op.getOperand(0);
4085 SDOperand DstPtr = Op.getOperand(1);
4086 SDOperand SrcPtr = Op.getOperand(2);
4087 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4088 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4090 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4091 SrcSV->getValue(), SrcSV->getOffset());
4092 Chain = SrcPtr.getValue(1);
4093 for (unsigned i = 0; i < 3; ++i) {
4094 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4095 SrcSV->getValue(), SrcSV->getOffset());
4096 Chain = Val.getValue(1);
4097 Chain = DAG.getStore(Chain, Val, DstPtr,
4098 DstSV->getValue(), DstSV->getOffset());
4101 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4102 DAG.getConstant(8, getPointerTy()));
4103 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4104 DAG.getConstant(8, getPointerTy()));
4110 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4111 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4113 default: return SDOperand(); // Don't custom lower most intrinsics.
4114 // Comparison intrinsics.
4115 case Intrinsic::x86_sse_comieq_ss:
4116 case Intrinsic::x86_sse_comilt_ss:
4117 case Intrinsic::x86_sse_comile_ss:
4118 case Intrinsic::x86_sse_comigt_ss:
4119 case Intrinsic::x86_sse_comige_ss:
4120 case Intrinsic::x86_sse_comineq_ss:
4121 case Intrinsic::x86_sse_ucomieq_ss:
4122 case Intrinsic::x86_sse_ucomilt_ss:
4123 case Intrinsic::x86_sse_ucomile_ss:
4124 case Intrinsic::x86_sse_ucomigt_ss:
4125 case Intrinsic::x86_sse_ucomige_ss:
4126 case Intrinsic::x86_sse_ucomineq_ss:
4127 case Intrinsic::x86_sse2_comieq_sd:
4128 case Intrinsic::x86_sse2_comilt_sd:
4129 case Intrinsic::x86_sse2_comile_sd:
4130 case Intrinsic::x86_sse2_comigt_sd:
4131 case Intrinsic::x86_sse2_comige_sd:
4132 case Intrinsic::x86_sse2_comineq_sd:
4133 case Intrinsic::x86_sse2_ucomieq_sd:
4134 case Intrinsic::x86_sse2_ucomilt_sd:
4135 case Intrinsic::x86_sse2_ucomile_sd:
4136 case Intrinsic::x86_sse2_ucomigt_sd:
4137 case Intrinsic::x86_sse2_ucomige_sd:
4138 case Intrinsic::x86_sse2_ucomineq_sd: {
4140 ISD::CondCode CC = ISD::SETCC_INVALID;
4143 case Intrinsic::x86_sse_comieq_ss:
4144 case Intrinsic::x86_sse2_comieq_sd:
4148 case Intrinsic::x86_sse_comilt_ss:
4149 case Intrinsic::x86_sse2_comilt_sd:
4153 case Intrinsic::x86_sse_comile_ss:
4154 case Intrinsic::x86_sse2_comile_sd:
4158 case Intrinsic::x86_sse_comigt_ss:
4159 case Intrinsic::x86_sse2_comigt_sd:
4163 case Intrinsic::x86_sse_comige_ss:
4164 case Intrinsic::x86_sse2_comige_sd:
4168 case Intrinsic::x86_sse_comineq_ss:
4169 case Intrinsic::x86_sse2_comineq_sd:
4173 case Intrinsic::x86_sse_ucomieq_ss:
4174 case Intrinsic::x86_sse2_ucomieq_sd:
4175 Opc = X86ISD::UCOMI;
4178 case Intrinsic::x86_sse_ucomilt_ss:
4179 case Intrinsic::x86_sse2_ucomilt_sd:
4180 Opc = X86ISD::UCOMI;
4183 case Intrinsic::x86_sse_ucomile_ss:
4184 case Intrinsic::x86_sse2_ucomile_sd:
4185 Opc = X86ISD::UCOMI;
4188 case Intrinsic::x86_sse_ucomigt_ss:
4189 case Intrinsic::x86_sse2_ucomigt_sd:
4190 Opc = X86ISD::UCOMI;
4193 case Intrinsic::x86_sse_ucomige_ss:
4194 case Intrinsic::x86_sse2_ucomige_sd:
4195 Opc = X86ISD::UCOMI;
4198 case Intrinsic::x86_sse_ucomineq_ss:
4199 case Intrinsic::x86_sse2_ucomineq_sd:
4200 Opc = X86ISD::UCOMI;
4206 SDOperand LHS = Op.getOperand(1);
4207 SDOperand RHS = Op.getOperand(2);
4208 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4210 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4211 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4212 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4213 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4214 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4215 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4216 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4221 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4222 // Depths > 0 not supported yet!
4223 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4226 // Just load the return address
4227 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4228 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4231 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4232 // Depths > 0 not supported yet!
4233 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4236 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4237 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4238 DAG.getConstant(4, getPointerTy()));
4241 SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4242 SelectionDAG &DAG) {
4243 // Is not yet supported on x86-64
4244 if (Subtarget->is64Bit())
4247 return DAG.getConstant(8, getPointerTy());
4250 SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4252 assert(!Subtarget->is64Bit() &&
4253 "Lowering of eh_return builtin is not supported yet on x86-64");
4255 MachineFunction &MF = DAG.getMachineFunction();
4256 SDOperand Chain = Op.getOperand(0);
4257 SDOperand Offset = Op.getOperand(1);
4258 SDOperand Handler = Op.getOperand(2);
4260 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4263 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4264 DAG.getConstant(-4UL, getPointerTy()));
4265 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4266 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4267 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4268 MF.addLiveOut(X86::ECX);
4270 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4271 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4274 SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4275 SelectionDAG &DAG) {
4276 SDOperand Root = Op.getOperand(0);
4277 SDOperand Trmp = Op.getOperand(1); // trampoline
4278 SDOperand FPtr = Op.getOperand(2); // nested function
4279 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4281 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4283 if (Subtarget->is64Bit()) {
4284 return SDOperand(); // not yet supported
4286 Function *Func = (Function *)
4287 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4288 unsigned CC = Func->getCallingConv();
4289 unsigned char NestReg;
4293 assert(0 && "Unsupported calling convention");
4294 case CallingConv::C:
4295 case CallingConv::Fast:
4296 case CallingConv::X86_StdCall: {
4297 // Pass 'nest' parameter in ECX.
4298 // Must be kept in sync with X86CallingConv.td
4301 // Check that ECX wasn't needed by an 'inreg' parameter.
4302 const FunctionType *FTy = Func->getFunctionType();
4303 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4305 if (Attrs && !Func->isVarArg()) {
4306 unsigned InRegCount = 0;
4309 for (FunctionType::param_iterator I = FTy->param_begin(),
4310 E = FTy->param_end(); I != E; ++I, ++Idx)
4311 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4312 // FIXME: should only count parameters that are lowered to integers.
4313 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4315 if (InRegCount > 2) {
4316 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4322 case CallingConv::X86_FastCall:
4323 // Pass 'nest' parameter in EAX.
4324 // Must be kept in sync with X86CallingConv.td
4329 SDOperand OutChains[4];
4330 SDOperand Addr, Disp;
4332 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4333 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4335 const unsigned char MOV32ri = 0xB8;
4336 const unsigned char JMP = 0xE9;
4338 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|NestReg, MVT::i8),
4339 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4341 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4342 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4343 TrmpSV->getOffset() + 1, false, 1);
4345 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4346 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4347 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4349 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4350 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4351 TrmpSV->getOffset() + 6, false, 1);
4353 return DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4);
4357 /// LowerOperation - Provide custom lowering hooks for some operations.
4359 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4360 switch (Op.getOpcode()) {
4361 default: assert(0 && "Should not custom lower this!");
4362 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4363 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4364 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4365 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4366 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4367 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4368 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4369 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
4370 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4371 case ISD::SHL_PARTS:
4372 case ISD::SRA_PARTS:
4373 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4374 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4375 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4376 case ISD::FABS: return LowerFABS(Op, DAG);
4377 case ISD::FNEG: return LowerFNEG(Op, DAG);
4378 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
4379 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
4380 case ISD::SELECT: return LowerSELECT(Op, DAG);
4381 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4382 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4383 case ISD::CALL: return LowerCALL(Op, DAG);
4384 case ISD::RET: return LowerRET(Op, DAG);
4385 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4386 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4387 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4388 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4389 case ISD::VASTART: return LowerVASTART(Op, DAG);
4390 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
4391 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4392 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4393 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4394 case ISD::FRAME_TO_ARGS_OFFSET:
4395 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
4396 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
4397 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
4398 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
4403 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4405 default: return NULL;
4406 case X86ISD::SHLD: return "X86ISD::SHLD";
4407 case X86ISD::SHRD: return "X86ISD::SHRD";
4408 case X86ISD::FAND: return "X86ISD::FAND";
4409 case X86ISD::FOR: return "X86ISD::FOR";
4410 case X86ISD::FXOR: return "X86ISD::FXOR";
4411 case X86ISD::FSRL: return "X86ISD::FSRL";
4412 case X86ISD::FILD: return "X86ISD::FILD";
4413 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4414 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4415 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4416 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4417 case X86ISD::FLD: return "X86ISD::FLD";
4418 case X86ISD::FST: return "X86ISD::FST";
4419 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4420 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4421 case X86ISD::CALL: return "X86ISD::CALL";
4422 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4423 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4424 case X86ISD::CMP: return "X86ISD::CMP";
4425 case X86ISD::COMI: return "X86ISD::COMI";
4426 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4427 case X86ISD::SETCC: return "X86ISD::SETCC";
4428 case X86ISD::CMOV: return "X86ISD::CMOV";
4429 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4430 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4431 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4432 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4433 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4434 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4435 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4436 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4437 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4438 case X86ISD::FMAX: return "X86ISD::FMAX";
4439 case X86ISD::FMIN: return "X86ISD::FMIN";
4440 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
4441 case X86ISD::FRCP: return "X86ISD::FRCP";
4442 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4443 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
4444 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
4448 // isLegalAddressingMode - Return true if the addressing mode represented
4449 // by AM is legal for this target, for a load/store of the specified type.
4450 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4451 const Type *Ty) const {
4452 // X86 supports extremely general addressing modes.
4454 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4455 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4459 // X86-64 only supports addr of globals in small code model.
4460 if (Subtarget->is64Bit() &&
4461 getTargetMachine().getCodeModel() != CodeModel::Small)
4464 // We can only fold this if we don't need a load either.
4465 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4475 // These scales always work.
4480 // These scales are formed with basereg+scalereg. Only accept if there is
4485 default: // Other stuff never works.
4493 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4494 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4495 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4496 /// are assumed to be legal.
4498 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4499 // Only do shuffles on 128-bit vector types for now.
4500 if (MVT::getSizeInBits(VT) == 64) return false;
4501 return (Mask.Val->getNumOperands() <= 4 ||
4502 isIdentityMask(Mask.Val) ||
4503 isIdentityMask(Mask.Val, true) ||
4504 isSplatMask(Mask.Val) ||
4505 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4506 X86::isUNPCKLMask(Mask.Val) ||
4507 X86::isUNPCKHMask(Mask.Val) ||
4508 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4509 X86::isUNPCKH_v_undef_Mask(Mask.Val));
4512 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4514 SelectionDAG &DAG) const {
4515 unsigned NumElts = BVOps.size();
4516 // Only do shuffles on 128-bit vector types for now.
4517 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4518 if (NumElts == 2) return true;
4520 return (isMOVLMask(&BVOps[0], 4) ||
4521 isCommutedMOVL(&BVOps[0], 4, true) ||
4522 isSHUFPMask(&BVOps[0], 4) ||
4523 isCommutedSHUFP(&BVOps[0], 4));
4528 //===----------------------------------------------------------------------===//
4529 // X86 Scheduler Hooks
4530 //===----------------------------------------------------------------------===//
4533 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4534 MachineBasicBlock *BB) {
4535 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4536 switch (MI->getOpcode()) {
4537 default: assert(false && "Unexpected instr type to insert");
4538 case X86::CMOV_FR32:
4539 case X86::CMOV_FR64:
4540 case X86::CMOV_V4F32:
4541 case X86::CMOV_V2F64:
4542 case X86::CMOV_V2I64: {
4543 // To "insert" a SELECT_CC instruction, we actually have to insert the
4544 // diamond control-flow pattern. The incoming instruction knows the
4545 // destination vreg to set, the condition code register to branch on, the
4546 // true/false values to select between, and a branch opcode to use.
4547 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4548 ilist<MachineBasicBlock>::iterator It = BB;
4554 // cmpTY ccX, r1, r2
4556 // fallthrough --> copy0MBB
4557 MachineBasicBlock *thisMBB = BB;
4558 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4559 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4561 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4562 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4563 MachineFunction *F = BB->getParent();
4564 F->getBasicBlockList().insert(It, copy0MBB);
4565 F->getBasicBlockList().insert(It, sinkMBB);
4566 // Update machine-CFG edges by first adding all successors of the current
4567 // block to the new block which will contain the Phi node for the select.
4568 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4569 e = BB->succ_end(); i != e; ++i)
4570 sinkMBB->addSuccessor(*i);
4571 // Next, remove all successors of the current block, and add the true
4572 // and fallthrough blocks as its successors.
4573 while(!BB->succ_empty())
4574 BB->removeSuccessor(BB->succ_begin());
4575 BB->addSuccessor(copy0MBB);
4576 BB->addSuccessor(sinkMBB);
4579 // %FalseValue = ...
4580 // # fallthrough to sinkMBB
4583 // Update machine-CFG edges
4584 BB->addSuccessor(sinkMBB);
4587 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4590 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4591 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4592 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4594 delete MI; // The pseudo instruction is gone now.
4598 case X86::FP32_TO_INT16_IN_MEM:
4599 case X86::FP32_TO_INT32_IN_MEM:
4600 case X86::FP32_TO_INT64_IN_MEM:
4601 case X86::FP64_TO_INT16_IN_MEM:
4602 case X86::FP64_TO_INT32_IN_MEM:
4603 case X86::FP64_TO_INT64_IN_MEM: {
4604 // Change the floating point control register to use "round towards zero"
4605 // mode when truncating to an integer value.
4606 MachineFunction *F = BB->getParent();
4607 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4608 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4610 // Load the old value of the high byte of the control word...
4612 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4613 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4615 // Set the high part to be round to zero...
4616 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4619 // Reload the modified control word now...
4620 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4622 // Restore the memory image of control word to original value
4623 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4626 // Get the X86 opcode to use.
4628 switch (MI->getOpcode()) {
4629 default: assert(0 && "illegal opcode!");
4630 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
4631 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
4632 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
4633 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
4634 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
4635 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
4639 MachineOperand &Op = MI->getOperand(0);
4640 if (Op.isRegister()) {
4641 AM.BaseType = X86AddressMode::RegBase;
4642 AM.Base.Reg = Op.getReg();
4644 AM.BaseType = X86AddressMode::FrameIndexBase;
4645 AM.Base.FrameIndex = Op.getFrameIndex();
4647 Op = MI->getOperand(1);
4648 if (Op.isImmediate())
4649 AM.Scale = Op.getImm();
4650 Op = MI->getOperand(2);
4651 if (Op.isImmediate())
4652 AM.IndexReg = Op.getImm();
4653 Op = MI->getOperand(3);
4654 if (Op.isGlobalAddress()) {
4655 AM.GV = Op.getGlobal();
4657 AM.Disp = Op.getImm();
4659 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4660 .addReg(MI->getOperand(4).getReg());
4662 // Reload the original control word now.
4663 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4665 delete MI; // The pseudo instruction is gone now.
4671 //===----------------------------------------------------------------------===//
4672 // X86 Optimization Hooks
4673 //===----------------------------------------------------------------------===//
4675 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4677 uint64_t &KnownZero,
4679 const SelectionDAG &DAG,
4680 unsigned Depth) const {
4681 unsigned Opc = Op.getOpcode();
4682 assert((Opc >= ISD::BUILTIN_OP_END ||
4683 Opc == ISD::INTRINSIC_WO_CHAIN ||
4684 Opc == ISD::INTRINSIC_W_CHAIN ||
4685 Opc == ISD::INTRINSIC_VOID) &&
4686 "Should use MaskedValueIsZero if you don't know whether Op"
4687 " is a target node!");
4689 KnownZero = KnownOne = 0; // Don't know anything.
4693 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4698 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
4699 /// element of the result of the vector shuffle.
4700 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4701 MVT::ValueType VT = N->getValueType(0);
4702 SDOperand PermMask = N->getOperand(2);
4703 unsigned NumElems = PermMask.getNumOperands();
4704 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4706 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4708 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4709 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4710 SDOperand Idx = PermMask.getOperand(i);
4711 if (Idx.getOpcode() == ISD::UNDEF)
4712 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4713 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4718 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4719 /// node is a GlobalAddress + an offset.
4720 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4721 unsigned Opc = N->getOpcode();
4722 if (Opc == X86ISD::Wrapper) {
4723 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4724 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4727 } else if (Opc == ISD::ADD) {
4728 SDOperand N1 = N->getOperand(0);
4729 SDOperand N2 = N->getOperand(1);
4730 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4731 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4733 Offset += V->getSignExtended();
4736 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4737 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4739 Offset += V->getSignExtended();
4747 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4749 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4750 MachineFrameInfo *MFI) {
4751 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4754 SDOperand Loc = N->getOperand(1);
4755 SDOperand BaseLoc = Base->getOperand(1);
4756 if (Loc.getOpcode() == ISD::FrameIndex) {
4757 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4759 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
4760 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4761 int FS = MFI->getObjectSize(FI);
4762 int BFS = MFI->getObjectSize(BFI);
4763 if (FS != BFS || FS != Size) return false;
4764 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4766 GlobalValue *GV1 = NULL;
4767 GlobalValue *GV2 = NULL;
4768 int64_t Offset1 = 0;
4769 int64_t Offset2 = 0;
4770 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4771 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4772 if (isGA1 && isGA2 && GV1 == GV2)
4773 return Offset1 == (Offset2 + Dist*Size);
4779 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4780 const X86Subtarget *Subtarget) {
4783 if (isGAPlusOffset(Base, GV, Offset))
4784 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4786 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4787 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
4789 // Fixed objects do not specify alignment, however the offsets are known.
4790 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4791 (MFI->getObjectOffset(BFI) % 16) == 0);
4793 return MFI->getObjectAlignment(BFI) >= 16;
4799 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4800 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4801 /// if the load addresses are consecutive, non-overlapping, and in the right
4803 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4804 const X86Subtarget *Subtarget) {
4805 MachineFunction &MF = DAG.getMachineFunction();
4806 MachineFrameInfo *MFI = MF.getFrameInfo();
4807 MVT::ValueType VT = N->getValueType(0);
4808 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4809 SDOperand PermMask = N->getOperand(2);
4810 int NumElems = (int)PermMask.getNumOperands();
4811 SDNode *Base = NULL;
4812 for (int i = 0; i < NumElems; ++i) {
4813 SDOperand Idx = PermMask.getOperand(i);
4814 if (Idx.getOpcode() == ISD::UNDEF) {
4815 if (!Base) return SDOperand();
4818 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4819 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
4823 else if (!isConsecutiveLoad(Arg.Val, Base,
4824 i, MVT::getSizeInBits(EVT)/8,MFI))
4829 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
4830 LoadSDNode *LD = cast<LoadSDNode>(Base);
4832 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4833 LD->getSrcValueOffset(), LD->isVolatile());
4835 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4836 LD->getSrcValueOffset(), LD->isVolatile(),
4837 LD->getAlignment());
4841 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4842 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4843 const X86Subtarget *Subtarget) {
4844 SDOperand Cond = N->getOperand(0);
4846 // If we have SSE[12] support, try to form min/max nodes.
4847 if (Subtarget->hasSSE2() &&
4848 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4849 if (Cond.getOpcode() == ISD::SETCC) {
4850 // Get the LHS/RHS of the select.
4851 SDOperand LHS = N->getOperand(1);
4852 SDOperand RHS = N->getOperand(2);
4853 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
4855 unsigned Opcode = 0;
4856 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
4859 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4862 if (!UnsafeFPMath) break;
4864 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4866 Opcode = X86ISD::FMIN;
4869 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4872 if (!UnsafeFPMath) break;
4874 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4876 Opcode = X86ISD::FMAX;
4879 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
4882 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4885 if (!UnsafeFPMath) break;
4887 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4889 Opcode = X86ISD::FMIN;
4892 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4895 if (!UnsafeFPMath) break;
4897 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4899 Opcode = X86ISD::FMAX;
4905 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
4914 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4915 DAGCombinerInfo &DCI) const {
4916 SelectionDAG &DAG = DCI.DAG;
4917 switch (N->getOpcode()) {
4919 case ISD::VECTOR_SHUFFLE:
4920 return PerformShuffleCombine(N, DAG, Subtarget);
4922 return PerformSELECTCombine(N, DAG, Subtarget);
4928 //===----------------------------------------------------------------------===//
4929 // X86 Inline Assembly Support
4930 //===----------------------------------------------------------------------===//
4932 /// getConstraintType - Given a constraint letter, return the type of
4933 /// constraint it is for this target.
4934 X86TargetLowering::ConstraintType
4935 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4936 if (Constraint.size() == 1) {
4937 switch (Constraint[0]) {
4946 return C_RegisterClass;
4951 return TargetLowering::getConstraintType(Constraint);
4954 /// isOperandValidForConstraint - Return the specified operand (possibly
4955 /// modified) if the specified SDOperand is valid for the specified target
4956 /// constraint letter, otherwise return null.
4957 SDOperand X86TargetLowering::
4958 isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4959 switch (Constraint) {
4962 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4963 if (C->getValue() <= 31)
4964 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
4966 return SDOperand(0,0);
4968 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4969 if (C->getValue() <= 255)
4970 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
4972 return SDOperand(0,0);
4974 // Literal immediates are always ok.
4975 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op))
4976 return DAG.getTargetConstant(CST->getValue(), Op.getValueType());
4978 // If we are in non-pic codegen mode, we allow the address of a global (with
4979 // an optional displacement) to be used with 'i'.
4980 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
4983 // Match either (GA) or (GA+C)
4985 Offset = GA->getOffset();
4986 } else if (Op.getOpcode() == ISD::ADD) {
4987 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4988 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4990 Offset = GA->getOffset()+C->getValue();
4992 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4993 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4995 Offset = GA->getOffset()+C->getValue();
5002 // If addressing this global requires a load (e.g. in PIC mode), we can't
5004 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5006 return SDOperand(0, 0);
5008 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5013 // Otherwise, not valid for this mode.
5014 return SDOperand(0, 0);
5017 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
5020 std::vector<unsigned> X86TargetLowering::
5021 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5022 MVT::ValueType VT) const {
5023 if (Constraint.size() == 1) {
5024 // FIXME: not handling fp-stack yet!
5025 switch (Constraint[0]) { // GCC X86 Constraint Letters
5026 default: break; // Unknown constraint letter
5027 case 'A': // EAX/EDX
5028 if (VT == MVT::i32 || VT == MVT::i64)
5029 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5031 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5034 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5035 else if (VT == MVT::i16)
5036 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5037 else if (VT == MVT::i8)
5038 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5043 return std::vector<unsigned>();
5046 std::pair<unsigned, const TargetRegisterClass*>
5047 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5048 MVT::ValueType VT) const {
5049 // First, see if this is a constraint that directly corresponds to an LLVM
5051 if (Constraint.size() == 1) {
5052 // GCC Constraint Letters
5053 switch (Constraint[0]) {
5055 case 'r': // GENERAL_REGS
5056 case 'R': // LEGACY_REGS
5057 case 'l': // INDEX_REGS
5058 if (VT == MVT::i64 && Subtarget->is64Bit())
5059 return std::make_pair(0U, X86::GR64RegisterClass);
5061 return std::make_pair(0U, X86::GR32RegisterClass);
5062 else if (VT == MVT::i16)
5063 return std::make_pair(0U, X86::GR16RegisterClass);
5064 else if (VT == MVT::i8)
5065 return std::make_pair(0U, X86::GR8RegisterClass);
5067 case 'y': // MMX_REGS if MMX allowed.
5068 if (!Subtarget->hasMMX()) break;
5069 return std::make_pair(0U, X86::VR64RegisterClass);
5071 case 'Y': // SSE_REGS if SSE2 allowed
5072 if (!Subtarget->hasSSE2()) break;
5074 case 'x': // SSE_REGS if SSE1 allowed
5075 if (!Subtarget->hasSSE1()) break;
5079 // Scalar SSE types.
5082 return std::make_pair(0U, X86::FR32RegisterClass);
5085 return std::make_pair(0U, X86::FR64RegisterClass);
5093 return std::make_pair(0U, X86::VR128RegisterClass);
5099 // Use the default implementation in TargetLowering to convert the register
5100 // constraint into a member of a register class.
5101 std::pair<unsigned, const TargetRegisterClass*> Res;
5102 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5104 // Not found as a standard register?
5105 if (Res.second == 0) {
5106 // GCC calls "st(0)" just plain "st".
5107 if (StringsEqualNoCase("{st}", Constraint)) {
5108 Res.first = X86::ST0;
5109 Res.second = X86::RSTRegisterClass;
5115 // Otherwise, check to see if this is a register class of the wrong value
5116 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5117 // turn into {ax},{dx}.
5118 if (Res.second->hasType(VT))
5119 return Res; // Correct type already, nothing to do.
5121 // All of the single-register GCC register classes map their values onto
5122 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5123 // really want an 8-bit or 32-bit register, map to the appropriate register
5124 // class and return the appropriate register.
5125 if (Res.second != X86::GR16RegisterClass)
5128 if (VT == MVT::i8) {
5129 unsigned DestReg = 0;
5130 switch (Res.first) {
5132 case X86::AX: DestReg = X86::AL; break;
5133 case X86::DX: DestReg = X86::DL; break;
5134 case X86::CX: DestReg = X86::CL; break;
5135 case X86::BX: DestReg = X86::BL; break;
5138 Res.first = DestReg;
5139 Res.second = Res.second = X86::GR8RegisterClass;
5141 } else if (VT == MVT::i32) {
5142 unsigned DestReg = 0;
5143 switch (Res.first) {
5145 case X86::AX: DestReg = X86::EAX; break;
5146 case X86::DX: DestReg = X86::EDX; break;
5147 case X86::CX: DestReg = X86::ECX; break;
5148 case X86::BX: DestReg = X86::EBX; break;
5149 case X86::SI: DestReg = X86::ESI; break;
5150 case X86::DI: DestReg = X86::EDI; break;
5151 case X86::BP: DestReg = X86::EBP; break;
5152 case X86::SP: DestReg = X86::ESP; break;
5155 Res.first = DestReg;
5156 Res.second = Res.second = X86::GR32RegisterClass;
5158 } else if (VT == MVT::i64) {
5159 unsigned DestReg = 0;
5160 switch (Res.first) {
5162 case X86::AX: DestReg = X86::RAX; break;
5163 case X86::DX: DestReg = X86::RDX; break;
5164 case X86::CX: DestReg = X86::RCX; break;
5165 case X86::BX: DestReg = X86::RBX; break;
5166 case X86::SI: DestReg = X86::RSI; break;
5167 case X86::DI: DestReg = X86::RDI; break;
5168 case X86::BP: DestReg = X86::RBP; break;
5169 case X86::SP: DestReg = X86::RSP; break;
5172 Res.first = DestReg;
5173 Res.second = Res.second = X86::GR64RegisterClass;