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/TargetOptions.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/ParameterAttributes.h"
40 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
41 : TargetLowering(TM) {
42 Subtarget = &TM.getSubtarget<X86Subtarget>();
43 X86ScalarSSEf64 = Subtarget->hasSSE2();
44 X86ScalarSSEf32 = Subtarget->hasSSE1();
45 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
47 RegInfo = TM.getRegisterInfo();
49 // Set up the TargetLowering object.
51 // X86 is weird, it always uses i8 for shift amounts and setcc results.
52 setShiftAmountType(MVT::i8);
53 setSetCCResultType(MVT::i8);
54 setSetCCResultContents(ZeroOrOneSetCCResult);
55 setSchedulingPreference(SchedulingForRegPressure);
56 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
57 setStackPointerRegisterToSaveRestore(X86StackPtr);
59 if (Subtarget->isTargetDarwin()) {
60 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
61 setUseUnderscoreSetJmp(false);
62 setUseUnderscoreLongJmp(false);
63 } else if (Subtarget->isTargetMingw()) {
64 // MS runtime is weird: it exports _setjmp, but longjmp!
65 setUseUnderscoreSetJmp(true);
66 setUseUnderscoreLongJmp(false);
68 setUseUnderscoreSetJmp(true);
69 setUseUnderscoreLongJmp(true);
72 // Set up the register classes.
73 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
74 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
75 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
76 if (Subtarget->is64Bit())
77 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
79 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
81 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
83 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
84 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
85 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
87 if (Subtarget->is64Bit()) {
88 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
89 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
92 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
93 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
95 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
98 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
100 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
101 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
102 // SSE has no i16 to fp conversion, only i32
103 if (X86ScalarSSEf32) {
104 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
105 // f32 and f64 cases are Legal, f80 case is not
106 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
108 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
109 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
112 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
113 // are Legal, f80 is custom lowered.
114 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
115 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
117 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
119 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
120 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
122 if (X86ScalarSSEf32) {
123 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
124 // f32 and f64 cases are Legal, f80 case is not
125 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
127 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
128 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
131 // Handle FP_TO_UINT by promoting the destination to a larger signed
133 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
134 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
135 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
137 if (Subtarget->is64Bit()) {
138 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
141 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
142 // Expand FP_TO_UINT into a select.
143 // FIXME: We would like to use a Custom expander here eventually to do
144 // the optimal thing for SSE vs. the default expansion in the legalizer.
145 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
147 // With SSE3 we can use fisttpll to convert to a signed i64.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
151 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
152 if (!X86ScalarSSEf64) {
153 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
154 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
157 // Divide and remainder are lowered to use div or idiv in legalize in
158 // order to expose the intermediate computations to trivial CSE. This is
159 // most noticeable when both x/y and x%y are being computed; they can be
160 // done with a single div or idiv.
161 setOperationAction(ISD::SDIV , MVT::i8 , Custom);
162 setOperationAction(ISD::UDIV , MVT::i8 , Custom);
163 setOperationAction(ISD::SREM , MVT::i8 , Custom);
164 setOperationAction(ISD::UREM , MVT::i8 , Custom);
165 setOperationAction(ISD::SDIV , MVT::i16 , Custom);
166 setOperationAction(ISD::UDIV , MVT::i16 , Custom);
167 setOperationAction(ISD::SREM , MVT::i16 , Custom);
168 setOperationAction(ISD::UREM , MVT::i16 , Custom);
169 setOperationAction(ISD::SDIV , MVT::i32 , Custom);
170 setOperationAction(ISD::UDIV , MVT::i32 , Custom);
171 setOperationAction(ISD::SREM , MVT::i32 , Custom);
172 setOperationAction(ISD::UREM , MVT::i32 , Custom);
173 setOperationAction(ISD::SDIV , MVT::i64 , Custom);
174 setOperationAction(ISD::UDIV , MVT::i64 , Custom);
175 setOperationAction(ISD::SREM , MVT::i64 , Custom);
176 setOperationAction(ISD::UREM , MVT::i64 , Custom);
178 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
179 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
180 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
181 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
182 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
183 if (Subtarget->is64Bit())
184 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
185 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
186 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
187 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
188 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
189 setOperationAction(ISD::FREM , MVT::f64 , Expand);
191 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
192 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
193 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
194 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
195 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
196 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
197 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
198 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
199 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
200 if (Subtarget->is64Bit()) {
201 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
202 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
203 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
206 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
207 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
209 // These should be promoted to a larger select which is supported.
210 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
211 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
212 // X86 wants to expand cmov itself.
213 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
214 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
215 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
216 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
217 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
218 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
219 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
220 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
221 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
222 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
223 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
224 if (Subtarget->is64Bit()) {
225 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
226 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
228 // X86 ret instruction may pop stack.
229 setOperationAction(ISD::RET , MVT::Other, Custom);
230 if (!Subtarget->is64Bit())
231 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
234 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
235 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
236 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
237 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
238 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
239 if (Subtarget->is64Bit()) {
240 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
241 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
242 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
243 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
245 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
246 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
247 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
248 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
249 // X86 wants to expand memset / memcpy itself.
250 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
251 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
253 // Use the default ISD::LOCATION expansion.
254 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
255 // FIXME - use subtarget debug flags
256 if (!Subtarget->isTargetDarwin() &&
257 !Subtarget->isTargetELF() &&
258 !Subtarget->isTargetCygMing())
259 setOperationAction(ISD::LABEL, MVT::Other, Expand);
261 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
262 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
263 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
264 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
265 if (Subtarget->is64Bit()) {
267 setExceptionPointerRegister(X86::RAX);
268 setExceptionSelectorRegister(X86::RDX);
270 setExceptionPointerRegister(X86::EAX);
271 setExceptionSelectorRegister(X86::EDX);
273 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
275 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
277 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
278 setOperationAction(ISD::VASTART , MVT::Other, Custom);
279 setOperationAction(ISD::VAARG , MVT::Other, Expand);
280 setOperationAction(ISD::VAEND , MVT::Other, Expand);
281 if (Subtarget->is64Bit())
282 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
284 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
286 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
287 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
288 if (Subtarget->is64Bit())
289 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
290 if (Subtarget->isTargetCygMing())
291 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
293 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
295 if (X86ScalarSSEf64) {
296 // f32 and f64 use SSE.
297 // Set up the FP register classes.
298 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
299 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
301 // Use ANDPD to simulate FABS.
302 setOperationAction(ISD::FABS , MVT::f64, Custom);
303 setOperationAction(ISD::FABS , MVT::f32, Custom);
305 // Use XORP to simulate FNEG.
306 setOperationAction(ISD::FNEG , MVT::f64, Custom);
307 setOperationAction(ISD::FNEG , MVT::f32, Custom);
309 // Use ANDPD and ORPD to simulate FCOPYSIGN.
310 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
311 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
313 // We don't support sin/cos/fmod
314 setOperationAction(ISD::FSIN , MVT::f64, Expand);
315 setOperationAction(ISD::FCOS , MVT::f64, Expand);
316 setOperationAction(ISD::FREM , MVT::f64, Expand);
317 setOperationAction(ISD::FSIN , MVT::f32, Expand);
318 setOperationAction(ISD::FCOS , MVT::f32, Expand);
319 setOperationAction(ISD::FREM , MVT::f32, Expand);
321 // Expand FP immediates into loads from the stack, except for the special
323 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
324 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
325 addLegalFPImmediate(APFloat(+0.0)); // xorpd
326 addLegalFPImmediate(APFloat(+0.0f)); // xorps
328 // Conversions to long double (in X87) go through memory.
329 setConvertAction(MVT::f32, MVT::f80, Expand);
330 setConvertAction(MVT::f64, MVT::f80, Expand);
332 // Conversions from long double (in X87) go through memory.
333 setConvertAction(MVT::f80, MVT::f32, Expand);
334 setConvertAction(MVT::f80, MVT::f64, Expand);
335 } else if (X86ScalarSSEf32) {
336 // Use SSE for f32, x87 for f64.
337 // Set up the FP register classes.
338 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
339 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
341 // Use ANDPS to simulate FABS.
342 setOperationAction(ISD::FABS , MVT::f32, Custom);
344 // Use XORP to simulate FNEG.
345 setOperationAction(ISD::FNEG , MVT::f32, Custom);
347 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
349 // Use ANDPS and ORPS to simulate FCOPYSIGN.
350 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
351 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
353 // We don't support sin/cos/fmod
354 setOperationAction(ISD::FSIN , MVT::f32, Expand);
355 setOperationAction(ISD::FCOS , MVT::f32, Expand);
356 setOperationAction(ISD::FREM , MVT::f32, Expand);
358 // Expand FP immediates into loads from the stack, except for the special
360 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
361 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
362 addLegalFPImmediate(APFloat(+0.0f)); // xorps
363 addLegalFPImmediate(APFloat(+0.0)); // FLD0
364 addLegalFPImmediate(APFloat(+1.0)); // FLD1
365 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
366 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
368 // SSE->x87 conversions go through memory.
369 setConvertAction(MVT::f32, MVT::f64, Expand);
370 setConvertAction(MVT::f32, MVT::f80, Expand);
372 // x87->SSE truncations need to go through memory.
373 setConvertAction(MVT::f80, MVT::f32, Expand);
374 setConvertAction(MVT::f64, MVT::f32, Expand);
375 // And x87->x87 truncations also.
376 setConvertAction(MVT::f80, MVT::f64, Expand);
379 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
380 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
383 // f32 and f64 in x87.
384 // Set up the FP register classes.
385 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
386 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
388 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
389 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
390 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
391 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
393 // Floating truncations need to go through memory.
394 setConvertAction(MVT::f80, MVT::f32, Expand);
395 setConvertAction(MVT::f64, MVT::f32, Expand);
396 setConvertAction(MVT::f80, MVT::f64, Expand);
399 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
400 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
403 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
404 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
405 addLegalFPImmediate(APFloat(+0.0)); // FLD0
406 addLegalFPImmediate(APFloat(+1.0)); // FLD1
407 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
408 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
409 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
410 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
411 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
412 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
415 // Long double always uses X87.
416 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
417 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
418 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
419 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
421 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
422 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
425 // First set operation action for all vector types to expand. Then we
426 // will selectively turn on ones that can be effectively codegen'd.
427 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
428 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
429 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
430 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
431 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
432 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
433 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
434 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
435 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
436 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
437 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
438 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
439 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
440 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
441 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
442 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
443 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
444 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
445 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
446 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
447 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
448 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
449 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
450 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
451 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
454 if (Subtarget->hasMMX()) {
455 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
456 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
457 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
458 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
460 // FIXME: add MMX packed arithmetics
462 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
463 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
464 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
465 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
467 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
468 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
469 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
471 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
472 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
474 setOperationAction(ISD::AND, MVT::v8i8, Promote);
475 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
476 setOperationAction(ISD::AND, MVT::v4i16, Promote);
477 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
478 setOperationAction(ISD::AND, MVT::v2i32, Promote);
479 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
480 setOperationAction(ISD::AND, MVT::v1i64, Legal);
482 setOperationAction(ISD::OR, MVT::v8i8, Promote);
483 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
484 setOperationAction(ISD::OR, MVT::v4i16, Promote);
485 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
486 setOperationAction(ISD::OR, MVT::v2i32, Promote);
487 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
488 setOperationAction(ISD::OR, MVT::v1i64, Legal);
490 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
491 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
492 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
493 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
494 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
495 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
496 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
498 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
499 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
500 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
501 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
502 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
503 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
504 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
506 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
507 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
508 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
509 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
511 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
512 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
513 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
514 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
516 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
517 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
518 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
519 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
522 if (Subtarget->hasSSE1()) {
523 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
525 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
526 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
527 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
528 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
529 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
530 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
531 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
532 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
533 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
534 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
535 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
538 if (Subtarget->hasSSE2()) {
539 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
540 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
541 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
542 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
543 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
545 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
546 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
547 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
548 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
549 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
550 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
551 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
552 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
553 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
554 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
555 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
556 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
557 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
558 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
559 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
561 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
562 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
563 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
564 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
565 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
566 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
568 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
569 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
570 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
571 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
572 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
574 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
575 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
576 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
577 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
578 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
579 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
581 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
582 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
583 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
584 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
585 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
586 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
587 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
588 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
589 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
590 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
591 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
592 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
595 // Custom lower v2i64 and v2f64 selects.
596 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
597 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
598 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
599 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
602 // We want to custom lower some of our intrinsics.
603 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
605 // We have target-specific dag combine patterns for the following nodes:
606 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
607 setTargetDAGCombine(ISD::SELECT);
609 computeRegisterProperties();
611 // FIXME: These should be based on subtarget info. Plus, the values should
612 // be smaller when we are in optimizing for size mode.
613 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
614 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
615 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
616 allowUnalignedMemoryAccesses = true; // x86 supports it!
620 //===----------------------------------------------------------------------===//
621 // Return Value Calling Convention Implementation
622 //===----------------------------------------------------------------------===//
624 #include "X86GenCallingConv.inc"
626 /// LowerRET - Lower an ISD::RET node.
627 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
628 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
630 SmallVector<CCValAssign, 16> RVLocs;
631 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
632 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
633 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
634 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
637 // If this is the first return lowered for this function, add the regs to the
638 // liveout set for the function.
639 if (DAG.getMachineFunction().liveout_empty()) {
640 for (unsigned i = 0; i != RVLocs.size(); ++i)
641 if (RVLocs[i].isRegLoc())
642 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
645 SDOperand Chain = Op.getOperand(0);
648 // Copy the result values into the output registers.
649 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
650 RVLocs[0].getLocReg() != X86::ST0) {
651 for (unsigned i = 0; i != RVLocs.size(); ++i) {
652 CCValAssign &VA = RVLocs[i];
653 assert(VA.isRegLoc() && "Can only return in registers!");
654 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
656 Flag = Chain.getValue(1);
659 // We need to handle a destination of ST0 specially, because it isn't really
661 SDOperand Value = Op.getOperand(1);
663 // If this is an FP return with ScalarSSE, we need to move the value from
664 // an XMM register onto the fp-stack.
665 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
666 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
669 // If this is a load into a scalarsse value, don't store the loaded value
670 // back to the stack, only to reload it: just replace the scalar-sse load.
671 if (ISD::isNON_EXTLoad(Value.Val) &&
672 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
673 Chain = Value.getOperand(0);
674 MemLoc = Value.getOperand(1);
676 // Spill the value to memory and reload it into top of stack.
677 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
678 MachineFunction &MF = DAG.getMachineFunction();
679 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
680 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
681 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
683 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
684 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
685 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
686 Chain = Value.getValue(1);
689 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
690 SDOperand Ops[] = { Chain, Value };
691 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
692 Flag = Chain.getValue(1);
695 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
697 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
699 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
703 /// LowerCallResult - Lower the result values of an ISD::CALL into the
704 /// appropriate copies out of appropriate physical registers. This assumes that
705 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
706 /// being lowered. The returns a SDNode with the same number of values as the
708 SDNode *X86TargetLowering::
709 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
710 unsigned CallingConv, SelectionDAG &DAG) {
712 // Assign locations to each value returned by this call.
713 SmallVector<CCValAssign, 16> RVLocs;
714 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
715 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
716 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
719 SmallVector<SDOperand, 8> ResultVals;
721 // Copy all of the result registers out of their specified physreg.
722 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
723 for (unsigned i = 0; i != RVLocs.size(); ++i) {
724 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
725 RVLocs[i].getValVT(), InFlag).getValue(1);
726 InFlag = Chain.getValue(2);
727 ResultVals.push_back(Chain.getValue(0));
730 // Copies from the FP stack are special, as ST0 isn't a valid register
731 // before the fp stackifier runs.
733 // Copy ST0 into an RFP register with FP_GET_RESULT.
734 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
735 SDOperand GROps[] = { Chain, InFlag };
736 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
737 Chain = RetVal.getValue(1);
738 InFlag = RetVal.getValue(2);
740 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
742 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
743 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
744 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
745 // shouldn't be necessary except that RFP cannot be live across
746 // multiple blocks. When stackifier is fixed, they can be uncoupled.
747 MachineFunction &MF = DAG.getMachineFunction();
748 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
749 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
751 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
753 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
754 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
755 Chain = RetVal.getValue(1);
757 ResultVals.push_back(RetVal);
760 // Merge everything together with a MERGE_VALUES node.
761 ResultVals.push_back(Chain);
762 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
763 &ResultVals[0], ResultVals.size()).Val;
767 //===----------------------------------------------------------------------===//
768 // C & StdCall Calling Convention implementation
769 //===----------------------------------------------------------------------===//
770 // StdCall calling convention seems to be standard for many Windows' API
771 // routines and around. It differs from C calling convention just a little:
772 // callee should clean up the stack, not caller. Symbols should be also
773 // decorated in some fancy way :) It doesn't support any vector arguments.
775 /// AddLiveIn - This helper function adds the specified physical register to the
776 /// MachineFunction as a live in value. It also creates a corresponding virtual
778 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
779 const TargetRegisterClass *RC) {
780 assert(RC->contains(PReg) && "Not the correct regclass!");
781 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
782 MF.addLiveIn(PReg, VReg);
786 SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
787 const CCValAssign &VA,
788 MachineFrameInfo *MFI,
789 SDOperand Root, unsigned i) {
790 // Create the nodes corresponding to a load from this parameter slot.
791 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
792 VA.getLocMemOffset());
793 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
795 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
797 if (Flags & ISD::ParamFlags::ByVal)
800 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
803 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
805 unsigned NumArgs = Op.Val->getNumValues() - 1;
806 MachineFunction &MF = DAG.getMachineFunction();
807 MachineFrameInfo *MFI = MF.getFrameInfo();
808 SDOperand Root = Op.getOperand(0);
809 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
811 // Assign locations to all of the incoming arguments.
812 SmallVector<CCValAssign, 16> ArgLocs;
813 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
814 getTargetMachine(), ArgLocs);
815 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
817 SmallVector<SDOperand, 8> ArgValues;
818 unsigned LastVal = ~0U;
819 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
820 CCValAssign &VA = ArgLocs[i];
821 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
823 assert(VA.getValNo() != LastVal &&
824 "Don't support value assigned to multiple locs yet");
825 LastVal = VA.getValNo();
828 MVT::ValueType RegVT = VA.getLocVT();
829 TargetRegisterClass *RC;
830 if (RegVT == MVT::i32)
831 RC = X86::GR32RegisterClass;
833 assert(MVT::isVector(RegVT));
834 RC = X86::VR128RegisterClass;
837 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
838 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
840 // If this is an 8 or 16-bit value, it is really passed promoted to 32
841 // bits. Insert an assert[sz]ext to capture this, then truncate to the
843 if (VA.getLocInfo() == CCValAssign::SExt)
844 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
845 DAG.getValueType(VA.getValVT()));
846 else if (VA.getLocInfo() == CCValAssign::ZExt)
847 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
848 DAG.getValueType(VA.getValVT()));
850 if (VA.getLocInfo() != CCValAssign::Full)
851 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
853 ArgValues.push_back(ArgValue);
855 assert(VA.isMemLoc());
856 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
860 unsigned StackSize = CCInfo.getNextStackOffset();
862 ArgValues.push_back(Root);
864 // If the function takes variable number of arguments, make a frame index for
865 // the start of the first vararg value... for expansion of llvm.va_start.
867 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
869 if (isStdCall && !isVarArg) {
870 BytesToPopOnReturn = StackSize; // Callee pops everything..
871 BytesCallerReserves = 0;
873 BytesToPopOnReturn = 0; // Callee pops nothing.
875 // If this is an sret function, the return should pop the hidden pointer.
877 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
878 ISD::ParamFlags::StructReturn))
879 BytesToPopOnReturn = 4;
881 BytesCallerReserves = StackSize;
884 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
886 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
887 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
889 // Return the new list of results.
890 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
891 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
894 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
896 SDOperand Chain = Op.getOperand(0);
897 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
898 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
899 SDOperand Callee = Op.getOperand(4);
900 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
902 // Analyze operands of the call, assigning locations to each operand.
903 SmallVector<CCValAssign, 16> ArgLocs;
904 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
905 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
907 // Get a count of how many bytes are to be pushed on the stack.
908 unsigned NumBytes = CCInfo.getNextStackOffset();
910 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
912 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
913 SmallVector<SDOperand, 8> MemOpChains;
917 // Walk the register/memloc assignments, inserting copies/loads.
918 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
919 CCValAssign &VA = ArgLocs[i];
920 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
922 // Promote the value if needed.
923 switch (VA.getLocInfo()) {
924 default: assert(0 && "Unknown loc info!");
925 case CCValAssign::Full: break;
926 case CCValAssign::SExt:
927 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
929 case CCValAssign::ZExt:
930 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
932 case CCValAssign::AExt:
933 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
938 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
940 assert(VA.isMemLoc());
941 if (StackPtr.Val == 0)
942 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
944 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
949 // If the first argument is an sret pointer, remember it.
950 bool isSRet = NumOps &&
951 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
952 ISD::ParamFlags::StructReturn);
954 if (!MemOpChains.empty())
955 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
956 &MemOpChains[0], MemOpChains.size());
958 // Build a sequence of copy-to-reg nodes chained together with token chain
959 // and flag operands which copy the outgoing args into registers.
961 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
962 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
964 InFlag = Chain.getValue(1);
967 // ELF / PIC requires GOT in the EBX register before function calls via PLT
969 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
970 Subtarget->isPICStyleGOT()) {
971 Chain = DAG.getCopyToReg(Chain, X86::EBX,
972 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
974 InFlag = Chain.getValue(1);
977 // If the callee is a GlobalAddress node (quite common, every direct call is)
978 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
979 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
980 // We should use extra load for direct calls to dllimported functions in
982 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
983 getTargetMachine(), true))
984 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
985 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
986 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
988 // Returns a chain & a flag for retval copy to use.
989 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
990 SmallVector<SDOperand, 8> Ops;
991 Ops.push_back(Chain);
992 Ops.push_back(Callee);
994 // Add argument registers to the end of the list so that they are known live
996 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
997 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
998 RegsToPass[i].second.getValueType()));
1000 // Add an implicit use GOT pointer in EBX.
1001 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1002 Subtarget->isPICStyleGOT())
1003 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1006 Ops.push_back(InFlag);
1008 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1009 NodeTys, &Ops[0], Ops.size());
1010 InFlag = Chain.getValue(1);
1012 // Create the CALLSEQ_END node.
1013 unsigned NumBytesForCalleeToPush = 0;
1015 if (CC == CallingConv::X86_StdCall) {
1017 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1019 NumBytesForCalleeToPush = NumBytes;
1021 // If this is is a call to a struct-return function, the callee
1022 // pops the hidden struct pointer, so we have to push it back.
1023 // This is common for Darwin/X86, Linux & Mingw32 targets.
1024 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1027 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1029 Ops.push_back(Chain);
1030 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1031 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
1032 Ops.push_back(InFlag);
1033 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1034 InFlag = Chain.getValue(1);
1036 // Handle result values, copying them out of physregs into vregs that we
1038 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1042 //===----------------------------------------------------------------------===//
1043 // FastCall Calling Convention implementation
1044 //===----------------------------------------------------------------------===//
1046 // The X86 'fastcall' calling convention passes up to two integer arguments in
1047 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1048 // and requires that the callee pop its arguments off the stack (allowing proper
1049 // tail calls), and has the same return value conventions as C calling convs.
1051 // This calling convention always arranges for the callee pop value to be 8n+4
1052 // bytes, which is needed for tail recursion elimination and stack alignment
1055 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1056 MachineFunction &MF = DAG.getMachineFunction();
1057 MachineFrameInfo *MFI = MF.getFrameInfo();
1058 SDOperand Root = Op.getOperand(0);
1059 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1061 // Assign locations to all of the incoming arguments.
1062 SmallVector<CCValAssign, 16> ArgLocs;
1063 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1064 getTargetMachine(), ArgLocs);
1065 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
1067 SmallVector<SDOperand, 8> ArgValues;
1068 unsigned LastVal = ~0U;
1069 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1070 CCValAssign &VA = ArgLocs[i];
1071 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1073 assert(VA.getValNo() != LastVal &&
1074 "Don't support value assigned to multiple locs yet");
1075 LastVal = VA.getValNo();
1077 if (VA.isRegLoc()) {
1078 MVT::ValueType RegVT = VA.getLocVT();
1079 TargetRegisterClass *RC;
1080 if (RegVT == MVT::i32)
1081 RC = X86::GR32RegisterClass;
1083 assert(MVT::isVector(RegVT));
1084 RC = X86::VR128RegisterClass;
1087 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1088 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1090 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1091 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1093 if (VA.getLocInfo() == CCValAssign::SExt)
1094 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1095 DAG.getValueType(VA.getValVT()));
1096 else if (VA.getLocInfo() == CCValAssign::ZExt)
1097 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1098 DAG.getValueType(VA.getValVT()));
1100 if (VA.getLocInfo() != CCValAssign::Full)
1101 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1103 ArgValues.push_back(ArgValue);
1105 assert(VA.isMemLoc());
1106 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
1110 ArgValues.push_back(Root);
1112 unsigned StackSize = CCInfo.getNextStackOffset();
1114 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1115 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1116 // arguments and the arguments after the retaddr has been pushed are aligned.
1117 if ((StackSize & 7) == 0)
1121 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1122 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1123 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
1124 BytesCallerReserves = 0;
1126 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1127 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1129 // Return the new list of results.
1130 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1131 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1135 X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1136 const SDOperand &StackPtr,
1137 const CCValAssign &VA,
1140 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1141 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1142 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1143 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1144 if (Flags & ISD::ParamFlags::ByVal) {
1145 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1146 ISD::ParamFlags::ByValAlignOffs);
1148 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1149 ISD::ParamFlags::ByValSizeOffs;
1151 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1152 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1154 return DAG.getNode(ISD::MEMCPY, MVT::Other, Chain, PtrOff, Arg, SizeNode,
1157 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1161 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1163 SDOperand Chain = Op.getOperand(0);
1164 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1165 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1166 SDOperand Callee = Op.getOperand(4);
1168 // Analyze operands of the call, assigning locations to each operand.
1169 SmallVector<CCValAssign, 16> ArgLocs;
1170 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1171 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
1173 // Get a count of how many bytes are to be pushed on the stack.
1174 unsigned NumBytes = CCInfo.getNextStackOffset();
1176 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1177 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1178 // arguments and the arguments after the retaddr has been pushed are aligned.
1179 if ((NumBytes & 7) == 0)
1183 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1185 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1186 SmallVector<SDOperand, 8> MemOpChains;
1190 // Walk the register/memloc assignments, inserting copies/loads.
1191 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1192 CCValAssign &VA = ArgLocs[i];
1193 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1195 // Promote the value if needed.
1196 switch (VA.getLocInfo()) {
1197 default: assert(0 && "Unknown loc info!");
1198 case CCValAssign::Full: break;
1199 case CCValAssign::SExt:
1200 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1202 case CCValAssign::ZExt:
1203 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1205 case CCValAssign::AExt:
1206 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1210 if (VA.isRegLoc()) {
1211 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1213 assert(VA.isMemLoc());
1214 if (StackPtr.Val == 0)
1215 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1217 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1222 if (!MemOpChains.empty())
1223 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1224 &MemOpChains[0], MemOpChains.size());
1226 // Build a sequence of copy-to-reg nodes chained together with token chain
1227 // and flag operands which copy the outgoing args into registers.
1229 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1230 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1232 InFlag = Chain.getValue(1);
1235 // If the callee is a GlobalAddress node (quite common, every direct call is)
1236 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1237 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1238 // We should use extra load for direct calls to dllimported functions in
1240 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1241 getTargetMachine(), true))
1242 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1243 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1244 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1246 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1248 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1249 Subtarget->isPICStyleGOT()) {
1250 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1251 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1253 InFlag = Chain.getValue(1);
1256 // Returns a chain & a flag for retval copy to use.
1257 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1258 SmallVector<SDOperand, 8> Ops;
1259 Ops.push_back(Chain);
1260 Ops.push_back(Callee);
1262 // Add argument registers to the end of the list so that they are known live
1264 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1265 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1266 RegsToPass[i].second.getValueType()));
1268 // Add an implicit use GOT pointer in EBX.
1269 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1270 Subtarget->isPICStyleGOT())
1271 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1274 Ops.push_back(InFlag);
1276 // FIXME: Do not generate X86ISD::TAILCALL for now.
1277 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1278 NodeTys, &Ops[0], Ops.size());
1279 InFlag = Chain.getValue(1);
1281 // Returns a flag for retval copy to use.
1282 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1284 Ops.push_back(Chain);
1285 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1286 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1287 Ops.push_back(InFlag);
1288 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1289 InFlag = Chain.getValue(1);
1291 // Handle result values, copying them out of physregs into vregs that we
1293 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1297 //===----------------------------------------------------------------------===//
1298 // X86-64 C Calling Convention implementation
1299 //===----------------------------------------------------------------------===//
1302 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1303 MachineFunction &MF = DAG.getMachineFunction();
1304 MachineFrameInfo *MFI = MF.getFrameInfo();
1305 SDOperand Root = Op.getOperand(0);
1306 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1308 static const unsigned GPR64ArgRegs[] = {
1309 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1311 static const unsigned XMMArgRegs[] = {
1312 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1313 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1317 // Assign locations to all of the incoming arguments.
1318 SmallVector<CCValAssign, 16> ArgLocs;
1319 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1320 getTargetMachine(), ArgLocs);
1321 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1323 SmallVector<SDOperand, 8> ArgValues;
1324 unsigned LastVal = ~0U;
1325 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1326 CCValAssign &VA = ArgLocs[i];
1327 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1329 assert(VA.getValNo() != LastVal &&
1330 "Don't support value assigned to multiple locs yet");
1331 LastVal = VA.getValNo();
1333 if (VA.isRegLoc()) {
1334 MVT::ValueType RegVT = VA.getLocVT();
1335 TargetRegisterClass *RC;
1336 if (RegVT == MVT::i32)
1337 RC = X86::GR32RegisterClass;
1338 else if (RegVT == MVT::i64)
1339 RC = X86::GR64RegisterClass;
1340 else if (RegVT == MVT::f32)
1341 RC = X86::FR32RegisterClass;
1342 else if (RegVT == MVT::f64)
1343 RC = X86::FR64RegisterClass;
1345 assert(MVT::isVector(RegVT));
1346 if (MVT::getSizeInBits(RegVT) == 64) {
1347 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1350 RC = X86::VR128RegisterClass;
1353 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1354 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1356 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1357 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1359 if (VA.getLocInfo() == CCValAssign::SExt)
1360 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1361 DAG.getValueType(VA.getValVT()));
1362 else if (VA.getLocInfo() == CCValAssign::ZExt)
1363 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1364 DAG.getValueType(VA.getValVT()));
1366 if (VA.getLocInfo() != CCValAssign::Full)
1367 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1369 // Handle MMX values passed in GPRs.
1370 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1371 MVT::getSizeInBits(RegVT) == 64)
1372 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1374 ArgValues.push_back(ArgValue);
1376 assert(VA.isMemLoc());
1377 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
1381 unsigned StackSize = CCInfo.getNextStackOffset();
1383 // If the function takes variable number of arguments, make a frame index for
1384 // the start of the first vararg value... for expansion of llvm.va_start.
1386 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1387 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1389 // For X86-64, if there are vararg parameters that are passed via
1390 // registers, then we must store them to their spots on the stack so they
1391 // may be loaded by deferencing the result of va_next.
1392 VarArgsGPOffset = NumIntRegs * 8;
1393 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1394 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1395 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1397 // Store the integer parameter registers.
1398 SmallVector<SDOperand, 8> MemOps;
1399 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1400 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1401 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1402 for (; NumIntRegs != 6; ++NumIntRegs) {
1403 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1404 X86::GR64RegisterClass);
1405 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1406 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1407 MemOps.push_back(Store);
1408 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1409 DAG.getConstant(8, getPointerTy()));
1412 // Now store the XMM (fp + vector) parameter registers.
1413 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1414 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1415 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1416 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1417 X86::VR128RegisterClass);
1418 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1419 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1420 MemOps.push_back(Store);
1421 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1422 DAG.getConstant(16, getPointerTy()));
1424 if (!MemOps.empty())
1425 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1426 &MemOps[0], MemOps.size());
1429 ArgValues.push_back(Root);
1431 BytesToPopOnReturn = 0; // Callee pops nothing.
1432 BytesCallerReserves = StackSize;
1434 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1435 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1437 // Return the new list of results.
1438 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1439 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1443 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1445 SDOperand Chain = Op.getOperand(0);
1446 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1447 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1448 SDOperand Callee = Op.getOperand(4);
1450 // Analyze operands of the call, assigning locations to each operand.
1451 SmallVector<CCValAssign, 16> ArgLocs;
1452 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1453 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1455 // Get a count of how many bytes are to be pushed on the stack.
1456 unsigned NumBytes = CCInfo.getNextStackOffset();
1457 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1459 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1460 SmallVector<SDOperand, 8> MemOpChains;
1464 // Walk the register/memloc assignments, inserting copies/loads.
1465 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1466 CCValAssign &VA = ArgLocs[i];
1467 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1469 // Promote the value if needed.
1470 switch (VA.getLocInfo()) {
1471 default: assert(0 && "Unknown loc info!");
1472 case CCValAssign::Full: break;
1473 case CCValAssign::SExt:
1474 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1476 case CCValAssign::ZExt:
1477 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1479 case CCValAssign::AExt:
1480 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1484 if (VA.isRegLoc()) {
1485 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1487 assert(VA.isMemLoc());
1488 if (StackPtr.Val == 0)
1489 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1491 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1496 if (!MemOpChains.empty())
1497 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1498 &MemOpChains[0], MemOpChains.size());
1500 // Build a sequence of copy-to-reg nodes chained together with token chain
1501 // and flag operands which copy the outgoing args into registers.
1503 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1504 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1506 InFlag = Chain.getValue(1);
1510 // From AMD64 ABI document:
1511 // For calls that may call functions that use varargs or stdargs
1512 // (prototype-less calls or calls to functions containing ellipsis (...) in
1513 // the declaration) %al is used as hidden argument to specify the number
1514 // of SSE registers used. The contents of %al do not need to match exactly
1515 // the number of registers, but must be an ubound on the number of SSE
1516 // registers used and is in the range 0 - 8 inclusive.
1518 // Count the number of XMM registers allocated.
1519 static const unsigned XMMArgRegs[] = {
1520 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1521 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1523 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1525 Chain = DAG.getCopyToReg(Chain, X86::AL,
1526 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1527 InFlag = Chain.getValue(1);
1530 // If the callee is a GlobalAddress node (quite common, every direct call is)
1531 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1532 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1533 // We should use extra load for direct calls to dllimported functions in
1535 if (getTargetMachine().getCodeModel() != CodeModel::Large
1536 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1537 getTargetMachine(), true))
1538 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1539 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1540 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1541 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1543 // Returns a chain & a flag for retval copy to use.
1544 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1545 SmallVector<SDOperand, 8> Ops;
1546 Ops.push_back(Chain);
1547 Ops.push_back(Callee);
1549 // Add argument registers to the end of the list so that they are known live
1551 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1552 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1553 RegsToPass[i].second.getValueType()));
1556 Ops.push_back(InFlag);
1558 // FIXME: Do not generate X86ISD::TAILCALL for now.
1559 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1560 NodeTys, &Ops[0], Ops.size());
1561 InFlag = Chain.getValue(1);
1563 // Returns a flag for retval copy to use.
1564 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1566 Ops.push_back(Chain);
1567 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1568 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1569 Ops.push_back(InFlag);
1570 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1571 InFlag = Chain.getValue(1);
1573 // Handle result values, copying them out of physregs into vregs that we
1575 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1579 //===----------------------------------------------------------------------===//
1580 // Other Lowering Hooks
1581 //===----------------------------------------------------------------------===//
1584 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1585 MachineFunction &MF = DAG.getMachineFunction();
1586 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1587 int ReturnAddrIndex = FuncInfo->getRAIndex();
1589 if (ReturnAddrIndex == 0) {
1590 // Set up a frame object for the return address.
1591 if (Subtarget->is64Bit())
1592 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1594 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1596 FuncInfo->setRAIndex(ReturnAddrIndex);
1599 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1604 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1605 /// specific condition code. It returns a false if it cannot do a direct
1606 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1608 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1609 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1610 SelectionDAG &DAG) {
1611 X86CC = X86::COND_INVALID;
1613 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1614 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1615 // X > -1 -> X == 0, jump !sign.
1616 RHS = DAG.getConstant(0, RHS.getValueType());
1617 X86CC = X86::COND_NS;
1619 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1620 // X < 0 -> X == 0, jump on sign.
1621 X86CC = X86::COND_S;
1623 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1625 RHS = DAG.getConstant(0, RHS.getValueType());
1626 X86CC = X86::COND_LE;
1631 switch (SetCCOpcode) {
1633 case ISD::SETEQ: X86CC = X86::COND_E; break;
1634 case ISD::SETGT: X86CC = X86::COND_G; break;
1635 case ISD::SETGE: X86CC = X86::COND_GE; break;
1636 case ISD::SETLT: X86CC = X86::COND_L; break;
1637 case ISD::SETLE: X86CC = X86::COND_LE; break;
1638 case ISD::SETNE: X86CC = X86::COND_NE; break;
1639 case ISD::SETULT: X86CC = X86::COND_B; break;
1640 case ISD::SETUGT: X86CC = X86::COND_A; break;
1641 case ISD::SETULE: X86CC = X86::COND_BE; break;
1642 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1645 // On a floating point condition, the flags are set as follows:
1647 // 0 | 0 | 0 | X > Y
1648 // 0 | 0 | 1 | X < Y
1649 // 1 | 0 | 0 | X == Y
1650 // 1 | 1 | 1 | unordered
1652 switch (SetCCOpcode) {
1655 case ISD::SETEQ: X86CC = X86::COND_E; break;
1656 case ISD::SETOLT: Flip = true; // Fallthrough
1658 case ISD::SETGT: X86CC = X86::COND_A; break;
1659 case ISD::SETOLE: Flip = true; // Fallthrough
1661 case ISD::SETGE: X86CC = X86::COND_AE; break;
1662 case ISD::SETUGT: Flip = true; // Fallthrough
1664 case ISD::SETLT: X86CC = X86::COND_B; break;
1665 case ISD::SETUGE: Flip = true; // Fallthrough
1667 case ISD::SETLE: X86CC = X86::COND_BE; break;
1669 case ISD::SETNE: X86CC = X86::COND_NE; break;
1670 case ISD::SETUO: X86CC = X86::COND_P; break;
1671 case ISD::SETO: X86CC = X86::COND_NP; break;
1674 std::swap(LHS, RHS);
1677 return X86CC != X86::COND_INVALID;
1680 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1681 /// code. Current x86 isa includes the following FP cmov instructions:
1682 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1683 static bool hasFPCMov(unsigned X86CC) {
1699 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1700 /// true if Op is undef or if its value falls within the specified range (L, H].
1701 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1702 if (Op.getOpcode() == ISD::UNDEF)
1705 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1706 return (Val >= Low && Val < Hi);
1709 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1710 /// true if Op is undef or if its value equal to the specified value.
1711 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1712 if (Op.getOpcode() == ISD::UNDEF)
1714 return cast<ConstantSDNode>(Op)->getValue() == Val;
1717 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1718 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1719 bool X86::isPSHUFDMask(SDNode *N) {
1720 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1722 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
1725 // Check if the value doesn't reference the second vector.
1726 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1727 SDOperand Arg = N->getOperand(i);
1728 if (Arg.getOpcode() == ISD::UNDEF) continue;
1729 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1730 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
1737 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1738 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1739 bool X86::isPSHUFHWMask(SDNode *N) {
1740 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1742 if (N->getNumOperands() != 8)
1745 // Lower quadword copied in order.
1746 for (unsigned i = 0; i != 4; ++i) {
1747 SDOperand Arg = N->getOperand(i);
1748 if (Arg.getOpcode() == ISD::UNDEF) continue;
1749 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1750 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1754 // Upper quadword shuffled.
1755 for (unsigned i = 4; i != 8; ++i) {
1756 SDOperand Arg = N->getOperand(i);
1757 if (Arg.getOpcode() == ISD::UNDEF) continue;
1758 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1759 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1760 if (Val < 4 || Val > 7)
1767 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1768 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1769 bool X86::isPSHUFLWMask(SDNode *N) {
1770 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1772 if (N->getNumOperands() != 8)
1775 // Upper quadword copied in order.
1776 for (unsigned i = 4; i != 8; ++i)
1777 if (!isUndefOrEqual(N->getOperand(i), i))
1780 // Lower quadword shuffled.
1781 for (unsigned i = 0; i != 4; ++i)
1782 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1788 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1789 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1790 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1791 if (NumElems != 2 && NumElems != 4) return false;
1793 unsigned Half = NumElems / 2;
1794 for (unsigned i = 0; i < Half; ++i)
1795 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1797 for (unsigned i = Half; i < NumElems; ++i)
1798 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1804 bool X86::isSHUFPMask(SDNode *N) {
1805 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1806 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1809 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly
1810 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1811 /// half elements to come from vector 1 (which would equal the dest.) and
1812 /// the upper half to come from vector 2.
1813 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1814 if (NumOps != 2 && NumOps != 4) return false;
1816 unsigned Half = NumOps / 2;
1817 for (unsigned i = 0; i < Half; ++i)
1818 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1820 for (unsigned i = Half; i < NumOps; ++i)
1821 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1826 static bool isCommutedSHUFP(SDNode *N) {
1827 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1828 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1831 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1832 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1833 bool X86::isMOVHLPSMask(SDNode *N) {
1834 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1836 if (N->getNumOperands() != 4)
1839 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1840 return isUndefOrEqual(N->getOperand(0), 6) &&
1841 isUndefOrEqual(N->getOperand(1), 7) &&
1842 isUndefOrEqual(N->getOperand(2), 2) &&
1843 isUndefOrEqual(N->getOperand(3), 3);
1846 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1847 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1849 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1850 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1852 if (N->getNumOperands() != 4)
1855 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1856 return isUndefOrEqual(N->getOperand(0), 2) &&
1857 isUndefOrEqual(N->getOperand(1), 3) &&
1858 isUndefOrEqual(N->getOperand(2), 2) &&
1859 isUndefOrEqual(N->getOperand(3), 3);
1862 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1863 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1864 bool X86::isMOVLPMask(SDNode *N) {
1865 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1867 unsigned NumElems = N->getNumOperands();
1868 if (NumElems != 2 && NumElems != 4)
1871 for (unsigned i = 0; i < NumElems/2; ++i)
1872 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1875 for (unsigned i = NumElems/2; i < NumElems; ++i)
1876 if (!isUndefOrEqual(N->getOperand(i), i))
1882 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1883 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1885 bool X86::isMOVHPMask(SDNode *N) {
1886 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1888 unsigned NumElems = N->getNumOperands();
1889 if (NumElems != 2 && NumElems != 4)
1892 for (unsigned i = 0; i < NumElems/2; ++i)
1893 if (!isUndefOrEqual(N->getOperand(i), i))
1896 for (unsigned i = 0; i < NumElems/2; ++i) {
1897 SDOperand Arg = N->getOperand(i + NumElems/2);
1898 if (!isUndefOrEqual(Arg, i + NumElems))
1905 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1906 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1907 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1908 bool V2IsSplat = false) {
1909 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1912 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1913 SDOperand BitI = Elts[i];
1914 SDOperand BitI1 = Elts[i+1];
1915 if (!isUndefOrEqual(BitI, j))
1918 if (isUndefOrEqual(BitI1, NumElts))
1921 if (!isUndefOrEqual(BitI1, j + NumElts))
1929 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1930 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1931 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1934 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1935 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1936 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1937 bool V2IsSplat = false) {
1938 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1941 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1942 SDOperand BitI = Elts[i];
1943 SDOperand BitI1 = Elts[i+1];
1944 if (!isUndefOrEqual(BitI, j + NumElts/2))
1947 if (isUndefOrEqual(BitI1, NumElts))
1950 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1958 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1959 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1960 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1963 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1964 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1966 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1967 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1969 unsigned NumElems = N->getNumOperands();
1970 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1973 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1974 SDOperand BitI = N->getOperand(i);
1975 SDOperand BitI1 = N->getOperand(i+1);
1977 if (!isUndefOrEqual(BitI, j))
1979 if (!isUndefOrEqual(BitI1, j))
1986 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1987 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1989 bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1990 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1992 unsigned NumElems = N->getNumOperands();
1993 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1996 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1997 SDOperand BitI = N->getOperand(i);
1998 SDOperand BitI1 = N->getOperand(i + 1);
2000 if (!isUndefOrEqual(BitI, j))
2002 if (!isUndefOrEqual(BitI1, j))
2009 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2010 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2011 /// MOVSD, and MOVD, i.e. setting the lowest element.
2012 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2013 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2016 if (!isUndefOrEqual(Elts[0], NumElts))
2019 for (unsigned i = 1; i < NumElts; ++i) {
2020 if (!isUndefOrEqual(Elts[i], i))
2027 bool X86::isMOVLMask(SDNode *N) {
2028 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2029 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2032 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2033 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2034 /// element of vector 2 and the other elements to come from vector 1 in order.
2035 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2036 bool V2IsSplat = false,
2037 bool V2IsUndef = false) {
2038 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2041 if (!isUndefOrEqual(Ops[0], 0))
2044 for (unsigned i = 1; i < NumOps; ++i) {
2045 SDOperand Arg = Ops[i];
2046 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2047 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2048 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2055 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2056 bool V2IsUndef = false) {
2057 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2058 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2059 V2IsSplat, V2IsUndef);
2062 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2063 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2064 bool X86::isMOVSHDUPMask(SDNode *N) {
2065 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2067 if (N->getNumOperands() != 4)
2070 // Expect 1, 1, 3, 3
2071 for (unsigned i = 0; i < 2; ++i) {
2072 SDOperand Arg = N->getOperand(i);
2073 if (Arg.getOpcode() == ISD::UNDEF) continue;
2074 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2075 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2076 if (Val != 1) return false;
2080 for (unsigned i = 2; i < 4; ++i) {
2081 SDOperand Arg = N->getOperand(i);
2082 if (Arg.getOpcode() == ISD::UNDEF) continue;
2083 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2084 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2085 if (Val != 3) return false;
2089 // Don't use movshdup if it can be done with a shufps.
2093 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2094 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2095 bool X86::isMOVSLDUPMask(SDNode *N) {
2096 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2098 if (N->getNumOperands() != 4)
2101 // Expect 0, 0, 2, 2
2102 for (unsigned i = 0; i < 2; ++i) {
2103 SDOperand Arg = N->getOperand(i);
2104 if (Arg.getOpcode() == ISD::UNDEF) continue;
2105 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2106 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2107 if (Val != 0) return false;
2111 for (unsigned i = 2; i < 4; ++i) {
2112 SDOperand Arg = N->getOperand(i);
2113 if (Arg.getOpcode() == ISD::UNDEF) continue;
2114 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2115 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2116 if (Val != 2) return false;
2120 // Don't use movshdup if it can be done with a shufps.
2124 /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2125 /// specifies a identity operation on the LHS or RHS.
2126 static bool isIdentityMask(SDNode *N, bool RHS = false) {
2127 unsigned NumElems = N->getNumOperands();
2128 for (unsigned i = 0; i < NumElems; ++i)
2129 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2134 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2135 /// a splat of a single element.
2136 static bool isSplatMask(SDNode *N) {
2137 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2139 // This is a splat operation if each element of the permute is the same, and
2140 // if the value doesn't reference the second vector.
2141 unsigned NumElems = N->getNumOperands();
2142 SDOperand ElementBase;
2144 for (; i != NumElems; ++i) {
2145 SDOperand Elt = N->getOperand(i);
2146 if (isa<ConstantSDNode>(Elt)) {
2152 if (!ElementBase.Val)
2155 for (; i != NumElems; ++i) {
2156 SDOperand Arg = N->getOperand(i);
2157 if (Arg.getOpcode() == ISD::UNDEF) continue;
2158 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2159 if (Arg != ElementBase) return false;
2162 // Make sure it is a splat of the first vector operand.
2163 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2166 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2167 /// a splat of a single element and it's a 2 or 4 element mask.
2168 bool X86::isSplatMask(SDNode *N) {
2169 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2171 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2172 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2174 return ::isSplatMask(N);
2177 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2178 /// specifies a splat of zero element.
2179 bool X86::isSplatLoMask(SDNode *N) {
2180 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2182 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2183 if (!isUndefOrEqual(N->getOperand(i), 0))
2188 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2189 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2191 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2192 unsigned NumOperands = N->getNumOperands();
2193 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2195 for (unsigned i = 0; i < NumOperands; ++i) {
2197 SDOperand Arg = N->getOperand(NumOperands-i-1);
2198 if (Arg.getOpcode() != ISD::UNDEF)
2199 Val = cast<ConstantSDNode>(Arg)->getValue();
2200 if (Val >= NumOperands) Val -= NumOperands;
2202 if (i != NumOperands - 1)
2209 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2210 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2212 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2214 // 8 nodes, but we only care about the last 4.
2215 for (unsigned i = 7; i >= 4; --i) {
2217 SDOperand Arg = N->getOperand(i);
2218 if (Arg.getOpcode() != ISD::UNDEF)
2219 Val = cast<ConstantSDNode>(Arg)->getValue();
2228 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2229 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2231 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2233 // 8 nodes, but we only care about the first 4.
2234 for (int i = 3; i >= 0; --i) {
2236 SDOperand Arg = N->getOperand(i);
2237 if (Arg.getOpcode() != ISD::UNDEF)
2238 Val = cast<ConstantSDNode>(Arg)->getValue();
2247 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2248 /// specifies a 8 element shuffle that can be broken into a pair of
2249 /// PSHUFHW and PSHUFLW.
2250 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2251 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2253 if (N->getNumOperands() != 8)
2256 // Lower quadword shuffled.
2257 for (unsigned i = 0; i != 4; ++i) {
2258 SDOperand Arg = N->getOperand(i);
2259 if (Arg.getOpcode() == ISD::UNDEF) continue;
2260 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2261 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2266 // Upper quadword shuffled.
2267 for (unsigned i = 4; i != 8; ++i) {
2268 SDOperand Arg = N->getOperand(i);
2269 if (Arg.getOpcode() == ISD::UNDEF) continue;
2270 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2271 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2272 if (Val < 4 || Val > 7)
2279 /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2280 /// values in ther permute mask.
2281 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2282 SDOperand &V2, SDOperand &Mask,
2283 SelectionDAG &DAG) {
2284 MVT::ValueType VT = Op.getValueType();
2285 MVT::ValueType MaskVT = Mask.getValueType();
2286 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2287 unsigned NumElems = Mask.getNumOperands();
2288 SmallVector<SDOperand, 8> MaskVec;
2290 for (unsigned i = 0; i != NumElems; ++i) {
2291 SDOperand Arg = Mask.getOperand(i);
2292 if (Arg.getOpcode() == ISD::UNDEF) {
2293 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2296 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2297 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2299 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2301 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2305 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2306 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2309 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2310 /// match movhlps. The lower half elements should come from upper half of
2311 /// V1 (and in order), and the upper half elements should come from the upper
2312 /// half of V2 (and in order).
2313 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2314 unsigned NumElems = Mask->getNumOperands();
2317 for (unsigned i = 0, e = 2; i != e; ++i)
2318 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2320 for (unsigned i = 2; i != 4; ++i)
2321 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2326 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2327 /// is promoted to a vector.
2328 static inline bool isScalarLoadToVector(SDNode *N) {
2329 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2330 N = N->getOperand(0).Val;
2331 return ISD::isNON_EXTLoad(N);
2336 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2337 /// match movlp{s|d}. The lower half elements should come from lower half of
2338 /// V1 (and in order), and the upper half elements should come from the upper
2339 /// half of V2 (and in order). And since V1 will become the source of the
2340 /// MOVLP, it must be either a vector load or a scalar load to vector.
2341 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2342 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2344 // Is V2 is a vector load, don't do this transformation. We will try to use
2345 // load folding shufps op.
2346 if (ISD::isNON_EXTLoad(V2))
2349 unsigned NumElems = Mask->getNumOperands();
2350 if (NumElems != 2 && NumElems != 4)
2352 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2353 if (!isUndefOrEqual(Mask->getOperand(i), i))
2355 for (unsigned i = NumElems/2; i != NumElems; ++i)
2356 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2361 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2363 static bool isSplatVector(SDNode *N) {
2364 if (N->getOpcode() != ISD::BUILD_VECTOR)
2367 SDOperand SplatValue = N->getOperand(0);
2368 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2369 if (N->getOperand(i) != SplatValue)
2374 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2376 static bool isUndefShuffle(SDNode *N) {
2377 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2380 SDOperand V1 = N->getOperand(0);
2381 SDOperand V2 = N->getOperand(1);
2382 SDOperand Mask = N->getOperand(2);
2383 unsigned NumElems = Mask.getNumOperands();
2384 for (unsigned i = 0; i != NumElems; ++i) {
2385 SDOperand Arg = Mask.getOperand(i);
2386 if (Arg.getOpcode() != ISD::UNDEF) {
2387 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2388 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2390 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2397 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2399 static inline bool isZeroNode(SDOperand Elt) {
2400 return ((isa<ConstantSDNode>(Elt) &&
2401 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2402 (isa<ConstantFPSDNode>(Elt) &&
2403 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2406 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2407 /// to an zero vector.
2408 static bool isZeroShuffle(SDNode *N) {
2409 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2412 SDOperand V1 = N->getOperand(0);
2413 SDOperand V2 = N->getOperand(1);
2414 SDOperand Mask = N->getOperand(2);
2415 unsigned NumElems = Mask.getNumOperands();
2416 for (unsigned i = 0; i != NumElems; ++i) {
2417 SDOperand Arg = Mask.getOperand(i);
2418 if (Arg.getOpcode() != ISD::UNDEF) {
2419 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2420 if (Idx < NumElems) {
2421 unsigned Opc = V1.Val->getOpcode();
2422 if (Opc == ISD::UNDEF)
2424 if (Opc != ISD::BUILD_VECTOR ||
2425 !isZeroNode(V1.Val->getOperand(Idx)))
2427 } else if (Idx >= NumElems) {
2428 unsigned Opc = V2.Val->getOpcode();
2429 if (Opc == ISD::UNDEF)
2431 if (Opc != ISD::BUILD_VECTOR ||
2432 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2440 /// getZeroVector - Returns a vector of specified type with all zero elements.
2442 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2443 assert(MVT::isVector(VT) && "Expected a vector type");
2444 unsigned NumElems = MVT::getVectorNumElements(VT);
2445 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2446 bool isFP = MVT::isFloatingPoint(EVT);
2447 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2448 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2449 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2452 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2453 /// that point to V2 points to its first element.
2454 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2455 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2457 bool Changed = false;
2458 SmallVector<SDOperand, 8> MaskVec;
2459 unsigned NumElems = Mask.getNumOperands();
2460 for (unsigned i = 0; i != NumElems; ++i) {
2461 SDOperand Arg = Mask.getOperand(i);
2462 if (Arg.getOpcode() != ISD::UNDEF) {
2463 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2464 if (Val > NumElems) {
2465 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2469 MaskVec.push_back(Arg);
2473 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2474 &MaskVec[0], MaskVec.size());
2478 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2479 /// operation of specified width.
2480 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2481 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2482 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2484 SmallVector<SDOperand, 8> MaskVec;
2485 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2486 for (unsigned i = 1; i != NumElems; ++i)
2487 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2488 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2491 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2492 /// of specified width.
2493 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2494 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2495 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2496 SmallVector<SDOperand, 8> MaskVec;
2497 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2498 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2499 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2501 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2504 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2505 /// of specified width.
2506 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2507 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2508 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2509 unsigned Half = NumElems/2;
2510 SmallVector<SDOperand, 8> MaskVec;
2511 for (unsigned i = 0; i != Half; ++i) {
2512 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2513 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2515 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2518 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2520 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2521 SDOperand V1 = Op.getOperand(0);
2522 SDOperand Mask = Op.getOperand(2);
2523 MVT::ValueType VT = Op.getValueType();
2524 unsigned NumElems = Mask.getNumOperands();
2525 Mask = getUnpacklMask(NumElems, DAG);
2526 while (NumElems != 4) {
2527 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2530 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2532 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2533 Mask = getZeroVector(MaskVT, DAG);
2534 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2535 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2536 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2539 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2540 /// vector of zero or undef vector.
2541 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2542 unsigned NumElems, unsigned Idx,
2543 bool isZero, SelectionDAG &DAG) {
2544 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2545 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2546 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2547 SDOperand Zero = DAG.getConstant(0, EVT);
2548 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2549 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2550 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2551 &MaskVec[0], MaskVec.size());
2552 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2555 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2557 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2558 unsigned NumNonZero, unsigned NumZero,
2559 SelectionDAG &DAG, TargetLowering &TLI) {
2565 for (unsigned i = 0; i < 16; ++i) {
2566 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2567 if (ThisIsNonZero && First) {
2569 V = getZeroVector(MVT::v8i16, DAG);
2571 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2576 SDOperand ThisElt(0, 0), LastElt(0, 0);
2577 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2578 if (LastIsNonZero) {
2579 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2581 if (ThisIsNonZero) {
2582 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2583 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2584 ThisElt, DAG.getConstant(8, MVT::i8));
2586 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2591 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2592 DAG.getConstant(i/2, TLI.getPointerTy()));
2596 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2599 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2601 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2602 unsigned NumNonZero, unsigned NumZero,
2603 SelectionDAG &DAG, TargetLowering &TLI) {
2609 for (unsigned i = 0; i < 8; ++i) {
2610 bool isNonZero = (NonZeros & (1 << i)) != 0;
2614 V = getZeroVector(MVT::v8i16, DAG);
2616 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2619 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2620 DAG.getConstant(i, TLI.getPointerTy()));
2628 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2629 // All zero's are handled with pxor.
2630 if (ISD::isBuildVectorAllZeros(Op.Val))
2633 // All one's are handled with pcmpeqd.
2634 if (ISD::isBuildVectorAllOnes(Op.Val))
2637 MVT::ValueType VT = Op.getValueType();
2638 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2639 unsigned EVTBits = MVT::getSizeInBits(EVT);
2641 unsigned NumElems = Op.getNumOperands();
2642 unsigned NumZero = 0;
2643 unsigned NumNonZero = 0;
2644 unsigned NonZeros = 0;
2645 unsigned NumNonZeroImms = 0;
2646 std::set<SDOperand> Values;
2647 for (unsigned i = 0; i < NumElems; ++i) {
2648 SDOperand Elt = Op.getOperand(i);
2649 if (Elt.getOpcode() != ISD::UNDEF) {
2651 if (isZeroNode(Elt))
2654 NonZeros |= (1 << i);
2656 if (Elt.getOpcode() == ISD::Constant ||
2657 Elt.getOpcode() == ISD::ConstantFP)
2663 if (NumNonZero == 0) {
2665 // All undef vector. Return an UNDEF.
2666 return DAG.getNode(ISD::UNDEF, VT);
2668 // A mix of zero and undef. Return a zero vector.
2669 return getZeroVector(VT, DAG);
2672 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2673 if (Values.size() == 1)
2676 // Special case for single non-zero element.
2677 if (NumNonZero == 1) {
2678 unsigned Idx = CountTrailingZeros_32(NonZeros);
2679 SDOperand Item = Op.getOperand(Idx);
2680 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2682 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2683 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2686 if (EVTBits == 32) {
2687 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2688 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2690 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2691 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2692 SmallVector<SDOperand, 8> MaskVec;
2693 for (unsigned i = 0; i < NumElems; i++)
2694 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2695 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2696 &MaskVec[0], MaskVec.size());
2697 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2698 DAG.getNode(ISD::UNDEF, VT), Mask);
2702 // A vector full of immediates; various special cases are already
2703 // handled, so this is best done with a single constant-pool load.
2704 if (NumNonZero == NumNonZeroImms)
2707 // Let legalizer expand 2-wide build_vectors.
2711 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2712 if (EVTBits == 8 && NumElems == 16) {
2713 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2715 if (V.Val) return V;
2718 if (EVTBits == 16 && NumElems == 8) {
2719 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2721 if (V.Val) return V;
2724 // If element VT is == 32 bits, turn it into a number of shuffles.
2725 SmallVector<SDOperand, 8> V;
2727 if (NumElems == 4 && NumZero > 0) {
2728 for (unsigned i = 0; i < 4; ++i) {
2729 bool isZero = !(NonZeros & (1 << i));
2731 V[i] = getZeroVector(VT, DAG);
2733 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2736 for (unsigned i = 0; i < 2; ++i) {
2737 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2740 V[i] = V[i*2]; // Must be a zero vector.
2743 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2744 getMOVLMask(NumElems, DAG));
2747 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2748 getMOVLMask(NumElems, DAG));
2751 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2752 getUnpacklMask(NumElems, DAG));
2757 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2758 // clears the upper bits.
2759 // FIXME: we can do the same for v4f32 case when we know both parts of
2760 // the lower half come from scalar_to_vector (loadf32). We should do
2761 // that in post legalizer dag combiner with target specific hooks.
2762 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2764 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2765 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2766 SmallVector<SDOperand, 8> MaskVec;
2767 bool Reverse = (NonZeros & 0x3) == 2;
2768 for (unsigned i = 0; i < 2; ++i)
2770 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2772 MaskVec.push_back(DAG.getConstant(i, EVT));
2773 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2774 for (unsigned i = 0; i < 2; ++i)
2776 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2778 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2779 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2780 &MaskVec[0], MaskVec.size());
2781 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2784 if (Values.size() > 2) {
2785 // Expand into a number of unpckl*.
2787 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2788 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2789 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2790 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2791 for (unsigned i = 0; i < NumElems; ++i)
2792 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2794 while (NumElems != 0) {
2795 for (unsigned i = 0; i < NumElems; ++i)
2796 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2807 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2808 SDOperand V1 = Op.getOperand(0);
2809 SDOperand V2 = Op.getOperand(1);
2810 SDOperand PermMask = Op.getOperand(2);
2811 MVT::ValueType VT = Op.getValueType();
2812 unsigned NumElems = PermMask.getNumOperands();
2813 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2814 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2815 bool V1IsSplat = false;
2816 bool V2IsSplat = false;
2818 if (isUndefShuffle(Op.Val))
2819 return DAG.getNode(ISD::UNDEF, VT);
2821 if (isZeroShuffle(Op.Val))
2822 return getZeroVector(VT, DAG);
2824 if (isIdentityMask(PermMask.Val))
2826 else if (isIdentityMask(PermMask.Val, true))
2829 if (isSplatMask(PermMask.Val)) {
2830 if (NumElems <= 4) return Op;
2831 // Promote it to a v4i32 splat.
2832 return PromoteSplat(Op, DAG);
2835 if (X86::isMOVLMask(PermMask.Val))
2836 return (V1IsUndef) ? V2 : Op;
2838 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2839 X86::isMOVSLDUPMask(PermMask.Val) ||
2840 X86::isMOVHLPSMask(PermMask.Val) ||
2841 X86::isMOVHPMask(PermMask.Val) ||
2842 X86::isMOVLPMask(PermMask.Val))
2845 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2846 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2847 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2849 bool Commuted = false;
2850 V1IsSplat = isSplatVector(V1.Val);
2851 V2IsSplat = isSplatVector(V2.Val);
2852 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2853 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2854 std::swap(V1IsSplat, V2IsSplat);
2855 std::swap(V1IsUndef, V2IsUndef);
2859 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2860 if (V2IsUndef) return V1;
2861 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2863 // V2 is a splat, so the mask may be malformed. That is, it may point
2864 // to any V2 element. The instruction selectior won't like this. Get
2865 // a corrected mask and commute to form a proper MOVS{S|D}.
2866 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2867 if (NewMask.Val != PermMask.Val)
2868 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2873 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2874 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2875 X86::isUNPCKLMask(PermMask.Val) ||
2876 X86::isUNPCKHMask(PermMask.Val))
2880 // Normalize mask so all entries that point to V2 points to its first
2881 // element then try to match unpck{h|l} again. If match, return a
2882 // new vector_shuffle with the corrected mask.
2883 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2884 if (NewMask.Val != PermMask.Val) {
2885 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2886 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2887 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2888 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2889 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2890 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2895 // Normalize the node to match x86 shuffle ops if needed
2896 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2897 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2900 // Commute is back and try unpck* again.
2901 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2902 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2903 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2904 X86::isUNPCKLMask(PermMask.Val) ||
2905 X86::isUNPCKHMask(PermMask.Val))
2909 // If VT is integer, try PSHUF* first, then SHUFP*.
2910 if (MVT::isInteger(VT)) {
2911 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
2912 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
2913 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
2914 X86::isPSHUFDMask(PermMask.Val)) ||
2915 X86::isPSHUFHWMask(PermMask.Val) ||
2916 X86::isPSHUFLWMask(PermMask.Val)) {
2917 if (V2.getOpcode() != ISD::UNDEF)
2918 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2919 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2923 if (X86::isSHUFPMask(PermMask.Val) &&
2924 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
2927 // Handle v8i16 shuffle high / low shuffle node pair.
2928 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2929 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2930 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2931 SmallVector<SDOperand, 8> MaskVec;
2932 for (unsigned i = 0; i != 4; ++i)
2933 MaskVec.push_back(PermMask.getOperand(i));
2934 for (unsigned i = 4; i != 8; ++i)
2935 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2936 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2937 &MaskVec[0], MaskVec.size());
2938 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2940 for (unsigned i = 0; i != 4; ++i)
2941 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2942 for (unsigned i = 4; i != 8; ++i)
2943 MaskVec.push_back(PermMask.getOperand(i));
2944 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2945 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2948 // Floating point cases in the other order.
2949 if (X86::isSHUFPMask(PermMask.Val))
2951 if (X86::isPSHUFDMask(PermMask.Val) ||
2952 X86::isPSHUFHWMask(PermMask.Val) ||
2953 X86::isPSHUFLWMask(PermMask.Val)) {
2954 if (V2.getOpcode() != ISD::UNDEF)
2955 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2956 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2961 if (NumElems == 4 &&
2962 // Don't do this for MMX.
2963 MVT::getSizeInBits(VT) != 64) {
2964 MVT::ValueType MaskVT = PermMask.getValueType();
2965 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2966 SmallVector<std::pair<int, int>, 8> Locs;
2967 Locs.reserve(NumElems);
2968 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2969 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2972 // If no more than two elements come from either vector. This can be
2973 // implemented with two shuffles. First shuffle gather the elements.
2974 // The second shuffle, which takes the first shuffle as both of its
2975 // vector operands, put the elements into the right order.
2976 for (unsigned i = 0; i != NumElems; ++i) {
2977 SDOperand Elt = PermMask.getOperand(i);
2978 if (Elt.getOpcode() == ISD::UNDEF) {
2979 Locs[i] = std::make_pair(-1, -1);
2981 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2982 if (Val < NumElems) {
2983 Locs[i] = std::make_pair(0, NumLo);
2987 Locs[i] = std::make_pair(1, NumHi);
2988 if (2+NumHi < NumElems)
2989 Mask1[2+NumHi] = Elt;
2994 if (NumLo <= 2 && NumHi <= 2) {
2995 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2996 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2997 &Mask1[0], Mask1.size()));
2998 for (unsigned i = 0; i != NumElems; ++i) {
2999 if (Locs[i].first == -1)
3002 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3003 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3004 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3008 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3009 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3010 &Mask2[0], Mask2.size()));
3013 // Break it into (shuffle shuffle_hi, shuffle_lo).
3015 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3016 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3017 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3018 unsigned MaskIdx = 0;
3020 unsigned HiIdx = NumElems/2;
3021 for (unsigned i = 0; i != NumElems; ++i) {
3022 if (i == NumElems/2) {
3028 SDOperand Elt = PermMask.getOperand(i);
3029 if (Elt.getOpcode() == ISD::UNDEF) {
3030 Locs[i] = std::make_pair(-1, -1);
3031 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3032 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3033 (*MaskPtr)[LoIdx] = Elt;
3036 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3037 (*MaskPtr)[HiIdx] = Elt;
3042 SDOperand LoShuffle =
3043 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3044 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3045 &LoMask[0], LoMask.size()));
3046 SDOperand HiShuffle =
3047 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3048 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3049 &HiMask[0], HiMask.size()));
3050 SmallVector<SDOperand, 8> MaskOps;
3051 for (unsigned i = 0; i != NumElems; ++i) {
3052 if (Locs[i].first == -1) {
3053 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3055 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3056 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3059 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3060 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3061 &MaskOps[0], MaskOps.size()));
3068 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3069 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3072 MVT::ValueType VT = Op.getValueType();
3073 // TODO: handle v16i8.
3074 if (MVT::getSizeInBits(VT) == 16) {
3075 // Transform it so it match pextrw which produces a 32-bit result.
3076 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3077 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3078 Op.getOperand(0), Op.getOperand(1));
3079 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3080 DAG.getValueType(VT));
3081 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3082 } else if (MVT::getSizeInBits(VT) == 32) {
3083 SDOperand Vec = Op.getOperand(0);
3084 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3087 // SHUFPS the element to the lowest double word, then movss.
3088 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3089 SmallVector<SDOperand, 8> IdxVec;
3090 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3091 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3092 IdxVec.push_back(DAG.getNode(ISD::UNDEF, 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()));
3100 } else if (MVT::getSizeInBits(VT) == 64) {
3101 SDOperand Vec = Op.getOperand(0);
3102 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3106 // UNPCKHPD the element to the lowest double word, then movsd.
3107 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3108 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3109 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3110 SmallVector<SDOperand, 8> IdxVec;
3111 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
3112 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3113 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3114 &IdxVec[0], IdxVec.size());
3115 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3116 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3117 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3118 DAG.getConstant(0, getPointerTy()));
3125 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3126 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3127 // as its second argument.
3128 MVT::ValueType VT = Op.getValueType();
3129 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
3130 SDOperand N0 = Op.getOperand(0);
3131 SDOperand N1 = Op.getOperand(1);
3132 SDOperand N2 = Op.getOperand(2);
3133 if (MVT::getSizeInBits(BaseVT) == 16) {
3134 if (N1.getValueType() != MVT::i32)
3135 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3136 if (N2.getValueType() != MVT::i32)
3137 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
3138 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3139 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3140 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3143 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3144 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3145 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3146 SmallVector<SDOperand, 8> MaskVec;
3147 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3148 for (unsigned i = 1; i <= 3; ++i)
3149 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3150 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3151 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3152 &MaskVec[0], MaskVec.size()));
3154 // Use two pinsrw instructions to insert a 32 bit value.
3156 if (MVT::isFloatingPoint(N1.getValueType())) {
3157 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3158 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3159 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3160 DAG.getConstant(0, getPointerTy()));
3162 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3163 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3164 DAG.getConstant(Idx, getPointerTy()));
3165 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3166 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3167 DAG.getConstant(Idx+1, getPointerTy()));
3168 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3176 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3177 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3178 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3181 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3182 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3183 // one of the above mentioned nodes. It has to be wrapped because otherwise
3184 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3185 // be used to form addressing mode. These wrapped nodes will be selected
3188 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3189 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3190 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3192 CP->getAlignment());
3193 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3194 // With PIC, the address is actually $g + Offset.
3195 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3196 !Subtarget->isPICStyleRIPRel()) {
3197 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3198 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3206 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3207 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3208 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3209 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3210 // With PIC, the address is actually $g + Offset.
3211 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3212 !Subtarget->isPICStyleRIPRel()) {
3213 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3214 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3218 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3219 // load the value at address GV, not the value of GV itself. This means that
3220 // the GlobalAddress must be in the base or index register of the address, not
3221 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3222 // The same applies for external symbols during PIC codegen
3223 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3224 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3229 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
3231 LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3232 const MVT::ValueType PtrVT) {
3234 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3235 DAG.getNode(X86ISD::GlobalBaseReg,
3237 InFlag = Chain.getValue(1);
3239 // emit leal symbol@TLSGD(,%ebx,1), %eax
3240 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3241 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3242 GA->getValueType(0),
3244 SDOperand Ops[] = { Chain, TGA, InFlag };
3245 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3246 InFlag = Result.getValue(2);
3247 Chain = Result.getValue(1);
3249 // call ___tls_get_addr. This function receives its argument in
3250 // the register EAX.
3251 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3252 InFlag = Chain.getValue(1);
3254 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3255 SDOperand Ops1[] = { Chain,
3256 DAG.getTargetExternalSymbol("___tls_get_addr",
3258 DAG.getRegister(X86::EAX, PtrVT),
3259 DAG.getRegister(X86::EBX, PtrVT),
3261 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3262 InFlag = Chain.getValue(1);
3264 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3267 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3268 // "local exec" model.
3270 LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3271 const MVT::ValueType PtrVT) {
3272 // Get the Thread Pointer
3273 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3274 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3276 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3277 GA->getValueType(0),
3279 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3281 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3282 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3284 // The address of the thread local variable is the add of the thread
3285 // pointer with the offset of the variable.
3286 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3290 X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3291 // TODO: implement the "local dynamic" model
3292 // TODO: implement the "initial exec"model for pic executables
3293 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3294 "TLS not implemented for non-ELF and 64-bit targets");
3295 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3296 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3297 // otherwise use the "Local Exec"TLS Model
3298 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3299 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3301 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3305 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3306 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3307 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3308 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3309 // With PIC, the address is actually $g + Offset.
3310 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3311 !Subtarget->isPICStyleRIPRel()) {
3312 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3313 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3320 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3321 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3322 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3323 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3324 // With PIC, the address is actually $g + Offset.
3325 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3326 !Subtarget->isPICStyleRIPRel()) {
3327 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3328 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3335 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3336 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3337 "Not an i64 shift!");
3338 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3339 SDOperand ShOpLo = Op.getOperand(0);
3340 SDOperand ShOpHi = Op.getOperand(1);
3341 SDOperand ShAmt = Op.getOperand(2);
3342 SDOperand Tmp1 = isSRA ?
3343 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3344 DAG.getConstant(0, MVT::i32);
3346 SDOperand Tmp2, Tmp3;
3347 if (Op.getOpcode() == ISD::SHL_PARTS) {
3348 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3349 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3351 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3352 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3355 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3356 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3357 DAG.getConstant(32, MVT::i8));
3358 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3359 AndNode, DAG.getConstant(0, MVT::i8));
3362 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3363 unsigned Opc = X86ISD::CMOV;
3364 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3365 SmallVector<SDOperand, 4> Ops;
3366 if (Op.getOpcode() == ISD::SHL_PARTS) {
3367 Ops.push_back(Tmp2);
3368 Ops.push_back(Tmp3);
3370 Ops.push_back(Cond);
3371 Hi = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
3374 Ops.push_back(Tmp3);
3375 Ops.push_back(Tmp1);
3377 Ops.push_back(Cond);
3378 Lo = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
3380 Ops.push_back(Tmp2);
3381 Ops.push_back(Tmp3);
3383 Ops.push_back(Cond);
3384 Lo = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
3387 Ops.push_back(Tmp3);
3388 Ops.push_back(Tmp1);
3390 Ops.push_back(Cond);
3391 Hi = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
3394 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3398 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3401 SDOperand X86TargetLowering::LowerIntegerDivOrRem(SDOperand Op, SelectionDAG &DAG) {
3402 unsigned Opcode = Op.getOpcode();
3403 MVT::ValueType NVT = Op.getValueType();
3404 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
3405 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
3406 unsigned Opc = isSigned ? X86ISD::IDIV : X86ISD::DIV;
3408 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
3409 SDOperand DR = DAG.getNode(Opc, DAG.getVTList(NVT, NVT), Ops, 2);
3414 return SDOperand(DR.Val, 1);
3417 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3418 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3419 Op.getOperand(0).getValueType() >= MVT::i16 &&
3420 "Unknown SINT_TO_FP to lower!");
3423 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3424 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3425 MachineFunction &MF = DAG.getMachineFunction();
3426 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3427 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3428 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3429 StackSlot, NULL, 0);
3431 // These are really Legal; caller falls through into that case.
3432 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
3434 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
3436 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
3437 Subtarget->is64Bit())
3442 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
3443 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
3445 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3447 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
3448 SmallVector<SDOperand, 8> Ops;
3449 Ops.push_back(Chain);
3450 Ops.push_back(StackSlot);
3451 Ops.push_back(DAG.getValueType(SrcVT));
3452 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3453 Tys, &Ops[0], Ops.size());
3456 Chain = Result.getValue(1);
3457 SDOperand InFlag = Result.getValue(2);
3459 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3460 // shouldn't be necessary except that RFP cannot be live across
3461 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3462 MachineFunction &MF = DAG.getMachineFunction();
3463 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3464 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3465 Tys = DAG.getVTList(MVT::Other);
3466 SmallVector<SDOperand, 8> Ops;
3467 Ops.push_back(Chain);
3468 Ops.push_back(Result);
3469 Ops.push_back(StackSlot);
3470 Ops.push_back(DAG.getValueType(Op.getValueType()));
3471 Ops.push_back(InFlag);
3472 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3473 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3479 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3480 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3481 "Unknown FP_TO_SINT to lower!");
3482 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3485 MachineFunction &MF = DAG.getMachineFunction();
3486 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3487 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3488 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3490 // These are really Legal.
3491 if (Op.getValueType() == MVT::i32 &&
3492 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
3494 if (Op.getValueType() == MVT::i32 &&
3495 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
3497 if (Subtarget->is64Bit() &&
3498 Op.getValueType() == MVT::i64 &&
3499 Op.getOperand(0).getValueType() != MVT::f80)
3503 switch (Op.getValueType()) {
3504 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3505 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3506 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3507 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3510 SDOperand Chain = DAG.getEntryNode();
3511 SDOperand Value = Op.getOperand(0);
3512 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
3513 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
3514 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3515 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3516 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
3518 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3520 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3521 Chain = Value.getValue(1);
3522 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3523 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3526 // Build the FP_TO_INT*_IN_MEM
3527 SDOperand Ops[] = { Chain, Value, StackSlot };
3528 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3531 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3534 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3535 MVT::ValueType VT = Op.getValueType();
3536 MVT::ValueType EltVT = VT;
3537 if (MVT::isVector(VT))
3538 EltVT = MVT::getVectorElementType(VT);
3539 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3540 std::vector<Constant*> CV;
3541 if (EltVT == MVT::f64) {
3542 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
3546 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
3552 Constant *C = ConstantVector::get(CV);
3553 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3554 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3556 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3559 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3560 MVT::ValueType VT = Op.getValueType();
3561 MVT::ValueType EltVT = VT;
3562 unsigned EltNum = 1;
3563 if (MVT::isVector(VT)) {
3564 EltVT = MVT::getVectorElementType(VT);
3565 EltNum = MVT::getVectorNumElements(VT);
3567 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3568 std::vector<Constant*> CV;
3569 if (EltVT == MVT::f64) {
3570 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
3574 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
3580 Constant *C = ConstantVector::get(CV);
3581 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3582 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3584 if (MVT::isVector(VT)) {
3585 return DAG.getNode(ISD::BIT_CONVERT, VT,
3586 DAG.getNode(ISD::XOR, MVT::v2i64,
3587 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
3588 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
3590 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3594 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3595 SDOperand Op0 = Op.getOperand(0);
3596 SDOperand Op1 = Op.getOperand(1);
3597 MVT::ValueType VT = Op.getValueType();
3598 MVT::ValueType SrcVT = Op1.getValueType();
3599 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3601 // If second operand is smaller, extend it first.
3602 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3603 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3605 SrcTy = MVT::getTypeForValueType(SrcVT);
3608 // First get the sign bit of second operand.
3609 std::vector<Constant*> CV;
3610 if (SrcVT == MVT::f64) {
3611 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
3612 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
3614 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
3615 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3616 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3617 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3619 Constant *C = ConstantVector::get(CV);
3620 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3621 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
3623 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3625 // Shift sign bit right or left if the two operands have different types.
3626 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3627 // Op0 is MVT::f32, Op1 is MVT::f64.
3628 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3629 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3630 DAG.getConstant(32, MVT::i32));
3631 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3632 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3633 DAG.getConstant(0, getPointerTy()));
3636 // Clear first operand sign bit.
3638 if (VT == MVT::f64) {
3639 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
3640 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
3642 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
3643 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3644 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3645 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3647 C = ConstantVector::get(CV);
3648 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3649 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3651 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3653 // Or the value with the sign bit.
3654 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3657 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3658 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3660 SDOperand Op0 = Op.getOperand(0);
3661 SDOperand Op1 = Op.getOperand(1);
3662 SDOperand CC = Op.getOperand(2);
3663 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3664 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3667 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3669 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
3670 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3671 DAG.getConstant(X86CC, MVT::i8), Cond);
3674 assert(isFP && "Illegal integer SetCC!");
3676 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
3677 switch (SetCCOpcode) {
3678 default: assert(false && "Illegal floating point SetCC!");
3679 case ISD::SETOEQ: { // !PF & ZF
3680 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3681 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
3682 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3683 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
3684 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3686 case ISD::SETUNE: { // PF | !ZF
3687 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3688 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
3689 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3690 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
3691 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3697 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3698 bool addTest = true;
3699 SDOperand Cond = Op.getOperand(0);
3702 if (Cond.getOpcode() == ISD::SETCC)
3703 Cond = LowerSETCC(Cond, DAG);
3705 if (Cond.getOpcode() == X86ISD::SETCC) {
3706 CC = Cond.getOperand(0);
3708 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3709 // (since flag operand cannot be shared). Use it as the condition setting
3710 // operand in place of the X86ISD::SETCC.
3711 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3712 // to use a test instead of duplicating the X86ISD::CMP (for register
3713 // pressure reason)?
3714 SDOperand Cmp = Cond.getOperand(1);
3715 unsigned Opc = Cmp.getOpcode();
3716 bool IllegalFPCMov =
3717 ! ((X86ScalarSSEf32 && Op.getValueType()==MVT::f32) ||
3718 (X86ScalarSSEf64 && Op.getValueType()==MVT::f64)) &&
3719 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3720 if ((Opc == X86ISD::CMP ||
3721 Opc == X86ISD::COMI ||
3722 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
3723 Cond = DAG.getNode(Opc, MVT::i32, Cmp.getOperand(0), Cmp.getOperand(1));
3729 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3730 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Cond,
3731 DAG.getConstant(0, MVT::i8));
3734 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
3736 SmallVector<SDOperand, 4> Ops;
3737 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3738 // condition is true.
3739 Ops.push_back(Op.getOperand(2));
3740 Ops.push_back(Op.getOperand(1));
3742 Ops.push_back(Cond);
3743 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3746 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3747 bool addTest = true;
3748 SDOperand Chain = Op.getOperand(0);
3749 SDOperand Cond = Op.getOperand(1);
3750 SDOperand Dest = Op.getOperand(2);
3753 if (Cond.getOpcode() == ISD::SETCC)
3754 Cond = LowerSETCC(Cond, DAG);
3756 if (Cond.getOpcode() == X86ISD::SETCC) {
3757 CC = Cond.getOperand(0);
3759 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3760 // (since flag operand cannot be shared). Use it as the condition setting
3761 // operand in place of the X86ISD::SETCC.
3762 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3763 // to use a test instead of duplicating the X86ISD::CMP (for register
3764 // pressure reason)?
3765 SDOperand Cmp = Cond.getOperand(1);
3766 unsigned Opc = Cmp.getOpcode();
3767 if (Opc == X86ISD::CMP ||
3768 Opc == X86ISD::COMI ||
3769 Opc == X86ISD::UCOMI) {
3770 Cond = DAG.getNode(Opc, MVT::i32, Cmp.getOperand(0), Cmp.getOperand(1));
3776 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3777 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
3779 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3780 Chain, Op.getOperand(2), CC, Cond);
3783 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3784 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3786 if (Subtarget->is64Bit())
3787 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3789 switch (CallingConv) {
3791 assert(0 && "Unsupported calling convention");
3792 case CallingConv::Fast:
3793 // TODO: Implement fastcc
3795 case CallingConv::C:
3796 case CallingConv::X86_StdCall:
3797 return LowerCCCCallTo(Op, DAG, CallingConv);
3798 case CallingConv::X86_FastCall:
3799 return LowerFastCCCallTo(Op, DAG, CallingConv);
3804 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3805 // Calls to _alloca is needed to probe the stack when allocating more than 4k
3806 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
3807 // that the guard pages used by the OS virtual memory manager are allocated in
3808 // correct sequence.
3810 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3811 SelectionDAG &DAG) {
3812 assert(Subtarget->isTargetCygMing() &&
3813 "This should be used only on Cygwin/Mingw targets");
3816 SDOperand Chain = Op.getOperand(0);
3817 SDOperand Size = Op.getOperand(1);
3818 // FIXME: Ensure alignment here
3822 MVT::ValueType IntPtr = getPointerTy();
3823 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
3825 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
3826 Flag = Chain.getValue(1);
3828 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3829 SDOperand Ops[] = { Chain,
3830 DAG.getTargetExternalSymbol("_alloca", IntPtr),
3831 DAG.getRegister(X86::EAX, IntPtr),
3833 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
3834 Flag = Chain.getValue(1);
3836 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
3838 std::vector<MVT::ValueType> Tys;
3839 Tys.push_back(SPTy);
3840 Tys.push_back(MVT::Other);
3841 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
3842 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
3846 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3847 MachineFunction &MF = DAG.getMachineFunction();
3848 const Function* Fn = MF.getFunction();
3849 if (Fn->hasExternalLinkage() &&
3850 Subtarget->isTargetCygMing() &&
3851 Fn->getName() == "main")
3852 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
3854 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3855 if (Subtarget->is64Bit())
3856 return LowerX86_64CCCArguments(Op, DAG);
3860 assert(0 && "Unsupported calling convention");
3861 case CallingConv::Fast:
3862 // TODO: implement fastcc.
3865 case CallingConv::C:
3866 return LowerCCCArguments(Op, DAG);
3867 case CallingConv::X86_StdCall:
3868 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
3869 return LowerCCCArguments(Op, DAG, true);
3870 case CallingConv::X86_FastCall:
3871 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
3872 return LowerFastCCArguments(Op, DAG);
3876 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3877 SDOperand InFlag(0, 0);
3878 SDOperand Chain = Op.getOperand(0);
3880 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3881 if (Align == 0) Align = 1;
3883 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3884 // If not DWORD aligned or size is more than the threshold, call memset.
3885 // The libc version is likely to be faster for these cases. It can use the
3886 // address value and run time information about the CPU.
3887 if ((Align & 3) != 0 ||
3888 (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) {
3889 MVT::ValueType IntPtr = getPointerTy();
3890 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3891 TargetLowering::ArgListTy Args;
3892 TargetLowering::ArgListEntry Entry;
3893 Entry.Node = Op.getOperand(1);
3894 Entry.Ty = IntPtrTy;
3895 Args.push_back(Entry);
3896 // Extend the unsigned i8 argument to be an int value for the call.
3897 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3898 Entry.Ty = IntPtrTy;
3899 Args.push_back(Entry);
3900 Entry.Node = Op.getOperand(3);
3901 Args.push_back(Entry);
3902 std::pair<SDOperand,SDOperand> CallResult =
3903 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3904 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3905 return CallResult.second;
3910 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3911 unsigned BytesLeft = 0;
3912 bool TwoRepStos = false;
3915 uint64_t Val = ValC->getValue() & 255;
3917 // If the value is a constant, then we can potentially use larger sets.
3918 switch (Align & 3) {
3919 case 2: // WORD aligned
3922 Val = (Val << 8) | Val;
3924 case 0: // DWORD aligned
3927 Val = (Val << 8) | Val;
3928 Val = (Val << 16) | Val;
3929 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3932 Val = (Val << 32) | Val;
3935 default: // Byte aligned
3938 Count = Op.getOperand(3);
3942 if (AVT > MVT::i8) {
3944 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3945 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3946 BytesLeft = I->getValue() % UBytes;
3948 assert(AVT >= MVT::i32 &&
3949 "Do not use rep;stos if not at least DWORD aligned");
3950 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3951 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3956 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3958 InFlag = Chain.getValue(1);
3961 Count = Op.getOperand(3);
3962 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3963 InFlag = Chain.getValue(1);
3966 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3968 InFlag = Chain.getValue(1);
3969 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3970 Op.getOperand(1), InFlag);
3971 InFlag = Chain.getValue(1);
3973 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3974 SmallVector<SDOperand, 8> Ops;
3975 Ops.push_back(Chain);
3976 Ops.push_back(DAG.getValueType(AVT));
3977 Ops.push_back(InFlag);
3978 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3981 InFlag = Chain.getValue(1);
3982 Count = Op.getOperand(3);
3983 MVT::ValueType CVT = Count.getValueType();
3984 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3985 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3986 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3988 InFlag = Chain.getValue(1);
3989 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3991 Ops.push_back(Chain);
3992 Ops.push_back(DAG.getValueType(MVT::i8));
3993 Ops.push_back(InFlag);
3994 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3995 } else if (BytesLeft) {
3996 // Issue stores for the last 1 - 7 bytes.
3998 unsigned Val = ValC->getValue() & 255;
3999 unsigned Offset = I->getValue() - BytesLeft;
4000 SDOperand DstAddr = Op.getOperand(1);
4001 MVT::ValueType AddrVT = DstAddr.getValueType();
4002 if (BytesLeft >= 4) {
4003 Val = (Val << 8) | Val;
4004 Val = (Val << 16) | Val;
4005 Value = DAG.getConstant(Val, MVT::i32);
4006 Chain = DAG.getStore(Chain, Value,
4007 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4008 DAG.getConstant(Offset, AddrVT)),
4013 if (BytesLeft >= 2) {
4014 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4015 Chain = DAG.getStore(Chain, Value,
4016 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4017 DAG.getConstant(Offset, AddrVT)),
4022 if (BytesLeft == 1) {
4023 Value = DAG.getConstant(Val, MVT::i8);
4024 Chain = DAG.getStore(Chain, Value,
4025 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4026 DAG.getConstant(Offset, AddrVT)),
4034 SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4035 SDOperand ChainOp = Op.getOperand(0);
4036 SDOperand DestOp = Op.getOperand(1);
4037 SDOperand SourceOp = Op.getOperand(2);
4038 SDOperand CountOp = Op.getOperand(3);
4039 SDOperand AlignOp = Op.getOperand(4);
4040 unsigned Align = (unsigned)cast<ConstantSDNode>(AlignOp)->getValue();
4041 if (Align == 0) Align = 1;
4043 // The libc version is likely to be faster for the following cases. It can
4044 // use the address value and run time information about the CPU.
4045 // With glibc 2.6.1 on a core 2, coping an array of 100M longs was 30% faster
4047 // If not DWORD aligned, call memcpy.
4048 if ((Align & 3) != 0)
4049 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4051 // If size is unknown, call memcpy.
4052 ConstantSDNode *I = dyn_cast<ConstantSDNode>(CountOp);
4054 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4056 // If size is more than the threshold, call memcpy.
4057 unsigned Size = I->getValue();
4058 if (Size > Subtarget->getMinRepStrSizeThreshold())
4059 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4061 return LowerMEMCPYInline(ChainOp, DestOp, SourceOp, Size, Align, DAG);
4064 SDOperand X86TargetLowering::LowerMEMCPYCall(SDOperand Chain,
4068 SelectionDAG &DAG) {
4069 MVT::ValueType IntPtr = getPointerTy();
4070 TargetLowering::ArgListTy Args;
4071 TargetLowering::ArgListEntry Entry;
4072 Entry.Ty = getTargetData()->getIntPtrType();
4073 Entry.Node = Dest; Args.push_back(Entry);
4074 Entry.Node = Source; Args.push_back(Entry);
4075 Entry.Node = Count; Args.push_back(Entry);
4076 std::pair<SDOperand,SDOperand> CallResult =
4077 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4078 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4079 return CallResult.second;
4082 SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4087 SelectionDAG &DAG) {
4089 unsigned BytesLeft = 0;
4090 switch (Align & 3) {
4091 case 2: // WORD aligned
4094 case 0: // DWORD aligned
4096 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4099 default: // Byte aligned
4104 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4105 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4106 BytesLeft = Size % UBytes;
4108 SDOperand InFlag(0, 0);
4109 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4111 InFlag = Chain.getValue(1);
4112 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4114 InFlag = Chain.getValue(1);
4115 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4117 InFlag = Chain.getValue(1);
4119 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4120 SmallVector<SDOperand, 8> Ops;
4121 Ops.push_back(Chain);
4122 Ops.push_back(DAG.getValueType(AVT));
4123 Ops.push_back(InFlag);
4124 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4127 // Issue loads and stores for the last 1 - 7 bytes.
4128 unsigned Offset = Size - BytesLeft;
4129 SDOperand DstAddr = Dest;
4130 MVT::ValueType DstVT = DstAddr.getValueType();
4131 SDOperand SrcAddr = Source;
4132 MVT::ValueType SrcVT = SrcAddr.getValueType();
4134 if (BytesLeft >= 4) {
4135 Value = DAG.getLoad(MVT::i32, Chain,
4136 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4137 DAG.getConstant(Offset, SrcVT)),
4139 Chain = Value.getValue(1);
4140 Chain = DAG.getStore(Chain, Value,
4141 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4142 DAG.getConstant(Offset, DstVT)),
4147 if (BytesLeft >= 2) {
4148 Value = DAG.getLoad(MVT::i16, Chain,
4149 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4150 DAG.getConstant(Offset, SrcVT)),
4152 Chain = Value.getValue(1);
4153 Chain = DAG.getStore(Chain, Value,
4154 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4155 DAG.getConstant(Offset, DstVT)),
4161 if (BytesLeft == 1) {
4162 Value = DAG.getLoad(MVT::i8, Chain,
4163 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4164 DAG.getConstant(Offset, SrcVT)),
4166 Chain = Value.getValue(1);
4167 Chain = DAG.getStore(Chain, Value,
4168 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4169 DAG.getConstant(Offset, DstVT)),
4178 X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4179 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4180 SDOperand TheOp = Op.getOperand(0);
4181 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
4182 if (Subtarget->is64Bit()) {
4183 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4184 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4185 MVT::i64, Copy1.getValue(2));
4186 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4187 DAG.getConstant(32, MVT::i8));
4189 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4192 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4193 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
4196 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4197 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4198 MVT::i32, Copy1.getValue(2));
4199 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4200 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4201 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
4204 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4205 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4207 if (!Subtarget->is64Bit()) {
4208 // vastart just stores the address of the VarArgsFrameIndex slot into the
4209 // memory location argument.
4210 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4211 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4216 // gp_offset (0 - 6 * 8)
4217 // fp_offset (48 - 48 + 8 * 16)
4218 // overflow_arg_area (point to parameters coming in memory).
4220 SmallVector<SDOperand, 8> MemOps;
4221 SDOperand FIN = Op.getOperand(1);
4223 SDOperand Store = DAG.getStore(Op.getOperand(0),
4224 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4225 FIN, SV->getValue(), SV->getOffset());
4226 MemOps.push_back(Store);
4229 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4230 DAG.getConstant(4, getPointerTy()));
4231 Store = DAG.getStore(Op.getOperand(0),
4232 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4233 FIN, SV->getValue(), SV->getOffset());
4234 MemOps.push_back(Store);
4236 // Store ptr to overflow_arg_area
4237 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4238 DAG.getConstant(4, getPointerTy()));
4239 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4240 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4242 MemOps.push_back(Store);
4244 // Store ptr to reg_save_area.
4245 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4246 DAG.getConstant(8, getPointerTy()));
4247 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4248 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4250 MemOps.push_back(Store);
4251 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4254 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4255 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4256 SDOperand Chain = Op.getOperand(0);
4257 SDOperand DstPtr = Op.getOperand(1);
4258 SDOperand SrcPtr = Op.getOperand(2);
4259 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4260 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4262 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4263 SrcSV->getValue(), SrcSV->getOffset());
4264 Chain = SrcPtr.getValue(1);
4265 for (unsigned i = 0; i < 3; ++i) {
4266 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4267 SrcSV->getValue(), SrcSV->getOffset());
4268 Chain = Val.getValue(1);
4269 Chain = DAG.getStore(Chain, Val, DstPtr,
4270 DstSV->getValue(), DstSV->getOffset());
4273 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4274 DAG.getConstant(8, getPointerTy()));
4275 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4276 DAG.getConstant(8, getPointerTy()));
4282 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4283 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4285 default: return SDOperand(); // Don't custom lower most intrinsics.
4286 // Comparison intrinsics.
4287 case Intrinsic::x86_sse_comieq_ss:
4288 case Intrinsic::x86_sse_comilt_ss:
4289 case Intrinsic::x86_sse_comile_ss:
4290 case Intrinsic::x86_sse_comigt_ss:
4291 case Intrinsic::x86_sse_comige_ss:
4292 case Intrinsic::x86_sse_comineq_ss:
4293 case Intrinsic::x86_sse_ucomieq_ss:
4294 case Intrinsic::x86_sse_ucomilt_ss:
4295 case Intrinsic::x86_sse_ucomile_ss:
4296 case Intrinsic::x86_sse_ucomigt_ss:
4297 case Intrinsic::x86_sse_ucomige_ss:
4298 case Intrinsic::x86_sse_ucomineq_ss:
4299 case Intrinsic::x86_sse2_comieq_sd:
4300 case Intrinsic::x86_sse2_comilt_sd:
4301 case Intrinsic::x86_sse2_comile_sd:
4302 case Intrinsic::x86_sse2_comigt_sd:
4303 case Intrinsic::x86_sse2_comige_sd:
4304 case Intrinsic::x86_sse2_comineq_sd:
4305 case Intrinsic::x86_sse2_ucomieq_sd:
4306 case Intrinsic::x86_sse2_ucomilt_sd:
4307 case Intrinsic::x86_sse2_ucomile_sd:
4308 case Intrinsic::x86_sse2_ucomigt_sd:
4309 case Intrinsic::x86_sse2_ucomige_sd:
4310 case Intrinsic::x86_sse2_ucomineq_sd: {
4312 ISD::CondCode CC = ISD::SETCC_INVALID;
4315 case Intrinsic::x86_sse_comieq_ss:
4316 case Intrinsic::x86_sse2_comieq_sd:
4320 case Intrinsic::x86_sse_comilt_ss:
4321 case Intrinsic::x86_sse2_comilt_sd:
4325 case Intrinsic::x86_sse_comile_ss:
4326 case Intrinsic::x86_sse2_comile_sd:
4330 case Intrinsic::x86_sse_comigt_ss:
4331 case Intrinsic::x86_sse2_comigt_sd:
4335 case Intrinsic::x86_sse_comige_ss:
4336 case Intrinsic::x86_sse2_comige_sd:
4340 case Intrinsic::x86_sse_comineq_ss:
4341 case Intrinsic::x86_sse2_comineq_sd:
4345 case Intrinsic::x86_sse_ucomieq_ss:
4346 case Intrinsic::x86_sse2_ucomieq_sd:
4347 Opc = X86ISD::UCOMI;
4350 case Intrinsic::x86_sse_ucomilt_ss:
4351 case Intrinsic::x86_sse2_ucomilt_sd:
4352 Opc = X86ISD::UCOMI;
4355 case Intrinsic::x86_sse_ucomile_ss:
4356 case Intrinsic::x86_sse2_ucomile_sd:
4357 Opc = X86ISD::UCOMI;
4360 case Intrinsic::x86_sse_ucomigt_ss:
4361 case Intrinsic::x86_sse2_ucomigt_sd:
4362 Opc = X86ISD::UCOMI;
4365 case Intrinsic::x86_sse_ucomige_ss:
4366 case Intrinsic::x86_sse2_ucomige_sd:
4367 Opc = X86ISD::UCOMI;
4370 case Intrinsic::x86_sse_ucomineq_ss:
4371 case Intrinsic::x86_sse2_ucomineq_sd:
4372 Opc = X86ISD::UCOMI;
4378 SDOperand LHS = Op.getOperand(1);
4379 SDOperand RHS = Op.getOperand(2);
4380 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4382 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4383 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4384 DAG.getConstant(X86CC, MVT::i8), Cond);
4385 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4390 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4391 // Depths > 0 not supported yet!
4392 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4395 // Just load the return address
4396 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4397 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4400 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4401 // Depths > 0 not supported yet!
4402 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4405 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4406 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4407 DAG.getConstant(4, getPointerTy()));
4410 SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4411 SelectionDAG &DAG) {
4412 // Is not yet supported on x86-64
4413 if (Subtarget->is64Bit())
4416 return DAG.getConstant(8, getPointerTy());
4419 SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4421 assert(!Subtarget->is64Bit() &&
4422 "Lowering of eh_return builtin is not supported yet on x86-64");
4424 MachineFunction &MF = DAG.getMachineFunction();
4425 SDOperand Chain = Op.getOperand(0);
4426 SDOperand Offset = Op.getOperand(1);
4427 SDOperand Handler = Op.getOperand(2);
4429 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4432 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4433 DAG.getConstant(-4UL, getPointerTy()));
4434 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4435 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4436 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4437 MF.addLiveOut(X86::ECX);
4439 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4440 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4443 SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4444 SelectionDAG &DAG) {
4445 SDOperand Root = Op.getOperand(0);
4446 SDOperand Trmp = Op.getOperand(1); // trampoline
4447 SDOperand FPtr = Op.getOperand(2); // nested function
4448 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4450 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4452 if (Subtarget->is64Bit()) {
4453 return SDOperand(); // not yet supported
4455 Function *Func = (Function *)
4456 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4457 unsigned CC = Func->getCallingConv();
4462 assert(0 && "Unsupported calling convention");
4463 case CallingConv::C:
4464 case CallingConv::Fast:
4465 case CallingConv::X86_StdCall: {
4466 // Pass 'nest' parameter in ECX.
4467 // Must be kept in sync with X86CallingConv.td
4470 // Check that ECX wasn't needed by an 'inreg' parameter.
4471 const FunctionType *FTy = Func->getFunctionType();
4472 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4474 if (Attrs && !Func->isVarArg()) {
4475 unsigned InRegCount = 0;
4478 for (FunctionType::param_iterator I = FTy->param_begin(),
4479 E = FTy->param_end(); I != E; ++I, ++Idx)
4480 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4481 // FIXME: should only count parameters that are lowered to integers.
4482 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4484 if (InRegCount > 2) {
4485 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4491 case CallingConv::X86_FastCall:
4492 // Pass 'nest' parameter in EAX.
4493 // Must be kept in sync with X86CallingConv.td
4498 const X86InstrInfo *TII =
4499 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4501 SDOperand OutChains[4];
4502 SDOperand Addr, Disp;
4504 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4505 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4507 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
4508 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
4509 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
4510 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4512 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4513 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4514 TrmpSV->getOffset() + 1, false, 1);
4516 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
4517 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4518 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4519 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4521 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4522 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4523 TrmpSV->getOffset() + 6, false, 1);
4526 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4527 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
4531 /// LowerOperation - Provide custom lowering hooks for some operations.
4533 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4534 switch (Op.getOpcode()) {
4535 default: assert(0 && "Should not custom lower this!");
4536 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4537 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4538 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4539 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4540 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4541 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4542 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4543 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
4544 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4545 case ISD::SHL_PARTS:
4546 case ISD::SRA_PARTS:
4547 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4551 case ISD::UREM: return LowerIntegerDivOrRem(Op, DAG);
4552 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4553 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4554 case ISD::FABS: return LowerFABS(Op, DAG);
4555 case ISD::FNEG: return LowerFNEG(Op, DAG);
4556 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
4557 case ISD::SETCC: return LowerSETCC(Op, DAG);
4558 case ISD::SELECT: return LowerSELECT(Op, DAG);
4559 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4560 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4561 case ISD::CALL: return LowerCALL(Op, DAG);
4562 case ISD::RET: return LowerRET(Op, DAG);
4563 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4564 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4565 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4566 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4567 case ISD::VASTART: return LowerVASTART(Op, DAG);
4568 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
4569 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4570 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4571 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4572 case ISD::FRAME_TO_ARGS_OFFSET:
4573 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
4574 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
4575 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
4576 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
4581 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4583 default: return NULL;
4584 case X86ISD::SHLD: return "X86ISD::SHLD";
4585 case X86ISD::SHRD: return "X86ISD::SHRD";
4586 case X86ISD::FAND: return "X86ISD::FAND";
4587 case X86ISD::FOR: return "X86ISD::FOR";
4588 case X86ISD::FXOR: return "X86ISD::FXOR";
4589 case X86ISD::FSRL: return "X86ISD::FSRL";
4590 case X86ISD::FILD: return "X86ISD::FILD";
4591 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4592 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4593 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4594 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4595 case X86ISD::FLD: return "X86ISD::FLD";
4596 case X86ISD::FST: return "X86ISD::FST";
4597 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4598 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4599 case X86ISD::CALL: return "X86ISD::CALL";
4600 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4601 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4602 case X86ISD::CMP: return "X86ISD::CMP";
4603 case X86ISD::COMI: return "X86ISD::COMI";
4604 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4605 case X86ISD::SETCC: return "X86ISD::SETCC";
4606 case X86ISD::CMOV: return "X86ISD::CMOV";
4607 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4608 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4609 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4610 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
4611 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4612 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4613 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4614 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4615 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4616 case X86ISD::FMAX: return "X86ISD::FMAX";
4617 case X86ISD::FMIN: return "X86ISD::FMIN";
4618 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
4619 case X86ISD::FRCP: return "X86ISD::FRCP";
4620 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4621 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
4622 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
4623 case X86ISD::DIV: return "X86ISD::DIV";
4624 case X86ISD::IDIV: return "X86ISD::IDIV";
4628 // isLegalAddressingMode - Return true if the addressing mode represented
4629 // by AM is legal for this target, for a load/store of the specified type.
4630 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4631 const Type *Ty) const {
4632 // X86 supports extremely general addressing modes.
4634 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4635 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4639 // We can only fold this if we don't need an extra load.
4640 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4643 // X86-64 only supports addr of globals in small code model.
4644 if (Subtarget->is64Bit()) {
4645 if (getTargetMachine().getCodeModel() != CodeModel::Small)
4647 // If lower 4G is not available, then we must use rip-relative addressing.
4648 if (AM.BaseOffs || AM.Scale > 1)
4659 // These scales always work.
4664 // These scales are formed with basereg+scalereg. Only accept if there is
4669 default: // Other stuff never works.
4677 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4678 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4679 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4680 /// are assumed to be legal.
4682 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4683 // Only do shuffles on 128-bit vector types for now.
4684 if (MVT::getSizeInBits(VT) == 64) return false;
4685 return (Mask.Val->getNumOperands() <= 4 ||
4686 isIdentityMask(Mask.Val) ||
4687 isIdentityMask(Mask.Val, true) ||
4688 isSplatMask(Mask.Val) ||
4689 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4690 X86::isUNPCKLMask(Mask.Val) ||
4691 X86::isUNPCKHMask(Mask.Val) ||
4692 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4693 X86::isUNPCKH_v_undef_Mask(Mask.Val));
4696 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4698 SelectionDAG &DAG) const {
4699 unsigned NumElts = BVOps.size();
4700 // Only do shuffles on 128-bit vector types for now.
4701 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4702 if (NumElts == 2) return true;
4704 return (isMOVLMask(&BVOps[0], 4) ||
4705 isCommutedMOVL(&BVOps[0], 4, true) ||
4706 isSHUFPMask(&BVOps[0], 4) ||
4707 isCommutedSHUFP(&BVOps[0], 4));
4712 //===----------------------------------------------------------------------===//
4713 // X86 Scheduler Hooks
4714 //===----------------------------------------------------------------------===//
4717 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4718 MachineBasicBlock *BB) {
4719 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4720 switch (MI->getOpcode()) {
4721 default: assert(false && "Unexpected instr type to insert");
4722 case X86::CMOV_FR32:
4723 case X86::CMOV_FR64:
4724 case X86::CMOV_V4F32:
4725 case X86::CMOV_V2F64:
4726 case X86::CMOV_V2I64: {
4727 // To "insert" a SELECT_CC instruction, we actually have to insert the
4728 // diamond control-flow pattern. The incoming instruction knows the
4729 // destination vreg to set, the condition code register to branch on, the
4730 // true/false values to select between, and a branch opcode to use.
4731 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4732 ilist<MachineBasicBlock>::iterator It = BB;
4738 // cmpTY ccX, r1, r2
4740 // fallthrough --> copy0MBB
4741 MachineBasicBlock *thisMBB = BB;
4742 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4743 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4745 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4746 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4747 MachineFunction *F = BB->getParent();
4748 F->getBasicBlockList().insert(It, copy0MBB);
4749 F->getBasicBlockList().insert(It, sinkMBB);
4750 // Update machine-CFG edges by first adding all successors of the current
4751 // block to the new block which will contain the Phi node for the select.
4752 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4753 e = BB->succ_end(); i != e; ++i)
4754 sinkMBB->addSuccessor(*i);
4755 // Next, remove all successors of the current block, and add the true
4756 // and fallthrough blocks as its successors.
4757 while(!BB->succ_empty())
4758 BB->removeSuccessor(BB->succ_begin());
4759 BB->addSuccessor(copy0MBB);
4760 BB->addSuccessor(sinkMBB);
4763 // %FalseValue = ...
4764 // # fallthrough to sinkMBB
4767 // Update machine-CFG edges
4768 BB->addSuccessor(sinkMBB);
4771 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4774 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4775 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4776 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4778 delete MI; // The pseudo instruction is gone now.
4782 case X86::FP32_TO_INT16_IN_MEM:
4783 case X86::FP32_TO_INT32_IN_MEM:
4784 case X86::FP32_TO_INT64_IN_MEM:
4785 case X86::FP64_TO_INT16_IN_MEM:
4786 case X86::FP64_TO_INT32_IN_MEM:
4787 case X86::FP64_TO_INT64_IN_MEM:
4788 case X86::FP80_TO_INT16_IN_MEM:
4789 case X86::FP80_TO_INT32_IN_MEM:
4790 case X86::FP80_TO_INT64_IN_MEM: {
4791 // Change the floating point control register to use "round towards zero"
4792 // mode when truncating to an integer value.
4793 MachineFunction *F = BB->getParent();
4794 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4795 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4797 // Load the old value of the high byte of the control word...
4799 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4800 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4802 // Set the high part to be round to zero...
4803 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4806 // Reload the modified control word now...
4807 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4809 // Restore the memory image of control word to original value
4810 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4813 // Get the X86 opcode to use.
4815 switch (MI->getOpcode()) {
4816 default: assert(0 && "illegal opcode!");
4817 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
4818 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
4819 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
4820 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
4821 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
4822 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
4823 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
4824 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
4825 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
4829 MachineOperand &Op = MI->getOperand(0);
4830 if (Op.isRegister()) {
4831 AM.BaseType = X86AddressMode::RegBase;
4832 AM.Base.Reg = Op.getReg();
4834 AM.BaseType = X86AddressMode::FrameIndexBase;
4835 AM.Base.FrameIndex = Op.getFrameIndex();
4837 Op = MI->getOperand(1);
4838 if (Op.isImmediate())
4839 AM.Scale = Op.getImm();
4840 Op = MI->getOperand(2);
4841 if (Op.isImmediate())
4842 AM.IndexReg = Op.getImm();
4843 Op = MI->getOperand(3);
4844 if (Op.isGlobalAddress()) {
4845 AM.GV = Op.getGlobal();
4847 AM.Disp = Op.getImm();
4849 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4850 .addReg(MI->getOperand(4).getReg());
4852 // Reload the original control word now.
4853 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4855 delete MI; // The pseudo instruction is gone now.
4861 //===----------------------------------------------------------------------===//
4862 // X86 Optimization Hooks
4863 //===----------------------------------------------------------------------===//
4865 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4867 uint64_t &KnownZero,
4869 const SelectionDAG &DAG,
4870 unsigned Depth) const {
4871 unsigned Opc = Op.getOpcode();
4872 assert((Opc >= ISD::BUILTIN_OP_END ||
4873 Opc == ISD::INTRINSIC_WO_CHAIN ||
4874 Opc == ISD::INTRINSIC_W_CHAIN ||
4875 Opc == ISD::INTRINSIC_VOID) &&
4876 "Should use MaskedValueIsZero if you don't know whether Op"
4877 " is a target node!");
4879 KnownZero = KnownOne = 0; // Don't know anything.
4883 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4888 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
4889 /// element of the result of the vector shuffle.
4890 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4891 MVT::ValueType VT = N->getValueType(0);
4892 SDOperand PermMask = N->getOperand(2);
4893 unsigned NumElems = PermMask.getNumOperands();
4894 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4896 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4898 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4899 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4900 SDOperand Idx = PermMask.getOperand(i);
4901 if (Idx.getOpcode() == ISD::UNDEF)
4902 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4903 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4908 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4909 /// node is a GlobalAddress + an offset.
4910 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4911 unsigned Opc = N->getOpcode();
4912 if (Opc == X86ISD::Wrapper) {
4913 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4914 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4917 } else if (Opc == ISD::ADD) {
4918 SDOperand N1 = N->getOperand(0);
4919 SDOperand N2 = N->getOperand(1);
4920 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4921 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4923 Offset += V->getSignExtended();
4926 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4927 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4929 Offset += V->getSignExtended();
4937 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
4939 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4940 MachineFrameInfo *MFI) {
4941 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4944 SDOperand Loc = N->getOperand(1);
4945 SDOperand BaseLoc = Base->getOperand(1);
4946 if (Loc.getOpcode() == ISD::FrameIndex) {
4947 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4949 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
4950 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4951 int FS = MFI->getObjectSize(FI);
4952 int BFS = MFI->getObjectSize(BFI);
4953 if (FS != BFS || FS != Size) return false;
4954 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4956 GlobalValue *GV1 = NULL;
4957 GlobalValue *GV2 = NULL;
4958 int64_t Offset1 = 0;
4959 int64_t Offset2 = 0;
4960 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4961 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4962 if (isGA1 && isGA2 && GV1 == GV2)
4963 return Offset1 == (Offset2 + Dist*Size);
4969 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4970 const X86Subtarget *Subtarget) {
4973 if (isGAPlusOffset(Base, GV, Offset))
4974 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4976 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4977 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
4979 // Fixed objects do not specify alignment, however the offsets are known.
4980 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4981 (MFI->getObjectOffset(BFI) % 16) == 0);
4983 return MFI->getObjectAlignment(BFI) >= 16;
4989 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4990 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4991 /// if the load addresses are consecutive, non-overlapping, and in the right
4993 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4994 const X86Subtarget *Subtarget) {
4995 MachineFunction &MF = DAG.getMachineFunction();
4996 MachineFrameInfo *MFI = MF.getFrameInfo();
4997 MVT::ValueType VT = N->getValueType(0);
4998 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4999 SDOperand PermMask = N->getOperand(2);
5000 int NumElems = (int)PermMask.getNumOperands();
5001 SDNode *Base = NULL;
5002 for (int i = 0; i < NumElems; ++i) {
5003 SDOperand Idx = PermMask.getOperand(i);
5004 if (Idx.getOpcode() == ISD::UNDEF) {
5005 if (!Base) return SDOperand();
5008 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5009 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5013 else if (!isConsecutiveLoad(Arg.Val, Base,
5014 i, MVT::getSizeInBits(EVT)/8,MFI))
5019 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
5020 LoadSDNode *LD = cast<LoadSDNode>(Base);
5022 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5023 LD->getSrcValueOffset(), LD->isVolatile());
5025 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5026 LD->getSrcValueOffset(), LD->isVolatile(),
5027 LD->getAlignment());
5031 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5032 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5033 const X86Subtarget *Subtarget) {
5034 SDOperand Cond = N->getOperand(0);
5036 // If we have SSE[12] support, try to form min/max nodes.
5037 if (Subtarget->hasSSE2() &&
5038 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5039 if (Cond.getOpcode() == ISD::SETCC) {
5040 // Get the LHS/RHS of the select.
5041 SDOperand LHS = N->getOperand(1);
5042 SDOperand RHS = N->getOperand(2);
5043 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5045 unsigned Opcode = 0;
5046 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5049 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5052 if (!UnsafeFPMath) break;
5054 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5056 Opcode = X86ISD::FMIN;
5059 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5062 if (!UnsafeFPMath) break;
5064 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5066 Opcode = X86ISD::FMAX;
5069 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5072 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5075 if (!UnsafeFPMath) break;
5077 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5079 Opcode = X86ISD::FMIN;
5082 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5085 if (!UnsafeFPMath) break;
5087 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5089 Opcode = X86ISD::FMAX;
5095 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5104 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5105 DAGCombinerInfo &DCI) const {
5106 SelectionDAG &DAG = DCI.DAG;
5107 switch (N->getOpcode()) {
5109 case ISD::VECTOR_SHUFFLE:
5110 return PerformShuffleCombine(N, DAG, Subtarget);
5112 return PerformSELECTCombine(N, DAG, Subtarget);
5118 //===----------------------------------------------------------------------===//
5119 // X86 Inline Assembly Support
5120 //===----------------------------------------------------------------------===//
5122 /// getConstraintType - Given a constraint letter, return the type of
5123 /// constraint it is for this target.
5124 X86TargetLowering::ConstraintType
5125 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5126 if (Constraint.size() == 1) {
5127 switch (Constraint[0]) {
5136 return C_RegisterClass;
5141 return TargetLowering::getConstraintType(Constraint);
5144 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5145 /// vector. If it is invalid, don't add anything to Ops.
5146 void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5148 std::vector<SDOperand>&Ops,
5149 SelectionDAG &DAG) {
5150 SDOperand Result(0, 0);
5152 switch (Constraint) {
5155 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
5156 if (C->getValue() <= 31) {
5157 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5163 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
5164 if (C->getValue() <= 255) {
5165 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5171 // Literal immediates are always ok.
5172 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5173 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5177 // If we are in non-pic codegen mode, we allow the address of a global (with
5178 // an optional displacement) to be used with 'i'.
5179 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5182 // Match either (GA) or (GA+C)
5184 Offset = GA->getOffset();
5185 } else if (Op.getOpcode() == ISD::ADD) {
5186 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5187 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5189 Offset = GA->getOffset()+C->getValue();
5191 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5192 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5194 Offset = GA->getOffset()+C->getValue();
5201 // If addressing this global requires a load (e.g. in PIC mode), we can't
5203 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5207 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5213 // Otherwise, not valid for this mode.
5219 Ops.push_back(Result);
5222 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
5225 std::vector<unsigned> X86TargetLowering::
5226 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5227 MVT::ValueType VT) const {
5228 if (Constraint.size() == 1) {
5229 // FIXME: not handling fp-stack yet!
5230 switch (Constraint[0]) { // GCC X86 Constraint Letters
5231 default: break; // Unknown constraint letter
5232 case 'A': // EAX/EDX
5233 if (VT == MVT::i32 || VT == MVT::i64)
5234 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5236 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5239 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5240 else if (VT == MVT::i16)
5241 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5242 else if (VT == MVT::i8)
5243 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
5248 return std::vector<unsigned>();
5251 std::pair<unsigned, const TargetRegisterClass*>
5252 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5253 MVT::ValueType VT) const {
5254 // First, see if this is a constraint that directly corresponds to an LLVM
5256 if (Constraint.size() == 1) {
5257 // GCC Constraint Letters
5258 switch (Constraint[0]) {
5260 case 'r': // GENERAL_REGS
5261 case 'R': // LEGACY_REGS
5262 case 'l': // INDEX_REGS
5263 if (VT == MVT::i64 && Subtarget->is64Bit())
5264 return std::make_pair(0U, X86::GR64RegisterClass);
5266 return std::make_pair(0U, X86::GR32RegisterClass);
5267 else if (VT == MVT::i16)
5268 return std::make_pair(0U, X86::GR16RegisterClass);
5269 else if (VT == MVT::i8)
5270 return std::make_pair(0U, X86::GR8RegisterClass);
5272 case 'y': // MMX_REGS if MMX allowed.
5273 if (!Subtarget->hasMMX()) break;
5274 return std::make_pair(0U, X86::VR64RegisterClass);
5276 case 'Y': // SSE_REGS if SSE2 allowed
5277 if (!Subtarget->hasSSE2()) break;
5279 case 'x': // SSE_REGS if SSE1 allowed
5280 if (!Subtarget->hasSSE1()) break;
5284 // Scalar SSE types.
5287 return std::make_pair(0U, X86::FR32RegisterClass);
5290 return std::make_pair(0U, X86::FR64RegisterClass);
5298 return std::make_pair(0U, X86::VR128RegisterClass);
5304 // Use the default implementation in TargetLowering to convert the register
5305 // constraint into a member of a register class.
5306 std::pair<unsigned, const TargetRegisterClass*> Res;
5307 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5309 // Not found as a standard register?
5310 if (Res.second == 0) {
5311 // GCC calls "st(0)" just plain "st".
5312 if (StringsEqualNoCase("{st}", Constraint)) {
5313 Res.first = X86::ST0;
5314 Res.second = X86::RFP80RegisterClass;
5320 // Otherwise, check to see if this is a register class of the wrong value
5321 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5322 // turn into {ax},{dx}.
5323 if (Res.second->hasType(VT))
5324 return Res; // Correct type already, nothing to do.
5326 // All of the single-register GCC register classes map their values onto
5327 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5328 // really want an 8-bit or 32-bit register, map to the appropriate register
5329 // class and return the appropriate register.
5330 if (Res.second != X86::GR16RegisterClass)
5333 if (VT == MVT::i8) {
5334 unsigned DestReg = 0;
5335 switch (Res.first) {
5337 case X86::AX: DestReg = X86::AL; break;
5338 case X86::DX: DestReg = X86::DL; break;
5339 case X86::CX: DestReg = X86::CL; break;
5340 case X86::BX: DestReg = X86::BL; break;
5343 Res.first = DestReg;
5344 Res.second = Res.second = X86::GR8RegisterClass;
5346 } else if (VT == MVT::i32) {
5347 unsigned DestReg = 0;
5348 switch (Res.first) {
5350 case X86::AX: DestReg = X86::EAX; break;
5351 case X86::DX: DestReg = X86::EDX; break;
5352 case X86::CX: DestReg = X86::ECX; break;
5353 case X86::BX: DestReg = X86::EBX; break;
5354 case X86::SI: DestReg = X86::ESI; break;
5355 case X86::DI: DestReg = X86::EDI; break;
5356 case X86::BP: DestReg = X86::EBP; break;
5357 case X86::SP: DestReg = X86::ESP; break;
5360 Res.first = DestReg;
5361 Res.second = Res.second = X86::GR32RegisterClass;
5363 } else if (VT == MVT::i64) {
5364 unsigned DestReg = 0;
5365 switch (Res.first) {
5367 case X86::AX: DestReg = X86::RAX; break;
5368 case X86::DX: DestReg = X86::RDX; break;
5369 case X86::CX: DestReg = X86::RCX; break;
5370 case X86::BX: DestReg = X86::RBX; break;
5371 case X86::SI: DestReg = X86::RSI; break;
5372 case X86::DI: DestReg = X86::RDI; break;
5373 case X86::BP: DestReg = X86::RBP; break;
5374 case X86::SP: DestReg = X86::RSP; break;
5377 Res.first = DestReg;
5378 Res.second = Res.second = X86::GR64RegisterClass;