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/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/ParameterAttributes.h"
42 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
43 : TargetLowering(TM) {
44 Subtarget = &TM.getSubtarget<X86Subtarget>();
45 X86ScalarSSEf64 = Subtarget->hasSSE2();
46 X86ScalarSSEf32 = Subtarget->hasSSE1();
47 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
50 RegInfo = TM.getRegisterInfo();
52 // Set up the TargetLowering object.
54 // X86 is weird, it always uses i8 for shift amounts and setcc results.
55 setShiftAmountType(MVT::i8);
56 setSetCCResultType(MVT::i8);
57 setSetCCResultContents(ZeroOrOneSetCCResult);
58 setSchedulingPreference(SchedulingForRegPressure);
59 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
60 setStackPointerRegisterToSaveRestore(X86StackPtr);
62 if (Subtarget->isTargetDarwin()) {
63 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
64 setUseUnderscoreSetJmp(false);
65 setUseUnderscoreLongJmp(false);
66 } else if (Subtarget->isTargetMingw()) {
67 // MS runtime is weird: it exports _setjmp, but longjmp!
68 setUseUnderscoreSetJmp(true);
69 setUseUnderscoreLongJmp(false);
71 setUseUnderscoreSetJmp(true);
72 setUseUnderscoreLongJmp(true);
75 // Set up the register classes.
76 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
77 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
78 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
79 if (Subtarget->is64Bit())
80 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
82 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
84 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
86 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
87 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
88 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
90 if (Subtarget->is64Bit()) {
91 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
92 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
95 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
96 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
98 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
101 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
103 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
104 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
105 // SSE has no i16 to fp conversion, only i32
106 if (X86ScalarSSEf32) {
107 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
108 // f32 and f64 cases are Legal, f80 case is not
109 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
111 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
112 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
115 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
116 // are Legal, f80 is custom lowered.
117 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
118 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
120 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
122 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
123 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
125 if (X86ScalarSSEf32) {
126 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
127 // f32 and f64 cases are Legal, f80 case is not
128 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
130 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
131 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
134 // Handle FP_TO_UINT by promoting the destination to a larger signed
136 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
137 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
138 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
140 if (Subtarget->is64Bit()) {
141 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
142 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
144 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
145 // Expand FP_TO_UINT into a select.
146 // FIXME: We would like to use a Custom expander here eventually to do
147 // the optimal thing for SSE vs. the default expansion in the legalizer.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
150 // With SSE3 we can use fisttpll to convert to a signed i64.
151 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
154 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
155 if (!X86ScalarSSEf64) {
156 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
157 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
160 // Scalar integer multiply, multiply-high, divide, and remainder are
161 // lowered to use operations that produce two results, to match the
162 // available instructions. This exposes the two-result form to trivial
163 // CSE, which is able to combine x/y and x%y into a single instruction,
164 // for example. The single-result multiply instructions are introduced
165 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
167 setOperationAction(ISD::MUL , MVT::i8 , Expand);
168 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
169 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
170 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
171 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
172 setOperationAction(ISD::SREM , MVT::i8 , Expand);
173 setOperationAction(ISD::UREM , MVT::i8 , Expand);
174 setOperationAction(ISD::MUL , MVT::i16 , Expand);
175 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
176 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
177 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
178 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
179 setOperationAction(ISD::SREM , MVT::i16 , Expand);
180 setOperationAction(ISD::UREM , MVT::i16 , Expand);
181 setOperationAction(ISD::MUL , MVT::i32 , Expand);
182 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
186 setOperationAction(ISD::SREM , MVT::i32 , Expand);
187 setOperationAction(ISD::UREM , MVT::i32 , Expand);
188 setOperationAction(ISD::MUL , MVT::i64 , Expand);
189 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
190 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
191 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
192 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
193 setOperationAction(ISD::SREM , MVT::i64 , Expand);
194 setOperationAction(ISD::UREM , MVT::i64 , Expand);
196 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
197 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
198 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
199 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
200 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
201 if (Subtarget->is64Bit())
202 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
203 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
206 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
207 setOperationAction(ISD::FREM , MVT::f64 , Expand);
208 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
210 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
211 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
212 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
213 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
214 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
215 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
216 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
217 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
218 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
219 if (Subtarget->is64Bit()) {
220 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
221 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
222 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
225 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
226 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
228 // These should be promoted to a larger select which is supported.
229 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
230 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
231 // X86 wants to expand cmov itself.
232 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
233 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
234 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
235 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
236 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
237 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
238 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
239 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
240 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
241 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
242 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
243 if (Subtarget->is64Bit()) {
244 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
245 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
247 // X86 ret instruction may pop stack.
248 setOperationAction(ISD::RET , MVT::Other, Custom);
249 if (!Subtarget->is64Bit())
250 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
253 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
254 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
255 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
256 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
257 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
258 if (Subtarget->is64Bit()) {
259 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
260 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
261 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
262 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
264 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
265 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
266 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
267 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
268 // X86 wants to expand memset / memcpy itself.
269 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
270 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
272 // Use the default ISD::LOCATION expansion.
273 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
274 // FIXME - use subtarget debug flags
275 if (!Subtarget->isTargetDarwin() &&
276 !Subtarget->isTargetELF() &&
277 !Subtarget->isTargetCygMing())
278 setOperationAction(ISD::LABEL, MVT::Other, Expand);
280 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
281 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
282 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
283 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
284 if (Subtarget->is64Bit()) {
286 setExceptionPointerRegister(X86::RAX);
287 setExceptionSelectorRegister(X86::RDX);
289 setExceptionPointerRegister(X86::EAX);
290 setExceptionSelectorRegister(X86::EDX);
292 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
294 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
296 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
297 setOperationAction(ISD::VASTART , MVT::Other, Custom);
298 setOperationAction(ISD::VAARG , MVT::Other, Expand);
299 setOperationAction(ISD::VAEND , MVT::Other, Expand);
300 if (Subtarget->is64Bit())
301 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
303 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
305 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
306 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
307 if (Subtarget->is64Bit())
308 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
309 if (Subtarget->isTargetCygMing())
310 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
314 if (X86ScalarSSEf64) {
315 // f32 and f64 use SSE.
316 // Set up the FP register classes.
317 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
318 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
320 // Use ANDPD to simulate FABS.
321 setOperationAction(ISD::FABS , MVT::f64, Custom);
322 setOperationAction(ISD::FABS , MVT::f32, Custom);
324 // Use XORP to simulate FNEG.
325 setOperationAction(ISD::FNEG , MVT::f64, Custom);
326 setOperationAction(ISD::FNEG , MVT::f32, Custom);
328 // Use ANDPD and ORPD to simulate FCOPYSIGN.
329 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
330 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
332 // We don't support sin/cos/fmod
333 setOperationAction(ISD::FSIN , MVT::f64, Expand);
334 setOperationAction(ISD::FCOS , MVT::f64, Expand);
335 setOperationAction(ISD::FREM , MVT::f64, Expand);
336 setOperationAction(ISD::FSIN , MVT::f32, Expand);
337 setOperationAction(ISD::FCOS , MVT::f32, Expand);
338 setOperationAction(ISD::FREM , MVT::f32, Expand);
340 // Expand FP immediates into loads from the stack, except for the special
342 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
343 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
344 addLegalFPImmediate(APFloat(+0.0)); // xorpd
345 addLegalFPImmediate(APFloat(+0.0f)); // xorps
347 // Conversions to long double (in X87) go through memory.
348 setConvertAction(MVT::f32, MVT::f80, Expand);
349 setConvertAction(MVT::f64, MVT::f80, Expand);
351 // Conversions from long double (in X87) go through memory.
352 setConvertAction(MVT::f80, MVT::f32, Expand);
353 setConvertAction(MVT::f80, MVT::f64, Expand);
354 } else if (X86ScalarSSEf32) {
355 // Use SSE for f32, x87 for f64.
356 // Set up the FP register classes.
357 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
358 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
360 // Use ANDPS to simulate FABS.
361 setOperationAction(ISD::FABS , MVT::f32, Custom);
363 // Use XORP to simulate FNEG.
364 setOperationAction(ISD::FNEG , MVT::f32, Custom);
366 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
368 // Use ANDPS and ORPS to simulate FCOPYSIGN.
369 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
370 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
372 // We don't support sin/cos/fmod
373 setOperationAction(ISD::FSIN , MVT::f32, Expand);
374 setOperationAction(ISD::FCOS , MVT::f32, Expand);
375 setOperationAction(ISD::FREM , MVT::f32, Expand);
377 // Expand FP immediates into loads from the stack, except for the special
379 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
380 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
381 addLegalFPImmediate(APFloat(+0.0f)); // xorps
382 addLegalFPImmediate(APFloat(+0.0)); // FLD0
383 addLegalFPImmediate(APFloat(+1.0)); // FLD1
384 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
385 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
387 // SSE->x87 conversions go through memory.
388 setConvertAction(MVT::f32, MVT::f64, Expand);
389 setConvertAction(MVT::f32, MVT::f80, Expand);
391 // x87->SSE truncations need to go through memory.
392 setConvertAction(MVT::f80, MVT::f32, Expand);
393 setConvertAction(MVT::f64, MVT::f32, Expand);
394 // And x87->x87 truncations also.
395 setConvertAction(MVT::f80, MVT::f64, Expand);
398 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
399 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
402 // f32 and f64 in x87.
403 // Set up the FP register classes.
404 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
405 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
407 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
408 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
409 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
410 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
412 // Floating truncations need to go through memory.
413 setConvertAction(MVT::f80, MVT::f32, Expand);
414 setConvertAction(MVT::f64, MVT::f32, Expand);
415 setConvertAction(MVT::f80, MVT::f64, Expand);
418 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
419 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
422 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
423 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
424 addLegalFPImmediate(APFloat(+0.0)); // FLD0
425 addLegalFPImmediate(APFloat(+1.0)); // FLD1
426 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
427 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
428 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
429 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
430 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
431 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
434 // Long double always uses X87.
435 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
436 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
437 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
438 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
440 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
441 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
444 // Always use a library call for pow.
445 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
446 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
447 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
449 // First set operation action for all vector types to expand. Then we
450 // will selectively turn on ones that can be effectively codegen'd.
451 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
452 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
453 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
454 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
455 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
456 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
457 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
458 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
459 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
460 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
461 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
464 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
465 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
466 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
467 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
468 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
469 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
470 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
471 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
475 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
476 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
477 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
478 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
479 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
480 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
481 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
482 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
486 if (Subtarget->hasMMX()) {
487 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
488 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
489 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
490 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
492 // FIXME: add MMX packed arithmetics
494 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
495 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
496 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
497 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
499 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
500 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
501 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
502 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
504 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
505 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
507 setOperationAction(ISD::AND, MVT::v8i8, Promote);
508 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
509 setOperationAction(ISD::AND, MVT::v4i16, Promote);
510 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
511 setOperationAction(ISD::AND, MVT::v2i32, Promote);
512 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
513 setOperationAction(ISD::AND, MVT::v1i64, Legal);
515 setOperationAction(ISD::OR, MVT::v8i8, Promote);
516 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
517 setOperationAction(ISD::OR, MVT::v4i16, Promote);
518 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
519 setOperationAction(ISD::OR, MVT::v2i32, Promote);
520 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
521 setOperationAction(ISD::OR, MVT::v1i64, Legal);
523 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
524 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
525 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
526 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
527 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
528 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
529 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
531 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
532 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
533 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
534 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
535 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
536 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
537 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
539 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
540 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
541 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
542 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
544 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
545 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
546 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
547 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
549 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
550 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
551 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
552 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
555 if (Subtarget->hasSSE1()) {
556 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
558 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
559 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
560 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
561 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
562 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
563 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
564 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
565 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
566 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
567 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
568 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
571 if (Subtarget->hasSSE2()) {
572 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
573 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
574 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
575 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
576 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
578 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
579 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
580 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
581 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
582 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
583 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
584 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
585 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
586 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
587 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
588 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
589 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
590 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
591 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
592 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
594 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
595 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
596 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
597 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
598 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
599 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
601 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
602 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
603 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
604 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
605 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
607 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
608 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
609 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
610 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
611 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
612 if (Subtarget->is64Bit())
613 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
615 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
616 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
617 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
618 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
619 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
620 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
621 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
622 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
623 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
624 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
625 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
626 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
629 // Custom lower v2i64 and v2f64 selects.
630 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
631 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
632 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
633 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
636 // We want to custom lower some of our intrinsics.
637 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
639 // We have target-specific dag combine patterns for the following nodes:
640 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
641 setTargetDAGCombine(ISD::SELECT);
643 computeRegisterProperties();
645 // FIXME: These should be based on subtarget info. Plus, the values should
646 // be smaller when we are in optimizing for size mode.
647 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
648 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
649 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
650 allowUnalignedMemoryAccesses = true; // x86 supports it!
654 /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
656 SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
657 SelectionDAG &DAG) const {
658 if (usesGlobalOffsetTable())
659 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
660 if (!Subtarget->isPICStyleRIPRel())
661 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
665 //===----------------------------------------------------------------------===//
666 // Return Value Calling Convention Implementation
667 //===----------------------------------------------------------------------===//
669 #include "X86GenCallingConv.inc"
671 /// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
672 /// exists skip possible ISD:TokenFactor.
673 static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
674 if (Chain.getOpcode()==X86ISD::TAILCALL) {
676 } else if (Chain.getOpcode()==ISD::TokenFactor) {
677 if (Chain.getNumOperands() &&
678 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
679 return Chain.getOperand(0);
684 /// LowerRET - Lower an ISD::RET node.
685 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
686 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
688 SmallVector<CCValAssign, 16> RVLocs;
689 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
690 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
691 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
692 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
694 // If this is the first return lowered for this function, add the regs to the
695 // liveout set for the function.
696 if (DAG.getMachineFunction().liveout_empty()) {
697 for (unsigned i = 0; i != RVLocs.size(); ++i)
698 if (RVLocs[i].isRegLoc())
699 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
701 SDOperand Chain = Op.getOperand(0);
703 // Handle tail call return.
704 Chain = GetPossiblePreceedingTailCall(Chain);
705 if (Chain.getOpcode() == X86ISD::TAILCALL) {
706 SDOperand TailCall = Chain;
707 SDOperand TargetAddress = TailCall.getOperand(1);
708 SDOperand StackAdjustment = TailCall.getOperand(2);
709 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
710 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
711 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
712 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
713 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
714 "Expecting an global address, external symbol, or register");
715 assert( StackAdjustment.getOpcode() == ISD::Constant &&
716 "Expecting a const value");
718 SmallVector<SDOperand,8> Operands;
719 Operands.push_back(Chain.getOperand(0));
720 Operands.push_back(TargetAddress);
721 Operands.push_back(StackAdjustment);
722 // Copy registers used by the call. Last operand is a flag so it is not
724 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
725 Operands.push_back(Chain.getOperand(i));
727 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
734 // Copy the result values into the output registers.
735 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
736 RVLocs[0].getLocReg() != X86::ST0) {
737 for (unsigned i = 0; i != RVLocs.size(); ++i) {
738 CCValAssign &VA = RVLocs[i];
739 assert(VA.isRegLoc() && "Can only return in registers!");
740 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
742 Flag = Chain.getValue(1);
745 // We need to handle a destination of ST0 specially, because it isn't really
747 SDOperand Value = Op.getOperand(1);
749 // If this is an FP return with ScalarSSE, we need to move the value from
750 // an XMM register onto the fp-stack.
751 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
752 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
755 // If this is a load into a scalarsse value, don't store the loaded value
756 // back to the stack, only to reload it: just replace the scalar-sse load.
757 if (ISD::isNON_EXTLoad(Value.Val) &&
758 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
759 Chain = Value.getOperand(0);
760 MemLoc = Value.getOperand(1);
762 // Spill the value to memory and reload it into top of stack.
763 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
764 MachineFunction &MF = DAG.getMachineFunction();
765 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
766 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
767 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
769 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
770 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
771 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
772 Chain = Value.getValue(1);
775 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
776 SDOperand Ops[] = { Chain, Value };
777 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
778 Flag = Chain.getValue(1);
781 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
783 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
785 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
789 /// LowerCallResult - Lower the result values of an ISD::CALL into the
790 /// appropriate copies out of appropriate physical registers. This assumes that
791 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
792 /// being lowered. The returns a SDNode with the same number of values as the
794 SDNode *X86TargetLowering::
795 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
796 unsigned CallingConv, SelectionDAG &DAG) {
798 // Assign locations to each value returned by this call.
799 SmallVector<CCValAssign, 16> RVLocs;
800 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
801 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
802 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
805 SmallVector<SDOperand, 8> ResultVals;
807 // Copy all of the result registers out of their specified physreg.
808 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
809 for (unsigned i = 0; i != RVLocs.size(); ++i) {
810 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
811 RVLocs[i].getValVT(), InFlag).getValue(1);
812 InFlag = Chain.getValue(2);
813 ResultVals.push_back(Chain.getValue(0));
816 // Copies from the FP stack are special, as ST0 isn't a valid register
817 // before the fp stackifier runs.
819 // Copy ST0 into an RFP register with FP_GET_RESULT.
820 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
821 SDOperand GROps[] = { Chain, InFlag };
822 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
823 Chain = RetVal.getValue(1);
824 InFlag = RetVal.getValue(2);
826 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
828 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
829 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
830 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
831 // shouldn't be necessary except that RFP cannot be live across
832 // multiple blocks. When stackifier is fixed, they can be uncoupled.
833 MachineFunction &MF = DAG.getMachineFunction();
834 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
835 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
837 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
839 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
840 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
841 Chain = RetVal.getValue(1);
843 ResultVals.push_back(RetVal);
846 // Merge everything together with a MERGE_VALUES node.
847 ResultVals.push_back(Chain);
848 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
849 &ResultVals[0], ResultVals.size()).Val;
853 //===----------------------------------------------------------------------===//
854 // C & StdCall & Fast Calling Convention implementation
855 //===----------------------------------------------------------------------===//
856 // StdCall calling convention seems to be standard for many Windows' API
857 // routines and around. It differs from C calling convention just a little:
858 // callee should clean up the stack, not caller. Symbols should be also
859 // decorated in some fancy way :) It doesn't support any vector arguments.
860 // For info on fast calling convention see Fast Calling Convention (tail call)
861 // implementation LowerX86_32FastCCCallTo.
863 /// AddLiveIn - This helper function adds the specified physical register to the
864 /// MachineFunction as a live in value. It also creates a corresponding virtual
866 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
867 const TargetRegisterClass *RC) {
868 assert(RC->contains(PReg) && "Not the correct regclass!");
869 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
870 MF.addLiveIn(PReg, VReg);
874 // align stack arguments according to platform alignment needed for tail calls
875 unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
877 SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
878 const CCValAssign &VA,
879 MachineFrameInfo *MFI,
880 SDOperand Root, unsigned i) {
881 // Create the nodes corresponding to a load from this parameter slot.
882 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
883 VA.getLocMemOffset());
884 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
886 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
888 if (Flags & ISD::ParamFlags::ByVal)
891 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
894 SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
896 unsigned NumArgs = Op.Val->getNumValues() - 1;
897 MachineFunction &MF = DAG.getMachineFunction();
898 MachineFrameInfo *MFI = MF.getFrameInfo();
899 SDOperand Root = Op.getOperand(0);
900 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
901 unsigned CC = MF.getFunction()->getCallingConv();
902 // Assign locations to all of the incoming arguments.
903 SmallVector<CCValAssign, 16> ArgLocs;
904 CCState CCInfo(CC, isVarArg,
905 getTargetMachine(), ArgLocs);
906 // Check for possible tail call calling convention.
907 if (CC == CallingConv::Fast && PerformTailCallOpt)
908 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
910 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
912 SmallVector<SDOperand, 8> ArgValues;
913 unsigned LastVal = ~0U;
914 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
915 CCValAssign &VA = ArgLocs[i];
916 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
918 assert(VA.getValNo() != LastVal &&
919 "Don't support value assigned to multiple locs yet");
920 LastVal = VA.getValNo();
923 MVT::ValueType RegVT = VA.getLocVT();
924 TargetRegisterClass *RC;
925 if (RegVT == MVT::i32)
926 RC = X86::GR32RegisterClass;
928 assert(MVT::isVector(RegVT));
929 RC = X86::VR128RegisterClass;
932 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
933 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
935 // If this is an 8 or 16-bit value, it is really passed promoted to 32
936 // bits. Insert an assert[sz]ext to capture this, then truncate to the
938 if (VA.getLocInfo() == CCValAssign::SExt)
939 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
940 DAG.getValueType(VA.getValVT()));
941 else if (VA.getLocInfo() == CCValAssign::ZExt)
942 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
943 DAG.getValueType(VA.getValVT()));
945 if (VA.getLocInfo() != CCValAssign::Full)
946 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
948 ArgValues.push_back(ArgValue);
950 assert(VA.isMemLoc());
951 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
955 unsigned StackSize = CCInfo.getNextStackOffset();
956 // align stack specially for tail calls
957 if (CC==CallingConv::Fast)
958 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
960 ArgValues.push_back(Root);
962 // If the function takes variable number of arguments, make a frame index for
963 // the start of the first vararg value... for expansion of llvm.va_start.
965 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
967 // Tail call calling convention (CallingConv::Fast) does not support varargs.
968 assert( !(isVarArg && CC == CallingConv::Fast) &&
969 "CallingConv::Fast does not support varargs.");
971 if (isStdCall && !isVarArg &&
972 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
973 BytesToPopOnReturn = StackSize; // Callee pops everything..
974 BytesCallerReserves = 0;
976 BytesToPopOnReturn = 0; // Callee pops nothing.
978 // If this is an sret function, the return should pop the hidden pointer.
980 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
981 ISD::ParamFlags::StructReturn))
982 BytesToPopOnReturn = 4;
984 BytesCallerReserves = StackSize;
987 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
989 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
990 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
992 // Return the new list of results.
993 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
994 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
997 SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
999 SDOperand Chain = Op.getOperand(0);
1000 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1001 SDOperand Callee = Op.getOperand(4);
1002 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1004 // Analyze operands of the call, assigning locations to each operand.
1005 SmallVector<CCValAssign, 16> ArgLocs;
1006 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1007 if(CC==CallingConv::Fast && PerformTailCallOpt)
1008 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1010 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
1012 // Get a count of how many bytes are to be pushed on the stack.
1013 unsigned NumBytes = CCInfo.getNextStackOffset();
1014 if (CC==CallingConv::Fast)
1015 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
1017 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1019 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1020 SmallVector<SDOperand, 8> MemOpChains;
1024 // Walk the register/memloc assignments, inserting copies/loads.
1025 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1026 CCValAssign &VA = ArgLocs[i];
1027 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1029 // Promote the value if needed.
1030 switch (VA.getLocInfo()) {
1031 default: assert(0 && "Unknown loc info!");
1032 case CCValAssign::Full: break;
1033 case CCValAssign::SExt:
1034 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1036 case CCValAssign::ZExt:
1037 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1039 case CCValAssign::AExt:
1040 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1044 if (VA.isRegLoc()) {
1045 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1047 assert(VA.isMemLoc());
1048 if (StackPtr.Val == 0)
1049 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1051 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1056 // If the first argument is an sret pointer, remember it.
1057 bool isSRet = NumOps &&
1058 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
1059 ISD::ParamFlags::StructReturn);
1061 if (!MemOpChains.empty())
1062 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1063 &MemOpChains[0], MemOpChains.size());
1065 // Build a sequence of copy-to-reg nodes chained together with token chain
1066 // and flag operands which copy the outgoing args into registers.
1068 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1069 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1071 InFlag = Chain.getValue(1);
1074 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1076 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1077 Subtarget->isPICStyleGOT()) {
1078 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1079 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1081 InFlag = Chain.getValue(1);
1084 // If the callee is a GlobalAddress node (quite common, every direct call is)
1085 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1086 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1087 // We should use extra load for direct calls to dllimported functions in
1089 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1090 getTargetMachine(), true))
1091 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1092 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1093 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1095 // Returns a chain & a flag for retval copy to use.
1096 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1097 SmallVector<SDOperand, 8> Ops;
1098 Ops.push_back(Chain);
1099 Ops.push_back(Callee);
1101 // Add argument registers to the end of the list so that they are known live
1103 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1104 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1105 RegsToPass[i].second.getValueType()));
1107 // Add an implicit use GOT pointer in EBX.
1108 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1109 Subtarget->isPICStyleGOT())
1110 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1113 Ops.push_back(InFlag);
1115 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
1116 InFlag = Chain.getValue(1);
1118 // Create the CALLSEQ_END node.
1119 unsigned NumBytesForCalleeToPush = 0;
1121 if (CC == CallingConv::X86_StdCall ||
1122 (CC == CallingConv::Fast && PerformTailCallOpt)) {
1124 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1126 NumBytesForCalleeToPush = NumBytes;
1127 assert(!(isVarArg && CC==CallingConv::Fast) &&
1128 "CallingConv::Fast does not support varargs.");
1130 // If this is is a call to a struct-return function, the callee
1131 // pops the hidden struct pointer, so we have to push it back.
1132 // This is common for Darwin/X86, Linux & Mingw32 targets.
1133 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1136 Chain = DAG.getCALLSEQ_END(Chain,
1137 DAG.getConstant(NumBytes, getPointerTy()),
1138 DAG.getConstant(NumBytesForCalleeToPush,
1141 InFlag = Chain.getValue(1);
1143 // Handle result values, copying them out of physregs into vregs that we
1145 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1149 //===----------------------------------------------------------------------===//
1150 // FastCall Calling Convention implementation
1151 //===----------------------------------------------------------------------===//
1153 // The X86 'fastcall' calling convention passes up to two integer arguments in
1154 // registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1155 // and requires that the callee pop its arguments off the stack (allowing proper
1156 // tail calls), and has the same return value conventions as C calling convs.
1158 // This calling convention always arranges for the callee pop value to be 8n+4
1159 // bytes, which is needed for tail recursion elimination and stack alignment
1162 X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1163 MachineFunction &MF = DAG.getMachineFunction();
1164 MachineFrameInfo *MFI = MF.getFrameInfo();
1165 SDOperand Root = Op.getOperand(0);
1166 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1168 // Assign locations to all of the incoming arguments.
1169 SmallVector<CCValAssign, 16> ArgLocs;
1170 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1171 getTargetMachine(), ArgLocs);
1172 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
1174 SmallVector<SDOperand, 8> ArgValues;
1175 unsigned LastVal = ~0U;
1176 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1177 CCValAssign &VA = ArgLocs[i];
1178 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1180 assert(VA.getValNo() != LastVal &&
1181 "Don't support value assigned to multiple locs yet");
1182 LastVal = VA.getValNo();
1184 if (VA.isRegLoc()) {
1185 MVT::ValueType RegVT = VA.getLocVT();
1186 TargetRegisterClass *RC;
1187 if (RegVT == MVT::i32)
1188 RC = X86::GR32RegisterClass;
1190 assert(MVT::isVector(RegVT));
1191 RC = X86::VR128RegisterClass;
1194 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1195 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1197 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1198 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1200 if (VA.getLocInfo() == CCValAssign::SExt)
1201 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1202 DAG.getValueType(VA.getValVT()));
1203 else if (VA.getLocInfo() == CCValAssign::ZExt)
1204 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1205 DAG.getValueType(VA.getValVT()));
1207 if (VA.getLocInfo() != CCValAssign::Full)
1208 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1210 ArgValues.push_back(ArgValue);
1212 assert(VA.isMemLoc());
1213 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
1217 ArgValues.push_back(Root);
1219 unsigned StackSize = CCInfo.getNextStackOffset();
1221 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1222 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1223 // arguments and the arguments after the retaddr has been pushed are
1225 if ((StackSize & 7) == 0)
1229 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1230 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1231 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
1232 BytesCallerReserves = 0;
1234 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1235 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1237 // Return the new list of results.
1238 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1239 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1243 X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1244 const SDOperand &StackPtr,
1245 const CCValAssign &VA,
1248 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1249 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1250 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1251 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1252 if (Flags & ISD::ParamFlags::ByVal) {
1253 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1254 ISD::ParamFlags::ByValAlignOffs);
1256 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1257 ISD::ParamFlags::ByValSizeOffs;
1259 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1260 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1261 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
1263 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1266 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1270 SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1272 SDOperand Chain = Op.getOperand(0);
1273 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1274 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1275 SDOperand Callee = Op.getOperand(4);
1277 // Analyze operands of the call, assigning locations to each operand.
1278 SmallVector<CCValAssign, 16> ArgLocs;
1279 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1280 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
1282 // Get a count of how many bytes are to be pushed on the stack.
1283 unsigned NumBytes = CCInfo.getNextStackOffset();
1285 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1286 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1287 // arguments and the arguments after the retaddr has been pushed are
1289 if ((NumBytes & 7) == 0)
1293 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1295 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1296 SmallVector<SDOperand, 8> MemOpChains;
1300 // Walk the register/memloc assignments, inserting copies/loads.
1301 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1302 CCValAssign &VA = ArgLocs[i];
1303 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1305 // Promote the value if needed.
1306 switch (VA.getLocInfo()) {
1307 default: assert(0 && "Unknown loc info!");
1308 case CCValAssign::Full: break;
1309 case CCValAssign::SExt:
1310 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1312 case CCValAssign::ZExt:
1313 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1315 case CCValAssign::AExt:
1316 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1320 if (VA.isRegLoc()) {
1321 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1323 assert(VA.isMemLoc());
1324 if (StackPtr.Val == 0)
1325 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1327 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1332 if (!MemOpChains.empty())
1333 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1334 &MemOpChains[0], MemOpChains.size());
1336 // Build a sequence of copy-to-reg nodes chained together with token chain
1337 // and flag operands which copy the outgoing args into registers.
1339 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1340 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1342 InFlag = Chain.getValue(1);
1345 // If the callee is a GlobalAddress node (quite common, every direct call is)
1346 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1347 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1348 // We should use extra load for direct calls to dllimported functions in
1350 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1351 getTargetMachine(), true))
1352 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1353 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1354 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1356 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1358 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1359 Subtarget->isPICStyleGOT()) {
1360 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1361 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1363 InFlag = Chain.getValue(1);
1366 // Returns a chain & a flag for retval copy to use.
1367 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1368 SmallVector<SDOperand, 8> Ops;
1369 Ops.push_back(Chain);
1370 Ops.push_back(Callee);
1372 // Add argument registers to the end of the list so that they are known live
1374 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1375 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1376 RegsToPass[i].second.getValueType()));
1378 // Add an implicit use GOT pointer in EBX.
1379 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1380 Subtarget->isPICStyleGOT())
1381 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1384 Ops.push_back(InFlag);
1386 assert(isTailCall==false && "no tail call here");
1387 Chain = DAG.getNode(X86ISD::CALL,
1388 NodeTys, &Ops[0], Ops.size());
1389 InFlag = Chain.getValue(1);
1391 // Returns a flag for retval copy to use.
1392 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1394 Ops.push_back(Chain);
1395 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1396 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1397 Ops.push_back(InFlag);
1398 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1399 InFlag = Chain.getValue(1);
1401 // Handle result values, copying them out of physregs into vregs that we
1403 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1406 //===----------------------------------------------------------------------===//
1407 // Fast Calling Convention (tail call) implementation
1408 //===----------------------------------------------------------------------===//
1410 // Like std call, callee cleans arguments, convention except that ECX is
1411 // reserved for storing the tail called function address. Only 2 registers are
1412 // free for argument passing (inreg). Tail call optimization is performed
1414 // * tailcallopt is enabled
1415 // * caller/callee are fastcc
1416 // * elf/pic is disabled OR
1417 // * elf/pic enabled + callee is in module + callee has
1418 // visibility protected or hidden
1419 // To keep the stack aligned according to platform abi the function
1420 // GetAlignedArgumentStackSize ensures that argument delta is always multiples
1421 // of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
1422 // If a tail called function callee has more arguments than the caller the
1423 // caller needs to make sure that there is room to move the RETADDR to. This is
1424 // achieved by reserving an area the size of the argument delta right after the
1425 // original REtADDR, but before the saved framepointer or the spilled registers
1426 // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1438 /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1439 /// for a 16 byte align requirement.
1440 unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1441 SelectionDAG& DAG) {
1442 if (PerformTailCallOpt) {
1443 MachineFunction &MF = DAG.getMachineFunction();
1444 const TargetMachine &TM = MF.getTarget();
1445 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1446 unsigned StackAlignment = TFI.getStackAlignment();
1447 uint64_t AlignMask = StackAlignment - 1;
1448 int64_t Offset = StackSize;
1449 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1450 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1451 // Number smaller than 12 so just add the difference.
1452 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1454 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1455 Offset = ((~AlignMask) & Offset) + StackAlignment +
1456 (StackAlignment-SlotSize);
1463 /// IsEligibleForTailCallElimination - Check to see whether the next instruction
1464 /// following the call is a return. A function is eligible if caller/callee
1465 /// calling conventions match, currently only fastcc supports tail calls, and
1466 /// the function CALL is immediatly followed by a RET.
1467 bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1469 SelectionDAG& DAG) const {
1470 if (!PerformTailCallOpt)
1473 // Check whether CALL node immediatly preceeds the RET node and whether the
1474 // return uses the result of the node or is a void return.
1475 unsigned NumOps = Ret.getNumOperands();
1477 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1478 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
1480 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1481 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
1482 MachineFunction &MF = DAG.getMachineFunction();
1483 unsigned CallerCC = MF.getFunction()->getCallingConv();
1484 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1485 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1486 SDOperand Callee = Call.getOperand(4);
1487 // On elf/pic %ebx needs to be livein.
1488 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1489 !Subtarget->isPICStyleGOT())
1492 // Can only do local tail calls with PIC.
1493 GlobalValue * GV = 0;
1494 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1496 (GV = G->getGlobal()) &&
1497 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1505 SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1508 SDOperand Chain = Op.getOperand(0);
1509 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1510 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1511 SDOperand Callee = Op.getOperand(4);
1512 bool is64Bit = Subtarget->is64Bit();
1514 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1516 // Analyze operands of the call, assigning locations to each operand.
1517 SmallVector<CCValAssign, 16> ArgLocs;
1518 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1520 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1522 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1525 // Lower arguments at fp - stackoffset + fpdiff.
1526 MachineFunction &MF = DAG.getMachineFunction();
1528 unsigned NumBytesToBePushed =
1529 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1531 unsigned NumBytesCallerPushed =
1532 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1533 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1535 // Set the delta of movement of the returnaddr stackslot.
1536 // But only set if delta is greater than previous delta.
1537 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1538 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1541 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1543 // Adjust the Return address stack slot.
1544 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1546 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
1547 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1548 // Load the "old" Return address.
1550 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1551 // Calculate the new stack slot for the return address.
1552 int SlotSize = is64Bit ? 8 : 4;
1553 int NewReturnAddrFI =
1554 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1555 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1556 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1559 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1560 SmallVector<SDOperand, 8> MemOpChains;
1561 SmallVector<SDOperand, 8> MemOpChains2;
1562 SDOperand FramePtr, StackPtr;
1567 // Walk the register/memloc assignments, inserting copies/loads. Lower
1568 // arguments first to the stack slot where they would normally - in case of a
1569 // normal function call - be.
1570 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1571 CCValAssign &VA = ArgLocs[i];
1572 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1574 // Promote the value if needed.
1575 switch (VA.getLocInfo()) {
1576 default: assert(0 && "Unknown loc info!");
1577 case CCValAssign::Full: break;
1578 case CCValAssign::SExt:
1579 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1581 case CCValAssign::ZExt:
1582 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1584 case CCValAssign::AExt:
1585 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1589 if (VA.isRegLoc()) {
1590 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1592 assert(VA.isMemLoc());
1593 if (StackPtr.Val == 0)
1594 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1596 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1601 if (!MemOpChains.empty())
1602 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1603 &MemOpChains[0], MemOpChains.size());
1605 // Build a sequence of copy-to-reg nodes chained together with token chain
1606 // and flag operands which copy the outgoing args into registers.
1608 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1609 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1611 InFlag = Chain.getValue(1);
1613 InFlag = SDOperand();
1615 // Copy from stack slots to stack slot of a tail called function. This needs
1616 // to be done because if we would lower the arguments directly to their real
1617 // stack slot we might end up overwriting each other.
1618 // TODO: To make this more efficient (sometimes saving a store/load) we could
1619 // analyse the arguments and emit this store/load/store sequence only for
1620 // arguments which would be overwritten otherwise.
1621 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1622 CCValAssign &VA = ArgLocs[i];
1623 if (!VA.isRegLoc()) {
1624 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1625 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1627 // Get source stack slot.
1628 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1629 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1630 // Create frame index.
1631 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1632 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1633 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1634 FIN = DAG.getFrameIndex(FI, MVT::i32);
1635 if (Flags & ISD::ParamFlags::ByVal) {
1636 // Copy relative to framepointer.
1637 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1638 ISD::ParamFlags::ByValAlignOffs);
1640 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1641 ISD::ParamFlags::ByValSizeOffs;
1643 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1644 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1645 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
1647 MemOpChains2.push_back(DAG.getMemcpy(Chain, FIN, PtrOff, SizeNode,
1648 AlignNode,AlwaysInline));
1650 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1651 // Store relative to framepointer.
1652 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1657 if (!MemOpChains2.empty())
1658 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1659 &MemOpChains2[0], MemOpChains.size());
1661 // Store the return address to the appropriate stack slot.
1663 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1665 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1667 // Does not work with tail call since ebx is not restored correctly by
1668 // tailcaller. TODO: at least for x86 - verify for x86-64
1670 // If the callee is a GlobalAddress node (quite common, every direct call is)
1671 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1672 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1673 // We should use extra load for direct calls to dllimported functions in
1675 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1676 getTargetMachine(), true))
1677 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1678 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1679 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1681 assert(Callee.getOpcode() == ISD::LOAD &&
1682 "Function destination must be loaded into virtual register");
1683 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1685 Chain = DAG.getCopyToReg(Chain,
1686 DAG.getRegister(Opc, getPointerTy()) ,
1688 Callee = DAG.getRegister(Opc, getPointerTy());
1689 // Add register as live out.
1690 DAG.getMachineFunction().addLiveOut(Opc);
1693 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1694 SmallVector<SDOperand, 8> Ops;
1696 Ops.push_back(Chain);
1697 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1698 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1700 Ops.push_back(InFlag);
1701 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1702 InFlag = Chain.getValue(1);
1704 // Returns a chain & a flag for retval copy to use.
1705 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1707 Ops.push_back(Chain);
1708 Ops.push_back(Callee);
1709 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1710 // Add argument registers to the end of the list so that they are known live
1712 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1713 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1714 RegsToPass[i].second.getValueType()));
1716 Ops.push_back(InFlag);
1717 assert(InFlag.Val &&
1718 "Flag must be set. Depend on flag being set in LowerRET");
1719 Chain = DAG.getNode(X86ISD::TAILCALL,
1720 Op.Val->getVTList(), &Ops[0], Ops.size());
1722 return SDOperand(Chain.Val, Op.ResNo);
1725 //===----------------------------------------------------------------------===//
1726 // X86-64 C Calling Convention implementation
1727 //===----------------------------------------------------------------------===//
1730 X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1731 MachineFunction &MF = DAG.getMachineFunction();
1732 MachineFrameInfo *MFI = MF.getFrameInfo();
1733 SDOperand Root = Op.getOperand(0);
1734 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1735 unsigned CC= MF.getFunction()->getCallingConv();
1737 static const unsigned GPR64ArgRegs[] = {
1738 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1740 static const unsigned XMMArgRegs[] = {
1741 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1742 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1746 // Assign locations to all of the incoming arguments.
1747 SmallVector<CCValAssign, 16> ArgLocs;
1748 CCState CCInfo(CC, isVarArg,
1749 getTargetMachine(), ArgLocs);
1750 if (CC == CallingConv::Fast && PerformTailCallOpt)
1751 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1753 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1755 SmallVector<SDOperand, 8> ArgValues;
1756 unsigned LastVal = ~0U;
1757 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1758 CCValAssign &VA = ArgLocs[i];
1759 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1761 assert(VA.getValNo() != LastVal &&
1762 "Don't support value assigned to multiple locs yet");
1763 LastVal = VA.getValNo();
1765 if (VA.isRegLoc()) {
1766 MVT::ValueType RegVT = VA.getLocVT();
1767 TargetRegisterClass *RC;
1768 if (RegVT == MVT::i32)
1769 RC = X86::GR32RegisterClass;
1770 else if (RegVT == MVT::i64)
1771 RC = X86::GR64RegisterClass;
1772 else if (RegVT == MVT::f32)
1773 RC = X86::FR32RegisterClass;
1774 else if (RegVT == MVT::f64)
1775 RC = X86::FR64RegisterClass;
1777 assert(MVT::isVector(RegVT));
1778 if (MVT::getSizeInBits(RegVT) == 64) {
1779 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1782 RC = X86::VR128RegisterClass;
1785 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1786 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1788 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1789 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1791 if (VA.getLocInfo() == CCValAssign::SExt)
1792 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1793 DAG.getValueType(VA.getValVT()));
1794 else if (VA.getLocInfo() == CCValAssign::ZExt)
1795 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1796 DAG.getValueType(VA.getValVT()));
1798 if (VA.getLocInfo() != CCValAssign::Full)
1799 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1801 // Handle MMX values passed in GPRs.
1802 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1803 MVT::getSizeInBits(RegVT) == 64)
1804 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1806 ArgValues.push_back(ArgValue);
1808 assert(VA.isMemLoc());
1809 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
1813 unsigned StackSize = CCInfo.getNextStackOffset();
1814 if (CC==CallingConv::Fast)
1815 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
1817 // If the function takes variable number of arguments, make a frame index for
1818 // the start of the first vararg value... for expansion of llvm.va_start.
1820 assert(CC!=CallingConv::Fast
1821 && "Var arg not supported with calling convention fastcc");
1822 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1823 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1825 // For X86-64, if there are vararg parameters that are passed via
1826 // registers, then we must store them to their spots on the stack so they
1827 // may be loaded by deferencing the result of va_next.
1828 VarArgsGPOffset = NumIntRegs * 8;
1829 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1830 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1831 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1833 // Store the integer parameter registers.
1834 SmallVector<SDOperand, 8> MemOps;
1835 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1836 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1837 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1838 for (; NumIntRegs != 6; ++NumIntRegs) {
1839 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1840 X86::GR64RegisterClass);
1841 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1842 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1843 MemOps.push_back(Store);
1844 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1845 DAG.getConstant(8, getPointerTy()));
1848 // Now store the XMM (fp + vector) parameter registers.
1849 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1850 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1851 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1852 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1853 X86::VR128RegisterClass);
1854 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1855 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1856 MemOps.push_back(Store);
1857 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1858 DAG.getConstant(16, getPointerTy()));
1860 if (!MemOps.empty())
1861 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1862 &MemOps[0], MemOps.size());
1865 ArgValues.push_back(Root);
1866 // Tail call convention (fastcc) needs callee pop.
1867 if (CC == CallingConv::Fast && PerformTailCallOpt) {
1868 BytesToPopOnReturn = StackSize; // Callee pops everything.
1869 BytesCallerReserves = 0;
1871 BytesToPopOnReturn = 0; // Callee pops nothing.
1872 BytesCallerReserves = StackSize;
1874 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1875 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1877 // Return the new list of results.
1878 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1879 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1883 X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1885 SDOperand Chain = Op.getOperand(0);
1886 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1887 SDOperand Callee = Op.getOperand(4);
1889 // Analyze operands of the call, assigning locations to each operand.
1890 SmallVector<CCValAssign, 16> ArgLocs;
1891 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1892 if (CC==CallingConv::Fast && PerformTailCallOpt)
1893 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1895 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1897 // Get a count of how many bytes are to be pushed on the stack.
1898 unsigned NumBytes = CCInfo.getNextStackOffset();
1899 if (CC == CallingConv::Fast)
1900 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1902 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1904 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1905 SmallVector<SDOperand, 8> MemOpChains;
1909 // Walk the register/memloc assignments, inserting copies/loads.
1910 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1911 CCValAssign &VA = ArgLocs[i];
1912 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1914 // Promote the value if needed.
1915 switch (VA.getLocInfo()) {
1916 default: assert(0 && "Unknown loc info!");
1917 case CCValAssign::Full: break;
1918 case CCValAssign::SExt:
1919 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1921 case CCValAssign::ZExt:
1922 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1924 case CCValAssign::AExt:
1925 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1929 if (VA.isRegLoc()) {
1930 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1932 assert(VA.isMemLoc());
1933 if (StackPtr.Val == 0)
1934 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1936 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1941 if (!MemOpChains.empty())
1942 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1943 &MemOpChains[0], MemOpChains.size());
1945 // Build a sequence of copy-to-reg nodes chained together with token chain
1946 // and flag operands which copy the outgoing args into registers.
1948 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1949 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1951 InFlag = Chain.getValue(1);
1955 assert ( CallingConv::Fast != CC &&
1956 "Var args not supported with calling convention fastcc");
1958 // From AMD64 ABI document:
1959 // For calls that may call functions that use varargs or stdargs
1960 // (prototype-less calls or calls to functions containing ellipsis (...) in
1961 // the declaration) %al is used as hidden argument to specify the number
1962 // of SSE registers used. The contents of %al do not need to match exactly
1963 // the number of registers, but must be an ubound on the number of SSE
1964 // registers used and is in the range 0 - 8 inclusive.
1966 // Count the number of XMM registers allocated.
1967 static const unsigned XMMArgRegs[] = {
1968 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1969 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1971 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1973 Chain = DAG.getCopyToReg(Chain, X86::AL,
1974 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1975 InFlag = Chain.getValue(1);
1978 // If the callee is a GlobalAddress node (quite common, every direct call is)
1979 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1980 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1981 // We should use extra load for direct calls to dllimported functions in
1983 if (getTargetMachine().getCodeModel() != CodeModel::Large
1984 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1985 getTargetMachine(), true))
1986 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1987 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1988 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1989 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1991 // Returns a chain & a flag for retval copy to use.
1992 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1993 SmallVector<SDOperand, 8> Ops;
1994 Ops.push_back(Chain);
1995 Ops.push_back(Callee);
1997 // Add argument registers to the end of the list so that they are known live
1999 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2000 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2001 RegsToPass[i].second.getValueType()));
2004 Ops.push_back(InFlag);
2006 Chain = DAG.getNode(X86ISD::CALL,
2007 NodeTys, &Ops[0], Ops.size());
2008 InFlag = Chain.getValue(1);
2009 int NumBytesForCalleeToPush = 0;
2010 if (CC==CallingConv::Fast && PerformTailCallOpt) {
2011 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
2013 NumBytesForCalleeToPush = 0; // Callee pops nothing.
2015 // Returns a flag for retval copy to use.
2016 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2018 Ops.push_back(Chain);
2019 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
2020 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
2021 Ops.push_back(InFlag);
2022 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2023 InFlag = Chain.getValue(1);
2025 // Handle result values, copying them out of physregs into vregs that we
2027 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
2031 //===----------------------------------------------------------------------===//
2032 // Other Lowering Hooks
2033 //===----------------------------------------------------------------------===//
2036 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
2037 MachineFunction &MF = DAG.getMachineFunction();
2038 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2039 int ReturnAddrIndex = FuncInfo->getRAIndex();
2041 if (ReturnAddrIndex == 0) {
2042 // Set up a frame object for the return address.
2043 if (Subtarget->is64Bit())
2044 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2046 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
2048 FuncInfo->setRAIndex(ReturnAddrIndex);
2051 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2056 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2057 /// specific condition code. It returns a false if it cannot do a direct
2058 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2060 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2061 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2062 SelectionDAG &DAG) {
2063 X86CC = X86::COND_INVALID;
2065 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2066 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2067 // X > -1 -> X == 0, jump !sign.
2068 RHS = DAG.getConstant(0, RHS.getValueType());
2069 X86CC = X86::COND_NS;
2071 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2072 // X < 0 -> X == 0, jump on sign.
2073 X86CC = X86::COND_S;
2075 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2077 RHS = DAG.getConstant(0, RHS.getValueType());
2078 X86CC = X86::COND_LE;
2083 switch (SetCCOpcode) {
2085 case ISD::SETEQ: X86CC = X86::COND_E; break;
2086 case ISD::SETGT: X86CC = X86::COND_G; break;
2087 case ISD::SETGE: X86CC = X86::COND_GE; break;
2088 case ISD::SETLT: X86CC = X86::COND_L; break;
2089 case ISD::SETLE: X86CC = X86::COND_LE; break;
2090 case ISD::SETNE: X86CC = X86::COND_NE; break;
2091 case ISD::SETULT: X86CC = X86::COND_B; break;
2092 case ISD::SETUGT: X86CC = X86::COND_A; break;
2093 case ISD::SETULE: X86CC = X86::COND_BE; break;
2094 case ISD::SETUGE: X86CC = X86::COND_AE; break;
2097 // On a floating point condition, the flags are set as follows:
2099 // 0 | 0 | 0 | X > Y
2100 // 0 | 0 | 1 | X < Y
2101 // 1 | 0 | 0 | X == Y
2102 // 1 | 1 | 1 | unordered
2104 switch (SetCCOpcode) {
2107 case ISD::SETEQ: X86CC = X86::COND_E; break;
2108 case ISD::SETOLT: Flip = true; // Fallthrough
2110 case ISD::SETGT: X86CC = X86::COND_A; break;
2111 case ISD::SETOLE: Flip = true; // Fallthrough
2113 case ISD::SETGE: X86CC = X86::COND_AE; break;
2114 case ISD::SETUGT: Flip = true; // Fallthrough
2116 case ISD::SETLT: X86CC = X86::COND_B; break;
2117 case ISD::SETUGE: Flip = true; // Fallthrough
2119 case ISD::SETLE: X86CC = X86::COND_BE; break;
2121 case ISD::SETNE: X86CC = X86::COND_NE; break;
2122 case ISD::SETUO: X86CC = X86::COND_P; break;
2123 case ISD::SETO: X86CC = X86::COND_NP; break;
2126 std::swap(LHS, RHS);
2129 return X86CC != X86::COND_INVALID;
2132 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
2133 /// code. Current x86 isa includes the following FP cmov instructions:
2134 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2135 static bool hasFPCMov(unsigned X86CC) {
2151 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2152 /// true if Op is undef or if its value falls within the specified range (L, H].
2153 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2154 if (Op.getOpcode() == ISD::UNDEF)
2157 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2158 return (Val >= Low && Val < Hi);
2161 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2162 /// true if Op is undef or if its value equal to the specified value.
2163 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2164 if (Op.getOpcode() == ISD::UNDEF)
2166 return cast<ConstantSDNode>(Op)->getValue() == Val;
2169 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2170 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
2171 bool X86::isPSHUFDMask(SDNode *N) {
2172 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2174 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
2177 // Check if the value doesn't reference the second vector.
2178 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2179 SDOperand Arg = N->getOperand(i);
2180 if (Arg.getOpcode() == ISD::UNDEF) continue;
2181 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2182 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
2189 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2190 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2191 bool X86::isPSHUFHWMask(SDNode *N) {
2192 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2194 if (N->getNumOperands() != 8)
2197 // Lower quadword copied in order.
2198 for (unsigned i = 0; i != 4; ++i) {
2199 SDOperand Arg = N->getOperand(i);
2200 if (Arg.getOpcode() == ISD::UNDEF) continue;
2201 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2202 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2206 // Upper quadword shuffled.
2207 for (unsigned i = 4; i != 8; ++i) {
2208 SDOperand Arg = N->getOperand(i);
2209 if (Arg.getOpcode() == ISD::UNDEF) continue;
2210 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2211 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2212 if (Val < 4 || Val > 7)
2219 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2220 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2221 bool X86::isPSHUFLWMask(SDNode *N) {
2222 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2224 if (N->getNumOperands() != 8)
2227 // Upper quadword copied in order.
2228 for (unsigned i = 4; i != 8; ++i)
2229 if (!isUndefOrEqual(N->getOperand(i), i))
2232 // Lower quadword shuffled.
2233 for (unsigned i = 0; i != 4; ++i)
2234 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2240 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2241 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
2242 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2243 if (NumElems != 2 && NumElems != 4) return false;
2245 unsigned Half = NumElems / 2;
2246 for (unsigned i = 0; i < Half; ++i)
2247 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2249 for (unsigned i = Half; i < NumElems; ++i)
2250 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2256 bool X86::isSHUFPMask(SDNode *N) {
2257 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2258 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2261 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2262 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2263 /// half elements to come from vector 1 (which would equal the dest.) and
2264 /// the upper half to come from vector 2.
2265 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2266 if (NumOps != 2 && NumOps != 4) return false;
2268 unsigned Half = NumOps / 2;
2269 for (unsigned i = 0; i < Half; ++i)
2270 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2272 for (unsigned i = Half; i < NumOps; ++i)
2273 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2278 static bool isCommutedSHUFP(SDNode *N) {
2279 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2280 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2283 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2284 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2285 bool X86::isMOVHLPSMask(SDNode *N) {
2286 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2288 if (N->getNumOperands() != 4)
2291 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2292 return isUndefOrEqual(N->getOperand(0), 6) &&
2293 isUndefOrEqual(N->getOperand(1), 7) &&
2294 isUndefOrEqual(N->getOperand(2), 2) &&
2295 isUndefOrEqual(N->getOperand(3), 3);
2298 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2299 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2301 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2302 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2304 if (N->getNumOperands() != 4)
2307 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2308 return isUndefOrEqual(N->getOperand(0), 2) &&
2309 isUndefOrEqual(N->getOperand(1), 3) &&
2310 isUndefOrEqual(N->getOperand(2), 2) &&
2311 isUndefOrEqual(N->getOperand(3), 3);
2314 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2315 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2316 bool X86::isMOVLPMask(SDNode *N) {
2317 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2319 unsigned NumElems = N->getNumOperands();
2320 if (NumElems != 2 && NumElems != 4)
2323 for (unsigned i = 0; i < NumElems/2; ++i)
2324 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2327 for (unsigned i = NumElems/2; i < NumElems; ++i)
2328 if (!isUndefOrEqual(N->getOperand(i), i))
2334 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2335 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2337 bool X86::isMOVHPMask(SDNode *N) {
2338 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2340 unsigned NumElems = N->getNumOperands();
2341 if (NumElems != 2 && NumElems != 4)
2344 for (unsigned i = 0; i < NumElems/2; ++i)
2345 if (!isUndefOrEqual(N->getOperand(i), i))
2348 for (unsigned i = 0; i < NumElems/2; ++i) {
2349 SDOperand Arg = N->getOperand(i + NumElems/2);
2350 if (!isUndefOrEqual(Arg, i + NumElems))
2357 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2358 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
2359 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2360 bool V2IsSplat = false) {
2361 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2364 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2365 SDOperand BitI = Elts[i];
2366 SDOperand BitI1 = Elts[i+1];
2367 if (!isUndefOrEqual(BitI, j))
2370 if (isUndefOrEqual(BitI1, NumElts))
2373 if (!isUndefOrEqual(BitI1, j + NumElts))
2381 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2382 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2383 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2386 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2387 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
2388 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2389 bool V2IsSplat = false) {
2390 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2393 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2394 SDOperand BitI = Elts[i];
2395 SDOperand BitI1 = Elts[i+1];
2396 if (!isUndefOrEqual(BitI, j + NumElts/2))
2399 if (isUndefOrEqual(BitI1, NumElts))
2402 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2410 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2411 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2412 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2415 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2416 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2418 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2419 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2421 unsigned NumElems = N->getNumOperands();
2422 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2425 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2426 SDOperand BitI = N->getOperand(i);
2427 SDOperand BitI1 = N->getOperand(i+1);
2429 if (!isUndefOrEqual(BitI, j))
2431 if (!isUndefOrEqual(BitI1, j))
2438 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2439 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2441 bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2442 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2444 unsigned NumElems = N->getNumOperands();
2445 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2448 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2449 SDOperand BitI = N->getOperand(i);
2450 SDOperand BitI1 = N->getOperand(i + 1);
2452 if (!isUndefOrEqual(BitI, j))
2454 if (!isUndefOrEqual(BitI1, j))
2461 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2462 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2463 /// MOVSD, and MOVD, i.e. setting the lowest element.
2464 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2465 if (NumElts != 2 && NumElts != 4)
2468 if (!isUndefOrEqual(Elts[0], NumElts))
2471 for (unsigned i = 1; i < NumElts; ++i) {
2472 if (!isUndefOrEqual(Elts[i], i))
2479 bool X86::isMOVLMask(SDNode *N) {
2480 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2481 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2484 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2485 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2486 /// element of vector 2 and the other elements to come from vector 1 in order.
2487 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2488 bool V2IsSplat = false,
2489 bool V2IsUndef = false) {
2490 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2493 if (!isUndefOrEqual(Ops[0], 0))
2496 for (unsigned i = 1; i < NumOps; ++i) {
2497 SDOperand Arg = Ops[i];
2498 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2499 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2500 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2507 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2508 bool V2IsUndef = false) {
2509 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2510 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2511 V2IsSplat, V2IsUndef);
2514 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2515 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2516 bool X86::isMOVSHDUPMask(SDNode *N) {
2517 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2519 if (N->getNumOperands() != 4)
2522 // Expect 1, 1, 3, 3
2523 for (unsigned i = 0; i < 2; ++i) {
2524 SDOperand Arg = N->getOperand(i);
2525 if (Arg.getOpcode() == ISD::UNDEF) continue;
2526 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2527 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2528 if (Val != 1) return false;
2532 for (unsigned i = 2; i < 4; ++i) {
2533 SDOperand Arg = N->getOperand(i);
2534 if (Arg.getOpcode() == ISD::UNDEF) continue;
2535 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2536 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2537 if (Val != 3) return false;
2541 // Don't use movshdup if it can be done with a shufps.
2545 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2546 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2547 bool X86::isMOVSLDUPMask(SDNode *N) {
2548 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2550 if (N->getNumOperands() != 4)
2553 // Expect 0, 0, 2, 2
2554 for (unsigned i = 0; i < 2; ++i) {
2555 SDOperand Arg = N->getOperand(i);
2556 if (Arg.getOpcode() == ISD::UNDEF) continue;
2557 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2558 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2559 if (Val != 0) return false;
2563 for (unsigned i = 2; i < 4; ++i) {
2564 SDOperand Arg = N->getOperand(i);
2565 if (Arg.getOpcode() == ISD::UNDEF) continue;
2566 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2567 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2568 if (Val != 2) return false;
2572 // Don't use movshdup if it can be done with a shufps.
2576 /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2577 /// specifies a identity operation on the LHS or RHS.
2578 static bool isIdentityMask(SDNode *N, bool RHS = false) {
2579 unsigned NumElems = N->getNumOperands();
2580 for (unsigned i = 0; i < NumElems; ++i)
2581 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2586 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2587 /// a splat of a single element.
2588 static bool isSplatMask(SDNode *N) {
2589 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2591 // This is a splat operation if each element of the permute is the same, and
2592 // if the value doesn't reference the second vector.
2593 unsigned NumElems = N->getNumOperands();
2594 SDOperand ElementBase;
2596 for (; i != NumElems; ++i) {
2597 SDOperand Elt = N->getOperand(i);
2598 if (isa<ConstantSDNode>(Elt)) {
2604 if (!ElementBase.Val)
2607 for (; i != NumElems; ++i) {
2608 SDOperand Arg = N->getOperand(i);
2609 if (Arg.getOpcode() == ISD::UNDEF) continue;
2610 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2611 if (Arg != ElementBase) return false;
2614 // Make sure it is a splat of the first vector operand.
2615 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2618 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2619 /// a splat of a single element and it's a 2 or 4 element mask.
2620 bool X86::isSplatMask(SDNode *N) {
2621 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2623 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2624 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2626 return ::isSplatMask(N);
2629 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2630 /// specifies a splat of zero element.
2631 bool X86::isSplatLoMask(SDNode *N) {
2632 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2634 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2635 if (!isUndefOrEqual(N->getOperand(i), 0))
2640 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2641 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2643 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2644 unsigned NumOperands = N->getNumOperands();
2645 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2647 for (unsigned i = 0; i < NumOperands; ++i) {
2649 SDOperand Arg = N->getOperand(NumOperands-i-1);
2650 if (Arg.getOpcode() != ISD::UNDEF)
2651 Val = cast<ConstantSDNode>(Arg)->getValue();
2652 if (Val >= NumOperands) Val -= NumOperands;
2654 if (i != NumOperands - 1)
2661 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2662 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2664 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2666 // 8 nodes, but we only care about the last 4.
2667 for (unsigned i = 7; i >= 4; --i) {
2669 SDOperand Arg = N->getOperand(i);
2670 if (Arg.getOpcode() != ISD::UNDEF)
2671 Val = cast<ConstantSDNode>(Arg)->getValue();
2680 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2681 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2683 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2685 // 8 nodes, but we only care about the first 4.
2686 for (int i = 3; i >= 0; --i) {
2688 SDOperand Arg = N->getOperand(i);
2689 if (Arg.getOpcode() != ISD::UNDEF)
2690 Val = cast<ConstantSDNode>(Arg)->getValue();
2699 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2700 /// specifies a 8 element shuffle that can be broken into a pair of
2701 /// PSHUFHW and PSHUFLW.
2702 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2703 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2705 if (N->getNumOperands() != 8)
2708 // Lower quadword shuffled.
2709 for (unsigned i = 0; i != 4; ++i) {
2710 SDOperand Arg = N->getOperand(i);
2711 if (Arg.getOpcode() == ISD::UNDEF) continue;
2712 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2713 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2718 // Upper quadword shuffled.
2719 for (unsigned i = 4; i != 8; ++i) {
2720 SDOperand Arg = N->getOperand(i);
2721 if (Arg.getOpcode() == ISD::UNDEF) continue;
2722 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2723 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2724 if (Val < 4 || Val > 7)
2731 /// CommuteVectorShuffle - Swap vector_shuffle operands as well as
2732 /// values in ther permute mask.
2733 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2734 SDOperand &V2, SDOperand &Mask,
2735 SelectionDAG &DAG) {
2736 MVT::ValueType VT = Op.getValueType();
2737 MVT::ValueType MaskVT = Mask.getValueType();
2738 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2739 unsigned NumElems = Mask.getNumOperands();
2740 SmallVector<SDOperand, 8> MaskVec;
2742 for (unsigned i = 0; i != NumElems; ++i) {
2743 SDOperand Arg = Mask.getOperand(i);
2744 if (Arg.getOpcode() == ISD::UNDEF) {
2745 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2748 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2749 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2751 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2753 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2757 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2758 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2762 SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2763 MVT::ValueType MaskVT = Mask.getValueType();
2764 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2765 unsigned NumElems = Mask.getNumOperands();
2766 SmallVector<SDOperand, 8> MaskVec;
2767 for (unsigned i = 0; i != NumElems; ++i) {
2768 SDOperand Arg = Mask.getOperand(i);
2769 if (Arg.getOpcode() == ISD::UNDEF) {
2770 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2773 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2774 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2776 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2778 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2780 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2784 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2785 /// match movhlps. The lower half elements should come from upper half of
2786 /// V1 (and in order), and the upper half elements should come from the upper
2787 /// half of V2 (and in order).
2788 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2789 unsigned NumElems = Mask->getNumOperands();
2792 for (unsigned i = 0, e = 2; i != e; ++i)
2793 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2795 for (unsigned i = 2; i != 4; ++i)
2796 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2801 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2802 /// is promoted to a vector.
2803 static inline bool isScalarLoadToVector(SDNode *N) {
2804 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2805 N = N->getOperand(0).Val;
2806 return ISD::isNON_EXTLoad(N);
2811 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2812 /// match movlp{s|d}. The lower half elements should come from lower half of
2813 /// V1 (and in order), and the upper half elements should come from the upper
2814 /// half of V2 (and in order). And since V1 will become the source of the
2815 /// MOVLP, it must be either a vector load or a scalar load to vector.
2816 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2817 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2819 // Is V2 is a vector load, don't do this transformation. We will try to use
2820 // load folding shufps op.
2821 if (ISD::isNON_EXTLoad(V2))
2824 unsigned NumElems = Mask->getNumOperands();
2825 if (NumElems != 2 && NumElems != 4)
2827 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2828 if (!isUndefOrEqual(Mask->getOperand(i), i))
2830 for (unsigned i = NumElems/2; i != NumElems; ++i)
2831 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2836 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2838 static bool isSplatVector(SDNode *N) {
2839 if (N->getOpcode() != ISD::BUILD_VECTOR)
2842 SDOperand SplatValue = N->getOperand(0);
2843 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2844 if (N->getOperand(i) != SplatValue)
2849 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2851 static bool isUndefShuffle(SDNode *N) {
2852 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2855 SDOperand V1 = N->getOperand(0);
2856 SDOperand V2 = N->getOperand(1);
2857 SDOperand Mask = N->getOperand(2);
2858 unsigned NumElems = Mask.getNumOperands();
2859 for (unsigned i = 0; i != NumElems; ++i) {
2860 SDOperand Arg = Mask.getOperand(i);
2861 if (Arg.getOpcode() != ISD::UNDEF) {
2862 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2863 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2865 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2872 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2874 static inline bool isZeroNode(SDOperand Elt) {
2875 return ((isa<ConstantSDNode>(Elt) &&
2876 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2877 (isa<ConstantFPSDNode>(Elt) &&
2878 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2881 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2882 /// to an zero vector.
2883 static bool isZeroShuffle(SDNode *N) {
2884 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2887 SDOperand V1 = N->getOperand(0);
2888 SDOperand V2 = N->getOperand(1);
2889 SDOperand Mask = N->getOperand(2);
2890 unsigned NumElems = Mask.getNumOperands();
2891 for (unsigned i = 0; i != NumElems; ++i) {
2892 SDOperand Arg = Mask.getOperand(i);
2893 if (Arg.getOpcode() == ISD::UNDEF)
2896 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2897 if (Idx < NumElems) {
2898 unsigned Opc = V1.Val->getOpcode();
2899 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2901 if (Opc != ISD::BUILD_VECTOR ||
2902 !isZeroNode(V1.Val->getOperand(Idx)))
2904 } else if (Idx >= NumElems) {
2905 unsigned Opc = V2.Val->getOpcode();
2906 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2908 if (Opc != ISD::BUILD_VECTOR ||
2909 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2916 /// getZeroVector - Returns a vector of specified type with all zero elements.
2918 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2919 assert(MVT::isVector(VT) && "Expected a vector type");
2921 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2922 // type. This ensures they get CSE'd.
2923 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2925 if (MVT::getSizeInBits(VT) == 64) // MMX
2926 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2928 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2929 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2932 /// getOnesVector - Returns a vector of specified type with all bits set.
2934 static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2935 assert(MVT::isVector(VT) && "Expected a vector type");
2937 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2938 // type. This ensures they get CSE'd.
2939 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2941 if (MVT::getSizeInBits(VT) == 64) // MMX
2942 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2944 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2945 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2949 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2950 /// that point to V2 points to its first element.
2951 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2952 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2954 bool Changed = false;
2955 SmallVector<SDOperand, 8> MaskVec;
2956 unsigned NumElems = Mask.getNumOperands();
2957 for (unsigned i = 0; i != NumElems; ++i) {
2958 SDOperand Arg = Mask.getOperand(i);
2959 if (Arg.getOpcode() != ISD::UNDEF) {
2960 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2961 if (Val > NumElems) {
2962 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2966 MaskVec.push_back(Arg);
2970 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2971 &MaskVec[0], MaskVec.size());
2975 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2976 /// operation of specified width.
2977 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2978 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2979 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2981 SmallVector<SDOperand, 8> MaskVec;
2982 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2983 for (unsigned i = 1; i != NumElems; ++i)
2984 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2985 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2988 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2989 /// of specified width.
2990 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2991 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2992 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2993 SmallVector<SDOperand, 8> MaskVec;
2994 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2995 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2996 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2998 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3001 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3002 /// of specified width.
3003 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
3004 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3005 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3006 unsigned Half = NumElems/2;
3007 SmallVector<SDOperand, 8> MaskVec;
3008 for (unsigned i = 0; i != Half; ++i) {
3009 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3010 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3012 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3015 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
3017 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
3018 SDOperand V1 = Op.getOperand(0);
3019 SDOperand Mask = Op.getOperand(2);
3020 MVT::ValueType VT = Op.getValueType();
3021 unsigned NumElems = Mask.getNumOperands();
3022 Mask = getUnpacklMask(NumElems, DAG);
3023 while (NumElems != 4) {
3024 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3027 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3029 Mask = getZeroVector(MVT::v4i32, DAG);
3030 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
3031 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
3032 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3035 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
3036 /// vector of zero or undef vector. This produces a shuffle where the low
3037 /// element of V2 is swizzled into the zero/undef vector, landing at element
3038 /// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
3039 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
3040 unsigned NumElems, unsigned Idx,
3041 bool isZero, SelectionDAG &DAG) {
3042 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
3043 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3044 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3045 SmallVector<SDOperand, 16> MaskVec;
3046 for (unsigned i = 0; i != NumElems; ++i)
3047 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3048 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3050 MaskVec.push_back(DAG.getConstant(i, EVT));
3051 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3052 &MaskVec[0], MaskVec.size());
3053 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3056 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3058 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3059 unsigned NumNonZero, unsigned NumZero,
3060 SelectionDAG &DAG, TargetLowering &TLI) {
3066 for (unsigned i = 0; i < 16; ++i) {
3067 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3068 if (ThisIsNonZero && First) {
3070 V = getZeroVector(MVT::v8i16, DAG);
3072 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3077 SDOperand ThisElt(0, 0), LastElt(0, 0);
3078 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3079 if (LastIsNonZero) {
3080 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3082 if (ThisIsNonZero) {
3083 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3084 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3085 ThisElt, DAG.getConstant(8, MVT::i8));
3087 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3092 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
3093 DAG.getConstant(i/2, TLI.getPointerTy()));
3097 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3100 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3102 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3103 unsigned NumNonZero, unsigned NumZero,
3104 SelectionDAG &DAG, TargetLowering &TLI) {
3110 for (unsigned i = 0; i < 8; ++i) {
3111 bool isNonZero = (NonZeros & (1 << i)) != 0;
3115 V = getZeroVector(MVT::v8i16, DAG);
3117 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3120 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
3121 DAG.getConstant(i, TLI.getPointerTy()));
3129 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3130 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3131 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3132 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3133 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3134 // eliminated on x86-32 hosts.
3135 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3138 if (ISD::isBuildVectorAllOnes(Op.Val))
3139 return getOnesVector(Op.getValueType(), DAG);
3140 return getZeroVector(Op.getValueType(), DAG);
3143 MVT::ValueType VT = Op.getValueType();
3144 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3145 unsigned EVTBits = MVT::getSizeInBits(EVT);
3147 unsigned NumElems = Op.getNumOperands();
3148 unsigned NumZero = 0;
3149 unsigned NumNonZero = 0;
3150 unsigned NonZeros = 0;
3151 unsigned NumNonZeroImms = 0;
3152 std::set<SDOperand> Values;
3153 for (unsigned i = 0; i < NumElems; ++i) {
3154 SDOperand Elt = Op.getOperand(i);
3155 if (Elt.getOpcode() != ISD::UNDEF) {
3157 if (isZeroNode(Elt))
3160 NonZeros |= (1 << i);
3162 if (Elt.getOpcode() == ISD::Constant ||
3163 Elt.getOpcode() == ISD::ConstantFP)
3169 if (NumNonZero == 0) {
3170 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3171 return DAG.getNode(ISD::UNDEF, VT);
3174 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3175 if (Values.size() == 1)
3178 // Special case for single non-zero element.
3179 if (NumNonZero == 1) {
3180 unsigned Idx = CountTrailingZeros_32(NonZeros);
3181 SDOperand Item = Op.getOperand(Idx);
3182 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3184 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3185 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3188 if (EVTBits == 32) {
3189 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3190 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3192 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3193 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3194 SmallVector<SDOperand, 8> MaskVec;
3195 for (unsigned i = 0; i < NumElems; i++)
3196 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3197 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3198 &MaskVec[0], MaskVec.size());
3199 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3200 DAG.getNode(ISD::UNDEF, VT), Mask);
3204 // A vector full of immediates; various special cases are already
3205 // handled, so this is best done with a single constant-pool load.
3206 if (NumNonZero == NumNonZeroImms)
3209 // Let legalizer expand 2-wide build_vectors.
3213 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3214 if (EVTBits == 8 && NumElems == 16) {
3215 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3217 if (V.Val) return V;
3220 if (EVTBits == 16 && NumElems == 8) {
3221 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3223 if (V.Val) return V;
3226 // If element VT is == 32 bits, turn it into a number of shuffles.
3227 SmallVector<SDOperand, 8> V;
3229 if (NumElems == 4 && NumZero > 0) {
3230 for (unsigned i = 0; i < 4; ++i) {
3231 bool isZero = !(NonZeros & (1 << i));
3233 V[i] = getZeroVector(VT, DAG);
3235 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3238 for (unsigned i = 0; i < 2; ++i) {
3239 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3242 V[i] = V[i*2]; // Must be a zero vector.
3245 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3246 getMOVLMask(NumElems, DAG));
3249 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3250 getMOVLMask(NumElems, DAG));
3253 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3254 getUnpacklMask(NumElems, DAG));
3259 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3260 // clears the upper bits.
3261 // FIXME: we can do the same for v4f32 case when we know both parts of
3262 // the lower half come from scalar_to_vector (loadf32). We should do
3263 // that in post legalizer dag combiner with target specific hooks.
3264 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3266 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3267 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3268 SmallVector<SDOperand, 8> MaskVec;
3269 bool Reverse = (NonZeros & 0x3) == 2;
3270 for (unsigned i = 0; i < 2; ++i)
3272 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3274 MaskVec.push_back(DAG.getConstant(i, EVT));
3275 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3276 for (unsigned i = 0; i < 2; ++i)
3278 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3280 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3281 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3282 &MaskVec[0], MaskVec.size());
3283 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3286 if (Values.size() > 2) {
3287 // Expand into a number of unpckl*.
3289 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3290 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3291 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3292 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3293 for (unsigned i = 0; i < NumElems; ++i)
3294 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3296 while (NumElems != 0) {
3297 for (unsigned i = 0; i < NumElems; ++i)
3298 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3309 SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3310 SDOperand PermMask, SelectionDAG &DAG,
3311 TargetLowering &TLI) {
3312 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3313 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3314 if (isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3315 // Handle v8i16 shuffle high / low shuffle node pair.
3316 SmallVector<SDOperand, 8> MaskVec;
3317 for (unsigned i = 0; i != 4; ++i)
3318 MaskVec.push_back(PermMask.getOperand(i));
3319 for (unsigned i = 4; i != 8; ++i)
3320 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3321 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3322 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V2, Mask);
3324 for (unsigned i = 0; i != 4; ++i)
3325 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3326 for (unsigned i = 4; i != 8; ++i)
3327 MaskVec.push_back(PermMask.getOperand(i));
3328 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3329 return DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V2, Mask);
3332 // Lower than into extracts and inserts but try to do as few as possible.
3333 // First, let's find out how many elements are already in the right order.
3334 unsigned V1InOrder = 0;
3335 unsigned V1FromV1 = 0;
3336 unsigned V2InOrder = 0;
3337 unsigned V2FromV2 = 0;
3338 SmallVector<unsigned, 8> V1Elts;
3339 SmallVector<unsigned, 8> V2Elts;
3340 for (unsigned i = 0; i < 8; ++i) {
3341 SDOperand Elt = PermMask.getOperand(i);
3342 if (Elt.getOpcode() == ISD::UNDEF) {
3343 V1Elts.push_back(i);
3344 V2Elts.push_back(i);
3348 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3350 V1Elts.push_back(i);
3351 V2Elts.push_back(i+8);
3353 } else if (EltIdx == i+8) {
3354 V1Elts.push_back(i+8);
3355 V2Elts.push_back(i);
3358 V1Elts.push_back(EltIdx);
3359 V2Elts.push_back(EltIdx);
3368 if (V2InOrder > V1InOrder) {
3369 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3371 std::swap(V1Elts, V2Elts);
3372 std::swap(V1FromV1, V2FromV2);
3375 MVT::ValueType PtrVT = TLI.getPointerTy();
3377 // If there are elements that are from V1 but out of place,
3378 // then first sort them in place
3379 SmallVector<SDOperand, 8> MaskVec;
3380 for (unsigned i = 0; i < 8; ++i) {
3381 unsigned EltIdx = V1Elts[i];
3383 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3385 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3387 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3388 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
3391 // Now let's insert elements from the other vector.
3392 for (unsigned i = 0; i < 8; ++i) {
3393 unsigned EltIdx = V1Elts[i];
3396 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3397 DAG.getConstant(EltIdx - 8, PtrVT));
3398 V1 = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V1, ExtOp,
3399 DAG.getConstant(i, PtrVT));
3405 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3406 SDOperand V1 = Op.getOperand(0);
3407 SDOperand V2 = Op.getOperand(1);
3408 SDOperand PermMask = Op.getOperand(2);
3409 MVT::ValueType VT = Op.getValueType();
3410 unsigned NumElems = PermMask.getNumOperands();
3411 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3412 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3413 bool V1IsSplat = false;
3414 bool V2IsSplat = false;
3416 if (isUndefShuffle(Op.Val))
3417 return DAG.getNode(ISD::UNDEF, VT);
3419 if (isZeroShuffle(Op.Val))
3420 return getZeroVector(VT, DAG);
3422 if (isIdentityMask(PermMask.Val))
3424 else if (isIdentityMask(PermMask.Val, true))
3427 if (isSplatMask(PermMask.Val)) {
3428 if (NumElems <= 4) return Op;
3429 // Promote it to a v4i32 splat.
3430 return PromoteSplat(Op, DAG);
3433 if (X86::isMOVLMask(PermMask.Val))
3434 return (V1IsUndef) ? V2 : Op;
3436 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3437 X86::isMOVSLDUPMask(PermMask.Val) ||
3438 X86::isMOVHLPSMask(PermMask.Val) ||
3439 X86::isMOVHPMask(PermMask.Val) ||
3440 X86::isMOVLPMask(PermMask.Val))
3443 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3444 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3445 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3447 bool Commuted = false;
3448 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3449 // 1,1,1,1 -> v8i16 though.
3450 V1IsSplat = isSplatVector(V1.Val);
3451 V2IsSplat = isSplatVector(V2.Val);
3453 // Canonicalize the splat or undef, if present, to be on the RHS.
3454 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3455 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3456 std::swap(V1IsSplat, V2IsSplat);
3457 std::swap(V1IsUndef, V2IsUndef);
3461 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3462 if (V2IsUndef) return V1;
3463 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3465 // V2 is a splat, so the mask may be malformed. That is, it may point
3466 // to any V2 element. The instruction selectior won't like this. Get
3467 // a corrected mask and commute to form a proper MOVS{S|D}.
3468 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3469 if (NewMask.Val != PermMask.Val)
3470 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3475 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3476 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3477 X86::isUNPCKLMask(PermMask.Val) ||
3478 X86::isUNPCKHMask(PermMask.Val))
3482 // Normalize mask so all entries that point to V2 points to its first
3483 // element then try to match unpck{h|l} again. If match, return a
3484 // new vector_shuffle with the corrected mask.
3485 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3486 if (NewMask.Val != PermMask.Val) {
3487 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3488 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3489 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3490 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3491 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3492 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3497 // Normalize the node to match x86 shuffle ops if needed
3498 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3499 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3502 // Commute is back and try unpck* again.
3503 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3504 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3505 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3506 X86::isUNPCKLMask(PermMask.Val) ||
3507 X86::isUNPCKHMask(PermMask.Val))
3511 // If VT is integer, try PSHUF* first, then SHUFP*.
3512 if (MVT::isInteger(VT)) {
3513 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3514 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3515 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3516 X86::isPSHUFDMask(PermMask.Val)) ||
3517 X86::isPSHUFHWMask(PermMask.Val) ||
3518 X86::isPSHUFLWMask(PermMask.Val)) {
3519 if (V2.getOpcode() != ISD::UNDEF)
3520 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3521 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3525 if (X86::isSHUFPMask(PermMask.Val) &&
3526 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3529 // Floating point cases in the other order.
3530 if (X86::isSHUFPMask(PermMask.Val))
3532 if (X86::isPSHUFDMask(PermMask.Val) ||
3533 X86::isPSHUFHWMask(PermMask.Val) ||
3534 X86::isPSHUFLWMask(PermMask.Val)) {
3535 if (V2.getOpcode() != ISD::UNDEF)
3536 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3537 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3542 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3543 if (VT == MVT::v8i16)
3544 return LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3546 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
3547 // Don't do this for MMX.
3548 MVT::ValueType MaskVT = PermMask.getValueType();
3549 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3550 SmallVector<std::pair<int, int>, 8> Locs;
3551 Locs.reserve(NumElems);
3552 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3553 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3556 // If no more than two elements come from either vector. This can be
3557 // implemented with two shuffles. First shuffle gather the elements.
3558 // The second shuffle, which takes the first shuffle as both of its
3559 // vector operands, put the elements into the right order.
3560 for (unsigned i = 0; i != NumElems; ++i) {
3561 SDOperand Elt = PermMask.getOperand(i);
3562 if (Elt.getOpcode() == ISD::UNDEF) {
3563 Locs[i] = std::make_pair(-1, -1);
3565 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3566 if (Val < NumElems) {
3567 Locs[i] = std::make_pair(0, NumLo);
3571 Locs[i] = std::make_pair(1, NumHi);
3572 if (2+NumHi < NumElems)
3573 Mask1[2+NumHi] = Elt;
3578 if (NumLo <= 2 && NumHi <= 2) {
3579 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3580 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3581 &Mask1[0], Mask1.size()));
3582 for (unsigned i = 0; i != NumElems; ++i) {
3583 if (Locs[i].first == -1)
3586 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3587 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3588 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3592 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3593 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3594 &Mask2[0], Mask2.size()));
3597 // Break it into (shuffle shuffle_hi, shuffle_lo).
3599 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3600 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3601 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3602 unsigned MaskIdx = 0;
3604 unsigned HiIdx = NumElems/2;
3605 for (unsigned i = 0; i != NumElems; ++i) {
3606 if (i == NumElems/2) {
3612 SDOperand Elt = PermMask.getOperand(i);
3613 if (Elt.getOpcode() == ISD::UNDEF) {
3614 Locs[i] = std::make_pair(-1, -1);
3615 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3616 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3617 (*MaskPtr)[LoIdx] = Elt;
3620 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3621 (*MaskPtr)[HiIdx] = Elt;
3626 SDOperand LoShuffle =
3627 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3628 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3629 &LoMask[0], LoMask.size()));
3630 SDOperand HiShuffle =
3631 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3632 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3633 &HiMask[0], HiMask.size()));
3634 SmallVector<SDOperand, 8> MaskOps;
3635 for (unsigned i = 0; i != NumElems; ++i) {
3636 if (Locs[i].first == -1) {
3637 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3639 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3640 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3643 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3644 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3645 &MaskOps[0], MaskOps.size()));
3652 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3653 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3656 MVT::ValueType VT = Op.getValueType();
3657 // TODO: handle v16i8.
3658 if (MVT::getSizeInBits(VT) == 16) {
3659 // Transform it so it match pextrw which produces a 32-bit result.
3660 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3661 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3662 Op.getOperand(0), Op.getOperand(1));
3663 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3664 DAG.getValueType(VT));
3665 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3666 } else if (MVT::getSizeInBits(VT) == 32) {
3667 SDOperand Vec = Op.getOperand(0);
3668 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3671 // SHUFPS the element to the lowest double word, then movss.
3672 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3673 SmallVector<SDOperand, 8> IdxVec;
3675 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3677 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3679 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3681 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3682 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3683 &IdxVec[0], IdxVec.size());
3684 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3685 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3686 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3687 DAG.getConstant(0, getPointerTy()));
3688 } else if (MVT::getSizeInBits(VT) == 64) {
3689 SDOperand Vec = Op.getOperand(0);
3690 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3694 // UNPCKHPD the element to the lowest double word, then movsd.
3695 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3696 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3697 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3698 SmallVector<SDOperand, 8> IdxVec;
3699 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
3701 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3702 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3703 &IdxVec[0], IdxVec.size());
3704 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3705 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3706 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3707 DAG.getConstant(0, getPointerTy()));
3714 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3715 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3716 // as its second argument.
3717 MVT::ValueType VT = Op.getValueType();
3718 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
3719 SDOperand N0 = Op.getOperand(0);
3720 SDOperand N1 = Op.getOperand(1);
3721 SDOperand N2 = Op.getOperand(2);
3722 if (MVT::getSizeInBits(BaseVT) == 16) {
3723 if (N1.getValueType() != MVT::i32)
3724 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3725 if (N2.getValueType() != MVT::i32)
3726 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
3727 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3728 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3729 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3732 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3733 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3734 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3735 SmallVector<SDOperand, 8> MaskVec;
3736 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3737 for (unsigned i = 1; i <= 3; ++i)
3738 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3739 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3740 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3741 &MaskVec[0], MaskVec.size()));
3743 // Use two pinsrw instructions to insert a 32 bit value.
3745 if (MVT::isFloatingPoint(N1.getValueType())) {
3746 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3747 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3748 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3749 DAG.getConstant(0, getPointerTy()));
3751 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3752 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3753 DAG.getConstant(Idx, getPointerTy()));
3754 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3755 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3756 DAG.getConstant(Idx+1, getPointerTy()));
3757 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3765 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3766 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3767 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3770 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3771 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3772 // one of the above mentioned nodes. It has to be wrapped because otherwise
3773 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3774 // be used to form addressing mode. These wrapped nodes will be selected
3777 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3778 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3779 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3781 CP->getAlignment());
3782 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3783 // With PIC, the address is actually $g + Offset.
3784 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3785 !Subtarget->isPICStyleRIPRel()) {
3786 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3787 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3795 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3796 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3797 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3798 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3799 // With PIC, the address is actually $g + Offset.
3800 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3801 !Subtarget->isPICStyleRIPRel()) {
3802 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3803 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3807 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3808 // load the value at address GV, not the value of GV itself. This means that
3809 // the GlobalAddress must be in the base or index register of the address, not
3810 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3811 // The same applies for external symbols during PIC codegen
3812 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3813 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3818 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
3820 LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3821 const MVT::ValueType PtrVT) {
3823 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3824 DAG.getNode(X86ISD::GlobalBaseReg,
3826 InFlag = Chain.getValue(1);
3828 // emit leal symbol@TLSGD(,%ebx,1), %eax
3829 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3830 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3831 GA->getValueType(0),
3833 SDOperand Ops[] = { Chain, TGA, InFlag };
3834 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3835 InFlag = Result.getValue(2);
3836 Chain = Result.getValue(1);
3838 // call ___tls_get_addr. This function receives its argument in
3839 // the register EAX.
3840 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3841 InFlag = Chain.getValue(1);
3843 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3844 SDOperand Ops1[] = { Chain,
3845 DAG.getTargetExternalSymbol("___tls_get_addr",
3847 DAG.getRegister(X86::EAX, PtrVT),
3848 DAG.getRegister(X86::EBX, PtrVT),
3850 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3851 InFlag = Chain.getValue(1);
3853 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3856 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3857 // "local exec" model.
3859 LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3860 const MVT::ValueType PtrVT) {
3861 // Get the Thread Pointer
3862 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3863 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3865 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3866 GA->getValueType(0),
3868 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3870 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3871 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3873 // The address of the thread local variable is the add of the thread
3874 // pointer with the offset of the variable.
3875 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3879 X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3880 // TODO: implement the "local dynamic" model
3881 // TODO: implement the "initial exec"model for pic executables
3882 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3883 "TLS not implemented for non-ELF and 64-bit targets");
3884 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3885 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3886 // otherwise use the "Local Exec"TLS Model
3887 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3888 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3890 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3894 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3895 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3896 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3897 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3898 // With PIC, the address is actually $g + Offset.
3899 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3900 !Subtarget->isPICStyleRIPRel()) {
3901 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3902 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3909 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3910 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3911 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3912 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3913 // With PIC, the address is actually $g + Offset.
3914 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3915 !Subtarget->isPICStyleRIPRel()) {
3916 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3917 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3924 /// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
3925 /// take a 2 x i32 value to shift plus a shift amount.
3926 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3927 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3928 "Not an i64 shift!");
3929 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3930 SDOperand ShOpLo = Op.getOperand(0);
3931 SDOperand ShOpHi = Op.getOperand(1);
3932 SDOperand ShAmt = Op.getOperand(2);
3933 SDOperand Tmp1 = isSRA ?
3934 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3935 DAG.getConstant(0, MVT::i32);
3937 SDOperand Tmp2, Tmp3;
3938 if (Op.getOpcode() == ISD::SHL_PARTS) {
3939 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3940 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3942 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3943 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3946 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3947 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3948 DAG.getConstant(32, MVT::i8));
3949 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3950 AndNode, DAG.getConstant(0, MVT::i8));
3953 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3954 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3955 SmallVector<SDOperand, 4> Ops;
3956 if (Op.getOpcode() == ISD::SHL_PARTS) {
3957 Ops.push_back(Tmp2);
3958 Ops.push_back(Tmp3);
3960 Ops.push_back(Cond);
3961 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3964 Ops.push_back(Tmp3);
3965 Ops.push_back(Tmp1);
3967 Ops.push_back(Cond);
3968 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3970 Ops.push_back(Tmp2);
3971 Ops.push_back(Tmp3);
3973 Ops.push_back(Cond);
3974 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3977 Ops.push_back(Tmp3);
3978 Ops.push_back(Tmp1);
3980 Ops.push_back(Cond);
3981 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3984 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3988 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3991 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3992 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3993 Op.getOperand(0).getValueType() >= MVT::i16 &&
3994 "Unknown SINT_TO_FP to lower!");
3997 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3998 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3999 MachineFunction &MF = DAG.getMachineFunction();
4000 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4001 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4002 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
4003 StackSlot, NULL, 0);
4005 // These are really Legal; caller falls through into that case.
4006 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
4008 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
4010 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
4011 Subtarget->is64Bit())
4016 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
4017 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
4019 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4021 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4022 SmallVector<SDOperand, 8> Ops;
4023 Ops.push_back(Chain);
4024 Ops.push_back(StackSlot);
4025 Ops.push_back(DAG.getValueType(SrcVT));
4026 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
4027 Tys, &Ops[0], Ops.size());
4030 Chain = Result.getValue(1);
4031 SDOperand InFlag = Result.getValue(2);
4033 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4034 // shouldn't be necessary except that RFP cannot be live across
4035 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4036 MachineFunction &MF = DAG.getMachineFunction();
4037 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4038 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4039 Tys = DAG.getVTList(MVT::Other);
4040 SmallVector<SDOperand, 8> Ops;
4041 Ops.push_back(Chain);
4042 Ops.push_back(Result);
4043 Ops.push_back(StackSlot);
4044 Ops.push_back(DAG.getValueType(Op.getValueType()));
4045 Ops.push_back(InFlag);
4046 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
4047 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
4053 std::pair<SDOperand,SDOperand> X86TargetLowering::
4054 FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
4055 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4056 "Unknown FP_TO_SINT to lower!");
4058 // These are really Legal.
4059 if (Op.getValueType() == MVT::i32 &&
4060 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
4061 return std::make_pair(SDOperand(), SDOperand());
4062 if (Op.getValueType() == MVT::i32 &&
4063 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
4064 return std::make_pair(SDOperand(), SDOperand());
4065 if (Subtarget->is64Bit() &&
4066 Op.getValueType() == MVT::i64 &&
4067 Op.getOperand(0).getValueType() != MVT::f80)
4068 return std::make_pair(SDOperand(), SDOperand());
4070 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4072 MachineFunction &MF = DAG.getMachineFunction();
4073 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4074 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4075 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4077 switch (Op.getValueType()) {
4078 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4079 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4080 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4081 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
4084 SDOperand Chain = DAG.getEntryNode();
4085 SDOperand Value = Op.getOperand(0);
4086 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
4087 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
4088 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
4089 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
4090 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4092 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4094 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4095 Chain = Value.getValue(1);
4096 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4097 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4100 // Build the FP_TO_INT*_IN_MEM
4101 SDOperand Ops[] = { Chain, Value, StackSlot };
4102 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4104 return std::make_pair(FIST, StackSlot);
4107 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
4108 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4109 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4110 if (FIST.Val == 0) return SDOperand();
4113 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4116 SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4117 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4118 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4119 if (FIST.Val == 0) return 0;
4121 // Return an i64 load from the stack slot.
4122 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4124 // Use a MERGE_VALUES node to drop the chain result value.
4125 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4128 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4129 MVT::ValueType VT = Op.getValueType();
4130 MVT::ValueType EltVT = VT;
4131 if (MVT::isVector(VT))
4132 EltVT = MVT::getVectorElementType(VT);
4133 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4134 std::vector<Constant*> CV;
4135 if (EltVT == MVT::f64) {
4136 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
4140 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
4146 Constant *C = ConstantVector::get(CV);
4147 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4148 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4150 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4153 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4154 MVT::ValueType VT = Op.getValueType();
4155 MVT::ValueType EltVT = VT;
4156 unsigned EltNum = 1;
4157 if (MVT::isVector(VT)) {
4158 EltVT = MVT::getVectorElementType(VT);
4159 EltNum = MVT::getVectorNumElements(VT);
4161 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4162 std::vector<Constant*> CV;
4163 if (EltVT == MVT::f64) {
4164 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
4168 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
4174 Constant *C = ConstantVector::get(CV);
4175 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4176 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4178 if (MVT::isVector(VT)) {
4179 return DAG.getNode(ISD::BIT_CONVERT, VT,
4180 DAG.getNode(ISD::XOR, MVT::v2i64,
4181 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4182 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4184 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4188 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4189 SDOperand Op0 = Op.getOperand(0);
4190 SDOperand Op1 = Op.getOperand(1);
4191 MVT::ValueType VT = Op.getValueType();
4192 MVT::ValueType SrcVT = Op1.getValueType();
4193 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4195 // If second operand is smaller, extend it first.
4196 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4197 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4199 SrcTy = MVT::getTypeForValueType(SrcVT);
4201 // And if it is bigger, shrink it first.
4202 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4203 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1);
4205 SrcTy = MVT::getTypeForValueType(SrcVT);
4208 // At this point the operands and the result should have the same
4209 // type, and that won't be f80 since that is not custom lowered.
4211 // First get the sign bit of second operand.
4212 std::vector<Constant*> CV;
4213 if (SrcVT == MVT::f64) {
4214 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4215 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
4217 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4218 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4219 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4220 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4222 Constant *C = ConstantVector::get(CV);
4223 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4224 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4226 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4228 // Shift sign bit right or left if the two operands have different types.
4229 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4230 // Op0 is MVT::f32, Op1 is MVT::f64.
4231 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4232 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4233 DAG.getConstant(32, MVT::i32));
4234 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4235 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4236 DAG.getConstant(0, getPointerTy()));
4239 // Clear first operand sign bit.
4241 if (VT == MVT::f64) {
4242 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4243 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
4245 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4246 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4247 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4248 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4250 C = ConstantVector::get(CV);
4251 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4252 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4254 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4256 // Or the value with the sign bit.
4257 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4260 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
4261 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
4263 SDOperand Op0 = Op.getOperand(0);
4264 SDOperand Op1 = Op.getOperand(1);
4265 SDOperand CC = Op.getOperand(2);
4266 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4267 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4270 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
4272 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4273 return DAG.getNode(X86ISD::SETCC, MVT::i8,
4274 DAG.getConstant(X86CC, MVT::i8), Cond);
4277 assert(isFP && "Illegal integer SetCC!");
4279 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4280 switch (SetCCOpcode) {
4281 default: assert(false && "Illegal floating point SetCC!");
4282 case ISD::SETOEQ: { // !PF & ZF
4283 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4284 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
4285 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4286 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4287 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4289 case ISD::SETUNE: { // PF | !ZF
4290 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4291 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
4292 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4293 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4294 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4300 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4301 bool addTest = true;
4302 SDOperand Cond = Op.getOperand(0);
4305 if (Cond.getOpcode() == ISD::SETCC)
4306 Cond = LowerSETCC(Cond, DAG);
4308 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4309 // setting operand in place of the X86ISD::SETCC.
4310 if (Cond.getOpcode() == X86ISD::SETCC) {
4311 CC = Cond.getOperand(0);
4313 SDOperand Cmp = Cond.getOperand(1);
4314 unsigned Opc = Cmp.getOpcode();
4315 MVT::ValueType VT = Op.getValueType();
4316 bool IllegalFPCMov = false;
4317 if (VT == MVT::f32 && !X86ScalarSSEf32)
4318 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4319 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4320 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4321 else if (VT == MVT::f80)
4322 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4323 if ((Opc == X86ISD::CMP ||
4324 Opc == X86ISD::COMI ||
4325 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
4332 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4333 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
4336 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4338 SmallVector<SDOperand, 4> Ops;
4339 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4340 // condition is true.
4341 Ops.push_back(Op.getOperand(2));
4342 Ops.push_back(Op.getOperand(1));
4344 Ops.push_back(Cond);
4345 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
4348 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4349 bool addTest = true;
4350 SDOperand Chain = Op.getOperand(0);
4351 SDOperand Cond = Op.getOperand(1);
4352 SDOperand Dest = Op.getOperand(2);
4355 if (Cond.getOpcode() == ISD::SETCC)
4356 Cond = LowerSETCC(Cond, DAG);
4358 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4359 // setting operand in place of the X86ISD::SETCC.
4360 if (Cond.getOpcode() == X86ISD::SETCC) {
4361 CC = Cond.getOperand(0);
4363 SDOperand Cmp = Cond.getOperand(1);
4364 unsigned Opc = Cmp.getOpcode();
4365 if (Opc == X86ISD::CMP ||
4366 Opc == X86ISD::COMI ||
4367 Opc == X86ISD::UCOMI) {
4374 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4375 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
4377 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
4378 Chain, Op.getOperand(2), CC, Cond);
4381 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
4382 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4383 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
4385 if (Subtarget->is64Bit())
4386 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4387 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4389 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
4391 switch (CallingConv) {
4393 assert(0 && "Unsupported calling convention");
4394 case CallingConv::Fast:
4395 if (isTailCall && PerformTailCallOpt)
4396 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4398 return LowerCCCCallTo(Op,DAG, CallingConv);
4399 case CallingConv::C:
4400 case CallingConv::X86_StdCall:
4401 return LowerCCCCallTo(Op, DAG, CallingConv);
4402 case CallingConv::X86_FastCall:
4403 return LowerFastCCCallTo(Op, DAG, CallingConv);
4408 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4409 // Calls to _alloca is needed to probe the stack when allocating more than 4k
4410 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
4411 // that the guard pages used by the OS virtual memory manager are allocated in
4412 // correct sequence.
4414 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4415 SelectionDAG &DAG) {
4416 assert(Subtarget->isTargetCygMing() &&
4417 "This should be used only on Cygwin/Mingw targets");
4420 SDOperand Chain = Op.getOperand(0);
4421 SDOperand Size = Op.getOperand(1);
4422 // FIXME: Ensure alignment here
4426 MVT::ValueType IntPtr = getPointerTy();
4427 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
4429 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4430 Flag = Chain.getValue(1);
4432 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4433 SDOperand Ops[] = { Chain,
4434 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4435 DAG.getRegister(X86::EAX, IntPtr),
4437 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4438 Flag = Chain.getValue(1);
4440 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4442 std::vector<MVT::ValueType> Tys;
4443 Tys.push_back(SPTy);
4444 Tys.push_back(MVT::Other);
4445 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4446 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4450 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
4451 MachineFunction &MF = DAG.getMachineFunction();
4452 const Function* Fn = MF.getFunction();
4453 if (Fn->hasExternalLinkage() &&
4454 Subtarget->isTargetCygMing() &&
4455 Fn->getName() == "main")
4456 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
4458 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4459 if (Subtarget->is64Bit())
4460 return LowerX86_64CCCArguments(Op, DAG);
4464 assert(0 && "Unsupported calling convention");
4465 case CallingConv::Fast:
4466 return LowerCCCArguments(Op,DAG, true);
4468 case CallingConv::C:
4469 return LowerCCCArguments(Op, DAG);
4470 case CallingConv::X86_StdCall:
4471 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
4472 return LowerCCCArguments(Op, DAG, true);
4473 case CallingConv::X86_FastCall:
4474 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
4475 return LowerFastCCArguments(Op, DAG);
4479 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4480 SDOperand InFlag(0, 0);
4481 SDOperand Chain = Op.getOperand(0);
4483 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4484 if (Align == 0) Align = 1;
4486 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4487 // If not DWORD aligned or size is more than the threshold, call memset.
4488 // The libc version is likely to be faster for these cases. It can use the
4489 // address value and run time information about the CPU.
4490 if ((Align & 3) != 0 ||
4491 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
4492 MVT::ValueType IntPtr = getPointerTy();
4493 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4494 TargetLowering::ArgListTy Args;
4495 TargetLowering::ArgListEntry Entry;
4496 Entry.Node = Op.getOperand(1);
4497 Entry.Ty = IntPtrTy;
4498 Args.push_back(Entry);
4499 // Extend the unsigned i8 argument to be an int value for the call.
4500 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4501 Entry.Ty = IntPtrTy;
4502 Args.push_back(Entry);
4503 Entry.Node = Op.getOperand(3);
4504 Args.push_back(Entry);
4505 std::pair<SDOperand,SDOperand> CallResult =
4506 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4507 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4508 return CallResult.second;
4513 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4514 unsigned BytesLeft = 0;
4515 bool TwoRepStos = false;
4518 uint64_t Val = ValC->getValue() & 255;
4520 // If the value is a constant, then we can potentially use larger sets.
4521 switch (Align & 3) {
4522 case 2: // WORD aligned
4525 Val = (Val << 8) | Val;
4527 case 0: // DWORD aligned
4530 Val = (Val << 8) | Val;
4531 Val = (Val << 16) | Val;
4532 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4535 Val = (Val << 32) | Val;
4538 default: // Byte aligned
4541 Count = Op.getOperand(3);
4545 if (AVT > MVT::i8) {
4547 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4548 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4549 BytesLeft = I->getValue() % UBytes;
4551 assert(AVT >= MVT::i32 &&
4552 "Do not use rep;stos if not at least DWORD aligned");
4553 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4554 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4559 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4561 InFlag = Chain.getValue(1);
4564 Count = Op.getOperand(3);
4565 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4566 InFlag = Chain.getValue(1);
4569 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4571 InFlag = Chain.getValue(1);
4572 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4573 Op.getOperand(1), InFlag);
4574 InFlag = Chain.getValue(1);
4576 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4577 SmallVector<SDOperand, 8> Ops;
4578 Ops.push_back(Chain);
4579 Ops.push_back(DAG.getValueType(AVT));
4580 Ops.push_back(InFlag);
4581 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4584 InFlag = Chain.getValue(1);
4585 Count = Op.getOperand(3);
4586 MVT::ValueType CVT = Count.getValueType();
4587 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4588 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4589 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4591 InFlag = Chain.getValue(1);
4592 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4594 Ops.push_back(Chain);
4595 Ops.push_back(DAG.getValueType(MVT::i8));
4596 Ops.push_back(InFlag);
4597 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4598 } else if (BytesLeft) {
4599 // Issue stores for the last 1 - 7 bytes.
4601 unsigned Val = ValC->getValue() & 255;
4602 unsigned Offset = I->getValue() - BytesLeft;
4603 SDOperand DstAddr = Op.getOperand(1);
4604 MVT::ValueType AddrVT = DstAddr.getValueType();
4605 if (BytesLeft >= 4) {
4606 Val = (Val << 8) | Val;
4607 Val = (Val << 16) | Val;
4608 Value = DAG.getConstant(Val, MVT::i32);
4609 Chain = DAG.getStore(Chain, Value,
4610 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4611 DAG.getConstant(Offset, AddrVT)),
4616 if (BytesLeft >= 2) {
4617 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4618 Chain = DAG.getStore(Chain, Value,
4619 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4620 DAG.getConstant(Offset, AddrVT)),
4625 if (BytesLeft == 1) {
4626 Value = DAG.getConstant(Val, MVT::i8);
4627 Chain = DAG.getStore(Chain, Value,
4628 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4629 DAG.getConstant(Offset, AddrVT)),
4637 SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4642 SelectionDAG &DAG) {
4644 unsigned BytesLeft = 0;
4645 switch (Align & 3) {
4646 case 2: // WORD aligned
4649 case 0: // DWORD aligned
4651 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4654 default: // Byte aligned
4659 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4660 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4661 BytesLeft = Size % UBytes;
4663 SDOperand InFlag(0, 0);
4664 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4666 InFlag = Chain.getValue(1);
4667 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4669 InFlag = Chain.getValue(1);
4670 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4672 InFlag = Chain.getValue(1);
4674 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4675 SmallVector<SDOperand, 8> Ops;
4676 Ops.push_back(Chain);
4677 Ops.push_back(DAG.getValueType(AVT));
4678 Ops.push_back(InFlag);
4679 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4682 // Issue loads and stores for the last 1 - 7 bytes.
4683 unsigned Offset = Size - BytesLeft;
4684 SDOperand DstAddr = Dest;
4685 MVT::ValueType DstVT = DstAddr.getValueType();
4686 SDOperand SrcAddr = Source;
4687 MVT::ValueType SrcVT = SrcAddr.getValueType();
4689 if (BytesLeft >= 4) {
4690 Value = DAG.getLoad(MVT::i32, Chain,
4691 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4692 DAG.getConstant(Offset, SrcVT)),
4694 Chain = Value.getValue(1);
4695 Chain = DAG.getStore(Chain, Value,
4696 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4697 DAG.getConstant(Offset, DstVT)),
4702 if (BytesLeft >= 2) {
4703 Value = DAG.getLoad(MVT::i16, Chain,
4704 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4705 DAG.getConstant(Offset, SrcVT)),
4707 Chain = Value.getValue(1);
4708 Chain = DAG.getStore(Chain, Value,
4709 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4710 DAG.getConstant(Offset, DstVT)),
4716 if (BytesLeft == 1) {
4717 Value = DAG.getLoad(MVT::i8, Chain,
4718 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4719 DAG.getConstant(Offset, SrcVT)),
4721 Chain = Value.getValue(1);
4722 Chain = DAG.getStore(Chain, Value,
4723 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4724 DAG.getConstant(Offset, DstVT)),
4732 /// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4733 SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
4734 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4735 SDOperand TheChain = N->getOperand(0);
4736 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
4737 if (Subtarget->is64Bit()) {
4738 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4739 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4740 MVT::i64, rax.getValue(2));
4741 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
4742 DAG.getConstant(32, MVT::i8));
4744 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
4747 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4748 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
4751 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4752 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4753 MVT::i32, eax.getValue(2));
4754 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4755 SDOperand Ops[] = { eax, edx };
4756 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4758 // Use a MERGE_VALUES to return the value and chain.
4759 Ops[1] = edx.getValue(1);
4760 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4761 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
4764 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4765 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4767 if (!Subtarget->is64Bit()) {
4768 // vastart just stores the address of the VarArgsFrameIndex slot into the
4769 // memory location argument.
4770 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4771 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4776 // gp_offset (0 - 6 * 8)
4777 // fp_offset (48 - 48 + 8 * 16)
4778 // overflow_arg_area (point to parameters coming in memory).
4780 SmallVector<SDOperand, 8> MemOps;
4781 SDOperand FIN = Op.getOperand(1);
4783 SDOperand Store = DAG.getStore(Op.getOperand(0),
4784 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4785 FIN, SV->getValue(), SV->getOffset());
4786 MemOps.push_back(Store);
4789 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4790 DAG.getConstant(4, getPointerTy()));
4791 Store = DAG.getStore(Op.getOperand(0),
4792 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4793 FIN, SV->getValue(), SV->getOffset());
4794 MemOps.push_back(Store);
4796 // Store ptr to overflow_arg_area
4797 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4798 DAG.getConstant(4, getPointerTy()));
4799 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4800 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4802 MemOps.push_back(Store);
4804 // Store ptr to reg_save_area.
4805 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4806 DAG.getConstant(8, getPointerTy()));
4807 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4808 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4810 MemOps.push_back(Store);
4811 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4814 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4815 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4816 SDOperand Chain = Op.getOperand(0);
4817 SDOperand DstPtr = Op.getOperand(1);
4818 SDOperand SrcPtr = Op.getOperand(2);
4819 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4820 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4822 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4823 SrcSV->getValue(), SrcSV->getOffset());
4824 Chain = SrcPtr.getValue(1);
4825 for (unsigned i = 0; i < 3; ++i) {
4826 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4827 SrcSV->getValue(), SrcSV->getOffset());
4828 Chain = Val.getValue(1);
4829 Chain = DAG.getStore(Chain, Val, DstPtr,
4830 DstSV->getValue(), DstSV->getOffset());
4833 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4834 DAG.getConstant(8, getPointerTy()));
4835 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4836 DAG.getConstant(8, getPointerTy()));
4842 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4843 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4845 default: return SDOperand(); // Don't custom lower most intrinsics.
4846 // Comparison intrinsics.
4847 case Intrinsic::x86_sse_comieq_ss:
4848 case Intrinsic::x86_sse_comilt_ss:
4849 case Intrinsic::x86_sse_comile_ss:
4850 case Intrinsic::x86_sse_comigt_ss:
4851 case Intrinsic::x86_sse_comige_ss:
4852 case Intrinsic::x86_sse_comineq_ss:
4853 case Intrinsic::x86_sse_ucomieq_ss:
4854 case Intrinsic::x86_sse_ucomilt_ss:
4855 case Intrinsic::x86_sse_ucomile_ss:
4856 case Intrinsic::x86_sse_ucomigt_ss:
4857 case Intrinsic::x86_sse_ucomige_ss:
4858 case Intrinsic::x86_sse_ucomineq_ss:
4859 case Intrinsic::x86_sse2_comieq_sd:
4860 case Intrinsic::x86_sse2_comilt_sd:
4861 case Intrinsic::x86_sse2_comile_sd:
4862 case Intrinsic::x86_sse2_comigt_sd:
4863 case Intrinsic::x86_sse2_comige_sd:
4864 case Intrinsic::x86_sse2_comineq_sd:
4865 case Intrinsic::x86_sse2_ucomieq_sd:
4866 case Intrinsic::x86_sse2_ucomilt_sd:
4867 case Intrinsic::x86_sse2_ucomile_sd:
4868 case Intrinsic::x86_sse2_ucomigt_sd:
4869 case Intrinsic::x86_sse2_ucomige_sd:
4870 case Intrinsic::x86_sse2_ucomineq_sd: {
4872 ISD::CondCode CC = ISD::SETCC_INVALID;
4875 case Intrinsic::x86_sse_comieq_ss:
4876 case Intrinsic::x86_sse2_comieq_sd:
4880 case Intrinsic::x86_sse_comilt_ss:
4881 case Intrinsic::x86_sse2_comilt_sd:
4885 case Intrinsic::x86_sse_comile_ss:
4886 case Intrinsic::x86_sse2_comile_sd:
4890 case Intrinsic::x86_sse_comigt_ss:
4891 case Intrinsic::x86_sse2_comigt_sd:
4895 case Intrinsic::x86_sse_comige_ss:
4896 case Intrinsic::x86_sse2_comige_sd:
4900 case Intrinsic::x86_sse_comineq_ss:
4901 case Intrinsic::x86_sse2_comineq_sd:
4905 case Intrinsic::x86_sse_ucomieq_ss:
4906 case Intrinsic::x86_sse2_ucomieq_sd:
4907 Opc = X86ISD::UCOMI;
4910 case Intrinsic::x86_sse_ucomilt_ss:
4911 case Intrinsic::x86_sse2_ucomilt_sd:
4912 Opc = X86ISD::UCOMI;
4915 case Intrinsic::x86_sse_ucomile_ss:
4916 case Intrinsic::x86_sse2_ucomile_sd:
4917 Opc = X86ISD::UCOMI;
4920 case Intrinsic::x86_sse_ucomigt_ss:
4921 case Intrinsic::x86_sse2_ucomigt_sd:
4922 Opc = X86ISD::UCOMI;
4925 case Intrinsic::x86_sse_ucomige_ss:
4926 case Intrinsic::x86_sse2_ucomige_sd:
4927 Opc = X86ISD::UCOMI;
4930 case Intrinsic::x86_sse_ucomineq_ss:
4931 case Intrinsic::x86_sse2_ucomineq_sd:
4932 Opc = X86ISD::UCOMI;
4938 SDOperand LHS = Op.getOperand(1);
4939 SDOperand RHS = Op.getOperand(2);
4940 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4942 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4943 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4944 DAG.getConstant(X86CC, MVT::i8), Cond);
4945 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4950 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4951 // Depths > 0 not supported yet!
4952 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4955 // Just load the return address
4956 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4957 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4960 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4961 // Depths > 0 not supported yet!
4962 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4965 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4966 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4967 DAG.getConstant(4, getPointerTy()));
4970 SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4971 SelectionDAG &DAG) {
4972 // Is not yet supported on x86-64
4973 if (Subtarget->is64Bit())
4976 return DAG.getConstant(8, getPointerTy());
4979 SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4981 assert(!Subtarget->is64Bit() &&
4982 "Lowering of eh_return builtin is not supported yet on x86-64");
4984 MachineFunction &MF = DAG.getMachineFunction();
4985 SDOperand Chain = Op.getOperand(0);
4986 SDOperand Offset = Op.getOperand(1);
4987 SDOperand Handler = Op.getOperand(2);
4989 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4992 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4993 DAG.getConstant(-4UL, getPointerTy()));
4994 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4995 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4996 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4997 MF.addLiveOut(X86::ECX);
4999 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5000 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5003 SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5004 SelectionDAG &DAG) {
5005 SDOperand Root = Op.getOperand(0);
5006 SDOperand Trmp = Op.getOperand(1); // trampoline
5007 SDOperand FPtr = Op.getOperand(2); // nested function
5008 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5010 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
5012 if (Subtarget->is64Bit()) {
5013 return SDOperand(); // not yet supported
5015 Function *Func = (Function *)
5016 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5017 unsigned CC = Func->getCallingConv();
5022 assert(0 && "Unsupported calling convention");
5023 case CallingConv::C:
5024 case CallingConv::X86_StdCall: {
5025 // Pass 'nest' parameter in ECX.
5026 // Must be kept in sync with X86CallingConv.td
5029 // Check that ECX wasn't needed by an 'inreg' parameter.
5030 const FunctionType *FTy = Func->getFunctionType();
5031 const ParamAttrsList *Attrs = Func->getParamAttrs();
5033 if (Attrs && !Func->isVarArg()) {
5034 unsigned InRegCount = 0;
5037 for (FunctionType::param_iterator I = FTy->param_begin(),
5038 E = FTy->param_end(); I != E; ++I, ++Idx)
5039 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5040 // FIXME: should only count parameters that are lowered to integers.
5041 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5043 if (InRegCount > 2) {
5044 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5050 case CallingConv::X86_FastCall:
5051 // Pass 'nest' parameter in EAX.
5052 // Must be kept in sync with X86CallingConv.td
5057 const X86InstrInfo *TII =
5058 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5060 SDOperand OutChains[4];
5061 SDOperand Addr, Disp;
5063 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5064 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5066 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5067 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
5068 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
5069 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
5071 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5072 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
5073 TrmpSV->getOffset() + 1, false, 1);
5075 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
5076 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5077 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5078 TrmpSV->getValue() + 5, TrmpSV->getOffset());
5080 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5081 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
5082 TrmpSV->getOffset() + 6, false, 1);
5085 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5086 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
5090 SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
5092 The rounding mode is in bits 11:10 of FPSR, and has the following
5099 FLT_ROUNDS, on the other hand, expects the following:
5106 To perform the conversion, we do:
5107 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5110 MachineFunction &MF = DAG.getMachineFunction();
5111 const TargetMachine &TM = MF.getTarget();
5112 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5113 unsigned StackAlignment = TFI.getStackAlignment();
5114 MVT::ValueType VT = Op.getValueType();
5116 // Save FP Control Word to stack slot
5117 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5118 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5120 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5121 DAG.getEntryNode(), StackSlot);
5123 // Load FP Control Word from stack slot
5124 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5126 // Transform as necessary
5128 DAG.getNode(ISD::SRL, MVT::i16,
5129 DAG.getNode(ISD::AND, MVT::i16,
5130 CWD, DAG.getConstant(0x800, MVT::i16)),
5131 DAG.getConstant(11, MVT::i8));
5133 DAG.getNode(ISD::SRL, MVT::i16,
5134 DAG.getNode(ISD::AND, MVT::i16,
5135 CWD, DAG.getConstant(0x400, MVT::i16)),
5136 DAG.getConstant(9, MVT::i8));
5139 DAG.getNode(ISD::AND, MVT::i16,
5140 DAG.getNode(ISD::ADD, MVT::i16,
5141 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5142 DAG.getConstant(1, MVT::i16)),
5143 DAG.getConstant(3, MVT::i16));
5146 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5147 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5150 /// LowerOperation - Provide custom lowering hooks for some operations.
5152 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5153 switch (Op.getOpcode()) {
5154 default: assert(0 && "Should not custom lower this!");
5155 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5156 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5157 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5158 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5159 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5160 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5161 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5162 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5163 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5164 case ISD::SHL_PARTS:
5165 case ISD::SRA_PARTS:
5166 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5167 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5168 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5169 case ISD::FABS: return LowerFABS(Op, DAG);
5170 case ISD::FNEG: return LowerFNEG(Op, DAG);
5171 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
5172 case ISD::SETCC: return LowerSETCC(Op, DAG);
5173 case ISD::SELECT: return LowerSELECT(Op, DAG);
5174 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
5175 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5176 case ISD::CALL: return LowerCALL(Op, DAG);
5177 case ISD::RET: return LowerRET(Op, DAG);
5178 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5179 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5180 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
5181 case ISD::VASTART: return LowerVASTART(Op, DAG);
5182 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5183 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5184 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5185 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5186 case ISD::FRAME_TO_ARGS_OFFSET:
5187 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5188 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5189 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
5190 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
5191 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
5194 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5195 case ISD::READCYCLECOUNTER:
5196 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
5200 /// ExpandOperation - Provide custom lowering hooks for expanding operations.
5201 SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5202 switch (N->getOpcode()) {
5203 default: assert(0 && "Should not custom lower this!");
5204 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5205 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5209 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5211 default: return NULL;
5212 case X86ISD::SHLD: return "X86ISD::SHLD";
5213 case X86ISD::SHRD: return "X86ISD::SHRD";
5214 case X86ISD::FAND: return "X86ISD::FAND";
5215 case X86ISD::FOR: return "X86ISD::FOR";
5216 case X86ISD::FXOR: return "X86ISD::FXOR";
5217 case X86ISD::FSRL: return "X86ISD::FSRL";
5218 case X86ISD::FILD: return "X86ISD::FILD";
5219 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5220 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5221 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5222 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5223 case X86ISD::FLD: return "X86ISD::FLD";
5224 case X86ISD::FST: return "X86ISD::FST";
5225 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
5226 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5227 case X86ISD::CALL: return "X86ISD::CALL";
5228 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5229 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5230 case X86ISD::CMP: return "X86ISD::CMP";
5231 case X86ISD::COMI: return "X86ISD::COMI";
5232 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5233 case X86ISD::SETCC: return "X86ISD::SETCC";
5234 case X86ISD::CMOV: return "X86ISD::CMOV";
5235 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5236 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5237 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5238 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
5239 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5240 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5241 case X86ISD::S2VEC: return "X86ISD::S2VEC";
5242 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5243 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5244 case X86ISD::FMAX: return "X86ISD::FMAX";
5245 case X86ISD::FMIN: return "X86ISD::FMIN";
5246 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5247 case X86ISD::FRCP: return "X86ISD::FRCP";
5248 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5249 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5250 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
5251 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
5252 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
5256 // isLegalAddressingMode - Return true if the addressing mode represented
5257 // by AM is legal for this target, for a load/store of the specified type.
5258 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5259 const Type *Ty) const {
5260 // X86 supports extremely general addressing modes.
5262 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5263 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5267 // We can only fold this if we don't need an extra load.
5268 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5271 // X86-64 only supports addr of globals in small code model.
5272 if (Subtarget->is64Bit()) {
5273 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5275 // If lower 4G is not available, then we must use rip-relative addressing.
5276 if (AM.BaseOffs || AM.Scale > 1)
5287 // These scales always work.
5292 // These scales are formed with basereg+scalereg. Only accept if there is
5297 default: // Other stuff never works.
5305 bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5306 if (!Ty1->isInteger() || !Ty2->isInteger())
5308 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5309 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5310 if (NumBits1 <= NumBits2)
5312 return Subtarget->is64Bit() || NumBits1 < 64;
5315 bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5316 MVT::ValueType VT2) const {
5317 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5319 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5320 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5321 if (NumBits1 <= NumBits2)
5323 return Subtarget->is64Bit() || NumBits1 < 64;
5326 /// isShuffleMaskLegal - Targets can use this to indicate that they only
5327 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5328 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5329 /// are assumed to be legal.
5331 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5332 // Only do shuffles on 128-bit vector types for now.
5333 if (MVT::getSizeInBits(VT) == 64) return false;
5334 return (Mask.Val->getNumOperands() <= 4 ||
5335 isIdentityMask(Mask.Val) ||
5336 isIdentityMask(Mask.Val, true) ||
5337 isSplatMask(Mask.Val) ||
5338 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5339 X86::isUNPCKLMask(Mask.Val) ||
5340 X86::isUNPCKHMask(Mask.Val) ||
5341 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5342 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5345 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5347 SelectionDAG &DAG) const {
5348 unsigned NumElts = BVOps.size();
5349 // Only do shuffles on 128-bit vector types for now.
5350 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5351 if (NumElts == 2) return true;
5353 return (isMOVLMask(&BVOps[0], 4) ||
5354 isCommutedMOVL(&BVOps[0], 4, true) ||
5355 isSHUFPMask(&BVOps[0], 4) ||
5356 isCommutedSHUFP(&BVOps[0], 4));
5361 //===----------------------------------------------------------------------===//
5362 // X86 Scheduler Hooks
5363 //===----------------------------------------------------------------------===//
5366 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5367 MachineBasicBlock *BB) {
5368 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5369 switch (MI->getOpcode()) {
5370 default: assert(false && "Unexpected instr type to insert");
5371 case X86::CMOV_FR32:
5372 case X86::CMOV_FR64:
5373 case X86::CMOV_V4F32:
5374 case X86::CMOV_V2F64:
5375 case X86::CMOV_V2I64: {
5376 // To "insert" a SELECT_CC instruction, we actually have to insert the
5377 // diamond control-flow pattern. The incoming instruction knows the
5378 // destination vreg to set, the condition code register to branch on, the
5379 // true/false values to select between, and a branch opcode to use.
5380 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5381 ilist<MachineBasicBlock>::iterator It = BB;
5387 // cmpTY ccX, r1, r2
5389 // fallthrough --> copy0MBB
5390 MachineBasicBlock *thisMBB = BB;
5391 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5392 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5394 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5395 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5396 MachineFunction *F = BB->getParent();
5397 F->getBasicBlockList().insert(It, copy0MBB);
5398 F->getBasicBlockList().insert(It, sinkMBB);
5399 // Update machine-CFG edges by first adding all successors of the current
5400 // block to the new block which will contain the Phi node for the select.
5401 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5402 e = BB->succ_end(); i != e; ++i)
5403 sinkMBB->addSuccessor(*i);
5404 // Next, remove all successors of the current block, and add the true
5405 // and fallthrough blocks as its successors.
5406 while(!BB->succ_empty())
5407 BB->removeSuccessor(BB->succ_begin());
5408 BB->addSuccessor(copy0MBB);
5409 BB->addSuccessor(sinkMBB);
5412 // %FalseValue = ...
5413 // # fallthrough to sinkMBB
5416 // Update machine-CFG edges
5417 BB->addSuccessor(sinkMBB);
5420 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5423 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5424 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5425 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5427 delete MI; // The pseudo instruction is gone now.
5431 case X86::FP32_TO_INT16_IN_MEM:
5432 case X86::FP32_TO_INT32_IN_MEM:
5433 case X86::FP32_TO_INT64_IN_MEM:
5434 case X86::FP64_TO_INT16_IN_MEM:
5435 case X86::FP64_TO_INT32_IN_MEM:
5436 case X86::FP64_TO_INT64_IN_MEM:
5437 case X86::FP80_TO_INT16_IN_MEM:
5438 case X86::FP80_TO_INT32_IN_MEM:
5439 case X86::FP80_TO_INT64_IN_MEM: {
5440 // Change the floating point control register to use "round towards zero"
5441 // mode when truncating to an integer value.
5442 MachineFunction *F = BB->getParent();
5443 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5444 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5446 // Load the old value of the high byte of the control word...
5448 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
5449 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5451 // Set the high part to be round to zero...
5452 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5455 // Reload the modified control word now...
5456 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5458 // Restore the memory image of control word to original value
5459 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5462 // Get the X86 opcode to use.
5464 switch (MI->getOpcode()) {
5465 default: assert(0 && "illegal opcode!");
5466 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5467 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5468 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5469 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5470 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5471 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
5472 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5473 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5474 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
5478 MachineOperand &Op = MI->getOperand(0);
5479 if (Op.isRegister()) {
5480 AM.BaseType = X86AddressMode::RegBase;
5481 AM.Base.Reg = Op.getReg();
5483 AM.BaseType = X86AddressMode::FrameIndexBase;
5484 AM.Base.FrameIndex = Op.getFrameIndex();
5486 Op = MI->getOperand(1);
5487 if (Op.isImmediate())
5488 AM.Scale = Op.getImm();
5489 Op = MI->getOperand(2);
5490 if (Op.isImmediate())
5491 AM.IndexReg = Op.getImm();
5492 Op = MI->getOperand(3);
5493 if (Op.isGlobalAddress()) {
5494 AM.GV = Op.getGlobal();
5496 AM.Disp = Op.getImm();
5498 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5499 .addReg(MI->getOperand(4).getReg());
5501 // Reload the original control word now.
5502 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5504 delete MI; // The pseudo instruction is gone now.
5510 //===----------------------------------------------------------------------===//
5511 // X86 Optimization Hooks
5512 //===----------------------------------------------------------------------===//
5514 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5516 uint64_t &KnownZero,
5518 const SelectionDAG &DAG,
5519 unsigned Depth) const {
5520 unsigned Opc = Op.getOpcode();
5521 assert((Opc >= ISD::BUILTIN_OP_END ||
5522 Opc == ISD::INTRINSIC_WO_CHAIN ||
5523 Opc == ISD::INTRINSIC_W_CHAIN ||
5524 Opc == ISD::INTRINSIC_VOID) &&
5525 "Should use MaskedValueIsZero if you don't know whether Op"
5526 " is a target node!");
5528 KnownZero = KnownOne = 0; // Don't know anything.
5532 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5537 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
5538 /// element of the result of the vector shuffle.
5539 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5540 MVT::ValueType VT = N->getValueType(0);
5541 SDOperand PermMask = N->getOperand(2);
5542 unsigned NumElems = PermMask.getNumOperands();
5543 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5545 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5547 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5548 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5549 SDOperand Idx = PermMask.getOperand(i);
5550 if (Idx.getOpcode() == ISD::UNDEF)
5551 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5552 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5557 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5558 /// node is a GlobalAddress + an offset.
5559 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5560 unsigned Opc = N->getOpcode();
5561 if (Opc == X86ISD::Wrapper) {
5562 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5563 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5566 } else if (Opc == ISD::ADD) {
5567 SDOperand N1 = N->getOperand(0);
5568 SDOperand N2 = N->getOperand(1);
5569 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5570 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5572 Offset += V->getSignExtended();
5575 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5576 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5578 Offset += V->getSignExtended();
5586 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
5588 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5589 MachineFrameInfo *MFI) {
5590 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5593 SDOperand Loc = N->getOperand(1);
5594 SDOperand BaseLoc = Base->getOperand(1);
5595 if (Loc.getOpcode() == ISD::FrameIndex) {
5596 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5598 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5599 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5600 int FS = MFI->getObjectSize(FI);
5601 int BFS = MFI->getObjectSize(BFI);
5602 if (FS != BFS || FS != Size) return false;
5603 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5605 GlobalValue *GV1 = NULL;
5606 GlobalValue *GV2 = NULL;
5607 int64_t Offset1 = 0;
5608 int64_t Offset2 = 0;
5609 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5610 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5611 if (isGA1 && isGA2 && GV1 == GV2)
5612 return Offset1 == (Offset2 + Dist*Size);
5618 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5619 const X86Subtarget *Subtarget) {
5622 if (isGAPlusOffset(Base, GV, Offset))
5623 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5625 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
5626 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
5628 // Fixed objects do not specify alignment, however the offsets are known.
5629 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5630 (MFI->getObjectOffset(BFI) % 16) == 0);
5632 return MFI->getObjectAlignment(BFI) >= 16;
5638 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5639 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5640 /// if the load addresses are consecutive, non-overlapping, and in the right
5642 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5643 const X86Subtarget *Subtarget) {
5644 MachineFunction &MF = DAG.getMachineFunction();
5645 MachineFrameInfo *MFI = MF.getFrameInfo();
5646 MVT::ValueType VT = N->getValueType(0);
5647 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5648 SDOperand PermMask = N->getOperand(2);
5649 int NumElems = (int)PermMask.getNumOperands();
5650 SDNode *Base = NULL;
5651 for (int i = 0; i < NumElems; ++i) {
5652 SDOperand Idx = PermMask.getOperand(i);
5653 if (Idx.getOpcode() == ISD::UNDEF) {
5654 if (!Base) return SDOperand();
5657 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5658 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5662 else if (!isConsecutiveLoad(Arg.Val, Base,
5663 i, MVT::getSizeInBits(EVT)/8,MFI))
5668 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
5669 LoadSDNode *LD = cast<LoadSDNode>(Base);
5671 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5672 LD->getSrcValueOffset(), LD->isVolatile());
5674 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5675 LD->getSrcValueOffset(), LD->isVolatile(),
5676 LD->getAlignment());
5680 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5681 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5682 const X86Subtarget *Subtarget) {
5683 SDOperand Cond = N->getOperand(0);
5685 // If we have SSE[12] support, try to form min/max nodes.
5686 if (Subtarget->hasSSE2() &&
5687 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5688 if (Cond.getOpcode() == ISD::SETCC) {
5689 // Get the LHS/RHS of the select.
5690 SDOperand LHS = N->getOperand(1);
5691 SDOperand RHS = N->getOperand(2);
5692 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5694 unsigned Opcode = 0;
5695 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5698 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5701 if (!UnsafeFPMath) break;
5703 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5705 Opcode = X86ISD::FMIN;
5708 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5711 if (!UnsafeFPMath) break;
5713 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5715 Opcode = X86ISD::FMAX;
5718 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5721 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5724 if (!UnsafeFPMath) break;
5726 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5728 Opcode = X86ISD::FMIN;
5731 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5734 if (!UnsafeFPMath) break;
5736 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5738 Opcode = X86ISD::FMAX;
5744 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5753 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5754 DAGCombinerInfo &DCI) const {
5755 SelectionDAG &DAG = DCI.DAG;
5756 switch (N->getOpcode()) {
5758 case ISD::VECTOR_SHUFFLE:
5759 return PerformShuffleCombine(N, DAG, Subtarget);
5761 return PerformSELECTCombine(N, DAG, Subtarget);
5767 //===----------------------------------------------------------------------===//
5768 // X86 Inline Assembly Support
5769 //===----------------------------------------------------------------------===//
5771 /// getConstraintType - Given a constraint letter, return the type of
5772 /// constraint it is for this target.
5773 X86TargetLowering::ConstraintType
5774 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5775 if (Constraint.size() == 1) {
5776 switch (Constraint[0]) {
5785 return C_RegisterClass;
5790 return TargetLowering::getConstraintType(Constraint);
5793 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5794 /// vector. If it is invalid, don't add anything to Ops.
5795 void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5797 std::vector<SDOperand>&Ops,
5798 SelectionDAG &DAG) {
5799 SDOperand Result(0, 0);
5801 switch (Constraint) {
5804 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
5805 if (C->getValue() <= 31) {
5806 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5812 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
5813 if (C->getValue() <= 255) {
5814 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5820 // Literal immediates are always ok.
5821 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5822 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5826 // If we are in non-pic codegen mode, we allow the address of a global (with
5827 // an optional displacement) to be used with 'i'.
5828 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5831 // Match either (GA) or (GA+C)
5833 Offset = GA->getOffset();
5834 } else if (Op.getOpcode() == ISD::ADD) {
5835 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5836 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5838 Offset = GA->getOffset()+C->getValue();
5840 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5841 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5843 Offset = GA->getOffset()+C->getValue();
5850 // If addressing this global requires a load (e.g. in PIC mode), we can't
5852 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5856 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5862 // Otherwise, not valid for this mode.
5868 Ops.push_back(Result);
5871 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
5874 std::vector<unsigned> X86TargetLowering::
5875 getRegClassForInlineAsmConstraint(const std::string &Constraint,
5876 MVT::ValueType VT) const {
5877 if (Constraint.size() == 1) {
5878 // FIXME: not handling fp-stack yet!
5879 switch (Constraint[0]) { // GCC X86 Constraint Letters
5880 default: break; // Unknown constraint letter
5881 case 'A': // EAX/EDX
5882 if (VT == MVT::i32 || VT == MVT::i64)
5883 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5885 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5888 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5889 else if (VT == MVT::i16)
5890 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5891 else if (VT == MVT::i8)
5892 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
5893 else if (VT == MVT::i64)
5894 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
5899 return std::vector<unsigned>();
5902 std::pair<unsigned, const TargetRegisterClass*>
5903 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5904 MVT::ValueType VT) const {
5905 // First, see if this is a constraint that directly corresponds to an LLVM
5907 if (Constraint.size() == 1) {
5908 // GCC Constraint Letters
5909 switch (Constraint[0]) {
5911 case 'r': // GENERAL_REGS
5912 case 'R': // LEGACY_REGS
5913 case 'l': // INDEX_REGS
5914 if (VT == MVT::i64 && Subtarget->is64Bit())
5915 return std::make_pair(0U, X86::GR64RegisterClass);
5917 return std::make_pair(0U, X86::GR32RegisterClass);
5918 else if (VT == MVT::i16)
5919 return std::make_pair(0U, X86::GR16RegisterClass);
5920 else if (VT == MVT::i8)
5921 return std::make_pair(0U, X86::GR8RegisterClass);
5923 case 'y': // MMX_REGS if MMX allowed.
5924 if (!Subtarget->hasMMX()) break;
5925 return std::make_pair(0U, X86::VR64RegisterClass);
5927 case 'Y': // SSE_REGS if SSE2 allowed
5928 if (!Subtarget->hasSSE2()) break;
5930 case 'x': // SSE_REGS if SSE1 allowed
5931 if (!Subtarget->hasSSE1()) break;
5935 // Scalar SSE types.
5938 return std::make_pair(0U, X86::FR32RegisterClass);
5941 return std::make_pair(0U, X86::FR64RegisterClass);
5949 return std::make_pair(0U, X86::VR128RegisterClass);
5955 // Use the default implementation in TargetLowering to convert the register
5956 // constraint into a member of a register class.
5957 std::pair<unsigned, const TargetRegisterClass*> Res;
5958 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5960 // Not found as a standard register?
5961 if (Res.second == 0) {
5962 // GCC calls "st(0)" just plain "st".
5963 if (StringsEqualNoCase("{st}", Constraint)) {
5964 Res.first = X86::ST0;
5965 Res.second = X86::RFP80RegisterClass;
5971 // Otherwise, check to see if this is a register class of the wrong value
5972 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5973 // turn into {ax},{dx}.
5974 if (Res.second->hasType(VT))
5975 return Res; // Correct type already, nothing to do.
5977 // All of the single-register GCC register classes map their values onto
5978 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5979 // really want an 8-bit or 32-bit register, map to the appropriate register
5980 // class and return the appropriate register.
5981 if (Res.second != X86::GR16RegisterClass)
5984 if (VT == MVT::i8) {
5985 unsigned DestReg = 0;
5986 switch (Res.first) {
5988 case X86::AX: DestReg = X86::AL; break;
5989 case X86::DX: DestReg = X86::DL; break;
5990 case X86::CX: DestReg = X86::CL; break;
5991 case X86::BX: DestReg = X86::BL; break;
5994 Res.first = DestReg;
5995 Res.second = Res.second = X86::GR8RegisterClass;
5997 } else if (VT == MVT::i32) {
5998 unsigned DestReg = 0;
5999 switch (Res.first) {
6001 case X86::AX: DestReg = X86::EAX; break;
6002 case X86::DX: DestReg = X86::EDX; break;
6003 case X86::CX: DestReg = X86::ECX; break;
6004 case X86::BX: DestReg = X86::EBX; break;
6005 case X86::SI: DestReg = X86::ESI; break;
6006 case X86::DI: DestReg = X86::EDI; break;
6007 case X86::BP: DestReg = X86::EBP; break;
6008 case X86::SP: DestReg = X86::ESP; break;
6011 Res.first = DestReg;
6012 Res.second = Res.second = X86::GR32RegisterClass;
6014 } else if (VT == MVT::i64) {
6015 unsigned DestReg = 0;
6016 switch (Res.first) {
6018 case X86::AX: DestReg = X86::RAX; break;
6019 case X86::DX: DestReg = X86::RDX; break;
6020 case X86::CX: DestReg = X86::RCX; break;
6021 case X86::BX: DestReg = X86::RBX; break;
6022 case X86::SI: DestReg = X86::RSI; break;
6023 case X86::DI: DestReg = X86::RDI; break;
6024 case X86::BP: DestReg = X86::RBP; break;
6025 case X86::SP: DestReg = X86::RSP; break;
6028 Res.first = DestReg;
6029 Res.second = Res.second = X86::GR64RegisterClass;