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 setSetCCResultType(MVT::i8);
61 setSetCCResultContents(ZeroOrOneSetCCResult);
62 setSchedulingPreference(SchedulingForRegPressure);
63 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
64 setStackPointerRegisterToSaveRestore(X86StackPtr);
66 if (Subtarget->isTargetDarwin()) {
67 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
68 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
70 } else if (Subtarget->isTargetMingw()) {
71 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
79 // Set up the register classes.
80 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
83 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
86 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
88 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
117 // SSE has no i16 to fp conversion, only i32
118 if (X86ScalarSSEf32) {
119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
137 if (X86ScalarSSEf32) {
138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
167 if (!X86ScalarSSEf64) {
168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
211 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
212 if (Subtarget->is64Bit())
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
218 setOperationAction(ISD::FREM , MVT::f32 , Expand);
219 setOperationAction(ISD::FREM , MVT::f64 , Expand);
220 setOperationAction(ISD::FREM , MVT::f80 , Expand);
221 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
223 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
224 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
226 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
227 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
229 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
230 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
231 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
232 if (Subtarget->is64Bit()) {
233 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
234 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
238 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
239 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
241 // These should be promoted to a larger select which is supported.
242 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
243 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
244 // X86 wants to expand cmov itself.
245 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
246 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
248 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
249 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
252 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
254 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
255 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
256 if (Subtarget->is64Bit()) {
257 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
258 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
260 // X86 ret instruction may pop stack.
261 setOperationAction(ISD::RET , MVT::Other, Custom);
262 if (!Subtarget->is64Bit())
263 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
266 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
267 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
269 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
270 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
271 if (Subtarget->is64Bit()) {
272 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
273 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
274 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
275 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
277 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
278 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
279 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
281 if (Subtarget->is64Bit()) {
282 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
283 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
286 // X86 wants to expand memset / memcpy itself.
287 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
288 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
290 if (!Subtarget->hasSSE2())
291 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
293 setOperationAction(ISD::ATOMIC_LCS , MVT::i8, Custom);
294 setOperationAction(ISD::ATOMIC_LCS , MVT::i16, Custom);
295 setOperationAction(ISD::ATOMIC_LCS , MVT::i32, Custom);
296 setOperationAction(ISD::ATOMIC_LCS , MVT::i64, Custom);
298 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
299 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
300 // FIXME - use subtarget debug flags
301 if (!Subtarget->isTargetDarwin() &&
302 !Subtarget->isTargetELF() &&
303 !Subtarget->isTargetCygMing())
304 setOperationAction(ISD::LABEL, MVT::Other, Expand);
306 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
307 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
308 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
309 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
310 if (Subtarget->is64Bit()) {
312 setExceptionPointerRegister(X86::RAX);
313 setExceptionSelectorRegister(X86::RDX);
315 setExceptionPointerRegister(X86::EAX);
316 setExceptionSelectorRegister(X86::EDX);
318 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
320 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
322 setOperationAction(ISD::TRAP, MVT::Other, Legal);
324 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
325 setOperationAction(ISD::VASTART , MVT::Other, Custom);
326 setOperationAction(ISD::VAARG , MVT::Other, Expand);
327 setOperationAction(ISD::VAEND , MVT::Other, Expand);
328 if (Subtarget->is64Bit())
329 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
331 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
333 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
334 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
335 if (Subtarget->is64Bit())
336 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
337 if (Subtarget->isTargetCygMing())
338 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
340 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
342 if (X86ScalarSSEf64) {
343 // f32 and f64 use SSE.
344 // Set up the FP register classes.
345 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
346 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
348 // Use ANDPD to simulate FABS.
349 setOperationAction(ISD::FABS , MVT::f64, Custom);
350 setOperationAction(ISD::FABS , MVT::f32, Custom);
352 // Use XORP to simulate FNEG.
353 setOperationAction(ISD::FNEG , MVT::f64, Custom);
354 setOperationAction(ISD::FNEG , MVT::f32, Custom);
356 // Use ANDPD and ORPD to simulate FCOPYSIGN.
357 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
358 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
360 // We don't support sin/cos/fmod
361 setOperationAction(ISD::FSIN , MVT::f64, Expand);
362 setOperationAction(ISD::FCOS , MVT::f64, Expand);
363 setOperationAction(ISD::FSIN , MVT::f32, Expand);
364 setOperationAction(ISD::FCOS , MVT::f32, Expand);
366 // Expand FP immediates into loads from the stack, except for the special
368 addLegalFPImmediate(APFloat(+0.0)); // xorpd
369 addLegalFPImmediate(APFloat(+0.0f)); // xorps
371 // Floating truncations from f80 and extensions to f80 go through memory.
372 // If optimizing, we lie about this though and handle it in
373 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
375 setConvertAction(MVT::f32, MVT::f80, Expand);
376 setConvertAction(MVT::f64, MVT::f80, Expand);
377 setConvertAction(MVT::f80, MVT::f32, Expand);
378 setConvertAction(MVT::f80, MVT::f64, Expand);
380 } else if (X86ScalarSSEf32) {
381 // Use SSE for f32, x87 for f64.
382 // Set up the FP register classes.
383 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
384 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
386 // Use ANDPS to simulate FABS.
387 setOperationAction(ISD::FABS , MVT::f32, Custom);
389 // Use XORP to simulate FNEG.
390 setOperationAction(ISD::FNEG , MVT::f32, Custom);
392 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
394 // Use ANDPS and ORPS to simulate FCOPYSIGN.
395 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
396 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
398 // We don't support sin/cos/fmod
399 setOperationAction(ISD::FSIN , MVT::f32, Expand);
400 setOperationAction(ISD::FCOS , MVT::f32, Expand);
402 // Special cases we handle for FP constants.
403 addLegalFPImmediate(APFloat(+0.0f)); // xorps
404 addLegalFPImmediate(APFloat(+0.0)); // FLD0
405 addLegalFPImmediate(APFloat(+1.0)); // FLD1
406 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
407 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
409 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
410 // this though and handle it in InstructionSelectPreprocess so that
411 // dagcombine2 can hack on these.
413 setConvertAction(MVT::f32, MVT::f64, Expand);
414 setConvertAction(MVT::f32, MVT::f80, Expand);
415 setConvertAction(MVT::f80, MVT::f32, Expand);
416 setConvertAction(MVT::f64, MVT::f32, Expand);
417 // And x87->x87 truncations also.
418 setConvertAction(MVT::f80, MVT::f64, Expand);
422 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
423 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
426 // f32 and f64 in x87.
427 // Set up the FP register classes.
428 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
429 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
431 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
432 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
433 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
434 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
436 // Floating truncations go through memory. If optimizing, we lie about
437 // this though and handle it in InstructionSelectPreprocess so that
438 // dagcombine2 can hack on these.
440 setConvertAction(MVT::f80, MVT::f32, Expand);
441 setConvertAction(MVT::f64, MVT::f32, Expand);
442 setConvertAction(MVT::f80, MVT::f64, Expand);
446 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
447 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
449 addLegalFPImmediate(APFloat(+0.0)); // FLD0
450 addLegalFPImmediate(APFloat(+1.0)); // FLD1
451 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
452 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
453 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
454 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
455 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
456 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
459 // Long double always uses X87.
460 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
461 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
462 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
464 APFloat TmpFlt(+0.0);
465 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
466 addLegalFPImmediate(TmpFlt); // FLD0
468 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
469 APFloat TmpFlt2(+1.0);
470 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
471 addLegalFPImmediate(TmpFlt2); // FLD1
472 TmpFlt2.changeSign();
473 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
477 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
478 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
481 // Always use a library call for pow.
482 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
483 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
484 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
486 // First set operation action for all vector types to expand. Then we
487 // will selectively turn on ones that can be effectively codegen'd.
488 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
489 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
490 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
521 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
523 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
524 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
525 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
526 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
529 if (Subtarget->hasMMX()) {
530 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
531 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
532 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
533 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
535 // FIXME: add MMX packed arithmetics
537 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
538 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
539 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
540 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
542 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
543 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
544 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
545 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
547 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
548 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
550 setOperationAction(ISD::AND, MVT::v8i8, Promote);
551 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
552 setOperationAction(ISD::AND, MVT::v4i16, Promote);
553 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
554 setOperationAction(ISD::AND, MVT::v2i32, Promote);
555 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
556 setOperationAction(ISD::AND, MVT::v1i64, Legal);
558 setOperationAction(ISD::OR, MVT::v8i8, Promote);
559 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
560 setOperationAction(ISD::OR, MVT::v4i16, Promote);
561 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
562 setOperationAction(ISD::OR, MVT::v2i32, Promote);
563 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
564 setOperationAction(ISD::OR, MVT::v1i64, Legal);
566 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
567 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
568 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
569 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
570 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
571 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
572 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
574 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
575 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
576 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
577 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
578 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
579 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
580 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
582 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
583 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
584 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
585 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
587 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
588 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
589 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
590 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
592 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
593 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
594 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
597 if (Subtarget->hasSSE1()) {
598 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
600 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
601 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
602 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
603 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
604 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
605 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
606 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
607 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
608 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
609 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
610 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
613 if (Subtarget->hasSSE2()) {
614 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
615 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
616 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
617 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
618 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
620 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
621 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
622 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
623 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
624 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
625 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
626 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
627 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
628 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
629 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
630 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
631 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
632 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
633 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
634 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
636 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
637 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
638 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
639 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
642 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
643 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
644 // Do not attempt to custom lower non-power-of-2 vectors
645 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
647 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
649 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
651 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
652 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
653 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
654 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
655 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
656 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
657 if (Subtarget->is64Bit()) {
658 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
659 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
662 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
663 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
664 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
665 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
666 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
667 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
668 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
669 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
670 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
671 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
672 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
673 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
676 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
678 // Custom lower v2i64 and v2f64 selects.
679 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
680 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
681 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
682 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
685 if (Subtarget->hasSSE41()) {
686 // FIXME: Do we need to handle scalar-to-vector here?
687 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
689 // i8 and i16 vectors are custom , because the source register and source
690 // source memory operand types are not the same width. f32 vectors are
691 // custom since the immediate controlling the insert encodes additional
693 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
694 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
698 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
703 if (Subtarget->is64Bit()) {
704 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
705 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
709 // We want to custom lower some of our intrinsics.
710 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
712 // We have target-specific dag combine patterns for the following nodes:
713 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
714 setTargetDAGCombine(ISD::SELECT);
715 setTargetDAGCombine(ISD::STORE);
717 computeRegisterProperties();
719 // FIXME: These should be based on subtarget info. Plus, the values should
720 // be smaller when we are in optimizing for size mode.
721 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
722 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
723 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
724 allowUnalignedMemoryAccesses = true; // x86 supports it!
725 setPrefLoopAlignment(16);
728 /// getMaxByValAlign - Helper for getByValTypeAlignment to determine
729 /// the desired ByVal argument alignment.
730 static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
733 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
734 if (VTy->getBitWidth() == 128)
736 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
737 unsigned EltAlign = 0;
738 getMaxByValAlign(ATy->getElementType(), EltAlign);
739 if (EltAlign > MaxAlign)
741 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
742 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
743 unsigned EltAlign = 0;
744 getMaxByValAlign(STy->getElementType(i), EltAlign);
745 if (EltAlign > MaxAlign)
754 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
755 /// function arguments in the caller parameter area. For X86, aggregates
756 /// that contain SSE vectors are placed at 16-byte boundaries while the rest
757 /// are at 4-byte boundaries.
758 unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
759 if (Subtarget->is64Bit())
760 return getTargetData()->getABITypeAlignment(Ty);
762 if (Subtarget->hasSSE1())
763 getMaxByValAlign(Ty, Align);
767 /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
769 SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
770 SelectionDAG &DAG) const {
771 if (usesGlobalOffsetTable())
772 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
773 if (!Subtarget->isPICStyleRIPRel())
774 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
778 //===----------------------------------------------------------------------===//
779 // Return Value Calling Convention Implementation
780 //===----------------------------------------------------------------------===//
782 #include "X86GenCallingConv.inc"
784 /// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
785 /// exists skip possible ISD:TokenFactor.
786 static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
787 if (Chain.getOpcode() == X86ISD::TAILCALL) {
789 } else if (Chain.getOpcode() == ISD::TokenFactor) {
790 if (Chain.getNumOperands() &&
791 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
792 return Chain.getOperand(0);
797 /// LowerRET - Lower an ISD::RET node.
798 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
799 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
801 SmallVector<CCValAssign, 16> RVLocs;
802 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
803 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
804 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
805 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
807 // If this is the first return lowered for this function, add the regs to the
808 // liveout set for the function.
809 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
810 for (unsigned i = 0; i != RVLocs.size(); ++i)
811 if (RVLocs[i].isRegLoc())
812 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
814 SDOperand Chain = Op.getOperand(0);
816 // Handle tail call return.
817 Chain = GetPossiblePreceedingTailCall(Chain);
818 if (Chain.getOpcode() == X86ISD::TAILCALL) {
819 SDOperand TailCall = Chain;
820 SDOperand TargetAddress = TailCall.getOperand(1);
821 SDOperand StackAdjustment = TailCall.getOperand(2);
822 assert(((TargetAddress.getOpcode() == ISD::Register &&
823 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
824 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
825 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
826 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
827 "Expecting an global address, external symbol, or register");
828 assert(StackAdjustment.getOpcode() == ISD::Constant &&
829 "Expecting a const value");
831 SmallVector<SDOperand,8> Operands;
832 Operands.push_back(Chain.getOperand(0));
833 Operands.push_back(TargetAddress);
834 Operands.push_back(StackAdjustment);
835 // Copy registers used by the call. Last operand is a flag so it is not
837 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
838 Operands.push_back(Chain.getOperand(i));
840 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
847 // Copy the result values into the output registers.
848 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
849 RVLocs[0].getLocReg() != X86::ST0) {
850 for (unsigned i = 0; i != RVLocs.size(); ++i) {
851 CCValAssign &VA = RVLocs[i];
852 assert(VA.isRegLoc() && "Can only return in registers!");
853 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
855 Flag = Chain.getValue(1);
858 // We need to handle a destination of ST0 specially, because it isn't really
860 SDOperand Value = Op.getOperand(1);
862 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
863 // This will get legalized into a load/store if it can't get optimized away.
864 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
865 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
867 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
868 SDOperand Ops[] = { Chain, Value };
869 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
870 Flag = Chain.getValue(1);
873 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
875 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
877 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
881 /// LowerCallResult - Lower the result values of an ISD::CALL into the
882 /// appropriate copies out of appropriate physical registers. This assumes that
883 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
884 /// being lowered. The returns a SDNode with the same number of values as the
886 SDNode *X86TargetLowering::
887 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
888 unsigned CallingConv, SelectionDAG &DAG) {
890 // Assign locations to each value returned by this call.
891 SmallVector<CCValAssign, 16> RVLocs;
892 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
893 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
894 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
896 SmallVector<SDOperand, 8> ResultVals;
898 // Copy all of the result registers out of their specified physreg.
899 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
900 for (unsigned i = 0; i != RVLocs.size(); ++i) {
901 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
902 RVLocs[i].getValVT(), InFlag).getValue(1);
903 InFlag = Chain.getValue(2);
904 ResultVals.push_back(Chain.getValue(0));
907 // Copies from the FP stack are special, as ST0 isn't a valid register
908 // before the fp stackifier runs.
910 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
911 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
912 // the specified value type.
913 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
914 if (isScalarFPTypeInSSEReg(GetResultTy))
915 GetResultTy = MVT::f80;
916 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
918 SDOperand GROps[] = { Chain, InFlag };
919 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
920 Chain = RetVal.getValue(1);
921 InFlag = RetVal.getValue(2);
923 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
925 if (GetResultTy != RVLocs[0].getValVT())
926 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
927 // This truncation won't change the value.
928 DAG.getIntPtrConstant(1));
930 ResultVals.push_back(RetVal);
933 // Merge everything together with a MERGE_VALUES node.
934 ResultVals.push_back(Chain);
935 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
936 &ResultVals[0], ResultVals.size()).Val;
939 /// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
940 /// ISD::CALL where the results are known to be in two 64-bit registers,
941 /// e.g. XMM0 and XMM1. This simplify store the two values back to the
942 /// fixed stack slot allocated for StructRet.
943 SDNode *X86TargetLowering::
944 LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
945 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
946 MVT::ValueType VT, SelectionDAG &DAG) {
947 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
948 Chain = RetVal1.getValue(1);
949 InFlag = RetVal1.getValue(2);
950 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
951 Chain = RetVal2.getValue(1);
952 InFlag = RetVal2.getValue(2);
953 SDOperand FIN = TheCall->getOperand(5);
954 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
955 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
956 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
960 /// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
961 /// where the results are known to be in ST0 and ST1.
962 SDNode *X86TargetLowering::
963 LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
964 SDNode *TheCall, SelectionDAG &DAG) {
965 SmallVector<SDOperand, 8> ResultVals;
966 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
967 SDVTList Tys = DAG.getVTList(VTs, 4);
968 SDOperand Ops[] = { Chain, InFlag };
969 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
970 Chain = RetVal.getValue(2);
971 SDOperand FIN = TheCall->getOperand(5);
972 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
973 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
974 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
978 //===----------------------------------------------------------------------===//
979 // C & StdCall & Fast Calling Convention implementation
980 //===----------------------------------------------------------------------===//
981 // StdCall calling convention seems to be standard for many Windows' API
982 // routines and around. It differs from C calling convention just a little:
983 // callee should clean up the stack, not caller. Symbols should be also
984 // decorated in some fancy way :) It doesn't support any vector arguments.
985 // For info on fast calling convention see Fast Calling Convention (tail call)
986 // implementation LowerX86_32FastCCCallTo.
988 /// AddLiveIn - This helper function adds the specified physical register to the
989 /// MachineFunction as a live in value. It also creates a corresponding virtual
991 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
992 const TargetRegisterClass *RC) {
993 assert(RC->contains(PReg) && "Not the correct regclass!");
994 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
995 MF.getRegInfo().addLiveIn(PReg, VReg);
999 /// CallIsStructReturn - Determines whether a CALL node uses struct return
1001 static bool CallIsStructReturn(SDOperand Op) {
1002 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1006 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
1007 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1010 /// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1011 /// return semantics.
1012 static bool ArgsAreStructReturn(SDOperand Op) {
1013 unsigned NumArgs = Op.Val->getNumValues() - 1;
1017 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1018 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1021 /// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1022 /// callee to pop its own arguments. Callee pop is necessary to support tail
1024 bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1025 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1029 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1032 case CallingConv::X86_StdCall:
1033 return !Subtarget->is64Bit();
1034 case CallingConv::X86_FastCall:
1035 return !Subtarget->is64Bit();
1036 case CallingConv::Fast:
1037 return PerformTailCallOpt;
1041 /// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1042 /// FORMAL_ARGUMENTS node.
1043 CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1044 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1046 if (Subtarget->is64Bit()) {
1047 if (CC == CallingConv::Fast && PerformTailCallOpt)
1048 return CC_X86_64_TailCall;
1053 if (CC == CallingConv::X86_FastCall)
1054 return CC_X86_32_FastCall;
1055 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1056 return CC_X86_32_TailCall;
1061 /// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1062 /// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
1064 X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1065 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1066 if (CC == CallingConv::X86_FastCall)
1068 else if (CC == CallingConv::X86_StdCall)
1073 /// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1074 /// possibly be overwritten when lowering the outgoing arguments in a tail
1075 /// call. Currently the implementation of this call is very conservative and
1076 /// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1077 /// virtual registers would be overwritten by direct lowering.
1078 static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1079 MachineFrameInfo * MFI) {
1080 RegisterSDNode * OpReg = NULL;
1081 FrameIndexSDNode * FrameIdxNode = NULL;
1083 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1084 (Op.getOpcode()== ISD::CopyFromReg &&
1085 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1086 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1087 (Op.getOpcode() == ISD::LOAD &&
1088 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1089 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1090 (MFI->getObjectOffset(FrameIdx) >= 0)))
1095 /// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1096 /// in a register before calling.
1097 bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1098 return !IsTailCall && !Is64Bit &&
1099 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1100 Subtarget->isPICStyleGOT();
1104 /// CallRequiresFnAddressInReg - Check whether the call requires the function
1105 /// address to be loaded in a register.
1107 X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1108 return !Is64Bit && IsTailCall &&
1109 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1110 Subtarget->isPICStyleGOT();
1113 /// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1114 /// arguments to force loading and guarantee that arguments sourcing from
1115 /// incomming parameters are not overwriting each other.
1117 CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1118 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1120 MachineFunction &MF,
1121 const TargetLowering * TL) {
1124 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1125 SDOperand Arg = TailCallClobberedVRegs[i].second;
1126 unsigned Idx = TailCallClobberedVRegs[i].first;
1129 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1130 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1131 InFlag = Chain.getValue(1);
1132 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1133 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1134 Chain = Arg.getValue(1);
1135 InFlag = Arg.getValue(2);
1140 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1141 /// by "Src" to address "Dst" with size and alignment information specified by
1142 /// the specific parameter attribute. The copy will be passed as a byval function
1145 CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1146 unsigned Flags, SelectionDAG &DAG) {
1147 unsigned Align = 1 <<
1148 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1149 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1150 ISD::ParamFlags::ByValSizeOffs;
1151 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1152 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1153 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
1154 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
1157 SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1158 const CCValAssign &VA,
1159 MachineFrameInfo *MFI,
1161 SDOperand Root, unsigned i) {
1162 // Create the nodes corresponding to a load from this parameter slot.
1163 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1164 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
1165 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1166 bool isImmutable = !AlwaysUseMutable && !isByVal;
1168 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1169 // changed with more analysis.
1170 // In case of tail call optimization mark all arguments mutable. Since they
1171 // could be overwritten by lowering of arguments in case of a tail call.
1172 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1173 VA.getLocMemOffset(), isImmutable);
1174 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1177 return DAG.getLoad(VA.getValVT(), Root, FIN,
1178 PseudoSourceValue::getFixedStack(), FI);
1182 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
1183 MachineFunction &MF = DAG.getMachineFunction();
1184 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1186 const Function* Fn = MF.getFunction();
1187 if (Fn->hasExternalLinkage() &&
1188 Subtarget->isTargetCygMing() &&
1189 Fn->getName() == "main")
1190 FuncInfo->setForceFramePointer(true);
1192 // Decorate the function name.
1193 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1195 MachineFrameInfo *MFI = MF.getFrameInfo();
1196 SDOperand Root = Op.getOperand(0);
1197 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1198 unsigned CC = MF.getFunction()->getCallingConv();
1199 bool Is64Bit = Subtarget->is64Bit();
1201 assert(!(isVarArg && CC == CallingConv::Fast) &&
1202 "Var args not supported with calling convention fastcc");
1204 // Assign locations to all of the incoming arguments.
1205 SmallVector<CCValAssign, 16> ArgLocs;
1206 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1207 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
1209 SmallVector<SDOperand, 8> ArgValues;
1210 unsigned LastVal = ~0U;
1211 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1212 CCValAssign &VA = ArgLocs[i];
1213 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1215 assert(VA.getValNo() != LastVal &&
1216 "Don't support value assigned to multiple locs yet");
1217 LastVal = VA.getValNo();
1219 if (VA.isRegLoc()) {
1220 MVT::ValueType RegVT = VA.getLocVT();
1221 TargetRegisterClass *RC;
1222 if (RegVT == MVT::i32)
1223 RC = X86::GR32RegisterClass;
1224 else if (Is64Bit && RegVT == MVT::i64)
1225 RC = X86::GR64RegisterClass;
1226 else if (RegVT == MVT::f32)
1227 RC = X86::FR32RegisterClass;
1228 else if (RegVT == MVT::f64)
1229 RC = X86::FR64RegisterClass;
1231 assert(MVT::isVector(RegVT));
1232 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1233 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1236 RC = X86::VR128RegisterClass;
1239 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1240 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1242 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1243 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1245 if (VA.getLocInfo() == CCValAssign::SExt)
1246 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1247 DAG.getValueType(VA.getValVT()));
1248 else if (VA.getLocInfo() == CCValAssign::ZExt)
1249 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1250 DAG.getValueType(VA.getValVT()));
1252 if (VA.getLocInfo() != CCValAssign::Full)
1253 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1255 // Handle MMX values passed in GPRs.
1256 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1257 MVT::getSizeInBits(RegVT) == 64)
1258 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1260 ArgValues.push_back(ArgValue);
1262 assert(VA.isMemLoc());
1263 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
1267 unsigned StackSize = CCInfo.getNextStackOffset();
1268 // align stack specially for tail calls
1269 if (CC == CallingConv::Fast)
1270 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
1272 // If the function takes variable number of arguments, make a frame index for
1273 // the start of the first vararg value... for expansion of llvm.va_start.
1275 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1276 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1279 static const unsigned GPR64ArgRegs[] = {
1280 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1282 static const unsigned XMMArgRegs[] = {
1283 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1284 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1287 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1288 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1290 // For X86-64, if there are vararg parameters that are passed via
1291 // registers, then we must store them to their spots on the stack so they
1292 // may be loaded by deferencing the result of va_next.
1293 VarArgsGPOffset = NumIntRegs * 8;
1294 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1295 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1297 // Store the integer parameter registers.
1298 SmallVector<SDOperand, 8> MemOps;
1299 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1300 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1301 DAG.getIntPtrConstant(VarArgsGPOffset));
1302 for (; NumIntRegs != 6; ++NumIntRegs) {
1303 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1304 X86::GR64RegisterClass);
1305 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1307 DAG.getStore(Val.getValue(1), Val, FIN,
1308 PseudoSourceValue::getFixedStack(),
1310 MemOps.push_back(Store);
1311 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1312 DAG.getIntPtrConstant(8));
1315 // Now store the XMM (fp + vector) parameter registers.
1316 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1317 DAG.getIntPtrConstant(VarArgsFPOffset));
1318 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1319 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1320 X86::VR128RegisterClass);
1321 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1323 DAG.getStore(Val.getValue(1), Val, FIN,
1324 PseudoSourceValue::getFixedStack(),
1326 MemOps.push_back(Store);
1327 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1328 DAG.getIntPtrConstant(16));
1330 if (!MemOps.empty())
1331 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1332 &MemOps[0], MemOps.size());
1336 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1337 // arguments and the arguments after the retaddr has been pushed are
1339 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1340 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1341 (StackSize & 7) == 0)
1344 ArgValues.push_back(Root);
1346 // Some CCs need callee pop.
1347 if (IsCalleePop(Op)) {
1348 BytesToPopOnReturn = StackSize; // Callee pops everything.
1349 BytesCallerReserves = 0;
1351 BytesToPopOnReturn = 0; // Callee pops nothing.
1352 // If this is an sret function, the return should pop the hidden pointer.
1353 if (!Is64Bit && ArgsAreStructReturn(Op))
1354 BytesToPopOnReturn = 4;
1355 BytesCallerReserves = StackSize;
1359 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1360 if (CC == CallingConv::X86_FastCall)
1361 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1364 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1366 // Return the new list of results.
1367 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1368 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1372 X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1373 const SDOperand &StackPtr,
1374 const CCValAssign &VA,
1377 unsigned LocMemOffset = VA.getLocMemOffset();
1378 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1379 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1380 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1381 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1382 if (Flags & ISD::ParamFlags::ByVal) {
1383 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
1385 return DAG.getStore(Chain, Arg, PtrOff,
1386 PseudoSourceValue::getStack(), LocMemOffset);
1389 /// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1390 /// struct return call to the specified function. X86-64 ABI specifies
1391 /// some SRet calls are actually returned in registers. Since current
1392 /// LLVM cannot represent multi-value calls, they are represent as
1393 /// calls where the results are passed in a hidden struct provided by
1394 /// the caller. This function examines the type of the struct to
1395 /// determine the correct way to implement the call.
1397 X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1398 // FIXME: Disabled for now.
1399 return X86::InMemory;
1401 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1402 const Type *RTy = PTy->getElementType();
1403 unsigned Size = getTargetData()->getABITypeSize(RTy);
1404 if (Size != 16 && Size != 32)
1405 return X86::InMemory;
1408 const StructType *STy = dyn_cast<StructType>(RTy);
1409 if (!STy) return X86::InMemory;
1410 if (STy->getNumElements() == 2 &&
1411 STy->getElementType(0) == Type::X86_FP80Ty &&
1412 STy->getElementType(1) == Type::X86_FP80Ty)
1417 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1419 const Type *STy = I->get();
1420 if (!STy->isFPOrFPVector()) {
1428 return X86::InGPR64;
1431 void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1434 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1435 for (unsigned i = 1; i != NumOps; ++i) {
1436 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1437 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1438 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1439 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1440 cerr << "Call operand #" << i << " has unhandled type "
1441 << MVT::getValueTypeString(ArgVT) << "\n";
1447 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1448 MachineFunction &MF = DAG.getMachineFunction();
1449 MachineFrameInfo * MFI = MF.getFrameInfo();
1450 SDOperand Chain = Op.getOperand(0);
1451 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1452 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1453 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1454 && CC == CallingConv::Fast && PerformTailCallOpt;
1455 SDOperand Callee = Op.getOperand(4);
1456 bool Is64Bit = Subtarget->is64Bit();
1457 bool IsStructRet = CallIsStructReturn(Op);
1459 assert(!(isVarArg && CC == CallingConv::Fast) &&
1460 "Var args not supported with calling convention fastcc");
1462 // Analyze operands of the call, assigning locations to each operand.
1463 SmallVector<CCValAssign, 16> ArgLocs;
1464 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1465 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1467 X86::X86_64SRet SRetMethod = X86::InMemory;
1468 if (Is64Bit && IsStructRet)
1469 // FIXME: We can't figure out type of the sret structure for indirect
1470 // calls. We need to copy more information from CallSite to the ISD::CALL
1472 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1474 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1476 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1477 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1479 if (SRetMethod != X86::InMemory)
1480 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1482 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
1484 // Get a count of how many bytes are to be pushed on the stack.
1485 unsigned NumBytes = CCInfo.getNextStackOffset();
1486 if (CC == CallingConv::Fast)
1487 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
1489 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1490 // arguments and the arguments after the retaddr has been pushed are aligned.
1491 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1492 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1493 (NumBytes & 7) == 0)
1498 // Lower arguments at fp - stackoffset + fpdiff.
1499 unsigned NumBytesCallerPushed =
1500 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1501 FPDiff = NumBytesCallerPushed - NumBytes;
1503 // Set the delta of movement of the returnaddr stackslot.
1504 // But only set if delta is greater than previous delta.
1505 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1506 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1509 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
1511 SDOperand RetAddrFrIdx;
1513 // Adjust the Return address stack slot.
1515 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1516 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1517 // Load the "old" Return address.
1519 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1520 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1524 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1525 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
1526 SmallVector<SDOperand, 8> MemOpChains;
1530 // Walk the register/memloc assignments, inserting copies/loads. For tail
1531 // calls, remember all arguments for later special lowering.
1532 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1533 CCValAssign &VA = ArgLocs[i];
1534 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1536 // Promote the value if needed.
1537 switch (VA.getLocInfo()) {
1538 default: assert(0 && "Unknown loc info!");
1539 case CCValAssign::Full: break;
1540 case CCValAssign::SExt:
1541 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1543 case CCValAssign::ZExt:
1544 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1546 case CCValAssign::AExt:
1547 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1551 if (VA.isRegLoc()) {
1552 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1555 assert(VA.isMemLoc());
1556 if (StackPtr.Val == 0)
1557 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1559 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1561 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1562 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
1567 if (!MemOpChains.empty())
1568 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1569 &MemOpChains[0], MemOpChains.size());
1571 // Build a sequence of copy-to-reg nodes chained together with token chain
1572 // and flag operands which copy the outgoing args into registers.
1574 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1575 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1577 InFlag = Chain.getValue(1);
1580 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1582 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1583 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1584 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1586 InFlag = Chain.getValue(1);
1588 // If we are tail calling and generating PIC/GOT style code load the address
1589 // of the callee into ecx. The value in ecx is used as target of the tail
1590 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1591 // calls on PIC/GOT architectures. Normally we would just put the address of
1592 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1593 // restored (since ebx is callee saved) before jumping to the target@PLT.
1594 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
1595 // Note: The actual moving to ecx is done further down.
1596 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1597 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1598 !G->getGlobal()->hasProtectedVisibility())
1599 Callee = LowerGlobalAddress(Callee, DAG);
1600 else if (isa<ExternalSymbolSDNode>(Callee))
1601 Callee = LowerExternalSymbol(Callee,DAG);
1604 if (Is64Bit && isVarArg) {
1605 // From AMD64 ABI document:
1606 // For calls that may call functions that use varargs or stdargs
1607 // (prototype-less calls or calls to functions containing ellipsis (...) in
1608 // the declaration) %al is used as hidden argument to specify the number
1609 // of SSE registers used. The contents of %al do not need to match exactly
1610 // the number of registers, but must be an ubound on the number of SSE
1611 // registers used and is in the range 0 - 8 inclusive.
1613 // Count the number of XMM registers allocated.
1614 static const unsigned XMMArgRegs[] = {
1615 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1616 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1618 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1620 Chain = DAG.getCopyToReg(Chain, X86::AL,
1621 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1622 InFlag = Chain.getValue(1);
1626 // For tail calls lower the arguments to the 'real' stack slot.
1628 SmallVector<SDOperand, 8> MemOpChains2;
1631 // Do not flag preceeding copytoreg stuff together with the following stuff.
1632 InFlag = SDOperand();
1634 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1637 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1638 CCValAssign &VA = ArgLocs[i];
1639 if (!VA.isRegLoc()) {
1640 assert(VA.isMemLoc());
1641 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1642 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1643 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1644 // Create frame index.
1645 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1646 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1647 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1648 FIN = DAG.getFrameIndex(FI, MVT::i32);
1650 // Find virtual register for this argument.
1652 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1653 if (TailCallClobberedVRegs[idx].first==i) {
1654 Arg = TailCallClobberedVRegs[idx].second;
1658 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1659 (Found==true && "No corresponding Argument was found"));
1661 if (Flags & ISD::ParamFlags::ByVal) {
1662 // Copy relative to framepointer.
1663 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
1666 // Store relative to framepointer.
1667 MemOpChains2.push_back(
1668 DAG.getStore(Chain, Arg, FIN,
1669 PseudoSourceValue::getFixedStack(), FI));
1674 if (!MemOpChains2.empty())
1675 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1676 &MemOpChains2[0], MemOpChains2.size());
1678 // Store the return address to the appropriate stack slot.
1680 // Calculate the new stack slot for the return address.
1681 int SlotSize = Is64Bit ? 8 : 4;
1682 int NewReturnAddrFI =
1683 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1684 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1685 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1686 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1687 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1691 // If the callee is a GlobalAddress node (quite common, every direct call is)
1692 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1693 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1694 // We should use extra load for direct calls to dllimported functions in
1696 if ((IsTailCall || !Is64Bit ||
1697 getTargetMachine().getCodeModel() != CodeModel::Large)
1698 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1699 getTargetMachine(), true))
1700 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1701 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1702 if (IsTailCall || !Is64Bit ||
1703 getTargetMachine().getCodeModel() != CodeModel::Large)
1704 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1705 } else if (IsTailCall) {
1706 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1708 Chain = DAG.getCopyToReg(Chain,
1709 DAG.getRegister(Opc, getPointerTy()),
1711 Callee = DAG.getRegister(Opc, getPointerTy());
1712 // Add register as live out.
1713 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
1716 // Returns a chain & a flag for retval copy to use.
1717 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1718 SmallVector<SDOperand, 8> Ops;
1721 Ops.push_back(Chain);
1722 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1723 Ops.push_back(DAG.getIntPtrConstant(0));
1725 Ops.push_back(InFlag);
1726 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1727 InFlag = Chain.getValue(1);
1729 // Returns a chain & a flag for retval copy to use.
1730 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1734 Ops.push_back(Chain);
1735 Ops.push_back(Callee);
1738 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1740 // Add an implicit use GOT pointer in EBX.
1741 if (!IsTailCall && !Is64Bit &&
1742 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1743 Subtarget->isPICStyleGOT())
1744 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1746 // Add argument registers to the end of the list so that they are known live
1748 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1749 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1750 RegsToPass[i].second.getValueType()));
1753 Ops.push_back(InFlag);
1756 assert(InFlag.Val &&
1757 "Flag must be set. Depend on flag being set in LowerRET");
1758 Chain = DAG.getNode(X86ISD::TAILCALL,
1759 Op.Val->getVTList(), &Ops[0], Ops.size());
1761 return SDOperand(Chain.Val, Op.ResNo);
1764 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
1765 InFlag = Chain.getValue(1);
1767 // Create the CALLSEQ_END node.
1768 unsigned NumBytesForCalleeToPush;
1769 if (IsCalleePop(Op))
1770 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
1771 else if (!Is64Bit && IsStructRet)
1772 // If this is is a call to a struct-return function, the callee
1773 // pops the hidden struct pointer, so we have to push it back.
1774 // This is common for Darwin/X86, Linux & Mingw32 targets.
1775 NumBytesForCalleeToPush = 4;
1777 NumBytesForCalleeToPush = 0; // Callee pops nothing.
1779 // Returns a flag for retval copy to use.
1780 Chain = DAG.getCALLSEQ_END(Chain,
1781 DAG.getIntPtrConstant(NumBytes),
1782 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
1784 InFlag = Chain.getValue(1);
1786 // Handle result values, copying them out of physregs into vregs that we
1788 switch (SRetMethod) {
1790 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1792 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1794 MVT::i64, DAG), Op.ResNo);
1796 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1797 X86::XMM0, X86::XMM1,
1798 MVT::f64, DAG), Op.ResNo);
1800 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1806 //===----------------------------------------------------------------------===//
1807 // Fast Calling Convention (tail call) implementation
1808 //===----------------------------------------------------------------------===//
1810 // Like std call, callee cleans arguments, convention except that ECX is
1811 // reserved for storing the tail called function address. Only 2 registers are
1812 // free for argument passing (inreg). Tail call optimization is performed
1814 // * tailcallopt is enabled
1815 // * caller/callee are fastcc
1816 // On X86_64 architecture with GOT-style position independent code only local
1817 // (within module) calls are supported at the moment.
1818 // To keep the stack aligned according to platform abi the function
1819 // GetAlignedArgumentStackSize ensures that argument delta is always multiples
1820 // of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
1821 // If a tail called function callee has more arguments than the caller the
1822 // caller needs to make sure that there is room to move the RETADDR to. This is
1823 // achieved by reserving an area the size of the argument delta right after the
1824 // original REtADDR, but before the saved framepointer or the spilled registers
1825 // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1837 /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1838 /// for a 16 byte align requirement.
1839 unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1840 SelectionDAG& DAG) {
1841 if (PerformTailCallOpt) {
1842 MachineFunction &MF = DAG.getMachineFunction();
1843 const TargetMachine &TM = MF.getTarget();
1844 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1845 unsigned StackAlignment = TFI.getStackAlignment();
1846 uint64_t AlignMask = StackAlignment - 1;
1847 int64_t Offset = StackSize;
1848 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1849 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1850 // Number smaller than 12 so just add the difference.
1851 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1853 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1854 Offset = ((~AlignMask) & Offset) + StackAlignment +
1855 (StackAlignment-SlotSize);
1862 /// IsEligibleForTailCallElimination - Check to see whether the next instruction
1863 /// following the call is a return. A function is eligible if caller/callee
1864 /// calling conventions match, currently only fastcc supports tail calls, and
1865 /// the function CALL is immediatly followed by a RET.
1866 bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1868 SelectionDAG& DAG) const {
1869 if (!PerformTailCallOpt)
1872 // Check whether CALL node immediatly preceeds the RET node and whether the
1873 // return uses the result of the node or is a void return.
1874 unsigned NumOps = Ret.getNumOperands();
1876 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1877 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
1879 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1880 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
1881 MachineFunction &MF = DAG.getMachineFunction();
1882 unsigned CallerCC = MF.getFunction()->getCallingConv();
1883 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1884 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1885 SDOperand Callee = Call.getOperand(4);
1886 // On x86/32Bit PIC/GOT tail calls are supported.
1887 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1888 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
1891 // Can only do local tail calls (in same module, hidden or protected) on
1892 // x86_64 PIC/GOT at the moment.
1893 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1894 return G->getGlobal()->hasHiddenVisibility()
1895 || G->getGlobal()->hasProtectedVisibility();
1902 //===----------------------------------------------------------------------===//
1903 // Other Lowering Hooks
1904 //===----------------------------------------------------------------------===//
1907 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1908 MachineFunction &MF = DAG.getMachineFunction();
1909 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1910 int ReturnAddrIndex = FuncInfo->getRAIndex();
1912 if (ReturnAddrIndex == 0) {
1913 // Set up a frame object for the return address.
1914 if (Subtarget->is64Bit())
1915 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1917 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1919 FuncInfo->setRAIndex(ReturnAddrIndex);
1922 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1927 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1928 /// specific condition code. It returns a false if it cannot do a direct
1929 /// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1931 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1932 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1933 SelectionDAG &DAG) {
1934 X86CC = X86::COND_INVALID;
1936 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1937 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1938 // X > -1 -> X == 0, jump !sign.
1939 RHS = DAG.getConstant(0, RHS.getValueType());
1940 X86CC = X86::COND_NS;
1942 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1943 // X < 0 -> X == 0, jump on sign.
1944 X86CC = X86::COND_S;
1946 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1948 RHS = DAG.getConstant(0, RHS.getValueType());
1949 X86CC = X86::COND_LE;
1954 switch (SetCCOpcode) {
1956 case ISD::SETEQ: X86CC = X86::COND_E; break;
1957 case ISD::SETGT: X86CC = X86::COND_G; break;
1958 case ISD::SETGE: X86CC = X86::COND_GE; break;
1959 case ISD::SETLT: X86CC = X86::COND_L; break;
1960 case ISD::SETLE: X86CC = X86::COND_LE; break;
1961 case ISD::SETNE: X86CC = X86::COND_NE; break;
1962 case ISD::SETULT: X86CC = X86::COND_B; break;
1963 case ISD::SETUGT: X86CC = X86::COND_A; break;
1964 case ISD::SETULE: X86CC = X86::COND_BE; break;
1965 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1968 // On a floating point condition, the flags are set as follows:
1970 // 0 | 0 | 0 | X > Y
1971 // 0 | 0 | 1 | X < Y
1972 // 1 | 0 | 0 | X == Y
1973 // 1 | 1 | 1 | unordered
1975 switch (SetCCOpcode) {
1978 case ISD::SETEQ: X86CC = X86::COND_E; break;
1979 case ISD::SETOLT: Flip = true; // Fallthrough
1981 case ISD::SETGT: X86CC = X86::COND_A; break;
1982 case ISD::SETOLE: Flip = true; // Fallthrough
1984 case ISD::SETGE: X86CC = X86::COND_AE; break;
1985 case ISD::SETUGT: Flip = true; // Fallthrough
1987 case ISD::SETLT: X86CC = X86::COND_B; break;
1988 case ISD::SETUGE: Flip = true; // Fallthrough
1990 case ISD::SETLE: X86CC = X86::COND_BE; break;
1992 case ISD::SETNE: X86CC = X86::COND_NE; break;
1993 case ISD::SETUO: X86CC = X86::COND_P; break;
1994 case ISD::SETO: X86CC = X86::COND_NP; break;
1997 std::swap(LHS, RHS);
2000 return X86CC != X86::COND_INVALID;
2003 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
2004 /// code. Current x86 isa includes the following FP cmov instructions:
2005 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2006 static bool hasFPCMov(unsigned X86CC) {
2022 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2023 /// true if Op is undef or if its value falls within the specified range (L, H].
2024 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2025 if (Op.getOpcode() == ISD::UNDEF)
2028 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2029 return (Val >= Low && Val < Hi);
2032 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2033 /// true if Op is undef or if its value equal to the specified value.
2034 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2035 if (Op.getOpcode() == ISD::UNDEF)
2037 return cast<ConstantSDNode>(Op)->getValue() == Val;
2040 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2041 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
2042 bool X86::isPSHUFDMask(SDNode *N) {
2043 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2045 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
2048 // Check if the value doesn't reference the second vector.
2049 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2050 SDOperand Arg = N->getOperand(i);
2051 if (Arg.getOpcode() == ISD::UNDEF) continue;
2052 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2053 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
2060 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2061 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2062 bool X86::isPSHUFHWMask(SDNode *N) {
2063 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2065 if (N->getNumOperands() != 8)
2068 // Lower quadword copied in order.
2069 for (unsigned i = 0; i != 4; ++i) {
2070 SDOperand Arg = N->getOperand(i);
2071 if (Arg.getOpcode() == ISD::UNDEF) continue;
2072 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2073 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2077 // Upper quadword shuffled.
2078 for (unsigned i = 4; i != 8; ++i) {
2079 SDOperand Arg = N->getOperand(i);
2080 if (Arg.getOpcode() == ISD::UNDEF) continue;
2081 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2082 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2083 if (Val < 4 || Val > 7)
2090 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2091 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2092 bool X86::isPSHUFLWMask(SDNode *N) {
2093 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2095 if (N->getNumOperands() != 8)
2098 // Upper quadword copied in order.
2099 for (unsigned i = 4; i != 8; ++i)
2100 if (!isUndefOrEqual(N->getOperand(i), i))
2103 // Lower quadword shuffled.
2104 for (unsigned i = 0; i != 4; ++i)
2105 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2111 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2112 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
2113 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2114 if (NumElems != 2 && NumElems != 4) return false;
2116 unsigned Half = NumElems / 2;
2117 for (unsigned i = 0; i < Half; ++i)
2118 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2120 for (unsigned i = Half; i < NumElems; ++i)
2121 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2127 bool X86::isSHUFPMask(SDNode *N) {
2128 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2129 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2132 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2133 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2134 /// half elements to come from vector 1 (which would equal the dest.) and
2135 /// the upper half to come from vector 2.
2136 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2137 if (NumOps != 2 && NumOps != 4) return false;
2139 unsigned Half = NumOps / 2;
2140 for (unsigned i = 0; i < Half; ++i)
2141 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2143 for (unsigned i = Half; i < NumOps; ++i)
2144 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2149 static bool isCommutedSHUFP(SDNode *N) {
2150 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2151 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2154 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2155 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2156 bool X86::isMOVHLPSMask(SDNode *N) {
2157 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2159 if (N->getNumOperands() != 4)
2162 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2163 return isUndefOrEqual(N->getOperand(0), 6) &&
2164 isUndefOrEqual(N->getOperand(1), 7) &&
2165 isUndefOrEqual(N->getOperand(2), 2) &&
2166 isUndefOrEqual(N->getOperand(3), 3);
2169 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2170 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2172 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2173 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2175 if (N->getNumOperands() != 4)
2178 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2179 return isUndefOrEqual(N->getOperand(0), 2) &&
2180 isUndefOrEqual(N->getOperand(1), 3) &&
2181 isUndefOrEqual(N->getOperand(2), 2) &&
2182 isUndefOrEqual(N->getOperand(3), 3);
2185 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2186 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2187 bool X86::isMOVLPMask(SDNode *N) {
2188 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2190 unsigned NumElems = N->getNumOperands();
2191 if (NumElems != 2 && NumElems != 4)
2194 for (unsigned i = 0; i < NumElems/2; ++i)
2195 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2198 for (unsigned i = NumElems/2; i < NumElems; ++i)
2199 if (!isUndefOrEqual(N->getOperand(i), i))
2205 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2206 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2208 bool X86::isMOVHPMask(SDNode *N) {
2209 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2211 unsigned NumElems = N->getNumOperands();
2212 if (NumElems != 2 && NumElems != 4)
2215 for (unsigned i = 0; i < NumElems/2; ++i)
2216 if (!isUndefOrEqual(N->getOperand(i), i))
2219 for (unsigned i = 0; i < NumElems/2; ++i) {
2220 SDOperand Arg = N->getOperand(i + NumElems/2);
2221 if (!isUndefOrEqual(Arg, i + NumElems))
2228 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2229 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
2230 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2231 bool V2IsSplat = false) {
2232 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2235 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2236 SDOperand BitI = Elts[i];
2237 SDOperand BitI1 = Elts[i+1];
2238 if (!isUndefOrEqual(BitI, j))
2241 if (isUndefOrEqual(BitI1, NumElts))
2244 if (!isUndefOrEqual(BitI1, j + NumElts))
2252 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2253 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2254 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2257 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2258 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
2259 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2260 bool V2IsSplat = false) {
2261 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2264 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2265 SDOperand BitI = Elts[i];
2266 SDOperand BitI1 = Elts[i+1];
2267 if (!isUndefOrEqual(BitI, j + NumElts/2))
2270 if (isUndefOrEqual(BitI1, NumElts))
2273 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2281 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2282 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2283 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2286 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2287 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2289 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2290 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2292 unsigned NumElems = N->getNumOperands();
2293 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2296 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2297 SDOperand BitI = N->getOperand(i);
2298 SDOperand BitI1 = N->getOperand(i+1);
2300 if (!isUndefOrEqual(BitI, j))
2302 if (!isUndefOrEqual(BitI1, j))
2309 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2310 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2312 bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2313 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2315 unsigned NumElems = N->getNumOperands();
2316 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2319 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2320 SDOperand BitI = N->getOperand(i);
2321 SDOperand BitI1 = N->getOperand(i + 1);
2323 if (!isUndefOrEqual(BitI, j))
2325 if (!isUndefOrEqual(BitI1, j))
2332 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2333 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2334 /// MOVSD, and MOVD, i.e. setting the lowest element.
2335 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2336 if (NumElts != 2 && NumElts != 4)
2339 if (!isUndefOrEqual(Elts[0], NumElts))
2342 for (unsigned i = 1; i < NumElts; ++i) {
2343 if (!isUndefOrEqual(Elts[i], i))
2350 bool X86::isMOVLMask(SDNode *N) {
2351 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2352 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2355 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2356 /// of what x86 movss want. X86 movs requires the lowest element to be lowest
2357 /// element of vector 2 and the other elements to come from vector 1 in order.
2358 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2359 bool V2IsSplat = false,
2360 bool V2IsUndef = false) {
2361 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2364 if (!isUndefOrEqual(Ops[0], 0))
2367 for (unsigned i = 1; i < NumOps; ++i) {
2368 SDOperand Arg = Ops[i];
2369 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2370 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2371 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2378 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2379 bool V2IsUndef = false) {
2380 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2381 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2382 V2IsSplat, V2IsUndef);
2385 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2386 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2387 bool X86::isMOVSHDUPMask(SDNode *N) {
2388 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2390 if (N->getNumOperands() != 4)
2393 // Expect 1, 1, 3, 3
2394 for (unsigned i = 0; i < 2; ++i) {
2395 SDOperand Arg = N->getOperand(i);
2396 if (Arg.getOpcode() == ISD::UNDEF) continue;
2397 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2398 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2399 if (Val != 1) return false;
2403 for (unsigned i = 2; i < 4; ++i) {
2404 SDOperand Arg = N->getOperand(i);
2405 if (Arg.getOpcode() == ISD::UNDEF) continue;
2406 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2407 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2408 if (Val != 3) return false;
2412 // Don't use movshdup if it can be done with a shufps.
2416 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2417 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2418 bool X86::isMOVSLDUPMask(SDNode *N) {
2419 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2421 if (N->getNumOperands() != 4)
2424 // Expect 0, 0, 2, 2
2425 for (unsigned i = 0; i < 2; ++i) {
2426 SDOperand Arg = N->getOperand(i);
2427 if (Arg.getOpcode() == ISD::UNDEF) continue;
2428 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2429 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2430 if (Val != 0) return false;
2434 for (unsigned i = 2; i < 4; ++i) {
2435 SDOperand Arg = N->getOperand(i);
2436 if (Arg.getOpcode() == ISD::UNDEF) continue;
2437 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2438 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2439 if (Val != 2) return false;
2443 // Don't use movshdup if it can be done with a shufps.
2447 /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2448 /// specifies a identity operation on the LHS or RHS.
2449 static bool isIdentityMask(SDNode *N, bool RHS = false) {
2450 unsigned NumElems = N->getNumOperands();
2451 for (unsigned i = 0; i < NumElems; ++i)
2452 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2457 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2458 /// a splat of a single element.
2459 static bool isSplatMask(SDNode *N) {
2460 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2462 // This is a splat operation if each element of the permute is the same, and
2463 // if the value doesn't reference the second vector.
2464 unsigned NumElems = N->getNumOperands();
2465 SDOperand ElementBase;
2467 for (; i != NumElems; ++i) {
2468 SDOperand Elt = N->getOperand(i);
2469 if (isa<ConstantSDNode>(Elt)) {
2475 if (!ElementBase.Val)
2478 for (; i != NumElems; ++i) {
2479 SDOperand Arg = N->getOperand(i);
2480 if (Arg.getOpcode() == ISD::UNDEF) continue;
2481 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2482 if (Arg != ElementBase) return false;
2485 // Make sure it is a splat of the first vector operand.
2486 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2489 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2490 /// a splat of a single element and it's a 2 or 4 element mask.
2491 bool X86::isSplatMask(SDNode *N) {
2492 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2494 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2495 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2497 return ::isSplatMask(N);
2500 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2501 /// specifies a splat of zero element.
2502 bool X86::isSplatLoMask(SDNode *N) {
2503 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2505 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2506 if (!isUndefOrEqual(N->getOperand(i), 0))
2511 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2512 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2514 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2515 unsigned NumOperands = N->getNumOperands();
2516 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2518 for (unsigned i = 0; i < NumOperands; ++i) {
2520 SDOperand Arg = N->getOperand(NumOperands-i-1);
2521 if (Arg.getOpcode() != ISD::UNDEF)
2522 Val = cast<ConstantSDNode>(Arg)->getValue();
2523 if (Val >= NumOperands) Val -= NumOperands;
2525 if (i != NumOperands - 1)
2532 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2533 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2535 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2537 // 8 nodes, but we only care about the last 4.
2538 for (unsigned i = 7; i >= 4; --i) {
2540 SDOperand Arg = N->getOperand(i);
2541 if (Arg.getOpcode() != ISD::UNDEF)
2542 Val = cast<ConstantSDNode>(Arg)->getValue();
2551 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2552 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2554 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2556 // 8 nodes, but we only care about the first 4.
2557 for (int i = 3; i >= 0; --i) {
2559 SDOperand Arg = N->getOperand(i);
2560 if (Arg.getOpcode() != ISD::UNDEF)
2561 Val = cast<ConstantSDNode>(Arg)->getValue();
2570 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2571 /// specifies a 8 element shuffle that can be broken into a pair of
2572 /// PSHUFHW and PSHUFLW.
2573 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2574 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2576 if (N->getNumOperands() != 8)
2579 // Lower quadword shuffled.
2580 for (unsigned i = 0; i != 4; ++i) {
2581 SDOperand Arg = N->getOperand(i);
2582 if (Arg.getOpcode() == ISD::UNDEF) continue;
2583 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2584 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2589 // Upper quadword shuffled.
2590 for (unsigned i = 4; i != 8; ++i) {
2591 SDOperand Arg = N->getOperand(i);
2592 if (Arg.getOpcode() == ISD::UNDEF) continue;
2593 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2594 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2595 if (Val < 4 || Val > 7)
2602 /// CommuteVectorShuffle - Swap vector_shuffle operands as well as
2603 /// values in ther permute mask.
2604 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2605 SDOperand &V2, SDOperand &Mask,
2606 SelectionDAG &DAG) {
2607 MVT::ValueType VT = Op.getValueType();
2608 MVT::ValueType MaskVT = Mask.getValueType();
2609 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2610 unsigned NumElems = Mask.getNumOperands();
2611 SmallVector<SDOperand, 8> MaskVec;
2613 for (unsigned i = 0; i != NumElems; ++i) {
2614 SDOperand Arg = Mask.getOperand(i);
2615 if (Arg.getOpcode() == ISD::UNDEF) {
2616 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2619 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2620 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2622 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2624 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2628 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2629 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2632 /// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2633 /// the two vector operands have swapped position.
2635 SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2636 MVT::ValueType MaskVT = Mask.getValueType();
2637 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2638 unsigned NumElems = Mask.getNumOperands();
2639 SmallVector<SDOperand, 8> MaskVec;
2640 for (unsigned i = 0; i != NumElems; ++i) {
2641 SDOperand Arg = Mask.getOperand(i);
2642 if (Arg.getOpcode() == ISD::UNDEF) {
2643 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2646 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2647 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2649 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2651 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2653 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2657 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2658 /// match movhlps. The lower half elements should come from upper half of
2659 /// V1 (and in order), and the upper half elements should come from the upper
2660 /// half of V2 (and in order).
2661 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2662 unsigned NumElems = Mask->getNumOperands();
2665 for (unsigned i = 0, e = 2; i != e; ++i)
2666 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2668 for (unsigned i = 2; i != 4; ++i)
2669 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2674 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2675 /// is promoted to a vector.
2676 static inline bool isScalarLoadToVector(SDNode *N) {
2677 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2678 N = N->getOperand(0).Val;
2679 return ISD::isNON_EXTLoad(N);
2684 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2685 /// match movlp{s|d}. The lower half elements should come from lower half of
2686 /// V1 (and in order), and the upper half elements should come from the upper
2687 /// half of V2 (and in order). And since V1 will become the source of the
2688 /// MOVLP, it must be either a vector load or a scalar load to vector.
2689 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2690 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2692 // Is V2 is a vector load, don't do this transformation. We will try to use
2693 // load folding shufps op.
2694 if (ISD::isNON_EXTLoad(V2))
2697 unsigned NumElems = Mask->getNumOperands();
2698 if (NumElems != 2 && NumElems != 4)
2700 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2701 if (!isUndefOrEqual(Mask->getOperand(i), i))
2703 for (unsigned i = NumElems/2; i != NumElems; ++i)
2704 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2709 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2711 static bool isSplatVector(SDNode *N) {
2712 if (N->getOpcode() != ISD::BUILD_VECTOR)
2715 SDOperand SplatValue = N->getOperand(0);
2716 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2717 if (N->getOperand(i) != SplatValue)
2722 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2724 static bool isUndefShuffle(SDNode *N) {
2725 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2728 SDOperand V1 = N->getOperand(0);
2729 SDOperand V2 = N->getOperand(1);
2730 SDOperand Mask = N->getOperand(2);
2731 unsigned NumElems = Mask.getNumOperands();
2732 for (unsigned i = 0; i != NumElems; ++i) {
2733 SDOperand Arg = Mask.getOperand(i);
2734 if (Arg.getOpcode() != ISD::UNDEF) {
2735 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2736 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2738 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2745 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2747 static inline bool isZeroNode(SDOperand Elt) {
2748 return ((isa<ConstantSDNode>(Elt) &&
2749 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2750 (isa<ConstantFPSDNode>(Elt) &&
2751 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2754 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2755 /// to an zero vector.
2756 static bool isZeroShuffle(SDNode *N) {
2757 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2760 SDOperand V1 = N->getOperand(0);
2761 SDOperand V2 = N->getOperand(1);
2762 SDOperand Mask = N->getOperand(2);
2763 unsigned NumElems = Mask.getNumOperands();
2764 for (unsigned i = 0; i != NumElems; ++i) {
2765 SDOperand Arg = Mask.getOperand(i);
2766 if (Arg.getOpcode() == ISD::UNDEF)
2769 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2770 if (Idx < NumElems) {
2771 unsigned Opc = V1.Val->getOpcode();
2772 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2774 if (Opc != ISD::BUILD_VECTOR ||
2775 !isZeroNode(V1.Val->getOperand(Idx)))
2777 } else if (Idx >= NumElems) {
2778 unsigned Opc = V2.Val->getOpcode();
2779 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2781 if (Opc != ISD::BUILD_VECTOR ||
2782 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2789 /// getZeroVector - Returns a vector of specified type with all zero elements.
2791 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2792 assert(MVT::isVector(VT) && "Expected a vector type");
2794 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2795 // type. This ensures they get CSE'd.
2796 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2798 if (MVT::getSizeInBits(VT) == 64) // MMX
2799 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2801 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2802 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2805 /// getOnesVector - Returns a vector of specified type with all bits set.
2807 static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2808 assert(MVT::isVector(VT) && "Expected a vector type");
2810 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2811 // type. This ensures they get CSE'd.
2812 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2814 if (MVT::getSizeInBits(VT) == 64) // MMX
2815 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2817 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2818 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2822 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2823 /// that point to V2 points to its first element.
2824 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2825 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2827 bool Changed = false;
2828 SmallVector<SDOperand, 8> MaskVec;
2829 unsigned NumElems = Mask.getNumOperands();
2830 for (unsigned i = 0; i != NumElems; ++i) {
2831 SDOperand Arg = Mask.getOperand(i);
2832 if (Arg.getOpcode() != ISD::UNDEF) {
2833 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2834 if (Val > NumElems) {
2835 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2839 MaskVec.push_back(Arg);
2843 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2844 &MaskVec[0], MaskVec.size());
2848 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2849 /// operation of specified width.
2850 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2851 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2852 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2854 SmallVector<SDOperand, 8> MaskVec;
2855 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2856 for (unsigned i = 1; i != NumElems; ++i)
2857 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2858 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2861 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2862 /// of specified width.
2863 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2864 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2865 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2866 SmallVector<SDOperand, 8> MaskVec;
2867 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2868 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2869 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2871 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2874 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2875 /// of specified width.
2876 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2877 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2878 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2879 unsigned Half = NumElems/2;
2880 SmallVector<SDOperand, 8> MaskVec;
2881 for (unsigned i = 0; i != Half; ++i) {
2882 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2883 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2885 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2888 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2890 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2891 SDOperand V1 = Op.getOperand(0);
2892 SDOperand Mask = Op.getOperand(2);
2893 MVT::ValueType VT = Op.getValueType();
2894 unsigned NumElems = Mask.getNumOperands();
2895 Mask = getUnpacklMask(NumElems, DAG);
2896 while (NumElems != 4) {
2897 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2900 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2902 Mask = getZeroVector(MVT::v4i32, DAG);
2903 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2904 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2905 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2908 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2909 /// vector of zero or undef vector. This produces a shuffle where the low
2910 /// element of V2 is swizzled into the zero/undef vector, landing at element
2911 /// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
2912 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2913 unsigned NumElems, unsigned Idx,
2914 bool isZero, SelectionDAG &DAG) {
2915 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2916 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2917 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2918 SmallVector<SDOperand, 16> MaskVec;
2919 for (unsigned i = 0; i != NumElems; ++i)
2920 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2921 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2923 MaskVec.push_back(DAG.getConstant(i, EVT));
2924 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2925 &MaskVec[0], MaskVec.size());
2926 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2929 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2931 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2932 unsigned NumNonZero, unsigned NumZero,
2933 SelectionDAG &DAG, TargetLowering &TLI) {
2939 for (unsigned i = 0; i < 16; ++i) {
2940 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2941 if (ThisIsNonZero && First) {
2943 V = getZeroVector(MVT::v8i16, DAG);
2945 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2950 SDOperand ThisElt(0, 0), LastElt(0, 0);
2951 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2952 if (LastIsNonZero) {
2953 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2955 if (ThisIsNonZero) {
2956 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2957 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2958 ThisElt, DAG.getConstant(8, MVT::i8));
2960 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2965 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2966 DAG.getIntPtrConstant(i/2));
2970 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2973 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2975 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2976 unsigned NumNonZero, unsigned NumZero,
2977 SelectionDAG &DAG, TargetLowering &TLI) {
2983 for (unsigned i = 0; i < 8; ++i) {
2984 bool isNonZero = (NonZeros & (1 << i)) != 0;
2988 V = getZeroVector(MVT::v8i16, DAG);
2990 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2993 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2994 DAG.getIntPtrConstant(i));
3002 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3003 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3004 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3005 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3006 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3007 // eliminated on x86-32 hosts.
3008 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3011 if (ISD::isBuildVectorAllOnes(Op.Val))
3012 return getOnesVector(Op.getValueType(), DAG);
3013 return getZeroVector(Op.getValueType(), DAG);
3016 MVT::ValueType VT = Op.getValueType();
3017 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3018 unsigned EVTBits = MVT::getSizeInBits(EVT);
3020 unsigned NumElems = Op.getNumOperands();
3021 unsigned NumZero = 0;
3022 unsigned NumNonZero = 0;
3023 unsigned NonZeros = 0;
3024 bool HasNonImms = false;
3025 SmallSet<SDOperand, 8> Values;
3026 for (unsigned i = 0; i < NumElems; ++i) {
3027 SDOperand Elt = Op.getOperand(i);
3028 if (Elt.getOpcode() == ISD::UNDEF)
3031 if (Elt.getOpcode() != ISD::Constant &&
3032 Elt.getOpcode() != ISD::ConstantFP)
3034 if (isZeroNode(Elt))
3037 NonZeros |= (1 << i);
3042 if (NumNonZero == 0) {
3043 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3044 return DAG.getNode(ISD::UNDEF, VT);
3047 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3048 if (Values.size() == 1)
3051 // Special case for single non-zero element.
3052 if (NumNonZero == 1 && NumElems <= 4) {
3053 unsigned Idx = CountTrailingZeros_32(NonZeros);
3054 SDOperand Item = Op.getOperand(Idx);
3055 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3057 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3058 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3060 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
3063 if (EVTBits == 32) {
3064 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3065 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3067 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3068 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3069 SmallVector<SDOperand, 8> MaskVec;
3070 for (unsigned i = 0; i < NumElems; i++)
3071 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3072 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3073 &MaskVec[0], MaskVec.size());
3074 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3075 DAG.getNode(ISD::UNDEF, VT), Mask);
3079 // A vector full of immediates; various special cases are already
3080 // handled, so this is best done with a single constant-pool load.
3084 // Let legalizer expand 2-wide build_vectors.
3088 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3089 if (EVTBits == 8 && NumElems == 16) {
3090 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3092 if (V.Val) return V;
3095 if (EVTBits == 16 && NumElems == 8) {
3096 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3098 if (V.Val) return V;
3101 // If element VT is == 32 bits, turn it into a number of shuffles.
3102 SmallVector<SDOperand, 8> V;
3104 if (NumElems == 4 && NumZero > 0) {
3105 for (unsigned i = 0; i < 4; ++i) {
3106 bool isZero = !(NonZeros & (1 << i));
3108 V[i] = getZeroVector(VT, DAG);
3110 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3113 for (unsigned i = 0; i < 2; ++i) {
3114 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3117 V[i] = V[i*2]; // Must be a zero vector.
3120 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3121 getMOVLMask(NumElems, DAG));
3124 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3125 getMOVLMask(NumElems, DAG));
3128 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3129 getUnpacklMask(NumElems, DAG));
3134 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3135 // clears the upper bits.
3136 // FIXME: we can do the same for v4f32 case when we know both parts of
3137 // the lower half come from scalar_to_vector (loadf32). We should do
3138 // that in post legalizer dag combiner with target specific hooks.
3139 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3141 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3142 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3143 SmallVector<SDOperand, 8> MaskVec;
3144 bool Reverse = (NonZeros & 0x3) == 2;
3145 for (unsigned i = 0; i < 2; ++i)
3147 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3149 MaskVec.push_back(DAG.getConstant(i, EVT));
3150 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3151 for (unsigned i = 0; i < 2; ++i)
3153 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3155 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3156 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3157 &MaskVec[0], MaskVec.size());
3158 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3161 if (Values.size() > 2) {
3162 // Expand into a number of unpckl*.
3164 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3165 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3166 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3167 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3168 for (unsigned i = 0; i < NumElems; ++i)
3169 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3171 while (NumElems != 0) {
3172 for (unsigned i = 0; i < NumElems; ++i)
3173 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3184 SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3185 SDOperand PermMask, SelectionDAG &DAG,
3186 TargetLowering &TLI) {
3188 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3189 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3190 MVT::ValueType PtrVT = TLI.getPointerTy();
3191 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3192 PermMask.Val->op_end());
3194 // First record which half of which vector the low elements come from.
3195 SmallVector<unsigned, 4> LowQuad(4);
3196 for (unsigned i = 0; i < 4; ++i) {
3197 SDOperand Elt = MaskElts[i];
3198 if (Elt.getOpcode() == ISD::UNDEF)
3200 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3201 int QuadIdx = EltIdx / 4;
3204 int BestLowQuad = -1;
3205 unsigned MaxQuad = 1;
3206 for (unsigned i = 0; i < 4; ++i) {
3207 if (LowQuad[i] > MaxQuad) {
3209 MaxQuad = LowQuad[i];
3213 // Record which half of which vector the high elements come from.
3214 SmallVector<unsigned, 4> HighQuad(4);
3215 for (unsigned i = 4; i < 8; ++i) {
3216 SDOperand Elt = MaskElts[i];
3217 if (Elt.getOpcode() == ISD::UNDEF)
3219 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3220 int QuadIdx = EltIdx / 4;
3221 ++HighQuad[QuadIdx];
3223 int BestHighQuad = -1;
3225 for (unsigned i = 0; i < 4; ++i) {
3226 if (HighQuad[i] > MaxQuad) {
3228 MaxQuad = HighQuad[i];
3232 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3233 if (BestLowQuad != -1 || BestHighQuad != -1) {
3234 // First sort the 4 chunks in order using shufpd.
3235 SmallVector<SDOperand, 8> MaskVec;
3236 if (BestLowQuad != -1)
3237 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3239 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3240 if (BestHighQuad != -1)
3241 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3243 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3244 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3245 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3246 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3247 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3248 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3250 // Now sort high and low parts separately.
3251 BitVector InOrder(8);
3252 if (BestLowQuad != -1) {
3253 // Sort lower half in order using PSHUFLW.
3255 bool AnyOutOrder = false;
3256 for (unsigned i = 0; i != 4; ++i) {
3257 SDOperand Elt = MaskElts[i];
3258 if (Elt.getOpcode() == ISD::UNDEF) {
3259 MaskVec.push_back(Elt);
3262 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3265 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3266 // If this element is in the right place after this shuffle, then
3268 if ((int)(EltIdx / 4) == BestLowQuad)
3273 for (unsigned i = 4; i != 8; ++i)
3274 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3275 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3276 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3280 if (BestHighQuad != -1) {
3281 // Sort high half in order using PSHUFHW if possible.
3283 for (unsigned i = 0; i != 4; ++i)
3284 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3285 bool AnyOutOrder = false;
3286 for (unsigned i = 4; i != 8; ++i) {
3287 SDOperand Elt = MaskElts[i];
3288 if (Elt.getOpcode() == ISD::UNDEF) {
3289 MaskVec.push_back(Elt);
3292 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3295 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3296 // If this element is in the right place after this shuffle, then
3298 if ((int)(EltIdx / 4) == BestHighQuad)
3303 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3304 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3308 // The other elements are put in the right place using pextrw and pinsrw.
3309 for (unsigned i = 0; i != 8; ++i) {
3312 SDOperand Elt = MaskElts[i];
3313 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3316 SDOperand ExtOp = (EltIdx < 8)
3317 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3318 DAG.getConstant(EltIdx, PtrVT))
3319 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3320 DAG.getConstant(EltIdx - 8, PtrVT));
3321 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3322 DAG.getConstant(i, PtrVT));
3327 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3328 ///as few as possible.
3329 // First, let's find out how many elements are already in the right order.
3330 unsigned V1InOrder = 0;
3331 unsigned V1FromV1 = 0;
3332 unsigned V2InOrder = 0;
3333 unsigned V2FromV2 = 0;
3334 SmallVector<SDOperand, 8> V1Elts;
3335 SmallVector<SDOperand, 8> V2Elts;
3336 for (unsigned i = 0; i < 8; ++i) {
3337 SDOperand Elt = MaskElts[i];
3338 if (Elt.getOpcode() == ISD::UNDEF) {
3339 V1Elts.push_back(Elt);
3340 V2Elts.push_back(Elt);
3345 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3347 V1Elts.push_back(Elt);
3348 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3350 } else if (EltIdx == i+8) {
3351 V1Elts.push_back(Elt);
3352 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3354 } else if (EltIdx < 8) {
3355 V1Elts.push_back(Elt);
3358 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3363 if (V2InOrder > V1InOrder) {
3364 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3366 std::swap(V1Elts, V2Elts);
3367 std::swap(V1FromV1, V2FromV2);
3370 if ((V1FromV1 + V1InOrder) != 8) {
3371 // Some elements are from V2.
3373 // If there are elements that are from V1 but out of place,
3374 // then first sort them in place
3375 SmallVector<SDOperand, 8> MaskVec;
3376 for (unsigned i = 0; i < 8; ++i) {
3377 SDOperand Elt = V1Elts[i];
3378 if (Elt.getOpcode() == ISD::UNDEF) {
3379 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3382 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3384 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3386 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3388 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3389 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
3393 for (unsigned i = 0; i < 8; ++i) {
3394 SDOperand Elt = V1Elts[i];
3395 if (Elt.getOpcode() == ISD::UNDEF)
3397 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3400 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3401 DAG.getConstant(EltIdx - 8, PtrVT));
3402 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3403 DAG.getConstant(i, PtrVT));
3407 // All elements are from V1.
3409 for (unsigned i = 0; i < 8; ++i) {
3410 SDOperand Elt = V1Elts[i];
3411 if (Elt.getOpcode() == ISD::UNDEF)
3413 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3414 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3415 DAG.getConstant(EltIdx, PtrVT));
3416 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3417 DAG.getConstant(i, PtrVT));
3423 /// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3424 /// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3425 /// done when every pair / quad of shuffle mask elements point to elements in
3426 /// the right sequence. e.g.
3427 /// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3429 SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3431 SDOperand PermMask, SelectionDAG &DAG,
3432 TargetLowering &TLI) {
3433 unsigned NumElems = PermMask.getNumOperands();
3434 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3435 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3436 MVT::ValueType NewVT = MaskVT;
3438 case MVT::v4f32: NewVT = MVT::v2f64; break;
3439 case MVT::v4i32: NewVT = MVT::v2i64; break;
3440 case MVT::v8i16: NewVT = MVT::v4i32; break;
3441 case MVT::v16i8: NewVT = MVT::v4i32; break;
3442 default: assert(false && "Unexpected!");
3445 if (NewWidth == 2) {
3446 if (MVT::isInteger(VT))
3451 unsigned Scale = NumElems / NewWidth;
3452 SmallVector<SDOperand, 8> MaskVec;
3453 for (unsigned i = 0; i < NumElems; i += Scale) {
3454 unsigned StartIdx = ~0U;
3455 for (unsigned j = 0; j < Scale; ++j) {
3456 SDOperand Elt = PermMask.getOperand(i+j);
3457 if (Elt.getOpcode() == ISD::UNDEF)
3459 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3460 if (StartIdx == ~0U)
3461 StartIdx = EltIdx - (EltIdx % Scale);
3462 if (EltIdx != StartIdx + j)
3465 if (StartIdx == ~0U)
3466 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3468 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
3471 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3472 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3473 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3474 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3475 &MaskVec[0], MaskVec.size()));
3479 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3480 SDOperand V1 = Op.getOperand(0);
3481 SDOperand V2 = Op.getOperand(1);
3482 SDOperand PermMask = Op.getOperand(2);
3483 MVT::ValueType VT = Op.getValueType();
3484 unsigned NumElems = PermMask.getNumOperands();
3485 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3486 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3487 bool V1IsSplat = false;
3488 bool V2IsSplat = false;
3490 if (isUndefShuffle(Op.Val))
3491 return DAG.getNode(ISD::UNDEF, VT);
3493 if (isZeroShuffle(Op.Val))
3494 return getZeroVector(VT, DAG);
3496 if (isIdentityMask(PermMask.Val))
3498 else if (isIdentityMask(PermMask.Val, true))
3501 if (isSplatMask(PermMask.Val)) {
3502 if (NumElems <= 4) return Op;
3503 // Promote it to a v4i32 splat.
3504 return PromoteSplat(Op, DAG);
3507 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3509 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3510 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3512 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3513 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3514 // FIXME: Figure out a cleaner way to do this.
3515 // Try to make use of movq to zero out the top part.
3516 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3517 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3519 SDOperand NewV1 = NewOp.getOperand(0);
3520 SDOperand NewV2 = NewOp.getOperand(1);
3521 SDOperand NewMask = NewOp.getOperand(2);
3522 if (isCommutedMOVL(NewMask.Val, true, false)) {
3523 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3524 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3525 NewV1, NewV2, getMOVLMask(2, DAG));
3526 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3529 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3530 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3531 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3532 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3536 if (X86::isMOVLMask(PermMask.Val))
3537 return (V1IsUndef) ? V2 : Op;
3539 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3540 X86::isMOVSLDUPMask(PermMask.Val) ||
3541 X86::isMOVHLPSMask(PermMask.Val) ||
3542 X86::isMOVHPMask(PermMask.Val) ||
3543 X86::isMOVLPMask(PermMask.Val))
3546 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3547 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3548 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3550 bool Commuted = false;
3551 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3552 // 1,1,1,1 -> v8i16 though.
3553 V1IsSplat = isSplatVector(V1.Val);
3554 V2IsSplat = isSplatVector(V2.Val);
3556 // Canonicalize the splat or undef, if present, to be on the RHS.
3557 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3558 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3559 std::swap(V1IsSplat, V2IsSplat);
3560 std::swap(V1IsUndef, V2IsUndef);
3564 // FIXME: Figure out a cleaner way to do this.
3565 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3566 if (V2IsUndef) return V1;
3567 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3569 // V2 is a splat, so the mask may be malformed. That is, it may point
3570 // to any V2 element. The instruction selectior won't like this. Get
3571 // a corrected mask and commute to form a proper MOVS{S|D}.
3572 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3573 if (NewMask.Val != PermMask.Val)
3574 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3579 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3580 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3581 X86::isUNPCKLMask(PermMask.Val) ||
3582 X86::isUNPCKHMask(PermMask.Val))
3586 // Normalize mask so all entries that point to V2 points to its first
3587 // element then try to match unpck{h|l} again. If match, return a
3588 // new vector_shuffle with the corrected mask.
3589 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3590 if (NewMask.Val != PermMask.Val) {
3591 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3592 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3593 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3594 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3595 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3596 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3601 // Normalize the node to match x86 shuffle ops if needed
3602 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3603 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3606 // Commute is back and try unpck* again.
3607 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3608 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3609 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3610 X86::isUNPCKLMask(PermMask.Val) ||
3611 X86::isUNPCKHMask(PermMask.Val))
3615 // If VT is integer, try PSHUF* first, then SHUFP*.
3616 if (MVT::isInteger(VT)) {
3617 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3618 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3619 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3620 X86::isPSHUFDMask(PermMask.Val)) ||
3621 X86::isPSHUFHWMask(PermMask.Val) ||
3622 X86::isPSHUFLWMask(PermMask.Val)) {
3623 if (V2.getOpcode() != ISD::UNDEF)
3624 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3625 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3629 if (X86::isSHUFPMask(PermMask.Val) &&
3630 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3633 // Floating point cases in the other order.
3634 if (X86::isSHUFPMask(PermMask.Val))
3636 if (X86::isPSHUFDMask(PermMask.Val) ||
3637 X86::isPSHUFHWMask(PermMask.Val) ||
3638 X86::isPSHUFLWMask(PermMask.Val)) {
3639 if (V2.getOpcode() != ISD::UNDEF)
3640 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3641 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3646 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3647 if (VT == MVT::v8i16) {
3648 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3653 // Handle all 4 wide cases with a number of shuffles.
3654 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
3655 // Don't do this for MMX.
3656 MVT::ValueType MaskVT = PermMask.getValueType();
3657 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3658 SmallVector<std::pair<int, int>, 8> Locs;
3659 Locs.reserve(NumElems);
3660 SmallVector<SDOperand, 8> Mask1(NumElems,
3661 DAG.getNode(ISD::UNDEF, MaskEVT));
3662 SmallVector<SDOperand, 8> Mask2(NumElems,
3663 DAG.getNode(ISD::UNDEF, MaskEVT));
3666 // If no more than two elements come from either vector. This can be
3667 // implemented with two shuffles. First shuffle gather the elements.
3668 // The second shuffle, which takes the first shuffle as both of its
3669 // vector operands, put the elements into the right order.
3670 for (unsigned i = 0; i != NumElems; ++i) {
3671 SDOperand Elt = PermMask.getOperand(i);
3672 if (Elt.getOpcode() == ISD::UNDEF) {
3673 Locs[i] = std::make_pair(-1, -1);
3675 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3676 if (Val < NumElems) {
3677 Locs[i] = std::make_pair(0, NumLo);
3681 Locs[i] = std::make_pair(1, NumHi);
3682 if (2+NumHi < NumElems)
3683 Mask1[2+NumHi] = Elt;
3688 if (NumLo <= 2 && NumHi <= 2) {
3689 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3690 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3691 &Mask1[0], Mask1.size()));
3692 for (unsigned i = 0; i != NumElems; ++i) {
3693 if (Locs[i].first == -1)
3696 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3697 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3698 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3702 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3703 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3704 &Mask2[0], Mask2.size()));
3707 // Break it into (shuffle shuffle_hi, shuffle_lo).
3709 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3710 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3711 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3712 unsigned MaskIdx = 0;
3714 unsigned HiIdx = NumElems/2;
3715 for (unsigned i = 0; i != NumElems; ++i) {
3716 if (i == NumElems/2) {
3722 SDOperand Elt = PermMask.getOperand(i);
3723 if (Elt.getOpcode() == ISD::UNDEF) {
3724 Locs[i] = std::make_pair(-1, -1);
3725 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3726 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3727 (*MaskPtr)[LoIdx] = Elt;
3730 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3731 (*MaskPtr)[HiIdx] = Elt;
3736 SDOperand LoShuffle =
3737 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3738 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3739 &LoMask[0], LoMask.size()));
3740 SDOperand HiShuffle =
3741 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3742 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3743 &HiMask[0], HiMask.size()));
3744 SmallVector<SDOperand, 8> MaskOps;
3745 for (unsigned i = 0; i != NumElems; ++i) {
3746 if (Locs[i].first == -1) {
3747 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3749 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3750 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3753 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3754 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3755 &MaskOps[0], MaskOps.size()));
3762 X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3763 SelectionDAG &DAG) {
3764 MVT::ValueType VT = Op.getValueType();
3765 if (MVT::getSizeInBits(VT) == 8) {
3766 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3767 Op.getOperand(0), Op.getOperand(1));
3768 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3769 DAG.getValueType(VT));
3770 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3771 } else if (MVT::getSizeInBits(VT) == 16) {
3772 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3773 Op.getOperand(0), Op.getOperand(1));
3774 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3775 DAG.getValueType(VT));
3776 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3783 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3784 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3787 if (Subtarget->hasSSE41())
3788 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3790 MVT::ValueType VT = Op.getValueType();
3791 // TODO: handle v16i8.
3792 if (MVT::getSizeInBits(VT) == 16) {
3793 SDOperand Vec = Op.getOperand(0);
3794 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3796 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3797 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3798 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3800 // Transform it so it match pextrw which produces a 32-bit result.
3801 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3802 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3803 Op.getOperand(0), Op.getOperand(1));
3804 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3805 DAG.getValueType(VT));
3806 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3807 } else if (MVT::getSizeInBits(VT) == 32) {
3808 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3811 // SHUFPS the element to the lowest double word, then movss.
3812 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3813 SmallVector<SDOperand, 8> IdxVec;
3815 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3817 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3819 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3821 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3822 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3823 &IdxVec[0], IdxVec.size());
3824 SDOperand Vec = Op.getOperand(0);
3825 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3826 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3827 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3828 DAG.getIntPtrConstant(0));
3829 } else if (MVT::getSizeInBits(VT) == 64) {
3830 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3831 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3832 // to match extract_elt for f64.
3833 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3837 // UNPCKHPD the element to the lowest double word, then movsd.
3838 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3839 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3840 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3841 SmallVector<SDOperand, 8> IdxVec;
3842 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
3844 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3845 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3846 &IdxVec[0], IdxVec.size());
3847 SDOperand Vec = Op.getOperand(0);
3848 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3849 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3850 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3851 DAG.getIntPtrConstant(0));
3858 X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3859 MVT::ValueType VT = Op.getValueType();
3860 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3862 SDOperand N0 = Op.getOperand(0);
3863 SDOperand N1 = Op.getOperand(1);
3864 SDOperand N2 = Op.getOperand(2);
3866 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3867 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3869 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3871 if (N1.getValueType() != MVT::i32)
3872 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3873 if (N2.getValueType() != MVT::i32)
3874 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3875 return DAG.getNode(Opc, VT, N0, N1, N2);
3876 } else if (EVT == MVT::f32) {
3877 // Bits [7:6] of the constant are the source select. This will always be
3878 // zero here. The DAG Combiner may combine an extract_elt index into these
3879 // bits. For example (insert (extract, 3), 2) could be matched by putting
3880 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3881 // Bits [5:4] of the constant are the destination select. This is the
3882 // value of the incoming immediate.
3883 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3884 // combine either bitwise AND or insert of float 0.0 to set these bits.
3885 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3886 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3892 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3893 MVT::ValueType VT = Op.getValueType();
3894 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3896 if (Subtarget->hasSSE41())
3897 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3902 SDOperand N0 = Op.getOperand(0);
3903 SDOperand N1 = Op.getOperand(1);
3904 SDOperand N2 = Op.getOperand(2);
3906 if (MVT::getSizeInBits(EVT) == 16) {
3907 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3908 // as its second argument.
3909 if (N1.getValueType() != MVT::i32)
3910 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3911 if (N2.getValueType() != MVT::i32)
3912 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3913 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3919 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3920 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3921 MVT::ValueType VT = MVT::v2i32;
3922 switch (Op.getValueType()) {
3929 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3930 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
3933 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3934 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3935 // one of the above mentioned nodes. It has to be wrapped because otherwise
3936 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3937 // be used to form addressing mode. These wrapped nodes will be selected
3940 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3941 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3942 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3944 CP->getAlignment());
3945 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3946 // With PIC, the address is actually $g + Offset.
3947 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3948 !Subtarget->isPICStyleRIPRel()) {
3949 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3950 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3958 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3959 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3960 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3961 // If it's a debug information descriptor, don't mess with it.
3962 if (DAG.isVerifiedDebugInfoDesc(Op))
3964 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3965 // With PIC, the address is actually $g + Offset.
3966 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3967 !Subtarget->isPICStyleRIPRel()) {
3968 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3969 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3973 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3974 // load the value at address GV, not the value of GV itself. This means that
3975 // the GlobalAddress must be in the base or index register of the address, not
3976 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3977 // The same applies for external symbols during PIC codegen
3978 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3979 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
3980 PseudoSourceValue::getGOT(), 0);
3985 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
3987 LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3988 const MVT::ValueType PtrVT) {
3990 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3991 DAG.getNode(X86ISD::GlobalBaseReg,
3993 InFlag = Chain.getValue(1);
3995 // emit leal symbol@TLSGD(,%ebx,1), %eax
3996 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3997 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3998 GA->getValueType(0),
4000 SDOperand Ops[] = { Chain, TGA, InFlag };
4001 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4002 InFlag = Result.getValue(2);
4003 Chain = Result.getValue(1);
4005 // call ___tls_get_addr. This function receives its argument in
4006 // the register EAX.
4007 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4008 InFlag = Chain.getValue(1);
4010 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4011 SDOperand Ops1[] = { Chain,
4012 DAG.getTargetExternalSymbol("___tls_get_addr",
4014 DAG.getRegister(X86::EAX, PtrVT),
4015 DAG.getRegister(X86::EBX, PtrVT),
4017 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4018 InFlag = Chain.getValue(1);
4020 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4023 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4024 // "local exec" model.
4026 LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4027 const MVT::ValueType PtrVT) {
4028 // Get the Thread Pointer
4029 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4030 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4032 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4033 GA->getValueType(0),
4035 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4037 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
4038 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
4039 PseudoSourceValue::getGOT(), 0);
4041 // The address of the thread local variable is the add of the thread
4042 // pointer with the offset of the variable.
4043 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4047 X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4048 // TODO: implement the "local dynamic" model
4049 // TODO: implement the "initial exec"model for pic executables
4050 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4051 "TLS not implemented for non-ELF and 64-bit targets");
4052 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4053 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4054 // otherwise use the "Local Exec"TLS Model
4055 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4056 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4058 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4062 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4063 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4064 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4065 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4066 // With PIC, the address is actually $g + Offset.
4067 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4068 !Subtarget->isPICStyleRIPRel()) {
4069 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4070 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4077 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4078 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4079 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4080 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4081 // With PIC, the address is actually $g + Offset.
4082 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4083 !Subtarget->isPICStyleRIPRel()) {
4084 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4085 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4092 /// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4093 /// take a 2 x i32 value to shift plus a shift amount.
4094 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
4095 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4096 MVT::ValueType VT = Op.getValueType();
4097 unsigned VTBits = MVT::getSizeInBits(VT);
4098 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4099 SDOperand ShOpLo = Op.getOperand(0);
4100 SDOperand ShOpHi = Op.getOperand(1);
4101 SDOperand ShAmt = Op.getOperand(2);
4102 SDOperand Tmp1 = isSRA ?
4103 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4104 DAG.getConstant(0, VT);
4106 SDOperand Tmp2, Tmp3;
4107 if (Op.getOpcode() == ISD::SHL_PARTS) {
4108 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4109 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
4111 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4112 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
4115 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4116 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4117 DAG.getConstant(VTBits, MVT::i8));
4118 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
4119 AndNode, DAG.getConstant(0, MVT::i8));
4122 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4123 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
4124 SmallVector<SDOperand, 4> Ops;
4125 if (Op.getOpcode() == ISD::SHL_PARTS) {
4126 Ops.push_back(Tmp2);
4127 Ops.push_back(Tmp3);
4129 Ops.push_back(Cond);
4130 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4133 Ops.push_back(Tmp3);
4134 Ops.push_back(Tmp1);
4136 Ops.push_back(Cond);
4137 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4139 Ops.push_back(Tmp2);
4140 Ops.push_back(Tmp3);
4142 Ops.push_back(Cond);
4143 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4146 Ops.push_back(Tmp3);
4147 Ops.push_back(Tmp1);
4149 Ops.push_back(Cond);
4150 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4153 VTs = DAG.getNodeValueTypes(VT, VT);
4157 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
4160 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4161 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4162 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4163 "Unknown SINT_TO_FP to lower!");
4165 // These are really Legal; caller falls through into that case.
4166 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4168 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4169 Subtarget->is64Bit())
4172 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4173 MachineFunction &MF = DAG.getMachineFunction();
4174 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4175 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4176 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
4178 PseudoSourceValue::getFixedStack(),
4183 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
4185 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4187 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4188 SmallVector<SDOperand, 8> Ops;
4189 Ops.push_back(Chain);
4190 Ops.push_back(StackSlot);
4191 Ops.push_back(DAG.getValueType(SrcVT));
4192 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4193 Tys, &Ops[0], Ops.size());
4196 Chain = Result.getValue(1);
4197 SDOperand InFlag = Result.getValue(2);
4199 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4200 // shouldn't be necessary except that RFP cannot be live across
4201 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4202 MachineFunction &MF = DAG.getMachineFunction();
4203 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4204 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4205 Tys = DAG.getVTList(MVT::Other);
4206 SmallVector<SDOperand, 8> Ops;
4207 Ops.push_back(Chain);
4208 Ops.push_back(Result);
4209 Ops.push_back(StackSlot);
4210 Ops.push_back(DAG.getValueType(Op.getValueType()));
4211 Ops.push_back(InFlag);
4212 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
4213 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
4214 PseudoSourceValue::getFixedStack(), SSFI);
4220 std::pair<SDOperand,SDOperand> X86TargetLowering::
4221 FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
4222 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4223 "Unknown FP_TO_SINT to lower!");
4225 // These are really Legal.
4226 if (Op.getValueType() == MVT::i32 &&
4227 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
4228 return std::make_pair(SDOperand(), SDOperand());
4229 if (Subtarget->is64Bit() &&
4230 Op.getValueType() == MVT::i64 &&
4231 Op.getOperand(0).getValueType() != MVT::f80)
4232 return std::make_pair(SDOperand(), SDOperand());
4234 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4236 MachineFunction &MF = DAG.getMachineFunction();
4237 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4238 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4239 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4241 switch (Op.getValueType()) {
4242 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4243 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4244 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4245 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
4248 SDOperand Chain = DAG.getEntryNode();
4249 SDOperand Value = Op.getOperand(0);
4250 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
4251 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
4252 Chain = DAG.getStore(Chain, Value, StackSlot,
4253 PseudoSourceValue::getFixedStack(), SSFI);
4254 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4256 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4258 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4259 Chain = Value.getValue(1);
4260 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4261 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4264 // Build the FP_TO_INT*_IN_MEM
4265 SDOperand Ops[] = { Chain, Value, StackSlot };
4266 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4268 return std::make_pair(FIST, StackSlot);
4271 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
4272 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4273 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4274 if (FIST.Val == 0) return SDOperand();
4277 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4280 SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4281 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4282 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4283 if (FIST.Val == 0) return 0;
4285 // Return an i64 load from the stack slot.
4286 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4288 // Use a MERGE_VALUES node to drop the chain result value.
4289 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4292 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4293 MVT::ValueType VT = Op.getValueType();
4294 MVT::ValueType EltVT = VT;
4295 if (MVT::isVector(VT))
4296 EltVT = MVT::getVectorElementType(VT);
4297 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4298 std::vector<Constant*> CV;
4299 if (EltVT == MVT::f64) {
4300 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
4304 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
4310 Constant *C = ConstantVector::get(CV);
4311 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4312 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4313 PseudoSourceValue::getConstantPool(), 0,
4315 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4318 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4319 MVT::ValueType VT = Op.getValueType();
4320 MVT::ValueType EltVT = VT;
4321 unsigned EltNum = 1;
4322 if (MVT::isVector(VT)) {
4323 EltVT = MVT::getVectorElementType(VT);
4324 EltNum = MVT::getVectorNumElements(VT);
4326 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4327 std::vector<Constant*> CV;
4328 if (EltVT == MVT::f64) {
4329 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
4333 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
4339 Constant *C = ConstantVector::get(CV);
4340 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4341 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4342 PseudoSourceValue::getConstantPool(), 0,
4344 if (MVT::isVector(VT)) {
4345 return DAG.getNode(ISD::BIT_CONVERT, VT,
4346 DAG.getNode(ISD::XOR, MVT::v2i64,
4347 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4348 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4350 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4354 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4355 SDOperand Op0 = Op.getOperand(0);
4356 SDOperand Op1 = Op.getOperand(1);
4357 MVT::ValueType VT = Op.getValueType();
4358 MVT::ValueType SrcVT = Op1.getValueType();
4359 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4361 // If second operand is smaller, extend it first.
4362 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4363 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4365 SrcTy = MVT::getTypeForValueType(SrcVT);
4367 // And if it is bigger, shrink it first.
4368 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4369 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
4371 SrcTy = MVT::getTypeForValueType(SrcVT);
4374 // At this point the operands and the result should have the same
4375 // type, and that won't be f80 since that is not custom lowered.
4377 // First get the sign bit of second operand.
4378 std::vector<Constant*> CV;
4379 if (SrcVT == MVT::f64) {
4380 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4381 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
4383 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4384 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4385 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4386 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4388 Constant *C = ConstantVector::get(CV);
4389 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4390 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
4391 PseudoSourceValue::getConstantPool(), 0,
4393 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4395 // Shift sign bit right or left if the two operands have different types.
4396 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4397 // Op0 is MVT::f32, Op1 is MVT::f64.
4398 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4399 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4400 DAG.getConstant(32, MVT::i32));
4401 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4402 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4403 DAG.getIntPtrConstant(0));
4406 // Clear first operand sign bit.
4408 if (VT == MVT::f64) {
4409 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4410 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
4412 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4413 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4414 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4415 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4417 C = ConstantVector::get(CV);
4418 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4419 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4420 PseudoSourceValue::getConstantPool(), 0,
4422 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4424 // Or the value with the sign bit.
4425 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4428 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
4429 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
4431 SDOperand Op0 = Op.getOperand(0);
4432 SDOperand Op1 = Op.getOperand(1);
4433 SDOperand CC = Op.getOperand(2);
4434 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4435 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4438 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
4440 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4441 return DAG.getNode(X86ISD::SETCC, MVT::i8,
4442 DAG.getConstant(X86CC, MVT::i8), Cond);
4445 assert(isFP && "Illegal integer SetCC!");
4447 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4448 switch (SetCCOpcode) {
4449 default: assert(false && "Illegal floating point SetCC!");
4450 case ISD::SETOEQ: { // !PF & ZF
4451 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4452 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
4453 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4454 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4455 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4457 case ISD::SETUNE: { // PF | !ZF
4458 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4459 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
4460 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4461 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4462 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4468 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4469 bool addTest = true;
4470 SDOperand Cond = Op.getOperand(0);
4473 if (Cond.getOpcode() == ISD::SETCC)
4474 Cond = LowerSETCC(Cond, DAG);
4476 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4477 // setting operand in place of the X86ISD::SETCC.
4478 if (Cond.getOpcode() == X86ISD::SETCC) {
4479 CC = Cond.getOperand(0);
4481 SDOperand Cmp = Cond.getOperand(1);
4482 unsigned Opc = Cmp.getOpcode();
4483 MVT::ValueType VT = Op.getValueType();
4485 bool IllegalFPCMov = false;
4486 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
4487 !isScalarFPTypeInSSEReg(VT)) // FPStack?
4488 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4490 if ((Opc == X86ISD::CMP ||
4491 Opc == X86ISD::COMI ||
4492 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
4499 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4500 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
4503 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4505 SmallVector<SDOperand, 4> Ops;
4506 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4507 // condition is true.
4508 Ops.push_back(Op.getOperand(2));
4509 Ops.push_back(Op.getOperand(1));
4511 Ops.push_back(Cond);
4512 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
4515 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4516 bool addTest = true;
4517 SDOperand Chain = Op.getOperand(0);
4518 SDOperand Cond = Op.getOperand(1);
4519 SDOperand Dest = Op.getOperand(2);
4522 if (Cond.getOpcode() == ISD::SETCC)
4523 Cond = LowerSETCC(Cond, DAG);
4525 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4526 // setting operand in place of the X86ISD::SETCC.
4527 if (Cond.getOpcode() == X86ISD::SETCC) {
4528 CC = Cond.getOperand(0);
4530 SDOperand Cmp = Cond.getOperand(1);
4531 unsigned Opc = Cmp.getOpcode();
4532 if (Opc == X86ISD::CMP ||
4533 Opc == X86ISD::COMI ||
4534 Opc == X86ISD::UCOMI) {
4541 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4542 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
4544 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
4545 Chain, Op.getOperand(2), CC, Cond);
4549 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4550 // Calls to _alloca is needed to probe the stack when allocating more than 4k
4551 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
4552 // that the guard pages used by the OS virtual memory manager are allocated in
4553 // correct sequence.
4555 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4556 SelectionDAG &DAG) {
4557 assert(Subtarget->isTargetCygMing() &&
4558 "This should be used only on Cygwin/Mingw targets");
4561 SDOperand Chain = Op.getOperand(0);
4562 SDOperand Size = Op.getOperand(1);
4563 // FIXME: Ensure alignment here
4567 MVT::ValueType IntPtr = getPointerTy();
4568 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
4570 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4571 Flag = Chain.getValue(1);
4573 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4574 SDOperand Ops[] = { Chain,
4575 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4576 DAG.getRegister(X86::EAX, IntPtr),
4578 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4579 Flag = Chain.getValue(1);
4581 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4583 std::vector<MVT::ValueType> Tys;
4584 Tys.push_back(SPTy);
4585 Tys.push_back(MVT::Other);
4586 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4587 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4590 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4591 SDOperand InFlag(0, 0);
4592 SDOperand Chain = Op.getOperand(0);
4594 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4595 if (Align == 0) Align = 1;
4597 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4598 // If not DWORD aligned or size is more than the threshold, call memset.
4599 // The libc version is likely to be faster for these cases. It can use the
4600 // address value and run time information about the CPU.
4601 if ((Align & 3) != 0 ||
4602 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
4603 MVT::ValueType IntPtr = getPointerTy();
4604 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4605 TargetLowering::ArgListTy Args;
4606 TargetLowering::ArgListEntry Entry;
4607 Entry.Node = Op.getOperand(1);
4608 Entry.Ty = IntPtrTy;
4609 Args.push_back(Entry);
4610 // Extend the unsigned i8 argument to be an int value for the call.
4611 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4612 Entry.Ty = IntPtrTy;
4613 Args.push_back(Entry);
4614 Entry.Node = Op.getOperand(3);
4615 Args.push_back(Entry);
4616 std::pair<SDOperand,SDOperand> CallResult =
4617 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4618 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4619 return CallResult.second;
4624 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4625 unsigned BytesLeft = 0;
4626 bool TwoRepStos = false;
4629 uint64_t Val = ValC->getValue() & 255;
4631 // If the value is a constant, then we can potentially use larger sets.
4632 switch (Align & 3) {
4633 case 2: // WORD aligned
4636 Val = (Val << 8) | Val;
4638 case 0: // DWORD aligned
4641 Val = (Val << 8) | Val;
4642 Val = (Val << 16) | Val;
4643 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4646 Val = (Val << 32) | Val;
4649 default: // Byte aligned
4652 Count = Op.getOperand(3);
4656 if (AVT > MVT::i8) {
4658 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4659 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
4660 BytesLeft = I->getValue() % UBytes;
4662 assert(AVT >= MVT::i32 &&
4663 "Do not use rep;stos if not at least DWORD aligned");
4664 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4665 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4670 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4672 InFlag = Chain.getValue(1);
4675 Count = Op.getOperand(3);
4676 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4677 InFlag = Chain.getValue(1);
4680 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4682 InFlag = Chain.getValue(1);
4683 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4684 Op.getOperand(1), InFlag);
4685 InFlag = Chain.getValue(1);
4687 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4688 SmallVector<SDOperand, 8> Ops;
4689 Ops.push_back(Chain);
4690 Ops.push_back(DAG.getValueType(AVT));
4691 Ops.push_back(InFlag);
4692 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4695 InFlag = Chain.getValue(1);
4696 Count = Op.getOperand(3);
4697 MVT::ValueType CVT = Count.getValueType();
4698 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4699 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4700 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4702 InFlag = Chain.getValue(1);
4703 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4705 Ops.push_back(Chain);
4706 Ops.push_back(DAG.getValueType(MVT::i8));
4707 Ops.push_back(InFlag);
4708 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4709 } else if (BytesLeft) {
4710 // Issue stores for the last 1 - 7 bytes.
4712 unsigned Val = ValC->getValue() & 255;
4713 unsigned Offset = I->getValue() - BytesLeft;
4714 SDOperand DstAddr = Op.getOperand(1);
4715 MVT::ValueType AddrVT = DstAddr.getValueType();
4716 if (BytesLeft >= 4) {
4717 Val = (Val << 8) | Val;
4718 Val = (Val << 16) | Val;
4719 Value = DAG.getConstant(Val, MVT::i32);
4720 Chain = DAG.getStore(Chain, Value,
4721 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4722 DAG.getConstant(Offset, AddrVT)),
4727 if (BytesLeft >= 2) {
4728 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4729 Chain = DAG.getStore(Chain, Value,
4730 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4731 DAG.getConstant(Offset, AddrVT)),
4736 if (BytesLeft == 1) {
4737 Value = DAG.getConstant(Val, MVT::i8);
4738 Chain = DAG.getStore(Chain, Value,
4739 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4740 DAG.getConstant(Offset, AddrVT)),
4748 SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4753 SelectionDAG &DAG) {
4755 unsigned BytesLeft = 0;
4756 switch (Align & 3) {
4757 case 2: // WORD aligned
4760 case 0: // DWORD aligned
4762 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4765 default: // Byte aligned
4770 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4771 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
4772 BytesLeft = Size % UBytes;
4774 SDOperand InFlag(0, 0);
4775 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4777 InFlag = Chain.getValue(1);
4778 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4780 InFlag = Chain.getValue(1);
4781 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4783 InFlag = Chain.getValue(1);
4785 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4786 SmallVector<SDOperand, 8> Ops;
4787 Ops.push_back(Chain);
4788 Ops.push_back(DAG.getValueType(AVT));
4789 Ops.push_back(InFlag);
4790 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4793 // Issue loads and stores for the last 1 - 7 bytes.
4794 unsigned Offset = Size - BytesLeft;
4795 SDOperand DstAddr = Dest;
4796 MVT::ValueType DstVT = DstAddr.getValueType();
4797 SDOperand SrcAddr = Source;
4798 MVT::ValueType SrcVT = SrcAddr.getValueType();
4800 if (BytesLeft >= 4) {
4801 Value = DAG.getLoad(MVT::i32, Chain,
4802 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4803 DAG.getConstant(Offset, SrcVT)),
4805 Chain = Value.getValue(1);
4806 Chain = DAG.getStore(Chain, Value,
4807 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4808 DAG.getConstant(Offset, DstVT)),
4813 if (BytesLeft >= 2) {
4814 Value = DAG.getLoad(MVT::i16, Chain,
4815 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4816 DAG.getConstant(Offset, SrcVT)),
4818 Chain = Value.getValue(1);
4819 Chain = DAG.getStore(Chain, Value,
4820 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4821 DAG.getConstant(Offset, DstVT)),
4827 if (BytesLeft == 1) {
4828 Value = DAG.getLoad(MVT::i8, Chain,
4829 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4830 DAG.getConstant(Offset, SrcVT)),
4832 Chain = Value.getValue(1);
4833 Chain = DAG.getStore(Chain, Value,
4834 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4835 DAG.getConstant(Offset, DstVT)),
4843 /// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4844 SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
4845 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4846 SDOperand TheChain = N->getOperand(0);
4847 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
4848 if (Subtarget->is64Bit()) {
4849 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4850 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4851 MVT::i64, rax.getValue(2));
4852 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
4853 DAG.getConstant(32, MVT::i8));
4855 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
4858 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4859 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
4862 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4863 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4864 MVT::i32, eax.getValue(2));
4865 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4866 SDOperand Ops[] = { eax, edx };
4867 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4869 // Use a MERGE_VALUES to return the value and chain.
4870 Ops[1] = edx.getValue(1);
4871 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4872 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
4875 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4876 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4878 if (!Subtarget->is64Bit()) {
4879 // vastart just stores the address of the VarArgsFrameIndex slot into the
4880 // memory location argument.
4881 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4882 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
4886 // gp_offset (0 - 6 * 8)
4887 // fp_offset (48 - 48 + 8 * 16)
4888 // overflow_arg_area (point to parameters coming in memory).
4890 SmallVector<SDOperand, 8> MemOps;
4891 SDOperand FIN = Op.getOperand(1);
4893 SDOperand Store = DAG.getStore(Op.getOperand(0),
4894 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4896 MemOps.push_back(Store);
4899 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
4900 Store = DAG.getStore(Op.getOperand(0),
4901 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4903 MemOps.push_back(Store);
4905 // Store ptr to overflow_arg_area
4906 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
4907 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4908 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
4909 MemOps.push_back(Store);
4911 // Store ptr to reg_save_area.
4912 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
4913 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4914 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
4915 MemOps.push_back(Store);
4916 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4919 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4920 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4921 SDOperand Chain = Op.getOperand(0);
4922 SDOperand DstPtr = Op.getOperand(1);
4923 SDOperand SrcPtr = Op.getOperand(2);
4924 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4925 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
4927 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
4928 Chain = SrcPtr.getValue(1);
4929 for (unsigned i = 0; i < 3; ++i) {
4930 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
4931 Chain = Val.getValue(1);
4932 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
4935 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4936 DAG.getIntPtrConstant(8));
4937 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4938 DAG.getIntPtrConstant(8));
4944 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4945 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4947 default: return SDOperand(); // Don't custom lower most intrinsics.
4948 // Comparison intrinsics.
4949 case Intrinsic::x86_sse_comieq_ss:
4950 case Intrinsic::x86_sse_comilt_ss:
4951 case Intrinsic::x86_sse_comile_ss:
4952 case Intrinsic::x86_sse_comigt_ss:
4953 case Intrinsic::x86_sse_comige_ss:
4954 case Intrinsic::x86_sse_comineq_ss:
4955 case Intrinsic::x86_sse_ucomieq_ss:
4956 case Intrinsic::x86_sse_ucomilt_ss:
4957 case Intrinsic::x86_sse_ucomile_ss:
4958 case Intrinsic::x86_sse_ucomigt_ss:
4959 case Intrinsic::x86_sse_ucomige_ss:
4960 case Intrinsic::x86_sse_ucomineq_ss:
4961 case Intrinsic::x86_sse2_comieq_sd:
4962 case Intrinsic::x86_sse2_comilt_sd:
4963 case Intrinsic::x86_sse2_comile_sd:
4964 case Intrinsic::x86_sse2_comigt_sd:
4965 case Intrinsic::x86_sse2_comige_sd:
4966 case Intrinsic::x86_sse2_comineq_sd:
4967 case Intrinsic::x86_sse2_ucomieq_sd:
4968 case Intrinsic::x86_sse2_ucomilt_sd:
4969 case Intrinsic::x86_sse2_ucomile_sd:
4970 case Intrinsic::x86_sse2_ucomigt_sd:
4971 case Intrinsic::x86_sse2_ucomige_sd:
4972 case Intrinsic::x86_sse2_ucomineq_sd: {
4974 ISD::CondCode CC = ISD::SETCC_INVALID;
4977 case Intrinsic::x86_sse_comieq_ss:
4978 case Intrinsic::x86_sse2_comieq_sd:
4982 case Intrinsic::x86_sse_comilt_ss:
4983 case Intrinsic::x86_sse2_comilt_sd:
4987 case Intrinsic::x86_sse_comile_ss:
4988 case Intrinsic::x86_sse2_comile_sd:
4992 case Intrinsic::x86_sse_comigt_ss:
4993 case Intrinsic::x86_sse2_comigt_sd:
4997 case Intrinsic::x86_sse_comige_ss:
4998 case Intrinsic::x86_sse2_comige_sd:
5002 case Intrinsic::x86_sse_comineq_ss:
5003 case Intrinsic::x86_sse2_comineq_sd:
5007 case Intrinsic::x86_sse_ucomieq_ss:
5008 case Intrinsic::x86_sse2_ucomieq_sd:
5009 Opc = X86ISD::UCOMI;
5012 case Intrinsic::x86_sse_ucomilt_ss:
5013 case Intrinsic::x86_sse2_ucomilt_sd:
5014 Opc = X86ISD::UCOMI;
5017 case Intrinsic::x86_sse_ucomile_ss:
5018 case Intrinsic::x86_sse2_ucomile_sd:
5019 Opc = X86ISD::UCOMI;
5022 case Intrinsic::x86_sse_ucomigt_ss:
5023 case Intrinsic::x86_sse2_ucomigt_sd:
5024 Opc = X86ISD::UCOMI;
5027 case Intrinsic::x86_sse_ucomige_ss:
5028 case Intrinsic::x86_sse2_ucomige_sd:
5029 Opc = X86ISD::UCOMI;
5032 case Intrinsic::x86_sse_ucomineq_ss:
5033 case Intrinsic::x86_sse2_ucomineq_sd:
5034 Opc = X86ISD::UCOMI;
5040 SDOperand LHS = Op.getOperand(1);
5041 SDOperand RHS = Op.getOperand(2);
5042 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5044 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5045 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5046 DAG.getConstant(X86CC, MVT::i8), Cond);
5047 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
5052 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5053 // Depths > 0 not supported yet!
5054 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5057 // Just load the return address
5058 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5059 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5062 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5063 // Depths > 0 not supported yet!
5064 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5067 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5068 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
5069 DAG.getIntPtrConstant(4));
5072 SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5073 SelectionDAG &DAG) {
5074 // Is not yet supported on x86-64
5075 if (Subtarget->is64Bit())
5078 return DAG.getIntPtrConstant(8);
5081 SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5083 assert(!Subtarget->is64Bit() &&
5084 "Lowering of eh_return builtin is not supported yet on x86-64");
5086 MachineFunction &MF = DAG.getMachineFunction();
5087 SDOperand Chain = Op.getOperand(0);
5088 SDOperand Offset = Op.getOperand(1);
5089 SDOperand Handler = Op.getOperand(2);
5091 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5094 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
5095 DAG.getIntPtrConstant(-4UL));
5096 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5097 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5098 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
5099 MF.getRegInfo().addLiveOut(X86::ECX);
5101 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5102 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5105 SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5106 SelectionDAG &DAG) {
5107 SDOperand Root = Op.getOperand(0);
5108 SDOperand Trmp = Op.getOperand(1); // trampoline
5109 SDOperand FPtr = Op.getOperand(2); // nested function
5110 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5112 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
5114 const X86InstrInfo *TII =
5115 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5117 if (Subtarget->is64Bit()) {
5118 SDOperand OutChains[6];
5120 // Large code-model.
5122 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5123 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5125 const unsigned char N86R10 =
5126 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
5127 const unsigned char N86R11 =
5128 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
5130 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5132 // Load the pointer to the nested function into R11.
5133 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5134 SDOperand Addr = Trmp;
5135 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5138 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
5139 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
5141 // Load the 'nest' parameter value into R10.
5142 // R10 is specified in X86CallingConv.td
5143 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5144 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5145 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5148 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
5149 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
5151 // Jump to the nested function.
5152 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5153 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5154 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5157 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5158 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5159 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
5163 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5164 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
5166 const Function *Func =
5167 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5168 unsigned CC = Func->getCallingConv();
5173 assert(0 && "Unsupported calling convention");
5174 case CallingConv::C:
5175 case CallingConv::X86_StdCall: {
5176 // Pass 'nest' parameter in ECX.
5177 // Must be kept in sync with X86CallingConv.td
5180 // Check that ECX wasn't needed by an 'inreg' parameter.
5181 const FunctionType *FTy = Func->getFunctionType();
5182 const ParamAttrsList *Attrs = Func->getParamAttrs();
5184 if (Attrs && !Func->isVarArg()) {
5185 unsigned InRegCount = 0;
5188 for (FunctionType::param_iterator I = FTy->param_begin(),
5189 E = FTy->param_end(); I != E; ++I, ++Idx)
5190 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5191 // FIXME: should only count parameters that are lowered to integers.
5192 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5194 if (InRegCount > 2) {
5195 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5201 case CallingConv::X86_FastCall:
5202 // Pass 'nest' parameter in EAX.
5203 // Must be kept in sync with X86CallingConv.td
5208 SDOperand OutChains[4];
5209 SDOperand Addr, Disp;
5211 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5212 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5214 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5215 const unsigned char N86Reg =
5216 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
5217 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
5220 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5221 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
5223 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
5224 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5225 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5226 TrmpAddr, 5, false, 1);
5228 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5229 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
5232 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5233 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
5237 SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
5239 The rounding mode is in bits 11:10 of FPSR, and has the following
5246 FLT_ROUNDS, on the other hand, expects the following:
5253 To perform the conversion, we do:
5254 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5257 MachineFunction &MF = DAG.getMachineFunction();
5258 const TargetMachine &TM = MF.getTarget();
5259 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5260 unsigned StackAlignment = TFI.getStackAlignment();
5261 MVT::ValueType VT = Op.getValueType();
5263 // Save FP Control Word to stack slot
5264 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5265 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5267 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5268 DAG.getEntryNode(), StackSlot);
5270 // Load FP Control Word from stack slot
5271 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5273 // Transform as necessary
5275 DAG.getNode(ISD::SRL, MVT::i16,
5276 DAG.getNode(ISD::AND, MVT::i16,
5277 CWD, DAG.getConstant(0x800, MVT::i16)),
5278 DAG.getConstant(11, MVT::i8));
5280 DAG.getNode(ISD::SRL, MVT::i16,
5281 DAG.getNode(ISD::AND, MVT::i16,
5282 CWD, DAG.getConstant(0x400, MVT::i16)),
5283 DAG.getConstant(9, MVT::i8));
5286 DAG.getNode(ISD::AND, MVT::i16,
5287 DAG.getNode(ISD::ADD, MVT::i16,
5288 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5289 DAG.getConstant(1, MVT::i16)),
5290 DAG.getConstant(3, MVT::i16));
5293 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5294 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5297 SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5298 MVT::ValueType VT = Op.getValueType();
5299 MVT::ValueType OpVT = VT;
5300 unsigned NumBits = MVT::getSizeInBits(VT);
5302 Op = Op.getOperand(0);
5303 if (VT == MVT::i8) {
5304 // Zero extend to i32 since there is not an i8 bsr.
5306 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5309 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5310 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5311 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5313 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5314 SmallVector<SDOperand, 4> Ops;
5316 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5317 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5318 Ops.push_back(Op.getValue(1));
5319 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5321 // Finally xor with NumBits-1.
5322 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5325 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5329 SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5330 MVT::ValueType VT = Op.getValueType();
5331 MVT::ValueType OpVT = VT;
5332 unsigned NumBits = MVT::getSizeInBits(VT);
5334 Op = Op.getOperand(0);
5335 if (VT == MVT::i8) {
5337 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5340 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5341 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5342 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5344 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5345 SmallVector<SDOperand, 4> Ops;
5347 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5348 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5349 Ops.push_back(Op.getValue(1));
5350 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5353 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5357 SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
5358 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
5362 case MVT::i8: Reg = X86::AL; size = 1; break;
5363 case MVT::i16: Reg = X86::AX; size = 2; break;
5364 case MVT::i32: Reg = X86::EAX; size = 4; break;
5366 if (Subtarget->is64Bit()) {
5367 Reg = X86::RAX; size = 8;
5368 } else //Should go away when LowerType stuff lands
5369 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5372 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5373 Op.getOperand(3), SDOperand());
5374 SDOperand Ops[] = { cpIn.getValue(0),
5377 DAG.getTargetConstant(size, MVT::i8),
5379 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5380 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5382 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5386 SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5387 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5388 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5389 SDOperand cpInL, cpInH;
5390 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5391 DAG.getConstant(0, MVT::i32));
5392 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5393 DAG.getConstant(1, MVT::i32));
5394 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5395 cpInL, SDOperand());
5396 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5397 cpInH, cpInL.getValue(1));
5398 SDOperand swapInL, swapInH;
5399 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5400 DAG.getConstant(0, MVT::i32));
5401 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5402 DAG.getConstant(1, MVT::i32));
5403 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5404 swapInL, cpInH.getValue(1));
5405 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5406 swapInH, swapInL.getValue(1));
5407 SDOperand Ops[] = { swapInH.getValue(0),
5409 swapInH.getValue(1)};
5410 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5411 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5412 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5413 Result.getValue(1));
5414 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5415 cpOutL.getValue(2));
5416 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5417 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5418 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5419 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5422 /// LowerOperation - Provide custom lowering hooks for some operations.
5424 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5425 switch (Op.getOpcode()) {
5426 default: assert(0 && "Should not custom lower this!");
5427 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
5428 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5429 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5430 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5431 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5432 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5433 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5434 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5435 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5436 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5437 case ISD::SHL_PARTS:
5438 case ISD::SRA_PARTS:
5439 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5440 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5441 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5442 case ISD::FABS: return LowerFABS(Op, DAG);
5443 case ISD::FNEG: return LowerFNEG(Op, DAG);
5444 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
5445 case ISD::SETCC: return LowerSETCC(Op, DAG);
5446 case ISD::SELECT: return LowerSELECT(Op, DAG);
5447 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
5448 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5449 case ISD::CALL: return LowerCALL(Op, DAG);
5450 case ISD::RET: return LowerRET(Op, DAG);
5451 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5452 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5453 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
5454 case ISD::VASTART: return LowerVASTART(Op, DAG);
5455 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5456 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5457 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5458 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5459 case ISD::FRAME_TO_ARGS_OFFSET:
5460 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5461 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5462 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
5463 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
5464 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
5465 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5466 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
5468 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5469 case ISD::READCYCLECOUNTER:
5470 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
5474 /// ExpandOperation - Provide custom lowering hooks for expanding operations.
5475 SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5476 switch (N->getOpcode()) {
5477 default: assert(0 && "Should not custom lower this!");
5478 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5479 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5480 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
5484 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5486 default: return NULL;
5487 case X86ISD::BSF: return "X86ISD::BSF";
5488 case X86ISD::BSR: return "X86ISD::BSR";
5489 case X86ISD::SHLD: return "X86ISD::SHLD";
5490 case X86ISD::SHRD: return "X86ISD::SHRD";
5491 case X86ISD::FAND: return "X86ISD::FAND";
5492 case X86ISD::FOR: return "X86ISD::FOR";
5493 case X86ISD::FXOR: return "X86ISD::FXOR";
5494 case X86ISD::FSRL: return "X86ISD::FSRL";
5495 case X86ISD::FILD: return "X86ISD::FILD";
5496 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5497 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5498 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5499 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5500 case X86ISD::FLD: return "X86ISD::FLD";
5501 case X86ISD::FST: return "X86ISD::FST";
5502 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
5503 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
5504 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5505 case X86ISD::CALL: return "X86ISD::CALL";
5506 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5507 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5508 case X86ISD::CMP: return "X86ISD::CMP";
5509 case X86ISD::COMI: return "X86ISD::COMI";
5510 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5511 case X86ISD::SETCC: return "X86ISD::SETCC";
5512 case X86ISD::CMOV: return "X86ISD::CMOV";
5513 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5514 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5515 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5516 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
5517 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5518 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5519 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
5520 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5521 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5522 case X86ISD::PINSRB: return "X86ISD::PINSRB";
5523 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5524 case X86ISD::FMAX: return "X86ISD::FMAX";
5525 case X86ISD::FMIN: return "X86ISD::FMIN";
5526 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5527 case X86ISD::FRCP: return "X86ISD::FRCP";
5528 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5529 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5530 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
5531 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
5532 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
5533 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
5534 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
5538 // isLegalAddressingMode - Return true if the addressing mode represented
5539 // by AM is legal for this target, for a load/store of the specified type.
5540 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5541 const Type *Ty) const {
5542 // X86 supports extremely general addressing modes.
5544 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5545 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5549 // We can only fold this if we don't need an extra load.
5550 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5553 // X86-64 only supports addr of globals in small code model.
5554 if (Subtarget->is64Bit()) {
5555 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5557 // If lower 4G is not available, then we must use rip-relative addressing.
5558 if (AM.BaseOffs || AM.Scale > 1)
5569 // These scales always work.
5574 // These scales are formed with basereg+scalereg. Only accept if there is
5579 default: // Other stuff never works.
5587 bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5588 if (!Ty1->isInteger() || !Ty2->isInteger())
5590 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5591 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5592 if (NumBits1 <= NumBits2)
5594 return Subtarget->is64Bit() || NumBits1 < 64;
5597 bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5598 MVT::ValueType VT2) const {
5599 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5601 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5602 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5603 if (NumBits1 <= NumBits2)
5605 return Subtarget->is64Bit() || NumBits1 < 64;
5608 /// isShuffleMaskLegal - Targets can use this to indicate that they only
5609 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5610 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5611 /// are assumed to be legal.
5613 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5614 // Only do shuffles on 128-bit vector types for now.
5615 if (MVT::getSizeInBits(VT) == 64) return false;
5616 return (Mask.Val->getNumOperands() <= 4 ||
5617 isIdentityMask(Mask.Val) ||
5618 isIdentityMask(Mask.Val, true) ||
5619 isSplatMask(Mask.Val) ||
5620 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5621 X86::isUNPCKLMask(Mask.Val) ||
5622 X86::isUNPCKHMask(Mask.Val) ||
5623 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5624 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5627 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5629 SelectionDAG &DAG) const {
5630 unsigned NumElts = BVOps.size();
5631 // Only do shuffles on 128-bit vector types for now.
5632 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5633 if (NumElts == 2) return true;
5635 return (isMOVLMask(&BVOps[0], 4) ||
5636 isCommutedMOVL(&BVOps[0], 4, true) ||
5637 isSHUFPMask(&BVOps[0], 4) ||
5638 isCommutedSHUFP(&BVOps[0], 4));
5643 //===----------------------------------------------------------------------===//
5644 // X86 Scheduler Hooks
5645 //===----------------------------------------------------------------------===//
5648 X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5649 MachineBasicBlock *BB) {
5650 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5651 switch (MI->getOpcode()) {
5652 default: assert(false && "Unexpected instr type to insert");
5653 case X86::CMOV_FR32:
5654 case X86::CMOV_FR64:
5655 case X86::CMOV_V4F32:
5656 case X86::CMOV_V2F64:
5657 case X86::CMOV_V2I64: {
5658 // To "insert" a SELECT_CC instruction, we actually have to insert the
5659 // diamond control-flow pattern. The incoming instruction knows the
5660 // destination vreg to set, the condition code register to branch on, the
5661 // true/false values to select between, and a branch opcode to use.
5662 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5663 ilist<MachineBasicBlock>::iterator It = BB;
5669 // cmpTY ccX, r1, r2
5671 // fallthrough --> copy0MBB
5672 MachineBasicBlock *thisMBB = BB;
5673 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5674 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5676 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5677 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5678 MachineFunction *F = BB->getParent();
5679 F->getBasicBlockList().insert(It, copy0MBB);
5680 F->getBasicBlockList().insert(It, sinkMBB);
5681 // Update machine-CFG edges by first adding all successors of the current
5682 // block to the new block which will contain the Phi node for the select.
5683 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5684 e = BB->succ_end(); i != e; ++i)
5685 sinkMBB->addSuccessor(*i);
5686 // Next, remove all successors of the current block, and add the true
5687 // and fallthrough blocks as its successors.
5688 while(!BB->succ_empty())
5689 BB->removeSuccessor(BB->succ_begin());
5690 BB->addSuccessor(copy0MBB);
5691 BB->addSuccessor(sinkMBB);
5694 // %FalseValue = ...
5695 // # fallthrough to sinkMBB
5698 // Update machine-CFG edges
5699 BB->addSuccessor(sinkMBB);
5702 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5705 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5706 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5707 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5709 delete MI; // The pseudo instruction is gone now.
5713 case X86::FP32_TO_INT16_IN_MEM:
5714 case X86::FP32_TO_INT32_IN_MEM:
5715 case X86::FP32_TO_INT64_IN_MEM:
5716 case X86::FP64_TO_INT16_IN_MEM:
5717 case X86::FP64_TO_INT32_IN_MEM:
5718 case X86::FP64_TO_INT64_IN_MEM:
5719 case X86::FP80_TO_INT16_IN_MEM:
5720 case X86::FP80_TO_INT32_IN_MEM:
5721 case X86::FP80_TO_INT64_IN_MEM: {
5722 // Change the floating point control register to use "round towards zero"
5723 // mode when truncating to an integer value.
5724 MachineFunction *F = BB->getParent();
5725 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5726 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5728 // Load the old value of the high byte of the control word...
5730 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
5731 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5733 // Set the high part to be round to zero...
5734 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5737 // Reload the modified control word now...
5738 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5740 // Restore the memory image of control word to original value
5741 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5744 // Get the X86 opcode to use.
5746 switch (MI->getOpcode()) {
5747 default: assert(0 && "illegal opcode!");
5748 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5749 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5750 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5751 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5752 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5753 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
5754 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5755 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5756 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
5760 MachineOperand &Op = MI->getOperand(0);
5761 if (Op.isRegister()) {
5762 AM.BaseType = X86AddressMode::RegBase;
5763 AM.Base.Reg = Op.getReg();
5765 AM.BaseType = X86AddressMode::FrameIndexBase;
5766 AM.Base.FrameIndex = Op.getIndex();
5768 Op = MI->getOperand(1);
5769 if (Op.isImmediate())
5770 AM.Scale = Op.getImm();
5771 Op = MI->getOperand(2);
5772 if (Op.isImmediate())
5773 AM.IndexReg = Op.getImm();
5774 Op = MI->getOperand(3);
5775 if (Op.isGlobalAddress()) {
5776 AM.GV = Op.getGlobal();
5778 AM.Disp = Op.getImm();
5780 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5781 .addReg(MI->getOperand(4).getReg());
5783 // Reload the original control word now.
5784 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5786 delete MI; // The pseudo instruction is gone now.
5792 //===----------------------------------------------------------------------===//
5793 // X86 Optimization Hooks
5794 //===----------------------------------------------------------------------===//
5796 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5800 const SelectionDAG &DAG,
5801 unsigned Depth) const {
5802 unsigned Opc = Op.getOpcode();
5803 assert((Opc >= ISD::BUILTIN_OP_END ||
5804 Opc == ISD::INTRINSIC_WO_CHAIN ||
5805 Opc == ISD::INTRINSIC_W_CHAIN ||
5806 Opc == ISD::INTRINSIC_VOID) &&
5807 "Should use MaskedValueIsZero if you don't know whether Op"
5808 " is a target node!");
5810 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
5814 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5815 Mask.getBitWidth() - 1);
5820 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
5821 /// element of the result of the vector shuffle.
5822 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5823 MVT::ValueType VT = N->getValueType(0);
5824 SDOperand PermMask = N->getOperand(2);
5825 unsigned NumElems = PermMask.getNumOperands();
5826 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5828 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5830 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5831 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5832 SDOperand Idx = PermMask.getOperand(i);
5833 if (Idx.getOpcode() == ISD::UNDEF)
5834 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5835 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5840 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5841 /// node is a GlobalAddress + an offset.
5842 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5843 unsigned Opc = N->getOpcode();
5844 if (Opc == X86ISD::Wrapper) {
5845 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5846 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5849 } else if (Opc == ISD::ADD) {
5850 SDOperand N1 = N->getOperand(0);
5851 SDOperand N2 = N->getOperand(1);
5852 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5853 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5855 Offset += V->getSignExtended();
5858 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5859 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5861 Offset += V->getSignExtended();
5869 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
5871 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5872 MachineFrameInfo *MFI) {
5873 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5876 SDOperand Loc = N->getOperand(1);
5877 SDOperand BaseLoc = Base->getOperand(1);
5878 if (Loc.getOpcode() == ISD::FrameIndex) {
5879 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5881 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5882 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5883 int FS = MFI->getObjectSize(FI);
5884 int BFS = MFI->getObjectSize(BFI);
5885 if (FS != BFS || FS != Size) return false;
5886 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5888 GlobalValue *GV1 = NULL;
5889 GlobalValue *GV2 = NULL;
5890 int64_t Offset1 = 0;
5891 int64_t Offset2 = 0;
5892 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5893 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5894 if (isGA1 && isGA2 && GV1 == GV2)
5895 return Offset1 == (Offset2 + Dist*Size);
5901 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5902 const X86Subtarget *Subtarget) {
5905 if (isGAPlusOffset(Base, GV, Offset))
5906 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5907 // DAG combine handles the stack object case.
5912 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5913 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5914 /// if the load addresses are consecutive, non-overlapping, and in the right
5916 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5917 const X86Subtarget *Subtarget) {
5918 MachineFunction &MF = DAG.getMachineFunction();
5919 MachineFrameInfo *MFI = MF.getFrameInfo();
5920 MVT::ValueType VT = N->getValueType(0);
5921 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5922 SDOperand PermMask = N->getOperand(2);
5923 int NumElems = (int)PermMask.getNumOperands();
5924 SDNode *Base = NULL;
5925 for (int i = 0; i < NumElems; ++i) {
5926 SDOperand Idx = PermMask.getOperand(i);
5927 if (Idx.getOpcode() == ISD::UNDEF) {
5928 if (!Base) return SDOperand();
5931 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5932 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5936 else if (!isConsecutiveLoad(Arg.Val, Base,
5937 i, MVT::getSizeInBits(EVT)/8,MFI))
5942 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
5943 LoadSDNode *LD = cast<LoadSDNode>(Base);
5945 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5946 LD->getSrcValueOffset(), LD->isVolatile());
5948 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5949 LD->getSrcValueOffset(), LD->isVolatile(),
5950 LD->getAlignment());
5954 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5955 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5956 const X86Subtarget *Subtarget) {
5957 SDOperand Cond = N->getOperand(0);
5959 // If we have SSE[12] support, try to form min/max nodes.
5960 if (Subtarget->hasSSE2() &&
5961 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5962 if (Cond.getOpcode() == ISD::SETCC) {
5963 // Get the LHS/RHS of the select.
5964 SDOperand LHS = N->getOperand(1);
5965 SDOperand RHS = N->getOperand(2);
5966 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5968 unsigned Opcode = 0;
5969 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5972 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5975 if (!UnsafeFPMath) break;
5977 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5979 Opcode = X86ISD::FMIN;
5982 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5985 if (!UnsafeFPMath) break;
5987 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5989 Opcode = X86ISD::FMAX;
5992 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5995 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5998 if (!UnsafeFPMath) break;
6000 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6002 Opcode = X86ISD::FMIN;
6005 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6008 if (!UnsafeFPMath) break;
6010 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6012 Opcode = X86ISD::FMAX;
6018 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6026 /// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6027 static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6028 const X86Subtarget *Subtarget) {
6029 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6030 // the FP state in cases where an emms may be missing.
6031 // A preferable solution to the general problem is to figure out the right
6032 // places to insert EMMS. This qualifies as a quick hack.
6033 if (MVT::isVector(St->getValue().getValueType()) &&
6034 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
6035 isa<LoadSDNode>(St->getValue()) &&
6036 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6037 St->getChain().hasOneUse() && !St->isVolatile()) {
6038 SDNode* LdVal = St->getValue().Val;
6040 int TokenFactorIndex = -1;
6041 SmallVector<SDOperand, 8> Ops;
6042 SDNode* ChainVal = St->getChain().Val;
6043 // Must be a store of a load. We currently handle two cases: the load
6044 // is a direct child, and it's under an intervening TokenFactor. It is
6045 // possible to dig deeper under nested TokenFactors.
6046 if (ChainVal == LdVal)
6047 Ld = cast<LoadSDNode>(St->getChain());
6048 else if (St->getValue().hasOneUse() &&
6049 ChainVal->getOpcode() == ISD::TokenFactor) {
6050 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
6051 if (ChainVal->getOperand(i).Val == LdVal) {
6052 TokenFactorIndex = i;
6053 Ld = cast<LoadSDNode>(St->getValue());
6055 Ops.push_back(ChainVal->getOperand(i));
6059 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6060 if (Subtarget->is64Bit()) {
6061 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6062 Ld->getBasePtr(), Ld->getSrcValue(),
6063 Ld->getSrcValueOffset(), Ld->isVolatile(),
6064 Ld->getAlignment());
6065 SDOperand NewChain = NewLd.getValue(1);
6066 if (TokenFactorIndex != -1) {
6067 Ops.push_back(NewLd);
6068 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6071 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6072 St->getSrcValue(), St->getSrcValueOffset(),
6073 St->isVolatile(), St->getAlignment());
6076 // Otherwise, lower to two 32-bit copies.
6077 SDOperand LoAddr = Ld->getBasePtr();
6078 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6079 DAG.getConstant(MVT::i32, 4));
6081 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6082 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6083 Ld->isVolatile(), Ld->getAlignment());
6084 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6085 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6087 MinAlign(Ld->getAlignment(), 4));
6089 SDOperand NewChain = LoLd.getValue(1);
6090 if (TokenFactorIndex != -1) {
6091 Ops.push_back(LoLd);
6092 Ops.push_back(HiLd);
6093 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6097 LoAddr = St->getBasePtr();
6098 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6099 DAG.getConstant(MVT::i32, 4));
6101 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
6102 St->getSrcValue(), St->getSrcValueOffset(),
6103 St->isVolatile(), St->getAlignment());
6104 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6105 St->getSrcValue(), St->getSrcValueOffset()+4,
6107 MinAlign(St->getAlignment(), 4));
6108 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
6114 /// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6115 /// X86ISD::FXOR nodes.
6116 static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
6117 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6118 // F[X]OR(0.0, x) -> x
6119 // F[X]OR(x, 0.0) -> x
6120 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6121 if (C->getValueAPF().isPosZero())
6122 return N->getOperand(1);
6123 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6124 if (C->getValueAPF().isPosZero())
6125 return N->getOperand(0);
6129 /// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6130 static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6131 // FAND(0.0, x) -> 0.0
6132 // FAND(x, 0.0) -> 0.0
6133 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6134 if (C->getValueAPF().isPosZero())
6135 return N->getOperand(0);
6136 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6137 if (C->getValueAPF().isPosZero())
6138 return N->getOperand(1);
6143 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6144 DAGCombinerInfo &DCI) const {
6145 SelectionDAG &DAG = DCI.DAG;
6146 switch (N->getOpcode()) {
6148 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6149 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
6151 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
6153 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6154 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
6160 //===----------------------------------------------------------------------===//
6161 // X86 Inline Assembly Support
6162 //===----------------------------------------------------------------------===//
6164 /// getConstraintType - Given a constraint letter, return the type of
6165 /// constraint it is for this target.
6166 X86TargetLowering::ConstraintType
6167 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6168 if (Constraint.size() == 1) {
6169 switch (Constraint[0]) {
6178 return C_RegisterClass;
6183 return TargetLowering::getConstraintType(Constraint);
6186 /// LowerXConstraint - try to replace an X constraint, which matches anything,
6187 /// with another that has more specific requirements based on the type of the
6188 /// corresponding operand.
6189 void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6190 std::string& s) const {
6191 if (MVT::isFloatingPoint(ConstraintVT)) {
6192 if (Subtarget->hasSSE2())
6194 else if (Subtarget->hasSSE1())
6199 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6202 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6203 /// vector. If it is invalid, don't add anything to Ops.
6204 void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6206 std::vector<SDOperand>&Ops,
6207 SelectionDAG &DAG) {
6208 SDOperand Result(0, 0);
6210 switch (Constraint) {
6213 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
6214 if (C->getValue() <= 31) {
6215 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6221 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
6222 if (C->getValue() <= 255) {
6223 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6229 // Literal immediates are always ok.
6230 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6231 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6235 // If we are in non-pic codegen mode, we allow the address of a global (with
6236 // an optional displacement) to be used with 'i'.
6237 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6240 // Match either (GA) or (GA+C)
6242 Offset = GA->getOffset();
6243 } else if (Op.getOpcode() == ISD::ADD) {
6244 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6245 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6247 Offset = GA->getOffset()+C->getValue();
6249 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6250 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6252 Offset = GA->getOffset()+C->getValue();
6259 // If addressing this global requires a load (e.g. in PIC mode), we can't
6261 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6265 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6271 // Otherwise, not valid for this mode.
6277 Ops.push_back(Result);
6280 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
6283 std::vector<unsigned> X86TargetLowering::
6284 getRegClassForInlineAsmConstraint(const std::string &Constraint,
6285 MVT::ValueType VT) const {
6286 if (Constraint.size() == 1) {
6287 // FIXME: not handling fp-stack yet!
6288 switch (Constraint[0]) { // GCC X86 Constraint Letters
6289 default: break; // Unknown constraint letter
6290 case 'A': // EAX/EDX
6291 if (VT == MVT::i32 || VT == MVT::i64)
6292 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6294 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6297 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6298 else if (VT == MVT::i16)
6299 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6300 else if (VT == MVT::i8)
6301 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
6302 else if (VT == MVT::i64)
6303 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6308 return std::vector<unsigned>();
6311 std::pair<unsigned, const TargetRegisterClass*>
6312 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6313 MVT::ValueType VT) const {
6314 // First, see if this is a constraint that directly corresponds to an LLVM
6316 if (Constraint.size() == 1) {
6317 // GCC Constraint Letters
6318 switch (Constraint[0]) {
6320 case 'r': // GENERAL_REGS
6321 case 'R': // LEGACY_REGS
6322 case 'l': // INDEX_REGS
6323 if (VT == MVT::i64 && Subtarget->is64Bit())
6324 return std::make_pair(0U, X86::GR64RegisterClass);
6326 return std::make_pair(0U, X86::GR32RegisterClass);
6327 else if (VT == MVT::i16)
6328 return std::make_pair(0U, X86::GR16RegisterClass);
6329 else if (VT == MVT::i8)
6330 return std::make_pair(0U, X86::GR8RegisterClass);
6332 case 'y': // MMX_REGS if MMX allowed.
6333 if (!Subtarget->hasMMX()) break;
6334 return std::make_pair(0U, X86::VR64RegisterClass);
6336 case 'Y': // SSE_REGS if SSE2 allowed
6337 if (!Subtarget->hasSSE2()) break;
6339 case 'x': // SSE_REGS if SSE1 allowed
6340 if (!Subtarget->hasSSE1()) break;
6344 // Scalar SSE types.
6347 return std::make_pair(0U, X86::FR32RegisterClass);
6350 return std::make_pair(0U, X86::FR64RegisterClass);
6358 return std::make_pair(0U, X86::VR128RegisterClass);
6364 // Use the default implementation in TargetLowering to convert the register
6365 // constraint into a member of a register class.
6366 std::pair<unsigned, const TargetRegisterClass*> Res;
6367 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6369 // Not found as a standard register?
6370 if (Res.second == 0) {
6371 // GCC calls "st(0)" just plain "st".
6372 if (StringsEqualNoCase("{st}", Constraint)) {
6373 Res.first = X86::ST0;
6374 Res.second = X86::RFP80RegisterClass;
6380 // Otherwise, check to see if this is a register class of the wrong value
6381 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6382 // turn into {ax},{dx}.
6383 if (Res.second->hasType(VT))
6384 return Res; // Correct type already, nothing to do.
6386 // All of the single-register GCC register classes map their values onto
6387 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6388 // really want an 8-bit or 32-bit register, map to the appropriate register
6389 // class and return the appropriate register.
6390 if (Res.second != X86::GR16RegisterClass)
6393 if (VT == MVT::i8) {
6394 unsigned DestReg = 0;
6395 switch (Res.first) {
6397 case X86::AX: DestReg = X86::AL; break;
6398 case X86::DX: DestReg = X86::DL; break;
6399 case X86::CX: DestReg = X86::CL; break;
6400 case X86::BX: DestReg = X86::BL; break;
6403 Res.first = DestReg;
6404 Res.second = Res.second = X86::GR8RegisterClass;
6406 } else if (VT == MVT::i32) {
6407 unsigned DestReg = 0;
6408 switch (Res.first) {
6410 case X86::AX: DestReg = X86::EAX; break;
6411 case X86::DX: DestReg = X86::EDX; break;
6412 case X86::CX: DestReg = X86::ECX; break;
6413 case X86::BX: DestReg = X86::EBX; break;
6414 case X86::SI: DestReg = X86::ESI; break;
6415 case X86::DI: DestReg = X86::EDI; break;
6416 case X86::BP: DestReg = X86::EBP; break;
6417 case X86::SP: DestReg = X86::ESP; break;
6420 Res.first = DestReg;
6421 Res.second = Res.second = X86::GR32RegisterClass;
6423 } else if (VT == MVT::i64) {
6424 unsigned DestReg = 0;
6425 switch (Res.first) {
6427 case X86::AX: DestReg = X86::RAX; break;
6428 case X86::DX: DestReg = X86::RDX; break;
6429 case X86::CX: DestReg = X86::RCX; break;
6430 case X86::BX: DestReg = X86::RBX; break;
6431 case X86::SI: DestReg = X86::RSI; break;
6432 case X86::DI: DestReg = X86::RDI; break;
6433 case X86::BP: DestReg = X86::RBP; break;
6434 case X86::SP: DestReg = X86::RSP; break;
6437 Res.first = DestReg;
6438 Res.second = Res.second = X86::GR64RegisterClass;