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 // Long double always uses X87.
326 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
328 // First set operation action for all vector types to expand. Then we
329 // will selectively turn on ones that can be effectively codegen'd.
330 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
331 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
332 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
333 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
334 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
335 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
336 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
337 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
338 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
339 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
340 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
341 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
342 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
343 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
344 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
345 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
346 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
347 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
348 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
349 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
350 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
351 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
352 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
353 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
354 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
357 if (Subtarget->hasMMX()) {
358 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
359 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
360 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
361 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
363 // FIXME: add MMX packed arithmetics
365 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
366 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
367 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
368 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
370 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
371 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
372 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
374 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
375 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
377 setOperationAction(ISD::AND, MVT::v8i8, Promote);
378 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
379 setOperationAction(ISD::AND, MVT::v4i16, Promote);
380 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
381 setOperationAction(ISD::AND, MVT::v2i32, Promote);
382 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
383 setOperationAction(ISD::AND, MVT::v1i64, Legal);
385 setOperationAction(ISD::OR, MVT::v8i8, Promote);
386 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
387 setOperationAction(ISD::OR, MVT::v4i16, Promote);
388 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
389 setOperationAction(ISD::OR, MVT::v2i32, Promote);
390 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
391 setOperationAction(ISD::OR, MVT::v1i64, Legal);
393 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
394 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
395 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
396 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
397 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
398 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
399 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
401 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
402 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
403 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
404 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
405 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
406 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
407 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
409 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
410 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
411 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
412 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
414 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
415 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
416 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
417 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
419 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
420 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
421 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
422 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
425 if (Subtarget->hasSSE1()) {
426 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
428 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
429 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
430 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
431 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
432 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
433 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
434 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
435 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
436 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
437 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
438 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
441 if (Subtarget->hasSSE2()) {
442 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
443 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
444 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
445 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
446 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
448 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
449 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
450 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
451 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
452 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
453 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
454 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
455 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
456 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
457 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
458 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
459 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
460 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
461 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
462 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
464 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
465 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
466 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
467 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
468 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
469 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
471 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
472 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
473 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
474 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
475 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
477 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
478 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
479 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
480 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
481 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
482 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
484 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
485 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
486 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
487 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
488 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
489 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
490 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
491 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
492 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
493 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
494 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
495 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
498 // Custom lower v2i64 and v2f64 selects.
499 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
500 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
501 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
502 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
505 // We want to custom lower some of our intrinsics.
506 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
508 // We have target-specific dag combine patterns for the following nodes:
509 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
510 setTargetDAGCombine(ISD::SELECT);
512 computeRegisterProperties();
514 // FIXME: These should be based on subtarget info. Plus, the values should
515 // be smaller when we are in optimizing for size mode.
516 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
517 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
518 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
519 allowUnalignedMemoryAccesses = true; // x86 supports it!
523 //===----------------------------------------------------------------------===//
524 // Return Value Calling Convention Implementation
525 //===----------------------------------------------------------------------===//
527 #include "X86GenCallingConv.inc"
529 /// LowerRET - Lower an ISD::RET node.
530 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
531 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
533 SmallVector<CCValAssign, 16> RVLocs;
534 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
535 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
536 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
537 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
540 // If this is the first return lowered for this function, add the regs to the
541 // liveout set for the function.
542 if (DAG.getMachineFunction().liveout_empty()) {
543 for (unsigned i = 0; i != RVLocs.size(); ++i)
544 if (RVLocs[i].isRegLoc())
545 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
548 SDOperand Chain = Op.getOperand(0);
551 // Copy the result values into the output registers.
552 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
553 RVLocs[0].getLocReg() != X86::ST0) {
554 for (unsigned i = 0; i != RVLocs.size(); ++i) {
555 CCValAssign &VA = RVLocs[i];
556 assert(VA.isRegLoc() && "Can only return in registers!");
557 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
559 Flag = Chain.getValue(1);
562 // We need to handle a destination of ST0 specially, because it isn't really
564 SDOperand Value = Op.getOperand(1);
566 // If this is an FP return with ScalarSSE, we need to move the value from
567 // an XMM register onto the fp-stack.
571 // If this is a load into a scalarsse value, don't store the loaded value
572 // back to the stack, only to reload it: just replace the scalar-sse load.
573 if (ISD::isNON_EXTLoad(Value.Val) &&
574 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
575 Chain = Value.getOperand(0);
576 MemLoc = Value.getOperand(1);
578 // Spill the value to memory and reload it into top of stack.
579 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
580 MachineFunction &MF = DAG.getMachineFunction();
581 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
582 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
583 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
585 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
586 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
587 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
588 Chain = Value.getValue(1);
591 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
592 SDOperand Ops[] = { Chain, Value };
593 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
594 Flag = Chain.getValue(1);
597 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
599 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
601 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
605 /// LowerCallResult - Lower the result values of an ISD::CALL into the
606 /// appropriate copies out of appropriate physical registers. This assumes that
607 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
608 /// being lowered. The returns a SDNode with the same number of values as the
610 SDNode *X86TargetLowering::
611 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
612 unsigned CallingConv, SelectionDAG &DAG) {
614 // Assign locations to each value returned by this call.
615 SmallVector<CCValAssign, 16> RVLocs;
616 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
617 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
618 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
621 SmallVector<SDOperand, 8> ResultVals;
623 // Copy all of the result registers out of their specified physreg.
624 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
625 for (unsigned i = 0; i != RVLocs.size(); ++i) {
626 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
627 RVLocs[i].getValVT(), InFlag).getValue(1);
628 InFlag = Chain.getValue(2);
629 ResultVals.push_back(Chain.getValue(0));
632 // Copies from the FP stack are special, as ST0 isn't a valid register
633 // before the fp stackifier runs.
635 // Copy ST0 into an RFP register with FP_GET_RESULT.
636 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
637 SDOperand GROps[] = { Chain, InFlag };
638 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
639 Chain = RetVal.getValue(1);
640 InFlag = RetVal.getValue(2);
642 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
645 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
646 // shouldn't be necessary except that RFP cannot be live across
647 // multiple blocks. When stackifier is fixed, they can be uncoupled.
648 MachineFunction &MF = DAG.getMachineFunction();
649 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
650 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
652 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
654 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
655 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
656 Chain = RetVal.getValue(1);
658 ResultVals.push_back(RetVal);
661 // Merge everything together with a MERGE_VALUES node.
662 ResultVals.push_back(Chain);
663 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
664 &ResultVals[0], ResultVals.size()).Val;
668 //===----------------------------------------------------------------------===//
669 // C & StdCall Calling Convention implementation
670 //===----------------------------------------------------------------------===//
671 // StdCall calling convention seems to be standard for many Windows' API
672 // routines and around. It differs from C calling convention just a little:
673 // callee should clean up the stack, not caller. Symbols should be also
674 // decorated in some fancy way :) It doesn't support any vector arguments.
676 /// AddLiveIn - This helper function adds the specified physical register to the
677 /// MachineFunction as a live in value. It also creates a corresponding virtual
679 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
680 const TargetRegisterClass *RC) {
681 assert(RC->contains(PReg) && "Not the correct regclass!");
682 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
683 MF.addLiveIn(PReg, VReg);
687 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
689 unsigned NumArgs = Op.Val->getNumValues() - 1;
690 MachineFunction &MF = DAG.getMachineFunction();
691 MachineFrameInfo *MFI = MF.getFrameInfo();
692 SDOperand Root = Op.getOperand(0);
693 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
695 // Assign locations to all of the incoming arguments.
696 SmallVector<CCValAssign, 16> ArgLocs;
697 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
698 getTargetMachine(), ArgLocs);
699 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
701 SmallVector<SDOperand, 8> ArgValues;
702 unsigned LastVal = ~0U;
703 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
704 CCValAssign &VA = ArgLocs[i];
705 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
707 assert(VA.getValNo() != LastVal &&
708 "Don't support value assigned to multiple locs yet");
709 LastVal = VA.getValNo();
712 MVT::ValueType RegVT = VA.getLocVT();
713 TargetRegisterClass *RC;
714 if (RegVT == MVT::i32)
715 RC = X86::GR32RegisterClass;
717 assert(MVT::isVector(RegVT));
718 RC = X86::VR128RegisterClass;
721 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
722 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
724 // If this is an 8 or 16-bit value, it is really passed promoted to 32
725 // bits. Insert an assert[sz]ext to capture this, then truncate to the
727 if (VA.getLocInfo() == CCValAssign::SExt)
728 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
729 DAG.getValueType(VA.getValVT()));
730 else if (VA.getLocInfo() == CCValAssign::ZExt)
731 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
732 DAG.getValueType(VA.getValVT()));
734 if (VA.getLocInfo() != CCValAssign::Full)
735 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
737 ArgValues.push_back(ArgValue);
739 assert(VA.isMemLoc());
741 // Create the nodes corresponding to a load from this parameter slot.
742 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
743 VA.getLocMemOffset());
744 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
745 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
749 unsigned StackSize = CCInfo.getNextStackOffset();
751 ArgValues.push_back(Root);
753 // If the function takes variable number of arguments, make a frame index for
754 // the start of the first vararg value... for expansion of llvm.va_start.
756 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
758 if (isStdCall && !isVarArg) {
759 BytesToPopOnReturn = StackSize; // Callee pops everything..
760 BytesCallerReserves = 0;
762 BytesToPopOnReturn = 0; // Callee pops nothing.
764 // If this is an sret function, the return should pop the hidden pointer.
766 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
767 ISD::ParamFlags::StructReturn))
768 BytesToPopOnReturn = 4;
770 BytesCallerReserves = StackSize;
773 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
774 ReturnAddrIndex = 0; // No return address slot generated yet.
776 MF.getInfo<X86MachineFunctionInfo>()
777 ->setBytesToPopOnReturn(BytesToPopOnReturn);
779 // Return the new list of results.
780 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
781 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
784 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
786 SDOperand Chain = Op.getOperand(0);
787 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
788 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
789 SDOperand Callee = Op.getOperand(4);
790 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
792 // Analyze operands of the call, assigning locations to each operand.
793 SmallVector<CCValAssign, 16> ArgLocs;
794 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
795 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
797 // Get a count of how many bytes are to be pushed on the stack.
798 unsigned NumBytes = CCInfo.getNextStackOffset();
800 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
802 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
803 SmallVector<SDOperand, 8> MemOpChains;
807 // Walk the register/memloc assignments, inserting copies/loads.
808 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
809 CCValAssign &VA = ArgLocs[i];
810 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
812 // Promote the value if needed.
813 switch (VA.getLocInfo()) {
814 default: assert(0 && "Unknown loc info!");
815 case CCValAssign::Full: break;
816 case CCValAssign::SExt:
817 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
819 case CCValAssign::ZExt:
820 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
822 case CCValAssign::AExt:
823 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
828 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
830 assert(VA.isMemLoc());
831 if (StackPtr.Val == 0)
832 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
833 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
834 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
835 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
839 // If the first argument is an sret pointer, remember it.
840 bool isSRet = NumOps &&
841 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
842 ISD::ParamFlags::StructReturn);
844 if (!MemOpChains.empty())
845 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
846 &MemOpChains[0], MemOpChains.size());
848 // Build a sequence of copy-to-reg nodes chained together with token chain
849 // and flag operands which copy the outgoing args into registers.
851 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
852 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
854 InFlag = Chain.getValue(1);
857 // ELF / PIC requires GOT in the EBX register before function calls via PLT
859 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
860 Subtarget->isPICStyleGOT()) {
861 Chain = DAG.getCopyToReg(Chain, X86::EBX,
862 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
864 InFlag = Chain.getValue(1);
867 // If the callee is a GlobalAddress node (quite common, every direct call is)
868 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
869 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
870 // We should use extra load for direct calls to dllimported functions in
872 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
873 getTargetMachine(), true))
874 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
875 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
876 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
878 // Returns a chain & a flag for retval copy to use.
879 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
880 SmallVector<SDOperand, 8> Ops;
881 Ops.push_back(Chain);
882 Ops.push_back(Callee);
884 // Add argument registers to the end of the list so that they are known live
886 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
887 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
888 RegsToPass[i].second.getValueType()));
890 // Add an implicit use GOT pointer in EBX.
891 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
892 Subtarget->isPICStyleGOT())
893 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
896 Ops.push_back(InFlag);
898 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
899 NodeTys, &Ops[0], Ops.size());
900 InFlag = Chain.getValue(1);
902 // Create the CALLSEQ_END node.
903 unsigned NumBytesForCalleeToPush = 0;
905 if (CC == CallingConv::X86_StdCall) {
907 NumBytesForCalleeToPush = isSRet ? 4 : 0;
909 NumBytesForCalleeToPush = NumBytes;
911 // If this is is a call to a struct-return function, the callee
912 // pops the hidden struct pointer, so we have to push it back.
913 // This is common for Darwin/X86, Linux & Mingw32 targets.
914 NumBytesForCalleeToPush = isSRet ? 4 : 0;
917 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
919 Ops.push_back(Chain);
920 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
921 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
922 Ops.push_back(InFlag);
923 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
924 InFlag = Chain.getValue(1);
926 // Handle result values, copying them out of physregs into vregs that we
928 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
932 //===----------------------------------------------------------------------===//
933 // FastCall Calling Convention implementation
934 //===----------------------------------------------------------------------===//
936 // The X86 'fastcall' calling convention passes up to two integer arguments in
937 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
938 // and requires that the callee pop its arguments off the stack (allowing proper
939 // tail calls), and has the same return value conventions as C calling convs.
941 // This calling convention always arranges for the callee pop value to be 8n+4
942 // bytes, which is needed for tail recursion elimination and stack alignment
945 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
946 MachineFunction &MF = DAG.getMachineFunction();
947 MachineFrameInfo *MFI = MF.getFrameInfo();
948 SDOperand Root = Op.getOperand(0);
949 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
951 // Assign locations to all of the incoming arguments.
952 SmallVector<CCValAssign, 16> ArgLocs;
953 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
954 getTargetMachine(), ArgLocs);
955 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
957 SmallVector<SDOperand, 8> ArgValues;
958 unsigned LastVal = ~0U;
959 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
960 CCValAssign &VA = ArgLocs[i];
961 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
963 assert(VA.getValNo() != LastVal &&
964 "Don't support value assigned to multiple locs yet");
965 LastVal = VA.getValNo();
968 MVT::ValueType RegVT = VA.getLocVT();
969 TargetRegisterClass *RC;
970 if (RegVT == MVT::i32)
971 RC = X86::GR32RegisterClass;
973 assert(MVT::isVector(RegVT));
974 RC = X86::VR128RegisterClass;
977 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
978 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
980 // If this is an 8 or 16-bit value, it is really passed promoted to 32
981 // bits. Insert an assert[sz]ext to capture this, then truncate to the
983 if (VA.getLocInfo() == CCValAssign::SExt)
984 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
985 DAG.getValueType(VA.getValVT()));
986 else if (VA.getLocInfo() == CCValAssign::ZExt)
987 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
988 DAG.getValueType(VA.getValVT()));
990 if (VA.getLocInfo() != CCValAssign::Full)
991 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
993 ArgValues.push_back(ArgValue);
995 assert(VA.isMemLoc());
997 // Create the nodes corresponding to a load from this parameter slot.
998 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
999 VA.getLocMemOffset());
1000 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1001 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1005 ArgValues.push_back(Root);
1007 unsigned StackSize = CCInfo.getNextStackOffset();
1009 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1010 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1011 // arguments and the arguments after the retaddr has been pushed are aligned.
1012 if ((StackSize & 7) == 0)
1016 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1017 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1018 ReturnAddrIndex = 0; // No return address slot generated yet.
1019 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
1020 BytesCallerReserves = 0;
1022 MF.getInfo<X86MachineFunctionInfo>()
1023 ->setBytesToPopOnReturn(BytesToPopOnReturn);
1025 // Return the new list of results.
1026 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1027 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1030 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1032 SDOperand Chain = Op.getOperand(0);
1033 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1034 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1035 SDOperand Callee = Op.getOperand(4);
1037 // Analyze operands of the call, assigning locations to each operand.
1038 SmallVector<CCValAssign, 16> ArgLocs;
1039 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1040 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
1042 // Get a count of how many bytes are to be pushed on the stack.
1043 unsigned NumBytes = CCInfo.getNextStackOffset();
1045 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1046 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1047 // arguments and the arguments after the retaddr has been pushed are aligned.
1048 if ((NumBytes & 7) == 0)
1052 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1054 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1055 SmallVector<SDOperand, 8> MemOpChains;
1059 // Walk the register/memloc assignments, inserting copies/loads.
1060 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1061 CCValAssign &VA = ArgLocs[i];
1062 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1064 // Promote the value if needed.
1065 switch (VA.getLocInfo()) {
1066 default: assert(0 && "Unknown loc info!");
1067 case CCValAssign::Full: break;
1068 case CCValAssign::SExt:
1069 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1071 case CCValAssign::ZExt:
1072 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1074 case CCValAssign::AExt:
1075 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1079 if (VA.isRegLoc()) {
1080 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1082 assert(VA.isMemLoc());
1083 if (StackPtr.Val == 0)
1084 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1085 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1086 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1087 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1091 if (!MemOpChains.empty())
1092 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1093 &MemOpChains[0], MemOpChains.size());
1095 // Build a sequence of copy-to-reg nodes chained together with token chain
1096 // and flag operands which copy the outgoing args into registers.
1098 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1099 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1101 InFlag = Chain.getValue(1);
1104 // If the callee is a GlobalAddress node (quite common, every direct call is)
1105 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1106 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1107 // We should use extra load for direct calls to dllimported functions in
1109 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1110 getTargetMachine(), true))
1111 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1112 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1113 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1115 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1117 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1118 Subtarget->isPICStyleGOT()) {
1119 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1120 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1122 InFlag = Chain.getValue(1);
1125 // Returns a chain & a flag for retval copy to use.
1126 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1127 SmallVector<SDOperand, 8> Ops;
1128 Ops.push_back(Chain);
1129 Ops.push_back(Callee);
1131 // Add argument registers to the end of the list so that they are known live
1133 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1134 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1135 RegsToPass[i].second.getValueType()));
1137 // Add an implicit use GOT pointer in EBX.
1138 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1139 Subtarget->isPICStyleGOT())
1140 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1143 Ops.push_back(InFlag);
1145 // FIXME: Do not generate X86ISD::TAILCALL for now.
1146 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1147 NodeTys, &Ops[0], Ops.size());
1148 InFlag = Chain.getValue(1);
1150 // Returns a flag for retval copy to use.
1151 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1153 Ops.push_back(Chain);
1154 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1155 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1156 Ops.push_back(InFlag);
1157 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1158 InFlag = Chain.getValue(1);
1160 // Handle result values, copying them out of physregs into vregs that we
1162 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1166 //===----------------------------------------------------------------------===//
1167 // X86-64 C Calling Convention implementation
1168 //===----------------------------------------------------------------------===//
1171 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1172 MachineFunction &MF = DAG.getMachineFunction();
1173 MachineFrameInfo *MFI = MF.getFrameInfo();
1174 SDOperand Root = Op.getOperand(0);
1175 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1177 static const unsigned GPR64ArgRegs[] = {
1178 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1180 static const unsigned XMMArgRegs[] = {
1181 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1182 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1186 // Assign locations to all of the incoming arguments.
1187 SmallVector<CCValAssign, 16> ArgLocs;
1188 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1189 getTargetMachine(), ArgLocs);
1190 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1192 SmallVector<SDOperand, 8> ArgValues;
1193 unsigned LastVal = ~0U;
1194 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1195 CCValAssign &VA = ArgLocs[i];
1196 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1198 assert(VA.getValNo() != LastVal &&
1199 "Don't support value assigned to multiple locs yet");
1200 LastVal = VA.getValNo();
1202 if (VA.isRegLoc()) {
1203 MVT::ValueType RegVT = VA.getLocVT();
1204 TargetRegisterClass *RC;
1205 if (RegVT == MVT::i32)
1206 RC = X86::GR32RegisterClass;
1207 else if (RegVT == MVT::i64)
1208 RC = X86::GR64RegisterClass;
1209 else if (RegVT == MVT::f32)
1210 RC = X86::FR32RegisterClass;
1211 else if (RegVT == MVT::f64)
1212 RC = X86::FR64RegisterClass;
1214 assert(MVT::isVector(RegVT));
1215 if (MVT::getSizeInBits(RegVT) == 64) {
1216 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1219 RC = X86::VR128RegisterClass;
1222 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1223 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1225 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1226 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1228 if (VA.getLocInfo() == CCValAssign::SExt)
1229 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1230 DAG.getValueType(VA.getValVT()));
1231 else if (VA.getLocInfo() == CCValAssign::ZExt)
1232 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1233 DAG.getValueType(VA.getValVT()));
1235 if (VA.getLocInfo() != CCValAssign::Full)
1236 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1238 // Handle MMX values passed in GPRs.
1239 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1240 MVT::getSizeInBits(RegVT) == 64)
1241 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1243 ArgValues.push_back(ArgValue);
1245 assert(VA.isMemLoc());
1247 // Create the nodes corresponding to a load from this parameter slot.
1248 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1249 VA.getLocMemOffset());
1250 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1251 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1255 unsigned StackSize = CCInfo.getNextStackOffset();
1257 // If the function takes variable number of arguments, make a frame index for
1258 // the start of the first vararg value... for expansion of llvm.va_start.
1260 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1261 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1263 // For X86-64, if there are vararg parameters that are passed via
1264 // registers, then we must store them to their spots on the stack so they
1265 // may be loaded by deferencing the result of va_next.
1266 VarArgsGPOffset = NumIntRegs * 8;
1267 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1268 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1269 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1271 // Store the integer parameter registers.
1272 SmallVector<SDOperand, 8> MemOps;
1273 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1274 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1275 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1276 for (; NumIntRegs != 6; ++NumIntRegs) {
1277 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1278 X86::GR64RegisterClass);
1279 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1280 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1281 MemOps.push_back(Store);
1282 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1283 DAG.getConstant(8, getPointerTy()));
1286 // Now store the XMM (fp + vector) parameter registers.
1287 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1288 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1289 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1290 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1291 X86::VR128RegisterClass);
1292 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1293 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1294 MemOps.push_back(Store);
1295 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1296 DAG.getConstant(16, getPointerTy()));
1298 if (!MemOps.empty())
1299 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1300 &MemOps[0], MemOps.size());
1303 ArgValues.push_back(Root);
1305 ReturnAddrIndex = 0; // No return address slot generated yet.
1306 BytesToPopOnReturn = 0; // Callee pops nothing.
1307 BytesCallerReserves = StackSize;
1309 // Return the new list of results.
1310 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1311 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1315 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1317 SDOperand Chain = Op.getOperand(0);
1318 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1319 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1320 SDOperand Callee = Op.getOperand(4);
1322 // Analyze operands of the call, assigning locations to each operand.
1323 SmallVector<CCValAssign, 16> ArgLocs;
1324 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1325 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1327 // Get a count of how many bytes are to be pushed on the stack.
1328 unsigned NumBytes = CCInfo.getNextStackOffset();
1329 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1331 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1332 SmallVector<SDOperand, 8> MemOpChains;
1336 // Walk the register/memloc assignments, inserting copies/loads.
1337 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1338 CCValAssign &VA = ArgLocs[i];
1339 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1341 // Promote the value if needed.
1342 switch (VA.getLocInfo()) {
1343 default: assert(0 && "Unknown loc info!");
1344 case CCValAssign::Full: break;
1345 case CCValAssign::SExt:
1346 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1348 case CCValAssign::ZExt:
1349 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1351 case CCValAssign::AExt:
1352 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1356 if (VA.isRegLoc()) {
1357 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1359 assert(VA.isMemLoc());
1360 if (StackPtr.Val == 0)
1361 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1362 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1363 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1364 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1368 if (!MemOpChains.empty())
1369 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1370 &MemOpChains[0], MemOpChains.size());
1372 // Build a sequence of copy-to-reg nodes chained together with token chain
1373 // and flag operands which copy the outgoing args into registers.
1375 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1376 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1378 InFlag = Chain.getValue(1);
1382 // From AMD64 ABI document:
1383 // For calls that may call functions that use varargs or stdargs
1384 // (prototype-less calls or calls to functions containing ellipsis (...) in
1385 // the declaration) %al is used as hidden argument to specify the number
1386 // of SSE registers used. The contents of %al do not need to match exactly
1387 // the number of registers, but must be an ubound on the number of SSE
1388 // registers used and is in the range 0 - 8 inclusive.
1390 // Count the number of XMM registers allocated.
1391 static const unsigned XMMArgRegs[] = {
1392 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1393 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1395 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1397 Chain = DAG.getCopyToReg(Chain, X86::AL,
1398 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1399 InFlag = Chain.getValue(1);
1402 // If the callee is a GlobalAddress node (quite common, every direct call is)
1403 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1404 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1405 // We should use extra load for direct calls to dllimported functions in
1407 if (getTargetMachine().getCodeModel() != CodeModel::Large
1408 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1409 getTargetMachine(), true))
1410 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1411 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1412 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1413 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1415 // Returns a chain & a flag for retval copy to use.
1416 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1417 SmallVector<SDOperand, 8> Ops;
1418 Ops.push_back(Chain);
1419 Ops.push_back(Callee);
1421 // Add argument registers to the end of the list so that they are known live
1423 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1424 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1425 RegsToPass[i].second.getValueType()));
1428 Ops.push_back(InFlag);
1430 // FIXME: Do not generate X86ISD::TAILCALL for now.
1431 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1432 NodeTys, &Ops[0], Ops.size());
1433 InFlag = Chain.getValue(1);
1435 // Returns a flag for retval copy to use.
1436 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1438 Ops.push_back(Chain);
1439 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1440 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1441 Ops.push_back(InFlag);
1442 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1443 InFlag = Chain.getValue(1);
1445 // Handle result values, copying them out of physregs into vregs that we
1447 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1451 //===----------------------------------------------------------------------===//
1452 // Other Lowering Hooks
1453 //===----------------------------------------------------------------------===//
1456 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1457 if (ReturnAddrIndex == 0) {
1458 // Set up a frame object for the return address.
1459 MachineFunction &MF = DAG.getMachineFunction();
1460 if (Subtarget->is64Bit())
1461 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1463 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1466 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1471 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1472 /// specific condition code. It returns a false if it cannot do a direct
1473 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1475 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1476 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1477 SelectionDAG &DAG) {
1478 X86CC = X86::COND_INVALID;
1480 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1481 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1482 // X > -1 -> X == 0, jump !sign.
1483 RHS = DAG.getConstant(0, RHS.getValueType());
1484 X86CC = X86::COND_NS;
1486 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1487 // X < 0 -> X == 0, jump on sign.
1488 X86CC = X86::COND_S;
1493 switch (SetCCOpcode) {
1495 case ISD::SETEQ: X86CC = X86::COND_E; break;
1496 case ISD::SETGT: X86CC = X86::COND_G; break;
1497 case ISD::SETGE: X86CC = X86::COND_GE; break;
1498 case ISD::SETLT: X86CC = X86::COND_L; break;
1499 case ISD::SETLE: X86CC = X86::COND_LE; break;
1500 case ISD::SETNE: X86CC = X86::COND_NE; break;
1501 case ISD::SETULT: X86CC = X86::COND_B; break;
1502 case ISD::SETUGT: X86CC = X86::COND_A; break;
1503 case ISD::SETULE: X86CC = X86::COND_BE; break;
1504 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1507 // On a floating point condition, the flags are set as follows:
1509 // 0 | 0 | 0 | X > Y
1510 // 0 | 0 | 1 | X < Y
1511 // 1 | 0 | 0 | X == Y
1512 // 1 | 1 | 1 | unordered
1514 switch (SetCCOpcode) {
1517 case ISD::SETEQ: X86CC = X86::COND_E; break;
1518 case ISD::SETOLT: Flip = true; // Fallthrough
1520 case ISD::SETGT: X86CC = X86::COND_A; break;
1521 case ISD::SETOLE: Flip = true; // Fallthrough
1523 case ISD::SETGE: X86CC = X86::COND_AE; break;
1524 case ISD::SETUGT: Flip = true; // Fallthrough
1526 case ISD::SETLT: X86CC = X86::COND_B; break;
1527 case ISD::SETUGE: Flip = true; // Fallthrough
1529 case ISD::SETLE: X86CC = X86::COND_BE; break;
1531 case ISD::SETNE: X86CC = X86::COND_NE; break;
1532 case ISD::SETUO: X86CC = X86::COND_P; break;
1533 case ISD::SETO: X86CC = X86::COND_NP; break;
1536 std::swap(LHS, RHS);
1539 return X86CC != X86::COND_INVALID;
1542 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1543 /// code. Current x86 isa includes the following FP cmov instructions:
1544 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1545 static bool hasFPCMov(unsigned X86CC) {
1561 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1562 /// true if Op is undef or if its value falls within the specified range (L, H].
1563 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1564 if (Op.getOpcode() == ISD::UNDEF)
1567 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1568 return (Val >= Low && Val < Hi);
1571 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1572 /// true if Op is undef or if its value equal to the specified value.
1573 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1574 if (Op.getOpcode() == ISD::UNDEF)
1576 return cast<ConstantSDNode>(Op)->getValue() == Val;
1579 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1580 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1581 bool X86::isPSHUFDMask(SDNode *N) {
1582 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1584 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
1587 // Check if the value doesn't reference the second vector.
1588 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1589 SDOperand Arg = N->getOperand(i);
1590 if (Arg.getOpcode() == ISD::UNDEF) continue;
1591 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1592 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
1599 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1600 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1601 bool X86::isPSHUFHWMask(SDNode *N) {
1602 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1604 if (N->getNumOperands() != 8)
1607 // Lower quadword copied in order.
1608 for (unsigned i = 0; i != 4; ++i) {
1609 SDOperand Arg = N->getOperand(i);
1610 if (Arg.getOpcode() == ISD::UNDEF) continue;
1611 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1612 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1616 // Upper quadword shuffled.
1617 for (unsigned i = 4; i != 8; ++i) {
1618 SDOperand Arg = N->getOperand(i);
1619 if (Arg.getOpcode() == ISD::UNDEF) continue;
1620 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1621 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1622 if (Val < 4 || Val > 7)
1629 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1630 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1631 bool X86::isPSHUFLWMask(SDNode *N) {
1632 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1634 if (N->getNumOperands() != 8)
1637 // Upper quadword copied in order.
1638 for (unsigned i = 4; i != 8; ++i)
1639 if (!isUndefOrEqual(N->getOperand(i), i))
1642 // Lower quadword shuffled.
1643 for (unsigned i = 0; i != 4; ++i)
1644 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1650 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1651 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1652 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1653 if (NumElems != 2 && NumElems != 4) return false;
1655 unsigned Half = NumElems / 2;
1656 for (unsigned i = 0; i < Half; ++i)
1657 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1659 for (unsigned i = Half; i < NumElems; ++i)
1660 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1666 bool X86::isSHUFPMask(SDNode *N) {
1667 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1668 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1671 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly
1672 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1673 /// half elements to come from vector 1 (which would equal the dest.) and
1674 /// the upper half to come from vector 2.
1675 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1676 if (NumOps != 2 && NumOps != 4) return false;
1678 unsigned Half = NumOps / 2;
1679 for (unsigned i = 0; i < Half; ++i)
1680 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1682 for (unsigned i = Half; i < NumOps; ++i)
1683 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1688 static bool isCommutedSHUFP(SDNode *N) {
1689 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1690 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1693 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1694 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1695 bool X86::isMOVHLPSMask(SDNode *N) {
1696 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1698 if (N->getNumOperands() != 4)
1701 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1702 return isUndefOrEqual(N->getOperand(0), 6) &&
1703 isUndefOrEqual(N->getOperand(1), 7) &&
1704 isUndefOrEqual(N->getOperand(2), 2) &&
1705 isUndefOrEqual(N->getOperand(3), 3);
1708 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1709 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1711 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1712 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1714 if (N->getNumOperands() != 4)
1717 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1718 return isUndefOrEqual(N->getOperand(0), 2) &&
1719 isUndefOrEqual(N->getOperand(1), 3) &&
1720 isUndefOrEqual(N->getOperand(2), 2) &&
1721 isUndefOrEqual(N->getOperand(3), 3);
1724 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1725 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1726 bool X86::isMOVLPMask(SDNode *N) {
1727 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1729 unsigned NumElems = N->getNumOperands();
1730 if (NumElems != 2 && NumElems != 4)
1733 for (unsigned i = 0; i < NumElems/2; ++i)
1734 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1737 for (unsigned i = NumElems/2; i < NumElems; ++i)
1738 if (!isUndefOrEqual(N->getOperand(i), i))
1744 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1745 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1747 bool X86::isMOVHPMask(SDNode *N) {
1748 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1750 unsigned NumElems = N->getNumOperands();
1751 if (NumElems != 2 && NumElems != 4)
1754 for (unsigned i = 0; i < NumElems/2; ++i)
1755 if (!isUndefOrEqual(N->getOperand(i), i))
1758 for (unsigned i = 0; i < NumElems/2; ++i) {
1759 SDOperand Arg = N->getOperand(i + NumElems/2);
1760 if (!isUndefOrEqual(Arg, i + NumElems))
1767 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1768 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1769 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1770 bool V2IsSplat = false) {
1771 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1774 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1775 SDOperand BitI = Elts[i];
1776 SDOperand BitI1 = Elts[i+1];
1777 if (!isUndefOrEqual(BitI, j))
1780 if (isUndefOrEqual(BitI1, NumElts))
1783 if (!isUndefOrEqual(BitI1, j + NumElts))
1791 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1792 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1793 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1796 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1797 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1798 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1799 bool V2IsSplat = false) {
1800 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1803 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1804 SDOperand BitI = Elts[i];
1805 SDOperand BitI1 = Elts[i+1];
1806 if (!isUndefOrEqual(BitI, j + NumElts/2))
1809 if (isUndefOrEqual(BitI1, NumElts))
1812 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1820 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1821 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1822 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1825 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1826 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1828 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1829 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1831 unsigned NumElems = N->getNumOperands();
1832 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1835 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1836 SDOperand BitI = N->getOperand(i);
1837 SDOperand BitI1 = N->getOperand(i+1);
1839 if (!isUndefOrEqual(BitI, j))
1841 if (!isUndefOrEqual(BitI1, j))
1848 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1849 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1851 bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1852 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1854 unsigned NumElems = N->getNumOperands();
1855 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1858 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1859 SDOperand BitI = N->getOperand(i);
1860 SDOperand BitI1 = N->getOperand(i + 1);
1862 if (!isUndefOrEqual(BitI, j))
1864 if (!isUndefOrEqual(BitI1, j))
1871 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1872 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1873 /// MOVSD, and MOVD, i.e. setting the lowest element.
1874 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1875 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1878 if (!isUndefOrEqual(Elts[0], NumElts))
1881 for (unsigned i = 1; i < NumElts; ++i) {
1882 if (!isUndefOrEqual(Elts[i], i))
1889 bool X86::isMOVLMask(SDNode *N) {
1890 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1891 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
1894 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1895 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
1896 /// element of vector 2 and the other elements to come from vector 1 in order.
1897 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1898 bool V2IsSplat = false,
1899 bool V2IsUndef = false) {
1900 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
1903 if (!isUndefOrEqual(Ops[0], 0))
1906 for (unsigned i = 1; i < NumOps; ++i) {
1907 SDOperand Arg = Ops[i];
1908 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1909 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1910 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
1917 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1918 bool V2IsUndef = false) {
1919 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1920 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1921 V2IsSplat, V2IsUndef);
1924 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1925 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1926 bool X86::isMOVSHDUPMask(SDNode *N) {
1927 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1929 if (N->getNumOperands() != 4)
1932 // Expect 1, 1, 3, 3
1933 for (unsigned i = 0; i < 2; ++i) {
1934 SDOperand Arg = N->getOperand(i);
1935 if (Arg.getOpcode() == ISD::UNDEF) continue;
1936 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1937 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1938 if (Val != 1) return false;
1942 for (unsigned i = 2; i < 4; ++i) {
1943 SDOperand Arg = N->getOperand(i);
1944 if (Arg.getOpcode() == ISD::UNDEF) continue;
1945 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1946 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1947 if (Val != 3) return false;
1951 // Don't use movshdup if it can be done with a shufps.
1955 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1956 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1957 bool X86::isMOVSLDUPMask(SDNode *N) {
1958 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1960 if (N->getNumOperands() != 4)
1963 // Expect 0, 0, 2, 2
1964 for (unsigned i = 0; i < 2; ++i) {
1965 SDOperand Arg = N->getOperand(i);
1966 if (Arg.getOpcode() == ISD::UNDEF) continue;
1967 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1968 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1969 if (Val != 0) return false;
1973 for (unsigned i = 2; i < 4; ++i) {
1974 SDOperand Arg = N->getOperand(i);
1975 if (Arg.getOpcode() == ISD::UNDEF) continue;
1976 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1977 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1978 if (Val != 2) return false;
1982 // Don't use movshdup if it can be done with a shufps.
1986 /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
1987 /// specifies a identity operation on the LHS or RHS.
1988 static bool isIdentityMask(SDNode *N, bool RHS = false) {
1989 unsigned NumElems = N->getNumOperands();
1990 for (unsigned i = 0; i < NumElems; ++i)
1991 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
1996 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1997 /// a splat of a single element.
1998 static bool isSplatMask(SDNode *N) {
1999 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2001 // This is a splat operation if each element of the permute is the same, and
2002 // if the value doesn't reference the second vector.
2003 unsigned NumElems = N->getNumOperands();
2004 SDOperand ElementBase;
2006 for (; i != NumElems; ++i) {
2007 SDOperand Elt = N->getOperand(i);
2008 if (isa<ConstantSDNode>(Elt)) {
2014 if (!ElementBase.Val)
2017 for (; i != NumElems; ++i) {
2018 SDOperand Arg = N->getOperand(i);
2019 if (Arg.getOpcode() == ISD::UNDEF) continue;
2020 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2021 if (Arg != ElementBase) return false;
2024 // Make sure it is a splat of the first vector operand.
2025 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2028 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2029 /// a splat of a single element and it's a 2 or 4 element mask.
2030 bool X86::isSplatMask(SDNode *N) {
2031 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2033 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2034 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2036 return ::isSplatMask(N);
2039 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2040 /// specifies a splat of zero element.
2041 bool X86::isSplatLoMask(SDNode *N) {
2042 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2044 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2045 if (!isUndefOrEqual(N->getOperand(i), 0))
2050 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2051 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2053 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2054 unsigned NumOperands = N->getNumOperands();
2055 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2057 for (unsigned i = 0; i < NumOperands; ++i) {
2059 SDOperand Arg = N->getOperand(NumOperands-i-1);
2060 if (Arg.getOpcode() != ISD::UNDEF)
2061 Val = cast<ConstantSDNode>(Arg)->getValue();
2062 if (Val >= NumOperands) Val -= NumOperands;
2064 if (i != NumOperands - 1)
2071 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2072 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2074 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2076 // 8 nodes, but we only care about the last 4.
2077 for (unsigned i = 7; i >= 4; --i) {
2079 SDOperand Arg = N->getOperand(i);
2080 if (Arg.getOpcode() != ISD::UNDEF)
2081 Val = cast<ConstantSDNode>(Arg)->getValue();
2090 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2091 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2093 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2095 // 8 nodes, but we only care about the first 4.
2096 for (int i = 3; i >= 0; --i) {
2098 SDOperand Arg = N->getOperand(i);
2099 if (Arg.getOpcode() != ISD::UNDEF)
2100 Val = cast<ConstantSDNode>(Arg)->getValue();
2109 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2110 /// specifies a 8 element shuffle that can be broken into a pair of
2111 /// PSHUFHW and PSHUFLW.
2112 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2113 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2115 if (N->getNumOperands() != 8)
2118 // Lower quadword shuffled.
2119 for (unsigned i = 0; i != 4; ++i) {
2120 SDOperand Arg = N->getOperand(i);
2121 if (Arg.getOpcode() == ISD::UNDEF) continue;
2122 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2123 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2128 // Upper quadword shuffled.
2129 for (unsigned i = 4; i != 8; ++i) {
2130 SDOperand Arg = N->getOperand(i);
2131 if (Arg.getOpcode() == ISD::UNDEF) continue;
2132 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2133 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2134 if (Val < 4 || Val > 7)
2141 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2142 /// values in ther permute mask.
2143 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2144 SDOperand &V2, SDOperand &Mask,
2145 SelectionDAG &DAG) {
2146 MVT::ValueType VT = Op.getValueType();
2147 MVT::ValueType MaskVT = Mask.getValueType();
2148 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2149 unsigned NumElems = Mask.getNumOperands();
2150 SmallVector<SDOperand, 8> MaskVec;
2152 for (unsigned i = 0; i != NumElems; ++i) {
2153 SDOperand Arg = Mask.getOperand(i);
2154 if (Arg.getOpcode() == ISD::UNDEF) {
2155 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2158 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2159 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2161 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2163 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2167 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2168 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2171 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2172 /// match movhlps. The lower half elements should come from upper half of
2173 /// V1 (and in order), and the upper half elements should come from the upper
2174 /// half of V2 (and in order).
2175 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2176 unsigned NumElems = Mask->getNumOperands();
2179 for (unsigned i = 0, e = 2; i != e; ++i)
2180 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2182 for (unsigned i = 2; i != 4; ++i)
2183 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2188 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2189 /// is promoted to a vector.
2190 static inline bool isScalarLoadToVector(SDNode *N) {
2191 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2192 N = N->getOperand(0).Val;
2193 return ISD::isNON_EXTLoad(N);
2198 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2199 /// match movlp{s|d}. The lower half elements should come from lower half of
2200 /// V1 (and in order), and the upper half elements should come from the upper
2201 /// half of V2 (and in order). And since V1 will become the source of the
2202 /// MOVLP, it must be either a vector load or a scalar load to vector.
2203 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2204 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2206 // Is V2 is a vector load, don't do this transformation. We will try to use
2207 // load folding shufps op.
2208 if (ISD::isNON_EXTLoad(V2))
2211 unsigned NumElems = Mask->getNumOperands();
2212 if (NumElems != 2 && NumElems != 4)
2214 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2215 if (!isUndefOrEqual(Mask->getOperand(i), i))
2217 for (unsigned i = NumElems/2; i != NumElems; ++i)
2218 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2223 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2225 static bool isSplatVector(SDNode *N) {
2226 if (N->getOpcode() != ISD::BUILD_VECTOR)
2229 SDOperand SplatValue = N->getOperand(0);
2230 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2231 if (N->getOperand(i) != SplatValue)
2236 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2238 static bool isUndefShuffle(SDNode *N) {
2239 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2242 SDOperand V1 = N->getOperand(0);
2243 SDOperand V2 = N->getOperand(1);
2244 SDOperand Mask = N->getOperand(2);
2245 unsigned NumElems = Mask.getNumOperands();
2246 for (unsigned i = 0; i != NumElems; ++i) {
2247 SDOperand Arg = Mask.getOperand(i);
2248 if (Arg.getOpcode() != ISD::UNDEF) {
2249 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2250 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2252 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2259 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2261 static inline bool isZeroNode(SDOperand Elt) {
2262 return ((isa<ConstantSDNode>(Elt) &&
2263 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2264 (isa<ConstantFPSDNode>(Elt) &&
2265 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2268 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2269 /// to an zero vector.
2270 static bool isZeroShuffle(SDNode *N) {
2271 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2274 SDOperand V1 = N->getOperand(0);
2275 SDOperand V2 = N->getOperand(1);
2276 SDOperand Mask = N->getOperand(2);
2277 unsigned NumElems = Mask.getNumOperands();
2278 for (unsigned i = 0; i != NumElems; ++i) {
2279 SDOperand Arg = Mask.getOperand(i);
2280 if (Arg.getOpcode() != ISD::UNDEF) {
2281 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2282 if (Idx < NumElems) {
2283 unsigned Opc = V1.Val->getOpcode();
2284 if (Opc == ISD::UNDEF)
2286 if (Opc != ISD::BUILD_VECTOR ||
2287 !isZeroNode(V1.Val->getOperand(Idx)))
2289 } else if (Idx >= NumElems) {
2290 unsigned Opc = V2.Val->getOpcode();
2291 if (Opc == ISD::UNDEF)
2293 if (Opc != ISD::BUILD_VECTOR ||
2294 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2302 /// getZeroVector - Returns a vector of specified type with all zero elements.
2304 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2305 assert(MVT::isVector(VT) && "Expected a vector type");
2306 unsigned NumElems = MVT::getVectorNumElements(VT);
2307 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2308 bool isFP = MVT::isFloatingPoint(EVT);
2309 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2310 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2311 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2314 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2315 /// that point to V2 points to its first element.
2316 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2317 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2319 bool Changed = false;
2320 SmallVector<SDOperand, 8> MaskVec;
2321 unsigned NumElems = Mask.getNumOperands();
2322 for (unsigned i = 0; i != NumElems; ++i) {
2323 SDOperand Arg = Mask.getOperand(i);
2324 if (Arg.getOpcode() != ISD::UNDEF) {
2325 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2326 if (Val > NumElems) {
2327 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2331 MaskVec.push_back(Arg);
2335 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2336 &MaskVec[0], MaskVec.size());
2340 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2341 /// operation of specified width.
2342 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2343 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2344 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2346 SmallVector<SDOperand, 8> MaskVec;
2347 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2348 for (unsigned i = 1; i != NumElems; ++i)
2349 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2350 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2353 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2354 /// of specified width.
2355 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2356 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2357 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2358 SmallVector<SDOperand, 8> MaskVec;
2359 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2360 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2361 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2363 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2366 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2367 /// of specified width.
2368 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2369 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2370 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2371 unsigned Half = NumElems/2;
2372 SmallVector<SDOperand, 8> MaskVec;
2373 for (unsigned i = 0; i != Half; ++i) {
2374 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2375 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2377 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2380 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2382 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2383 SDOperand V1 = Op.getOperand(0);
2384 SDOperand Mask = Op.getOperand(2);
2385 MVT::ValueType VT = Op.getValueType();
2386 unsigned NumElems = Mask.getNumOperands();
2387 Mask = getUnpacklMask(NumElems, DAG);
2388 while (NumElems != 4) {
2389 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2392 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2394 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2395 Mask = getZeroVector(MaskVT, DAG);
2396 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2397 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2398 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2401 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2402 /// vector of zero or undef vector.
2403 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2404 unsigned NumElems, unsigned Idx,
2405 bool isZero, SelectionDAG &DAG) {
2406 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2407 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2408 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2409 SDOperand Zero = DAG.getConstant(0, EVT);
2410 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2411 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2412 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2413 &MaskVec[0], MaskVec.size());
2414 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2417 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2419 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2420 unsigned NumNonZero, unsigned NumZero,
2421 SelectionDAG &DAG, TargetLowering &TLI) {
2427 for (unsigned i = 0; i < 16; ++i) {
2428 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2429 if (ThisIsNonZero && First) {
2431 V = getZeroVector(MVT::v8i16, DAG);
2433 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2438 SDOperand ThisElt(0, 0), LastElt(0, 0);
2439 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2440 if (LastIsNonZero) {
2441 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2443 if (ThisIsNonZero) {
2444 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2445 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2446 ThisElt, DAG.getConstant(8, MVT::i8));
2448 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2453 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2454 DAG.getConstant(i/2, TLI.getPointerTy()));
2458 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2461 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2463 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2464 unsigned NumNonZero, unsigned NumZero,
2465 SelectionDAG &DAG, TargetLowering &TLI) {
2471 for (unsigned i = 0; i < 8; ++i) {
2472 bool isNonZero = (NonZeros & (1 << i)) != 0;
2476 V = getZeroVector(MVT::v8i16, DAG);
2478 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2481 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2482 DAG.getConstant(i, TLI.getPointerTy()));
2490 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2491 // All zero's are handled with pxor.
2492 if (ISD::isBuildVectorAllZeros(Op.Val))
2495 // All one's are handled with pcmpeqd.
2496 if (ISD::isBuildVectorAllOnes(Op.Val))
2499 MVT::ValueType VT = Op.getValueType();
2500 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2501 unsigned EVTBits = MVT::getSizeInBits(EVT);
2503 unsigned NumElems = Op.getNumOperands();
2504 unsigned NumZero = 0;
2505 unsigned NumNonZero = 0;
2506 unsigned NonZeros = 0;
2507 unsigned NumNonZeroImms = 0;
2508 std::set<SDOperand> Values;
2509 for (unsigned i = 0; i < NumElems; ++i) {
2510 SDOperand Elt = Op.getOperand(i);
2511 if (Elt.getOpcode() != ISD::UNDEF) {
2513 if (isZeroNode(Elt))
2516 NonZeros |= (1 << i);
2518 if (Elt.getOpcode() == ISD::Constant ||
2519 Elt.getOpcode() == ISD::ConstantFP)
2525 if (NumNonZero == 0) {
2527 // All undef vector. Return an UNDEF.
2528 return DAG.getNode(ISD::UNDEF, VT);
2530 // A mix of zero and undef. Return a zero vector.
2531 return getZeroVector(VT, DAG);
2534 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2535 if (Values.size() == 1)
2538 // Special case for single non-zero element.
2539 if (NumNonZero == 1) {
2540 unsigned Idx = CountTrailingZeros_32(NonZeros);
2541 SDOperand Item = Op.getOperand(Idx);
2542 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2544 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2545 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2548 if (EVTBits == 32) {
2549 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2550 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2552 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2553 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2554 SmallVector<SDOperand, 8> MaskVec;
2555 for (unsigned i = 0; i < NumElems; i++)
2556 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2557 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2558 &MaskVec[0], MaskVec.size());
2559 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2560 DAG.getNode(ISD::UNDEF, VT), Mask);
2564 // A vector full of immediates; various special cases are already
2565 // handled, so this is best done with a single constant-pool load.
2566 if (NumNonZero == NumNonZeroImms)
2569 // Let legalizer expand 2-wide build_vectors.
2573 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2574 if (EVTBits == 8 && NumElems == 16) {
2575 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2577 if (V.Val) return V;
2580 if (EVTBits == 16 && NumElems == 8) {
2581 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2583 if (V.Val) return V;
2586 // If element VT is == 32 bits, turn it into a number of shuffles.
2587 SmallVector<SDOperand, 8> V;
2589 if (NumElems == 4 && NumZero > 0) {
2590 for (unsigned i = 0; i < 4; ++i) {
2591 bool isZero = !(NonZeros & (1 << i));
2593 V[i] = getZeroVector(VT, DAG);
2595 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2598 for (unsigned i = 0; i < 2; ++i) {
2599 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2602 V[i] = V[i*2]; // Must be a zero vector.
2605 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2606 getMOVLMask(NumElems, DAG));
2609 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2610 getMOVLMask(NumElems, DAG));
2613 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2614 getUnpacklMask(NumElems, DAG));
2619 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2620 // clears the upper bits.
2621 // FIXME: we can do the same for v4f32 case when we know both parts of
2622 // the lower half come from scalar_to_vector (loadf32). We should do
2623 // that in post legalizer dag combiner with target specific hooks.
2624 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2626 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2627 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2628 SmallVector<SDOperand, 8> MaskVec;
2629 bool Reverse = (NonZeros & 0x3) == 2;
2630 for (unsigned i = 0; i < 2; ++i)
2632 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2634 MaskVec.push_back(DAG.getConstant(i, EVT));
2635 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2636 for (unsigned i = 0; i < 2; ++i)
2638 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2640 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2641 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2642 &MaskVec[0], MaskVec.size());
2643 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2646 if (Values.size() > 2) {
2647 // Expand into a number of unpckl*.
2649 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2650 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2651 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2652 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2653 for (unsigned i = 0; i < NumElems; ++i)
2654 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2656 while (NumElems != 0) {
2657 for (unsigned i = 0; i < NumElems; ++i)
2658 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2669 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2670 SDOperand V1 = Op.getOperand(0);
2671 SDOperand V2 = Op.getOperand(1);
2672 SDOperand PermMask = Op.getOperand(2);
2673 MVT::ValueType VT = Op.getValueType();
2674 unsigned NumElems = PermMask.getNumOperands();
2675 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2676 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2677 bool V1IsSplat = false;
2678 bool V2IsSplat = false;
2680 if (isUndefShuffle(Op.Val))
2681 return DAG.getNode(ISD::UNDEF, VT);
2683 if (isZeroShuffle(Op.Val))
2684 return getZeroVector(VT, DAG);
2686 if (isIdentityMask(PermMask.Val))
2688 else if (isIdentityMask(PermMask.Val, true))
2691 if (isSplatMask(PermMask.Val)) {
2692 if (NumElems <= 4) return Op;
2693 // Promote it to a v4i32 splat.
2694 return PromoteSplat(Op, DAG);
2697 if (X86::isMOVLMask(PermMask.Val))
2698 return (V1IsUndef) ? V2 : Op;
2700 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2701 X86::isMOVSLDUPMask(PermMask.Val) ||
2702 X86::isMOVHLPSMask(PermMask.Val) ||
2703 X86::isMOVHPMask(PermMask.Val) ||
2704 X86::isMOVLPMask(PermMask.Val))
2707 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2708 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2709 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2711 bool Commuted = false;
2712 V1IsSplat = isSplatVector(V1.Val);
2713 V2IsSplat = isSplatVector(V2.Val);
2714 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2715 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2716 std::swap(V1IsSplat, V2IsSplat);
2717 std::swap(V1IsUndef, V2IsUndef);
2721 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2722 if (V2IsUndef) return V1;
2723 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2725 // V2 is a splat, so the mask may be malformed. That is, it may point
2726 // to any V2 element. The instruction selectior won't like this. Get
2727 // a corrected mask and commute to form a proper MOVS{S|D}.
2728 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2729 if (NewMask.Val != PermMask.Val)
2730 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2735 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2736 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2737 X86::isUNPCKLMask(PermMask.Val) ||
2738 X86::isUNPCKHMask(PermMask.Val))
2742 // Normalize mask so all entries that point to V2 points to its first
2743 // element then try to match unpck{h|l} again. If match, return a
2744 // new vector_shuffle with the corrected mask.
2745 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2746 if (NewMask.Val != PermMask.Val) {
2747 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2748 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2749 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2750 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2751 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2752 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2757 // Normalize the node to match x86 shuffle ops if needed
2758 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2759 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2762 // Commute is back and try unpck* again.
2763 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2764 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2765 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2766 X86::isUNPCKLMask(PermMask.Val) ||
2767 X86::isUNPCKHMask(PermMask.Val))
2771 // If VT is integer, try PSHUF* first, then SHUFP*.
2772 if (MVT::isInteger(VT)) {
2773 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
2774 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
2775 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
2776 X86::isPSHUFDMask(PermMask.Val)) ||
2777 X86::isPSHUFHWMask(PermMask.Val) ||
2778 X86::isPSHUFLWMask(PermMask.Val)) {
2779 if (V2.getOpcode() != ISD::UNDEF)
2780 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2781 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2785 if (X86::isSHUFPMask(PermMask.Val) &&
2786 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
2789 // Handle v8i16 shuffle high / low shuffle node pair.
2790 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2791 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2792 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2793 SmallVector<SDOperand, 8> MaskVec;
2794 for (unsigned i = 0; i != 4; ++i)
2795 MaskVec.push_back(PermMask.getOperand(i));
2796 for (unsigned i = 4; i != 8; ++i)
2797 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2798 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2799 &MaskVec[0], MaskVec.size());
2800 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2802 for (unsigned i = 0; i != 4; ++i)
2803 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2804 for (unsigned i = 4; i != 8; ++i)
2805 MaskVec.push_back(PermMask.getOperand(i));
2806 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2807 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2810 // Floating point cases in the other order.
2811 if (X86::isSHUFPMask(PermMask.Val))
2813 if (X86::isPSHUFDMask(PermMask.Val) ||
2814 X86::isPSHUFHWMask(PermMask.Val) ||
2815 X86::isPSHUFLWMask(PermMask.Val)) {
2816 if (V2.getOpcode() != ISD::UNDEF)
2817 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2818 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2823 if (NumElems == 4 &&
2824 // Don't do this for MMX.
2825 MVT::getSizeInBits(VT) != 64) {
2826 MVT::ValueType MaskVT = PermMask.getValueType();
2827 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2828 SmallVector<std::pair<int, int>, 8> Locs;
2829 Locs.reserve(NumElems);
2830 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2831 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2834 // If no more than two elements come from either vector. This can be
2835 // implemented with two shuffles. First shuffle gather the elements.
2836 // The second shuffle, which takes the first shuffle as both of its
2837 // vector operands, put the elements into the right order.
2838 for (unsigned i = 0; i != NumElems; ++i) {
2839 SDOperand Elt = PermMask.getOperand(i);
2840 if (Elt.getOpcode() == ISD::UNDEF) {
2841 Locs[i] = std::make_pair(-1, -1);
2843 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2844 if (Val < NumElems) {
2845 Locs[i] = std::make_pair(0, NumLo);
2849 Locs[i] = std::make_pair(1, NumHi);
2850 if (2+NumHi < NumElems)
2851 Mask1[2+NumHi] = Elt;
2856 if (NumLo <= 2 && NumHi <= 2) {
2857 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2858 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2859 &Mask1[0], Mask1.size()));
2860 for (unsigned i = 0; i != NumElems; ++i) {
2861 if (Locs[i].first == -1)
2864 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2865 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2866 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2870 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2871 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2872 &Mask2[0], Mask2.size()));
2875 // Break it into (shuffle shuffle_hi, shuffle_lo).
2877 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2878 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2879 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
2880 unsigned MaskIdx = 0;
2882 unsigned HiIdx = NumElems/2;
2883 for (unsigned i = 0; i != NumElems; ++i) {
2884 if (i == NumElems/2) {
2890 SDOperand Elt = PermMask.getOperand(i);
2891 if (Elt.getOpcode() == ISD::UNDEF) {
2892 Locs[i] = std::make_pair(-1, -1);
2893 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2894 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2895 (*MaskPtr)[LoIdx] = Elt;
2898 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2899 (*MaskPtr)[HiIdx] = Elt;
2904 SDOperand LoShuffle =
2905 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2906 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2907 &LoMask[0], LoMask.size()));
2908 SDOperand HiShuffle =
2909 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2910 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2911 &HiMask[0], HiMask.size()));
2912 SmallVector<SDOperand, 8> MaskOps;
2913 for (unsigned i = 0; i != NumElems; ++i) {
2914 if (Locs[i].first == -1) {
2915 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2917 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2918 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2921 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2922 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2923 &MaskOps[0], MaskOps.size()));
2930 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2931 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2934 MVT::ValueType VT = Op.getValueType();
2935 // TODO: handle v16i8.
2936 if (MVT::getSizeInBits(VT) == 16) {
2937 // Transform it so it match pextrw which produces a 32-bit result.
2938 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2939 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2940 Op.getOperand(0), Op.getOperand(1));
2941 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2942 DAG.getValueType(VT));
2943 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2944 } else if (MVT::getSizeInBits(VT) == 32) {
2945 SDOperand Vec = Op.getOperand(0);
2946 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2949 // SHUFPS the element to the lowest double word, then movss.
2950 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2951 SmallVector<SDOperand, 8> IdxVec;
2952 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
2953 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2954 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2955 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2956 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2957 &IdxVec[0], IdxVec.size());
2958 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2959 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2960 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2961 DAG.getConstant(0, getPointerTy()));
2962 } else if (MVT::getSizeInBits(VT) == 64) {
2963 SDOperand Vec = Op.getOperand(0);
2964 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2968 // UNPCKHPD the element to the lowest double word, then movsd.
2969 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2970 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2971 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2972 SmallVector<SDOperand, 8> IdxVec;
2973 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
2974 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2975 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2976 &IdxVec[0], IdxVec.size());
2977 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2978 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2979 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2980 DAG.getConstant(0, getPointerTy()));
2987 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2988 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2989 // as its second argument.
2990 MVT::ValueType VT = Op.getValueType();
2991 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
2992 SDOperand N0 = Op.getOperand(0);
2993 SDOperand N1 = Op.getOperand(1);
2994 SDOperand N2 = Op.getOperand(2);
2995 if (MVT::getSizeInBits(BaseVT) == 16) {
2996 if (N1.getValueType() != MVT::i32)
2997 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2998 if (N2.getValueType() != MVT::i32)
2999 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
3000 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3001 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3002 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3005 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3006 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3007 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3008 SmallVector<SDOperand, 8> MaskVec;
3009 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3010 for (unsigned i = 1; i <= 3; ++i)
3011 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3012 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3013 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3014 &MaskVec[0], MaskVec.size()));
3016 // Use two pinsrw instructions to insert a 32 bit value.
3018 if (MVT::isFloatingPoint(N1.getValueType())) {
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()));
3024 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3025 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3026 DAG.getConstant(Idx, getPointerTy()));
3027 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3028 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3029 DAG.getConstant(Idx+1, getPointerTy()));
3030 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3038 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3039 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3040 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3043 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3044 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3045 // one of the above mentioned nodes. It has to be wrapped because otherwise
3046 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3047 // be used to form addressing mode. These wrapped nodes will be selected
3050 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3051 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3052 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3054 CP->getAlignment());
3055 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3056 // With PIC, the address is actually $g + Offset.
3057 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3058 !Subtarget->isPICStyleRIPRel()) {
3059 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3060 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3068 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3069 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3070 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3071 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3072 // With PIC, the address is actually $g + Offset.
3073 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3074 !Subtarget->isPICStyleRIPRel()) {
3075 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3076 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3080 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3081 // load the value at address GV, not the value of GV itself. This means that
3082 // the GlobalAddress must be in the base or index register of the address, not
3083 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3084 // The same applies for external symbols during PIC codegen
3085 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3086 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3091 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
3093 LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3094 const MVT::ValueType PtrVT) {
3096 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3097 DAG.getNode(X86ISD::GlobalBaseReg,
3099 InFlag = Chain.getValue(1);
3101 // emit leal symbol@TLSGD(,%ebx,1), %eax
3102 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3103 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3104 GA->getValueType(0),
3106 SDOperand Ops[] = { Chain, TGA, InFlag };
3107 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3108 InFlag = Result.getValue(2);
3109 Chain = Result.getValue(1);
3111 // call ___tls_get_addr. This function receives its argument in
3112 // the register EAX.
3113 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3114 InFlag = Chain.getValue(1);
3116 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3117 SDOperand Ops1[] = { Chain,
3118 DAG.getTargetExternalSymbol("___tls_get_addr",
3120 DAG.getRegister(X86::EAX, PtrVT),
3121 DAG.getRegister(X86::EBX, PtrVT),
3123 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3124 InFlag = Chain.getValue(1);
3126 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3129 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3130 // "local exec" model.
3132 LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3133 const MVT::ValueType PtrVT) {
3134 // Get the Thread Pointer
3135 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3136 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3138 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3139 GA->getValueType(0),
3141 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3143 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3144 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3146 // The address of the thread local variable is the add of the thread
3147 // pointer with the offset of the variable.
3148 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3152 X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3153 // TODO: implement the "local dynamic" model
3154 // TODO: implement the "initial exec"model for pic executables
3155 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3156 "TLS not implemented for non-ELF and 64-bit targets");
3157 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3158 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3159 // otherwise use the "Local Exec"TLS Model
3160 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3161 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3163 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3167 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3168 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3169 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3170 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3171 // With PIC, the address is actually $g + Offset.
3172 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3173 !Subtarget->isPICStyleRIPRel()) {
3174 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3175 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3182 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3183 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3184 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3185 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3186 // With PIC, the address is actually $g + Offset.
3187 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3188 !Subtarget->isPICStyleRIPRel()) {
3189 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3190 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3197 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3198 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3199 "Not an i64 shift!");
3200 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3201 SDOperand ShOpLo = Op.getOperand(0);
3202 SDOperand ShOpHi = Op.getOperand(1);
3203 SDOperand ShAmt = Op.getOperand(2);
3204 SDOperand Tmp1 = isSRA ?
3205 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3206 DAG.getConstant(0, MVT::i32);
3208 SDOperand Tmp2, Tmp3;
3209 if (Op.getOpcode() == ISD::SHL_PARTS) {
3210 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3211 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3213 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3214 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3217 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3218 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3219 DAG.getConstant(32, MVT::i8));
3220 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3221 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3224 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3226 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3227 SmallVector<SDOperand, 4> Ops;
3228 if (Op.getOpcode() == ISD::SHL_PARTS) {
3229 Ops.push_back(Tmp2);
3230 Ops.push_back(Tmp3);
3232 Ops.push_back(InFlag);
3233 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3234 InFlag = Hi.getValue(1);
3237 Ops.push_back(Tmp3);
3238 Ops.push_back(Tmp1);
3240 Ops.push_back(InFlag);
3241 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3243 Ops.push_back(Tmp2);
3244 Ops.push_back(Tmp3);
3246 Ops.push_back(InFlag);
3247 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3248 InFlag = Lo.getValue(1);
3251 Ops.push_back(Tmp3);
3252 Ops.push_back(Tmp1);
3254 Ops.push_back(InFlag);
3255 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3258 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3262 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3265 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3266 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3267 Op.getOperand(0).getValueType() >= MVT::i16 &&
3268 "Unknown SINT_TO_FP to lower!");
3271 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3272 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3273 MachineFunction &MF = DAG.getMachineFunction();
3274 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3275 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3276 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3277 StackSlot, NULL, 0);
3282 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3284 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
3285 SmallVector<SDOperand, 8> Ops;
3286 Ops.push_back(Chain);
3287 Ops.push_back(StackSlot);
3288 Ops.push_back(DAG.getValueType(SrcVT));
3289 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3290 Tys, &Ops[0], Ops.size());
3293 Chain = Result.getValue(1);
3294 SDOperand InFlag = Result.getValue(2);
3296 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3297 // shouldn't be necessary except that RFP cannot be live across
3298 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3299 MachineFunction &MF = DAG.getMachineFunction();
3300 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3301 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3302 Tys = DAG.getVTList(MVT::Other);
3303 SmallVector<SDOperand, 8> Ops;
3304 Ops.push_back(Chain);
3305 Ops.push_back(Result);
3306 Ops.push_back(StackSlot);
3307 Ops.push_back(DAG.getValueType(Op.getValueType()));
3308 Ops.push_back(InFlag);
3309 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3310 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3316 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3317 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3318 "Unknown FP_TO_SINT to lower!");
3319 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3321 MachineFunction &MF = DAG.getMachineFunction();
3322 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3323 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3324 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3327 switch (Op.getValueType()) {
3328 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3329 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3330 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3331 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3334 SDOperand Chain = DAG.getEntryNode();
3335 SDOperand Value = Op.getOperand(0);
3337 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3338 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3339 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
3341 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3343 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3344 Chain = Value.getValue(1);
3345 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3346 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3349 // Build the FP_TO_INT*_IN_MEM
3350 SDOperand Ops[] = { Chain, Value, StackSlot };
3351 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3354 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3357 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3358 MVT::ValueType VT = Op.getValueType();
3359 MVT::ValueType EltVT = VT;
3360 if (MVT::isVector(VT))
3361 EltVT = MVT::getVectorElementType(VT);
3362 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3363 std::vector<Constant*> CV;
3364 if (EltVT == MVT::f64) {
3365 Constant *C = ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)));
3369 Constant *C = ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)));
3375 Constant *C = ConstantVector::get(CV);
3376 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3377 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3379 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3382 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3383 MVT::ValueType VT = Op.getValueType();
3384 MVT::ValueType EltVT = VT;
3385 unsigned EltNum = 1;
3386 if (MVT::isVector(VT)) {
3387 EltVT = MVT::getVectorElementType(VT);
3388 EltNum = MVT::getVectorNumElements(VT);
3390 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3391 std::vector<Constant*> CV;
3392 if (EltVT == MVT::f64) {
3393 Constant *C = ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63));
3397 Constant *C = ConstantFP::get(OpNTy, BitsToFloat(1U << 31));
3403 Constant *C = ConstantVector::get(CV);
3404 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3405 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3407 if (MVT::isVector(VT)) {
3408 return DAG.getNode(ISD::BIT_CONVERT, VT,
3409 DAG.getNode(ISD::XOR, MVT::v2i64,
3410 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
3411 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
3413 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3417 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3418 SDOperand Op0 = Op.getOperand(0);
3419 SDOperand Op1 = Op.getOperand(1);
3420 MVT::ValueType VT = Op.getValueType();
3421 MVT::ValueType SrcVT = Op1.getValueType();
3422 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3424 // If second operand is smaller, extend it first.
3425 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3426 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3430 // First get the sign bit of second operand.
3431 std::vector<Constant*> CV;
3432 if (SrcVT == MVT::f64) {
3433 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3434 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3436 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3437 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3438 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3439 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3441 Constant *C = ConstantVector::get(CV);
3442 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3443 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
3445 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3447 // Shift sign bit right or left if the two operands have different types.
3448 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3449 // Op0 is MVT::f32, Op1 is MVT::f64.
3450 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3451 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3452 DAG.getConstant(32, MVT::i32));
3453 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3454 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3455 DAG.getConstant(0, getPointerTy()));
3458 // Clear first operand sign bit.
3460 if (VT == MVT::f64) {
3461 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3462 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3464 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3465 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3466 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3467 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3469 C = ConstantVector::get(CV);
3470 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3471 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3473 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3475 // Or the value with the sign bit.
3476 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3479 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3481 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3483 SDOperand Op0 = Op.getOperand(0);
3484 SDOperand Op1 = Op.getOperand(1);
3485 SDOperand CC = Op.getOperand(2);
3486 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3487 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3488 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3489 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3492 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3494 SDOperand Ops1[] = { Chain, Op0, Op1 };
3495 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3496 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3497 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3500 assert(isFP && "Illegal integer SetCC!");
3502 SDOperand COps[] = { Chain, Op0, Op1 };
3503 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3505 switch (SetCCOpcode) {
3506 default: assert(false && "Illegal floating point SetCC!");
3507 case ISD::SETOEQ: { // !PF & ZF
3508 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3509 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3510 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3512 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3513 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3515 case ISD::SETUNE: { // PF | !ZF
3516 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3517 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3518 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3520 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3521 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3526 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3527 bool addTest = true;
3528 SDOperand Chain = DAG.getEntryNode();
3529 SDOperand Cond = Op.getOperand(0);
3531 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3533 if (Cond.getOpcode() == ISD::SETCC)
3534 Cond = LowerSETCC(Cond, DAG, Chain);
3536 if (Cond.getOpcode() == X86ISD::SETCC) {
3537 CC = Cond.getOperand(0);
3539 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3540 // (since flag operand cannot be shared). Use it as the condition setting
3541 // operand in place of the X86ISD::SETCC.
3542 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3543 // to use a test instead of duplicating the X86ISD::CMP (for register
3544 // pressure reason)?
3545 SDOperand Cmp = Cond.getOperand(1);
3546 unsigned Opc = Cmp.getOpcode();
3547 bool IllegalFPCMov = !X86ScalarSSE &&
3548 MVT::isFloatingPoint(Op.getValueType()) &&
3549 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3550 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3552 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3553 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3559 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3560 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3561 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3564 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3565 SmallVector<SDOperand, 4> Ops;
3566 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3567 // condition is true.
3568 Ops.push_back(Op.getOperand(2));
3569 Ops.push_back(Op.getOperand(1));
3571 Ops.push_back(Cond.getValue(1));
3572 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3575 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3576 bool addTest = true;
3577 SDOperand Chain = Op.getOperand(0);
3578 SDOperand Cond = Op.getOperand(1);
3579 SDOperand Dest = Op.getOperand(2);
3581 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3583 if (Cond.getOpcode() == ISD::SETCC)
3584 Cond = LowerSETCC(Cond, DAG, Chain);
3586 if (Cond.getOpcode() == X86ISD::SETCC) {
3587 CC = Cond.getOperand(0);
3589 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3590 // (since flag operand cannot be shared). Use it as the condition setting
3591 // operand in place of the X86ISD::SETCC.
3592 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3593 // to use a test instead of duplicating the X86ISD::CMP (for register
3594 // pressure reason)?
3595 SDOperand Cmp = Cond.getOperand(1);
3596 unsigned Opc = Cmp.getOpcode();
3597 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3598 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3599 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3605 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3606 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3607 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3609 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3610 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3613 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3614 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3616 if (Subtarget->is64Bit())
3617 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3619 switch (CallingConv) {
3621 assert(0 && "Unsupported calling convention");
3622 case CallingConv::Fast:
3623 // TODO: Implement fastcc
3625 case CallingConv::C:
3626 case CallingConv::X86_StdCall:
3627 return LowerCCCCallTo(Op, DAG, CallingConv);
3628 case CallingConv::X86_FastCall:
3629 return LowerFastCCCallTo(Op, DAG, CallingConv);
3634 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3635 // Calls to _alloca is needed to probe the stack when allocating more than 4k
3636 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
3637 // that the guard pages used by the OS virtual memory manager are allocated in
3638 // correct sequence.
3640 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3641 SelectionDAG &DAG) {
3642 assert(Subtarget->isTargetCygMing() &&
3643 "This should be used only on Cygwin/Mingw targets");
3646 SDOperand Chain = Op.getOperand(0);
3647 SDOperand Size = Op.getOperand(1);
3648 // FIXME: Ensure alignment here
3652 MVT::ValueType IntPtr = getPointerTy();
3653 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
3655 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
3656 Flag = Chain.getValue(1);
3658 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3659 SDOperand Ops[] = { Chain,
3660 DAG.getTargetExternalSymbol("_alloca", IntPtr),
3661 DAG.getRegister(X86::EAX, IntPtr),
3663 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
3664 Flag = Chain.getValue(1);
3666 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
3668 std::vector<MVT::ValueType> Tys;
3669 Tys.push_back(SPTy);
3670 Tys.push_back(MVT::Other);
3671 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
3672 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
3676 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3677 MachineFunction &MF = DAG.getMachineFunction();
3678 const Function* Fn = MF.getFunction();
3679 if (Fn->hasExternalLinkage() &&
3680 Subtarget->isTargetCygMing() &&
3681 Fn->getName() == "main")
3682 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
3684 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3685 if (Subtarget->is64Bit())
3686 return LowerX86_64CCCArguments(Op, DAG);
3690 assert(0 && "Unsupported calling convention");
3691 case CallingConv::Fast:
3692 // TODO: implement fastcc.
3695 case CallingConv::C:
3696 return LowerCCCArguments(Op, DAG);
3697 case CallingConv::X86_StdCall:
3698 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
3699 return LowerCCCArguments(Op, DAG, true);
3700 case CallingConv::X86_FastCall:
3701 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
3702 return LowerFastCCArguments(Op, DAG);
3706 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3707 SDOperand InFlag(0, 0);
3708 SDOperand Chain = Op.getOperand(0);
3710 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3711 if (Align == 0) Align = 1;
3713 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3714 // If not DWORD aligned, call memset if size is less than the threshold.
3715 // It knows how to align to the right boundary first.
3716 if ((Align & 3) != 0 ||
3717 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3718 MVT::ValueType IntPtr = getPointerTy();
3719 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3720 TargetLowering::ArgListTy Args;
3721 TargetLowering::ArgListEntry Entry;
3722 Entry.Node = Op.getOperand(1);
3723 Entry.Ty = IntPtrTy;
3724 Args.push_back(Entry);
3725 // Extend the unsigned i8 argument to be an int value for the call.
3726 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3727 Entry.Ty = IntPtrTy;
3728 Args.push_back(Entry);
3729 Entry.Node = Op.getOperand(3);
3730 Args.push_back(Entry);
3731 std::pair<SDOperand,SDOperand> CallResult =
3732 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3733 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3734 return CallResult.second;
3739 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3740 unsigned BytesLeft = 0;
3741 bool TwoRepStos = false;
3744 uint64_t Val = ValC->getValue() & 255;
3746 // If the value is a constant, then we can potentially use larger sets.
3747 switch (Align & 3) {
3748 case 2: // WORD aligned
3751 Val = (Val << 8) | Val;
3753 case 0: // DWORD aligned
3756 Val = (Val << 8) | Val;
3757 Val = (Val << 16) | Val;
3758 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3761 Val = (Val << 32) | Val;
3764 default: // Byte aligned
3767 Count = Op.getOperand(3);
3771 if (AVT > MVT::i8) {
3773 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3774 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3775 BytesLeft = I->getValue() % UBytes;
3777 assert(AVT >= MVT::i32 &&
3778 "Do not use rep;stos if not at least DWORD aligned");
3779 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3780 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3785 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3787 InFlag = Chain.getValue(1);
3790 Count = Op.getOperand(3);
3791 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3792 InFlag = Chain.getValue(1);
3795 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3797 InFlag = Chain.getValue(1);
3798 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3799 Op.getOperand(1), InFlag);
3800 InFlag = Chain.getValue(1);
3802 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3803 SmallVector<SDOperand, 8> Ops;
3804 Ops.push_back(Chain);
3805 Ops.push_back(DAG.getValueType(AVT));
3806 Ops.push_back(InFlag);
3807 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3810 InFlag = Chain.getValue(1);
3811 Count = Op.getOperand(3);
3812 MVT::ValueType CVT = Count.getValueType();
3813 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3814 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3815 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3817 InFlag = Chain.getValue(1);
3818 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3820 Ops.push_back(Chain);
3821 Ops.push_back(DAG.getValueType(MVT::i8));
3822 Ops.push_back(InFlag);
3823 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3824 } else if (BytesLeft) {
3825 // Issue stores for the last 1 - 7 bytes.
3827 unsigned Val = ValC->getValue() & 255;
3828 unsigned Offset = I->getValue() - BytesLeft;
3829 SDOperand DstAddr = Op.getOperand(1);
3830 MVT::ValueType AddrVT = DstAddr.getValueType();
3831 if (BytesLeft >= 4) {
3832 Val = (Val << 8) | Val;
3833 Val = (Val << 16) | Val;
3834 Value = DAG.getConstant(Val, MVT::i32);
3835 Chain = DAG.getStore(Chain, Value,
3836 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3837 DAG.getConstant(Offset, AddrVT)),
3842 if (BytesLeft >= 2) {
3843 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3844 Chain = DAG.getStore(Chain, Value,
3845 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3846 DAG.getConstant(Offset, AddrVT)),
3851 if (BytesLeft == 1) {
3852 Value = DAG.getConstant(Val, MVT::i8);
3853 Chain = DAG.getStore(Chain, Value,
3854 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3855 DAG.getConstant(Offset, AddrVT)),
3863 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3864 SDOperand Chain = Op.getOperand(0);
3866 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3867 if (Align == 0) Align = 1;
3869 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3870 // If not DWORD aligned, call memcpy if size is less than the threshold.
3871 // It knows how to align to the right boundary first.
3872 if ((Align & 3) != 0 ||
3873 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3874 MVT::ValueType IntPtr = getPointerTy();
3875 TargetLowering::ArgListTy Args;
3876 TargetLowering::ArgListEntry Entry;
3877 Entry.Ty = getTargetData()->getIntPtrType();
3878 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3879 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3880 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
3881 std::pair<SDOperand,SDOperand> CallResult =
3882 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3883 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3884 return CallResult.second;
3889 unsigned BytesLeft = 0;
3890 bool TwoRepMovs = false;
3891 switch (Align & 3) {
3892 case 2: // WORD aligned
3895 case 0: // DWORD aligned
3897 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3900 default: // Byte aligned
3902 Count = Op.getOperand(3);
3906 if (AVT > MVT::i8) {
3908 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3909 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3910 BytesLeft = I->getValue() % UBytes;
3912 assert(AVT >= MVT::i32 &&
3913 "Do not use rep;movs if not at least DWORD aligned");
3914 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3915 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3920 SDOperand InFlag(0, 0);
3921 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3923 InFlag = Chain.getValue(1);
3924 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3925 Op.getOperand(1), InFlag);
3926 InFlag = Chain.getValue(1);
3927 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3928 Op.getOperand(2), InFlag);
3929 InFlag = Chain.getValue(1);
3931 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3932 SmallVector<SDOperand, 8> Ops;
3933 Ops.push_back(Chain);
3934 Ops.push_back(DAG.getValueType(AVT));
3935 Ops.push_back(InFlag);
3936 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3939 InFlag = Chain.getValue(1);
3940 Count = Op.getOperand(3);
3941 MVT::ValueType CVT = Count.getValueType();
3942 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3943 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3944 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3946 InFlag = Chain.getValue(1);
3947 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3949 Ops.push_back(Chain);
3950 Ops.push_back(DAG.getValueType(MVT::i8));
3951 Ops.push_back(InFlag);
3952 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3953 } else if (BytesLeft) {
3954 // Issue loads and stores for the last 1 - 7 bytes.
3955 unsigned Offset = I->getValue() - BytesLeft;
3956 SDOperand DstAddr = Op.getOperand(1);
3957 MVT::ValueType DstVT = DstAddr.getValueType();
3958 SDOperand SrcAddr = Op.getOperand(2);
3959 MVT::ValueType SrcVT = SrcAddr.getValueType();
3961 if (BytesLeft >= 4) {
3962 Value = DAG.getLoad(MVT::i32, Chain,
3963 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3964 DAG.getConstant(Offset, SrcVT)),
3966 Chain = Value.getValue(1);
3967 Chain = DAG.getStore(Chain, Value,
3968 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3969 DAG.getConstant(Offset, DstVT)),
3974 if (BytesLeft >= 2) {
3975 Value = DAG.getLoad(MVT::i16, Chain,
3976 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3977 DAG.getConstant(Offset, SrcVT)),
3979 Chain = Value.getValue(1);
3980 Chain = DAG.getStore(Chain, Value,
3981 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3982 DAG.getConstant(Offset, DstVT)),
3988 if (BytesLeft == 1) {
3989 Value = DAG.getLoad(MVT::i8, Chain,
3990 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3991 DAG.getConstant(Offset, SrcVT)),
3993 Chain = Value.getValue(1);
3994 Chain = DAG.getStore(Chain, Value,
3995 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3996 DAG.getConstant(Offset, DstVT)),
4005 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4006 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4007 SDOperand TheOp = Op.getOperand(0);
4008 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
4009 if (Subtarget->is64Bit()) {
4010 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4011 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4012 MVT::i64, Copy1.getValue(2));
4013 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4014 DAG.getConstant(32, MVT::i8));
4016 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4019 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4020 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
4023 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4024 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4025 MVT::i32, Copy1.getValue(2));
4026 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4027 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4028 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
4031 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4032 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4034 if (!Subtarget->is64Bit()) {
4035 // vastart just stores the address of the VarArgsFrameIndex slot into the
4036 // memory location argument.
4037 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4038 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4043 // gp_offset (0 - 6 * 8)
4044 // fp_offset (48 - 48 + 8 * 16)
4045 // overflow_arg_area (point to parameters coming in memory).
4047 SmallVector<SDOperand, 8> MemOps;
4048 SDOperand FIN = Op.getOperand(1);
4050 SDOperand Store = DAG.getStore(Op.getOperand(0),
4051 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4052 FIN, SV->getValue(), SV->getOffset());
4053 MemOps.push_back(Store);
4056 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4057 DAG.getConstant(4, getPointerTy()));
4058 Store = DAG.getStore(Op.getOperand(0),
4059 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4060 FIN, SV->getValue(), SV->getOffset());
4061 MemOps.push_back(Store);
4063 // Store ptr to overflow_arg_area
4064 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4065 DAG.getConstant(4, getPointerTy()));
4066 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4067 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4069 MemOps.push_back(Store);
4071 // Store ptr to reg_save_area.
4072 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4073 DAG.getConstant(8, getPointerTy()));
4074 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4075 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4077 MemOps.push_back(Store);
4078 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4081 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4082 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4083 SDOperand Chain = Op.getOperand(0);
4084 SDOperand DstPtr = Op.getOperand(1);
4085 SDOperand SrcPtr = Op.getOperand(2);
4086 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4087 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4089 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4090 SrcSV->getValue(), SrcSV->getOffset());
4091 Chain = SrcPtr.getValue(1);
4092 for (unsigned i = 0; i < 3; ++i) {
4093 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4094 SrcSV->getValue(), SrcSV->getOffset());
4095 Chain = Val.getValue(1);
4096 Chain = DAG.getStore(Chain, Val, DstPtr,
4097 DstSV->getValue(), DstSV->getOffset());
4100 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4101 DAG.getConstant(8, getPointerTy()));
4102 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4103 DAG.getConstant(8, getPointerTy()));
4109 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4110 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4112 default: return SDOperand(); // Don't custom lower most intrinsics.
4113 // Comparison intrinsics.
4114 case Intrinsic::x86_sse_comieq_ss:
4115 case Intrinsic::x86_sse_comilt_ss:
4116 case Intrinsic::x86_sse_comile_ss:
4117 case Intrinsic::x86_sse_comigt_ss:
4118 case Intrinsic::x86_sse_comige_ss:
4119 case Intrinsic::x86_sse_comineq_ss:
4120 case Intrinsic::x86_sse_ucomieq_ss:
4121 case Intrinsic::x86_sse_ucomilt_ss:
4122 case Intrinsic::x86_sse_ucomile_ss:
4123 case Intrinsic::x86_sse_ucomigt_ss:
4124 case Intrinsic::x86_sse_ucomige_ss:
4125 case Intrinsic::x86_sse_ucomineq_ss:
4126 case Intrinsic::x86_sse2_comieq_sd:
4127 case Intrinsic::x86_sse2_comilt_sd:
4128 case Intrinsic::x86_sse2_comile_sd:
4129 case Intrinsic::x86_sse2_comigt_sd:
4130 case Intrinsic::x86_sse2_comige_sd:
4131 case Intrinsic::x86_sse2_comineq_sd:
4132 case Intrinsic::x86_sse2_ucomieq_sd:
4133 case Intrinsic::x86_sse2_ucomilt_sd:
4134 case Intrinsic::x86_sse2_ucomile_sd:
4135 case Intrinsic::x86_sse2_ucomigt_sd:
4136 case Intrinsic::x86_sse2_ucomige_sd:
4137 case Intrinsic::x86_sse2_ucomineq_sd: {
4139 ISD::CondCode CC = ISD::SETCC_INVALID;
4142 case Intrinsic::x86_sse_comieq_ss:
4143 case Intrinsic::x86_sse2_comieq_sd:
4147 case Intrinsic::x86_sse_comilt_ss:
4148 case Intrinsic::x86_sse2_comilt_sd:
4152 case Intrinsic::x86_sse_comile_ss:
4153 case Intrinsic::x86_sse2_comile_sd:
4157 case Intrinsic::x86_sse_comigt_ss:
4158 case Intrinsic::x86_sse2_comigt_sd:
4162 case Intrinsic::x86_sse_comige_ss:
4163 case Intrinsic::x86_sse2_comige_sd:
4167 case Intrinsic::x86_sse_comineq_ss:
4168 case Intrinsic::x86_sse2_comineq_sd:
4172 case Intrinsic::x86_sse_ucomieq_ss:
4173 case Intrinsic::x86_sse2_ucomieq_sd:
4174 Opc = X86ISD::UCOMI;
4177 case Intrinsic::x86_sse_ucomilt_ss:
4178 case Intrinsic::x86_sse2_ucomilt_sd:
4179 Opc = X86ISD::UCOMI;
4182 case Intrinsic::x86_sse_ucomile_ss:
4183 case Intrinsic::x86_sse2_ucomile_sd:
4184 Opc = X86ISD::UCOMI;
4187 case Intrinsic::x86_sse_ucomigt_ss:
4188 case Intrinsic::x86_sse2_ucomigt_sd:
4189 Opc = X86ISD::UCOMI;
4192 case Intrinsic::x86_sse_ucomige_ss:
4193 case Intrinsic::x86_sse2_ucomige_sd:
4194 Opc = X86ISD::UCOMI;
4197 case Intrinsic::x86_sse_ucomineq_ss:
4198 case Intrinsic::x86_sse2_ucomineq_sd:
4199 Opc = X86ISD::UCOMI;
4205 SDOperand LHS = Op.getOperand(1);
4206 SDOperand RHS = Op.getOperand(2);
4207 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4209 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4210 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4211 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4212 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4213 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4214 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4215 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4220 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4221 // Depths > 0 not supported yet!
4222 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4225 // Just load the return address
4226 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4227 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4230 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4231 // Depths > 0 not supported yet!
4232 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4235 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4236 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4237 DAG.getConstant(4, getPointerTy()));
4240 SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4241 SelectionDAG &DAG) {
4242 // Is not yet supported on x86-64
4243 if (Subtarget->is64Bit())
4246 return DAG.getConstant(8, getPointerTy());
4249 SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4251 assert(!Subtarget->is64Bit() &&
4252 "Lowering of eh_return builtin is not supported yet on x86-64");
4254 MachineFunction &MF = DAG.getMachineFunction();
4255 SDOperand Chain = Op.getOperand(0);
4256 SDOperand Offset = Op.getOperand(1);
4257 SDOperand Handler = Op.getOperand(2);
4259 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4262 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4263 DAG.getConstant(-4UL, getPointerTy()));
4264 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4265 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4266 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4267 MF.addLiveOut(X86::ECX);
4269 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4270 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4273 SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4274 SelectionDAG &DAG) {
4275 SDOperand Root = Op.getOperand(0);
4276 SDOperand Trmp = Op.getOperand(1); // trampoline
4277 SDOperand FPtr = Op.getOperand(2); // nested function
4278 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4280 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4282 if (Subtarget->is64Bit()) {
4283 return SDOperand(); // not yet supported
4285 Function *Func = (Function *)
4286 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4287 unsigned CC = Func->getCallingConv();
4288 unsigned char NestReg;
4292 assert(0 && "Unsupported calling convention");
4293 case CallingConv::C:
4294 case CallingConv::Fast:
4295 case CallingConv::X86_StdCall: {
4296 // Pass 'nest' parameter in ECX.
4297 // Must be kept in sync with X86CallingConv.td
4300 // Check that ECX wasn't needed by an 'inreg' parameter.
4301 const FunctionType *FTy = Func->getFunctionType();
4302 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4304 if (Attrs && !Func->isVarArg()) {
4305 unsigned InRegCount = 0;
4308 for (FunctionType::param_iterator I = FTy->param_begin(),
4309 E = FTy->param_end(); I != E; ++I, ++Idx)
4310 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4311 // FIXME: should only count parameters that are lowered to integers.
4312 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4314 if (InRegCount > 2) {
4315 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4321 case CallingConv::X86_FastCall:
4322 // Pass 'nest' parameter in EAX.
4323 // Must be kept in sync with X86CallingConv.td
4328 SDOperand OutChains[4];
4329 SDOperand Addr, Disp;
4331 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4332 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4334 const unsigned char MOV32ri = 0xB8;
4335 const unsigned char JMP = 0xE9;
4337 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|NestReg, MVT::i8),
4338 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4340 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4341 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4342 TrmpSV->getOffset() + 1, false, 1);
4344 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4345 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4346 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4348 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4349 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4350 TrmpSV->getOffset() + 6, false, 1);
4352 return DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4);
4356 /// LowerOperation - Provide custom lowering hooks for some operations.
4358 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4359 switch (Op.getOpcode()) {
4360 default: assert(0 && "Should not custom lower this!");
4361 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4362 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4363 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4364 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4365 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4366 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4367 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4368 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
4369 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4370 case ISD::SHL_PARTS:
4371 case ISD::SRA_PARTS:
4372 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4373 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4374 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4375 case ISD::FABS: return LowerFABS(Op, DAG);
4376 case ISD::FNEG: return LowerFNEG(Op, DAG);
4377 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
4378 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
4379 case ISD::SELECT: return LowerSELECT(Op, DAG);
4380 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4381 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4382 case ISD::CALL: return LowerCALL(Op, DAG);
4383 case ISD::RET: return LowerRET(Op, DAG);
4384 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4385 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4386 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4387 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4388 case ISD::VASTART: return LowerVASTART(Op, DAG);
4389 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
4390 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4391 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4392 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4393 case ISD::FRAME_TO_ARGS_OFFSET:
4394 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
4395 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
4396 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
4397 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
4402 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4404 default: return NULL;
4405 case X86ISD::SHLD: return "X86ISD::SHLD";
4406 case X86ISD::SHRD: return "X86ISD::SHRD";
4407 case X86ISD::FAND: return "X86ISD::FAND";
4408 case X86ISD::FOR: return "X86ISD::FOR";
4409 case X86ISD::FXOR: return "X86ISD::FXOR";
4410 case X86ISD::FSRL: return "X86ISD::FSRL";
4411 case X86ISD::FILD: return "X86ISD::FILD";
4412 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4413 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4414 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4415 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4416 case X86ISD::FLD: return "X86ISD::FLD";
4417 case X86ISD::FST: return "X86ISD::FST";
4418 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4419 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4420 case X86ISD::CALL: return "X86ISD::CALL";
4421 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4422 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4423 case X86ISD::CMP: return "X86ISD::CMP";
4424 case X86ISD::COMI: return "X86ISD::COMI";
4425 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4426 case X86ISD::SETCC: return "X86ISD::SETCC";
4427 case X86ISD::CMOV: return "X86ISD::CMOV";
4428 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4429 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4430 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4431 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4432 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4433 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4434 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4435 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4436 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4437 case X86ISD::FMAX: return "X86ISD::FMAX";
4438 case X86ISD::FMIN: return "X86ISD::FMIN";
4439 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
4440 case X86ISD::FRCP: return "X86ISD::FRCP";
4441 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4442 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
4443 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
4447 // isLegalAddressingMode - Return true if the addressing mode represented
4448 // by AM is legal for this target, for a load/store of the specified type.
4449 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4450 const Type *Ty) const {
4451 // X86 supports extremely general addressing modes.
4453 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4454 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4458 // We can only fold this if we don't need an extra load.
4459 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4462 // X86-64 only supports addr of globals in small code model.
4463 if (Subtarget->is64Bit()) {
4464 if (getTargetMachine().getCodeModel() != CodeModel::Small)
4466 // If lower 4G is not available, then we must use rip-relative addressing.
4467 if (AM.BaseOffs || AM.Scale > 1)
4478 // These scales always work.
4483 // These scales are formed with basereg+scalereg. Only accept if there is
4488 default: // Other stuff never works.
4496 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4497 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4498 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4499 /// are assumed to be legal.
4501 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4502 // Only do shuffles on 128-bit vector types for now.
4503 if (MVT::getSizeInBits(VT) == 64) return false;
4504 return (Mask.Val->getNumOperands() <= 4 ||
4505 isIdentityMask(Mask.Val) ||
4506 isIdentityMask(Mask.Val, true) ||
4507 isSplatMask(Mask.Val) ||
4508 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4509 X86::isUNPCKLMask(Mask.Val) ||
4510 X86::isUNPCKHMask(Mask.Val) ||
4511 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4512 X86::isUNPCKH_v_undef_Mask(Mask.Val));
4515 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4517 SelectionDAG &DAG) const {
4518 unsigned NumElts = BVOps.size();
4519 // Only do shuffles on 128-bit vector types for now.
4520 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4521 if (NumElts == 2) return true;
4523 return (isMOVLMask(&BVOps[0], 4) ||
4524 isCommutedMOVL(&BVOps[0], 4, true) ||
4525 isSHUFPMask(&BVOps[0], 4) ||
4526 isCommutedSHUFP(&BVOps[0], 4));
4531 //===----------------------------------------------------------------------===//
4532 // X86 Scheduler Hooks
4533 //===----------------------------------------------------------------------===//
4536 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4537 MachineBasicBlock *BB) {
4538 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4539 switch (MI->getOpcode()) {
4540 default: assert(false && "Unexpected instr type to insert");
4541 case X86::CMOV_FR32:
4542 case X86::CMOV_FR64:
4543 case X86::CMOV_V4F32:
4544 case X86::CMOV_V2F64:
4545 case X86::CMOV_V2I64: {
4546 // To "insert" a SELECT_CC instruction, we actually have to insert the
4547 // diamond control-flow pattern. The incoming instruction knows the
4548 // destination vreg to set, the condition code register to branch on, the
4549 // true/false values to select between, and a branch opcode to use.
4550 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4551 ilist<MachineBasicBlock>::iterator It = BB;
4557 // cmpTY ccX, r1, r2
4559 // fallthrough --> copy0MBB
4560 MachineBasicBlock *thisMBB = BB;
4561 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4562 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4564 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4565 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4566 MachineFunction *F = BB->getParent();
4567 F->getBasicBlockList().insert(It, copy0MBB);
4568 F->getBasicBlockList().insert(It, sinkMBB);
4569 // Update machine-CFG edges by first adding all successors of the current
4570 // block to the new block which will contain the Phi node for the select.
4571 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4572 e = BB->succ_end(); i != e; ++i)
4573 sinkMBB->addSuccessor(*i);
4574 // Next, remove all successors of the current block, and add the true
4575 // and fallthrough blocks as its successors.
4576 while(!BB->succ_empty())
4577 BB->removeSuccessor(BB->succ_begin());
4578 BB->addSuccessor(copy0MBB);
4579 BB->addSuccessor(sinkMBB);
4582 // %FalseValue = ...
4583 // # fallthrough to sinkMBB
4586 // Update machine-CFG edges
4587 BB->addSuccessor(sinkMBB);
4590 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4593 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4594 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4595 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4597 delete MI; // The pseudo instruction is gone now.
4601 case X86::FP32_TO_INT16_IN_MEM:
4602 case X86::FP32_TO_INT32_IN_MEM:
4603 case X86::FP32_TO_INT64_IN_MEM:
4604 case X86::FP64_TO_INT16_IN_MEM:
4605 case X86::FP64_TO_INT32_IN_MEM:
4606 case X86::FP64_TO_INT64_IN_MEM:
4607 case X86::FP80_TO_INT16_IN_MEM:
4608 case X86::FP80_TO_INT32_IN_MEM:
4609 case X86::FP80_TO_INT64_IN_MEM: {
4610 // Change the floating point control register to use "round towards zero"
4611 // mode when truncating to an integer value.
4612 MachineFunction *F = BB->getParent();
4613 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4614 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4616 // Load the old value of the high byte of the control word...
4618 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4619 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4621 // Set the high part to be round to zero...
4622 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4625 // Reload the modified control word now...
4626 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4628 // Restore the memory image of control word to original value
4629 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4632 // Get the X86 opcode to use.
4634 switch (MI->getOpcode()) {
4635 default: assert(0 && "illegal opcode!");
4636 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
4637 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
4638 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
4639 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
4640 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
4641 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
4642 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
4643 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
4644 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
4648 MachineOperand &Op = MI->getOperand(0);
4649 if (Op.isRegister()) {
4650 AM.BaseType = X86AddressMode::RegBase;
4651 AM.Base.Reg = Op.getReg();
4653 AM.BaseType = X86AddressMode::FrameIndexBase;
4654 AM.Base.FrameIndex = Op.getFrameIndex();
4656 Op = MI->getOperand(1);
4657 if (Op.isImmediate())
4658 AM.Scale = Op.getImm();
4659 Op = MI->getOperand(2);
4660 if (Op.isImmediate())
4661 AM.IndexReg = Op.getImm();
4662 Op = MI->getOperand(3);
4663 if (Op.isGlobalAddress()) {
4664 AM.GV = Op.getGlobal();
4666 AM.Disp = Op.getImm();
4668 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4669 .addReg(MI->getOperand(4).getReg());
4671 // Reload the original control word now.
4672 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4674 delete MI; // The pseudo instruction is gone now.
4680 //===----------------------------------------------------------------------===//
4681 // X86 Optimization Hooks
4682 //===----------------------------------------------------------------------===//
4684 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4686 uint64_t &KnownZero,
4688 const SelectionDAG &DAG,
4689 unsigned Depth) const {
4690 unsigned Opc = Op.getOpcode();
4691 assert((Opc >= ISD::BUILTIN_OP_END ||
4692 Opc == ISD::INTRINSIC_WO_CHAIN ||
4693 Opc == ISD::INTRINSIC_W_CHAIN ||
4694 Opc == ISD::INTRINSIC_VOID) &&
4695 "Should use MaskedValueIsZero if you don't know whether Op"
4696 " is a target node!");
4698 KnownZero = KnownOne = 0; // Don't know anything.
4702 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4707 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
4708 /// element of the result of the vector shuffle.
4709 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4710 MVT::ValueType VT = N->getValueType(0);
4711 SDOperand PermMask = N->getOperand(2);
4712 unsigned NumElems = PermMask.getNumOperands();
4713 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4715 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4717 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4718 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4719 SDOperand Idx = PermMask.getOperand(i);
4720 if (Idx.getOpcode() == ISD::UNDEF)
4721 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4722 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4727 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4728 /// node is a GlobalAddress + an offset.
4729 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4730 unsigned Opc = N->getOpcode();
4731 if (Opc == X86ISD::Wrapper) {
4732 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4733 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4736 } else if (Opc == ISD::ADD) {
4737 SDOperand N1 = N->getOperand(0);
4738 SDOperand N2 = N->getOperand(1);
4739 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4740 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4742 Offset += V->getSignExtended();
4745 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4746 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4748 Offset += V->getSignExtended();
4756 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4758 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4759 MachineFrameInfo *MFI) {
4760 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4763 SDOperand Loc = N->getOperand(1);
4764 SDOperand BaseLoc = Base->getOperand(1);
4765 if (Loc.getOpcode() == ISD::FrameIndex) {
4766 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4768 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
4769 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4770 int FS = MFI->getObjectSize(FI);
4771 int BFS = MFI->getObjectSize(BFI);
4772 if (FS != BFS || FS != Size) return false;
4773 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4775 GlobalValue *GV1 = NULL;
4776 GlobalValue *GV2 = NULL;
4777 int64_t Offset1 = 0;
4778 int64_t Offset2 = 0;
4779 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4780 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4781 if (isGA1 && isGA2 && GV1 == GV2)
4782 return Offset1 == (Offset2 + Dist*Size);
4788 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4789 const X86Subtarget *Subtarget) {
4792 if (isGAPlusOffset(Base, GV, Offset))
4793 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4795 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4796 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
4798 // Fixed objects do not specify alignment, however the offsets are known.
4799 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4800 (MFI->getObjectOffset(BFI) % 16) == 0);
4802 return MFI->getObjectAlignment(BFI) >= 16;
4808 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4809 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4810 /// if the load addresses are consecutive, non-overlapping, and in the right
4812 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4813 const X86Subtarget *Subtarget) {
4814 MachineFunction &MF = DAG.getMachineFunction();
4815 MachineFrameInfo *MFI = MF.getFrameInfo();
4816 MVT::ValueType VT = N->getValueType(0);
4817 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4818 SDOperand PermMask = N->getOperand(2);
4819 int NumElems = (int)PermMask.getNumOperands();
4820 SDNode *Base = NULL;
4821 for (int i = 0; i < NumElems; ++i) {
4822 SDOperand Idx = PermMask.getOperand(i);
4823 if (Idx.getOpcode() == ISD::UNDEF) {
4824 if (!Base) return SDOperand();
4827 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4828 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
4832 else if (!isConsecutiveLoad(Arg.Val, Base,
4833 i, MVT::getSizeInBits(EVT)/8,MFI))
4838 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
4839 LoadSDNode *LD = cast<LoadSDNode>(Base);
4841 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4842 LD->getSrcValueOffset(), LD->isVolatile());
4844 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4845 LD->getSrcValueOffset(), LD->isVolatile(),
4846 LD->getAlignment());
4850 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4851 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4852 const X86Subtarget *Subtarget) {
4853 SDOperand Cond = N->getOperand(0);
4855 // If we have SSE[12] support, try to form min/max nodes.
4856 if (Subtarget->hasSSE2() &&
4857 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4858 if (Cond.getOpcode() == ISD::SETCC) {
4859 // Get the LHS/RHS of the select.
4860 SDOperand LHS = N->getOperand(1);
4861 SDOperand RHS = N->getOperand(2);
4862 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
4864 unsigned Opcode = 0;
4865 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
4868 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4871 if (!UnsafeFPMath) break;
4873 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4875 Opcode = X86ISD::FMIN;
4878 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4881 if (!UnsafeFPMath) break;
4883 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4885 Opcode = X86ISD::FMAX;
4888 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
4891 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4894 if (!UnsafeFPMath) break;
4896 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4898 Opcode = X86ISD::FMIN;
4901 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4904 if (!UnsafeFPMath) break;
4906 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4908 Opcode = X86ISD::FMAX;
4914 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
4923 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4924 DAGCombinerInfo &DCI) const {
4925 SelectionDAG &DAG = DCI.DAG;
4926 switch (N->getOpcode()) {
4928 case ISD::VECTOR_SHUFFLE:
4929 return PerformShuffleCombine(N, DAG, Subtarget);
4931 return PerformSELECTCombine(N, DAG, Subtarget);
4937 //===----------------------------------------------------------------------===//
4938 // X86 Inline Assembly Support
4939 //===----------------------------------------------------------------------===//
4941 /// getConstraintType - Given a constraint letter, return the type of
4942 /// constraint it is for this target.
4943 X86TargetLowering::ConstraintType
4944 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4945 if (Constraint.size() == 1) {
4946 switch (Constraint[0]) {
4955 return C_RegisterClass;
4960 return TargetLowering::getConstraintType(Constraint);
4963 /// isOperandValidForConstraint - Return the specified operand (possibly
4964 /// modified) if the specified SDOperand is valid for the specified target
4965 /// constraint letter, otherwise return null.
4966 SDOperand X86TargetLowering::
4967 isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4968 switch (Constraint) {
4971 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4972 if (C->getValue() <= 31)
4973 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
4975 return SDOperand(0,0);
4977 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4978 if (C->getValue() <= 255)
4979 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
4981 return SDOperand(0,0);
4983 // Literal immediates are always ok.
4984 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op))
4985 return DAG.getTargetConstant(CST->getValue(), Op.getValueType());
4987 // If we are in non-pic codegen mode, we allow the address of a global (with
4988 // an optional displacement) to be used with 'i'.
4989 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
4992 // Match either (GA) or (GA+C)
4994 Offset = GA->getOffset();
4995 } else if (Op.getOpcode() == ISD::ADD) {
4996 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4997 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4999 Offset = GA->getOffset()+C->getValue();
5001 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5002 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5004 Offset = GA->getOffset()+C->getValue();
5011 // If addressing this global requires a load (e.g. in PIC mode), we can't
5013 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5015 return SDOperand(0, 0);
5017 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5022 // Otherwise, not valid for this mode.
5023 return SDOperand(0, 0);
5026 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
5029 std::vector<unsigned> X86TargetLowering::
5030 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5031 MVT::ValueType VT) const {
5032 if (Constraint.size() == 1) {
5033 // FIXME: not handling fp-stack yet!
5034 switch (Constraint[0]) { // GCC X86 Constraint Letters
5035 default: break; // Unknown constraint letter
5036 case 'A': // EAX/EDX
5037 if (VT == MVT::i32 || VT == MVT::i64)
5038 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5040 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5043 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5044 else if (VT == MVT::i16)
5045 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5046 else if (VT == MVT::i8)
5047 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5052 return std::vector<unsigned>();
5055 std::pair<unsigned, const TargetRegisterClass*>
5056 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5057 MVT::ValueType VT) const {
5058 // First, see if this is a constraint that directly corresponds to an LLVM
5060 if (Constraint.size() == 1) {
5061 // GCC Constraint Letters
5062 switch (Constraint[0]) {
5064 case 'r': // GENERAL_REGS
5065 case 'R': // LEGACY_REGS
5066 case 'l': // INDEX_REGS
5067 if (VT == MVT::i64 && Subtarget->is64Bit())
5068 return std::make_pair(0U, X86::GR64RegisterClass);
5070 return std::make_pair(0U, X86::GR32RegisterClass);
5071 else if (VT == MVT::i16)
5072 return std::make_pair(0U, X86::GR16RegisterClass);
5073 else if (VT == MVT::i8)
5074 return std::make_pair(0U, X86::GR8RegisterClass);
5076 case 'y': // MMX_REGS if MMX allowed.
5077 if (!Subtarget->hasMMX()) break;
5078 return std::make_pair(0U, X86::VR64RegisterClass);
5080 case 'Y': // SSE_REGS if SSE2 allowed
5081 if (!Subtarget->hasSSE2()) break;
5083 case 'x': // SSE_REGS if SSE1 allowed
5084 if (!Subtarget->hasSSE1()) break;
5088 // Scalar SSE types.
5091 return std::make_pair(0U, X86::FR32RegisterClass);
5094 return std::make_pair(0U, X86::FR64RegisterClass);
5102 return std::make_pair(0U, X86::VR128RegisterClass);
5108 // Use the default implementation in TargetLowering to convert the register
5109 // constraint into a member of a register class.
5110 std::pair<unsigned, const TargetRegisterClass*> Res;
5111 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5113 // Not found as a standard register?
5114 if (Res.second == 0) {
5115 // GCC calls "st(0)" just plain "st".
5116 if (StringsEqualNoCase("{st}", Constraint)) {
5117 Res.first = X86::ST0;
5118 Res.second = X86::RSTRegisterClass;
5124 // Otherwise, check to see if this is a register class of the wrong value
5125 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5126 // turn into {ax},{dx}.
5127 if (Res.second->hasType(VT))
5128 return Res; // Correct type already, nothing to do.
5130 // All of the single-register GCC register classes map their values onto
5131 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5132 // really want an 8-bit or 32-bit register, map to the appropriate register
5133 // class and return the appropriate register.
5134 if (Res.second != X86::GR16RegisterClass)
5137 if (VT == MVT::i8) {
5138 unsigned DestReg = 0;
5139 switch (Res.first) {
5141 case X86::AX: DestReg = X86::AL; break;
5142 case X86::DX: DestReg = X86::DL; break;
5143 case X86::CX: DestReg = X86::CL; break;
5144 case X86::BX: DestReg = X86::BL; break;
5147 Res.first = DestReg;
5148 Res.second = Res.second = X86::GR8RegisterClass;
5150 } else if (VT == MVT::i32) {
5151 unsigned DestReg = 0;
5152 switch (Res.first) {
5154 case X86::AX: DestReg = X86::EAX; break;
5155 case X86::DX: DestReg = X86::EDX; break;
5156 case X86::CX: DestReg = X86::ECX; break;
5157 case X86::BX: DestReg = X86::EBX; break;
5158 case X86::SI: DestReg = X86::ESI; break;
5159 case X86::DI: DestReg = X86::EDI; break;
5160 case X86::BP: DestReg = X86::EBP; break;
5161 case X86::SP: DestReg = X86::ESP; break;
5164 Res.first = DestReg;
5165 Res.second = Res.second = X86::GR32RegisterClass;
5167 } else if (VT == MVT::i64) {
5168 unsigned DestReg = 0;
5169 switch (Res.first) {
5171 case X86::AX: DestReg = X86::RAX; break;
5172 case X86::DX: DestReg = X86::RDX; break;
5173 case X86::CX: DestReg = X86::RCX; break;
5174 case X86::BX: DestReg = X86::RBX; break;
5175 case X86::SI: DestReg = X86::RSI; break;
5176 case X86::DI: DestReg = X86::RDI; break;
5177 case X86::BP: DestReg = X86::RBP; break;
5178 case X86::SP: DestReg = X86::RSP; break;
5181 Res.first = DestReg;
5182 Res.second = Res.second = X86::GR64RegisterClass;