1 //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/BitVector.h"
27 #include "llvm/ADT/VectorExtras.h"
28 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
29 #include "llvm/CodeGen/CallingConvLower.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/PseudoSourceValue.h"
36 #include "llvm/CodeGen/SelectionDAG.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Target/TargetOptions.h"
40 #include "llvm/ADT/SmallSet.h"
41 #include "llvm/ADT/StringExtras.h"
42 #include "llvm/ParamAttrsList.h"
45 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
47 Subtarget = &TM.getSubtarget<X86Subtarget>();
48 X86ScalarSSEf64 = Subtarget->hasSSE2();
49 X86ScalarSSEf32 = Subtarget->hasSSE1();
50 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
54 RegInfo = TM.getRegisterInfo();
56 // Set up the TargetLowering object.
58 // X86 is weird, it always uses i8 for shift amounts and setcc results.
59 setShiftAmountType(MVT::i8);
60 setSetCCResultContents(ZeroOrOneSetCCResult);
61 setSchedulingPreference(SchedulingForRegPressure);
62 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
63 setStackPointerRegisterToSaveRestore(X86StackPtr);
65 if (Subtarget->isTargetDarwin()) {
66 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
67 setUseUnderscoreSetJmp(false);
68 setUseUnderscoreLongJmp(false);
69 } else if (Subtarget->isTargetMingw()) {
70 // MS runtime is weird: it exports _setjmp, but longjmp!
71 setUseUnderscoreSetJmp(true);
72 setUseUnderscoreLongJmp(false);
74 setUseUnderscoreSetJmp(true);
75 setUseUnderscoreLongJmp(true);
78 // Set up the register classes.
79 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
80 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
81 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
82 if (Subtarget->is64Bit())
83 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
85 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
87 // We don't accept any truncstore of integer registers.
88 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
89 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
91 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
98 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
101 if (Subtarget->is64Bit()) {
102 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
103 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
106 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
107 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
112 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
115 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
116 // SSE has no i16 to fp conversion, only i32
117 if (X86ScalarSSEf32) {
118 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
119 // f32 and f64 cases are Legal, f80 case is not
120 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
123 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
126 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
127 // are Legal, f80 is custom lowered.
128 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
129 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
131 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
134 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136 if (X86ScalarSSEf32) {
137 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
138 // f32 and f64 cases are Legal, f80 case is not
139 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
141 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
142 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
145 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
148 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151 if (Subtarget->is64Bit()) {
152 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
153 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
155 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
156 // Expand FP_TO_UINT into a select.
157 // FIXME: We would like to use a Custom expander here eventually to do
158 // the optimal thing for SSE vs. the default expansion in the legalizer.
159 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 // With SSE3 we can use fisttpll to convert to a signed i64.
162 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
165 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
166 if (!X86ScalarSSEf64) {
167 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
168 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
171 // Scalar integer divide and remainder are lowered to use operations that
172 // produce two results, to match the available instructions. This exposes
173 // the two-result form to trivial CSE, which is able to combine x/y and x%y
174 // into a single instruction.
176 // Scalar integer multiply-high is also lowered to use two-result
177 // operations, to match the available instructions. However, plain multiply
178 // (low) operations are left as Legal, as there are single-result
179 // instructions for this in x86. Using the two-result multiply instructions
180 // when both high and low results are needed must be arranged by dagcombine.
181 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
182 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
183 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
184 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::SREM , MVT::i8 , Expand);
186 setOperationAction(ISD::UREM , MVT::i8 , Expand);
187 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
188 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
189 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
190 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::SREM , MVT::i16 , Expand);
192 setOperationAction(ISD::UREM , MVT::i16 , Expand);
193 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
194 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
195 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
196 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::SREM , MVT::i32 , Expand);
198 setOperationAction(ISD::UREM , MVT::i32 , Expand);
199 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
200 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
201 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
202 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::SREM , MVT::i64 , Expand);
204 setOperationAction(ISD::UREM , MVT::i64 , Expand);
206 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
207 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
208 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
209 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
210 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
211 if (Subtarget->is64Bit())
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
216 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
217 setOperationAction(ISD::FREM , MVT::f32 , Expand);
218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
219 setOperationAction(ISD::FREM , MVT::f80 , Expand);
220 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
222 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
223 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
225 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
226 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
227 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
228 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
229 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
230 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
231 if (Subtarget->is64Bit()) {
232 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
233 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
234 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
237 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
238 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
240 // These should be promoted to a larger select which is supported.
241 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
242 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
243 // X86 wants to expand cmov itself.
244 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
245 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
248 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
254 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
257 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
259 // X86 ret instruction may pop stack.
260 setOperationAction(ISD::RET , MVT::Other, Custom);
261 if (!Subtarget->is64Bit())
262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
269 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
270 if (Subtarget->is64Bit()) {
271 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
272 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
273 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
274 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
276 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
277 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
278 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
279 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
280 if (Subtarget->is64Bit()) {
281 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
282 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
283 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
285 // X86 wants to expand memset / memcpy itself.
286 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
287 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
289 if (Subtarget->hasSSE1())
290 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
292 if (!Subtarget->hasSSE2())
293 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
295 setOperationAction(ISD::ATOMIC_LCS , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_LCS , MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_LCS , MVT::i32, Custom);
298 setOperationAction(ISD::ATOMIC_LCS , MVT::i64, Custom);
300 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
301 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
302 // FIXME - use subtarget debug flags
303 if (!Subtarget->isTargetDarwin() &&
304 !Subtarget->isTargetELF() &&
305 !Subtarget->isTargetCygMing())
306 setOperationAction(ISD::LABEL, MVT::Other, Expand);
308 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
309 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
310 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
311 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
312 if (Subtarget->is64Bit()) {
314 setExceptionPointerRegister(X86::RAX);
315 setExceptionSelectorRegister(X86::RDX);
317 setExceptionPointerRegister(X86::EAX);
318 setExceptionSelectorRegister(X86::EDX);
320 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
322 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
324 setOperationAction(ISD::TRAP, MVT::Other, Legal);
326 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
327 setOperationAction(ISD::VASTART , MVT::Other, Custom);
328 setOperationAction(ISD::VAARG , MVT::Other, Expand);
329 setOperationAction(ISD::VAEND , MVT::Other, Expand);
330 if (Subtarget->is64Bit())
331 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
333 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
335 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
336 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
337 if (Subtarget->is64Bit())
338 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
339 if (Subtarget->isTargetCygMing())
340 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
342 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
344 if (X86ScalarSSEf64) {
345 // f32 and f64 use SSE.
346 // Set up the FP register classes.
347 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
348 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
350 // Use ANDPD to simulate FABS.
351 setOperationAction(ISD::FABS , MVT::f64, Custom);
352 setOperationAction(ISD::FABS , MVT::f32, Custom);
354 // Use XORP to simulate FNEG.
355 setOperationAction(ISD::FNEG , MVT::f64, Custom);
356 setOperationAction(ISD::FNEG , MVT::f32, Custom);
358 // Use ANDPD and ORPD to simulate FCOPYSIGN.
359 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
360 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
362 // We don't support sin/cos/fmod
363 setOperationAction(ISD::FSIN , MVT::f64, Expand);
364 setOperationAction(ISD::FCOS , MVT::f64, Expand);
365 setOperationAction(ISD::FSIN , MVT::f32, Expand);
366 setOperationAction(ISD::FCOS , MVT::f32, Expand);
368 // Expand FP immediates into loads from the stack, except for the special
370 addLegalFPImmediate(APFloat(+0.0)); // xorpd
371 addLegalFPImmediate(APFloat(+0.0f)); // xorps
373 // Floating truncations from f80 and extensions to f80 go through memory.
374 // If optimizing, we lie about this though and handle it in
375 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
377 setConvertAction(MVT::f32, MVT::f80, Expand);
378 setConvertAction(MVT::f64, MVT::f80, Expand);
379 setConvertAction(MVT::f80, MVT::f32, Expand);
380 setConvertAction(MVT::f80, MVT::f64, Expand);
382 } else if (X86ScalarSSEf32) {
383 // Use SSE for f32, x87 for f64.
384 // Set up the FP register classes.
385 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
386 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
388 // Use ANDPS to simulate FABS.
389 setOperationAction(ISD::FABS , MVT::f32, Custom);
391 // Use XORP to simulate FNEG.
392 setOperationAction(ISD::FNEG , MVT::f32, Custom);
394 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
396 // Use ANDPS and ORPS to simulate FCOPYSIGN.
397 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
398 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
400 // We don't support sin/cos/fmod
401 setOperationAction(ISD::FSIN , MVT::f32, Expand);
402 setOperationAction(ISD::FCOS , MVT::f32, Expand);
404 // Special cases we handle for FP constants.
405 addLegalFPImmediate(APFloat(+0.0f)); // xorps
406 addLegalFPImmediate(APFloat(+0.0)); // FLD0
407 addLegalFPImmediate(APFloat(+1.0)); // FLD1
408 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
409 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
411 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
412 // this though and handle it in InstructionSelectPreprocess so that
413 // dagcombine2 can hack on these.
415 setConvertAction(MVT::f32, MVT::f64, Expand);
416 setConvertAction(MVT::f32, MVT::f80, Expand);
417 setConvertAction(MVT::f80, MVT::f32, Expand);
418 setConvertAction(MVT::f64, MVT::f32, Expand);
419 // And x87->x87 truncations also.
420 setConvertAction(MVT::f80, MVT::f64, Expand);
424 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
425 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
428 // f32 and f64 in x87.
429 // Set up the FP register classes.
430 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
431 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
433 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
434 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
435 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
436 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
438 // Floating truncations go through memory. If optimizing, we lie about
439 // this though and handle it in InstructionSelectPreprocess so that
440 // dagcombine2 can hack on these.
442 setConvertAction(MVT::f80, MVT::f32, Expand);
443 setConvertAction(MVT::f64, MVT::f32, Expand);
444 setConvertAction(MVT::f80, MVT::f64, Expand);
448 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
449 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
451 addLegalFPImmediate(APFloat(+0.0)); // FLD0
452 addLegalFPImmediate(APFloat(+1.0)); // FLD1
453 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
454 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
455 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
456 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
457 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
458 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
461 // Long double always uses X87.
462 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
463 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
464 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
466 APFloat TmpFlt(+0.0);
467 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
468 addLegalFPImmediate(TmpFlt); // FLD0
470 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
471 APFloat TmpFlt2(+1.0);
472 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
473 addLegalFPImmediate(TmpFlt2); // FLD1
474 TmpFlt2.changeSign();
475 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
479 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
480 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
483 // Always use a library call for pow.
484 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
485 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
486 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
488 // First set operation action for all vector types to expand. Then we
489 // will selectively turn on ones that can be effectively codegen'd.
490 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
491 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
492 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
521 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
523 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
524 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
525 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
526 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
527 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
528 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
531 if (Subtarget->hasMMX()) {
532 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
533 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
534 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
535 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
537 // FIXME: add MMX packed arithmetics
539 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
540 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
541 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
542 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
544 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
545 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
546 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
547 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
549 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
550 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
552 setOperationAction(ISD::AND, MVT::v8i8, Promote);
553 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
554 setOperationAction(ISD::AND, MVT::v4i16, Promote);
555 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
556 setOperationAction(ISD::AND, MVT::v2i32, Promote);
557 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
558 setOperationAction(ISD::AND, MVT::v1i64, Legal);
560 setOperationAction(ISD::OR, MVT::v8i8, Promote);
561 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
562 setOperationAction(ISD::OR, MVT::v4i16, Promote);
563 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
564 setOperationAction(ISD::OR, MVT::v2i32, Promote);
565 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
566 setOperationAction(ISD::OR, MVT::v1i64, Legal);
568 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
569 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
570 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
571 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
572 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
573 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
574 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
576 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
577 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
578 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
579 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
580 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
581 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
582 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
584 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
585 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
586 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
587 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
589 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
590 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
591 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
592 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
594 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
595 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
596 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
599 if (Subtarget->hasSSE1()) {
600 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
602 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
603 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
604 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
605 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
606 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
607 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
608 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
609 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
610 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
611 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
612 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
615 if (Subtarget->hasSSE2()) {
616 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
617 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
618 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
619 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
620 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
622 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
623 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
624 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
625 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
626 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
627 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
628 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
629 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
630 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
631 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
632 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
633 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
634 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
635 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
636 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
638 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
641 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
642 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
644 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
645 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
646 // Do not attempt to custom lower non-power-of-2 vectors
647 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
649 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
650 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
651 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
653 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
654 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
655 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
656 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
657 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
658 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
659 if (Subtarget->is64Bit()) {
660 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
661 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
664 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
665 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
666 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
667 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
668 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
669 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
670 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
671 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
672 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
673 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
674 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
675 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
678 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
680 // Custom lower v2i64 and v2f64 selects.
681 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
682 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
683 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
684 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
687 if (Subtarget->hasSSE41()) {
688 // FIXME: Do we need to handle scalar-to-vector here?
689 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
691 // i8 and i16 vectors are custom , because the source register and source
692 // source memory operand types are not the same width. f32 vectors are
693 // custom since the immediate controlling the insert encodes additional
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
697 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
698 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
702 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
703 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
705 if (Subtarget->is64Bit()) {
706 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
707 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
711 // We want to custom lower some of our intrinsics.
712 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
714 // We have target-specific dag combine patterns for the following nodes:
715 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
716 setTargetDAGCombine(ISD::SELECT);
717 setTargetDAGCombine(ISD::STORE);
719 computeRegisterProperties();
721 // FIXME: These should be based on subtarget info. Plus, the values should
722 // be smaller when we are in optimizing for size mode.
723 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
724 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
725 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
726 allowUnalignedMemoryAccesses = true; // x86 supports it!
727 setPrefLoopAlignment(16);
732 X86TargetLowering::getSetCCResultType(const SDOperand &) const {
737 /// getMaxByValAlign - Helper for getByValTypeAlignment to determine
738 /// the desired ByVal argument alignment.
739 static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
742 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
743 if (VTy->getBitWidth() == 128)
745 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
746 unsigned EltAlign = 0;
747 getMaxByValAlign(ATy->getElementType(), EltAlign);
748 if (EltAlign > MaxAlign)
750 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
751 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
752 unsigned EltAlign = 0;
753 getMaxByValAlign(STy->getElementType(i), EltAlign);
754 if (EltAlign > MaxAlign)
763 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
764 /// function arguments in the caller parameter area. For X86, aggregates
765 /// that contain SSE vectors are placed at 16-byte boundaries while the rest
766 /// are at 4-byte boundaries.
767 unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
768 if (Subtarget->is64Bit())
769 return getTargetData()->getABITypeAlignment(Ty);
771 if (Subtarget->hasSSE1())
772 getMaxByValAlign(Ty, Align);
776 /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
778 SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
779 SelectionDAG &DAG) const {
780 if (usesGlobalOffsetTable())
781 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
782 if (!Subtarget->isPICStyleRIPRel())
783 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
787 //===----------------------------------------------------------------------===//
788 // Return Value Calling Convention Implementation
789 //===----------------------------------------------------------------------===//
791 #include "X86GenCallingConv.inc"
793 /// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
794 /// exists skip possible ISD:TokenFactor.
795 static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
796 if (Chain.getOpcode() == X86ISD::TAILCALL) {
798 } else if (Chain.getOpcode() == ISD::TokenFactor) {
799 if (Chain.getNumOperands() &&
800 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
801 return Chain.getOperand(0);
806 /// LowerRET - Lower an ISD::RET node.
807 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
808 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
810 SmallVector<CCValAssign, 16> RVLocs;
811 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
812 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
813 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
814 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
816 // If this is the first return lowered for this function, add the regs to the
817 // liveout set for the function.
818 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
819 for (unsigned i = 0; i != RVLocs.size(); ++i)
820 if (RVLocs[i].isRegLoc())
821 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
823 SDOperand Chain = Op.getOperand(0);
825 // Handle tail call return.
826 Chain = GetPossiblePreceedingTailCall(Chain);
827 if (Chain.getOpcode() == X86ISD::TAILCALL) {
828 SDOperand TailCall = Chain;
829 SDOperand TargetAddress = TailCall.getOperand(1);
830 SDOperand StackAdjustment = TailCall.getOperand(2);
831 assert(((TargetAddress.getOpcode() == ISD::Register &&
832 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
833 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
834 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
835 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
836 "Expecting an global address, external symbol, or register");
837 assert(StackAdjustment.getOpcode() == ISD::Constant &&
838 "Expecting a const value");
840 SmallVector<SDOperand,8> Operands;
841 Operands.push_back(Chain.getOperand(0));
842 Operands.push_back(TargetAddress);
843 Operands.push_back(StackAdjustment);
844 // Copy registers used by the call. Last operand is a flag so it is not
846 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
847 Operands.push_back(Chain.getOperand(i));
849 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
856 // Copy the result values into the output registers.
857 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
858 RVLocs[0].getLocReg() != X86::ST0) {
859 for (unsigned i = 0; i != RVLocs.size(); ++i) {
860 CCValAssign &VA = RVLocs[i];
861 assert(VA.isRegLoc() && "Can only return in registers!");
862 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
864 Flag = Chain.getValue(1);
867 // We need to handle a destination of ST0 specially, because it isn't really
869 SDOperand Value = Op.getOperand(1);
871 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
872 // This will get legalized into a load/store if it can't get optimized away.
873 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
874 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
876 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
877 SDOperand Ops[] = { Chain, Value };
878 Chain = DAG.getNode(X86ISD::FP_SET_ST0, Tys, Ops, 2);
879 Flag = Chain.getValue(1);
882 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
884 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
886 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
890 /// LowerCallResult - Lower the result values of an ISD::CALL into the
891 /// appropriate copies out of appropriate physical registers. This assumes that
892 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
893 /// being lowered. The returns a SDNode with the same number of values as the
895 SDNode *X86TargetLowering::
896 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
897 unsigned CallingConv, SelectionDAG &DAG) {
899 // Assign locations to each value returned by this call.
900 SmallVector<CCValAssign, 16> RVLocs;
901 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
902 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
903 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
905 SmallVector<SDOperand, 8> ResultVals;
907 // Copy all of the result registers out of their specified physreg.
908 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
909 for (unsigned i = 0; i != RVLocs.size(); ++i) {
910 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
911 RVLocs[i].getValVT(), InFlag).getValue(1);
912 InFlag = Chain.getValue(2);
913 ResultVals.push_back(Chain.getValue(0));
916 // Copies from the FP stack are special, as ST0 isn't a valid register
917 // before the fp stackifier runs.
919 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
920 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
921 // the specified value type.
922 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
923 if (isScalarFPTypeInSSEReg(GetResultTy))
924 GetResultTy = MVT::f80;
925 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
926 SDOperand GROps[] = { Chain, InFlag };
927 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_ST0, Tys, GROps, 2);
928 Chain = RetVal.getValue(1);
929 InFlag = RetVal.getValue(2);
931 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
933 if (GetResultTy != RVLocs[0].getValVT())
934 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
935 // This truncation won't change the value.
936 DAG.getIntPtrConstant(1));
938 ResultVals.push_back(RetVal);
941 // Merge everything together with a MERGE_VALUES node.
942 ResultVals.push_back(Chain);
943 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
944 &ResultVals[0], ResultVals.size()).Val;
947 /// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
948 /// ISD::CALL where the results are known to be in two 64-bit registers,
949 /// e.g. XMM0 and XMM1. This simplify store the two values back to the
950 /// fixed stack slot allocated for StructRet.
951 SDNode *X86TargetLowering::
952 LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
953 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
954 MVT::ValueType VT, SelectionDAG &DAG) {
955 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
956 Chain = RetVal1.getValue(1);
957 InFlag = RetVal1.getValue(2);
958 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
959 Chain = RetVal2.getValue(1);
960 InFlag = RetVal2.getValue(2);
961 SDOperand FIN = TheCall->getOperand(5);
962 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
963 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
964 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
968 /// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
969 /// where the results are known to be in ST0 and ST1.
970 SDNode *X86TargetLowering::
971 LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
972 SDNode *TheCall, SelectionDAG &DAG) {
973 SmallVector<SDOperand, 8> ResultVals;
974 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
975 SDVTList Tys = DAG.getVTList(VTs, 4);
976 SDOperand Ops[] = { Chain, InFlag };
977 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_ST0_ST1, Tys, Ops, 2);
978 Chain = RetVal.getValue(2);
979 SDOperand FIN = TheCall->getOperand(5);
980 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
981 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
982 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
986 //===----------------------------------------------------------------------===//
987 // C & StdCall & Fast Calling Convention implementation
988 //===----------------------------------------------------------------------===//
989 // StdCall calling convention seems to be standard for many Windows' API
990 // routines and around. It differs from C calling convention just a little:
991 // callee should clean up the stack, not caller. Symbols should be also
992 // decorated in some fancy way :) It doesn't support any vector arguments.
993 // For info on fast calling convention see Fast Calling Convention (tail call)
994 // implementation LowerX86_32FastCCCallTo.
996 /// AddLiveIn - This helper function adds the specified physical register to the
997 /// MachineFunction as a live in value. It also creates a corresponding virtual
999 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1000 const TargetRegisterClass *RC) {
1001 assert(RC->contains(PReg) && "Not the correct regclass!");
1002 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1003 MF.getRegInfo().addLiveIn(PReg, VReg);
1007 /// CallIsStructReturn - Determines whether a CALL node uses struct return
1009 static bool CallIsStructReturn(SDOperand Op) {
1010 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1014 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
1015 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1018 /// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1019 /// return semantics.
1020 static bool ArgsAreStructReturn(SDOperand Op) {
1021 unsigned NumArgs = Op.Val->getNumValues() - 1;
1025 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1026 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1029 /// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1030 /// callee to pop its own arguments. Callee pop is necessary to support tail
1032 bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1033 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1037 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1040 case CallingConv::X86_StdCall:
1041 return !Subtarget->is64Bit();
1042 case CallingConv::X86_FastCall:
1043 return !Subtarget->is64Bit();
1044 case CallingConv::Fast:
1045 return PerformTailCallOpt;
1049 /// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1050 /// FORMAL_ARGUMENTS node.
1051 CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1052 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1054 if (Subtarget->is64Bit()) {
1055 if (CC == CallingConv::Fast && PerformTailCallOpt)
1056 return CC_X86_64_TailCall;
1061 if (CC == CallingConv::X86_FastCall)
1062 return CC_X86_32_FastCall;
1063 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1064 return CC_X86_32_TailCall;
1069 /// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1070 /// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
1072 X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1073 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1074 if (CC == CallingConv::X86_FastCall)
1076 else if (CC == CallingConv::X86_StdCall)
1081 /// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1082 /// possibly be overwritten when lowering the outgoing arguments in a tail
1083 /// call. Currently the implementation of this call is very conservative and
1084 /// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1085 /// virtual registers would be overwritten by direct lowering.
1086 static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1087 MachineFrameInfo * MFI) {
1088 RegisterSDNode * OpReg = NULL;
1089 FrameIndexSDNode * FrameIdxNode = NULL;
1091 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1092 (Op.getOpcode()== ISD::CopyFromReg &&
1093 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1094 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1095 (Op.getOpcode() == ISD::LOAD &&
1096 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1097 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1098 (MFI->getObjectOffset(FrameIdx) >= 0)))
1103 /// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1104 /// in a register before calling.
1105 bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1106 return !IsTailCall && !Is64Bit &&
1107 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1108 Subtarget->isPICStyleGOT();
1112 /// CallRequiresFnAddressInReg - Check whether the call requires the function
1113 /// address to be loaded in a register.
1115 X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1116 return !Is64Bit && IsTailCall &&
1117 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1118 Subtarget->isPICStyleGOT();
1121 /// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1122 /// arguments to force loading and guarantee that arguments sourcing from
1123 /// incomming parameters are not overwriting each other.
1125 CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1126 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1128 MachineFunction &MF,
1129 const TargetLowering * TL) {
1132 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1133 SDOperand Arg = TailCallClobberedVRegs[i].second;
1134 unsigned Idx = TailCallClobberedVRegs[i].first;
1137 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1138 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1139 InFlag = Chain.getValue(1);
1140 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1141 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1142 Chain = Arg.getValue(1);
1143 InFlag = Arg.getValue(2);
1148 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1149 /// by "Src" to address "Dst" with size and alignment information specified by
1150 /// the specific parameter attribute. The copy will be passed as a byval function
1153 CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1154 ISD::ParamFlags::ParamFlagsTy Flags,
1155 SelectionDAG &DAG) {
1156 unsigned Align = ISD::ParamFlags::One <<
1157 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1158 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1159 ISD::ParamFlags::ByValSizeOffs;
1160 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1161 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1162 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
1163 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
1166 SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1167 const CCValAssign &VA,
1168 MachineFrameInfo *MFI,
1170 SDOperand Root, unsigned i) {
1171 // Create the nodes corresponding to a load from this parameter slot.
1172 ISD::ParamFlags::ParamFlagsTy Flags =
1173 cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1174 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
1175 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1176 bool isImmutable = !AlwaysUseMutable && !isByVal;
1178 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1179 // changed with more analysis.
1180 // In case of tail call optimization mark all arguments mutable. Since they
1181 // could be overwritten by lowering of arguments in case of a tail call.
1182 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1183 VA.getLocMemOffset(), isImmutable);
1184 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1187 return DAG.getLoad(VA.getValVT(), Root, FIN,
1188 PseudoSourceValue::getFixedStack(), FI);
1192 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
1193 MachineFunction &MF = DAG.getMachineFunction();
1194 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1196 const Function* Fn = MF.getFunction();
1197 if (Fn->hasExternalLinkage() &&
1198 Subtarget->isTargetCygMing() &&
1199 Fn->getName() == "main")
1200 FuncInfo->setForceFramePointer(true);
1202 // Decorate the function name.
1203 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1205 MachineFrameInfo *MFI = MF.getFrameInfo();
1206 SDOperand Root = Op.getOperand(0);
1207 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1208 unsigned CC = MF.getFunction()->getCallingConv();
1209 bool Is64Bit = Subtarget->is64Bit();
1211 assert(!(isVarArg && CC == CallingConv::Fast) &&
1212 "Var args not supported with calling convention fastcc");
1214 // Assign locations to all of the incoming arguments.
1215 SmallVector<CCValAssign, 16> ArgLocs;
1216 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1217 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
1219 SmallVector<SDOperand, 8> ArgValues;
1220 unsigned LastVal = ~0U;
1221 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1222 CCValAssign &VA = ArgLocs[i];
1223 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1225 assert(VA.getValNo() != LastVal &&
1226 "Don't support value assigned to multiple locs yet");
1227 LastVal = VA.getValNo();
1229 if (VA.isRegLoc()) {
1230 MVT::ValueType RegVT = VA.getLocVT();
1231 TargetRegisterClass *RC;
1232 if (RegVT == MVT::i32)
1233 RC = X86::GR32RegisterClass;
1234 else if (Is64Bit && RegVT == MVT::i64)
1235 RC = X86::GR64RegisterClass;
1236 else if (RegVT == MVT::f32)
1237 RC = X86::FR32RegisterClass;
1238 else if (RegVT == MVT::f64)
1239 RC = X86::FR64RegisterClass;
1241 assert(MVT::isVector(RegVT));
1242 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1243 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1246 RC = X86::VR128RegisterClass;
1249 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1250 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1252 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1253 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1255 if (VA.getLocInfo() == CCValAssign::SExt)
1256 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1257 DAG.getValueType(VA.getValVT()));
1258 else if (VA.getLocInfo() == CCValAssign::ZExt)
1259 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1260 DAG.getValueType(VA.getValVT()));
1262 if (VA.getLocInfo() != CCValAssign::Full)
1263 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1265 // Handle MMX values passed in GPRs.
1266 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1267 MVT::getSizeInBits(RegVT) == 64)
1268 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1270 ArgValues.push_back(ArgValue);
1272 assert(VA.isMemLoc());
1273 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
1277 unsigned StackSize = CCInfo.getNextStackOffset();
1278 // align stack specially for tail calls
1279 if (CC == CallingConv::Fast)
1280 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
1282 // If the function takes variable number of arguments, make a frame index for
1283 // the start of the first vararg value... for expansion of llvm.va_start.
1285 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1286 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1289 static const unsigned GPR64ArgRegs[] = {
1290 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1292 static const unsigned XMMArgRegs[] = {
1293 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1294 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1297 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1298 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1300 // For X86-64, if there are vararg parameters that are passed via
1301 // registers, then we must store them to their spots on the stack so they
1302 // may be loaded by deferencing the result of va_next.
1303 VarArgsGPOffset = NumIntRegs * 8;
1304 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1305 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1307 // Store the integer parameter registers.
1308 SmallVector<SDOperand, 8> MemOps;
1309 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1310 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1311 DAG.getIntPtrConstant(VarArgsGPOffset));
1312 for (; NumIntRegs != 6; ++NumIntRegs) {
1313 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1314 X86::GR64RegisterClass);
1315 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1317 DAG.getStore(Val.getValue(1), Val, FIN,
1318 PseudoSourceValue::getFixedStack(),
1320 MemOps.push_back(Store);
1321 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1322 DAG.getIntPtrConstant(8));
1325 // Now store the XMM (fp + vector) parameter registers.
1326 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1327 DAG.getIntPtrConstant(VarArgsFPOffset));
1328 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1329 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1330 X86::VR128RegisterClass);
1331 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1333 DAG.getStore(Val.getValue(1), Val, FIN,
1334 PseudoSourceValue::getFixedStack(),
1336 MemOps.push_back(Store);
1337 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1338 DAG.getIntPtrConstant(16));
1340 if (!MemOps.empty())
1341 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1342 &MemOps[0], MemOps.size());
1346 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1347 // arguments and the arguments after the retaddr has been pushed are
1349 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1350 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1351 (StackSize & 7) == 0)
1354 ArgValues.push_back(Root);
1356 // Some CCs need callee pop.
1357 if (IsCalleePop(Op)) {
1358 BytesToPopOnReturn = StackSize; // Callee pops everything.
1359 BytesCallerReserves = 0;
1361 BytesToPopOnReturn = 0; // Callee pops nothing.
1362 // If this is an sret function, the return should pop the hidden pointer.
1363 if (!Is64Bit && ArgsAreStructReturn(Op))
1364 BytesToPopOnReturn = 4;
1365 BytesCallerReserves = StackSize;
1369 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1370 if (CC == CallingConv::X86_FastCall)
1371 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1374 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1376 // Return the new list of results.
1377 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1378 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1382 X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1383 const SDOperand &StackPtr,
1384 const CCValAssign &VA,
1387 unsigned LocMemOffset = VA.getLocMemOffset();
1388 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1389 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1390 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1391 ISD::ParamFlags::ParamFlagsTy Flags =
1392 cast<ConstantSDNode>(FlagsOp)->getValue();
1393 if (Flags & ISD::ParamFlags::ByVal) {
1394 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
1396 return DAG.getStore(Chain, Arg, PtrOff,
1397 PseudoSourceValue::getStack(), LocMemOffset);
1400 /// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1401 /// struct return call to the specified function. X86-64 ABI specifies
1402 /// some SRet calls are actually returned in registers. Since current
1403 /// LLVM cannot represent multi-value calls, they are represent as
1404 /// calls where the results are passed in a hidden struct provided by
1405 /// the caller. This function examines the type of the struct to
1406 /// determine the correct way to implement the call.
1408 X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1409 // FIXME: Disabled for now.
1410 return X86::InMemory;
1412 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1413 const Type *RTy = PTy->getElementType();
1414 unsigned Size = getTargetData()->getABITypeSize(RTy);
1415 if (Size != 16 && Size != 32)
1416 return X86::InMemory;
1419 const StructType *STy = dyn_cast<StructType>(RTy);
1420 if (!STy) return X86::InMemory;
1421 if (STy->getNumElements() == 2 &&
1422 STy->getElementType(0) == Type::X86_FP80Ty &&
1423 STy->getElementType(1) == Type::X86_FP80Ty)
1428 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1430 const Type *STy = I->get();
1431 if (!STy->isFPOrFPVector()) {
1439 return X86::InGPR64;
1442 void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1445 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1446 for (unsigned i = 1; i != NumOps; ++i) {
1447 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1448 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1449 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1450 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1451 cerr << "Call operand #" << i << " has unhandled type "
1452 << MVT::getValueTypeString(ArgVT) << "\n";
1458 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1459 MachineFunction &MF = DAG.getMachineFunction();
1460 MachineFrameInfo * MFI = MF.getFrameInfo();
1461 SDOperand Chain = Op.getOperand(0);
1462 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1463 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1464 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1465 && CC == CallingConv::Fast && PerformTailCallOpt;
1466 SDOperand Callee = Op.getOperand(4);
1467 bool Is64Bit = Subtarget->is64Bit();
1468 bool IsStructRet = CallIsStructReturn(Op);
1470 assert(!(isVarArg && CC == CallingConv::Fast) &&
1471 "Var args not supported with calling convention fastcc");
1473 // Analyze operands of the call, assigning locations to each operand.
1474 SmallVector<CCValAssign, 16> ArgLocs;
1475 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1476 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1478 X86::X86_64SRet SRetMethod = X86::InMemory;
1479 if (Is64Bit && IsStructRet)
1480 // FIXME: We can't figure out type of the sret structure for indirect
1481 // calls. We need to copy more information from CallSite to the ISD::CALL
1483 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1485 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1487 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1488 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1490 if (SRetMethod != X86::InMemory)
1491 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1493 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
1495 // Get a count of how many bytes are to be pushed on the stack.
1496 unsigned NumBytes = CCInfo.getNextStackOffset();
1497 if (CC == CallingConv::Fast)
1498 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
1500 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1501 // arguments and the arguments after the retaddr has been pushed are aligned.
1502 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1503 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1504 (NumBytes & 7) == 0)
1509 // Lower arguments at fp - stackoffset + fpdiff.
1510 unsigned NumBytesCallerPushed =
1511 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1512 FPDiff = NumBytesCallerPushed - NumBytes;
1514 // Set the delta of movement of the returnaddr stackslot.
1515 // But only set if delta is greater than previous delta.
1516 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1517 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1520 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
1522 SDOperand RetAddrFrIdx;
1524 // Adjust the Return address stack slot.
1526 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1527 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1528 // Load the "old" Return address.
1530 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1531 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1535 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1536 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
1537 SmallVector<SDOperand, 8> MemOpChains;
1541 // Walk the register/memloc assignments, inserting copies/loads. For tail
1542 // calls, remember all arguments for later special lowering.
1543 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1544 CCValAssign &VA = ArgLocs[i];
1545 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1547 // Promote the value if needed.
1548 switch (VA.getLocInfo()) {
1549 default: assert(0 && "Unknown loc info!");
1550 case CCValAssign::Full: break;
1551 case CCValAssign::SExt:
1552 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1554 case CCValAssign::ZExt:
1555 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1557 case CCValAssign::AExt:
1558 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1562 if (VA.isRegLoc()) {
1563 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1566 assert(VA.isMemLoc());
1567 if (StackPtr.Val == 0)
1568 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1570 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1572 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1573 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
1578 if (!MemOpChains.empty())
1579 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1580 &MemOpChains[0], MemOpChains.size());
1582 // Build a sequence of copy-to-reg nodes chained together with token chain
1583 // and flag operands which copy the outgoing args into registers.
1585 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1586 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1588 InFlag = Chain.getValue(1);
1591 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1593 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1594 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1595 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1597 InFlag = Chain.getValue(1);
1599 // If we are tail calling and generating PIC/GOT style code load the address
1600 // of the callee into ecx. The value in ecx is used as target of the tail
1601 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1602 // calls on PIC/GOT architectures. Normally we would just put the address of
1603 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1604 // restored (since ebx is callee saved) before jumping to the target@PLT.
1605 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
1606 // Note: The actual moving to ecx is done further down.
1607 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1608 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1609 !G->getGlobal()->hasProtectedVisibility())
1610 Callee = LowerGlobalAddress(Callee, DAG);
1611 else if (isa<ExternalSymbolSDNode>(Callee))
1612 Callee = LowerExternalSymbol(Callee,DAG);
1615 if (Is64Bit && isVarArg) {
1616 // From AMD64 ABI document:
1617 // For calls that may call functions that use varargs or stdargs
1618 // (prototype-less calls or calls to functions containing ellipsis (...) in
1619 // the declaration) %al is used as hidden argument to specify the number
1620 // of SSE registers used. The contents of %al do not need to match exactly
1621 // the number of registers, but must be an ubound on the number of SSE
1622 // registers used and is in the range 0 - 8 inclusive.
1624 // Count the number of XMM registers allocated.
1625 static const unsigned XMMArgRegs[] = {
1626 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1627 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1629 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1631 Chain = DAG.getCopyToReg(Chain, X86::AL,
1632 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1633 InFlag = Chain.getValue(1);
1637 // For tail calls lower the arguments to the 'real' stack slot.
1639 SmallVector<SDOperand, 8> MemOpChains2;
1642 // Do not flag preceeding copytoreg stuff together with the following stuff.
1643 InFlag = SDOperand();
1645 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1648 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1649 CCValAssign &VA = ArgLocs[i];
1650 if (!VA.isRegLoc()) {
1651 assert(VA.isMemLoc());
1652 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1653 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1654 ISD::ParamFlags::ParamFlagsTy Flags =
1655 cast<ConstantSDNode>(FlagsOp)->getValue();
1656 // Create frame index.
1657 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1658 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1659 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1660 FIN = DAG.getFrameIndex(FI, MVT::i32);
1662 // Find virtual register for this argument.
1664 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1665 if (TailCallClobberedVRegs[idx].first==i) {
1666 Arg = TailCallClobberedVRegs[idx].second;
1670 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1671 (Found==true && "No corresponding Argument was found"));
1673 if (Flags & ISD::ParamFlags::ByVal) {
1674 // Copy relative to framepointer.
1675 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
1678 // Store relative to framepointer.
1679 MemOpChains2.push_back(
1680 DAG.getStore(Chain, Arg, FIN,
1681 PseudoSourceValue::getFixedStack(), FI));
1686 if (!MemOpChains2.empty())
1687 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1688 &MemOpChains2[0], MemOpChains2.size());
1690 // Store the return address to the appropriate stack slot.
1692 // Calculate the new stack slot for the return address.
1693 int SlotSize = Is64Bit ? 8 : 4;
1694 int NewReturnAddrFI =
1695 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1696 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1697 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1698 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1699 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1703 // If the callee is a GlobalAddress node (quite common, every direct call is)
1704 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1705 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1706 // We should use extra load for direct calls to dllimported functions in
1708 if ((IsTailCall || !Is64Bit ||
1709 getTargetMachine().getCodeModel() != CodeModel::Large)
1710 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1711 getTargetMachine(), true))
1712 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1713 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1714 if (IsTailCall || !Is64Bit ||
1715 getTargetMachine().getCodeModel() != CodeModel::Large)
1716 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1717 } else if (IsTailCall) {
1718 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1720 Chain = DAG.getCopyToReg(Chain,
1721 DAG.getRegister(Opc, getPointerTy()),
1723 Callee = DAG.getRegister(Opc, getPointerTy());
1724 // Add register as live out.
1725 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
1728 // Returns a chain & a flag for retval copy to use.
1729 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1730 SmallVector<SDOperand, 8> Ops;
1733 Ops.push_back(Chain);
1734 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1735 Ops.push_back(DAG.getIntPtrConstant(0));
1737 Ops.push_back(InFlag);
1738 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1739 InFlag = Chain.getValue(1);
1741 // Returns a chain & a flag for retval copy to use.
1742 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1746 Ops.push_back(Chain);
1747 Ops.push_back(Callee);
1750 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1752 // Add an implicit use GOT pointer in EBX.
1753 if (!IsTailCall && !Is64Bit &&
1754 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1755 Subtarget->isPICStyleGOT())
1756 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1758 // Add argument registers to the end of the list so that they are known live
1760 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1761 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1762 RegsToPass[i].second.getValueType()));
1765 Ops.push_back(InFlag);
1768 assert(InFlag.Val &&
1769 "Flag must be set. Depend on flag being set in LowerRET");
1770 Chain = DAG.getNode(X86ISD::TAILCALL,
1771 Op.Val->getVTList(), &Ops[0], Ops.size());
1773 return SDOperand(Chain.Val, Op.ResNo);
1776 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
1777 InFlag = Chain.getValue(1);
1779 // Create the CALLSEQ_END node.
1780 unsigned NumBytesForCalleeToPush;
1781 if (IsCalleePop(Op))
1782 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
1783 else if (!Is64Bit && IsStructRet)
1784 // If this is is a call to a struct-return function, the callee
1785 // pops the hidden struct pointer, so we have to push it back.
1786 // This is common for Darwin/X86, Linux & Mingw32 targets.
1787 NumBytesForCalleeToPush = 4;
1789 NumBytesForCalleeToPush = 0; // Callee pops nothing.
1791 // Returns a flag for retval copy to use.
1792 Chain = DAG.getCALLSEQ_END(Chain,
1793 DAG.getIntPtrConstant(NumBytes),
1794 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
1796 InFlag = Chain.getValue(1);
1798 // Handle result values, copying them out of physregs into vregs that we
1800 switch (SRetMethod) {
1802 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1804 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1806 MVT::i64, DAG), Op.ResNo);
1808 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1809 X86::XMM0, X86::XMM1,
1810 MVT::f64, DAG), Op.ResNo);
1812 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1818 //===----------------------------------------------------------------------===//
1819 // Fast Calling Convention (tail call) implementation
1820 //===----------------------------------------------------------------------===//
1822 // Like std call, callee cleans arguments, convention except that ECX is
1823 // reserved for storing the tail called function address. Only 2 registers are
1824 // free for argument passing (inreg). Tail call optimization is performed
1826 // * tailcallopt is enabled
1827 // * caller/callee are fastcc
1828 // On X86_64 architecture with GOT-style position independent code only local
1829 // (within module) calls are supported at the moment.
1830 // To keep the stack aligned according to platform abi the function
1831 // GetAlignedArgumentStackSize ensures that argument delta is always multiples
1832 // of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
1833 // If a tail called function callee has more arguments than the caller the
1834 // caller needs to make sure that there is room to move the RETADDR to. This is
1835 // achieved by reserving an area the size of the argument delta right after the
1836 // original REtADDR, but before the saved framepointer or the spilled registers
1837 // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1849 /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1850 /// for a 16 byte align requirement.
1851 unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1852 SelectionDAG& DAG) {
1853 if (PerformTailCallOpt) {
1854 MachineFunction &MF = DAG.getMachineFunction();
1855 const TargetMachine &TM = MF.getTarget();
1856 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1857 unsigned StackAlignment = TFI.getStackAlignment();
1858 uint64_t AlignMask = StackAlignment - 1;
1859 int64_t Offset = StackSize;
1860 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1861 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1862 // Number smaller than 12 so just add the difference.
1863 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1865 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1866 Offset = ((~AlignMask) & Offset) + StackAlignment +
1867 (StackAlignment-SlotSize);
1874 /// IsEligibleForTailCallElimination - Check to see whether the next instruction
1875 /// following the call is a return. A function is eligible if caller/callee
1876 /// calling conventions match, currently only fastcc supports tail calls, and
1877 /// the function CALL is immediatly followed by a RET.
1878 bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1880 SelectionDAG& DAG) const {
1881 if (!PerformTailCallOpt)
1884 // Check whether CALL node immediatly preceeds the RET node and whether the
1885 // return uses the result of the node or is a void return.
1886 unsigned NumOps = Ret.getNumOperands();
1888 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1889 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
1891 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1892 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
1893 MachineFunction &MF = DAG.getMachineFunction();
1894 unsigned CallerCC = MF.getFunction()->getCallingConv();
1895 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1896 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1897 SDOperand Callee = Call.getOperand(4);
1898 // On x86/32Bit PIC/GOT tail calls are supported.
1899 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1900 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
1903 // Can only do local tail calls (in same module, hidden or protected) on
1904 // x86_64 PIC/GOT at the moment.
1905 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1906 return G->getGlobal()->hasHiddenVisibility()
1907 || G->getGlobal()->hasProtectedVisibility();
1914 //===----------------------------------------------------------------------===//
1915 // Other Lowering Hooks
1916 //===----------------------------------------------------------------------===//
1919 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1920 MachineFunction &MF = DAG.getMachineFunction();
1921 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1922 int ReturnAddrIndex = FuncInfo->getRAIndex();
1924 if (ReturnAddrIndex == 0) {
1925 // Set up a frame object for the return address.
1926 if (Subtarget->is64Bit())
1927 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1929 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1931 FuncInfo->setRAIndex(ReturnAddrIndex);
1934 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1939 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1940 /// specific condition code. It returns a false if it cannot do a direct
1941 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1943 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1944 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1945 SelectionDAG &DAG) {
1946 X86CC = X86::COND_INVALID;
1948 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1949 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1950 // X > -1 -> X == 0, jump !sign.
1951 RHS = DAG.getConstant(0, RHS.getValueType());
1952 X86CC = X86::COND_NS;
1954 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1955 // X < 0 -> X == 0, jump on sign.
1956 X86CC = X86::COND_S;
1958 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1960 RHS = DAG.getConstant(0, RHS.getValueType());
1961 X86CC = X86::COND_LE;
1966 switch (SetCCOpcode) {
1968 case ISD::SETEQ: X86CC = X86::COND_E; break;
1969 case ISD::SETGT: X86CC = X86::COND_G; break;
1970 case ISD::SETGE: X86CC = X86::COND_GE; break;
1971 case ISD::SETLT: X86CC = X86::COND_L; break;
1972 case ISD::SETLE: X86CC = X86::COND_LE; break;
1973 case ISD::SETNE: X86CC = X86::COND_NE; break;
1974 case ISD::SETULT: X86CC = X86::COND_B; break;
1975 case ISD::SETUGT: X86CC = X86::COND_A; break;
1976 case ISD::SETULE: X86CC = X86::COND_BE; break;
1977 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1980 // On a floating point condition, the flags are set as follows:
1982 // 0 | 0 | 0 | X > Y
1983 // 0 | 0 | 1 | X < Y
1984 // 1 | 0 | 0 | X == Y
1985 // 1 | 1 | 1 | unordered
1987 switch (SetCCOpcode) {
1990 case ISD::SETEQ: X86CC = X86::COND_E; break;
1991 case ISD::SETOLT: Flip = true; // Fallthrough
1993 case ISD::SETGT: X86CC = X86::COND_A; break;
1994 case ISD::SETOLE: Flip = true; // Fallthrough
1996 case ISD::SETGE: X86CC = X86::COND_AE; break;
1997 case ISD::SETUGT: Flip = true; // Fallthrough
1999 case ISD::SETLT: X86CC = X86::COND_B; break;
2000 case ISD::SETUGE: Flip = true; // Fallthrough
2002 case ISD::SETLE: X86CC = X86::COND_BE; break;
2004 case ISD::SETNE: X86CC = X86::COND_NE; break;
2005 case ISD::SETUO: X86CC = X86::COND_P; break;
2006 case ISD::SETO: X86CC = X86::COND_NP; break;
2009 std::swap(LHS, RHS);
2012 return X86CC != X86::COND_INVALID;
2015 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
2016 /// code. Current x86 isa includes the following FP cmov instructions:
2017 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2018 static bool hasFPCMov(unsigned X86CC) {
2034 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2035 /// true if Op is undef or if its value falls within the specified range (L, H].
2036 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2037 if (Op.getOpcode() == ISD::UNDEF)
2040 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2041 return (Val >= Low && Val < Hi);
2044 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2045 /// true if Op is undef or if its value equal to the specified value.
2046 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2047 if (Op.getOpcode() == ISD::UNDEF)
2049 return cast<ConstantSDNode>(Op)->getValue() == Val;
2052 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2053 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
2054 bool X86::isPSHUFDMask(SDNode *N) {
2055 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2057 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
2060 // Check if the value doesn't reference the second vector.
2061 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2062 SDOperand Arg = N->getOperand(i);
2063 if (Arg.getOpcode() == ISD::UNDEF) continue;
2064 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2065 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
2072 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2073 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2074 bool X86::isPSHUFHWMask(SDNode *N) {
2075 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2077 if (N->getNumOperands() != 8)
2080 // Lower quadword copied in order.
2081 for (unsigned i = 0; i != 4; ++i) {
2082 SDOperand Arg = N->getOperand(i);
2083 if (Arg.getOpcode() == ISD::UNDEF) continue;
2084 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2085 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2089 // Upper quadword shuffled.
2090 for (unsigned i = 4; i != 8; ++i) {
2091 SDOperand Arg = N->getOperand(i);
2092 if (Arg.getOpcode() == ISD::UNDEF) continue;
2093 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2094 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2095 if (Val < 4 || Val > 7)
2102 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2103 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2104 bool X86::isPSHUFLWMask(SDNode *N) {
2105 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2107 if (N->getNumOperands() != 8)
2110 // Upper quadword copied in order.
2111 for (unsigned i = 4; i != 8; ++i)
2112 if (!isUndefOrEqual(N->getOperand(i), i))
2115 // Lower quadword shuffled.
2116 for (unsigned i = 0; i != 4; ++i)
2117 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2123 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2124 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
2125 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2126 if (NumElems != 2 && NumElems != 4) return false;
2128 unsigned Half = NumElems / 2;
2129 for (unsigned i = 0; i < Half; ++i)
2130 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2132 for (unsigned i = Half; i < NumElems; ++i)
2133 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2139 bool X86::isSHUFPMask(SDNode *N) {
2140 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2141 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2144 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2145 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2146 /// half elements to come from vector 1 (which would equal the dest.) and
2147 /// the upper half to come from vector 2.
2148 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2149 if (NumOps != 2 && NumOps != 4) return false;
2151 unsigned Half = NumOps / 2;
2152 for (unsigned i = 0; i < Half; ++i)
2153 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2155 for (unsigned i = Half; i < NumOps; ++i)
2156 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2161 static bool isCommutedSHUFP(SDNode *N) {
2162 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2163 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2166 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2167 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2168 bool X86::isMOVHLPSMask(SDNode *N) {
2169 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2171 if (N->getNumOperands() != 4)
2174 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2175 return isUndefOrEqual(N->getOperand(0), 6) &&
2176 isUndefOrEqual(N->getOperand(1), 7) &&
2177 isUndefOrEqual(N->getOperand(2), 2) &&
2178 isUndefOrEqual(N->getOperand(3), 3);
2181 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2182 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2184 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2185 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2187 if (N->getNumOperands() != 4)
2190 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2191 return isUndefOrEqual(N->getOperand(0), 2) &&
2192 isUndefOrEqual(N->getOperand(1), 3) &&
2193 isUndefOrEqual(N->getOperand(2), 2) &&
2194 isUndefOrEqual(N->getOperand(3), 3);
2197 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2198 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2199 bool X86::isMOVLPMask(SDNode *N) {
2200 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2202 unsigned NumElems = N->getNumOperands();
2203 if (NumElems != 2 && NumElems != 4)
2206 for (unsigned i = 0; i < NumElems/2; ++i)
2207 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2210 for (unsigned i = NumElems/2; i < NumElems; ++i)
2211 if (!isUndefOrEqual(N->getOperand(i), i))
2217 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2218 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2220 bool X86::isMOVHPMask(SDNode *N) {
2221 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2223 unsigned NumElems = N->getNumOperands();
2224 if (NumElems != 2 && NumElems != 4)
2227 for (unsigned i = 0; i < NumElems/2; ++i)
2228 if (!isUndefOrEqual(N->getOperand(i), i))
2231 for (unsigned i = 0; i < NumElems/2; ++i) {
2232 SDOperand Arg = N->getOperand(i + NumElems/2);
2233 if (!isUndefOrEqual(Arg, i + NumElems))
2240 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2241 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
2242 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2243 bool V2IsSplat = false) {
2244 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2247 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2248 SDOperand BitI = Elts[i];
2249 SDOperand BitI1 = Elts[i+1];
2250 if (!isUndefOrEqual(BitI, j))
2253 if (isUndefOrEqual(BitI1, NumElts))
2256 if (!isUndefOrEqual(BitI1, j + NumElts))
2264 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2265 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2266 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2269 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2270 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
2271 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2272 bool V2IsSplat = false) {
2273 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2276 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2277 SDOperand BitI = Elts[i];
2278 SDOperand BitI1 = Elts[i+1];
2279 if (!isUndefOrEqual(BitI, j + NumElts/2))
2282 if (isUndefOrEqual(BitI1, NumElts))
2285 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2293 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2294 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2295 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2298 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2299 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2301 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2302 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2304 unsigned NumElems = N->getNumOperands();
2305 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2308 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2309 SDOperand BitI = N->getOperand(i);
2310 SDOperand BitI1 = N->getOperand(i+1);
2312 if (!isUndefOrEqual(BitI, j))
2314 if (!isUndefOrEqual(BitI1, j))
2321 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2322 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2324 bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2325 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2327 unsigned NumElems = N->getNumOperands();
2328 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2331 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2332 SDOperand BitI = N->getOperand(i);
2333 SDOperand BitI1 = N->getOperand(i + 1);
2335 if (!isUndefOrEqual(BitI, j))
2337 if (!isUndefOrEqual(BitI1, j))
2344 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2345 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2346 /// MOVSD, and MOVD, i.e. setting the lowest element.
2347 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2348 if (NumElts != 2 && NumElts != 4)
2351 if (!isUndefOrEqual(Elts[0], NumElts))
2354 for (unsigned i = 1; i < NumElts; ++i) {
2355 if (!isUndefOrEqual(Elts[i], i))
2362 bool X86::isMOVLMask(SDNode *N) {
2363 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2364 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2367 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2368 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2369 /// element of vector 2 and the other elements to come from vector 1 in order.
2370 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2371 bool V2IsSplat = false,
2372 bool V2IsUndef = false) {
2373 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2376 if (!isUndefOrEqual(Ops[0], 0))
2379 for (unsigned i = 1; i < NumOps; ++i) {
2380 SDOperand Arg = Ops[i];
2381 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2382 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2383 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2390 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2391 bool V2IsUndef = false) {
2392 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2393 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2394 V2IsSplat, V2IsUndef);
2397 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2398 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2399 bool X86::isMOVSHDUPMask(SDNode *N) {
2400 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2402 if (N->getNumOperands() != 4)
2405 // Expect 1, 1, 3, 3
2406 for (unsigned i = 0; i < 2; ++i) {
2407 SDOperand Arg = N->getOperand(i);
2408 if (Arg.getOpcode() == ISD::UNDEF) continue;
2409 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2410 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2411 if (Val != 1) return false;
2415 for (unsigned i = 2; i < 4; ++i) {
2416 SDOperand Arg = N->getOperand(i);
2417 if (Arg.getOpcode() == ISD::UNDEF) continue;
2418 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2419 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2420 if (Val != 3) return false;
2424 // Don't use movshdup if it can be done with a shufps.
2428 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2429 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2430 bool X86::isMOVSLDUPMask(SDNode *N) {
2431 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2433 if (N->getNumOperands() != 4)
2436 // Expect 0, 0, 2, 2
2437 for (unsigned i = 0; i < 2; ++i) {
2438 SDOperand Arg = N->getOperand(i);
2439 if (Arg.getOpcode() == ISD::UNDEF) continue;
2440 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2441 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2442 if (Val != 0) return false;
2446 for (unsigned i = 2; i < 4; ++i) {
2447 SDOperand Arg = N->getOperand(i);
2448 if (Arg.getOpcode() == ISD::UNDEF) continue;
2449 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2450 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2451 if (Val != 2) return false;
2455 // Don't use movshdup if it can be done with a shufps.
2459 /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2460 /// specifies a identity operation on the LHS or RHS.
2461 static bool isIdentityMask(SDNode *N, bool RHS = false) {
2462 unsigned NumElems = N->getNumOperands();
2463 for (unsigned i = 0; i < NumElems; ++i)
2464 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2469 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2470 /// a splat of a single element.
2471 static bool isSplatMask(SDNode *N) {
2472 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2474 // This is a splat operation if each element of the permute is the same, and
2475 // if the value doesn't reference the second vector.
2476 unsigned NumElems = N->getNumOperands();
2477 SDOperand ElementBase;
2479 for (; i != NumElems; ++i) {
2480 SDOperand Elt = N->getOperand(i);
2481 if (isa<ConstantSDNode>(Elt)) {
2487 if (!ElementBase.Val)
2490 for (; i != NumElems; ++i) {
2491 SDOperand Arg = N->getOperand(i);
2492 if (Arg.getOpcode() == ISD::UNDEF) continue;
2493 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2494 if (Arg != ElementBase) return false;
2497 // Make sure it is a splat of the first vector operand.
2498 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2501 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2502 /// a splat of a single element and it's a 2 or 4 element mask.
2503 bool X86::isSplatMask(SDNode *N) {
2504 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2506 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2507 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2509 return ::isSplatMask(N);
2512 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2513 /// specifies a splat of zero element.
2514 bool X86::isSplatLoMask(SDNode *N) {
2515 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2517 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2518 if (!isUndefOrEqual(N->getOperand(i), 0))
2523 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2524 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2526 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2527 unsigned NumOperands = N->getNumOperands();
2528 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2530 for (unsigned i = 0; i < NumOperands; ++i) {
2532 SDOperand Arg = N->getOperand(NumOperands-i-1);
2533 if (Arg.getOpcode() != ISD::UNDEF)
2534 Val = cast<ConstantSDNode>(Arg)->getValue();
2535 if (Val >= NumOperands) Val -= NumOperands;
2537 if (i != NumOperands - 1)
2544 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2545 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2547 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2549 // 8 nodes, but we only care about the last 4.
2550 for (unsigned i = 7; i >= 4; --i) {
2552 SDOperand Arg = N->getOperand(i);
2553 if (Arg.getOpcode() != ISD::UNDEF)
2554 Val = cast<ConstantSDNode>(Arg)->getValue();
2563 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2564 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2566 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2568 // 8 nodes, but we only care about the first 4.
2569 for (int i = 3; i >= 0; --i) {
2571 SDOperand Arg = N->getOperand(i);
2572 if (Arg.getOpcode() != ISD::UNDEF)
2573 Val = cast<ConstantSDNode>(Arg)->getValue();
2582 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2583 /// specifies a 8 element shuffle that can be broken into a pair of
2584 /// PSHUFHW and PSHUFLW.
2585 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2586 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2588 if (N->getNumOperands() != 8)
2591 // Lower quadword shuffled.
2592 for (unsigned i = 0; i != 4; ++i) {
2593 SDOperand Arg = N->getOperand(i);
2594 if (Arg.getOpcode() == ISD::UNDEF) continue;
2595 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2596 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2601 // Upper quadword shuffled.
2602 for (unsigned i = 4; i != 8; ++i) {
2603 SDOperand Arg = N->getOperand(i);
2604 if (Arg.getOpcode() == ISD::UNDEF) continue;
2605 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2606 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2607 if (Val < 4 || Val > 7)
2614 /// CommuteVectorShuffle - Swap vector_shuffle operands as well as
2615 /// values in ther permute mask.
2616 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2617 SDOperand &V2, SDOperand &Mask,
2618 SelectionDAG &DAG) {
2619 MVT::ValueType VT = Op.getValueType();
2620 MVT::ValueType MaskVT = Mask.getValueType();
2621 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2622 unsigned NumElems = Mask.getNumOperands();
2623 SmallVector<SDOperand, 8> MaskVec;
2625 for (unsigned i = 0; i != NumElems; ++i) {
2626 SDOperand Arg = Mask.getOperand(i);
2627 if (Arg.getOpcode() == ISD::UNDEF) {
2628 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2631 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2632 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2634 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2636 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2640 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2641 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2644 /// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2645 /// the two vector operands have swapped position.
2647 SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2648 MVT::ValueType MaskVT = Mask.getValueType();
2649 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2650 unsigned NumElems = Mask.getNumOperands();
2651 SmallVector<SDOperand, 8> MaskVec;
2652 for (unsigned i = 0; i != NumElems; ++i) {
2653 SDOperand Arg = Mask.getOperand(i);
2654 if (Arg.getOpcode() == ISD::UNDEF) {
2655 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2658 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2659 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2661 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2663 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2665 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2669 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2670 /// match movhlps. The lower half elements should come from upper half of
2671 /// V1 (and in order), and the upper half elements should come from the upper
2672 /// half of V2 (and in order).
2673 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2674 unsigned NumElems = Mask->getNumOperands();
2677 for (unsigned i = 0, e = 2; i != e; ++i)
2678 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2680 for (unsigned i = 2; i != 4; ++i)
2681 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2686 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2687 /// is promoted to a vector.
2688 static inline bool isScalarLoadToVector(SDNode *N) {
2689 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2690 N = N->getOperand(0).Val;
2691 return ISD::isNON_EXTLoad(N);
2696 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2697 /// match movlp{s|d}. The lower half elements should come from lower half of
2698 /// V1 (and in order), and the upper half elements should come from the upper
2699 /// half of V2 (and in order). And since V1 will become the source of the
2700 /// MOVLP, it must be either a vector load or a scalar load to vector.
2701 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2702 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2704 // Is V2 is a vector load, don't do this transformation. We will try to use
2705 // load folding shufps op.
2706 if (ISD::isNON_EXTLoad(V2))
2709 unsigned NumElems = Mask->getNumOperands();
2710 if (NumElems != 2 && NumElems != 4)
2712 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2713 if (!isUndefOrEqual(Mask->getOperand(i), i))
2715 for (unsigned i = NumElems/2; i != NumElems; ++i)
2716 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2721 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2723 static bool isSplatVector(SDNode *N) {
2724 if (N->getOpcode() != ISD::BUILD_VECTOR)
2727 SDOperand SplatValue = N->getOperand(0);
2728 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2729 if (N->getOperand(i) != SplatValue)
2734 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2736 static bool isUndefShuffle(SDNode *N) {
2737 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2740 SDOperand V1 = N->getOperand(0);
2741 SDOperand V2 = N->getOperand(1);
2742 SDOperand Mask = N->getOperand(2);
2743 unsigned NumElems = Mask.getNumOperands();
2744 for (unsigned i = 0; i != NumElems; ++i) {
2745 SDOperand Arg = Mask.getOperand(i);
2746 if (Arg.getOpcode() != ISD::UNDEF) {
2747 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2748 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2750 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2757 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2759 static inline bool isZeroNode(SDOperand Elt) {
2760 return ((isa<ConstantSDNode>(Elt) &&
2761 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2762 (isa<ConstantFPSDNode>(Elt) &&
2763 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2766 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2767 /// to an zero vector.
2768 static bool isZeroShuffle(SDNode *N) {
2769 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2772 SDOperand V1 = N->getOperand(0);
2773 SDOperand V2 = N->getOperand(1);
2774 SDOperand Mask = N->getOperand(2);
2775 unsigned NumElems = Mask.getNumOperands();
2776 for (unsigned i = 0; i != NumElems; ++i) {
2777 SDOperand Arg = Mask.getOperand(i);
2778 if (Arg.getOpcode() == ISD::UNDEF)
2781 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2782 if (Idx < NumElems) {
2783 unsigned Opc = V1.Val->getOpcode();
2784 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2786 if (Opc != ISD::BUILD_VECTOR ||
2787 !isZeroNode(V1.Val->getOperand(Idx)))
2789 } else if (Idx >= NumElems) {
2790 unsigned Opc = V2.Val->getOpcode();
2791 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2793 if (Opc != ISD::BUILD_VECTOR ||
2794 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2801 /// getZeroVector - Returns a vector of specified type with all zero elements.
2803 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2804 assert(MVT::isVector(VT) && "Expected a vector type");
2806 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2807 // type. This ensures they get CSE'd.
2808 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2810 if (MVT::getSizeInBits(VT) == 64) // MMX
2811 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2813 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2814 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2817 /// getOnesVector - Returns a vector of specified type with all bits set.
2819 static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2820 assert(MVT::isVector(VT) && "Expected a vector type");
2822 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2823 // type. This ensures they get CSE'd.
2824 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2826 if (MVT::getSizeInBits(VT) == 64) // MMX
2827 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2829 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2830 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2834 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2835 /// that point to V2 points to its first element.
2836 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2837 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2839 bool Changed = false;
2840 SmallVector<SDOperand, 8> MaskVec;
2841 unsigned NumElems = Mask.getNumOperands();
2842 for (unsigned i = 0; i != NumElems; ++i) {
2843 SDOperand Arg = Mask.getOperand(i);
2844 if (Arg.getOpcode() != ISD::UNDEF) {
2845 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2846 if (Val > NumElems) {
2847 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2851 MaskVec.push_back(Arg);
2855 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2856 &MaskVec[0], MaskVec.size());
2860 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2861 /// operation of specified width.
2862 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2863 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2864 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2866 SmallVector<SDOperand, 8> MaskVec;
2867 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2868 for (unsigned i = 1; i != NumElems; ++i)
2869 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2870 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2873 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2874 /// of specified width.
2875 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2876 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2877 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2878 SmallVector<SDOperand, 8> MaskVec;
2879 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2880 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2881 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2883 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2886 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2887 /// of specified width.
2888 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2889 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2890 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2891 unsigned Half = NumElems/2;
2892 SmallVector<SDOperand, 8> MaskVec;
2893 for (unsigned i = 0; i != Half; ++i) {
2894 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2895 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2897 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2900 /// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2901 /// element #0 of a vector with the specified index, leaving the rest of the
2902 /// elements in place.
2903 static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2904 SelectionDAG &DAG) {
2905 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2906 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2907 SmallVector<SDOperand, 8> MaskVec;
2908 // Element #0 of the result gets the elt we are replacing.
2909 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2910 for (unsigned i = 1; i != NumElems; ++i)
2911 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2912 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2915 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2917 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2918 SDOperand V1 = Op.getOperand(0);
2919 SDOperand Mask = Op.getOperand(2);
2920 MVT::ValueType VT = Op.getValueType();
2921 unsigned NumElems = Mask.getNumOperands();
2922 Mask = getUnpacklMask(NumElems, DAG);
2923 while (NumElems != 4) {
2924 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2927 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2929 Mask = getZeroVector(MVT::v4i32, DAG);
2930 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2931 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2932 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2935 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2936 /// vector of zero or undef vector. This produces a shuffle where the low
2937 /// element of V2 is swizzled into the zero/undef vector, landing at element
2938 /// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
2939 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
2940 bool isZero, SelectionDAG &DAG) {
2941 MVT::ValueType VT = V2.getValueType();
2942 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2943 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
2944 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2945 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2946 SmallVector<SDOperand, 16> MaskVec;
2947 for (unsigned i = 0; i != NumElems; ++i)
2948 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2949 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2951 MaskVec.push_back(DAG.getConstant(i, EVT));
2952 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2953 &MaskVec[0], MaskVec.size());
2954 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2957 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2959 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2960 unsigned NumNonZero, unsigned NumZero,
2961 SelectionDAG &DAG, TargetLowering &TLI) {
2967 for (unsigned i = 0; i < 16; ++i) {
2968 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2969 if (ThisIsNonZero && First) {
2971 V = getZeroVector(MVT::v8i16, DAG);
2973 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2978 SDOperand ThisElt(0, 0), LastElt(0, 0);
2979 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2980 if (LastIsNonZero) {
2981 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2983 if (ThisIsNonZero) {
2984 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2985 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2986 ThisElt, DAG.getConstant(8, MVT::i8));
2988 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2993 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2994 DAG.getIntPtrConstant(i/2));
2998 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3001 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3003 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3004 unsigned NumNonZero, unsigned NumZero,
3005 SelectionDAG &DAG, TargetLowering &TLI) {
3011 for (unsigned i = 0; i < 8; ++i) {
3012 bool isNonZero = (NonZeros & (1 << i)) != 0;
3016 V = getZeroVector(MVT::v8i16, DAG);
3018 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3021 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
3022 DAG.getIntPtrConstant(i));
3030 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3031 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3032 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3033 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3034 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3035 // eliminated on x86-32 hosts.
3036 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3039 if (ISD::isBuildVectorAllOnes(Op.Val))
3040 return getOnesVector(Op.getValueType(), DAG);
3041 return getZeroVector(Op.getValueType(), DAG);
3044 MVT::ValueType VT = Op.getValueType();
3045 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3046 unsigned EVTBits = MVT::getSizeInBits(EVT);
3048 unsigned NumElems = Op.getNumOperands();
3049 unsigned NumZero = 0;
3050 unsigned NumNonZero = 0;
3051 unsigned NonZeros = 0;
3052 bool IsAllConstants = true;
3053 SmallSet<SDOperand, 8> Values;
3054 for (unsigned i = 0; i < NumElems; ++i) {
3055 SDOperand Elt = Op.getOperand(i);
3056 if (Elt.getOpcode() == ISD::UNDEF)
3059 if (Elt.getOpcode() != ISD::Constant &&
3060 Elt.getOpcode() != ISD::ConstantFP)
3061 IsAllConstants = false;
3062 if (isZeroNode(Elt))
3065 NonZeros |= (1 << i);
3070 if (NumNonZero == 0) {
3071 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3072 return DAG.getNode(ISD::UNDEF, VT);
3075 // Special case for single non-zero, non-undef, element.
3076 if (NumNonZero == 1 && NumElems <= 4) {
3077 unsigned Idx = CountTrailingZeros_32(NonZeros);
3078 SDOperand Item = Op.getOperand(Idx);
3080 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3081 // the value are obviously zero, truncate the value to i32 and do the
3082 // insertion that way. Only do this if the value is non-constant or if the
3083 // value is a constant being inserted into element 0. It is cheaper to do
3084 // a constant pool load than it is to do a movd + shuffle.
3085 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3086 (!IsAllConstants || Idx == 0)) {
3087 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3088 // Handle MMX and SSE both.
3089 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3090 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3092 // Truncate the value (which may itself be a constant) to i32, and
3093 // convert it to a vector with movd (S2V+shuffle to zero extend).
3094 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3095 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3096 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3098 // Now we have our 32-bit value zero extended in the low element of
3099 // a vector. If Idx != 0, swizzle it into place.
3102 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3103 getSwapEltZeroMask(VecElts, Idx, DAG)
3105 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3107 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3111 // If we have a constant or non-constant insertion into the low element of
3112 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3113 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3114 // depending on what the source datatype is. Because we can only get here
3115 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3117 // Don't do this for i64 values on x86-32.
3118 (EVT != MVT::i64 || Subtarget->is64Bit())) {
3119 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3120 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3121 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
3124 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
3127 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3128 // is a non-constant being inserted into an element other than the low one,
3129 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3130 // movd/movss) to move this into the low element, then shuffle it into
3132 if (EVTBits == 32) {
3133 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3135 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3136 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
3137 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3138 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3139 SmallVector<SDOperand, 8> MaskVec;
3140 for (unsigned i = 0; i < NumElems; i++)
3141 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3142 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3143 &MaskVec[0], MaskVec.size());
3144 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3145 DAG.getNode(ISD::UNDEF, VT), Mask);
3149 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3150 if (Values.size() == 1)
3153 // A vector full of immediates; various special cases are already
3154 // handled, so this is best done with a single constant-pool load.
3158 // Let legalizer expand 2-wide build_vectors.
3162 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3163 if (EVTBits == 8 && NumElems == 16) {
3164 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3166 if (V.Val) return V;
3169 if (EVTBits == 16 && NumElems == 8) {
3170 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3172 if (V.Val) return V;
3175 // If element VT is == 32 bits, turn it into a number of shuffles.
3176 SmallVector<SDOperand, 8> V;
3178 if (NumElems == 4 && NumZero > 0) {
3179 for (unsigned i = 0; i < 4; ++i) {
3180 bool isZero = !(NonZeros & (1 << i));
3182 V[i] = getZeroVector(VT, DAG);
3184 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3187 for (unsigned i = 0; i < 2; ++i) {
3188 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3191 V[i] = V[i*2]; // Must be a zero vector.
3194 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3195 getMOVLMask(NumElems, DAG));
3198 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3199 getMOVLMask(NumElems, DAG));
3202 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3203 getUnpacklMask(NumElems, DAG));
3208 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3209 // clears the upper bits.
3210 // FIXME: we can do the same for v4f32 case when we know both parts of
3211 // the lower half come from scalar_to_vector (loadf32). We should do
3212 // that in post legalizer dag combiner with target specific hooks.
3213 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3215 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3216 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3217 SmallVector<SDOperand, 8> MaskVec;
3218 bool Reverse = (NonZeros & 0x3) == 2;
3219 for (unsigned i = 0; i < 2; ++i)
3221 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3223 MaskVec.push_back(DAG.getConstant(i, EVT));
3224 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3225 for (unsigned i = 0; i < 2; ++i)
3227 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3229 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3230 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3231 &MaskVec[0], MaskVec.size());
3232 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3235 if (Values.size() > 2) {
3236 // Expand into a number of unpckl*.
3238 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3239 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3240 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3241 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3242 for (unsigned i = 0; i < NumElems; ++i)
3243 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3245 while (NumElems != 0) {
3246 for (unsigned i = 0; i < NumElems; ++i)
3247 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3258 SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3259 SDOperand PermMask, SelectionDAG &DAG,
3260 TargetLowering &TLI) {
3262 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3263 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3264 MVT::ValueType PtrVT = TLI.getPointerTy();
3265 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3266 PermMask.Val->op_end());
3268 // First record which half of which vector the low elements come from.
3269 SmallVector<unsigned, 4> LowQuad(4);
3270 for (unsigned i = 0; i < 4; ++i) {
3271 SDOperand Elt = MaskElts[i];
3272 if (Elt.getOpcode() == ISD::UNDEF)
3274 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3275 int QuadIdx = EltIdx / 4;
3278 int BestLowQuad = -1;
3279 unsigned MaxQuad = 1;
3280 for (unsigned i = 0; i < 4; ++i) {
3281 if (LowQuad[i] > MaxQuad) {
3283 MaxQuad = LowQuad[i];
3287 // Record which half of which vector the high elements come from.
3288 SmallVector<unsigned, 4> HighQuad(4);
3289 for (unsigned i = 4; i < 8; ++i) {
3290 SDOperand Elt = MaskElts[i];
3291 if (Elt.getOpcode() == ISD::UNDEF)
3293 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3294 int QuadIdx = EltIdx / 4;
3295 ++HighQuad[QuadIdx];
3297 int BestHighQuad = -1;
3299 for (unsigned i = 0; i < 4; ++i) {
3300 if (HighQuad[i] > MaxQuad) {
3302 MaxQuad = HighQuad[i];
3306 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3307 if (BestLowQuad != -1 || BestHighQuad != -1) {
3308 // First sort the 4 chunks in order using shufpd.
3309 SmallVector<SDOperand, 8> MaskVec;
3310 if (BestLowQuad != -1)
3311 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3313 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3314 if (BestHighQuad != -1)
3315 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3317 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3318 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3319 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3320 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3321 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3322 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3324 // Now sort high and low parts separately.
3325 BitVector InOrder(8);
3326 if (BestLowQuad != -1) {
3327 // Sort lower half in order using PSHUFLW.
3329 bool AnyOutOrder = false;
3330 for (unsigned i = 0; i != 4; ++i) {
3331 SDOperand Elt = MaskElts[i];
3332 if (Elt.getOpcode() == ISD::UNDEF) {
3333 MaskVec.push_back(Elt);
3336 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3339 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3340 // If this element is in the right place after this shuffle, then
3342 if ((int)(EltIdx / 4) == BestLowQuad)
3347 for (unsigned i = 4; i != 8; ++i)
3348 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3349 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3350 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3354 if (BestHighQuad != -1) {
3355 // Sort high half in order using PSHUFHW if possible.
3357 for (unsigned i = 0; i != 4; ++i)
3358 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3359 bool AnyOutOrder = false;
3360 for (unsigned i = 4; i != 8; ++i) {
3361 SDOperand Elt = MaskElts[i];
3362 if (Elt.getOpcode() == ISD::UNDEF) {
3363 MaskVec.push_back(Elt);
3366 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3369 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3370 // If this element is in the right place after this shuffle, then
3372 if ((int)(EltIdx / 4) == BestHighQuad)
3377 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3378 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3382 // The other elements are put in the right place using pextrw and pinsrw.
3383 for (unsigned i = 0; i != 8; ++i) {
3386 SDOperand Elt = MaskElts[i];
3387 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3390 SDOperand ExtOp = (EltIdx < 8)
3391 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3392 DAG.getConstant(EltIdx, PtrVT))
3393 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3394 DAG.getConstant(EltIdx - 8, PtrVT));
3395 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3396 DAG.getConstant(i, PtrVT));
3401 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3402 ///as few as possible.
3403 // First, let's find out how many elements are already in the right order.
3404 unsigned V1InOrder = 0;
3405 unsigned V1FromV1 = 0;
3406 unsigned V2InOrder = 0;
3407 unsigned V2FromV2 = 0;
3408 SmallVector<SDOperand, 8> V1Elts;
3409 SmallVector<SDOperand, 8> V2Elts;
3410 for (unsigned i = 0; i < 8; ++i) {
3411 SDOperand Elt = MaskElts[i];
3412 if (Elt.getOpcode() == ISD::UNDEF) {
3413 V1Elts.push_back(Elt);
3414 V2Elts.push_back(Elt);
3419 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3421 V1Elts.push_back(Elt);
3422 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3424 } else if (EltIdx == i+8) {
3425 V1Elts.push_back(Elt);
3426 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3428 } else if (EltIdx < 8) {
3429 V1Elts.push_back(Elt);
3432 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3437 if (V2InOrder > V1InOrder) {
3438 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3440 std::swap(V1Elts, V2Elts);
3441 std::swap(V1FromV1, V2FromV2);
3444 if ((V1FromV1 + V1InOrder) != 8) {
3445 // Some elements are from V2.
3447 // If there are elements that are from V1 but out of place,
3448 // then first sort them in place
3449 SmallVector<SDOperand, 8> MaskVec;
3450 for (unsigned i = 0; i < 8; ++i) {
3451 SDOperand Elt = V1Elts[i];
3452 if (Elt.getOpcode() == ISD::UNDEF) {
3453 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3456 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3458 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3460 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3462 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3463 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
3467 for (unsigned i = 0; i < 8; ++i) {
3468 SDOperand Elt = V1Elts[i];
3469 if (Elt.getOpcode() == ISD::UNDEF)
3471 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3474 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3475 DAG.getConstant(EltIdx - 8, PtrVT));
3476 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3477 DAG.getConstant(i, PtrVT));
3481 // All elements are from V1.
3483 for (unsigned i = 0; i < 8; ++i) {
3484 SDOperand Elt = V1Elts[i];
3485 if (Elt.getOpcode() == ISD::UNDEF)
3487 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3488 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3489 DAG.getConstant(EltIdx, PtrVT));
3490 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3491 DAG.getConstant(i, PtrVT));
3497 /// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3498 /// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3499 /// done when every pair / quad of shuffle mask elements point to elements in
3500 /// the right sequence. e.g.
3501 /// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3503 SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3505 SDOperand PermMask, SelectionDAG &DAG,
3506 TargetLowering &TLI) {
3507 unsigned NumElems = PermMask.getNumOperands();
3508 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3509 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3510 MVT::ValueType NewVT = MaskVT;
3512 case MVT::v4f32: NewVT = MVT::v2f64; break;
3513 case MVT::v4i32: NewVT = MVT::v2i64; break;
3514 case MVT::v8i16: NewVT = MVT::v4i32; break;
3515 case MVT::v16i8: NewVT = MVT::v4i32; break;
3516 default: assert(false && "Unexpected!");
3519 if (NewWidth == 2) {
3520 if (MVT::isInteger(VT))
3525 unsigned Scale = NumElems / NewWidth;
3526 SmallVector<SDOperand, 8> MaskVec;
3527 for (unsigned i = 0; i < NumElems; i += Scale) {
3528 unsigned StartIdx = ~0U;
3529 for (unsigned j = 0; j < Scale; ++j) {
3530 SDOperand Elt = PermMask.getOperand(i+j);
3531 if (Elt.getOpcode() == ISD::UNDEF)
3533 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3534 if (StartIdx == ~0U)
3535 StartIdx = EltIdx - (EltIdx % Scale);
3536 if (EltIdx != StartIdx + j)
3539 if (StartIdx == ~0U)
3540 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3542 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
3545 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3546 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3547 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3548 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3549 &MaskVec[0], MaskVec.size()));
3553 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3554 SDOperand V1 = Op.getOperand(0);
3555 SDOperand V2 = Op.getOperand(1);
3556 SDOperand PermMask = Op.getOperand(2);
3557 MVT::ValueType VT = Op.getValueType();
3558 unsigned NumElems = PermMask.getNumOperands();
3559 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3560 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3561 bool V1IsSplat = false;
3562 bool V2IsSplat = false;
3564 if (isUndefShuffle(Op.Val))
3565 return DAG.getNode(ISD::UNDEF, VT);
3567 if (isZeroShuffle(Op.Val))
3568 return getZeroVector(VT, DAG);
3570 if (isIdentityMask(PermMask.Val))
3572 else if (isIdentityMask(PermMask.Val, true))
3575 if (isSplatMask(PermMask.Val)) {
3576 if (NumElems <= 4) return Op;
3577 // Promote it to a v4i32 splat.
3578 return PromoteSplat(Op, DAG);
3581 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3583 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3584 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3586 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3587 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3588 // FIXME: Figure out a cleaner way to do this.
3589 // Try to make use of movq to zero out the top part.
3590 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3591 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3593 SDOperand NewV1 = NewOp.getOperand(0);
3594 SDOperand NewV2 = NewOp.getOperand(1);
3595 SDOperand NewMask = NewOp.getOperand(2);
3596 if (isCommutedMOVL(NewMask.Val, true, false)) {
3597 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3598 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3599 NewV1, NewV2, getMOVLMask(2, DAG));
3600 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3603 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3604 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3605 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3606 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3610 if (X86::isMOVLMask(PermMask.Val))
3611 return (V1IsUndef) ? V2 : Op;
3613 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3614 X86::isMOVSLDUPMask(PermMask.Val) ||
3615 X86::isMOVHLPSMask(PermMask.Val) ||
3616 X86::isMOVHPMask(PermMask.Val) ||
3617 X86::isMOVLPMask(PermMask.Val))
3620 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3621 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3622 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3624 bool Commuted = false;
3625 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3626 // 1,1,1,1 -> v8i16 though.
3627 V1IsSplat = isSplatVector(V1.Val);
3628 V2IsSplat = isSplatVector(V2.Val);
3630 // Canonicalize the splat or undef, if present, to be on the RHS.
3631 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3632 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3633 std::swap(V1IsSplat, V2IsSplat);
3634 std::swap(V1IsUndef, V2IsUndef);
3638 // FIXME: Figure out a cleaner way to do this.
3639 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3640 if (V2IsUndef) return V1;
3641 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3643 // V2 is a splat, so the mask may be malformed. That is, it may point
3644 // to any V2 element. The instruction selectior won't like this. Get
3645 // a corrected mask and commute to form a proper MOVS{S|D}.
3646 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3647 if (NewMask.Val != PermMask.Val)
3648 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3653 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3654 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3655 X86::isUNPCKLMask(PermMask.Val) ||
3656 X86::isUNPCKHMask(PermMask.Val))
3660 // Normalize mask so all entries that point to V2 points to its first
3661 // element then try to match unpck{h|l} again. If match, return a
3662 // new vector_shuffle with the corrected mask.
3663 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3664 if (NewMask.Val != PermMask.Val) {
3665 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3666 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3667 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3668 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3669 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3670 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3675 // Normalize the node to match x86 shuffle ops if needed
3676 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3677 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3680 // Commute is back and try unpck* again.
3681 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3682 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3683 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3684 X86::isUNPCKLMask(PermMask.Val) ||
3685 X86::isUNPCKHMask(PermMask.Val))
3689 // If VT is integer, try PSHUF* first, then SHUFP*.
3690 if (MVT::isInteger(VT)) {
3691 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3692 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3693 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3694 X86::isPSHUFDMask(PermMask.Val)) ||
3695 X86::isPSHUFHWMask(PermMask.Val) ||
3696 X86::isPSHUFLWMask(PermMask.Val)) {
3697 if (V2.getOpcode() != ISD::UNDEF)
3698 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3699 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3703 if (X86::isSHUFPMask(PermMask.Val) &&
3704 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3707 // Floating point cases in the other order.
3708 if (X86::isSHUFPMask(PermMask.Val))
3710 if (X86::isPSHUFDMask(PermMask.Val) ||
3711 X86::isPSHUFHWMask(PermMask.Val) ||
3712 X86::isPSHUFLWMask(PermMask.Val)) {
3713 if (V2.getOpcode() != ISD::UNDEF)
3714 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3715 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3720 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3721 if (VT == MVT::v8i16) {
3722 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3727 // Handle all 4 wide cases with a number of shuffles.
3728 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
3729 // Don't do this for MMX.
3730 MVT::ValueType MaskVT = PermMask.getValueType();
3731 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3732 SmallVector<std::pair<int, int>, 8> Locs;
3733 Locs.reserve(NumElems);
3734 SmallVector<SDOperand, 8> Mask1(NumElems,
3735 DAG.getNode(ISD::UNDEF, MaskEVT));
3736 SmallVector<SDOperand, 8> Mask2(NumElems,
3737 DAG.getNode(ISD::UNDEF, MaskEVT));
3740 // If no more than two elements come from either vector. This can be
3741 // implemented with two shuffles. First shuffle gather the elements.
3742 // The second shuffle, which takes the first shuffle as both of its
3743 // vector operands, put the elements into the right order.
3744 for (unsigned i = 0; i != NumElems; ++i) {
3745 SDOperand Elt = PermMask.getOperand(i);
3746 if (Elt.getOpcode() == ISD::UNDEF) {
3747 Locs[i] = std::make_pair(-1, -1);
3749 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3750 if (Val < NumElems) {
3751 Locs[i] = std::make_pair(0, NumLo);
3755 Locs[i] = std::make_pair(1, NumHi);
3756 if (2+NumHi < NumElems)
3757 Mask1[2+NumHi] = Elt;
3762 if (NumLo <= 2 && NumHi <= 2) {
3763 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3764 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3765 &Mask1[0], Mask1.size()));
3766 for (unsigned i = 0; i != NumElems; ++i) {
3767 if (Locs[i].first == -1)
3770 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3771 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3772 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3776 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3777 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3778 &Mask2[0], Mask2.size()));
3781 // Break it into (shuffle shuffle_hi, shuffle_lo).
3783 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3784 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3785 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3786 unsigned MaskIdx = 0;
3788 unsigned HiIdx = NumElems/2;
3789 for (unsigned i = 0; i != NumElems; ++i) {
3790 if (i == NumElems/2) {
3796 SDOperand Elt = PermMask.getOperand(i);
3797 if (Elt.getOpcode() == ISD::UNDEF) {
3798 Locs[i] = std::make_pair(-1, -1);
3799 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3800 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3801 (*MaskPtr)[LoIdx] = Elt;
3804 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3805 (*MaskPtr)[HiIdx] = Elt;
3810 SDOperand LoShuffle =
3811 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3812 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3813 &LoMask[0], LoMask.size()));
3814 SDOperand HiShuffle =
3815 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3816 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3817 &HiMask[0], HiMask.size()));
3818 SmallVector<SDOperand, 8> MaskOps;
3819 for (unsigned i = 0; i != NumElems; ++i) {
3820 if (Locs[i].first == -1) {
3821 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3823 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3824 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3827 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3828 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3829 &MaskOps[0], MaskOps.size()));
3836 X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3837 SelectionDAG &DAG) {
3838 MVT::ValueType VT = Op.getValueType();
3839 if (MVT::getSizeInBits(VT) == 8) {
3840 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3841 Op.getOperand(0), Op.getOperand(1));
3842 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3843 DAG.getValueType(VT));
3844 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3845 } else if (MVT::getSizeInBits(VT) == 16) {
3846 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3847 Op.getOperand(0), Op.getOperand(1));
3848 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3849 DAG.getValueType(VT));
3850 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3857 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3858 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3861 if (Subtarget->hasSSE41())
3862 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3864 MVT::ValueType VT = Op.getValueType();
3865 // TODO: handle v16i8.
3866 if (MVT::getSizeInBits(VT) == 16) {
3867 SDOperand Vec = Op.getOperand(0);
3868 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3870 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3871 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3872 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3874 // Transform it so it match pextrw which produces a 32-bit result.
3875 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3876 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3877 Op.getOperand(0), Op.getOperand(1));
3878 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3879 DAG.getValueType(VT));
3880 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3881 } else if (MVT::getSizeInBits(VT) == 32) {
3882 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3885 // SHUFPS the element to the lowest double word, then movss.
3886 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3887 SmallVector<SDOperand, 8> IdxVec;
3889 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3891 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3893 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3895 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3896 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3897 &IdxVec[0], IdxVec.size());
3898 SDOperand Vec = Op.getOperand(0);
3899 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3900 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3901 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3902 DAG.getIntPtrConstant(0));
3903 } else if (MVT::getSizeInBits(VT) == 64) {
3904 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3905 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3906 // to match extract_elt for f64.
3907 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3911 // UNPCKHPD the element to the lowest double word, then movsd.
3912 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3913 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3914 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3915 SmallVector<SDOperand, 8> IdxVec;
3916 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
3918 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3919 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3920 &IdxVec[0], IdxVec.size());
3921 SDOperand Vec = Op.getOperand(0);
3922 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3923 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3924 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3925 DAG.getIntPtrConstant(0));
3932 X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3933 MVT::ValueType VT = Op.getValueType();
3934 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3936 SDOperand N0 = Op.getOperand(0);
3937 SDOperand N1 = Op.getOperand(1);
3938 SDOperand N2 = Op.getOperand(2);
3940 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3941 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3943 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3945 if (N1.getValueType() != MVT::i32)
3946 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3947 if (N2.getValueType() != MVT::i32)
3948 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3949 return DAG.getNode(Opc, VT, N0, N1, N2);
3950 } else if (EVT == MVT::f32) {
3951 // Bits [7:6] of the constant are the source select. This will always be
3952 // zero here. The DAG Combiner may combine an extract_elt index into these
3953 // bits. For example (insert (extract, 3), 2) could be matched by putting
3954 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3955 // Bits [5:4] of the constant are the destination select. This is the
3956 // value of the incoming immediate.
3957 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3958 // combine either bitwise AND or insert of float 0.0 to set these bits.
3959 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3960 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3966 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3967 MVT::ValueType VT = Op.getValueType();
3968 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3970 if (Subtarget->hasSSE41())
3971 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3976 SDOperand N0 = Op.getOperand(0);
3977 SDOperand N1 = Op.getOperand(1);
3978 SDOperand N2 = Op.getOperand(2);
3980 if (MVT::getSizeInBits(EVT) == 16) {
3981 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3982 // as its second argument.
3983 if (N1.getValueType() != MVT::i32)
3984 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3985 if (N2.getValueType() != MVT::i32)
3986 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3987 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3993 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3994 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3995 MVT::ValueType VT = MVT::v2i32;
3996 switch (Op.getValueType()) {
4003 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4004 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
4007 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4008 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4009 // one of the above mentioned nodes. It has to be wrapped because otherwise
4010 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4011 // be used to form addressing mode. These wrapped nodes will be selected
4014 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4015 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4016 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4018 CP->getAlignment());
4019 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4020 // With PIC, the address is actually $g + Offset.
4021 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4022 !Subtarget->isPICStyleRIPRel()) {
4023 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4024 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4032 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4033 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4034 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
4035 // If it's a debug information descriptor, don't mess with it.
4036 if (DAG.isVerifiedDebugInfoDesc(Op))
4038 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4039 // With PIC, the address is actually $g + Offset.
4040 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4041 !Subtarget->isPICStyleRIPRel()) {
4042 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4043 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4047 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4048 // load the value at address GV, not the value of GV itself. This means that
4049 // the GlobalAddress must be in the base or index register of the address, not
4050 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4051 // The same applies for external symbols during PIC codegen
4052 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
4053 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
4054 PseudoSourceValue::getGOT(), 0);
4059 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
4061 LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4062 const MVT::ValueType PtrVT) {
4064 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4065 DAG.getNode(X86ISD::GlobalBaseReg,
4067 InFlag = Chain.getValue(1);
4069 // emit leal symbol@TLSGD(,%ebx,1), %eax
4070 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4071 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4072 GA->getValueType(0),
4074 SDOperand Ops[] = { Chain, TGA, InFlag };
4075 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4076 InFlag = Result.getValue(2);
4077 Chain = Result.getValue(1);
4079 // call ___tls_get_addr. This function receives its argument in
4080 // the register EAX.
4081 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4082 InFlag = Chain.getValue(1);
4084 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4085 SDOperand Ops1[] = { Chain,
4086 DAG.getTargetExternalSymbol("___tls_get_addr",
4088 DAG.getRegister(X86::EAX, PtrVT),
4089 DAG.getRegister(X86::EBX, PtrVT),
4091 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4092 InFlag = Chain.getValue(1);
4094 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4097 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4098 // "local exec" model.
4100 LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4101 const MVT::ValueType PtrVT) {
4102 // Get the Thread Pointer
4103 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4104 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4106 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4107 GA->getValueType(0),
4109 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4111 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
4112 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
4113 PseudoSourceValue::getGOT(), 0);
4115 // The address of the thread local variable is the add of the thread
4116 // pointer with the offset of the variable.
4117 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4121 X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4122 // TODO: implement the "local dynamic" model
4123 // TODO: implement the "initial exec"model for pic executables
4124 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4125 "TLS not implemented for non-ELF and 64-bit targets");
4126 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4127 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4128 // otherwise use the "Local Exec"TLS Model
4129 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4130 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4132 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4136 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4137 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4138 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4139 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4140 // With PIC, the address is actually $g + Offset.
4141 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4142 !Subtarget->isPICStyleRIPRel()) {
4143 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4144 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4151 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4152 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4153 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4154 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4155 // With PIC, the address is actually $g + Offset.
4156 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4157 !Subtarget->isPICStyleRIPRel()) {
4158 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4159 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4166 /// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4167 /// take a 2 x i32 value to shift plus a shift amount.
4168 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
4169 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4170 MVT::ValueType VT = Op.getValueType();
4171 unsigned VTBits = MVT::getSizeInBits(VT);
4172 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4173 SDOperand ShOpLo = Op.getOperand(0);
4174 SDOperand ShOpHi = Op.getOperand(1);
4175 SDOperand ShAmt = Op.getOperand(2);
4176 SDOperand Tmp1 = isSRA ?
4177 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4178 DAG.getConstant(0, VT);
4180 SDOperand Tmp2, Tmp3;
4181 if (Op.getOpcode() == ISD::SHL_PARTS) {
4182 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4183 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
4185 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4186 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
4189 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4190 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4191 DAG.getConstant(VTBits, MVT::i8));
4192 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
4193 AndNode, DAG.getConstant(0, MVT::i8));
4196 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4197 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
4198 SmallVector<SDOperand, 4> Ops;
4199 if (Op.getOpcode() == ISD::SHL_PARTS) {
4200 Ops.push_back(Tmp2);
4201 Ops.push_back(Tmp3);
4203 Ops.push_back(Cond);
4204 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4207 Ops.push_back(Tmp3);
4208 Ops.push_back(Tmp1);
4210 Ops.push_back(Cond);
4211 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4213 Ops.push_back(Tmp2);
4214 Ops.push_back(Tmp3);
4216 Ops.push_back(Cond);
4217 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4220 Ops.push_back(Tmp3);
4221 Ops.push_back(Tmp1);
4223 Ops.push_back(Cond);
4224 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4227 VTs = DAG.getNodeValueTypes(VT, VT);
4231 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
4234 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4235 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4236 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4237 "Unknown SINT_TO_FP to lower!");
4239 // These are really Legal; caller falls through into that case.
4240 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4242 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4243 Subtarget->is64Bit())
4246 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4247 MachineFunction &MF = DAG.getMachineFunction();
4248 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4249 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4250 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
4252 PseudoSourceValue::getFixedStack(),
4257 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
4259 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4261 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4262 SmallVector<SDOperand, 8> Ops;
4263 Ops.push_back(Chain);
4264 Ops.push_back(StackSlot);
4265 Ops.push_back(DAG.getValueType(SrcVT));
4266 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4267 Tys, &Ops[0], Ops.size());
4270 Chain = Result.getValue(1);
4271 SDOperand InFlag = Result.getValue(2);
4273 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4274 // shouldn't be necessary except that RFP cannot be live across
4275 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4276 MachineFunction &MF = DAG.getMachineFunction();
4277 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4278 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4279 Tys = DAG.getVTList(MVT::Other);
4280 SmallVector<SDOperand, 8> Ops;
4281 Ops.push_back(Chain);
4282 Ops.push_back(Result);
4283 Ops.push_back(StackSlot);
4284 Ops.push_back(DAG.getValueType(Op.getValueType()));
4285 Ops.push_back(InFlag);
4286 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
4287 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
4288 PseudoSourceValue::getFixedStack(), SSFI);
4294 std::pair<SDOperand,SDOperand> X86TargetLowering::
4295 FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
4296 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4297 "Unknown FP_TO_SINT to lower!");
4299 // These are really Legal.
4300 if (Op.getValueType() == MVT::i32 &&
4301 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
4302 return std::make_pair(SDOperand(), SDOperand());
4303 if (Subtarget->is64Bit() &&
4304 Op.getValueType() == MVT::i64 &&
4305 Op.getOperand(0).getValueType() != MVT::f80)
4306 return std::make_pair(SDOperand(), SDOperand());
4308 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4310 MachineFunction &MF = DAG.getMachineFunction();
4311 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4312 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4313 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4315 switch (Op.getValueType()) {
4316 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4317 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4318 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4319 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
4322 SDOperand Chain = DAG.getEntryNode();
4323 SDOperand Value = Op.getOperand(0);
4324 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
4325 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
4326 Chain = DAG.getStore(Chain, Value, StackSlot,
4327 PseudoSourceValue::getFixedStack(), SSFI);
4328 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4330 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4332 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4333 Chain = Value.getValue(1);
4334 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4335 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4338 // Build the FP_TO_INT*_IN_MEM
4339 SDOperand Ops[] = { Chain, Value, StackSlot };
4340 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4342 return std::make_pair(FIST, StackSlot);
4345 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
4346 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4347 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4348 if (FIST.Val == 0) return SDOperand();
4351 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4354 SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4355 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4356 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4357 if (FIST.Val == 0) return 0;
4359 // Return an i64 load from the stack slot.
4360 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4362 // Use a MERGE_VALUES node to drop the chain result value.
4363 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4366 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4367 MVT::ValueType VT = Op.getValueType();
4368 MVT::ValueType EltVT = VT;
4369 if (MVT::isVector(VT))
4370 EltVT = MVT::getVectorElementType(VT);
4371 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4372 std::vector<Constant*> CV;
4373 if (EltVT == MVT::f64) {
4374 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
4378 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
4384 Constant *C = ConstantVector::get(CV);
4385 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4386 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4387 PseudoSourceValue::getConstantPool(), 0,
4389 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4392 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4393 MVT::ValueType VT = Op.getValueType();
4394 MVT::ValueType EltVT = VT;
4395 unsigned EltNum = 1;
4396 if (MVT::isVector(VT)) {
4397 EltVT = MVT::getVectorElementType(VT);
4398 EltNum = MVT::getVectorNumElements(VT);
4400 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4401 std::vector<Constant*> CV;
4402 if (EltVT == MVT::f64) {
4403 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
4407 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
4413 Constant *C = ConstantVector::get(CV);
4414 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4415 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4416 PseudoSourceValue::getConstantPool(), 0,
4418 if (MVT::isVector(VT)) {
4419 return DAG.getNode(ISD::BIT_CONVERT, VT,
4420 DAG.getNode(ISD::XOR, MVT::v2i64,
4421 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4422 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4424 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4428 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4429 SDOperand Op0 = Op.getOperand(0);
4430 SDOperand Op1 = Op.getOperand(1);
4431 MVT::ValueType VT = Op.getValueType();
4432 MVT::ValueType SrcVT = Op1.getValueType();
4433 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4435 // If second operand is smaller, extend it first.
4436 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4437 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4439 SrcTy = MVT::getTypeForValueType(SrcVT);
4441 // And if it is bigger, shrink it first.
4442 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4443 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
4445 SrcTy = MVT::getTypeForValueType(SrcVT);
4448 // At this point the operands and the result should have the same
4449 // type, and that won't be f80 since that is not custom lowered.
4451 // First get the sign bit of second operand.
4452 std::vector<Constant*> CV;
4453 if (SrcVT == MVT::f64) {
4454 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4455 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
4457 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4458 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4459 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4460 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4462 Constant *C = ConstantVector::get(CV);
4463 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4464 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
4465 PseudoSourceValue::getConstantPool(), 0,
4467 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4469 // Shift sign bit right or left if the two operands have different types.
4470 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4471 // Op0 is MVT::f32, Op1 is MVT::f64.
4472 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4473 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4474 DAG.getConstant(32, MVT::i32));
4475 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4476 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4477 DAG.getIntPtrConstant(0));
4480 // Clear first operand sign bit.
4482 if (VT == MVT::f64) {
4483 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4484 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
4486 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4487 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4488 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4489 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4491 C = ConstantVector::get(CV);
4492 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4493 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4494 PseudoSourceValue::getConstantPool(), 0,
4496 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4498 // Or the value with the sign bit.
4499 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4502 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
4503 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
4505 SDOperand Op0 = Op.getOperand(0);
4506 SDOperand Op1 = Op.getOperand(1);
4507 SDOperand CC = Op.getOperand(2);
4508 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4509 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4512 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
4514 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4515 return DAG.getNode(X86ISD::SETCC, MVT::i8,
4516 DAG.getConstant(X86CC, MVT::i8), Cond);
4519 assert(isFP && "Illegal integer SetCC!");
4521 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4522 switch (SetCCOpcode) {
4523 default: assert(false && "Illegal floating point SetCC!");
4524 case ISD::SETOEQ: { // !PF & ZF
4525 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4526 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
4527 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4528 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4529 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4531 case ISD::SETUNE: { // PF | !ZF
4532 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4533 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
4534 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4535 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4536 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4542 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4543 bool addTest = true;
4544 SDOperand Cond = Op.getOperand(0);
4547 if (Cond.getOpcode() == ISD::SETCC)
4548 Cond = LowerSETCC(Cond, DAG);
4550 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4551 // setting operand in place of the X86ISD::SETCC.
4552 if (Cond.getOpcode() == X86ISD::SETCC) {
4553 CC = Cond.getOperand(0);
4555 SDOperand Cmp = Cond.getOperand(1);
4556 unsigned Opc = Cmp.getOpcode();
4557 MVT::ValueType VT = Op.getValueType();
4559 bool IllegalFPCMov = false;
4560 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
4561 !isScalarFPTypeInSSEReg(VT)) // FPStack?
4562 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4564 if ((Opc == X86ISD::CMP ||
4565 Opc == X86ISD::COMI ||
4566 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
4573 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4574 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
4577 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4579 SmallVector<SDOperand, 4> Ops;
4580 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4581 // condition is true.
4582 Ops.push_back(Op.getOperand(2));
4583 Ops.push_back(Op.getOperand(1));
4585 Ops.push_back(Cond);
4586 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
4589 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4590 bool addTest = true;
4591 SDOperand Chain = Op.getOperand(0);
4592 SDOperand Cond = Op.getOperand(1);
4593 SDOperand Dest = Op.getOperand(2);
4596 if (Cond.getOpcode() == ISD::SETCC)
4597 Cond = LowerSETCC(Cond, DAG);
4599 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4600 // setting operand in place of the X86ISD::SETCC.
4601 if (Cond.getOpcode() == X86ISD::SETCC) {
4602 CC = Cond.getOperand(0);
4604 SDOperand Cmp = Cond.getOperand(1);
4605 unsigned Opc = Cmp.getOpcode();
4606 if (Opc == X86ISD::CMP ||
4607 Opc == X86ISD::COMI ||
4608 Opc == X86ISD::UCOMI) {
4615 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4616 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
4618 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
4619 Chain, Op.getOperand(2), CC, Cond);
4623 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4624 // Calls to _alloca is needed to probe the stack when allocating more than 4k
4625 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
4626 // that the guard pages used by the OS virtual memory manager are allocated in
4627 // correct sequence.
4629 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4630 SelectionDAG &DAG) {
4631 assert(Subtarget->isTargetCygMing() &&
4632 "This should be used only on Cygwin/Mingw targets");
4635 SDOperand Chain = Op.getOperand(0);
4636 SDOperand Size = Op.getOperand(1);
4637 // FIXME: Ensure alignment here
4641 MVT::ValueType IntPtr = getPointerTy();
4642 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
4644 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4645 Flag = Chain.getValue(1);
4647 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4648 SDOperand Ops[] = { Chain,
4649 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4650 DAG.getRegister(X86::EAX, IntPtr),
4652 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4653 Flag = Chain.getValue(1);
4655 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4657 std::vector<MVT::ValueType> Tys;
4658 Tys.push_back(SPTy);
4659 Tys.push_back(MVT::Other);
4660 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4661 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4664 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4665 SDOperand InFlag(0, 0);
4666 SDOperand Chain = Op.getOperand(0);
4668 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4669 if (Align == 0) Align = 1;
4671 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4672 // If not DWORD aligned or size is more than the threshold, call memset.
4673 // The libc version is likely to be faster for these cases. It can use the
4674 // address value and run time information about the CPU.
4675 if ((Align & 3) != 0 ||
4676 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
4677 MVT::ValueType IntPtr = getPointerTy();
4678 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4679 TargetLowering::ArgListTy Args;
4680 TargetLowering::ArgListEntry Entry;
4681 Entry.Node = Op.getOperand(1);
4682 Entry.Ty = IntPtrTy;
4683 Args.push_back(Entry);
4684 // Extend the unsigned i8 argument to be an int value for the call.
4685 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4686 Entry.Ty = IntPtrTy;
4687 Args.push_back(Entry);
4688 Entry.Node = Op.getOperand(3);
4689 Args.push_back(Entry);
4690 std::pair<SDOperand,SDOperand> CallResult =
4691 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4692 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4693 return CallResult.second;
4698 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4699 unsigned BytesLeft = 0;
4700 bool TwoRepStos = false;
4703 uint64_t Val = ValC->getValue() & 255;
4705 // If the value is a constant, then we can potentially use larger sets.
4706 switch (Align & 3) {
4707 case 2: // WORD aligned
4710 Val = (Val << 8) | Val;
4712 case 0: // DWORD aligned
4715 Val = (Val << 8) | Val;
4716 Val = (Val << 16) | Val;
4717 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4720 Val = (Val << 32) | Val;
4723 default: // Byte aligned
4726 Count = Op.getOperand(3);
4730 if (AVT > MVT::i8) {
4732 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4733 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
4734 BytesLeft = I->getValue() % UBytes;
4736 assert(AVT >= MVT::i32 &&
4737 "Do not use rep;stos if not at least DWORD aligned");
4738 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4739 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4744 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4746 InFlag = Chain.getValue(1);
4749 Count = Op.getOperand(3);
4750 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4751 InFlag = Chain.getValue(1);
4754 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4756 InFlag = Chain.getValue(1);
4757 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4758 Op.getOperand(1), InFlag);
4759 InFlag = Chain.getValue(1);
4761 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4762 SmallVector<SDOperand, 8> Ops;
4763 Ops.push_back(Chain);
4764 Ops.push_back(DAG.getValueType(AVT));
4765 Ops.push_back(InFlag);
4766 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4769 InFlag = Chain.getValue(1);
4770 Count = Op.getOperand(3);
4771 MVT::ValueType CVT = Count.getValueType();
4772 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4773 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4774 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4776 InFlag = Chain.getValue(1);
4777 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4779 Ops.push_back(Chain);
4780 Ops.push_back(DAG.getValueType(MVT::i8));
4781 Ops.push_back(InFlag);
4782 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4783 } else if (BytesLeft) {
4784 // Issue stores for the last 1 - 7 bytes.
4786 unsigned Val = ValC->getValue() & 255;
4787 unsigned Offset = I->getValue() - BytesLeft;
4788 SDOperand DstAddr = Op.getOperand(1);
4789 MVT::ValueType AddrVT = DstAddr.getValueType();
4790 if (BytesLeft >= 4) {
4791 Val = (Val << 8) | Val;
4792 Val = (Val << 16) | Val;
4793 Value = DAG.getConstant(Val, MVT::i32);
4794 Chain = DAG.getStore(Chain, Value,
4795 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4796 DAG.getConstant(Offset, AddrVT)),
4801 if (BytesLeft >= 2) {
4802 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4803 Chain = DAG.getStore(Chain, Value,
4804 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4805 DAG.getConstant(Offset, AddrVT)),
4810 if (BytesLeft == 1) {
4811 Value = DAG.getConstant(Val, MVT::i8);
4812 Chain = DAG.getStore(Chain, Value,
4813 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4814 DAG.getConstant(Offset, AddrVT)),
4822 SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4827 SelectionDAG &DAG) {
4829 unsigned BytesLeft = 0;
4830 switch (Align & 3) {
4831 case 2: // WORD aligned
4834 case 0: // DWORD aligned
4836 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4839 default: // Byte aligned
4844 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4845 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
4846 BytesLeft = Size % UBytes;
4848 SDOperand InFlag(0, 0);
4849 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4851 InFlag = Chain.getValue(1);
4852 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4854 InFlag = Chain.getValue(1);
4855 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4857 InFlag = Chain.getValue(1);
4859 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4860 SmallVector<SDOperand, 8> Ops;
4861 Ops.push_back(Chain);
4862 Ops.push_back(DAG.getValueType(AVT));
4863 Ops.push_back(InFlag);
4864 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4867 // Issue loads and stores for the last 1 - 7 bytes.
4868 unsigned Offset = Size - BytesLeft;
4869 SDOperand DstAddr = Dest;
4870 MVT::ValueType DstVT = DstAddr.getValueType();
4871 SDOperand SrcAddr = Source;
4872 MVT::ValueType SrcVT = SrcAddr.getValueType();
4874 if (BytesLeft >= 4) {
4875 Value = DAG.getLoad(MVT::i32, Chain,
4876 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4877 DAG.getConstant(Offset, SrcVT)),
4879 Chain = Value.getValue(1);
4880 Chain = DAG.getStore(Chain, Value,
4881 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4882 DAG.getConstant(Offset, DstVT)),
4887 if (BytesLeft >= 2) {
4888 Value = DAG.getLoad(MVT::i16, Chain,
4889 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4890 DAG.getConstant(Offset, SrcVT)),
4892 Chain = Value.getValue(1);
4893 Chain = DAG.getStore(Chain, Value,
4894 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4895 DAG.getConstant(Offset, DstVT)),
4901 if (BytesLeft == 1) {
4902 Value = DAG.getLoad(MVT::i8, Chain,
4903 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4904 DAG.getConstant(Offset, SrcVT)),
4906 Chain = Value.getValue(1);
4907 Chain = DAG.getStore(Chain, Value,
4908 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4909 DAG.getConstant(Offset, DstVT)),
4917 /// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4918 SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
4919 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4920 SDOperand TheChain = N->getOperand(0);
4921 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
4922 if (Subtarget->is64Bit()) {
4923 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4924 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4925 MVT::i64, rax.getValue(2));
4926 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
4927 DAG.getConstant(32, MVT::i8));
4929 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
4932 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4933 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
4936 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4937 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4938 MVT::i32, eax.getValue(2));
4939 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4940 SDOperand Ops[] = { eax, edx };
4941 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4943 // Use a MERGE_VALUES to return the value and chain.
4944 Ops[1] = edx.getValue(1);
4945 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4946 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
4949 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4950 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4952 if (!Subtarget->is64Bit()) {
4953 // vastart just stores the address of the VarArgsFrameIndex slot into the
4954 // memory location argument.
4955 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4956 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
4960 // gp_offset (0 - 6 * 8)
4961 // fp_offset (48 - 48 + 8 * 16)
4962 // overflow_arg_area (point to parameters coming in memory).
4964 SmallVector<SDOperand, 8> MemOps;
4965 SDOperand FIN = Op.getOperand(1);
4967 SDOperand Store = DAG.getStore(Op.getOperand(0),
4968 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4970 MemOps.push_back(Store);
4973 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
4974 Store = DAG.getStore(Op.getOperand(0),
4975 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4977 MemOps.push_back(Store);
4979 // Store ptr to overflow_arg_area
4980 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
4981 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4982 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
4983 MemOps.push_back(Store);
4985 // Store ptr to reg_save_area.
4986 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
4987 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4988 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
4989 MemOps.push_back(Store);
4990 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4993 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4994 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4995 SDOperand Chain = Op.getOperand(0);
4996 SDOperand DstPtr = Op.getOperand(1);
4997 SDOperand SrcPtr = Op.getOperand(2);
4998 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4999 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
5001 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
5002 Chain = SrcPtr.getValue(1);
5003 for (unsigned i = 0; i < 3; ++i) {
5004 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
5005 Chain = Val.getValue(1);
5006 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
5009 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
5010 DAG.getIntPtrConstant(8));
5011 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
5012 DAG.getIntPtrConstant(8));
5018 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5019 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5021 default: return SDOperand(); // Don't custom lower most intrinsics.
5022 // Comparison intrinsics.
5023 case Intrinsic::x86_sse_comieq_ss:
5024 case Intrinsic::x86_sse_comilt_ss:
5025 case Intrinsic::x86_sse_comile_ss:
5026 case Intrinsic::x86_sse_comigt_ss:
5027 case Intrinsic::x86_sse_comige_ss:
5028 case Intrinsic::x86_sse_comineq_ss:
5029 case Intrinsic::x86_sse_ucomieq_ss:
5030 case Intrinsic::x86_sse_ucomilt_ss:
5031 case Intrinsic::x86_sse_ucomile_ss:
5032 case Intrinsic::x86_sse_ucomigt_ss:
5033 case Intrinsic::x86_sse_ucomige_ss:
5034 case Intrinsic::x86_sse_ucomineq_ss:
5035 case Intrinsic::x86_sse2_comieq_sd:
5036 case Intrinsic::x86_sse2_comilt_sd:
5037 case Intrinsic::x86_sse2_comile_sd:
5038 case Intrinsic::x86_sse2_comigt_sd:
5039 case Intrinsic::x86_sse2_comige_sd:
5040 case Intrinsic::x86_sse2_comineq_sd:
5041 case Intrinsic::x86_sse2_ucomieq_sd:
5042 case Intrinsic::x86_sse2_ucomilt_sd:
5043 case Intrinsic::x86_sse2_ucomile_sd:
5044 case Intrinsic::x86_sse2_ucomigt_sd:
5045 case Intrinsic::x86_sse2_ucomige_sd:
5046 case Intrinsic::x86_sse2_ucomineq_sd: {
5048 ISD::CondCode CC = ISD::SETCC_INVALID;
5051 case Intrinsic::x86_sse_comieq_ss:
5052 case Intrinsic::x86_sse2_comieq_sd:
5056 case Intrinsic::x86_sse_comilt_ss:
5057 case Intrinsic::x86_sse2_comilt_sd:
5061 case Intrinsic::x86_sse_comile_ss:
5062 case Intrinsic::x86_sse2_comile_sd:
5066 case Intrinsic::x86_sse_comigt_ss:
5067 case Intrinsic::x86_sse2_comigt_sd:
5071 case Intrinsic::x86_sse_comige_ss:
5072 case Intrinsic::x86_sse2_comige_sd:
5076 case Intrinsic::x86_sse_comineq_ss:
5077 case Intrinsic::x86_sse2_comineq_sd:
5081 case Intrinsic::x86_sse_ucomieq_ss:
5082 case Intrinsic::x86_sse2_ucomieq_sd:
5083 Opc = X86ISD::UCOMI;
5086 case Intrinsic::x86_sse_ucomilt_ss:
5087 case Intrinsic::x86_sse2_ucomilt_sd:
5088 Opc = X86ISD::UCOMI;
5091 case Intrinsic::x86_sse_ucomile_ss:
5092 case Intrinsic::x86_sse2_ucomile_sd:
5093 Opc = X86ISD::UCOMI;
5096 case Intrinsic::x86_sse_ucomigt_ss:
5097 case Intrinsic::x86_sse2_ucomigt_sd:
5098 Opc = X86ISD::UCOMI;
5101 case Intrinsic::x86_sse_ucomige_ss:
5102 case Intrinsic::x86_sse2_ucomige_sd:
5103 Opc = X86ISD::UCOMI;
5106 case Intrinsic::x86_sse_ucomineq_ss:
5107 case Intrinsic::x86_sse2_ucomineq_sd:
5108 Opc = X86ISD::UCOMI;
5114 SDOperand LHS = Op.getOperand(1);
5115 SDOperand RHS = Op.getOperand(2);
5116 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5118 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5119 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5120 DAG.getConstant(X86CC, MVT::i8), Cond);
5121 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
5126 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5127 // Depths > 0 not supported yet!
5128 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5131 // Just load the return address
5132 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5133 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5136 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5137 // Depths > 0 not supported yet!
5138 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5141 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5142 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
5143 DAG.getIntPtrConstant(4));
5146 SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5147 SelectionDAG &DAG) {
5148 // Is not yet supported on x86-64
5149 if (Subtarget->is64Bit())
5152 return DAG.getIntPtrConstant(8);
5155 SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5157 assert(!Subtarget->is64Bit() &&
5158 "Lowering of eh_return builtin is not supported yet on x86-64");
5160 MachineFunction &MF = DAG.getMachineFunction();
5161 SDOperand Chain = Op.getOperand(0);
5162 SDOperand Offset = Op.getOperand(1);
5163 SDOperand Handler = Op.getOperand(2);
5165 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5168 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
5169 DAG.getIntPtrConstant(-4UL));
5170 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5171 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5172 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
5173 MF.getRegInfo().addLiveOut(X86::ECX);
5175 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5176 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5179 SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5180 SelectionDAG &DAG) {
5181 SDOperand Root = Op.getOperand(0);
5182 SDOperand Trmp = Op.getOperand(1); // trampoline
5183 SDOperand FPtr = Op.getOperand(2); // nested function
5184 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5186 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
5188 const X86InstrInfo *TII =
5189 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5191 if (Subtarget->is64Bit()) {
5192 SDOperand OutChains[6];
5194 // Large code-model.
5196 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5197 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5199 const unsigned char N86R10 =
5200 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
5201 const unsigned char N86R11 =
5202 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
5204 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5206 // Load the pointer to the nested function into R11.
5207 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5208 SDOperand Addr = Trmp;
5209 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5212 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
5213 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
5215 // Load the 'nest' parameter value into R10.
5216 // R10 is specified in X86CallingConv.td
5217 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5218 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5219 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5222 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
5223 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
5225 // Jump to the nested function.
5226 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5227 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5228 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5231 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5232 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5233 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
5237 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5238 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
5240 const Function *Func =
5241 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5242 unsigned CC = Func->getCallingConv();
5247 assert(0 && "Unsupported calling convention");
5248 case CallingConv::C:
5249 case CallingConv::X86_StdCall: {
5250 // Pass 'nest' parameter in ECX.
5251 // Must be kept in sync with X86CallingConv.td
5254 // Check that ECX wasn't needed by an 'inreg' parameter.
5255 const FunctionType *FTy = Func->getFunctionType();
5256 const ParamAttrsList *Attrs = Func->getParamAttrs();
5258 if (Attrs && !Func->isVarArg()) {
5259 unsigned InRegCount = 0;
5262 for (FunctionType::param_iterator I = FTy->param_begin(),
5263 E = FTy->param_end(); I != E; ++I, ++Idx)
5264 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5265 // FIXME: should only count parameters that are lowered to integers.
5266 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5268 if (InRegCount > 2) {
5269 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5275 case CallingConv::X86_FastCall:
5276 // Pass 'nest' parameter in EAX.
5277 // Must be kept in sync with X86CallingConv.td
5282 SDOperand OutChains[4];
5283 SDOperand Addr, Disp;
5285 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5286 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5288 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5289 const unsigned char N86Reg =
5290 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
5291 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
5294 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5295 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
5297 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
5298 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5299 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5300 TrmpAddr, 5, false, 1);
5302 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5303 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
5306 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5307 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
5311 SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
5313 The rounding mode is in bits 11:10 of FPSR, and has the following
5320 FLT_ROUNDS, on the other hand, expects the following:
5327 To perform the conversion, we do:
5328 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5331 MachineFunction &MF = DAG.getMachineFunction();
5332 const TargetMachine &TM = MF.getTarget();
5333 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5334 unsigned StackAlignment = TFI.getStackAlignment();
5335 MVT::ValueType VT = Op.getValueType();
5337 // Save FP Control Word to stack slot
5338 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5339 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5341 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5342 DAG.getEntryNode(), StackSlot);
5344 // Load FP Control Word from stack slot
5345 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5347 // Transform as necessary
5349 DAG.getNode(ISD::SRL, MVT::i16,
5350 DAG.getNode(ISD::AND, MVT::i16,
5351 CWD, DAG.getConstant(0x800, MVT::i16)),
5352 DAG.getConstant(11, MVT::i8));
5354 DAG.getNode(ISD::SRL, MVT::i16,
5355 DAG.getNode(ISD::AND, MVT::i16,
5356 CWD, DAG.getConstant(0x400, MVT::i16)),
5357 DAG.getConstant(9, MVT::i8));
5360 DAG.getNode(ISD::AND, MVT::i16,
5361 DAG.getNode(ISD::ADD, MVT::i16,
5362 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5363 DAG.getConstant(1, MVT::i16)),
5364 DAG.getConstant(3, MVT::i16));
5367 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5368 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5371 SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5372 MVT::ValueType VT = Op.getValueType();
5373 MVT::ValueType OpVT = VT;
5374 unsigned NumBits = MVT::getSizeInBits(VT);
5376 Op = Op.getOperand(0);
5377 if (VT == MVT::i8) {
5378 // Zero extend to i32 since there is not an i8 bsr.
5380 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5383 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5384 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5385 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5387 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5388 SmallVector<SDOperand, 4> Ops;
5390 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5391 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5392 Ops.push_back(Op.getValue(1));
5393 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5395 // Finally xor with NumBits-1.
5396 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5399 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5403 SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5404 MVT::ValueType VT = Op.getValueType();
5405 MVT::ValueType OpVT = VT;
5406 unsigned NumBits = MVT::getSizeInBits(VT);
5408 Op = Op.getOperand(0);
5409 if (VT == MVT::i8) {
5411 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5414 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5415 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5416 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5418 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5419 SmallVector<SDOperand, 4> Ops;
5421 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5422 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5423 Ops.push_back(Op.getValue(1));
5424 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5427 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5431 SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
5432 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
5436 case MVT::i8: Reg = X86::AL; size = 1; break;
5437 case MVT::i16: Reg = X86::AX; size = 2; break;
5438 case MVT::i32: Reg = X86::EAX; size = 4; break;
5440 if (Subtarget->is64Bit()) {
5441 Reg = X86::RAX; size = 8;
5442 } else //Should go away when LowerType stuff lands
5443 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5446 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5447 Op.getOperand(3), SDOperand());
5448 SDOperand Ops[] = { cpIn.getValue(0),
5451 DAG.getTargetConstant(size, MVT::i8),
5453 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5454 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5456 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5460 SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5461 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5462 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5463 SDOperand cpInL, cpInH;
5464 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5465 DAG.getConstant(0, MVT::i32));
5466 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5467 DAG.getConstant(1, MVT::i32));
5468 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5469 cpInL, SDOperand());
5470 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5471 cpInH, cpInL.getValue(1));
5472 SDOperand swapInL, swapInH;
5473 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5474 DAG.getConstant(0, MVT::i32));
5475 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5476 DAG.getConstant(1, MVT::i32));
5477 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5478 swapInL, cpInH.getValue(1));
5479 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5480 swapInH, swapInL.getValue(1));
5481 SDOperand Ops[] = { swapInH.getValue(0),
5483 swapInH.getValue(1)};
5484 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5485 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5486 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5487 Result.getValue(1));
5488 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5489 cpOutL.getValue(2));
5490 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5491 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5492 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5493 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5496 /// LowerOperation - Provide custom lowering hooks for some operations.
5498 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5499 switch (Op.getOpcode()) {
5500 default: assert(0 && "Should not custom lower this!");
5501 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
5502 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5503 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5504 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5505 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5506 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5507 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5508 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5509 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5510 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5511 case ISD::SHL_PARTS:
5512 case ISD::SRA_PARTS:
5513 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5514 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5515 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5516 case ISD::FABS: return LowerFABS(Op, DAG);
5517 case ISD::FNEG: return LowerFNEG(Op, DAG);
5518 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
5519 case ISD::SETCC: return LowerSETCC(Op, DAG);
5520 case ISD::SELECT: return LowerSELECT(Op, DAG);
5521 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
5522 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5523 case ISD::CALL: return LowerCALL(Op, DAG);
5524 case ISD::RET: return LowerRET(Op, DAG);
5525 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5526 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5527 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
5528 case ISD::VASTART: return LowerVASTART(Op, DAG);
5529 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5530 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5531 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5532 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5533 case ISD::FRAME_TO_ARGS_OFFSET:
5534 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5535 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5536 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
5537 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
5538 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
5539 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5540 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
5542 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5543 case ISD::READCYCLECOUNTER:
5544 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
5548 /// ExpandOperation - Provide custom lowering hooks for expanding operations.
5549 SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5550 switch (N->getOpcode()) {
5551 default: assert(0 && "Should not custom lower this!");
5552 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5553 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5554 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
5558 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5560 default: return NULL;
5561 case X86ISD::BSF: return "X86ISD::BSF";
5562 case X86ISD::BSR: return "X86ISD::BSR";
5563 case X86ISD::SHLD: return "X86ISD::SHLD";
5564 case X86ISD::SHRD: return "X86ISD::SHRD";
5565 case X86ISD::FAND: return "X86ISD::FAND";
5566 case X86ISD::FOR: return "X86ISD::FOR";
5567 case X86ISD::FXOR: return "X86ISD::FXOR";
5568 case X86ISD::FSRL: return "X86ISD::FSRL";
5569 case X86ISD::FILD: return "X86ISD::FILD";
5570 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5571 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5572 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5573 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5574 case X86ISD::FLD: return "X86ISD::FLD";
5575 case X86ISD::FST: return "X86ISD::FST";
5576 case X86ISD::FP_GET_ST0: return "X86ISD::FP_GET_ST0";
5577 case X86ISD::FP_GET_ST0_ST1: return "X86ISD::FP_GET_ST0_ST1";
5578 case X86ISD::FP_SET_ST0: return "X86ISD::FP_SET_ST0";
5579 case X86ISD::CALL: return "X86ISD::CALL";
5580 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5581 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5582 case X86ISD::CMP: return "X86ISD::CMP";
5583 case X86ISD::COMI: return "X86ISD::COMI";
5584 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5585 case X86ISD::SETCC: return "X86ISD::SETCC";
5586 case X86ISD::CMOV: return "X86ISD::CMOV";
5587 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5588 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5589 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5590 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
5591 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5592 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5593 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
5594 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5595 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5596 case X86ISD::PINSRB: return "X86ISD::PINSRB";
5597 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5598 case X86ISD::FMAX: return "X86ISD::FMAX";
5599 case X86ISD::FMIN: return "X86ISD::FMIN";
5600 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5601 case X86ISD::FRCP: return "X86ISD::FRCP";
5602 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5603 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5604 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
5605 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
5606 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
5607 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
5608 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
5612 // isLegalAddressingMode - Return true if the addressing mode represented
5613 // by AM is legal for this target, for a load/store of the specified type.
5614 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5615 const Type *Ty) const {
5616 // X86 supports extremely general addressing modes.
5618 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5619 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5623 // We can only fold this if we don't need an extra load.
5624 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5627 // X86-64 only supports addr of globals in small code model.
5628 if (Subtarget->is64Bit()) {
5629 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5631 // If lower 4G is not available, then we must use rip-relative addressing.
5632 if (AM.BaseOffs || AM.Scale > 1)
5643 // These scales always work.
5648 // These scales are formed with basereg+scalereg. Only accept if there is
5653 default: // Other stuff never works.
5661 bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5662 if (!Ty1->isInteger() || !Ty2->isInteger())
5664 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5665 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5666 if (NumBits1 <= NumBits2)
5668 return Subtarget->is64Bit() || NumBits1 < 64;
5671 bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5672 MVT::ValueType VT2) const {
5673 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5675 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5676 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5677 if (NumBits1 <= NumBits2)
5679 return Subtarget->is64Bit() || NumBits1 < 64;
5682 /// isShuffleMaskLegal - Targets can use this to indicate that they only
5683 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5684 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5685 /// are assumed to be legal.
5687 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5688 // Only do shuffles on 128-bit vector types for now.
5689 if (MVT::getSizeInBits(VT) == 64) return false;
5690 return (Mask.Val->getNumOperands() <= 4 ||
5691 isIdentityMask(Mask.Val) ||
5692 isIdentityMask(Mask.Val, true) ||
5693 isSplatMask(Mask.Val) ||
5694 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5695 X86::isUNPCKLMask(Mask.Val) ||
5696 X86::isUNPCKHMask(Mask.Val) ||
5697 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5698 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5701 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5703 SelectionDAG &DAG) const {
5704 unsigned NumElts = BVOps.size();
5705 // Only do shuffles on 128-bit vector types for now.
5706 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5707 if (NumElts == 2) return true;
5709 return (isMOVLMask(&BVOps[0], 4) ||
5710 isCommutedMOVL(&BVOps[0], 4, true) ||
5711 isSHUFPMask(&BVOps[0], 4) ||
5712 isCommutedSHUFP(&BVOps[0], 4));
5717 //===----------------------------------------------------------------------===//
5718 // X86 Scheduler Hooks
5719 //===----------------------------------------------------------------------===//
5722 X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5723 MachineBasicBlock *BB) {
5724 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5725 switch (MI->getOpcode()) {
5726 default: assert(false && "Unexpected instr type to insert");
5727 case X86::CMOV_FR32:
5728 case X86::CMOV_FR64:
5729 case X86::CMOV_V4F32:
5730 case X86::CMOV_V2F64:
5731 case X86::CMOV_V2I64: {
5732 // To "insert" a SELECT_CC instruction, we actually have to insert the
5733 // diamond control-flow pattern. The incoming instruction knows the
5734 // destination vreg to set, the condition code register to branch on, the
5735 // true/false values to select between, and a branch opcode to use.
5736 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5737 ilist<MachineBasicBlock>::iterator It = BB;
5743 // cmpTY ccX, r1, r2
5745 // fallthrough --> copy0MBB
5746 MachineBasicBlock *thisMBB = BB;
5747 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5748 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5750 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5751 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5752 MachineFunction *F = BB->getParent();
5753 F->getBasicBlockList().insert(It, copy0MBB);
5754 F->getBasicBlockList().insert(It, sinkMBB);
5755 // Update machine-CFG edges by first adding all successors of the current
5756 // block to the new block which will contain the Phi node for the select.
5757 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5758 e = BB->succ_end(); i != e; ++i)
5759 sinkMBB->addSuccessor(*i);
5760 // Next, remove all successors of the current block, and add the true
5761 // and fallthrough blocks as its successors.
5762 while(!BB->succ_empty())
5763 BB->removeSuccessor(BB->succ_begin());
5764 BB->addSuccessor(copy0MBB);
5765 BB->addSuccessor(sinkMBB);
5768 // %FalseValue = ...
5769 // # fallthrough to sinkMBB
5772 // Update machine-CFG edges
5773 BB->addSuccessor(sinkMBB);
5776 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5779 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5780 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5781 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5783 delete MI; // The pseudo instruction is gone now.
5787 case X86::FP32_TO_INT16_IN_MEM:
5788 case X86::FP32_TO_INT32_IN_MEM:
5789 case X86::FP32_TO_INT64_IN_MEM:
5790 case X86::FP64_TO_INT16_IN_MEM:
5791 case X86::FP64_TO_INT32_IN_MEM:
5792 case X86::FP64_TO_INT64_IN_MEM:
5793 case X86::FP80_TO_INT16_IN_MEM:
5794 case X86::FP80_TO_INT32_IN_MEM:
5795 case X86::FP80_TO_INT64_IN_MEM: {
5796 // Change the floating point control register to use "round towards zero"
5797 // mode when truncating to an integer value.
5798 MachineFunction *F = BB->getParent();
5799 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5800 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5802 // Load the old value of the high byte of the control word...
5804 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
5805 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5807 // Set the high part to be round to zero...
5808 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5811 // Reload the modified control word now...
5812 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5814 // Restore the memory image of control word to original value
5815 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5818 // Get the X86 opcode to use.
5820 switch (MI->getOpcode()) {
5821 default: assert(0 && "illegal opcode!");
5822 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5823 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5824 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5825 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5826 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5827 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
5828 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5829 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5830 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
5834 MachineOperand &Op = MI->getOperand(0);
5835 if (Op.isRegister()) {
5836 AM.BaseType = X86AddressMode::RegBase;
5837 AM.Base.Reg = Op.getReg();
5839 AM.BaseType = X86AddressMode::FrameIndexBase;
5840 AM.Base.FrameIndex = Op.getIndex();
5842 Op = MI->getOperand(1);
5843 if (Op.isImmediate())
5844 AM.Scale = Op.getImm();
5845 Op = MI->getOperand(2);
5846 if (Op.isImmediate())
5847 AM.IndexReg = Op.getImm();
5848 Op = MI->getOperand(3);
5849 if (Op.isGlobalAddress()) {
5850 AM.GV = Op.getGlobal();
5852 AM.Disp = Op.getImm();
5854 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5855 .addReg(MI->getOperand(4).getReg());
5857 // Reload the original control word now.
5858 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5860 delete MI; // The pseudo instruction is gone now.
5866 //===----------------------------------------------------------------------===//
5867 // X86 Optimization Hooks
5868 //===----------------------------------------------------------------------===//
5870 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5874 const SelectionDAG &DAG,
5875 unsigned Depth) const {
5876 unsigned Opc = Op.getOpcode();
5877 assert((Opc >= ISD::BUILTIN_OP_END ||
5878 Opc == ISD::INTRINSIC_WO_CHAIN ||
5879 Opc == ISD::INTRINSIC_W_CHAIN ||
5880 Opc == ISD::INTRINSIC_VOID) &&
5881 "Should use MaskedValueIsZero if you don't know whether Op"
5882 " is a target node!");
5884 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
5888 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5889 Mask.getBitWidth() - 1);
5894 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
5895 /// element of the result of the vector shuffle.
5896 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5897 MVT::ValueType VT = N->getValueType(0);
5898 SDOperand PermMask = N->getOperand(2);
5899 unsigned NumElems = PermMask.getNumOperands();
5900 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5902 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5904 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5905 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5906 SDOperand Idx = PermMask.getOperand(i);
5907 if (Idx.getOpcode() == ISD::UNDEF)
5908 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5909 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5914 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5915 /// node is a GlobalAddress + an offset.
5916 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5917 unsigned Opc = N->getOpcode();
5918 if (Opc == X86ISD::Wrapper) {
5919 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5920 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5923 } else if (Opc == ISD::ADD) {
5924 SDOperand N1 = N->getOperand(0);
5925 SDOperand N2 = N->getOperand(1);
5926 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5927 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5929 Offset += V->getSignExtended();
5932 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5933 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5935 Offset += V->getSignExtended();
5943 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
5945 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5946 MachineFrameInfo *MFI) {
5947 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5950 SDOperand Loc = N->getOperand(1);
5951 SDOperand BaseLoc = Base->getOperand(1);
5952 if (Loc.getOpcode() == ISD::FrameIndex) {
5953 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5955 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5956 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5957 int FS = MFI->getObjectSize(FI);
5958 int BFS = MFI->getObjectSize(BFI);
5959 if (FS != BFS || FS != Size) return false;
5960 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5962 GlobalValue *GV1 = NULL;
5963 GlobalValue *GV2 = NULL;
5964 int64_t Offset1 = 0;
5965 int64_t Offset2 = 0;
5966 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5967 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5968 if (isGA1 && isGA2 && GV1 == GV2)
5969 return Offset1 == (Offset2 + Dist*Size);
5975 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5976 const X86Subtarget *Subtarget) {
5979 if (isGAPlusOffset(Base, GV, Offset))
5980 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5981 // DAG combine handles the stack object case.
5986 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5987 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5988 /// if the load addresses are consecutive, non-overlapping, and in the right
5990 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5991 const X86Subtarget *Subtarget) {
5992 MachineFunction &MF = DAG.getMachineFunction();
5993 MachineFrameInfo *MFI = MF.getFrameInfo();
5994 MVT::ValueType VT = N->getValueType(0);
5995 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5996 SDOperand PermMask = N->getOperand(2);
5997 int NumElems = (int)PermMask.getNumOperands();
5998 SDNode *Base = NULL;
5999 for (int i = 0; i < NumElems; ++i) {
6000 SDOperand Idx = PermMask.getOperand(i);
6001 if (Idx.getOpcode() == ISD::UNDEF) {
6002 if (!Base) return SDOperand();
6005 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
6006 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
6010 else if (!isConsecutiveLoad(Arg.Val, Base,
6011 i, MVT::getSizeInBits(EVT)/8,MFI))
6016 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
6017 LoadSDNode *LD = cast<LoadSDNode>(Base);
6019 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6020 LD->getSrcValueOffset(), LD->isVolatile());
6022 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6023 LD->getSrcValueOffset(), LD->isVolatile(),
6024 LD->getAlignment());
6028 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6029 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6030 const X86Subtarget *Subtarget) {
6031 SDOperand Cond = N->getOperand(0);
6033 // If we have SSE[12] support, try to form min/max nodes.
6034 if (Subtarget->hasSSE2() &&
6035 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6036 if (Cond.getOpcode() == ISD::SETCC) {
6037 // Get the LHS/RHS of the select.
6038 SDOperand LHS = N->getOperand(1);
6039 SDOperand RHS = N->getOperand(2);
6040 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6042 unsigned Opcode = 0;
6043 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6046 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6049 if (!UnsafeFPMath) break;
6051 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6053 Opcode = X86ISD::FMIN;
6056 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6059 if (!UnsafeFPMath) break;
6061 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6063 Opcode = X86ISD::FMAX;
6066 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6069 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6072 if (!UnsafeFPMath) break;
6074 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6076 Opcode = X86ISD::FMIN;
6079 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6082 if (!UnsafeFPMath) break;
6084 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6086 Opcode = X86ISD::FMAX;
6092 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6100 /// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6101 static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6102 const X86Subtarget *Subtarget) {
6103 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6104 // the FP state in cases where an emms may be missing.
6105 // A preferable solution to the general problem is to figure out the right
6106 // places to insert EMMS. This qualifies as a quick hack.
6107 if (MVT::isVector(St->getValue().getValueType()) &&
6108 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
6109 isa<LoadSDNode>(St->getValue()) &&
6110 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6111 St->getChain().hasOneUse() && !St->isVolatile()) {
6112 SDNode* LdVal = St->getValue().Val;
6114 int TokenFactorIndex = -1;
6115 SmallVector<SDOperand, 8> Ops;
6116 SDNode* ChainVal = St->getChain().Val;
6117 // Must be a store of a load. We currently handle two cases: the load
6118 // is a direct child, and it's under an intervening TokenFactor. It is
6119 // possible to dig deeper under nested TokenFactors.
6120 if (ChainVal == LdVal)
6121 Ld = cast<LoadSDNode>(St->getChain());
6122 else if (St->getValue().hasOneUse() &&
6123 ChainVal->getOpcode() == ISD::TokenFactor) {
6124 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
6125 if (ChainVal->getOperand(i).Val == LdVal) {
6126 TokenFactorIndex = i;
6127 Ld = cast<LoadSDNode>(St->getValue());
6129 Ops.push_back(ChainVal->getOperand(i));
6133 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6134 if (Subtarget->is64Bit()) {
6135 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6136 Ld->getBasePtr(), Ld->getSrcValue(),
6137 Ld->getSrcValueOffset(), Ld->isVolatile(),
6138 Ld->getAlignment());
6139 SDOperand NewChain = NewLd.getValue(1);
6140 if (TokenFactorIndex != -1) {
6141 Ops.push_back(NewLd);
6142 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6145 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6146 St->getSrcValue(), St->getSrcValueOffset(),
6147 St->isVolatile(), St->getAlignment());
6150 // Otherwise, lower to two 32-bit copies.
6151 SDOperand LoAddr = Ld->getBasePtr();
6152 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6153 DAG.getConstant(MVT::i32, 4));
6155 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6156 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6157 Ld->isVolatile(), Ld->getAlignment());
6158 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6159 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6161 MinAlign(Ld->getAlignment(), 4));
6163 SDOperand NewChain = LoLd.getValue(1);
6164 if (TokenFactorIndex != -1) {
6165 Ops.push_back(LoLd);
6166 Ops.push_back(HiLd);
6167 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6171 LoAddr = St->getBasePtr();
6172 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6173 DAG.getConstant(MVT::i32, 4));
6175 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
6176 St->getSrcValue(), St->getSrcValueOffset(),
6177 St->isVolatile(), St->getAlignment());
6178 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6179 St->getSrcValue(), St->getSrcValueOffset()+4,
6181 MinAlign(St->getAlignment(), 4));
6182 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
6188 /// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6189 /// X86ISD::FXOR nodes.
6190 static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
6191 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6192 // F[X]OR(0.0, x) -> x
6193 // F[X]OR(x, 0.0) -> x
6194 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6195 if (C->getValueAPF().isPosZero())
6196 return N->getOperand(1);
6197 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6198 if (C->getValueAPF().isPosZero())
6199 return N->getOperand(0);
6203 /// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6204 static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6205 // FAND(0.0, x) -> 0.0
6206 // FAND(x, 0.0) -> 0.0
6207 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6208 if (C->getValueAPF().isPosZero())
6209 return N->getOperand(0);
6210 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6211 if (C->getValueAPF().isPosZero())
6212 return N->getOperand(1);
6217 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6218 DAGCombinerInfo &DCI) const {
6219 SelectionDAG &DAG = DCI.DAG;
6220 switch (N->getOpcode()) {
6222 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6223 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
6225 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
6227 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6228 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
6234 //===----------------------------------------------------------------------===//
6235 // X86 Inline Assembly Support
6236 //===----------------------------------------------------------------------===//
6238 /// getConstraintType - Given a constraint letter, return the type of
6239 /// constraint it is for this target.
6240 X86TargetLowering::ConstraintType
6241 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6242 if (Constraint.size() == 1) {
6243 switch (Constraint[0]) {
6252 return C_RegisterClass;
6257 return TargetLowering::getConstraintType(Constraint);
6260 /// LowerXConstraint - try to replace an X constraint, which matches anything,
6261 /// with another that has more specific requirements based on the type of the
6262 /// corresponding operand.
6263 void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6264 std::string& s) const {
6265 if (MVT::isFloatingPoint(ConstraintVT)) {
6266 if (Subtarget->hasSSE2())
6268 else if (Subtarget->hasSSE1())
6273 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6276 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6277 /// vector. If it is invalid, don't add anything to Ops.
6278 void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6280 std::vector<SDOperand>&Ops,
6281 SelectionDAG &DAG) {
6282 SDOperand Result(0, 0);
6284 switch (Constraint) {
6287 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
6288 if (C->getValue() <= 31) {
6289 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6295 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
6296 if (C->getValue() <= 255) {
6297 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6303 // Literal immediates are always ok.
6304 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6305 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6309 // If we are in non-pic codegen mode, we allow the address of a global (with
6310 // an optional displacement) to be used with 'i'.
6311 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6314 // Match either (GA) or (GA+C)
6316 Offset = GA->getOffset();
6317 } else if (Op.getOpcode() == ISD::ADD) {
6318 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6319 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6321 Offset = GA->getOffset()+C->getValue();
6323 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6324 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6326 Offset = GA->getOffset()+C->getValue();
6333 // If addressing this global requires a load (e.g. in PIC mode), we can't
6335 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6339 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6345 // Otherwise, not valid for this mode.
6351 Ops.push_back(Result);
6354 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
6357 std::vector<unsigned> X86TargetLowering::
6358 getRegClassForInlineAsmConstraint(const std::string &Constraint,
6359 MVT::ValueType VT) const {
6360 if (Constraint.size() == 1) {
6361 // FIXME: not handling fp-stack yet!
6362 switch (Constraint[0]) { // GCC X86 Constraint Letters
6363 default: break; // Unknown constraint letter
6364 case 'A': // EAX/EDX
6365 if (VT == MVT::i32 || VT == MVT::i64)
6366 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6368 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6371 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6372 else if (VT == MVT::i16)
6373 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6374 else if (VT == MVT::i8)
6375 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
6376 else if (VT == MVT::i64)
6377 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6382 return std::vector<unsigned>();
6385 std::pair<unsigned, const TargetRegisterClass*>
6386 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6387 MVT::ValueType VT) const {
6388 // First, see if this is a constraint that directly corresponds to an LLVM
6390 if (Constraint.size() == 1) {
6391 // GCC Constraint Letters
6392 switch (Constraint[0]) {
6394 case 'r': // GENERAL_REGS
6395 case 'R': // LEGACY_REGS
6396 case 'l': // INDEX_REGS
6397 if (VT == MVT::i64 && Subtarget->is64Bit())
6398 return std::make_pair(0U, X86::GR64RegisterClass);
6400 return std::make_pair(0U, X86::GR32RegisterClass);
6401 else if (VT == MVT::i16)
6402 return std::make_pair(0U, X86::GR16RegisterClass);
6403 else if (VT == MVT::i8)
6404 return std::make_pair(0U, X86::GR8RegisterClass);
6406 case 'y': // MMX_REGS if MMX allowed.
6407 if (!Subtarget->hasMMX()) break;
6408 return std::make_pair(0U, X86::VR64RegisterClass);
6410 case 'Y': // SSE_REGS if SSE2 allowed
6411 if (!Subtarget->hasSSE2()) break;
6413 case 'x': // SSE_REGS if SSE1 allowed
6414 if (!Subtarget->hasSSE1()) break;
6418 // Scalar SSE types.
6421 return std::make_pair(0U, X86::FR32RegisterClass);
6424 return std::make_pair(0U, X86::FR64RegisterClass);
6432 return std::make_pair(0U, X86::VR128RegisterClass);
6438 // Use the default implementation in TargetLowering to convert the register
6439 // constraint into a member of a register class.
6440 std::pair<unsigned, const TargetRegisterClass*> Res;
6441 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6443 // Not found as a standard register?
6444 if (Res.second == 0) {
6445 // GCC calls "st(0)" just plain "st".
6446 if (StringsEqualNoCase("{st}", Constraint)) {
6447 Res.first = X86::ST0;
6448 Res.second = X86::RFP80RegisterClass;
6454 // Otherwise, check to see if this is a register class of the wrong value
6455 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6456 // turn into {ax},{dx}.
6457 if (Res.second->hasType(VT))
6458 return Res; // Correct type already, nothing to do.
6460 // All of the single-register GCC register classes map their values onto
6461 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6462 // really want an 8-bit or 32-bit register, map to the appropriate register
6463 // class and return the appropriate register.
6464 if (Res.second != X86::GR16RegisterClass)
6467 if (VT == MVT::i8) {
6468 unsigned DestReg = 0;
6469 switch (Res.first) {
6471 case X86::AX: DestReg = X86::AL; break;
6472 case X86::DX: DestReg = X86::DL; break;
6473 case X86::CX: DestReg = X86::CL; break;
6474 case X86::BX: DestReg = X86::BL; break;
6477 Res.first = DestReg;
6478 Res.second = Res.second = X86::GR8RegisterClass;
6480 } else if (VT == MVT::i32) {
6481 unsigned DestReg = 0;
6482 switch (Res.first) {
6484 case X86::AX: DestReg = X86::EAX; break;
6485 case X86::DX: DestReg = X86::EDX; break;
6486 case X86::CX: DestReg = X86::ECX; break;
6487 case X86::BX: DestReg = X86::EBX; break;
6488 case X86::SI: DestReg = X86::ESI; break;
6489 case X86::DI: DestReg = X86::EDI; break;
6490 case X86::BP: DestReg = X86::EBP; break;
6491 case X86::SP: DestReg = X86::ESP; break;
6494 Res.first = DestReg;
6495 Res.second = Res.second = X86::GR32RegisterClass;
6497 } else if (VT == MVT::i64) {
6498 unsigned DestReg = 0;
6499 switch (Res.first) {
6501 case X86::AX: DestReg = X86::RAX; break;
6502 case X86::DX: DestReg = X86::RDX; break;
6503 case X86::CX: DestReg = X86::RCX; break;
6504 case X86::BX: DestReg = X86::RBX; break;
6505 case X86::SI: DestReg = X86::RSI; break;
6506 case X86::DI: DestReg = X86::RDI; break;
6507 case X86::BP: DestReg = X86::RBP; break;
6508 case X86::SP: DestReg = X86::RSP; break;
6511 Res.first = DestReg;
6512 Res.second = Res.second = X86::GR64RegisterClass;