1 //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that X86 uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
16 #include "X86InstrBuilder.h"
17 #include "X86ISelLowering.h"
18 #include "X86MachineFunctionInfo.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/CallingConv.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Function.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/ADT/VectorExtras.h"
27 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
28 #include "llvm/CodeGen/CallingConvLower.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/SelectionDAG.h"
33 #include "llvm/CodeGen/SSARegMap.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Target/TargetAsmInfo.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 X86ScalarSSEf64 = Subtarget->hasSSE2();
45 X86ScalarSSEf32 = Subtarget->hasSSE1();
46 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
48 RegInfo = TM.getRegisterInfo();
50 // Set up the TargetLowering object.
52 // X86 is weird, it always uses i8 for shift amounts and setcc results.
53 setShiftAmountType(MVT::i8);
54 setSetCCResultType(MVT::i8);
55 setSetCCResultContents(ZeroOrOneSetCCResult);
56 setSchedulingPreference(SchedulingForRegPressure);
57 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
58 setStackPointerRegisterToSaveRestore(X86StackPtr);
60 if (Subtarget->isTargetDarwin()) {
61 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
62 setUseUnderscoreSetJmp(false);
63 setUseUnderscoreLongJmp(false);
64 } else if (Subtarget->isTargetMingw()) {
65 // MS runtime is weird: it exports _setjmp, but longjmp!
66 setUseUnderscoreSetJmp(true);
67 setUseUnderscoreLongJmp(false);
69 setUseUnderscoreSetJmp(true);
70 setUseUnderscoreLongJmp(true);
73 // Set up the register classes.
74 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
75 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
76 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
77 if (Subtarget->is64Bit())
78 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
80 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
82 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
84 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
85 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
86 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
88 if (Subtarget->is64Bit()) {
89 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
90 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
93 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
94 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
96 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
99 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
101 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
102 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
103 // SSE has no i16 to fp conversion, only i32
104 if (X86ScalarSSEf32) {
105 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
106 // f32 and f64 cases are Legal, f80 case is not
107 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
109 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
110 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
113 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
114 // are Legal, f80 is custom lowered.
115 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
118 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
120 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
121 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
123 if (X86ScalarSSEf32) {
124 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
125 // f32 and f64 cases are Legal, f80 case is not
126 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
128 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
129 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
132 // Handle FP_TO_UINT by promoting the destination to a larger signed
134 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
136 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
138 if (Subtarget->is64Bit()) {
139 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
140 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
142 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
143 // Expand FP_TO_UINT into a select.
144 // FIXME: We would like to use a Custom expander here eventually to do
145 // the optimal thing for SSE vs. the default expansion in the legalizer.
146 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
148 // With SSE3 we can use fisttpll to convert to a signed i64.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
152 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
153 if (!X86ScalarSSEf64) {
154 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
155 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
158 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
159 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
160 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
161 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
162 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
163 if (Subtarget->is64Bit())
164 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
165 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
166 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
167 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
168 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
169 setOperationAction(ISD::FREM , MVT::f64 , Expand);
171 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
172 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
173 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
174 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
175 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
176 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
177 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
178 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
179 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
180 if (Subtarget->is64Bit()) {
181 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
182 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
183 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
186 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
187 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
189 // These should be promoted to a larger select which is supported.
190 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
191 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
192 // X86 wants to expand cmov itself.
193 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
194 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
195 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
196 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
197 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
198 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
199 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
200 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
201 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
202 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
203 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
204 if (Subtarget->is64Bit()) {
205 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
206 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
208 // X86 ret instruction may pop stack.
209 setOperationAction(ISD::RET , MVT::Other, Custom);
210 if (!Subtarget->is64Bit())
211 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
214 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
215 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
216 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
217 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
218 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
219 if (Subtarget->is64Bit()) {
220 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
221 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
222 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
223 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
225 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
226 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
227 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
228 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
229 // X86 wants to expand memset / memcpy itself.
230 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
231 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
233 // Use the default ISD::LOCATION expansion, and tell Legalize it's
234 // ok to use DEBUG_LOC if we have an assembler that supports it.
235 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
236 if (TM.getTargetAsmInfo()->hasDotLocAndDotFile())
237 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Legal);
239 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
240 // FIXME - use subtarget debug flags
241 if (!Subtarget->isTargetDarwin() &&
242 !Subtarget->isTargetELF() &&
243 !Subtarget->isTargetCygMing())
244 setOperationAction(ISD::LABEL, MVT::Other, Expand);
246 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
247 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
248 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
249 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
250 if (Subtarget->is64Bit()) {
252 setExceptionPointerRegister(X86::RAX);
253 setExceptionSelectorRegister(X86::RDX);
255 setExceptionPointerRegister(X86::EAX);
256 setExceptionSelectorRegister(X86::EDX);
258 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
260 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
262 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
263 setOperationAction(ISD::VASTART , MVT::Other, Custom);
264 setOperationAction(ISD::VAARG , MVT::Other, Expand);
265 setOperationAction(ISD::VAEND , MVT::Other, Expand);
266 if (Subtarget->is64Bit())
267 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
269 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
271 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
272 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
273 if (Subtarget->is64Bit())
274 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
275 if (Subtarget->isTargetCygMing())
276 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
278 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
280 if (X86ScalarSSEf64) {
281 // f32 and f64 use SSE.
282 // Set up the FP register classes.
283 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
284 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
286 // Use ANDPD to simulate FABS.
287 setOperationAction(ISD::FABS , MVT::f64, Custom);
288 setOperationAction(ISD::FABS , MVT::f32, Custom);
290 // Use XORP to simulate FNEG.
291 setOperationAction(ISD::FNEG , MVT::f64, Custom);
292 setOperationAction(ISD::FNEG , MVT::f32, Custom);
294 // Use ANDPD and ORPD to simulate FCOPYSIGN.
295 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
296 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
298 // We don't support sin/cos/fmod
299 setOperationAction(ISD::FSIN , MVT::f64, Expand);
300 setOperationAction(ISD::FCOS , MVT::f64, Expand);
301 setOperationAction(ISD::FREM , MVT::f64, Expand);
302 setOperationAction(ISD::FSIN , MVT::f32, Expand);
303 setOperationAction(ISD::FCOS , MVT::f32, Expand);
304 setOperationAction(ISD::FREM , MVT::f32, Expand);
306 // Expand FP immediates into loads from the stack, except for the special
308 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
309 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
310 addLegalFPImmediate(APFloat(+0.0)); // xorpd
311 addLegalFPImmediate(APFloat(+0.0f)); // xorps
313 // Conversions to long double (in X87) go through memory.
314 setConvertAction(MVT::f32, MVT::f80, Expand);
315 setConvertAction(MVT::f64, MVT::f80, Expand);
317 // Conversions from long double (in X87) go through memory.
318 setConvertAction(MVT::f80, MVT::f32, Expand);
319 setConvertAction(MVT::f80, MVT::f64, Expand);
320 } else if (X86ScalarSSEf32) {
321 // Use SSE for f32, x87 for f64.
322 // Set up the FP register classes.
323 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
324 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
326 // Use ANDPS to simulate FABS.
327 setOperationAction(ISD::FABS , MVT::f32, Custom);
329 // Use XORP to simulate FNEG.
330 setOperationAction(ISD::FNEG , MVT::f32, Custom);
332 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
334 // Use ANDPS and ORPS to simulate FCOPYSIGN.
335 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
336 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
338 // We don't support sin/cos/fmod
339 setOperationAction(ISD::FSIN , MVT::f32, Expand);
340 setOperationAction(ISD::FCOS , MVT::f32, Expand);
341 setOperationAction(ISD::FREM , MVT::f32, Expand);
343 // Expand FP immediates into loads from the stack, except for the special
345 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
346 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
347 addLegalFPImmediate(APFloat(+0.0f)); // xorps
348 addLegalFPImmediate(APFloat(+0.0)); // FLD0
349 addLegalFPImmediate(APFloat(+1.0)); // FLD1
350 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
351 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
353 // SSE->x87 conversions go through memory.
354 setConvertAction(MVT::f32, MVT::f64, Expand);
355 setConvertAction(MVT::f32, MVT::f80, Expand);
357 // x87->SSE truncations need to go through memory.
358 setConvertAction(MVT::f80, MVT::f32, Expand);
359 setConvertAction(MVT::f64, MVT::f32, Expand);
360 // And x87->x87 truncations also.
361 setConvertAction(MVT::f80, MVT::f64, Expand);
364 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
365 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
368 // f32 and f64 in x87.
369 // Set up the FP register classes.
370 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
371 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
373 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
374 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
375 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
376 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
378 // Floating truncations need to go through memory.
379 setConvertAction(MVT::f80, MVT::f32, Expand);
380 setConvertAction(MVT::f64, MVT::f32, Expand);
381 setConvertAction(MVT::f80, MVT::f64, Expand);
384 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
385 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
388 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
389 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
390 addLegalFPImmediate(APFloat(+0.0)); // FLD0
391 addLegalFPImmediate(APFloat(+1.0)); // FLD1
392 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
393 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
394 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
395 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
396 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
397 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
400 // Long double always uses X87.
401 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
402 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
403 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
404 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
406 // First set operation action for all vector types to expand. Then we
407 // will selectively turn on ones that can be effectively codegen'd.
408 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
409 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
410 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
411 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
412 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
413 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
414 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
415 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
416 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
417 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
418 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
419 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
420 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
421 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
422 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
423 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
424 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
425 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
426 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
427 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
428 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
429 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
430 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
431 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
432 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
435 if (Subtarget->hasMMX()) {
436 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
437 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
438 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
439 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
441 // FIXME: add MMX packed arithmetics
443 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
444 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
445 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
446 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
448 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
449 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
450 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
452 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
453 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
455 setOperationAction(ISD::AND, MVT::v8i8, Promote);
456 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
457 setOperationAction(ISD::AND, MVT::v4i16, Promote);
458 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
459 setOperationAction(ISD::AND, MVT::v2i32, Promote);
460 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
461 setOperationAction(ISD::AND, MVT::v1i64, Legal);
463 setOperationAction(ISD::OR, MVT::v8i8, Promote);
464 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
465 setOperationAction(ISD::OR, MVT::v4i16, Promote);
466 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
467 setOperationAction(ISD::OR, MVT::v2i32, Promote);
468 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
469 setOperationAction(ISD::OR, MVT::v1i64, Legal);
471 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
472 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
473 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
474 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
475 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
476 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
477 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
479 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
480 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
481 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
482 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
483 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
484 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
485 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
487 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
488 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
489 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
490 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
492 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
493 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
494 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
495 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
497 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
498 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
499 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
500 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
503 if (Subtarget->hasSSE1()) {
504 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
506 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
507 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
508 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
509 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
510 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
511 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
512 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
513 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
514 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
515 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
516 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
519 if (Subtarget->hasSSE2()) {
520 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
521 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
522 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
523 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
524 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
526 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
527 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
528 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
529 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
530 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
531 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
532 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
533 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
534 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
535 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
536 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
537 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
538 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
539 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
540 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
542 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
543 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
544 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
545 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
546 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
547 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
549 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
550 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
551 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
552 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
553 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
555 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
556 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
557 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
558 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
559 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
560 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
562 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
563 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
564 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
565 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
566 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
567 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
568 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
569 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
570 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
571 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
572 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
573 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
576 // Custom lower v2i64 and v2f64 selects.
577 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
578 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
579 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
580 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
583 // We want to custom lower some of our intrinsics.
584 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
586 // We have target-specific dag combine patterns for the following nodes:
587 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
588 setTargetDAGCombine(ISD::SELECT);
590 computeRegisterProperties();
592 // FIXME: These should be based on subtarget info. Plus, the values should
593 // be smaller when we are in optimizing for size mode.
594 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
595 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
596 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
597 allowUnalignedMemoryAccesses = true; // x86 supports it!
601 //===----------------------------------------------------------------------===//
602 // Return Value Calling Convention Implementation
603 //===----------------------------------------------------------------------===//
605 #include "X86GenCallingConv.inc"
607 /// LowerRET - Lower an ISD::RET node.
608 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
609 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
611 SmallVector<CCValAssign, 16> RVLocs;
612 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
613 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
614 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
615 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
618 // If this is the first return lowered for this function, add the regs to the
619 // liveout set for the function.
620 if (DAG.getMachineFunction().liveout_empty()) {
621 for (unsigned i = 0; i != RVLocs.size(); ++i)
622 if (RVLocs[i].isRegLoc())
623 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
626 SDOperand Chain = Op.getOperand(0);
629 // Copy the result values into the output registers.
630 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
631 RVLocs[0].getLocReg() != X86::ST0) {
632 for (unsigned i = 0; i != RVLocs.size(); ++i) {
633 CCValAssign &VA = RVLocs[i];
634 assert(VA.isRegLoc() && "Can only return in registers!");
635 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
637 Flag = Chain.getValue(1);
640 // We need to handle a destination of ST0 specially, because it isn't really
642 SDOperand Value = Op.getOperand(1);
644 // If this is an FP return with ScalarSSE, we need to move the value from
645 // an XMM register onto the fp-stack.
646 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
647 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
650 // If this is a load into a scalarsse value, don't store the loaded value
651 // back to the stack, only to reload it: just replace the scalar-sse load.
652 if (ISD::isNON_EXTLoad(Value.Val) &&
653 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
654 Chain = Value.getOperand(0);
655 MemLoc = Value.getOperand(1);
657 // Spill the value to memory and reload it into top of stack.
658 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
659 MachineFunction &MF = DAG.getMachineFunction();
660 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
661 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
662 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
664 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
665 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
666 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
667 Chain = Value.getValue(1);
670 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
671 SDOperand Ops[] = { Chain, Value };
672 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
673 Flag = Chain.getValue(1);
676 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
678 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
680 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
684 /// LowerCallResult - Lower the result values of an ISD::CALL into the
685 /// appropriate copies out of appropriate physical registers. This assumes that
686 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
687 /// being lowered. The returns a SDNode with the same number of values as the
689 SDNode *X86TargetLowering::
690 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
691 unsigned CallingConv, SelectionDAG &DAG) {
693 // Assign locations to each value returned by this call.
694 SmallVector<CCValAssign, 16> RVLocs;
695 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
696 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
697 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
700 SmallVector<SDOperand, 8> ResultVals;
702 // Copy all of the result registers out of their specified physreg.
703 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
704 for (unsigned i = 0; i != RVLocs.size(); ++i) {
705 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
706 RVLocs[i].getValVT(), InFlag).getValue(1);
707 InFlag = Chain.getValue(2);
708 ResultVals.push_back(Chain.getValue(0));
711 // Copies from the FP stack are special, as ST0 isn't a valid register
712 // before the fp stackifier runs.
714 // Copy ST0 into an RFP register with FP_GET_RESULT.
715 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
716 SDOperand GROps[] = { Chain, InFlag };
717 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
718 Chain = RetVal.getValue(1);
719 InFlag = RetVal.getValue(2);
721 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
723 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
724 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
725 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
726 // shouldn't be necessary except that RFP cannot be live across
727 // multiple blocks. When stackifier is fixed, they can be uncoupled.
728 MachineFunction &MF = DAG.getMachineFunction();
729 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
730 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
732 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
734 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
735 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
736 Chain = RetVal.getValue(1);
738 ResultVals.push_back(RetVal);
741 // Merge everything together with a MERGE_VALUES node.
742 ResultVals.push_back(Chain);
743 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
744 &ResultVals[0], ResultVals.size()).Val;
748 //===----------------------------------------------------------------------===//
749 // C & StdCall Calling Convention implementation
750 //===----------------------------------------------------------------------===//
751 // StdCall calling convention seems to be standard for many Windows' API
752 // routines and around. It differs from C calling convention just a little:
753 // callee should clean up the stack, not caller. Symbols should be also
754 // decorated in some fancy way :) It doesn't support any vector arguments.
756 /// AddLiveIn - This helper function adds the specified physical register to the
757 /// MachineFunction as a live in value. It also creates a corresponding virtual
759 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
760 const TargetRegisterClass *RC) {
761 assert(RC->contains(PReg) && "Not the correct regclass!");
762 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
763 MF.addLiveIn(PReg, VReg);
767 SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
768 const CCValAssign &VA,
769 MachineFrameInfo *MFI,
770 SDOperand Root, unsigned i) {
771 // Create the nodes corresponding to a load from this parameter slot.
772 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
773 VA.getLocMemOffset());
774 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
776 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
778 if (Flags & ISD::ParamFlags::ByVal)
781 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
784 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
786 unsigned NumArgs = Op.Val->getNumValues() - 1;
787 MachineFunction &MF = DAG.getMachineFunction();
788 MachineFrameInfo *MFI = MF.getFrameInfo();
789 SDOperand Root = Op.getOperand(0);
790 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
792 // Assign locations to all of the incoming arguments.
793 SmallVector<CCValAssign, 16> ArgLocs;
794 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
795 getTargetMachine(), ArgLocs);
796 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
798 SmallVector<SDOperand, 8> ArgValues;
799 unsigned LastVal = ~0U;
800 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
801 CCValAssign &VA = ArgLocs[i];
802 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
804 assert(VA.getValNo() != LastVal &&
805 "Don't support value assigned to multiple locs yet");
806 LastVal = VA.getValNo();
809 MVT::ValueType RegVT = VA.getLocVT();
810 TargetRegisterClass *RC;
811 if (RegVT == MVT::i32)
812 RC = X86::GR32RegisterClass;
814 assert(MVT::isVector(RegVT));
815 RC = X86::VR128RegisterClass;
818 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
819 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
821 // If this is an 8 or 16-bit value, it is really passed promoted to 32
822 // bits. Insert an assert[sz]ext to capture this, then truncate to the
824 if (VA.getLocInfo() == CCValAssign::SExt)
825 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
826 DAG.getValueType(VA.getValVT()));
827 else if (VA.getLocInfo() == CCValAssign::ZExt)
828 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
829 DAG.getValueType(VA.getValVT()));
831 if (VA.getLocInfo() != CCValAssign::Full)
832 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
834 ArgValues.push_back(ArgValue);
836 assert(VA.isMemLoc());
837 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
841 unsigned StackSize = CCInfo.getNextStackOffset();
843 ArgValues.push_back(Root);
845 // If the function takes variable number of arguments, make a frame index for
846 // the start of the first vararg value... for expansion of llvm.va_start.
848 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
850 if (isStdCall && !isVarArg) {
851 BytesToPopOnReturn = StackSize; // Callee pops everything..
852 BytesCallerReserves = 0;
854 BytesToPopOnReturn = 0; // Callee pops nothing.
856 // If this is an sret function, the return should pop the hidden pointer.
858 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
859 ISD::ParamFlags::StructReturn))
860 BytesToPopOnReturn = 4;
862 BytesCallerReserves = StackSize;
865 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
867 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
868 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
870 // Return the new list of results.
871 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
872 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
875 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
877 SDOperand Chain = Op.getOperand(0);
878 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
879 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
880 SDOperand Callee = Op.getOperand(4);
881 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
883 // Analyze operands of the call, assigning locations to each operand.
884 SmallVector<CCValAssign, 16> ArgLocs;
885 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
886 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
888 // Get a count of how many bytes are to be pushed on the stack.
889 unsigned NumBytes = CCInfo.getNextStackOffset();
891 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
893 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
894 SmallVector<SDOperand, 8> MemOpChains;
898 // Walk the register/memloc assignments, inserting copies/loads.
899 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
900 CCValAssign &VA = ArgLocs[i];
901 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
903 // Promote the value if needed.
904 switch (VA.getLocInfo()) {
905 default: assert(0 && "Unknown loc info!");
906 case CCValAssign::Full: break;
907 case CCValAssign::SExt:
908 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
910 case CCValAssign::ZExt:
911 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
913 case CCValAssign::AExt:
914 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
919 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
921 assert(VA.isMemLoc());
922 if (StackPtr.Val == 0)
923 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
925 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
930 // If the first argument is an sret pointer, remember it.
931 bool isSRet = NumOps &&
932 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
933 ISD::ParamFlags::StructReturn);
935 if (!MemOpChains.empty())
936 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
937 &MemOpChains[0], MemOpChains.size());
939 // Build a sequence of copy-to-reg nodes chained together with token chain
940 // and flag operands which copy the outgoing args into registers.
942 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
943 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
945 InFlag = Chain.getValue(1);
948 // ELF / PIC requires GOT in the EBX register before function calls via PLT
950 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
951 Subtarget->isPICStyleGOT()) {
952 Chain = DAG.getCopyToReg(Chain, X86::EBX,
953 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
955 InFlag = Chain.getValue(1);
958 // If the callee is a GlobalAddress node (quite common, every direct call is)
959 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
960 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
961 // We should use extra load for direct calls to dllimported functions in
963 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
964 getTargetMachine(), true))
965 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
966 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
967 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
969 // Returns a chain & a flag for retval copy to use.
970 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
971 SmallVector<SDOperand, 8> Ops;
972 Ops.push_back(Chain);
973 Ops.push_back(Callee);
975 // Add argument registers to the end of the list so that they are known live
977 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
978 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
979 RegsToPass[i].second.getValueType()));
981 // Add an implicit use GOT pointer in EBX.
982 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
983 Subtarget->isPICStyleGOT())
984 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
987 Ops.push_back(InFlag);
989 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
990 NodeTys, &Ops[0], Ops.size());
991 InFlag = Chain.getValue(1);
993 // Create the CALLSEQ_END node.
994 unsigned NumBytesForCalleeToPush = 0;
996 if (CC == CallingConv::X86_StdCall) {
998 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1000 NumBytesForCalleeToPush = NumBytes;
1002 // If this is is a call to a struct-return function, the callee
1003 // pops the hidden struct pointer, so we have to push it back.
1004 // This is common for Darwin/X86, Linux & Mingw32 targets.
1005 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1008 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1010 Ops.push_back(Chain);
1011 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1012 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
1013 Ops.push_back(InFlag);
1014 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1015 InFlag = Chain.getValue(1);
1017 // Handle result values, copying them out of physregs into vregs that we
1019 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1023 //===----------------------------------------------------------------------===//
1024 // FastCall Calling Convention implementation
1025 //===----------------------------------------------------------------------===//
1027 // The X86 'fastcall' calling convention passes up to two integer arguments in
1028 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1029 // and requires that the callee pop its arguments off the stack (allowing proper
1030 // tail calls), and has the same return value conventions as C calling convs.
1032 // This calling convention always arranges for the callee pop value to be 8n+4
1033 // bytes, which is needed for tail recursion elimination and stack alignment
1036 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1037 MachineFunction &MF = DAG.getMachineFunction();
1038 MachineFrameInfo *MFI = MF.getFrameInfo();
1039 SDOperand Root = Op.getOperand(0);
1040 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1042 // Assign locations to all of the incoming arguments.
1043 SmallVector<CCValAssign, 16> ArgLocs;
1044 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1045 getTargetMachine(), ArgLocs);
1046 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
1048 SmallVector<SDOperand, 8> ArgValues;
1049 unsigned LastVal = ~0U;
1050 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1051 CCValAssign &VA = ArgLocs[i];
1052 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1054 assert(VA.getValNo() != LastVal &&
1055 "Don't support value assigned to multiple locs yet");
1056 LastVal = VA.getValNo();
1058 if (VA.isRegLoc()) {
1059 MVT::ValueType RegVT = VA.getLocVT();
1060 TargetRegisterClass *RC;
1061 if (RegVT == MVT::i32)
1062 RC = X86::GR32RegisterClass;
1064 assert(MVT::isVector(RegVT));
1065 RC = X86::VR128RegisterClass;
1068 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1069 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1071 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1072 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1074 if (VA.getLocInfo() == CCValAssign::SExt)
1075 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1076 DAG.getValueType(VA.getValVT()));
1077 else if (VA.getLocInfo() == CCValAssign::ZExt)
1078 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1079 DAG.getValueType(VA.getValVT()));
1081 if (VA.getLocInfo() != CCValAssign::Full)
1082 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1084 ArgValues.push_back(ArgValue);
1086 assert(VA.isMemLoc());
1087 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
1091 ArgValues.push_back(Root);
1093 unsigned StackSize = CCInfo.getNextStackOffset();
1095 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1096 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1097 // arguments and the arguments after the retaddr has been pushed are aligned.
1098 if ((StackSize & 7) == 0)
1102 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1103 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1104 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
1105 BytesCallerReserves = 0;
1107 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1108 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1110 // Return the new list of results.
1111 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1112 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1116 X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1117 const SDOperand &StackPtr,
1118 const CCValAssign &VA,
1121 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1122 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1123 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1124 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1125 if (Flags & ISD::ParamFlags::ByVal) {
1126 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1127 ISD::ParamFlags::ByValAlignOffs);
1129 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1130 ISD::ParamFlags::ByValSizeOffs;
1132 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1133 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1135 return DAG.getNode(ISD::MEMCPY, MVT::Other, Chain, PtrOff, Arg, SizeNode,
1138 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1142 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1144 SDOperand Chain = Op.getOperand(0);
1145 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1146 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1147 SDOperand Callee = Op.getOperand(4);
1149 // Analyze operands of the call, assigning locations to each operand.
1150 SmallVector<CCValAssign, 16> ArgLocs;
1151 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1152 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
1154 // Get a count of how many bytes are to be pushed on the stack.
1155 unsigned NumBytes = CCInfo.getNextStackOffset();
1157 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1158 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1159 // arguments and the arguments after the retaddr has been pushed are aligned.
1160 if ((NumBytes & 7) == 0)
1164 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1166 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1167 SmallVector<SDOperand, 8> MemOpChains;
1171 // Walk the register/memloc assignments, inserting copies/loads.
1172 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1173 CCValAssign &VA = ArgLocs[i];
1174 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1176 // Promote the value if needed.
1177 switch (VA.getLocInfo()) {
1178 default: assert(0 && "Unknown loc info!");
1179 case CCValAssign::Full: break;
1180 case CCValAssign::SExt:
1181 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1183 case CCValAssign::ZExt:
1184 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1186 case CCValAssign::AExt:
1187 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1191 if (VA.isRegLoc()) {
1192 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1194 assert(VA.isMemLoc());
1195 if (StackPtr.Val == 0)
1196 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1198 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1203 if (!MemOpChains.empty())
1204 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1205 &MemOpChains[0], MemOpChains.size());
1207 // Build a sequence of copy-to-reg nodes chained together with token chain
1208 // and flag operands which copy the outgoing args into registers.
1210 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1211 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1213 InFlag = Chain.getValue(1);
1216 // If the callee is a GlobalAddress node (quite common, every direct call is)
1217 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1218 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1219 // We should use extra load for direct calls to dllimported functions in
1221 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1222 getTargetMachine(), true))
1223 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1224 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1225 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1227 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1229 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1230 Subtarget->isPICStyleGOT()) {
1231 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1232 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1234 InFlag = Chain.getValue(1);
1237 // Returns a chain & a flag for retval copy to use.
1238 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1239 SmallVector<SDOperand, 8> Ops;
1240 Ops.push_back(Chain);
1241 Ops.push_back(Callee);
1243 // Add argument registers to the end of the list so that they are known live
1245 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1246 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1247 RegsToPass[i].second.getValueType()));
1249 // Add an implicit use GOT pointer in EBX.
1250 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1251 Subtarget->isPICStyleGOT())
1252 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1255 Ops.push_back(InFlag);
1257 // FIXME: Do not generate X86ISD::TAILCALL for now.
1258 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1259 NodeTys, &Ops[0], Ops.size());
1260 InFlag = Chain.getValue(1);
1262 // Returns a flag for retval copy to use.
1263 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1265 Ops.push_back(Chain);
1266 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1267 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1268 Ops.push_back(InFlag);
1269 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1270 InFlag = Chain.getValue(1);
1272 // Handle result values, copying them out of physregs into vregs that we
1274 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1278 //===----------------------------------------------------------------------===//
1279 // X86-64 C Calling Convention implementation
1280 //===----------------------------------------------------------------------===//
1283 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1284 MachineFunction &MF = DAG.getMachineFunction();
1285 MachineFrameInfo *MFI = MF.getFrameInfo();
1286 SDOperand Root = Op.getOperand(0);
1287 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1289 static const unsigned GPR64ArgRegs[] = {
1290 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1292 static const unsigned XMMArgRegs[] = {
1293 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1294 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1298 // Assign locations to all of the incoming arguments.
1299 SmallVector<CCValAssign, 16> ArgLocs;
1300 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1301 getTargetMachine(), ArgLocs);
1302 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1304 SmallVector<SDOperand, 8> ArgValues;
1305 unsigned LastVal = ~0U;
1306 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1307 CCValAssign &VA = ArgLocs[i];
1308 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1310 assert(VA.getValNo() != LastVal &&
1311 "Don't support value assigned to multiple locs yet");
1312 LastVal = VA.getValNo();
1314 if (VA.isRegLoc()) {
1315 MVT::ValueType RegVT = VA.getLocVT();
1316 TargetRegisterClass *RC;
1317 if (RegVT == MVT::i32)
1318 RC = X86::GR32RegisterClass;
1319 else if (RegVT == MVT::i64)
1320 RC = X86::GR64RegisterClass;
1321 else if (RegVT == MVT::f32)
1322 RC = X86::FR32RegisterClass;
1323 else if (RegVT == MVT::f64)
1324 RC = X86::FR64RegisterClass;
1326 assert(MVT::isVector(RegVT));
1327 if (MVT::getSizeInBits(RegVT) == 64) {
1328 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1331 RC = X86::VR128RegisterClass;
1334 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1335 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1337 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1338 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1340 if (VA.getLocInfo() == CCValAssign::SExt)
1341 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1342 DAG.getValueType(VA.getValVT()));
1343 else if (VA.getLocInfo() == CCValAssign::ZExt)
1344 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1345 DAG.getValueType(VA.getValVT()));
1347 if (VA.getLocInfo() != CCValAssign::Full)
1348 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1350 // Handle MMX values passed in GPRs.
1351 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1352 MVT::getSizeInBits(RegVT) == 64)
1353 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1355 ArgValues.push_back(ArgValue);
1357 assert(VA.isMemLoc());
1358 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
1362 unsigned StackSize = CCInfo.getNextStackOffset();
1364 // If the function takes variable number of arguments, make a frame index for
1365 // the start of the first vararg value... for expansion of llvm.va_start.
1367 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1368 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1370 // For X86-64, if there are vararg parameters that are passed via
1371 // registers, then we must store them to their spots on the stack so they
1372 // may be loaded by deferencing the result of va_next.
1373 VarArgsGPOffset = NumIntRegs * 8;
1374 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1375 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1376 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1378 // Store the integer parameter registers.
1379 SmallVector<SDOperand, 8> MemOps;
1380 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1381 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1382 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1383 for (; NumIntRegs != 6; ++NumIntRegs) {
1384 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1385 X86::GR64RegisterClass);
1386 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1387 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1388 MemOps.push_back(Store);
1389 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1390 DAG.getConstant(8, getPointerTy()));
1393 // Now store the XMM (fp + vector) parameter registers.
1394 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1395 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1396 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1397 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1398 X86::VR128RegisterClass);
1399 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1400 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1401 MemOps.push_back(Store);
1402 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1403 DAG.getConstant(16, getPointerTy()));
1405 if (!MemOps.empty())
1406 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1407 &MemOps[0], MemOps.size());
1410 ArgValues.push_back(Root);
1412 BytesToPopOnReturn = 0; // Callee pops nothing.
1413 BytesCallerReserves = StackSize;
1415 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1416 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1418 // Return the new list of results.
1419 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1420 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1424 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1426 SDOperand Chain = Op.getOperand(0);
1427 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1428 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1429 SDOperand Callee = Op.getOperand(4);
1431 // Analyze operands of the call, assigning locations to each operand.
1432 SmallVector<CCValAssign, 16> ArgLocs;
1433 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1434 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1436 // Get a count of how many bytes are to be pushed on the stack.
1437 unsigned NumBytes = CCInfo.getNextStackOffset();
1438 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1440 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1441 SmallVector<SDOperand, 8> MemOpChains;
1445 // Walk the register/memloc assignments, inserting copies/loads.
1446 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1447 CCValAssign &VA = ArgLocs[i];
1448 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1450 // Promote the value if needed.
1451 switch (VA.getLocInfo()) {
1452 default: assert(0 && "Unknown loc info!");
1453 case CCValAssign::Full: break;
1454 case CCValAssign::SExt:
1455 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1457 case CCValAssign::ZExt:
1458 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1460 case CCValAssign::AExt:
1461 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1465 if (VA.isRegLoc()) {
1466 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1468 assert(VA.isMemLoc());
1469 if (StackPtr.Val == 0)
1470 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1472 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1477 if (!MemOpChains.empty())
1478 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1479 &MemOpChains[0], MemOpChains.size());
1481 // Build a sequence of copy-to-reg nodes chained together with token chain
1482 // and flag operands which copy the outgoing args into registers.
1484 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1485 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1487 InFlag = Chain.getValue(1);
1491 // From AMD64 ABI document:
1492 // For calls that may call functions that use varargs or stdargs
1493 // (prototype-less calls or calls to functions containing ellipsis (...) in
1494 // the declaration) %al is used as hidden argument to specify the number
1495 // of SSE registers used. The contents of %al do not need to match exactly
1496 // the number of registers, but must be an ubound on the number of SSE
1497 // registers used and is in the range 0 - 8 inclusive.
1499 // Count the number of XMM registers allocated.
1500 static const unsigned XMMArgRegs[] = {
1501 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1502 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1504 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1506 Chain = DAG.getCopyToReg(Chain, X86::AL,
1507 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1508 InFlag = Chain.getValue(1);
1511 // If the callee is a GlobalAddress node (quite common, every direct call is)
1512 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1513 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1514 // We should use extra load for direct calls to dllimported functions in
1516 if (getTargetMachine().getCodeModel() != CodeModel::Large
1517 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1518 getTargetMachine(), true))
1519 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1520 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1521 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1522 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1524 // Returns a chain & a flag for retval copy to use.
1525 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1526 SmallVector<SDOperand, 8> Ops;
1527 Ops.push_back(Chain);
1528 Ops.push_back(Callee);
1530 // Add argument registers to the end of the list so that they are known live
1532 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1533 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1534 RegsToPass[i].second.getValueType()));
1537 Ops.push_back(InFlag);
1539 // FIXME: Do not generate X86ISD::TAILCALL for now.
1540 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1541 NodeTys, &Ops[0], Ops.size());
1542 InFlag = Chain.getValue(1);
1544 // Returns a flag for retval copy to use.
1545 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1547 Ops.push_back(Chain);
1548 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1549 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1550 Ops.push_back(InFlag);
1551 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1552 InFlag = Chain.getValue(1);
1554 // Handle result values, copying them out of physregs into vregs that we
1556 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1560 //===----------------------------------------------------------------------===//
1561 // Other Lowering Hooks
1562 //===----------------------------------------------------------------------===//
1565 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1566 MachineFunction &MF = DAG.getMachineFunction();
1567 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1568 int ReturnAddrIndex = FuncInfo->getRAIndex();
1570 if (ReturnAddrIndex == 0) {
1571 // Set up a frame object for the return address.
1572 if (Subtarget->is64Bit())
1573 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1575 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1577 FuncInfo->setRAIndex(ReturnAddrIndex);
1580 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1585 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1586 /// specific condition code. It returns a false if it cannot do a direct
1587 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1589 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1590 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1591 SelectionDAG &DAG) {
1592 X86CC = X86::COND_INVALID;
1594 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1595 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1596 // X > -1 -> X == 0, jump !sign.
1597 RHS = DAG.getConstant(0, RHS.getValueType());
1598 X86CC = X86::COND_NS;
1600 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1601 // X < 0 -> X == 0, jump on sign.
1602 X86CC = X86::COND_S;
1604 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1606 RHS = DAG.getConstant(0, RHS.getValueType());
1607 X86CC = X86::COND_LE;
1612 switch (SetCCOpcode) {
1614 case ISD::SETEQ: X86CC = X86::COND_E; break;
1615 case ISD::SETGT: X86CC = X86::COND_G; break;
1616 case ISD::SETGE: X86CC = X86::COND_GE; break;
1617 case ISD::SETLT: X86CC = X86::COND_L; break;
1618 case ISD::SETLE: X86CC = X86::COND_LE; break;
1619 case ISD::SETNE: X86CC = X86::COND_NE; break;
1620 case ISD::SETULT: X86CC = X86::COND_B; break;
1621 case ISD::SETUGT: X86CC = X86::COND_A; break;
1622 case ISD::SETULE: X86CC = X86::COND_BE; break;
1623 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1626 // On a floating point condition, the flags are set as follows:
1628 // 0 | 0 | 0 | X > Y
1629 // 0 | 0 | 1 | X < Y
1630 // 1 | 0 | 0 | X == Y
1631 // 1 | 1 | 1 | unordered
1633 switch (SetCCOpcode) {
1636 case ISD::SETEQ: X86CC = X86::COND_E; break;
1637 case ISD::SETOLT: Flip = true; // Fallthrough
1639 case ISD::SETGT: X86CC = X86::COND_A; break;
1640 case ISD::SETOLE: Flip = true; // Fallthrough
1642 case ISD::SETGE: X86CC = X86::COND_AE; break;
1643 case ISD::SETUGT: Flip = true; // Fallthrough
1645 case ISD::SETLT: X86CC = X86::COND_B; break;
1646 case ISD::SETUGE: Flip = true; // Fallthrough
1648 case ISD::SETLE: X86CC = X86::COND_BE; break;
1650 case ISD::SETNE: X86CC = X86::COND_NE; break;
1651 case ISD::SETUO: X86CC = X86::COND_P; break;
1652 case ISD::SETO: X86CC = X86::COND_NP; break;
1655 std::swap(LHS, RHS);
1658 return X86CC != X86::COND_INVALID;
1661 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1662 /// code. Current x86 isa includes the following FP cmov instructions:
1663 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1664 static bool hasFPCMov(unsigned X86CC) {
1680 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1681 /// true if Op is undef or if its value falls within the specified range (L, H].
1682 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1683 if (Op.getOpcode() == ISD::UNDEF)
1686 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1687 return (Val >= Low && Val < Hi);
1690 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1691 /// true if Op is undef or if its value equal to the specified value.
1692 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1693 if (Op.getOpcode() == ISD::UNDEF)
1695 return cast<ConstantSDNode>(Op)->getValue() == Val;
1698 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1699 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1700 bool X86::isPSHUFDMask(SDNode *N) {
1701 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1703 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
1706 // Check if the value doesn't reference the second vector.
1707 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1708 SDOperand Arg = N->getOperand(i);
1709 if (Arg.getOpcode() == ISD::UNDEF) continue;
1710 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1711 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
1718 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1719 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1720 bool X86::isPSHUFHWMask(SDNode *N) {
1721 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1723 if (N->getNumOperands() != 8)
1726 // Lower quadword copied in order.
1727 for (unsigned i = 0; i != 4; ++i) {
1728 SDOperand Arg = N->getOperand(i);
1729 if (Arg.getOpcode() == ISD::UNDEF) continue;
1730 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1731 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1735 // Upper quadword shuffled.
1736 for (unsigned i = 4; i != 8; ++i) {
1737 SDOperand Arg = N->getOperand(i);
1738 if (Arg.getOpcode() == ISD::UNDEF) continue;
1739 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1740 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1741 if (Val < 4 || Val > 7)
1748 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1749 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1750 bool X86::isPSHUFLWMask(SDNode *N) {
1751 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1753 if (N->getNumOperands() != 8)
1756 // Upper quadword copied in order.
1757 for (unsigned i = 4; i != 8; ++i)
1758 if (!isUndefOrEqual(N->getOperand(i), i))
1761 // Lower quadword shuffled.
1762 for (unsigned i = 0; i != 4; ++i)
1763 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1769 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1770 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1771 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1772 if (NumElems != 2 && NumElems != 4) return false;
1774 unsigned Half = NumElems / 2;
1775 for (unsigned i = 0; i < Half; ++i)
1776 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1778 for (unsigned i = Half; i < NumElems; ++i)
1779 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1785 bool X86::isSHUFPMask(SDNode *N) {
1786 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1787 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1790 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly
1791 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1792 /// half elements to come from vector 1 (which would equal the dest.) and
1793 /// the upper half to come from vector 2.
1794 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1795 if (NumOps != 2 && NumOps != 4) return false;
1797 unsigned Half = NumOps / 2;
1798 for (unsigned i = 0; i < Half; ++i)
1799 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1801 for (unsigned i = Half; i < NumOps; ++i)
1802 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1807 static bool isCommutedSHUFP(SDNode *N) {
1808 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1809 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1812 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1813 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1814 bool X86::isMOVHLPSMask(SDNode *N) {
1815 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1817 if (N->getNumOperands() != 4)
1820 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1821 return isUndefOrEqual(N->getOperand(0), 6) &&
1822 isUndefOrEqual(N->getOperand(1), 7) &&
1823 isUndefOrEqual(N->getOperand(2), 2) &&
1824 isUndefOrEqual(N->getOperand(3), 3);
1827 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1828 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1830 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1831 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1833 if (N->getNumOperands() != 4)
1836 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1837 return isUndefOrEqual(N->getOperand(0), 2) &&
1838 isUndefOrEqual(N->getOperand(1), 3) &&
1839 isUndefOrEqual(N->getOperand(2), 2) &&
1840 isUndefOrEqual(N->getOperand(3), 3);
1843 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1844 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1845 bool X86::isMOVLPMask(SDNode *N) {
1846 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1848 unsigned NumElems = N->getNumOperands();
1849 if (NumElems != 2 && NumElems != 4)
1852 for (unsigned i = 0; i < NumElems/2; ++i)
1853 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1856 for (unsigned i = NumElems/2; i < NumElems; ++i)
1857 if (!isUndefOrEqual(N->getOperand(i), i))
1863 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1864 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1866 bool X86::isMOVHPMask(SDNode *N) {
1867 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1869 unsigned NumElems = N->getNumOperands();
1870 if (NumElems != 2 && NumElems != 4)
1873 for (unsigned i = 0; i < NumElems/2; ++i)
1874 if (!isUndefOrEqual(N->getOperand(i), i))
1877 for (unsigned i = 0; i < NumElems/2; ++i) {
1878 SDOperand Arg = N->getOperand(i + NumElems/2);
1879 if (!isUndefOrEqual(Arg, i + NumElems))
1886 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1887 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1888 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1889 bool V2IsSplat = false) {
1890 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1893 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1894 SDOperand BitI = Elts[i];
1895 SDOperand BitI1 = Elts[i+1];
1896 if (!isUndefOrEqual(BitI, j))
1899 if (isUndefOrEqual(BitI1, NumElts))
1902 if (!isUndefOrEqual(BitI1, j + NumElts))
1910 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1911 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1912 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1915 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1916 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1917 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1918 bool V2IsSplat = false) {
1919 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1922 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1923 SDOperand BitI = Elts[i];
1924 SDOperand BitI1 = Elts[i+1];
1925 if (!isUndefOrEqual(BitI, j + NumElts/2))
1928 if (isUndefOrEqual(BitI1, NumElts))
1931 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1939 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1940 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1941 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1944 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1945 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1947 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1948 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1950 unsigned NumElems = N->getNumOperands();
1951 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1954 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1955 SDOperand BitI = N->getOperand(i);
1956 SDOperand BitI1 = N->getOperand(i+1);
1958 if (!isUndefOrEqual(BitI, j))
1960 if (!isUndefOrEqual(BitI1, j))
1967 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1968 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1970 bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1971 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1973 unsigned NumElems = N->getNumOperands();
1974 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1977 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1978 SDOperand BitI = N->getOperand(i);
1979 SDOperand BitI1 = N->getOperand(i + 1);
1981 if (!isUndefOrEqual(BitI, j))
1983 if (!isUndefOrEqual(BitI1, j))
1990 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1991 /// specifies a shuffle of elements that is suitable for input to MOVSS,
1992 /// MOVSD, and MOVD, i.e. setting the lowest element.
1993 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1994 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1997 if (!isUndefOrEqual(Elts[0], NumElts))
2000 for (unsigned i = 1; i < NumElts; ++i) {
2001 if (!isUndefOrEqual(Elts[i], i))
2008 bool X86::isMOVLMask(SDNode *N) {
2009 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2010 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2013 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2014 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2015 /// element of vector 2 and the other elements to come from vector 1 in order.
2016 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2017 bool V2IsSplat = false,
2018 bool V2IsUndef = false) {
2019 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2022 if (!isUndefOrEqual(Ops[0], 0))
2025 for (unsigned i = 1; i < NumOps; ++i) {
2026 SDOperand Arg = Ops[i];
2027 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2028 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2029 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2036 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2037 bool V2IsUndef = false) {
2038 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2039 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2040 V2IsSplat, V2IsUndef);
2043 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2044 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2045 bool X86::isMOVSHDUPMask(SDNode *N) {
2046 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2048 if (N->getNumOperands() != 4)
2051 // Expect 1, 1, 3, 3
2052 for (unsigned i = 0; i < 2; ++i) {
2053 SDOperand Arg = N->getOperand(i);
2054 if (Arg.getOpcode() == ISD::UNDEF) continue;
2055 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2056 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2057 if (Val != 1) return false;
2061 for (unsigned i = 2; i < 4; ++i) {
2062 SDOperand Arg = N->getOperand(i);
2063 if (Arg.getOpcode() == ISD::UNDEF) continue;
2064 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2065 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2066 if (Val != 3) return false;
2070 // Don't use movshdup if it can be done with a shufps.
2074 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2075 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2076 bool X86::isMOVSLDUPMask(SDNode *N) {
2077 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2079 if (N->getNumOperands() != 4)
2082 // Expect 0, 0, 2, 2
2083 for (unsigned i = 0; i < 2; ++i) {
2084 SDOperand Arg = N->getOperand(i);
2085 if (Arg.getOpcode() == ISD::UNDEF) continue;
2086 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2087 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2088 if (Val != 0) return false;
2092 for (unsigned i = 2; i < 4; ++i) {
2093 SDOperand Arg = N->getOperand(i);
2094 if (Arg.getOpcode() == ISD::UNDEF) continue;
2095 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2096 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2097 if (Val != 2) return false;
2101 // Don't use movshdup if it can be done with a shufps.
2105 /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2106 /// specifies a identity operation on the LHS or RHS.
2107 static bool isIdentityMask(SDNode *N, bool RHS = false) {
2108 unsigned NumElems = N->getNumOperands();
2109 for (unsigned i = 0; i < NumElems; ++i)
2110 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2115 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2116 /// a splat of a single element.
2117 static bool isSplatMask(SDNode *N) {
2118 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2120 // This is a splat operation if each element of the permute is the same, and
2121 // if the value doesn't reference the second vector.
2122 unsigned NumElems = N->getNumOperands();
2123 SDOperand ElementBase;
2125 for (; i != NumElems; ++i) {
2126 SDOperand Elt = N->getOperand(i);
2127 if (isa<ConstantSDNode>(Elt)) {
2133 if (!ElementBase.Val)
2136 for (; i != NumElems; ++i) {
2137 SDOperand Arg = N->getOperand(i);
2138 if (Arg.getOpcode() == ISD::UNDEF) continue;
2139 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2140 if (Arg != ElementBase) return false;
2143 // Make sure it is a splat of the first vector operand.
2144 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2147 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2148 /// a splat of a single element and it's a 2 or 4 element mask.
2149 bool X86::isSplatMask(SDNode *N) {
2150 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2152 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2153 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2155 return ::isSplatMask(N);
2158 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2159 /// specifies a splat of zero element.
2160 bool X86::isSplatLoMask(SDNode *N) {
2161 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2163 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2164 if (!isUndefOrEqual(N->getOperand(i), 0))
2169 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2170 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2172 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2173 unsigned NumOperands = N->getNumOperands();
2174 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2176 for (unsigned i = 0; i < NumOperands; ++i) {
2178 SDOperand Arg = N->getOperand(NumOperands-i-1);
2179 if (Arg.getOpcode() != ISD::UNDEF)
2180 Val = cast<ConstantSDNode>(Arg)->getValue();
2181 if (Val >= NumOperands) Val -= NumOperands;
2183 if (i != NumOperands - 1)
2190 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2191 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2193 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2195 // 8 nodes, but we only care about the last 4.
2196 for (unsigned i = 7; i >= 4; --i) {
2198 SDOperand Arg = N->getOperand(i);
2199 if (Arg.getOpcode() != ISD::UNDEF)
2200 Val = cast<ConstantSDNode>(Arg)->getValue();
2209 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2210 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2212 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2214 // 8 nodes, but we only care about the first 4.
2215 for (int i = 3; i >= 0; --i) {
2217 SDOperand Arg = N->getOperand(i);
2218 if (Arg.getOpcode() != ISD::UNDEF)
2219 Val = cast<ConstantSDNode>(Arg)->getValue();
2228 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2229 /// specifies a 8 element shuffle that can be broken into a pair of
2230 /// PSHUFHW and PSHUFLW.
2231 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2232 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2234 if (N->getNumOperands() != 8)
2237 // Lower quadword shuffled.
2238 for (unsigned i = 0; i != 4; ++i) {
2239 SDOperand Arg = N->getOperand(i);
2240 if (Arg.getOpcode() == ISD::UNDEF) continue;
2241 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2242 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2247 // Upper quadword shuffled.
2248 for (unsigned i = 4; i != 8; ++i) {
2249 SDOperand Arg = N->getOperand(i);
2250 if (Arg.getOpcode() == ISD::UNDEF) continue;
2251 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2252 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2253 if (Val < 4 || Val > 7)
2260 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2261 /// values in ther permute mask.
2262 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2263 SDOperand &V2, SDOperand &Mask,
2264 SelectionDAG &DAG) {
2265 MVT::ValueType VT = Op.getValueType();
2266 MVT::ValueType MaskVT = Mask.getValueType();
2267 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2268 unsigned NumElems = Mask.getNumOperands();
2269 SmallVector<SDOperand, 8> MaskVec;
2271 for (unsigned i = 0; i != NumElems; ++i) {
2272 SDOperand Arg = Mask.getOperand(i);
2273 if (Arg.getOpcode() == ISD::UNDEF) {
2274 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2277 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2278 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2280 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2282 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2286 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2287 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2290 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2291 /// match movhlps. The lower half elements should come from upper half of
2292 /// V1 (and in order), and the upper half elements should come from the upper
2293 /// half of V2 (and in order).
2294 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2295 unsigned NumElems = Mask->getNumOperands();
2298 for (unsigned i = 0, e = 2; i != e; ++i)
2299 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2301 for (unsigned i = 2; i != 4; ++i)
2302 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2307 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2308 /// is promoted to a vector.
2309 static inline bool isScalarLoadToVector(SDNode *N) {
2310 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2311 N = N->getOperand(0).Val;
2312 return ISD::isNON_EXTLoad(N);
2317 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2318 /// match movlp{s|d}. The lower half elements should come from lower half of
2319 /// V1 (and in order), and the upper half elements should come from the upper
2320 /// half of V2 (and in order). And since V1 will become the source of the
2321 /// MOVLP, it must be either a vector load or a scalar load to vector.
2322 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2323 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2325 // Is V2 is a vector load, don't do this transformation. We will try to use
2326 // load folding shufps op.
2327 if (ISD::isNON_EXTLoad(V2))
2330 unsigned NumElems = Mask->getNumOperands();
2331 if (NumElems != 2 && NumElems != 4)
2333 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2334 if (!isUndefOrEqual(Mask->getOperand(i), i))
2336 for (unsigned i = NumElems/2; i != NumElems; ++i)
2337 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2342 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2344 static bool isSplatVector(SDNode *N) {
2345 if (N->getOpcode() != ISD::BUILD_VECTOR)
2348 SDOperand SplatValue = N->getOperand(0);
2349 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2350 if (N->getOperand(i) != SplatValue)
2355 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2357 static bool isUndefShuffle(SDNode *N) {
2358 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2361 SDOperand V1 = N->getOperand(0);
2362 SDOperand V2 = N->getOperand(1);
2363 SDOperand Mask = N->getOperand(2);
2364 unsigned NumElems = Mask.getNumOperands();
2365 for (unsigned i = 0; i != NumElems; ++i) {
2366 SDOperand Arg = Mask.getOperand(i);
2367 if (Arg.getOpcode() != ISD::UNDEF) {
2368 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2369 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2371 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2378 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2380 static inline bool isZeroNode(SDOperand Elt) {
2381 return ((isa<ConstantSDNode>(Elt) &&
2382 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2383 (isa<ConstantFPSDNode>(Elt) &&
2384 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2387 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2388 /// to an zero vector.
2389 static bool isZeroShuffle(SDNode *N) {
2390 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2393 SDOperand V1 = N->getOperand(0);
2394 SDOperand V2 = N->getOperand(1);
2395 SDOperand Mask = N->getOperand(2);
2396 unsigned NumElems = Mask.getNumOperands();
2397 for (unsigned i = 0; i != NumElems; ++i) {
2398 SDOperand Arg = Mask.getOperand(i);
2399 if (Arg.getOpcode() != ISD::UNDEF) {
2400 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2401 if (Idx < NumElems) {
2402 unsigned Opc = V1.Val->getOpcode();
2403 if (Opc == ISD::UNDEF)
2405 if (Opc != ISD::BUILD_VECTOR ||
2406 !isZeroNode(V1.Val->getOperand(Idx)))
2408 } else if (Idx >= NumElems) {
2409 unsigned Opc = V2.Val->getOpcode();
2410 if (Opc == ISD::UNDEF)
2412 if (Opc != ISD::BUILD_VECTOR ||
2413 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2421 /// getZeroVector - Returns a vector of specified type with all zero elements.
2423 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2424 assert(MVT::isVector(VT) && "Expected a vector type");
2425 unsigned NumElems = MVT::getVectorNumElements(VT);
2426 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2427 bool isFP = MVT::isFloatingPoint(EVT);
2428 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2429 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2430 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2433 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2434 /// that point to V2 points to its first element.
2435 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2436 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2438 bool Changed = false;
2439 SmallVector<SDOperand, 8> MaskVec;
2440 unsigned NumElems = Mask.getNumOperands();
2441 for (unsigned i = 0; i != NumElems; ++i) {
2442 SDOperand Arg = Mask.getOperand(i);
2443 if (Arg.getOpcode() != ISD::UNDEF) {
2444 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2445 if (Val > NumElems) {
2446 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2450 MaskVec.push_back(Arg);
2454 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2455 &MaskVec[0], MaskVec.size());
2459 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2460 /// operation of specified width.
2461 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2462 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2463 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2465 SmallVector<SDOperand, 8> MaskVec;
2466 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2467 for (unsigned i = 1; i != NumElems; ++i)
2468 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2469 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2472 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2473 /// of specified width.
2474 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2475 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2476 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2477 SmallVector<SDOperand, 8> MaskVec;
2478 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2479 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2480 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2482 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2485 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2486 /// of specified width.
2487 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2488 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2489 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2490 unsigned Half = NumElems/2;
2491 SmallVector<SDOperand, 8> MaskVec;
2492 for (unsigned i = 0; i != Half; ++i) {
2493 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2494 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2496 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2499 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2501 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2502 SDOperand V1 = Op.getOperand(0);
2503 SDOperand Mask = Op.getOperand(2);
2504 MVT::ValueType VT = Op.getValueType();
2505 unsigned NumElems = Mask.getNumOperands();
2506 Mask = getUnpacklMask(NumElems, DAG);
2507 while (NumElems != 4) {
2508 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2511 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2513 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2514 Mask = getZeroVector(MaskVT, DAG);
2515 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2516 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2517 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2520 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2521 /// vector of zero or undef vector.
2522 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2523 unsigned NumElems, unsigned Idx,
2524 bool isZero, SelectionDAG &DAG) {
2525 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2526 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2527 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2528 SDOperand Zero = DAG.getConstant(0, EVT);
2529 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2530 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2531 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2532 &MaskVec[0], MaskVec.size());
2533 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2536 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2538 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2539 unsigned NumNonZero, unsigned NumZero,
2540 SelectionDAG &DAG, TargetLowering &TLI) {
2546 for (unsigned i = 0; i < 16; ++i) {
2547 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2548 if (ThisIsNonZero && First) {
2550 V = getZeroVector(MVT::v8i16, DAG);
2552 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2557 SDOperand ThisElt(0, 0), LastElt(0, 0);
2558 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2559 if (LastIsNonZero) {
2560 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2562 if (ThisIsNonZero) {
2563 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2564 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2565 ThisElt, DAG.getConstant(8, MVT::i8));
2567 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2572 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2573 DAG.getConstant(i/2, TLI.getPointerTy()));
2577 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2580 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2582 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2583 unsigned NumNonZero, unsigned NumZero,
2584 SelectionDAG &DAG, TargetLowering &TLI) {
2590 for (unsigned i = 0; i < 8; ++i) {
2591 bool isNonZero = (NonZeros & (1 << i)) != 0;
2595 V = getZeroVector(MVT::v8i16, DAG);
2597 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2600 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2601 DAG.getConstant(i, TLI.getPointerTy()));
2609 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2610 // All zero's are handled with pxor.
2611 if (ISD::isBuildVectorAllZeros(Op.Val))
2614 // All one's are handled with pcmpeqd.
2615 if (ISD::isBuildVectorAllOnes(Op.Val))
2618 MVT::ValueType VT = Op.getValueType();
2619 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2620 unsigned EVTBits = MVT::getSizeInBits(EVT);
2622 unsigned NumElems = Op.getNumOperands();
2623 unsigned NumZero = 0;
2624 unsigned NumNonZero = 0;
2625 unsigned NonZeros = 0;
2626 unsigned NumNonZeroImms = 0;
2627 std::set<SDOperand> Values;
2628 for (unsigned i = 0; i < NumElems; ++i) {
2629 SDOperand Elt = Op.getOperand(i);
2630 if (Elt.getOpcode() != ISD::UNDEF) {
2632 if (isZeroNode(Elt))
2635 NonZeros |= (1 << i);
2637 if (Elt.getOpcode() == ISD::Constant ||
2638 Elt.getOpcode() == ISD::ConstantFP)
2644 if (NumNonZero == 0) {
2646 // All undef vector. Return an UNDEF.
2647 return DAG.getNode(ISD::UNDEF, VT);
2649 // A mix of zero and undef. Return a zero vector.
2650 return getZeroVector(VT, DAG);
2653 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2654 if (Values.size() == 1)
2657 // Special case for single non-zero element.
2658 if (NumNonZero == 1) {
2659 unsigned Idx = CountTrailingZeros_32(NonZeros);
2660 SDOperand Item = Op.getOperand(Idx);
2661 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2663 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2664 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2667 if (EVTBits == 32) {
2668 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2669 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2671 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2672 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2673 SmallVector<SDOperand, 8> MaskVec;
2674 for (unsigned i = 0; i < NumElems; i++)
2675 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2676 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2677 &MaskVec[0], MaskVec.size());
2678 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2679 DAG.getNode(ISD::UNDEF, VT), Mask);
2683 // A vector full of immediates; various special cases are already
2684 // handled, so this is best done with a single constant-pool load.
2685 if (NumNonZero == NumNonZeroImms)
2688 // Let legalizer expand 2-wide build_vectors.
2692 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2693 if (EVTBits == 8 && NumElems == 16) {
2694 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2696 if (V.Val) return V;
2699 if (EVTBits == 16 && NumElems == 8) {
2700 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2702 if (V.Val) return V;
2705 // If element VT is == 32 bits, turn it into a number of shuffles.
2706 SmallVector<SDOperand, 8> V;
2708 if (NumElems == 4 && NumZero > 0) {
2709 for (unsigned i = 0; i < 4; ++i) {
2710 bool isZero = !(NonZeros & (1 << i));
2712 V[i] = getZeroVector(VT, DAG);
2714 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2717 for (unsigned i = 0; i < 2; ++i) {
2718 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2721 V[i] = V[i*2]; // Must be a zero vector.
2724 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2725 getMOVLMask(NumElems, DAG));
2728 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2729 getMOVLMask(NumElems, DAG));
2732 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2733 getUnpacklMask(NumElems, DAG));
2738 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2739 // clears the upper bits.
2740 // FIXME: we can do the same for v4f32 case when we know both parts of
2741 // the lower half come from scalar_to_vector (loadf32). We should do
2742 // that in post legalizer dag combiner with target specific hooks.
2743 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2745 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2746 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2747 SmallVector<SDOperand, 8> MaskVec;
2748 bool Reverse = (NonZeros & 0x3) == 2;
2749 for (unsigned i = 0; i < 2; ++i)
2751 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2753 MaskVec.push_back(DAG.getConstant(i, EVT));
2754 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2755 for (unsigned i = 0; i < 2; ++i)
2757 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2759 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2760 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2761 &MaskVec[0], MaskVec.size());
2762 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2765 if (Values.size() > 2) {
2766 // Expand into a number of unpckl*.
2768 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2769 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2770 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2771 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2772 for (unsigned i = 0; i < NumElems; ++i)
2773 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2775 while (NumElems != 0) {
2776 for (unsigned i = 0; i < NumElems; ++i)
2777 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2788 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2789 SDOperand V1 = Op.getOperand(0);
2790 SDOperand V2 = Op.getOperand(1);
2791 SDOperand PermMask = Op.getOperand(2);
2792 MVT::ValueType VT = Op.getValueType();
2793 unsigned NumElems = PermMask.getNumOperands();
2794 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2795 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2796 bool V1IsSplat = false;
2797 bool V2IsSplat = false;
2799 if (isUndefShuffle(Op.Val))
2800 return DAG.getNode(ISD::UNDEF, VT);
2802 if (isZeroShuffle(Op.Val))
2803 return getZeroVector(VT, DAG);
2805 if (isIdentityMask(PermMask.Val))
2807 else if (isIdentityMask(PermMask.Val, true))
2810 if (isSplatMask(PermMask.Val)) {
2811 if (NumElems <= 4) return Op;
2812 // Promote it to a v4i32 splat.
2813 return PromoteSplat(Op, DAG);
2816 if (X86::isMOVLMask(PermMask.Val))
2817 return (V1IsUndef) ? V2 : Op;
2819 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2820 X86::isMOVSLDUPMask(PermMask.Val) ||
2821 X86::isMOVHLPSMask(PermMask.Val) ||
2822 X86::isMOVHPMask(PermMask.Val) ||
2823 X86::isMOVLPMask(PermMask.Val))
2826 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2827 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2828 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2830 bool Commuted = false;
2831 V1IsSplat = isSplatVector(V1.Val);
2832 V2IsSplat = isSplatVector(V2.Val);
2833 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2834 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2835 std::swap(V1IsSplat, V2IsSplat);
2836 std::swap(V1IsUndef, V2IsUndef);
2840 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2841 if (V2IsUndef) return V1;
2842 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2844 // V2 is a splat, so the mask may be malformed. That is, it may point
2845 // to any V2 element. The instruction selectior won't like this. Get
2846 // a corrected mask and commute to form a proper MOVS{S|D}.
2847 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2848 if (NewMask.Val != PermMask.Val)
2849 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2854 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2855 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2856 X86::isUNPCKLMask(PermMask.Val) ||
2857 X86::isUNPCKHMask(PermMask.Val))
2861 // Normalize mask so all entries that point to V2 points to its first
2862 // element then try to match unpck{h|l} again. If match, return a
2863 // new vector_shuffle with the corrected mask.
2864 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2865 if (NewMask.Val != PermMask.Val) {
2866 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2867 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2868 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2869 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2870 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2871 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2876 // Normalize the node to match x86 shuffle ops if needed
2877 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2878 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2881 // Commute is back and try unpck* again.
2882 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2883 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2884 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2885 X86::isUNPCKLMask(PermMask.Val) ||
2886 X86::isUNPCKHMask(PermMask.Val))
2890 // If VT is integer, try PSHUF* first, then SHUFP*.
2891 if (MVT::isInteger(VT)) {
2892 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
2893 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
2894 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
2895 X86::isPSHUFDMask(PermMask.Val)) ||
2896 X86::isPSHUFHWMask(PermMask.Val) ||
2897 X86::isPSHUFLWMask(PermMask.Val)) {
2898 if (V2.getOpcode() != ISD::UNDEF)
2899 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2900 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2904 if (X86::isSHUFPMask(PermMask.Val) &&
2905 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
2908 // Handle v8i16 shuffle high / low shuffle node pair.
2909 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2910 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2911 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2912 SmallVector<SDOperand, 8> MaskVec;
2913 for (unsigned i = 0; i != 4; ++i)
2914 MaskVec.push_back(PermMask.getOperand(i));
2915 for (unsigned i = 4; i != 8; ++i)
2916 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2917 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2918 &MaskVec[0], MaskVec.size());
2919 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2921 for (unsigned i = 0; i != 4; ++i)
2922 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2923 for (unsigned i = 4; i != 8; ++i)
2924 MaskVec.push_back(PermMask.getOperand(i));
2925 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2926 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2929 // Floating point cases in the other order.
2930 if (X86::isSHUFPMask(PermMask.Val))
2932 if (X86::isPSHUFDMask(PermMask.Val) ||
2933 X86::isPSHUFHWMask(PermMask.Val) ||
2934 X86::isPSHUFLWMask(PermMask.Val)) {
2935 if (V2.getOpcode() != ISD::UNDEF)
2936 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2937 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2942 if (NumElems == 4 &&
2943 // Don't do this for MMX.
2944 MVT::getSizeInBits(VT) != 64) {
2945 MVT::ValueType MaskVT = PermMask.getValueType();
2946 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2947 SmallVector<std::pair<int, int>, 8> Locs;
2948 Locs.reserve(NumElems);
2949 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2950 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2953 // If no more than two elements come from either vector. This can be
2954 // implemented with two shuffles. First shuffle gather the elements.
2955 // The second shuffle, which takes the first shuffle as both of its
2956 // vector operands, put the elements into the right order.
2957 for (unsigned i = 0; i != NumElems; ++i) {
2958 SDOperand Elt = PermMask.getOperand(i);
2959 if (Elt.getOpcode() == ISD::UNDEF) {
2960 Locs[i] = std::make_pair(-1, -1);
2962 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2963 if (Val < NumElems) {
2964 Locs[i] = std::make_pair(0, NumLo);
2968 Locs[i] = std::make_pair(1, NumHi);
2969 if (2+NumHi < NumElems)
2970 Mask1[2+NumHi] = Elt;
2975 if (NumLo <= 2 && NumHi <= 2) {
2976 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2977 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2978 &Mask1[0], Mask1.size()));
2979 for (unsigned i = 0; i != NumElems; ++i) {
2980 if (Locs[i].first == -1)
2983 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2984 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2985 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2989 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2990 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2991 &Mask2[0], Mask2.size()));
2994 // Break it into (shuffle shuffle_hi, shuffle_lo).
2996 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2997 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2998 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
2999 unsigned MaskIdx = 0;
3001 unsigned HiIdx = NumElems/2;
3002 for (unsigned i = 0; i != NumElems; ++i) {
3003 if (i == NumElems/2) {
3009 SDOperand Elt = PermMask.getOperand(i);
3010 if (Elt.getOpcode() == ISD::UNDEF) {
3011 Locs[i] = std::make_pair(-1, -1);
3012 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3013 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3014 (*MaskPtr)[LoIdx] = Elt;
3017 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3018 (*MaskPtr)[HiIdx] = Elt;
3023 SDOperand LoShuffle =
3024 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3025 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3026 &LoMask[0], LoMask.size()));
3027 SDOperand HiShuffle =
3028 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3029 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3030 &HiMask[0], HiMask.size()));
3031 SmallVector<SDOperand, 8> MaskOps;
3032 for (unsigned i = 0; i != NumElems; ++i) {
3033 if (Locs[i].first == -1) {
3034 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3036 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3037 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3040 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3041 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3042 &MaskOps[0], MaskOps.size()));
3049 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3050 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3053 MVT::ValueType VT = Op.getValueType();
3054 // TODO: handle v16i8.
3055 if (MVT::getSizeInBits(VT) == 16) {
3056 // Transform it so it match pextrw which produces a 32-bit result.
3057 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3058 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3059 Op.getOperand(0), Op.getOperand(1));
3060 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3061 DAG.getValueType(VT));
3062 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3063 } else if (MVT::getSizeInBits(VT) == 32) {
3064 SDOperand Vec = Op.getOperand(0);
3065 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3068 // SHUFPS the element to the lowest double word, then movss.
3069 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3070 SmallVector<SDOperand, 8> IdxVec;
3071 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3072 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3073 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3074 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3075 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3076 &IdxVec[0], IdxVec.size());
3077 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3078 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3079 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3080 DAG.getConstant(0, getPointerTy()));
3081 } else if (MVT::getSizeInBits(VT) == 64) {
3082 SDOperand Vec = Op.getOperand(0);
3083 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3087 // UNPCKHPD the element to the lowest double word, then movsd.
3088 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3089 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3090 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3091 SmallVector<SDOperand, 8> IdxVec;
3092 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
3093 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3094 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3095 &IdxVec[0], IdxVec.size());
3096 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3097 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3098 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3099 DAG.getConstant(0, getPointerTy()));
3106 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3107 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3108 // as its second argument.
3109 MVT::ValueType VT = Op.getValueType();
3110 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
3111 SDOperand N0 = Op.getOperand(0);
3112 SDOperand N1 = Op.getOperand(1);
3113 SDOperand N2 = Op.getOperand(2);
3114 if (MVT::getSizeInBits(BaseVT) == 16) {
3115 if (N1.getValueType() != MVT::i32)
3116 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3117 if (N2.getValueType() != MVT::i32)
3118 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
3119 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3120 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3121 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3124 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3125 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3126 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3127 SmallVector<SDOperand, 8> MaskVec;
3128 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3129 for (unsigned i = 1; i <= 3; ++i)
3130 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3131 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3132 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3133 &MaskVec[0], MaskVec.size()));
3135 // Use two pinsrw instructions to insert a 32 bit value.
3137 if (MVT::isFloatingPoint(N1.getValueType())) {
3138 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3139 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3140 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3141 DAG.getConstant(0, getPointerTy()));
3143 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3144 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3145 DAG.getConstant(Idx, getPointerTy()));
3146 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3147 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3148 DAG.getConstant(Idx+1, getPointerTy()));
3149 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3157 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3158 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3159 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3162 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3163 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3164 // one of the above mentioned nodes. It has to be wrapped because otherwise
3165 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3166 // be used to form addressing mode. These wrapped nodes will be selected
3169 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3170 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3171 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3173 CP->getAlignment());
3174 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3175 // With PIC, the address is actually $g + Offset.
3176 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3177 !Subtarget->isPICStyleRIPRel()) {
3178 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3179 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3187 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3188 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3189 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3190 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3191 // With PIC, the address is actually $g + Offset.
3192 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3193 !Subtarget->isPICStyleRIPRel()) {
3194 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3195 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3199 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3200 // load the value at address GV, not the value of GV itself. This means that
3201 // the GlobalAddress must be in the base or index register of the address, not
3202 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3203 // The same applies for external symbols during PIC codegen
3204 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3205 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3210 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
3212 LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3213 const MVT::ValueType PtrVT) {
3215 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3216 DAG.getNode(X86ISD::GlobalBaseReg,
3218 InFlag = Chain.getValue(1);
3220 // emit leal symbol@TLSGD(,%ebx,1), %eax
3221 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3222 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3223 GA->getValueType(0),
3225 SDOperand Ops[] = { Chain, TGA, InFlag };
3226 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3227 InFlag = Result.getValue(2);
3228 Chain = Result.getValue(1);
3230 // call ___tls_get_addr. This function receives its argument in
3231 // the register EAX.
3232 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3233 InFlag = Chain.getValue(1);
3235 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3236 SDOperand Ops1[] = { Chain,
3237 DAG.getTargetExternalSymbol("___tls_get_addr",
3239 DAG.getRegister(X86::EAX, PtrVT),
3240 DAG.getRegister(X86::EBX, PtrVT),
3242 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3243 InFlag = Chain.getValue(1);
3245 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3248 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3249 // "local exec" model.
3251 LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3252 const MVT::ValueType PtrVT) {
3253 // Get the Thread Pointer
3254 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3255 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3257 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3258 GA->getValueType(0),
3260 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3262 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3263 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3265 // The address of the thread local variable is the add of the thread
3266 // pointer with the offset of the variable.
3267 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3271 X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3272 // TODO: implement the "local dynamic" model
3273 // TODO: implement the "initial exec"model for pic executables
3274 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3275 "TLS not implemented for non-ELF and 64-bit targets");
3276 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3277 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3278 // otherwise use the "Local Exec"TLS Model
3279 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3280 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3282 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3286 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3287 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3288 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3289 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3290 // With PIC, the address is actually $g + Offset.
3291 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3292 !Subtarget->isPICStyleRIPRel()) {
3293 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3294 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3301 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3302 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3303 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3304 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3305 // With PIC, the address is actually $g + Offset.
3306 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3307 !Subtarget->isPICStyleRIPRel()) {
3308 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3309 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3316 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3317 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3318 "Not an i64 shift!");
3319 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3320 SDOperand ShOpLo = Op.getOperand(0);
3321 SDOperand ShOpHi = Op.getOperand(1);
3322 SDOperand ShAmt = Op.getOperand(2);
3323 SDOperand Tmp1 = isSRA ?
3324 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3325 DAG.getConstant(0, MVT::i32);
3327 SDOperand Tmp2, Tmp3;
3328 if (Op.getOpcode() == ISD::SHL_PARTS) {
3329 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3330 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3332 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3333 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3336 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3337 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3338 DAG.getConstant(32, MVT::i8));
3339 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3340 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3343 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3345 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3346 SmallVector<SDOperand, 4> Ops;
3347 if (Op.getOpcode() == ISD::SHL_PARTS) {
3348 Ops.push_back(Tmp2);
3349 Ops.push_back(Tmp3);
3351 Ops.push_back(InFlag);
3352 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3353 InFlag = Hi.getValue(1);
3356 Ops.push_back(Tmp3);
3357 Ops.push_back(Tmp1);
3359 Ops.push_back(InFlag);
3360 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3362 Ops.push_back(Tmp2);
3363 Ops.push_back(Tmp3);
3365 Ops.push_back(InFlag);
3366 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3367 InFlag = Lo.getValue(1);
3370 Ops.push_back(Tmp3);
3371 Ops.push_back(Tmp1);
3373 Ops.push_back(InFlag);
3374 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3377 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3381 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3384 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3385 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3386 Op.getOperand(0).getValueType() >= MVT::i16 &&
3387 "Unknown SINT_TO_FP to lower!");
3390 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3391 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3392 MachineFunction &MF = DAG.getMachineFunction();
3393 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3394 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3395 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3396 StackSlot, NULL, 0);
3398 // These are really Legal; caller falls through into that case.
3399 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
3401 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
3403 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
3404 Subtarget->is64Bit())
3409 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
3410 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
3412 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3414 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
3415 SmallVector<SDOperand, 8> Ops;
3416 Ops.push_back(Chain);
3417 Ops.push_back(StackSlot);
3418 Ops.push_back(DAG.getValueType(SrcVT));
3419 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3420 Tys, &Ops[0], Ops.size());
3423 Chain = Result.getValue(1);
3424 SDOperand InFlag = Result.getValue(2);
3426 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3427 // shouldn't be necessary except that RFP cannot be live across
3428 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3429 MachineFunction &MF = DAG.getMachineFunction();
3430 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3431 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3432 Tys = DAG.getVTList(MVT::Other);
3433 SmallVector<SDOperand, 8> Ops;
3434 Ops.push_back(Chain);
3435 Ops.push_back(Result);
3436 Ops.push_back(StackSlot);
3437 Ops.push_back(DAG.getValueType(Op.getValueType()));
3438 Ops.push_back(InFlag);
3439 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3440 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3446 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3447 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3448 "Unknown FP_TO_SINT to lower!");
3449 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3452 MachineFunction &MF = DAG.getMachineFunction();
3453 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3454 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3455 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3457 // These are really Legal.
3458 if (Op.getValueType() == MVT::i32 &&
3459 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
3461 if (Op.getValueType() == MVT::i32 &&
3462 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
3464 if (Subtarget->is64Bit() &&
3465 Op.getValueType() == MVT::i64 &&
3466 Op.getOperand(0).getValueType() != MVT::f80)
3470 switch (Op.getValueType()) {
3471 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3472 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3473 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3474 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3477 SDOperand Chain = DAG.getEntryNode();
3478 SDOperand Value = Op.getOperand(0);
3479 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
3480 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
3481 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3482 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3483 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
3485 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3487 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3488 Chain = Value.getValue(1);
3489 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3490 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3493 // Build the FP_TO_INT*_IN_MEM
3494 SDOperand Ops[] = { Chain, Value, StackSlot };
3495 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3498 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3501 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3502 MVT::ValueType VT = Op.getValueType();
3503 MVT::ValueType EltVT = VT;
3504 if (MVT::isVector(VT))
3505 EltVT = MVT::getVectorElementType(VT);
3506 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3507 std::vector<Constant*> CV;
3508 if (EltVT == MVT::f64) {
3509 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
3513 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
3519 Constant *C = ConstantVector::get(CV);
3520 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3521 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3523 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3526 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3527 MVT::ValueType VT = Op.getValueType();
3528 MVT::ValueType EltVT = VT;
3529 unsigned EltNum = 1;
3530 if (MVT::isVector(VT)) {
3531 EltVT = MVT::getVectorElementType(VT);
3532 EltNum = MVT::getVectorNumElements(VT);
3534 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3535 std::vector<Constant*> CV;
3536 if (EltVT == MVT::f64) {
3537 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
3541 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
3547 Constant *C = ConstantVector::get(CV);
3548 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3549 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3551 if (MVT::isVector(VT)) {
3552 return DAG.getNode(ISD::BIT_CONVERT, VT,
3553 DAG.getNode(ISD::XOR, MVT::v2i64,
3554 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
3555 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
3557 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3561 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3562 SDOperand Op0 = Op.getOperand(0);
3563 SDOperand Op1 = Op.getOperand(1);
3564 MVT::ValueType VT = Op.getValueType();
3565 MVT::ValueType SrcVT = Op1.getValueType();
3566 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3568 // If second operand is smaller, extend it first.
3569 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3570 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3572 SrcTy = MVT::getTypeForValueType(SrcVT);
3575 // First get the sign bit of second operand.
3576 std::vector<Constant*> CV;
3577 if (SrcVT == MVT::f64) {
3578 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
3579 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
3581 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
3582 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3583 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3584 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3586 Constant *C = ConstantVector::get(CV);
3587 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3588 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
3590 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3592 // Shift sign bit right or left if the two operands have different types.
3593 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3594 // Op0 is MVT::f32, Op1 is MVT::f64.
3595 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3596 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3597 DAG.getConstant(32, MVT::i32));
3598 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3599 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3600 DAG.getConstant(0, getPointerTy()));
3603 // Clear first operand sign bit.
3605 if (VT == MVT::f64) {
3606 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
3607 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
3609 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
3610 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3611 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3612 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3614 C = ConstantVector::get(CV);
3615 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3616 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3618 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3620 // Or the value with the sign bit.
3621 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3624 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3626 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3628 SDOperand Op0 = Op.getOperand(0);
3629 SDOperand Op1 = Op.getOperand(1);
3630 SDOperand CC = Op.getOperand(2);
3631 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3632 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3633 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3634 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3637 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3639 SDOperand Ops1[] = { Chain, Op0, Op1 };
3640 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3641 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3642 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3645 assert(isFP && "Illegal integer SetCC!");
3647 SDOperand COps[] = { Chain, Op0, Op1 };
3648 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3650 switch (SetCCOpcode) {
3651 default: assert(false && "Illegal floating point SetCC!");
3652 case ISD::SETOEQ: { // !PF & ZF
3653 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3654 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3655 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3657 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3658 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3660 case ISD::SETUNE: { // PF | !ZF
3661 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3662 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3663 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3665 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3666 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3671 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3672 bool addTest = true;
3673 SDOperand Chain = DAG.getEntryNode();
3674 SDOperand Cond = Op.getOperand(0);
3676 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3678 if (Cond.getOpcode() == ISD::SETCC)
3679 Cond = LowerSETCC(Cond, DAG, Chain);
3681 if (Cond.getOpcode() == X86ISD::SETCC) {
3682 CC = Cond.getOperand(0);
3684 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3685 // (since flag operand cannot be shared). Use it as the condition setting
3686 // operand in place of the X86ISD::SETCC.
3687 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3688 // to use a test instead of duplicating the X86ISD::CMP (for register
3689 // pressure reason)?
3690 SDOperand Cmp = Cond.getOperand(1);
3691 unsigned Opc = Cmp.getOpcode();
3692 bool IllegalFPCMov =
3693 ! ((X86ScalarSSEf32 && Op.getValueType()==MVT::f32) ||
3694 (X86ScalarSSEf64 && Op.getValueType()==MVT::f64)) &&
3695 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3696 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3698 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3699 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3705 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3706 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3707 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3710 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3711 SmallVector<SDOperand, 4> Ops;
3712 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3713 // condition is true.
3714 Ops.push_back(Op.getOperand(2));
3715 Ops.push_back(Op.getOperand(1));
3717 Ops.push_back(Cond.getValue(1));
3718 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3721 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3722 bool addTest = true;
3723 SDOperand Chain = Op.getOperand(0);
3724 SDOperand Cond = Op.getOperand(1);
3725 SDOperand Dest = Op.getOperand(2);
3727 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3729 if (Cond.getOpcode() == ISD::SETCC)
3730 Cond = LowerSETCC(Cond, DAG, Chain);
3732 if (Cond.getOpcode() == X86ISD::SETCC) {
3733 CC = Cond.getOperand(0);
3735 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3736 // (since flag operand cannot be shared). Use it as the condition setting
3737 // operand in place of the X86ISD::SETCC.
3738 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3739 // to use a test instead of duplicating the X86ISD::CMP (for register
3740 // pressure reason)?
3741 SDOperand Cmp = Cond.getOperand(1);
3742 unsigned Opc = Cmp.getOpcode();
3743 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3744 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3745 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3751 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3752 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3753 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3755 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3756 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3759 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3760 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3762 if (Subtarget->is64Bit())
3763 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3765 switch (CallingConv) {
3767 assert(0 && "Unsupported calling convention");
3768 case CallingConv::Fast:
3769 // TODO: Implement fastcc
3771 case CallingConv::C:
3772 case CallingConv::X86_StdCall:
3773 return LowerCCCCallTo(Op, DAG, CallingConv);
3774 case CallingConv::X86_FastCall:
3775 return LowerFastCCCallTo(Op, DAG, CallingConv);
3780 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3781 // Calls to _alloca is needed to probe the stack when allocating more than 4k
3782 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
3783 // that the guard pages used by the OS virtual memory manager are allocated in
3784 // correct sequence.
3786 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3787 SelectionDAG &DAG) {
3788 assert(Subtarget->isTargetCygMing() &&
3789 "This should be used only on Cygwin/Mingw targets");
3792 SDOperand Chain = Op.getOperand(0);
3793 SDOperand Size = Op.getOperand(1);
3794 // FIXME: Ensure alignment here
3798 MVT::ValueType IntPtr = getPointerTy();
3799 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
3801 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
3802 Flag = Chain.getValue(1);
3804 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3805 SDOperand Ops[] = { Chain,
3806 DAG.getTargetExternalSymbol("_alloca", IntPtr),
3807 DAG.getRegister(X86::EAX, IntPtr),
3809 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
3810 Flag = Chain.getValue(1);
3812 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
3814 std::vector<MVT::ValueType> Tys;
3815 Tys.push_back(SPTy);
3816 Tys.push_back(MVT::Other);
3817 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
3818 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
3822 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3823 MachineFunction &MF = DAG.getMachineFunction();
3824 const Function* Fn = MF.getFunction();
3825 if (Fn->hasExternalLinkage() &&
3826 Subtarget->isTargetCygMing() &&
3827 Fn->getName() == "main")
3828 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
3830 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3831 if (Subtarget->is64Bit())
3832 return LowerX86_64CCCArguments(Op, DAG);
3836 assert(0 && "Unsupported calling convention");
3837 case CallingConv::Fast:
3838 // TODO: implement fastcc.
3841 case CallingConv::C:
3842 return LowerCCCArguments(Op, DAG);
3843 case CallingConv::X86_StdCall:
3844 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
3845 return LowerCCCArguments(Op, DAG, true);
3846 case CallingConv::X86_FastCall:
3847 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
3848 return LowerFastCCArguments(Op, DAG);
3852 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3853 SDOperand InFlag(0, 0);
3854 SDOperand Chain = Op.getOperand(0);
3856 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3857 if (Align == 0) Align = 1;
3859 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3860 // If not DWORD aligned or size is more than the threshold, call memset.
3861 // The libc version is likely to be faster for these cases. It can use the
3862 // address value and run time information about the CPU.
3863 if ((Align & 3) != 0 ||
3864 (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) {
3865 MVT::ValueType IntPtr = getPointerTy();
3866 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3867 TargetLowering::ArgListTy Args;
3868 TargetLowering::ArgListEntry Entry;
3869 Entry.Node = Op.getOperand(1);
3870 Entry.Ty = IntPtrTy;
3871 Args.push_back(Entry);
3872 // Extend the unsigned i8 argument to be an int value for the call.
3873 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3874 Entry.Ty = IntPtrTy;
3875 Args.push_back(Entry);
3876 Entry.Node = Op.getOperand(3);
3877 Args.push_back(Entry);
3878 std::pair<SDOperand,SDOperand> CallResult =
3879 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3880 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3881 return CallResult.second;
3886 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3887 unsigned BytesLeft = 0;
3888 bool TwoRepStos = false;
3891 uint64_t Val = ValC->getValue() & 255;
3893 // If the value is a constant, then we can potentially use larger sets.
3894 switch (Align & 3) {
3895 case 2: // WORD aligned
3898 Val = (Val << 8) | Val;
3900 case 0: // DWORD aligned
3903 Val = (Val << 8) | Val;
3904 Val = (Val << 16) | Val;
3905 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3908 Val = (Val << 32) | Val;
3911 default: // Byte aligned
3914 Count = Op.getOperand(3);
3918 if (AVT > MVT::i8) {
3920 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3921 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3922 BytesLeft = I->getValue() % UBytes;
3924 assert(AVT >= MVT::i32 &&
3925 "Do not use rep;stos if not at least DWORD aligned");
3926 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3927 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3932 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3934 InFlag = Chain.getValue(1);
3937 Count = Op.getOperand(3);
3938 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3939 InFlag = Chain.getValue(1);
3942 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3944 InFlag = Chain.getValue(1);
3945 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3946 Op.getOperand(1), InFlag);
3947 InFlag = Chain.getValue(1);
3949 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3950 SmallVector<SDOperand, 8> Ops;
3951 Ops.push_back(Chain);
3952 Ops.push_back(DAG.getValueType(AVT));
3953 Ops.push_back(InFlag);
3954 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3957 InFlag = Chain.getValue(1);
3958 Count = Op.getOperand(3);
3959 MVT::ValueType CVT = Count.getValueType();
3960 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3961 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3962 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3964 InFlag = Chain.getValue(1);
3965 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3967 Ops.push_back(Chain);
3968 Ops.push_back(DAG.getValueType(MVT::i8));
3969 Ops.push_back(InFlag);
3970 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3971 } else if (BytesLeft) {
3972 // Issue stores for the last 1 - 7 bytes.
3974 unsigned Val = ValC->getValue() & 255;
3975 unsigned Offset = I->getValue() - BytesLeft;
3976 SDOperand DstAddr = Op.getOperand(1);
3977 MVT::ValueType AddrVT = DstAddr.getValueType();
3978 if (BytesLeft >= 4) {
3979 Val = (Val << 8) | Val;
3980 Val = (Val << 16) | Val;
3981 Value = DAG.getConstant(Val, MVT::i32);
3982 Chain = DAG.getStore(Chain, Value,
3983 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3984 DAG.getConstant(Offset, AddrVT)),
3989 if (BytesLeft >= 2) {
3990 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3991 Chain = DAG.getStore(Chain, Value,
3992 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3993 DAG.getConstant(Offset, AddrVT)),
3998 if (BytesLeft == 1) {
3999 Value = DAG.getConstant(Val, MVT::i8);
4000 Chain = DAG.getStore(Chain, Value,
4001 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4002 DAG.getConstant(Offset, AddrVT)),
4010 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4011 SDOperand Chain = Op.getOperand(0);
4013 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4014 if (Align == 0) Align = 1;
4016 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4017 // If not DWORD aligned or size is more than the threshold, call memcpy.
4018 // The libc version is likely to be faster for these cases. It can use the
4019 // address value and run time information about the CPU.
4020 // With glibc 2.6.1 on a core 2, coping an array of 100M longs was 30% faster
4021 if ((Align & 3) != 0 ||
4022 (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) {
4023 MVT::ValueType IntPtr = getPointerTy();
4024 TargetLowering::ArgListTy Args;
4025 TargetLowering::ArgListEntry Entry;
4026 Entry.Ty = getTargetData()->getIntPtrType();
4027 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
4028 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
4029 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
4030 std::pair<SDOperand,SDOperand> CallResult =
4031 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4032 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4033 return CallResult.second;
4038 unsigned BytesLeft = 0;
4039 bool TwoRepMovs = false;
4040 switch (Align & 3) {
4041 case 2: // WORD aligned
4044 case 0: // DWORD aligned
4046 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4049 default: // Byte aligned
4051 Count = Op.getOperand(3);
4055 if (AVT > MVT::i8) {
4057 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4058 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4059 BytesLeft = I->getValue() % UBytes;
4061 assert(AVT >= MVT::i32 &&
4062 "Do not use rep;movs if not at least DWORD aligned");
4063 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4064 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4069 SDOperand InFlag(0, 0);
4070 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4072 InFlag = Chain.getValue(1);
4073 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4074 Op.getOperand(1), InFlag);
4075 InFlag = Chain.getValue(1);
4076 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4077 Op.getOperand(2), InFlag);
4078 InFlag = Chain.getValue(1);
4080 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4081 SmallVector<SDOperand, 8> Ops;
4082 Ops.push_back(Chain);
4083 Ops.push_back(DAG.getValueType(AVT));
4084 Ops.push_back(InFlag);
4085 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4088 InFlag = Chain.getValue(1);
4089 Count = Op.getOperand(3);
4090 MVT::ValueType CVT = Count.getValueType();
4091 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4092 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4093 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4095 InFlag = Chain.getValue(1);
4096 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4098 Ops.push_back(Chain);
4099 Ops.push_back(DAG.getValueType(MVT::i8));
4100 Ops.push_back(InFlag);
4101 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4102 } else if (BytesLeft) {
4103 // Issue loads and stores for the last 1 - 7 bytes.
4104 unsigned Offset = I->getValue() - BytesLeft;
4105 SDOperand DstAddr = Op.getOperand(1);
4106 MVT::ValueType DstVT = DstAddr.getValueType();
4107 SDOperand SrcAddr = Op.getOperand(2);
4108 MVT::ValueType SrcVT = SrcAddr.getValueType();
4110 if (BytesLeft >= 4) {
4111 Value = DAG.getLoad(MVT::i32, Chain,
4112 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4113 DAG.getConstant(Offset, SrcVT)),
4115 Chain = Value.getValue(1);
4116 Chain = DAG.getStore(Chain, Value,
4117 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4118 DAG.getConstant(Offset, DstVT)),
4123 if (BytesLeft >= 2) {
4124 Value = DAG.getLoad(MVT::i16, Chain,
4125 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4126 DAG.getConstant(Offset, SrcVT)),
4128 Chain = Value.getValue(1);
4129 Chain = DAG.getStore(Chain, Value,
4130 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4131 DAG.getConstant(Offset, DstVT)),
4137 if (BytesLeft == 1) {
4138 Value = DAG.getLoad(MVT::i8, Chain,
4139 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4140 DAG.getConstant(Offset, SrcVT)),
4142 Chain = Value.getValue(1);
4143 Chain = DAG.getStore(Chain, Value,
4144 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4145 DAG.getConstant(Offset, DstVT)),
4154 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4155 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4156 SDOperand TheOp = Op.getOperand(0);
4157 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
4158 if (Subtarget->is64Bit()) {
4159 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4160 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4161 MVT::i64, Copy1.getValue(2));
4162 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4163 DAG.getConstant(32, MVT::i8));
4165 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4168 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4169 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
4172 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4173 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4174 MVT::i32, Copy1.getValue(2));
4175 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4176 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4177 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
4180 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4181 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4183 if (!Subtarget->is64Bit()) {
4184 // vastart just stores the address of the VarArgsFrameIndex slot into the
4185 // memory location argument.
4186 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4187 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4192 // gp_offset (0 - 6 * 8)
4193 // fp_offset (48 - 48 + 8 * 16)
4194 // overflow_arg_area (point to parameters coming in memory).
4196 SmallVector<SDOperand, 8> MemOps;
4197 SDOperand FIN = Op.getOperand(1);
4199 SDOperand Store = DAG.getStore(Op.getOperand(0),
4200 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4201 FIN, SV->getValue(), SV->getOffset());
4202 MemOps.push_back(Store);
4205 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4206 DAG.getConstant(4, getPointerTy()));
4207 Store = DAG.getStore(Op.getOperand(0),
4208 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4209 FIN, SV->getValue(), SV->getOffset());
4210 MemOps.push_back(Store);
4212 // Store ptr to overflow_arg_area
4213 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4214 DAG.getConstant(4, getPointerTy()));
4215 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4216 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4218 MemOps.push_back(Store);
4220 // Store ptr to reg_save_area.
4221 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4222 DAG.getConstant(8, getPointerTy()));
4223 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4224 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4226 MemOps.push_back(Store);
4227 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4230 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4231 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4232 SDOperand Chain = Op.getOperand(0);
4233 SDOperand DstPtr = Op.getOperand(1);
4234 SDOperand SrcPtr = Op.getOperand(2);
4235 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4236 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4238 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4239 SrcSV->getValue(), SrcSV->getOffset());
4240 Chain = SrcPtr.getValue(1);
4241 for (unsigned i = 0; i < 3; ++i) {
4242 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4243 SrcSV->getValue(), SrcSV->getOffset());
4244 Chain = Val.getValue(1);
4245 Chain = DAG.getStore(Chain, Val, DstPtr,
4246 DstSV->getValue(), DstSV->getOffset());
4249 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4250 DAG.getConstant(8, getPointerTy()));
4251 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4252 DAG.getConstant(8, getPointerTy()));
4258 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4259 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4261 default: return SDOperand(); // Don't custom lower most intrinsics.
4262 // Comparison intrinsics.
4263 case Intrinsic::x86_sse_comieq_ss:
4264 case Intrinsic::x86_sse_comilt_ss:
4265 case Intrinsic::x86_sse_comile_ss:
4266 case Intrinsic::x86_sse_comigt_ss:
4267 case Intrinsic::x86_sse_comige_ss:
4268 case Intrinsic::x86_sse_comineq_ss:
4269 case Intrinsic::x86_sse_ucomieq_ss:
4270 case Intrinsic::x86_sse_ucomilt_ss:
4271 case Intrinsic::x86_sse_ucomile_ss:
4272 case Intrinsic::x86_sse_ucomigt_ss:
4273 case Intrinsic::x86_sse_ucomige_ss:
4274 case Intrinsic::x86_sse_ucomineq_ss:
4275 case Intrinsic::x86_sse2_comieq_sd:
4276 case Intrinsic::x86_sse2_comilt_sd:
4277 case Intrinsic::x86_sse2_comile_sd:
4278 case Intrinsic::x86_sse2_comigt_sd:
4279 case Intrinsic::x86_sse2_comige_sd:
4280 case Intrinsic::x86_sse2_comineq_sd:
4281 case Intrinsic::x86_sse2_ucomieq_sd:
4282 case Intrinsic::x86_sse2_ucomilt_sd:
4283 case Intrinsic::x86_sse2_ucomile_sd:
4284 case Intrinsic::x86_sse2_ucomigt_sd:
4285 case Intrinsic::x86_sse2_ucomige_sd:
4286 case Intrinsic::x86_sse2_ucomineq_sd: {
4288 ISD::CondCode CC = ISD::SETCC_INVALID;
4291 case Intrinsic::x86_sse_comieq_ss:
4292 case Intrinsic::x86_sse2_comieq_sd:
4296 case Intrinsic::x86_sse_comilt_ss:
4297 case Intrinsic::x86_sse2_comilt_sd:
4301 case Intrinsic::x86_sse_comile_ss:
4302 case Intrinsic::x86_sse2_comile_sd:
4306 case Intrinsic::x86_sse_comigt_ss:
4307 case Intrinsic::x86_sse2_comigt_sd:
4311 case Intrinsic::x86_sse_comige_ss:
4312 case Intrinsic::x86_sse2_comige_sd:
4316 case Intrinsic::x86_sse_comineq_ss:
4317 case Intrinsic::x86_sse2_comineq_sd:
4321 case Intrinsic::x86_sse_ucomieq_ss:
4322 case Intrinsic::x86_sse2_ucomieq_sd:
4323 Opc = X86ISD::UCOMI;
4326 case Intrinsic::x86_sse_ucomilt_ss:
4327 case Intrinsic::x86_sse2_ucomilt_sd:
4328 Opc = X86ISD::UCOMI;
4331 case Intrinsic::x86_sse_ucomile_ss:
4332 case Intrinsic::x86_sse2_ucomile_sd:
4333 Opc = X86ISD::UCOMI;
4336 case Intrinsic::x86_sse_ucomigt_ss:
4337 case Intrinsic::x86_sse2_ucomigt_sd:
4338 Opc = X86ISD::UCOMI;
4341 case Intrinsic::x86_sse_ucomige_ss:
4342 case Intrinsic::x86_sse2_ucomige_sd:
4343 Opc = X86ISD::UCOMI;
4346 case Intrinsic::x86_sse_ucomineq_ss:
4347 case Intrinsic::x86_sse2_ucomineq_sd:
4348 Opc = X86ISD::UCOMI;
4354 SDOperand LHS = Op.getOperand(1);
4355 SDOperand RHS = Op.getOperand(2);
4356 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4358 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4359 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4360 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4361 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4362 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4363 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4364 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4369 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4370 // Depths > 0 not supported yet!
4371 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4374 // Just load the return address
4375 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4376 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4379 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4380 // Depths > 0 not supported yet!
4381 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4384 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4385 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4386 DAG.getConstant(4, getPointerTy()));
4389 SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4390 SelectionDAG &DAG) {
4391 // Is not yet supported on x86-64
4392 if (Subtarget->is64Bit())
4395 return DAG.getConstant(8, getPointerTy());
4398 SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4400 assert(!Subtarget->is64Bit() &&
4401 "Lowering of eh_return builtin is not supported yet on x86-64");
4403 MachineFunction &MF = DAG.getMachineFunction();
4404 SDOperand Chain = Op.getOperand(0);
4405 SDOperand Offset = Op.getOperand(1);
4406 SDOperand Handler = Op.getOperand(2);
4408 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4411 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4412 DAG.getConstant(-4UL, getPointerTy()));
4413 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4414 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4415 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4416 MF.addLiveOut(X86::ECX);
4418 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4419 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4422 SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4423 SelectionDAG &DAG) {
4424 SDOperand Root = Op.getOperand(0);
4425 SDOperand Trmp = Op.getOperand(1); // trampoline
4426 SDOperand FPtr = Op.getOperand(2); // nested function
4427 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4429 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4431 if (Subtarget->is64Bit()) {
4432 return SDOperand(); // not yet supported
4434 Function *Func = (Function *)
4435 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4436 unsigned CC = Func->getCallingConv();
4441 assert(0 && "Unsupported calling convention");
4442 case CallingConv::C:
4443 case CallingConv::Fast:
4444 case CallingConv::X86_StdCall: {
4445 // Pass 'nest' parameter in ECX.
4446 // Must be kept in sync with X86CallingConv.td
4449 // Check that ECX wasn't needed by an 'inreg' parameter.
4450 const FunctionType *FTy = Func->getFunctionType();
4451 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4453 if (Attrs && !Func->isVarArg()) {
4454 unsigned InRegCount = 0;
4457 for (FunctionType::param_iterator I = FTy->param_begin(),
4458 E = FTy->param_end(); I != E; ++I, ++Idx)
4459 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4460 // FIXME: should only count parameters that are lowered to integers.
4461 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4463 if (InRegCount > 2) {
4464 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4470 case CallingConv::X86_FastCall:
4471 // Pass 'nest' parameter in EAX.
4472 // Must be kept in sync with X86CallingConv.td
4477 const X86InstrInfo *TII =
4478 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4480 SDOperand OutChains[4];
4481 SDOperand Addr, Disp;
4483 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4484 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4486 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
4487 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
4488 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
4489 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4491 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4492 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4493 TrmpSV->getOffset() + 1, false, 1);
4495 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
4496 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4497 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4498 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4500 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4501 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4502 TrmpSV->getOffset() + 6, false, 1);
4505 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4506 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
4510 /// LowerOperation - Provide custom lowering hooks for some operations.
4512 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4513 switch (Op.getOpcode()) {
4514 default: assert(0 && "Should not custom lower this!");
4515 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4516 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4517 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4518 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4519 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4520 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4521 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4522 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
4523 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4524 case ISD::SHL_PARTS:
4525 case ISD::SRA_PARTS:
4526 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4527 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4528 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4529 case ISD::FABS: return LowerFABS(Op, DAG);
4530 case ISD::FNEG: return LowerFNEG(Op, DAG);
4531 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
4532 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
4533 case ISD::SELECT: return LowerSELECT(Op, DAG);
4534 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4535 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4536 case ISD::CALL: return LowerCALL(Op, DAG);
4537 case ISD::RET: return LowerRET(Op, DAG);
4538 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4539 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4540 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4541 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4542 case ISD::VASTART: return LowerVASTART(Op, DAG);
4543 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
4544 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4545 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4546 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4547 case ISD::FRAME_TO_ARGS_OFFSET:
4548 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
4549 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
4550 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
4551 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
4556 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4558 default: return NULL;
4559 case X86ISD::SHLD: return "X86ISD::SHLD";
4560 case X86ISD::SHRD: return "X86ISD::SHRD";
4561 case X86ISD::FAND: return "X86ISD::FAND";
4562 case X86ISD::FOR: return "X86ISD::FOR";
4563 case X86ISD::FXOR: return "X86ISD::FXOR";
4564 case X86ISD::FSRL: return "X86ISD::FSRL";
4565 case X86ISD::FILD: return "X86ISD::FILD";
4566 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4567 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4568 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4569 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4570 case X86ISD::FLD: return "X86ISD::FLD";
4571 case X86ISD::FST: return "X86ISD::FST";
4572 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4573 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4574 case X86ISD::CALL: return "X86ISD::CALL";
4575 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4576 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4577 case X86ISD::CMP: return "X86ISD::CMP";
4578 case X86ISD::COMI: return "X86ISD::COMI";
4579 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4580 case X86ISD::SETCC: return "X86ISD::SETCC";
4581 case X86ISD::CMOV: return "X86ISD::CMOV";
4582 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4583 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4584 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4585 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4586 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4587 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4588 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4589 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4590 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4591 case X86ISD::FMAX: return "X86ISD::FMAX";
4592 case X86ISD::FMIN: return "X86ISD::FMIN";
4593 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
4594 case X86ISD::FRCP: return "X86ISD::FRCP";
4595 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4596 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
4597 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
4601 // isLegalAddressingMode - Return true if the addressing mode represented
4602 // by AM is legal for this target, for a load/store of the specified type.
4603 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4604 const Type *Ty) const {
4605 // X86 supports extremely general addressing modes.
4607 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4608 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4612 // We can only fold this if we don't need an extra load.
4613 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4616 // X86-64 only supports addr of globals in small code model.
4617 if (Subtarget->is64Bit()) {
4618 if (getTargetMachine().getCodeModel() != CodeModel::Small)
4620 // If lower 4G is not available, then we must use rip-relative addressing.
4621 if (AM.BaseOffs || AM.Scale > 1)
4632 // These scales always work.
4637 // These scales are formed with basereg+scalereg. Only accept if there is
4642 default: // Other stuff never works.
4650 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4651 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4652 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4653 /// are assumed to be legal.
4655 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4656 // Only do shuffles on 128-bit vector types for now.
4657 if (MVT::getSizeInBits(VT) == 64) return false;
4658 return (Mask.Val->getNumOperands() <= 4 ||
4659 isIdentityMask(Mask.Val) ||
4660 isIdentityMask(Mask.Val, true) ||
4661 isSplatMask(Mask.Val) ||
4662 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4663 X86::isUNPCKLMask(Mask.Val) ||
4664 X86::isUNPCKHMask(Mask.Val) ||
4665 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4666 X86::isUNPCKH_v_undef_Mask(Mask.Val));
4669 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4671 SelectionDAG &DAG) const {
4672 unsigned NumElts = BVOps.size();
4673 // Only do shuffles on 128-bit vector types for now.
4674 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4675 if (NumElts == 2) return true;
4677 return (isMOVLMask(&BVOps[0], 4) ||
4678 isCommutedMOVL(&BVOps[0], 4, true) ||
4679 isSHUFPMask(&BVOps[0], 4) ||
4680 isCommutedSHUFP(&BVOps[0], 4));
4685 //===----------------------------------------------------------------------===//
4686 // X86 Scheduler Hooks
4687 //===----------------------------------------------------------------------===//
4690 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4691 MachineBasicBlock *BB) {
4692 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4693 switch (MI->getOpcode()) {
4694 default: assert(false && "Unexpected instr type to insert");
4695 case X86::CMOV_FR32:
4696 case X86::CMOV_FR64:
4697 case X86::CMOV_V4F32:
4698 case X86::CMOV_V2F64:
4699 case X86::CMOV_V2I64: {
4700 // To "insert" a SELECT_CC instruction, we actually have to insert the
4701 // diamond control-flow pattern. The incoming instruction knows the
4702 // destination vreg to set, the condition code register to branch on, the
4703 // true/false values to select between, and a branch opcode to use.
4704 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4705 ilist<MachineBasicBlock>::iterator It = BB;
4711 // cmpTY ccX, r1, r2
4713 // fallthrough --> copy0MBB
4714 MachineBasicBlock *thisMBB = BB;
4715 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4716 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4718 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4719 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4720 MachineFunction *F = BB->getParent();
4721 F->getBasicBlockList().insert(It, copy0MBB);
4722 F->getBasicBlockList().insert(It, sinkMBB);
4723 // Update machine-CFG edges by first adding all successors of the current
4724 // block to the new block which will contain the Phi node for the select.
4725 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4726 e = BB->succ_end(); i != e; ++i)
4727 sinkMBB->addSuccessor(*i);
4728 // Next, remove all successors of the current block, and add the true
4729 // and fallthrough blocks as its successors.
4730 while(!BB->succ_empty())
4731 BB->removeSuccessor(BB->succ_begin());
4732 BB->addSuccessor(copy0MBB);
4733 BB->addSuccessor(sinkMBB);
4736 // %FalseValue = ...
4737 // # fallthrough to sinkMBB
4740 // Update machine-CFG edges
4741 BB->addSuccessor(sinkMBB);
4744 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4747 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4748 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4749 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4751 delete MI; // The pseudo instruction is gone now.
4755 case X86::FP32_TO_INT16_IN_MEM:
4756 case X86::FP32_TO_INT32_IN_MEM:
4757 case X86::FP32_TO_INT64_IN_MEM:
4758 case X86::FP64_TO_INT16_IN_MEM:
4759 case X86::FP64_TO_INT32_IN_MEM:
4760 case X86::FP64_TO_INT64_IN_MEM:
4761 case X86::FP80_TO_INT16_IN_MEM:
4762 case X86::FP80_TO_INT32_IN_MEM:
4763 case X86::FP80_TO_INT64_IN_MEM: {
4764 // Change the floating point control register to use "round towards zero"
4765 // mode when truncating to an integer value.
4766 MachineFunction *F = BB->getParent();
4767 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4768 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4770 // Load the old value of the high byte of the control word...
4772 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4773 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4775 // Set the high part to be round to zero...
4776 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4779 // Reload the modified control word now...
4780 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4782 // Restore the memory image of control word to original value
4783 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4786 // Get the X86 opcode to use.
4788 switch (MI->getOpcode()) {
4789 default: assert(0 && "illegal opcode!");
4790 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
4791 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
4792 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
4793 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
4794 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
4795 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
4796 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
4797 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
4798 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
4802 MachineOperand &Op = MI->getOperand(0);
4803 if (Op.isRegister()) {
4804 AM.BaseType = X86AddressMode::RegBase;
4805 AM.Base.Reg = Op.getReg();
4807 AM.BaseType = X86AddressMode::FrameIndexBase;
4808 AM.Base.FrameIndex = Op.getFrameIndex();
4810 Op = MI->getOperand(1);
4811 if (Op.isImmediate())
4812 AM.Scale = Op.getImm();
4813 Op = MI->getOperand(2);
4814 if (Op.isImmediate())
4815 AM.IndexReg = Op.getImm();
4816 Op = MI->getOperand(3);
4817 if (Op.isGlobalAddress()) {
4818 AM.GV = Op.getGlobal();
4820 AM.Disp = Op.getImm();
4822 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4823 .addReg(MI->getOperand(4).getReg());
4825 // Reload the original control word now.
4826 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4828 delete MI; // The pseudo instruction is gone now.
4834 //===----------------------------------------------------------------------===//
4835 // X86 Optimization Hooks
4836 //===----------------------------------------------------------------------===//
4838 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4840 uint64_t &KnownZero,
4842 const SelectionDAG &DAG,
4843 unsigned Depth) const {
4844 unsigned Opc = Op.getOpcode();
4845 assert((Opc >= ISD::BUILTIN_OP_END ||
4846 Opc == ISD::INTRINSIC_WO_CHAIN ||
4847 Opc == ISD::INTRINSIC_W_CHAIN ||
4848 Opc == ISD::INTRINSIC_VOID) &&
4849 "Should use MaskedValueIsZero if you don't know whether Op"
4850 " is a target node!");
4852 KnownZero = KnownOne = 0; // Don't know anything.
4856 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4861 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
4862 /// element of the result of the vector shuffle.
4863 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4864 MVT::ValueType VT = N->getValueType(0);
4865 SDOperand PermMask = N->getOperand(2);
4866 unsigned NumElems = PermMask.getNumOperands();
4867 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4869 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4871 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4872 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4873 SDOperand Idx = PermMask.getOperand(i);
4874 if (Idx.getOpcode() == ISD::UNDEF)
4875 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4876 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4881 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4882 /// node is a GlobalAddress + an offset.
4883 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4884 unsigned Opc = N->getOpcode();
4885 if (Opc == X86ISD::Wrapper) {
4886 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4887 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4890 } else if (Opc == ISD::ADD) {
4891 SDOperand N1 = N->getOperand(0);
4892 SDOperand N2 = N->getOperand(1);
4893 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4894 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4896 Offset += V->getSignExtended();
4899 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4900 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4902 Offset += V->getSignExtended();
4910 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4912 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4913 MachineFrameInfo *MFI) {
4914 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4917 SDOperand Loc = N->getOperand(1);
4918 SDOperand BaseLoc = Base->getOperand(1);
4919 if (Loc.getOpcode() == ISD::FrameIndex) {
4920 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4922 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
4923 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4924 int FS = MFI->getObjectSize(FI);
4925 int BFS = MFI->getObjectSize(BFI);
4926 if (FS != BFS || FS != Size) return false;
4927 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4929 GlobalValue *GV1 = NULL;
4930 GlobalValue *GV2 = NULL;
4931 int64_t Offset1 = 0;
4932 int64_t Offset2 = 0;
4933 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4934 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4935 if (isGA1 && isGA2 && GV1 == GV2)
4936 return Offset1 == (Offset2 + Dist*Size);
4942 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4943 const X86Subtarget *Subtarget) {
4946 if (isGAPlusOffset(Base, GV, Offset))
4947 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4949 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4950 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
4952 // Fixed objects do not specify alignment, however the offsets are known.
4953 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4954 (MFI->getObjectOffset(BFI) % 16) == 0);
4956 return MFI->getObjectAlignment(BFI) >= 16;
4962 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4963 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4964 /// if the load addresses are consecutive, non-overlapping, and in the right
4966 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4967 const X86Subtarget *Subtarget) {
4968 MachineFunction &MF = DAG.getMachineFunction();
4969 MachineFrameInfo *MFI = MF.getFrameInfo();
4970 MVT::ValueType VT = N->getValueType(0);
4971 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4972 SDOperand PermMask = N->getOperand(2);
4973 int NumElems = (int)PermMask.getNumOperands();
4974 SDNode *Base = NULL;
4975 for (int i = 0; i < NumElems; ++i) {
4976 SDOperand Idx = PermMask.getOperand(i);
4977 if (Idx.getOpcode() == ISD::UNDEF) {
4978 if (!Base) return SDOperand();
4981 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4982 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
4986 else if (!isConsecutiveLoad(Arg.Val, Base,
4987 i, MVT::getSizeInBits(EVT)/8,MFI))
4992 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
4993 LoadSDNode *LD = cast<LoadSDNode>(Base);
4995 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4996 LD->getSrcValueOffset(), LD->isVolatile());
4998 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4999 LD->getSrcValueOffset(), LD->isVolatile(),
5000 LD->getAlignment());
5004 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5005 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5006 const X86Subtarget *Subtarget) {
5007 SDOperand Cond = N->getOperand(0);
5009 // If we have SSE[12] support, try to form min/max nodes.
5010 if (Subtarget->hasSSE2() &&
5011 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5012 if (Cond.getOpcode() == ISD::SETCC) {
5013 // Get the LHS/RHS of the select.
5014 SDOperand LHS = N->getOperand(1);
5015 SDOperand RHS = N->getOperand(2);
5016 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5018 unsigned Opcode = 0;
5019 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5022 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5025 if (!UnsafeFPMath) break;
5027 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5029 Opcode = X86ISD::FMIN;
5032 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5035 if (!UnsafeFPMath) break;
5037 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5039 Opcode = X86ISD::FMAX;
5042 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5045 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5048 if (!UnsafeFPMath) break;
5050 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5052 Opcode = X86ISD::FMIN;
5055 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5058 if (!UnsafeFPMath) break;
5060 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5062 Opcode = X86ISD::FMAX;
5068 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5077 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5078 DAGCombinerInfo &DCI) const {
5079 SelectionDAG &DAG = DCI.DAG;
5080 switch (N->getOpcode()) {
5082 case ISD::VECTOR_SHUFFLE:
5083 return PerformShuffleCombine(N, DAG, Subtarget);
5085 return PerformSELECTCombine(N, DAG, Subtarget);
5091 //===----------------------------------------------------------------------===//
5092 // X86 Inline Assembly Support
5093 //===----------------------------------------------------------------------===//
5095 /// getConstraintType - Given a constraint letter, return the type of
5096 /// constraint it is for this target.
5097 X86TargetLowering::ConstraintType
5098 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5099 if (Constraint.size() == 1) {
5100 switch (Constraint[0]) {
5109 return C_RegisterClass;
5114 return TargetLowering::getConstraintType(Constraint);
5117 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5118 /// vector. If it is invalid, don't add anything to Ops.
5119 void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5121 std::vector<SDOperand>&Ops,
5122 SelectionDAG &DAG) {
5123 SDOperand Result(0, 0);
5125 switch (Constraint) {
5128 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
5129 if (C->getValue() <= 31) {
5130 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5136 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
5137 if (C->getValue() <= 255) {
5138 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5144 // Literal immediates are always ok.
5145 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5146 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5150 // If we are in non-pic codegen mode, we allow the address of a global (with
5151 // an optional displacement) to be used with 'i'.
5152 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5155 // Match either (GA) or (GA+C)
5157 Offset = GA->getOffset();
5158 } else if (Op.getOpcode() == ISD::ADD) {
5159 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5160 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5162 Offset = GA->getOffset()+C->getValue();
5164 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5165 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5167 Offset = GA->getOffset()+C->getValue();
5174 // If addressing this global requires a load (e.g. in PIC mode), we can't
5176 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5180 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5186 // Otherwise, not valid for this mode.
5192 Ops.push_back(Result);
5195 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
5198 std::vector<unsigned> X86TargetLowering::
5199 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5200 MVT::ValueType VT) const {
5201 if (Constraint.size() == 1) {
5202 // FIXME: not handling fp-stack yet!
5203 switch (Constraint[0]) { // GCC X86 Constraint Letters
5204 default: break; // Unknown constraint letter
5205 case 'A': // EAX/EDX
5206 if (VT == MVT::i32 || VT == MVT::i64)
5207 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5209 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5212 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5213 else if (VT == MVT::i16)
5214 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5215 else if (VT == MVT::i8)
5216 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
5221 return std::vector<unsigned>();
5224 std::pair<unsigned, const TargetRegisterClass*>
5225 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5226 MVT::ValueType VT) const {
5227 // First, see if this is a constraint that directly corresponds to an LLVM
5229 if (Constraint.size() == 1) {
5230 // GCC Constraint Letters
5231 switch (Constraint[0]) {
5233 case 'r': // GENERAL_REGS
5234 case 'R': // LEGACY_REGS
5235 case 'l': // INDEX_REGS
5236 if (VT == MVT::i64 && Subtarget->is64Bit())
5237 return std::make_pair(0U, X86::GR64RegisterClass);
5239 return std::make_pair(0U, X86::GR32RegisterClass);
5240 else if (VT == MVT::i16)
5241 return std::make_pair(0U, X86::GR16RegisterClass);
5242 else if (VT == MVT::i8)
5243 return std::make_pair(0U, X86::GR8RegisterClass);
5245 case 'y': // MMX_REGS if MMX allowed.
5246 if (!Subtarget->hasMMX()) break;
5247 return std::make_pair(0U, X86::VR64RegisterClass);
5249 case 'Y': // SSE_REGS if SSE2 allowed
5250 if (!Subtarget->hasSSE2()) break;
5252 case 'x': // SSE_REGS if SSE1 allowed
5253 if (!Subtarget->hasSSE1()) break;
5257 // Scalar SSE types.
5260 return std::make_pair(0U, X86::FR32RegisterClass);
5263 return std::make_pair(0U, X86::FR64RegisterClass);
5271 return std::make_pair(0U, X86::VR128RegisterClass);
5277 // Use the default implementation in TargetLowering to convert the register
5278 // constraint into a member of a register class.
5279 std::pair<unsigned, const TargetRegisterClass*> Res;
5280 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5282 // Not found as a standard register?
5283 if (Res.second == 0) {
5284 // GCC calls "st(0)" just plain "st".
5285 if (StringsEqualNoCase("{st}", Constraint)) {
5286 Res.first = X86::ST0;
5287 Res.second = X86::RFP80RegisterClass;
5293 // Otherwise, check to see if this is a register class of the wrong value
5294 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5295 // turn into {ax},{dx}.
5296 if (Res.second->hasType(VT))
5297 return Res; // Correct type already, nothing to do.
5299 // All of the single-register GCC register classes map their values onto
5300 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5301 // really want an 8-bit or 32-bit register, map to the appropriate register
5302 // class and return the appropriate register.
5303 if (Res.second != X86::GR16RegisterClass)
5306 if (VT == MVT::i8) {
5307 unsigned DestReg = 0;
5308 switch (Res.first) {
5310 case X86::AX: DestReg = X86::AL; break;
5311 case X86::DX: DestReg = X86::DL; break;
5312 case X86::CX: DestReg = X86::CL; break;
5313 case X86::BX: DestReg = X86::BL; break;
5316 Res.first = DestReg;
5317 Res.second = Res.second = X86::GR8RegisterClass;
5319 } else if (VT == MVT::i32) {
5320 unsigned DestReg = 0;
5321 switch (Res.first) {
5323 case X86::AX: DestReg = X86::EAX; break;
5324 case X86::DX: DestReg = X86::EDX; break;
5325 case X86::CX: DestReg = X86::ECX; break;
5326 case X86::BX: DestReg = X86::EBX; break;
5327 case X86::SI: DestReg = X86::ESI; break;
5328 case X86::DI: DestReg = X86::EDI; break;
5329 case X86::BP: DestReg = X86::EBP; break;
5330 case X86::SP: DestReg = X86::ESP; break;
5333 Res.first = DestReg;
5334 Res.second = Res.second = X86::GR32RegisterClass;
5336 } else if (VT == MVT::i64) {
5337 unsigned DestReg = 0;
5338 switch (Res.first) {
5340 case X86::AX: DestReg = X86::RAX; break;
5341 case X86::DX: DestReg = X86::RDX; break;
5342 case X86::CX: DestReg = X86::RCX; break;
5343 case X86::BX: DestReg = X86::RBX; break;
5344 case X86::SI: DestReg = X86::RSI; break;
5345 case X86::DI: DestReg = X86::RDI; break;
5346 case X86::BP: DestReg = X86::RBP; break;
5347 case X86::SP: DestReg = X86::RSP; break;
5350 Res.first = DestReg;
5351 Res.second = Res.second = X86::GR64RegisterClass;