blob: 94505c45c0f6a63fd9d5803dc86b498a5e9c463c [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/ADT/VectorExtras.h"
27#include "llvm/Analysis/ScalarEvolutionExpressions.h"
28#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/SelectionDAG.h"
33#include "llvm/CodeGen/SSARegMap.h"
34#include "llvm/Support/MathExtras.h"
35#include "llvm/Target/TargetOptions.h"
36#include "llvm/ADT/StringExtras.h"
37using namespace llvm;
38
39X86TargetLowering::X86TargetLowering(TargetMachine &TM)
40 : TargetLowering(TM) {
41 Subtarget = &TM.getSubtarget<X86Subtarget>();
42 X86ScalarSSE = Subtarget->hasSSE2();
43 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
44
45 RegInfo = TM.getRegisterInfo();
46
47 // Set up the TargetLowering object.
48
49 // X86 is weird, it always uses i8 for shift amounts and setcc results.
50 setShiftAmountType(MVT::i8);
51 setSetCCResultType(MVT::i8);
52 setSetCCResultContents(ZeroOrOneSetCCResult);
53 setSchedulingPreference(SchedulingForRegPressure);
54 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
55 setStackPointerRegisterToSaveRestore(X86StackPtr);
56
57 if (Subtarget->isTargetDarwin()) {
58 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
59 setUseUnderscoreSetJmp(false);
60 setUseUnderscoreLongJmp(false);
61 } else if (Subtarget->isTargetMingw()) {
62 // MS runtime is weird: it exports _setjmp, but longjmp!
63 setUseUnderscoreSetJmp(true);
64 setUseUnderscoreLongJmp(false);
65 } else {
66 setUseUnderscoreSetJmp(true);
67 setUseUnderscoreLongJmp(true);
68 }
69
70 // Set up the register classes.
71 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
72 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
73 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
74 if (Subtarget->is64Bit())
75 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
76
77 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
78
79 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
80 // operation.
81 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
82 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
83 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
84
85 if (Subtarget->is64Bit()) {
86 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
87 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
88 } else {
89 if (X86ScalarSSE)
90 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
91 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
92 else
93 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
94 }
95
96 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
97 // this operation.
98 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
100 // SSE has no i16 to fp conversion, only i32
101 if (X86ScalarSSE)
102 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
103 else {
104 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
105 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
106 }
107
108 if (!Subtarget->is64Bit()) {
109 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
110 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
111 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
112 }
113
114 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
115 // this operation.
116 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
117 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
118
119 if (X86ScalarSSE) {
120 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
121 } else {
122 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
123 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
124 }
125
126 // Handle FP_TO_UINT by promoting the destination to a larger signed
127 // conversion.
128 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
129 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
130 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
131
132 if (Subtarget->is64Bit()) {
133 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
134 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
135 } else {
136 if (X86ScalarSSE && !Subtarget->hasSSE3())
137 // Expand FP_TO_UINT into a select.
138 // FIXME: We would like to use a Custom expander here eventually to do
139 // the optimal thing for SSE vs. the default expansion in the legalizer.
140 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
141 else
142 // With SSE3 we can use fisttpll to convert to a signed i64.
143 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
144 }
145
146 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
147 if (!X86ScalarSSE) {
148 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
149 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
150 }
151
152 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
153 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
154 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
155 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
156 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
157 if (Subtarget->is64Bit())
158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
160 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
161 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
162 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
163 setOperationAction(ISD::FREM , MVT::f64 , Expand);
164
165 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
166 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
167 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
168 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
169 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
170 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
171 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
172 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
173 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
174 if (Subtarget->is64Bit()) {
175 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
176 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
177 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
178 }
179
180 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
181 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
182
183 // These should be promoted to a larger select which is supported.
184 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
185 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
186 // X86 wants to expand cmov itself.
187 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
188 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
189 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
190 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
191 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
192 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
193 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
194 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
195 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
196 if (Subtarget->is64Bit()) {
197 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
198 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
199 }
200 // X86 ret instruction may pop stack.
201 setOperationAction(ISD::RET , MVT::Other, Custom);
202 if (!Subtarget->is64Bit())
203 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
204
205 // Darwin ABI issue.
206 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
207 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
208 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
209 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
210 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
211 if (Subtarget->is64Bit()) {
212 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
213 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
214 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
215 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
216 }
217 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
218 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
219 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
220 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
221 // X86 wants to expand memset / memcpy itself.
222 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
223 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
224
225 // We don't have line number support yet.
226 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
227 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
228 // FIXME - use subtarget debug flags
229 if (!Subtarget->isTargetDarwin() &&
230 !Subtarget->isTargetELF() &&
231 !Subtarget->isTargetCygMing())
232 setOperationAction(ISD::LABEL, MVT::Other, Expand);
233
234 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
235 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
236 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
237 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
238 if (Subtarget->is64Bit()) {
239 // FIXME: Verify
240 setExceptionPointerRegister(X86::RAX);
241 setExceptionSelectorRegister(X86::RDX);
242 } else {
243 setExceptionPointerRegister(X86::EAX);
244 setExceptionSelectorRegister(X86::EDX);
245 }
246
247 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
248 setOperationAction(ISD::VASTART , MVT::Other, Custom);
249 setOperationAction(ISD::VAARG , MVT::Other, Expand);
250 setOperationAction(ISD::VAEND , MVT::Other, Expand);
251 if (Subtarget->is64Bit())
252 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
253 else
254 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
255
256 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
257 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
258 if (Subtarget->is64Bit())
259 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
260 if (Subtarget->isTargetCygMing())
261 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
262 else
263 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
264
265 if (X86ScalarSSE) {
266 // Set up the FP register classes.
267 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
268 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
269
270 // Use ANDPD to simulate FABS.
271 setOperationAction(ISD::FABS , MVT::f64, Custom);
272 setOperationAction(ISD::FABS , MVT::f32, Custom);
273
274 // Use XORP to simulate FNEG.
275 setOperationAction(ISD::FNEG , MVT::f64, Custom);
276 setOperationAction(ISD::FNEG , MVT::f32, Custom);
277
278 // Use ANDPD and ORPD to simulate FCOPYSIGN.
279 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
280 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
281
282 // We don't support sin/cos/fmod
283 setOperationAction(ISD::FSIN , MVT::f64, Expand);
284 setOperationAction(ISD::FCOS , MVT::f64, Expand);
285 setOperationAction(ISD::FREM , MVT::f64, Expand);
286 setOperationAction(ISD::FSIN , MVT::f32, Expand);
287 setOperationAction(ISD::FCOS , MVT::f32, Expand);
288 setOperationAction(ISD::FREM , MVT::f32, Expand);
289
290 // Expand FP immediates into loads from the stack, except for the special
291 // cases we handle.
292 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
293 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
294 addLegalFPImmediate(+0.0); // xorps / xorpd
295 } else {
296 // Set up the FP register classes.
297 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
298 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
299
300 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
301 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
302 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
303 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
304 setOperationAction(ISD::FP_ROUND, MVT::f32, Expand);
305
306 if (!UnsafeFPMath) {
307 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
308 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
309 }
310
311 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
312 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
313 addLegalFPImmediate(+0.0); // FLD0
314 addLegalFPImmediate(+1.0); // FLD1
315 addLegalFPImmediate(-0.0); // FLD0/FCHS
316 addLegalFPImmediate(-1.0); // FLD1/FCHS
317 }
318
319 // First set operation action for all vector types to expand. Then we
320 // will selectively turn on ones that can be effectively codegen'd.
321 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
322 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
323 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
324 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
325 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
326 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
327 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
328 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
329 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
330 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
331 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
332 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
333 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
334 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
335 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
336 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
337 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
338 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
339 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
340 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
341 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
342 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
343 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
344 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
345 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
346 }
347
348 if (Subtarget->hasMMX()) {
349 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
350 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
351 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
352 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
353
354 // FIXME: add MMX packed arithmetics
355
356 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
357 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
358 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
359 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
360
361 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
362 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
363 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
364
365 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
366 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
367
368 setOperationAction(ISD::AND, MVT::v8i8, Promote);
369 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
370 setOperationAction(ISD::AND, MVT::v4i16, Promote);
371 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
372 setOperationAction(ISD::AND, MVT::v2i32, Promote);
373 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
374 setOperationAction(ISD::AND, MVT::v1i64, Legal);
375
376 setOperationAction(ISD::OR, MVT::v8i8, Promote);
377 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
378 setOperationAction(ISD::OR, MVT::v4i16, Promote);
379 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
380 setOperationAction(ISD::OR, MVT::v2i32, Promote);
381 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
382 setOperationAction(ISD::OR, MVT::v1i64, Legal);
383
384 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
385 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
386 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
387 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
388 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
389 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
390 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
391
392 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
393 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
394 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
395 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
396 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
397 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
398 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
399
400 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
401 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
402 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
403 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
404
405 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
406 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
407 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
408 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
409
410 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
411 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
412 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
413 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
414 }
415
416 if (Subtarget->hasSSE1()) {
417 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
418
419 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
420 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
421 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
422 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
423 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
424 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
426 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
427 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
428 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
429 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
430 }
431
432 if (Subtarget->hasSSE2()) {
433 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
434 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
435 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
436 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
437 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
438
439 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
440 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
441 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
442 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
443 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
444 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
445 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
446 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
447 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
448 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
449 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
450 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
451 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
452 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
453 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454
455 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
456 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
457 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
458 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
459 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
460 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
461
462 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
463 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
464 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
465 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
466 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
467 }
468 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
469 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
470 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
471 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
472 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
473 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
474
475 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
476 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
477 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
478 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
479 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
480 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
481 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
482 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
483 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
484 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
485 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
486 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
487 }
488
489 // Custom lower v2i64 and v2f64 selects.
490 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
491 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
492 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
493 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
494 }
495
496 // We want to custom lower some of our intrinsics.
497 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
498
499 // We have target-specific dag combine patterns for the following nodes:
500 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
501 setTargetDAGCombine(ISD::SELECT);
502
503 computeRegisterProperties();
504
505 // FIXME: These should be based on subtarget info. Plus, the values should
506 // be smaller when we are in optimizing for size mode.
507 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
508 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
509 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
510 allowUnalignedMemoryAccesses = true; // x86 supports it!
511}
512
513
514//===----------------------------------------------------------------------===//
515// Return Value Calling Convention Implementation
516//===----------------------------------------------------------------------===//
517
518#include "X86GenCallingConv.inc"
519
520/// LowerRET - Lower an ISD::RET node.
521SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
522 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
523
524 SmallVector<CCValAssign, 16> RVLocs;
525 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
526 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
527 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
528 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
529
530
531 // If this is the first return lowered for this function, add the regs to the
532 // liveout set for the function.
533 if (DAG.getMachineFunction().liveout_empty()) {
534 for (unsigned i = 0; i != RVLocs.size(); ++i)
535 if (RVLocs[i].isRegLoc())
536 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
537 }
538
539 SDOperand Chain = Op.getOperand(0);
540 SDOperand Flag;
541
542 // Copy the result values into the output registers.
543 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
544 RVLocs[0].getLocReg() != X86::ST0) {
545 for (unsigned i = 0; i != RVLocs.size(); ++i) {
546 CCValAssign &VA = RVLocs[i];
547 assert(VA.isRegLoc() && "Can only return in registers!");
548 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
549 Flag);
550 Flag = Chain.getValue(1);
551 }
552 } else {
553 // We need to handle a destination of ST0 specially, because it isn't really
554 // a register.
555 SDOperand Value = Op.getOperand(1);
556
557 // If this is an FP return with ScalarSSE, we need to move the value from
558 // an XMM register onto the fp-stack.
559 if (X86ScalarSSE) {
560 SDOperand MemLoc;
561
562 // If this is a load into a scalarsse value, don't store the loaded value
563 // back to the stack, only to reload it: just replace the scalar-sse load.
564 if (ISD::isNON_EXTLoad(Value.Val) &&
565 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
566 Chain = Value.getOperand(0);
567 MemLoc = Value.getOperand(1);
568 } else {
569 // Spill the value to memory and reload it into top of stack.
570 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
571 MachineFunction &MF = DAG.getMachineFunction();
572 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
573 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
574 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
575 }
576 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
577 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
578 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
579 Chain = Value.getValue(1);
580 }
581
582 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
583 SDOperand Ops[] = { Chain, Value };
584 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
585 Flag = Chain.getValue(1);
586 }
587
588 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
589 if (Flag.Val)
590 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
591 else
592 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
593}
594
595
596/// LowerCallResult - Lower the result values of an ISD::CALL into the
597/// appropriate copies out of appropriate physical registers. This assumes that
598/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
599/// being lowered. The returns a SDNode with the same number of values as the
600/// ISD::CALL.
601SDNode *X86TargetLowering::
602LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
603 unsigned CallingConv, SelectionDAG &DAG) {
604
605 // Assign locations to each value returned by this call.
606 SmallVector<CCValAssign, 16> RVLocs;
607 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
608 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
609 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
610
611
612 SmallVector<SDOperand, 8> ResultVals;
613
614 // Copy all of the result registers out of their specified physreg.
615 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
616 for (unsigned i = 0; i != RVLocs.size(); ++i) {
617 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
618 RVLocs[i].getValVT(), InFlag).getValue(1);
619 InFlag = Chain.getValue(2);
620 ResultVals.push_back(Chain.getValue(0));
621 }
622 } else {
623 // Copies from the FP stack are special, as ST0 isn't a valid register
624 // before the fp stackifier runs.
625
626 // Copy ST0 into an RFP register with FP_GET_RESULT.
627 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
628 SDOperand GROps[] = { Chain, InFlag };
629 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
630 Chain = RetVal.getValue(1);
631 InFlag = RetVal.getValue(2);
632
633 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
634 // an XMM register.
635 if (X86ScalarSSE) {
636 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
637 // shouldn't be necessary except that RFP cannot be live across
638 // multiple blocks. When stackifier is fixed, they can be uncoupled.
639 MachineFunction &MF = DAG.getMachineFunction();
640 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
641 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
642 SDOperand Ops[] = {
643 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
644 };
645 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
646 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
647 Chain = RetVal.getValue(1);
648 }
649 ResultVals.push_back(RetVal);
650 }
651
652 // Merge everything together with a MERGE_VALUES node.
653 ResultVals.push_back(Chain);
654 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
655 &ResultVals[0], ResultVals.size()).Val;
656}
657
658
659//===----------------------------------------------------------------------===//
660// C & StdCall Calling Convention implementation
661//===----------------------------------------------------------------------===//
662// StdCall calling convention seems to be standard for many Windows' API
663// routines and around. It differs from C calling convention just a little:
664// callee should clean up the stack, not caller. Symbols should be also
665// decorated in some fancy way :) It doesn't support any vector arguments.
666
667/// AddLiveIn - This helper function adds the specified physical register to the
668/// MachineFunction as a live in value. It also creates a corresponding virtual
669/// register for it.
670static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
671 const TargetRegisterClass *RC) {
672 assert(RC->contains(PReg) && "Not the correct regclass!");
673 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
674 MF.addLiveIn(PReg, VReg);
675 return VReg;
676}
677
678SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
679 bool isStdCall) {
680 unsigned NumArgs = Op.Val->getNumValues() - 1;
681 MachineFunction &MF = DAG.getMachineFunction();
682 MachineFrameInfo *MFI = MF.getFrameInfo();
683 SDOperand Root = Op.getOperand(0);
684 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
685
686 // Assign locations to all of the incoming arguments.
687 SmallVector<CCValAssign, 16> ArgLocs;
688 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
689 getTargetMachine(), ArgLocs);
690 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
691
692 SmallVector<SDOperand, 8> ArgValues;
693 unsigned LastVal = ~0U;
694 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
695 CCValAssign &VA = ArgLocs[i];
696 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
697 // places.
698 assert(VA.getValNo() != LastVal &&
699 "Don't support value assigned to multiple locs yet");
700 LastVal = VA.getValNo();
701
702 if (VA.isRegLoc()) {
703 MVT::ValueType RegVT = VA.getLocVT();
704 TargetRegisterClass *RC;
705 if (RegVT == MVT::i32)
706 RC = X86::GR32RegisterClass;
707 else {
708 assert(MVT::isVector(RegVT));
709 RC = X86::VR128RegisterClass;
710 }
711
712 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
713 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
714
715 // If this is an 8 or 16-bit value, it is really passed promoted to 32
716 // bits. Insert an assert[sz]ext to capture this, then truncate to the
717 // right size.
718 if (VA.getLocInfo() == CCValAssign::SExt)
719 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
720 DAG.getValueType(VA.getValVT()));
721 else if (VA.getLocInfo() == CCValAssign::ZExt)
722 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
723 DAG.getValueType(VA.getValVT()));
724
725 if (VA.getLocInfo() != CCValAssign::Full)
726 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
727
728 ArgValues.push_back(ArgValue);
729 } else {
730 assert(VA.isMemLoc());
731
732 // Create the nodes corresponding to a load from this parameter slot.
733 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
734 VA.getLocMemOffset());
735 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
736 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
737 }
738 }
739
740 unsigned StackSize = CCInfo.getNextStackOffset();
741
742 ArgValues.push_back(Root);
743
744 // If the function takes variable number of arguments, make a frame index for
745 // the start of the first vararg value... for expansion of llvm.va_start.
746 if (isVarArg)
747 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
748
749 if (isStdCall && !isVarArg) {
750 BytesToPopOnReturn = StackSize; // Callee pops everything..
751 BytesCallerReserves = 0;
752 } else {
753 BytesToPopOnReturn = 0; // Callee pops nothing.
754
755 // If this is an sret function, the return should pop the hidden pointer.
756 if (NumArgs &&
757 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
758 ISD::ParamFlags::StructReturn))
759 BytesToPopOnReturn = 4;
760
761 BytesCallerReserves = StackSize;
762 }
763
764 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
765 ReturnAddrIndex = 0; // No return address slot generated yet.
766
767 MF.getInfo<X86MachineFunctionInfo>()
768 ->setBytesToPopOnReturn(BytesToPopOnReturn);
769
770 // Return the new list of results.
771 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
772 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
773}
774
775SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
776 unsigned CC) {
777 SDOperand Chain = Op.getOperand(0);
778 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
779 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
780 SDOperand Callee = Op.getOperand(4);
781 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
782
783 // Analyze operands of the call, assigning locations to each operand.
784 SmallVector<CCValAssign, 16> ArgLocs;
785 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
786 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
787
788 // Get a count of how many bytes are to be pushed on the stack.
789 unsigned NumBytes = CCInfo.getNextStackOffset();
790
791 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
792
793 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
794 SmallVector<SDOperand, 8> MemOpChains;
795
796 SDOperand StackPtr;
797
798 // Walk the register/memloc assignments, inserting copies/loads.
799 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
800 CCValAssign &VA = ArgLocs[i];
801 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
802
803 // Promote the value if needed.
804 switch (VA.getLocInfo()) {
805 default: assert(0 && "Unknown loc info!");
806 case CCValAssign::Full: break;
807 case CCValAssign::SExt:
808 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
809 break;
810 case CCValAssign::ZExt:
811 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
812 break;
813 case CCValAssign::AExt:
814 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
815 break;
816 }
817
818 if (VA.isRegLoc()) {
819 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
820 } else {
821 assert(VA.isMemLoc());
822 if (StackPtr.Val == 0)
823 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
824 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
825 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
826 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
827 }
828 }
829
830 // If the first argument is an sret pointer, remember it.
831 bool isSRet = NumOps &&
832 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
833 ISD::ParamFlags::StructReturn);
834
835 if (!MemOpChains.empty())
836 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
837 &MemOpChains[0], MemOpChains.size());
838
839 // Build a sequence of copy-to-reg nodes chained together with token chain
840 // and flag operands which copy the outgoing args into registers.
841 SDOperand InFlag;
842 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
843 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
844 InFlag);
845 InFlag = Chain.getValue(1);
846 }
847
848 // ELF / PIC requires GOT in the EBX register before function calls via PLT
849 // GOT pointer.
850 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
851 Subtarget->isPICStyleGOT()) {
852 Chain = DAG.getCopyToReg(Chain, X86::EBX,
853 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
854 InFlag);
855 InFlag = Chain.getValue(1);
856 }
857
858 // If the callee is a GlobalAddress node (quite common, every direct call is)
859 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
860 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
861 // We should use extra load for direct calls to dllimported functions in
862 // non-JIT mode.
863 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
864 getTargetMachine(), true))
865 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
866 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
867 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
868
869 // Returns a chain & a flag for retval copy to use.
870 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
871 SmallVector<SDOperand, 8> Ops;
872 Ops.push_back(Chain);
873 Ops.push_back(Callee);
874
875 // Add argument registers to the end of the list so that they are known live
876 // into the call.
877 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
878 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
879 RegsToPass[i].second.getValueType()));
880
881 // Add an implicit use GOT pointer in EBX.
882 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
883 Subtarget->isPICStyleGOT())
884 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
885
886 if (InFlag.Val)
887 Ops.push_back(InFlag);
888
889 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
890 NodeTys, &Ops[0], Ops.size());
891 InFlag = Chain.getValue(1);
892
893 // Create the CALLSEQ_END node.
894 unsigned NumBytesForCalleeToPush = 0;
895
896 if (CC == CallingConv::X86_StdCall) {
897 if (isVarArg)
898 NumBytesForCalleeToPush = isSRet ? 4 : 0;
899 else
900 NumBytesForCalleeToPush = NumBytes;
901 } else {
902 // If this is is a call to a struct-return function, the callee
903 // pops the hidden struct pointer, so we have to push it back.
904 // This is common for Darwin/X86, Linux & Mingw32 targets.
905 NumBytesForCalleeToPush = isSRet ? 4 : 0;
906 }
907
908 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
909 Ops.clear();
910 Ops.push_back(Chain);
911 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
912 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
913 Ops.push_back(InFlag);
914 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
915 InFlag = Chain.getValue(1);
916
917 // Handle result values, copying them out of physregs into vregs that we
918 // return.
919 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
920}
921
922
923//===----------------------------------------------------------------------===//
924// FastCall Calling Convention implementation
925//===----------------------------------------------------------------------===//
926//
927// The X86 'fastcall' calling convention passes up to two integer arguments in
928// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
929// and requires that the callee pop its arguments off the stack (allowing proper
930// tail calls), and has the same return value conventions as C calling convs.
931//
932// This calling convention always arranges for the callee pop value to be 8n+4
933// bytes, which is needed for tail recursion elimination and stack alignment
934// reasons.
935SDOperand
936X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
937 MachineFunction &MF = DAG.getMachineFunction();
938 MachineFrameInfo *MFI = MF.getFrameInfo();
939 SDOperand Root = Op.getOperand(0);
940 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
941
942 // Assign locations to all of the incoming arguments.
943 SmallVector<CCValAssign, 16> ArgLocs;
944 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
945 getTargetMachine(), ArgLocs);
946 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
947
948 SmallVector<SDOperand, 8> ArgValues;
949 unsigned LastVal = ~0U;
950 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
951 CCValAssign &VA = ArgLocs[i];
952 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
953 // places.
954 assert(VA.getValNo() != LastVal &&
955 "Don't support value assigned to multiple locs yet");
956 LastVal = VA.getValNo();
957
958 if (VA.isRegLoc()) {
959 MVT::ValueType RegVT = VA.getLocVT();
960 TargetRegisterClass *RC;
961 if (RegVT == MVT::i32)
962 RC = X86::GR32RegisterClass;
963 else {
964 assert(MVT::isVector(RegVT));
965 RC = X86::VR128RegisterClass;
966 }
967
968 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
969 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
970
971 // If this is an 8 or 16-bit value, it is really passed promoted to 32
972 // bits. Insert an assert[sz]ext to capture this, then truncate to the
973 // right size.
974 if (VA.getLocInfo() == CCValAssign::SExt)
975 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
976 DAG.getValueType(VA.getValVT()));
977 else if (VA.getLocInfo() == CCValAssign::ZExt)
978 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
979 DAG.getValueType(VA.getValVT()));
980
981 if (VA.getLocInfo() != CCValAssign::Full)
982 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
983
984 ArgValues.push_back(ArgValue);
985 } else {
986 assert(VA.isMemLoc());
987
988 // Create the nodes corresponding to a load from this parameter slot.
989 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
990 VA.getLocMemOffset());
991 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
992 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
993 }
994 }
995
996 ArgValues.push_back(Root);
997
998 unsigned StackSize = CCInfo.getNextStackOffset();
999
1000 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1001 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1002 // arguments and the arguments after the retaddr has been pushed are aligned.
1003 if ((StackSize & 7) == 0)
1004 StackSize += 4;
1005 }
1006
1007 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1008 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
1009 ReturnAddrIndex = 0; // No return address slot generated yet.
1010 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
1011 BytesCallerReserves = 0;
1012
1013 MF.getInfo<X86MachineFunctionInfo>()
1014 ->setBytesToPopOnReturn(BytesToPopOnReturn);
1015
1016 // Return the new list of results.
1017 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1018 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1019}
1020
1021SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1022 unsigned CC) {
1023 SDOperand Chain = Op.getOperand(0);
1024 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1025 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1026 SDOperand Callee = Op.getOperand(4);
1027
1028 // Analyze operands of the call, assigning locations to each operand.
1029 SmallVector<CCValAssign, 16> ArgLocs;
1030 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1031 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
1032
1033 // Get a count of how many bytes are to be pushed on the stack.
1034 unsigned NumBytes = CCInfo.getNextStackOffset();
1035
1036 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1037 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1038 // arguments and the arguments after the retaddr has been pushed are aligned.
1039 if ((NumBytes & 7) == 0)
1040 NumBytes += 4;
1041 }
1042
1043 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1044
1045 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1046 SmallVector<SDOperand, 8> MemOpChains;
1047
1048 SDOperand StackPtr;
1049
1050 // Walk the register/memloc assignments, inserting copies/loads.
1051 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1052 CCValAssign &VA = ArgLocs[i];
1053 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1054
1055 // Promote the value if needed.
1056 switch (VA.getLocInfo()) {
1057 default: assert(0 && "Unknown loc info!");
1058 case CCValAssign::Full: break;
1059 case CCValAssign::SExt:
1060 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1061 break;
1062 case CCValAssign::ZExt:
1063 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1064 break;
1065 case CCValAssign::AExt:
1066 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1067 break;
1068 }
1069
1070 if (VA.isRegLoc()) {
1071 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1072 } else {
1073 assert(VA.isMemLoc());
1074 if (StackPtr.Val == 0)
1075 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1076 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1077 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1078 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1079 }
1080 }
1081
1082 if (!MemOpChains.empty())
1083 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1084 &MemOpChains[0], MemOpChains.size());
1085
1086 // Build a sequence of copy-to-reg nodes chained together with token chain
1087 // and flag operands which copy the outgoing args into registers.
1088 SDOperand InFlag;
1089 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1090 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1091 InFlag);
1092 InFlag = Chain.getValue(1);
1093 }
1094
1095 // If the callee is a GlobalAddress node (quite common, every direct call is)
1096 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1097 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1098 // We should use extra load for direct calls to dllimported functions in
1099 // non-JIT mode.
1100 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1101 getTargetMachine(), true))
1102 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1103 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1104 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1105
1106 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1107 // GOT pointer.
1108 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1109 Subtarget->isPICStyleGOT()) {
1110 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1111 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1112 InFlag);
1113 InFlag = Chain.getValue(1);
1114 }
1115
1116 // Returns a chain & a flag for retval copy to use.
1117 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1118 SmallVector<SDOperand, 8> Ops;
1119 Ops.push_back(Chain);
1120 Ops.push_back(Callee);
1121
1122 // Add argument registers to the end of the list so that they are known live
1123 // into the call.
1124 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1125 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1126 RegsToPass[i].second.getValueType()));
1127
1128 // Add an implicit use GOT pointer in EBX.
1129 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1130 Subtarget->isPICStyleGOT())
1131 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1132
1133 if (InFlag.Val)
1134 Ops.push_back(InFlag);
1135
1136 // FIXME: Do not generate X86ISD::TAILCALL for now.
1137 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1138 NodeTys, &Ops[0], Ops.size());
1139 InFlag = Chain.getValue(1);
1140
1141 // Returns a flag for retval copy to use.
1142 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1143 Ops.clear();
1144 Ops.push_back(Chain);
1145 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1146 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1147 Ops.push_back(InFlag);
1148 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1149 InFlag = Chain.getValue(1);
1150
1151 // Handle result values, copying them out of physregs into vregs that we
1152 // return.
1153 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1154}
1155
1156
1157//===----------------------------------------------------------------------===//
1158// X86-64 C Calling Convention implementation
1159//===----------------------------------------------------------------------===//
1160
1161SDOperand
1162X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1163 MachineFunction &MF = DAG.getMachineFunction();
1164 MachineFrameInfo *MFI = MF.getFrameInfo();
1165 SDOperand Root = Op.getOperand(0);
1166 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1167
1168 static const unsigned GPR64ArgRegs[] = {
1169 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1170 };
1171 static const unsigned XMMArgRegs[] = {
1172 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1173 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1174 };
1175
1176
1177 // Assign locations to all of the incoming arguments.
1178 SmallVector<CCValAssign, 16> ArgLocs;
1179 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1180 getTargetMachine(), ArgLocs);
1181 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
1182
1183 SmallVector<SDOperand, 8> ArgValues;
1184 unsigned LastVal = ~0U;
1185 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1186 CCValAssign &VA = ArgLocs[i];
1187 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1188 // places.
1189 assert(VA.getValNo() != LastVal &&
1190 "Don't support value assigned to multiple locs yet");
1191 LastVal = VA.getValNo();
1192
1193 if (VA.isRegLoc()) {
1194 MVT::ValueType RegVT = VA.getLocVT();
1195 TargetRegisterClass *RC;
1196 if (RegVT == MVT::i32)
1197 RC = X86::GR32RegisterClass;
1198 else if (RegVT == MVT::i64)
1199 RC = X86::GR64RegisterClass;
1200 else if (RegVT == MVT::f32)
1201 RC = X86::FR32RegisterClass;
1202 else if (RegVT == MVT::f64)
1203 RC = X86::FR64RegisterClass;
1204 else {
1205 assert(MVT::isVector(RegVT));
1206 if (MVT::getSizeInBits(RegVT) == 64) {
1207 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1208 RegVT = MVT::i64;
1209 } else
1210 RC = X86::VR128RegisterClass;
1211 }
1212
1213 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1214 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1215
1216 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1217 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1218 // right size.
1219 if (VA.getLocInfo() == CCValAssign::SExt)
1220 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1221 DAG.getValueType(VA.getValVT()));
1222 else if (VA.getLocInfo() == CCValAssign::ZExt)
1223 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1224 DAG.getValueType(VA.getValVT()));
1225
1226 if (VA.getLocInfo() != CCValAssign::Full)
1227 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1228
1229 // Handle MMX values passed in GPRs.
1230 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1231 MVT::getSizeInBits(RegVT) == 64)
1232 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1233
1234 ArgValues.push_back(ArgValue);
1235 } else {
1236 assert(VA.isMemLoc());
1237
1238 // Create the nodes corresponding to a load from this parameter slot.
1239 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1240 VA.getLocMemOffset());
1241 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1242 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1243 }
1244 }
1245
1246 unsigned StackSize = CCInfo.getNextStackOffset();
1247
1248 // If the function takes variable number of arguments, make a frame index for
1249 // the start of the first vararg value... for expansion of llvm.va_start.
1250 if (isVarArg) {
1251 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1252 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1253
1254 // For X86-64, if there are vararg parameters that are passed via
1255 // registers, then we must store them to their spots on the stack so they
1256 // may be loaded by deferencing the result of va_next.
1257 VarArgsGPOffset = NumIntRegs * 8;
1258 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1259 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1260 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1261
1262 // Store the integer parameter registers.
1263 SmallVector<SDOperand, 8> MemOps;
1264 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1265 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1266 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1267 for (; NumIntRegs != 6; ++NumIntRegs) {
1268 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1269 X86::GR64RegisterClass);
1270 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1271 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1272 MemOps.push_back(Store);
1273 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1274 DAG.getConstant(8, getPointerTy()));
1275 }
1276
1277 // Now store the XMM (fp + vector) parameter registers.
1278 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1279 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1280 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1281 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1282 X86::VR128RegisterClass);
1283 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1284 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1285 MemOps.push_back(Store);
1286 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1287 DAG.getConstant(16, getPointerTy()));
1288 }
1289 if (!MemOps.empty())
1290 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1291 &MemOps[0], MemOps.size());
1292 }
1293
1294 ArgValues.push_back(Root);
1295
1296 ReturnAddrIndex = 0; // No return address slot generated yet.
1297 BytesToPopOnReturn = 0; // Callee pops nothing.
1298 BytesCallerReserves = StackSize;
1299
1300 // Return the new list of results.
1301 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1302 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1303}
1304
1305SDOperand
1306X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1307 unsigned CC) {
1308 SDOperand Chain = Op.getOperand(0);
1309 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1310 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1311 SDOperand Callee = Op.getOperand(4);
1312
1313 // Analyze operands of the call, assigning locations to each operand.
1314 SmallVector<CCValAssign, 16> ArgLocs;
1315 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1316 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
1317
1318 // Get a count of how many bytes are to be pushed on the stack.
1319 unsigned NumBytes = CCInfo.getNextStackOffset();
1320 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1321
1322 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1323 SmallVector<SDOperand, 8> MemOpChains;
1324
1325 SDOperand StackPtr;
1326
1327 // Walk the register/memloc assignments, inserting copies/loads.
1328 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1329 CCValAssign &VA = ArgLocs[i];
1330 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1331
1332 // Promote the value if needed.
1333 switch (VA.getLocInfo()) {
1334 default: assert(0 && "Unknown loc info!");
1335 case CCValAssign::Full: break;
1336 case CCValAssign::SExt:
1337 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1338 break;
1339 case CCValAssign::ZExt:
1340 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1341 break;
1342 case CCValAssign::AExt:
1343 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1344 break;
1345 }
1346
1347 if (VA.isRegLoc()) {
1348 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1349 } else {
1350 assert(VA.isMemLoc());
1351 if (StackPtr.Val == 0)
1352 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1353 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1354 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1355 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1356 }
1357 }
1358
1359 if (!MemOpChains.empty())
1360 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1361 &MemOpChains[0], MemOpChains.size());
1362
1363 // Build a sequence of copy-to-reg nodes chained together with token chain
1364 // and flag operands which copy the outgoing args into registers.
1365 SDOperand InFlag;
1366 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1367 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1368 InFlag);
1369 InFlag = Chain.getValue(1);
1370 }
1371
1372 if (isVarArg) {
1373 // From AMD64 ABI document:
1374 // For calls that may call functions that use varargs or stdargs
1375 // (prototype-less calls or calls to functions containing ellipsis (...) in
1376 // the declaration) %al is used as hidden argument to specify the number
1377 // of SSE registers used. The contents of %al do not need to match exactly
1378 // the number of registers, but must be an ubound on the number of SSE
1379 // registers used and is in the range 0 - 8 inclusive.
1380
1381 // Count the number of XMM registers allocated.
1382 static const unsigned XMMArgRegs[] = {
1383 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1384 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1385 };
1386 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1387
1388 Chain = DAG.getCopyToReg(Chain, X86::AL,
1389 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1390 InFlag = Chain.getValue(1);
1391 }
1392
1393 // If the callee is a GlobalAddress node (quite common, every direct call is)
1394 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1395 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1396 // We should use extra load for direct calls to dllimported functions in
1397 // non-JIT mode.
1398 if (getTargetMachine().getCodeModel() != CodeModel::Large
1399 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1400 getTargetMachine(), true))
1401 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1402 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1403 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1404 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1405
1406 // Returns a chain & a flag for retval copy to use.
1407 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1408 SmallVector<SDOperand, 8> Ops;
1409 Ops.push_back(Chain);
1410 Ops.push_back(Callee);
1411
1412 // Add argument registers to the end of the list so that they are known live
1413 // into the call.
1414 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1415 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1416 RegsToPass[i].second.getValueType()));
1417
1418 if (InFlag.Val)
1419 Ops.push_back(InFlag);
1420
1421 // FIXME: Do not generate X86ISD::TAILCALL for now.
1422 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1423 NodeTys, &Ops[0], Ops.size());
1424 InFlag = Chain.getValue(1);
1425
1426 // Returns a flag for retval copy to use.
1427 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1428 Ops.clear();
1429 Ops.push_back(Chain);
1430 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1431 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1432 Ops.push_back(InFlag);
1433 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1434 InFlag = Chain.getValue(1);
1435
1436 // Handle result values, copying them out of physregs into vregs that we
1437 // return.
1438 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1439}
1440
1441
1442//===----------------------------------------------------------------------===//
1443// Other Lowering Hooks
1444//===----------------------------------------------------------------------===//
1445
1446
1447SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1448 if (ReturnAddrIndex == 0) {
1449 // Set up a frame object for the return address.
1450 MachineFunction &MF = DAG.getMachineFunction();
1451 if (Subtarget->is64Bit())
1452 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1453 else
1454 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1455 }
1456
1457 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1458}
1459
1460
1461
1462/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1463/// specific condition code. It returns a false if it cannot do a direct
1464/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1465/// needed.
1466static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1467 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1468 SelectionDAG &DAG) {
1469 X86CC = X86::COND_INVALID;
1470 if (!isFP) {
1471 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1472 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1473 // X > -1 -> X == 0, jump !sign.
1474 RHS = DAG.getConstant(0, RHS.getValueType());
1475 X86CC = X86::COND_NS;
1476 return true;
1477 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1478 // X < 0 -> X == 0, jump on sign.
1479 X86CC = X86::COND_S;
1480 return true;
1481 }
1482 }
1483
1484 switch (SetCCOpcode) {
1485 default: break;
1486 case ISD::SETEQ: X86CC = X86::COND_E; break;
1487 case ISD::SETGT: X86CC = X86::COND_G; break;
1488 case ISD::SETGE: X86CC = X86::COND_GE; break;
1489 case ISD::SETLT: X86CC = X86::COND_L; break;
1490 case ISD::SETLE: X86CC = X86::COND_LE; break;
1491 case ISD::SETNE: X86CC = X86::COND_NE; break;
1492 case ISD::SETULT: X86CC = X86::COND_B; break;
1493 case ISD::SETUGT: X86CC = X86::COND_A; break;
1494 case ISD::SETULE: X86CC = X86::COND_BE; break;
1495 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1496 }
1497 } else {
1498 // On a floating point condition, the flags are set as follows:
1499 // ZF PF CF op
1500 // 0 | 0 | 0 | X > Y
1501 // 0 | 0 | 1 | X < Y
1502 // 1 | 0 | 0 | X == Y
1503 // 1 | 1 | 1 | unordered
1504 bool Flip = false;
1505 switch (SetCCOpcode) {
1506 default: break;
1507 case ISD::SETUEQ:
1508 case ISD::SETEQ: X86CC = X86::COND_E; break;
1509 case ISD::SETOLT: Flip = true; // Fallthrough
1510 case ISD::SETOGT:
1511 case ISD::SETGT: X86CC = X86::COND_A; break;
1512 case ISD::SETOLE: Flip = true; // Fallthrough
1513 case ISD::SETOGE:
1514 case ISD::SETGE: X86CC = X86::COND_AE; break;
1515 case ISD::SETUGT: Flip = true; // Fallthrough
1516 case ISD::SETULT:
1517 case ISD::SETLT: X86CC = X86::COND_B; break;
1518 case ISD::SETUGE: Flip = true; // Fallthrough
1519 case ISD::SETULE:
1520 case ISD::SETLE: X86CC = X86::COND_BE; break;
1521 case ISD::SETONE:
1522 case ISD::SETNE: X86CC = X86::COND_NE; break;
1523 case ISD::SETUO: X86CC = X86::COND_P; break;
1524 case ISD::SETO: X86CC = X86::COND_NP; break;
1525 }
1526 if (Flip)
1527 std::swap(LHS, RHS);
1528 }
1529
1530 return X86CC != X86::COND_INVALID;
1531}
1532
1533/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1534/// code. Current x86 isa includes the following FP cmov instructions:
1535/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1536static bool hasFPCMov(unsigned X86CC) {
1537 switch (X86CC) {
1538 default:
1539 return false;
1540 case X86::COND_B:
1541 case X86::COND_BE:
1542 case X86::COND_E:
1543 case X86::COND_P:
1544 case X86::COND_A:
1545 case X86::COND_AE:
1546 case X86::COND_NE:
1547 case X86::COND_NP:
1548 return true;
1549 }
1550}
1551
1552/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1553/// true if Op is undef or if its value falls within the specified range (L, H].
1554static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1555 if (Op.getOpcode() == ISD::UNDEF)
1556 return true;
1557
1558 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1559 return (Val >= Low && Val < Hi);
1560}
1561
1562/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1563/// true if Op is undef or if its value equal to the specified value.
1564static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1565 if (Op.getOpcode() == ISD::UNDEF)
1566 return true;
1567 return cast<ConstantSDNode>(Op)->getValue() == Val;
1568}
1569
1570/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1571/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1572bool X86::isPSHUFDMask(SDNode *N) {
1573 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1574
1575 if (N->getNumOperands() != 4)
1576 return false;
1577
1578 // Check if the value doesn't reference the second vector.
1579 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1580 SDOperand Arg = N->getOperand(i);
1581 if (Arg.getOpcode() == ISD::UNDEF) continue;
1582 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1583 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1584 return false;
1585 }
1586
1587 return true;
1588}
1589
1590/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1591/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1592bool X86::isPSHUFHWMask(SDNode *N) {
1593 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1594
1595 if (N->getNumOperands() != 8)
1596 return false;
1597
1598 // Lower quadword copied in order.
1599 for (unsigned i = 0; i != 4; ++i) {
1600 SDOperand Arg = N->getOperand(i);
1601 if (Arg.getOpcode() == ISD::UNDEF) continue;
1602 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1603 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1604 return false;
1605 }
1606
1607 // Upper quadword shuffled.
1608 for (unsigned i = 4; i != 8; ++i) {
1609 SDOperand Arg = N->getOperand(i);
1610 if (Arg.getOpcode() == ISD::UNDEF) continue;
1611 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1612 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1613 if (Val < 4 || Val > 7)
1614 return false;
1615 }
1616
1617 return true;
1618}
1619
1620/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1621/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1622bool X86::isPSHUFLWMask(SDNode *N) {
1623 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1624
1625 if (N->getNumOperands() != 8)
1626 return false;
1627
1628 // Upper quadword copied in order.
1629 for (unsigned i = 4; i != 8; ++i)
1630 if (!isUndefOrEqual(N->getOperand(i), i))
1631 return false;
1632
1633 // Lower quadword shuffled.
1634 for (unsigned i = 0; i != 4; ++i)
1635 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1636 return false;
1637
1638 return true;
1639}
1640
1641/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1642/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1643static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1644 if (NumElems != 2 && NumElems != 4) return false;
1645
1646 unsigned Half = NumElems / 2;
1647 for (unsigned i = 0; i < Half; ++i)
1648 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1649 return false;
1650 for (unsigned i = Half; i < NumElems; ++i)
1651 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1652 return false;
1653
1654 return true;
1655}
1656
1657bool X86::isSHUFPMask(SDNode *N) {
1658 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1659 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1660}
1661
1662/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
1663/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1664/// half elements to come from vector 1 (which would equal the dest.) and
1665/// the upper half to come from vector 2.
1666static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1667 if (NumOps != 2 && NumOps != 4) return false;
1668
1669 unsigned Half = NumOps / 2;
1670 for (unsigned i = 0; i < Half; ++i)
1671 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1672 return false;
1673 for (unsigned i = Half; i < NumOps; ++i)
1674 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1675 return false;
1676 return true;
1677}
1678
1679static bool isCommutedSHUFP(SDNode *N) {
1680 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1681 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1682}
1683
1684/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1685/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1686bool X86::isMOVHLPSMask(SDNode *N) {
1687 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1688
1689 if (N->getNumOperands() != 4)
1690 return false;
1691
1692 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1693 return isUndefOrEqual(N->getOperand(0), 6) &&
1694 isUndefOrEqual(N->getOperand(1), 7) &&
1695 isUndefOrEqual(N->getOperand(2), 2) &&
1696 isUndefOrEqual(N->getOperand(3), 3);
1697}
1698
1699/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1700/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1701/// <2, 3, 2, 3>
1702bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1703 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1704
1705 if (N->getNumOperands() != 4)
1706 return false;
1707
1708 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1709 return isUndefOrEqual(N->getOperand(0), 2) &&
1710 isUndefOrEqual(N->getOperand(1), 3) &&
1711 isUndefOrEqual(N->getOperand(2), 2) &&
1712 isUndefOrEqual(N->getOperand(3), 3);
1713}
1714
1715/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1716/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1717bool X86::isMOVLPMask(SDNode *N) {
1718 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1719
1720 unsigned NumElems = N->getNumOperands();
1721 if (NumElems != 2 && NumElems != 4)
1722 return false;
1723
1724 for (unsigned i = 0; i < NumElems/2; ++i)
1725 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1726 return false;
1727
1728 for (unsigned i = NumElems/2; i < NumElems; ++i)
1729 if (!isUndefOrEqual(N->getOperand(i), i))
1730 return false;
1731
1732 return true;
1733}
1734
1735/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1736/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1737/// and MOVLHPS.
1738bool X86::isMOVHPMask(SDNode *N) {
1739 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1740
1741 unsigned NumElems = N->getNumOperands();
1742 if (NumElems != 2 && NumElems != 4)
1743 return false;
1744
1745 for (unsigned i = 0; i < NumElems/2; ++i)
1746 if (!isUndefOrEqual(N->getOperand(i), i))
1747 return false;
1748
1749 for (unsigned i = 0; i < NumElems/2; ++i) {
1750 SDOperand Arg = N->getOperand(i + NumElems/2);
1751 if (!isUndefOrEqual(Arg, i + NumElems))
1752 return false;
1753 }
1754
1755 return true;
1756}
1757
1758/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1759/// specifies a shuffle of elements that is suitable for input to UNPCKL.
1760bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1761 bool V2IsSplat = false) {
1762 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1763 return false;
1764
1765 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1766 SDOperand BitI = Elts[i];
1767 SDOperand BitI1 = Elts[i+1];
1768 if (!isUndefOrEqual(BitI, j))
1769 return false;
1770 if (V2IsSplat) {
1771 if (isUndefOrEqual(BitI1, NumElts))
1772 return false;
1773 } else {
1774 if (!isUndefOrEqual(BitI1, j + NumElts))
1775 return false;
1776 }
1777 }
1778
1779 return true;
1780}
1781
1782bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1783 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1784 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1785}
1786
1787/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1788/// specifies a shuffle of elements that is suitable for input to UNPCKH.
1789bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1790 bool V2IsSplat = false) {
1791 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1792 return false;
1793
1794 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1795 SDOperand BitI = Elts[i];
1796 SDOperand BitI1 = Elts[i+1];
1797 if (!isUndefOrEqual(BitI, j + NumElts/2))
1798 return false;
1799 if (V2IsSplat) {
1800 if (isUndefOrEqual(BitI1, NumElts))
1801 return false;
1802 } else {
1803 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
1804 return false;
1805 }
1806 }
1807
1808 return true;
1809}
1810
1811bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1812 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1813 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
1814}
1815
1816/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1817/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1818/// <0, 0, 1, 1>
1819bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1820 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1821
1822 unsigned NumElems = N->getNumOperands();
1823 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1824 return false;
1825
1826 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1827 SDOperand BitI = N->getOperand(i);
1828 SDOperand BitI1 = N->getOperand(i+1);
1829
1830 if (!isUndefOrEqual(BitI, j))
1831 return false;
1832 if (!isUndefOrEqual(BitI1, j))
1833 return false;
1834 }
1835
1836 return true;
1837}
1838
1839/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1840/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1841/// <2, 2, 3, 3>
1842bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1843 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1844
1845 unsigned NumElems = N->getNumOperands();
1846 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1847 return false;
1848
1849 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1850 SDOperand BitI = N->getOperand(i);
1851 SDOperand BitI1 = N->getOperand(i + 1);
1852
1853 if (!isUndefOrEqual(BitI, j))
1854 return false;
1855 if (!isUndefOrEqual(BitI1, j))
1856 return false;
1857 }
1858
1859 return true;
1860}
1861
1862/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1863/// specifies a shuffle of elements that is suitable for input to MOVSS,
1864/// MOVSD, and MOVD, i.e. setting the lowest element.
1865static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1866 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1867 return false;
1868
1869 if (!isUndefOrEqual(Elts[0], NumElts))
1870 return false;
1871
1872 for (unsigned i = 1; i < NumElts; ++i) {
1873 if (!isUndefOrEqual(Elts[i], i))
1874 return false;
1875 }
1876
1877 return true;
1878}
1879
1880bool X86::isMOVLMask(SDNode *N) {
1881 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1882 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
1883}
1884
1885/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1886/// of what x86 movss want. X86 movs requires the lowest element to be lowest
1887/// element of vector 2 and the other elements to come from vector 1 in order.
1888static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1889 bool V2IsSplat = false,
1890 bool V2IsUndef = false) {
1891 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
1892 return false;
1893
1894 if (!isUndefOrEqual(Ops[0], 0))
1895 return false;
1896
1897 for (unsigned i = 1; i < NumOps; ++i) {
1898 SDOperand Arg = Ops[i];
1899 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1900 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1901 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
1902 return false;
1903 }
1904
1905 return true;
1906}
1907
1908static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1909 bool V2IsUndef = false) {
1910 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1911 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1912 V2IsSplat, V2IsUndef);
1913}
1914
1915/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1916/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1917bool X86::isMOVSHDUPMask(SDNode *N) {
1918 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1919
1920 if (N->getNumOperands() != 4)
1921 return false;
1922
1923 // Expect 1, 1, 3, 3
1924 for (unsigned i = 0; i < 2; ++i) {
1925 SDOperand Arg = N->getOperand(i);
1926 if (Arg.getOpcode() == ISD::UNDEF) continue;
1927 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1928 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1929 if (Val != 1) return false;
1930 }
1931
1932 bool HasHi = false;
1933 for (unsigned i = 2; i < 4; ++i) {
1934 SDOperand Arg = N->getOperand(i);
1935 if (Arg.getOpcode() == ISD::UNDEF) continue;
1936 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1937 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1938 if (Val != 3) return false;
1939 HasHi = true;
1940 }
1941
1942 // Don't use movshdup if it can be done with a shufps.
1943 return HasHi;
1944}
1945
1946/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1947/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1948bool X86::isMOVSLDUPMask(SDNode *N) {
1949 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1950
1951 if (N->getNumOperands() != 4)
1952 return false;
1953
1954 // Expect 0, 0, 2, 2
1955 for (unsigned i = 0; i < 2; ++i) {
1956 SDOperand Arg = N->getOperand(i);
1957 if (Arg.getOpcode() == ISD::UNDEF) continue;
1958 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1959 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1960 if (Val != 0) return false;
1961 }
1962
1963 bool HasHi = false;
1964 for (unsigned i = 2; i < 4; ++i) {
1965 SDOperand Arg = N->getOperand(i);
1966 if (Arg.getOpcode() == ISD::UNDEF) continue;
1967 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1968 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1969 if (Val != 2) return false;
1970 HasHi = true;
1971 }
1972
1973 // Don't use movshdup if it can be done with a shufps.
1974 return HasHi;
1975}
1976
1977/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
1978/// specifies a identity operation on the LHS or RHS.
1979static bool isIdentityMask(SDNode *N, bool RHS = false) {
1980 unsigned NumElems = N->getNumOperands();
1981 for (unsigned i = 0; i < NumElems; ++i)
1982 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
1983 return false;
1984 return true;
1985}
1986
1987/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1988/// a splat of a single element.
1989static bool isSplatMask(SDNode *N) {
1990 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1991
1992 // This is a splat operation if each element of the permute is the same, and
1993 // if the value doesn't reference the second vector.
1994 unsigned NumElems = N->getNumOperands();
1995 SDOperand ElementBase;
1996 unsigned i = 0;
1997 for (; i != NumElems; ++i) {
1998 SDOperand Elt = N->getOperand(i);
1999 if (isa<ConstantSDNode>(Elt)) {
2000 ElementBase = Elt;
2001 break;
2002 }
2003 }
2004
2005 if (!ElementBase.Val)
2006 return false;
2007
2008 for (; i != NumElems; ++i) {
2009 SDOperand Arg = N->getOperand(i);
2010 if (Arg.getOpcode() == ISD::UNDEF) continue;
2011 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2012 if (Arg != ElementBase) return false;
2013 }
2014
2015 // Make sure it is a splat of the first vector operand.
2016 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2017}
2018
2019/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2020/// a splat of a single element and it's a 2 or 4 element mask.
2021bool X86::isSplatMask(SDNode *N) {
2022 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2023
2024 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2025 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2026 return false;
2027 return ::isSplatMask(N);
2028}
2029
2030/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2031/// specifies a splat of zero element.
2032bool X86::isSplatLoMask(SDNode *N) {
2033 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2034
2035 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2036 if (!isUndefOrEqual(N->getOperand(i), 0))
2037 return false;
2038 return true;
2039}
2040
2041/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2042/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2043/// instructions.
2044unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2045 unsigned NumOperands = N->getNumOperands();
2046 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2047 unsigned Mask = 0;
2048 for (unsigned i = 0; i < NumOperands; ++i) {
2049 unsigned Val = 0;
2050 SDOperand Arg = N->getOperand(NumOperands-i-1);
2051 if (Arg.getOpcode() != ISD::UNDEF)
2052 Val = cast<ConstantSDNode>(Arg)->getValue();
2053 if (Val >= NumOperands) Val -= NumOperands;
2054 Mask |= Val;
2055 if (i != NumOperands - 1)
2056 Mask <<= Shift;
2057 }
2058
2059 return Mask;
2060}
2061
2062/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2063/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2064/// instructions.
2065unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2066 unsigned Mask = 0;
2067 // 8 nodes, but we only care about the last 4.
2068 for (unsigned i = 7; i >= 4; --i) {
2069 unsigned Val = 0;
2070 SDOperand Arg = N->getOperand(i);
2071 if (Arg.getOpcode() != ISD::UNDEF)
2072 Val = cast<ConstantSDNode>(Arg)->getValue();
2073 Mask |= (Val - 4);
2074 if (i != 4)
2075 Mask <<= 2;
2076 }
2077
2078 return Mask;
2079}
2080
2081/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2082/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2083/// instructions.
2084unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2085 unsigned Mask = 0;
2086 // 8 nodes, but we only care about the first 4.
2087 for (int i = 3; i >= 0; --i) {
2088 unsigned Val = 0;
2089 SDOperand Arg = N->getOperand(i);
2090 if (Arg.getOpcode() != ISD::UNDEF)
2091 Val = cast<ConstantSDNode>(Arg)->getValue();
2092 Mask |= Val;
2093 if (i != 0)
2094 Mask <<= 2;
2095 }
2096
2097 return Mask;
2098}
2099
2100/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2101/// specifies a 8 element shuffle that can be broken into a pair of
2102/// PSHUFHW and PSHUFLW.
2103static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2104 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2105
2106 if (N->getNumOperands() != 8)
2107 return false;
2108
2109 // Lower quadword shuffled.
2110 for (unsigned i = 0; i != 4; ++i) {
2111 SDOperand Arg = N->getOperand(i);
2112 if (Arg.getOpcode() == ISD::UNDEF) continue;
2113 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2114 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2115 if (Val > 4)
2116 return false;
2117 }
2118
2119 // Upper quadword shuffled.
2120 for (unsigned i = 4; i != 8; ++i) {
2121 SDOperand Arg = N->getOperand(i);
2122 if (Arg.getOpcode() == ISD::UNDEF) continue;
2123 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2124 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2125 if (Val < 4 || Val > 7)
2126 return false;
2127 }
2128
2129 return true;
2130}
2131
2132/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2133/// values in ther permute mask.
2134static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2135 SDOperand &V2, SDOperand &Mask,
2136 SelectionDAG &DAG) {
2137 MVT::ValueType VT = Op.getValueType();
2138 MVT::ValueType MaskVT = Mask.getValueType();
2139 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2140 unsigned NumElems = Mask.getNumOperands();
2141 SmallVector<SDOperand, 8> MaskVec;
2142
2143 for (unsigned i = 0; i != NumElems; ++i) {
2144 SDOperand Arg = Mask.getOperand(i);
2145 if (Arg.getOpcode() == ISD::UNDEF) {
2146 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2147 continue;
2148 }
2149 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2150 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2151 if (Val < NumElems)
2152 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2153 else
2154 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2155 }
2156
2157 std::swap(V1, V2);
2158 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2159 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2160}
2161
2162/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2163/// match movhlps. The lower half elements should come from upper half of
2164/// V1 (and in order), and the upper half elements should come from the upper
2165/// half of V2 (and in order).
2166static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2167 unsigned NumElems = Mask->getNumOperands();
2168 if (NumElems != 4)
2169 return false;
2170 for (unsigned i = 0, e = 2; i != e; ++i)
2171 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2172 return false;
2173 for (unsigned i = 2; i != 4; ++i)
2174 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2175 return false;
2176 return true;
2177}
2178
2179/// isScalarLoadToVector - Returns true if the node is a scalar load that
2180/// is promoted to a vector.
2181static inline bool isScalarLoadToVector(SDNode *N) {
2182 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2183 N = N->getOperand(0).Val;
2184 return ISD::isNON_EXTLoad(N);
2185 }
2186 return false;
2187}
2188
2189/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2190/// match movlp{s|d}. The lower half elements should come from lower half of
2191/// V1 (and in order), and the upper half elements should come from the upper
2192/// half of V2 (and in order). And since V1 will become the source of the
2193/// MOVLP, it must be either a vector load or a scalar load to vector.
2194static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2195 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2196 return false;
2197 // Is V2 is a vector load, don't do this transformation. We will try to use
2198 // load folding shufps op.
2199 if (ISD::isNON_EXTLoad(V2))
2200 return false;
2201
2202 unsigned NumElems = Mask->getNumOperands();
2203 if (NumElems != 2 && NumElems != 4)
2204 return false;
2205 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2206 if (!isUndefOrEqual(Mask->getOperand(i), i))
2207 return false;
2208 for (unsigned i = NumElems/2; i != NumElems; ++i)
2209 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2210 return false;
2211 return true;
2212}
2213
2214/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2215/// all the same.
2216static bool isSplatVector(SDNode *N) {
2217 if (N->getOpcode() != ISD::BUILD_VECTOR)
2218 return false;
2219
2220 SDOperand SplatValue = N->getOperand(0);
2221 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2222 if (N->getOperand(i) != SplatValue)
2223 return false;
2224 return true;
2225}
2226
2227/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2228/// to an undef.
2229static bool isUndefShuffle(SDNode *N) {
2230 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2231 return false;
2232
2233 SDOperand V1 = N->getOperand(0);
2234 SDOperand V2 = N->getOperand(1);
2235 SDOperand Mask = N->getOperand(2);
2236 unsigned NumElems = Mask.getNumOperands();
2237 for (unsigned i = 0; i != NumElems; ++i) {
2238 SDOperand Arg = Mask.getOperand(i);
2239 if (Arg.getOpcode() != ISD::UNDEF) {
2240 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2241 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2242 return false;
2243 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2244 return false;
2245 }
2246 }
2247 return true;
2248}
2249
2250/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2251/// constant +0.0.
2252static inline bool isZeroNode(SDOperand Elt) {
2253 return ((isa<ConstantSDNode>(Elt) &&
2254 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2255 (isa<ConstantFPSDNode>(Elt) &&
2256 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2257}
2258
2259/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2260/// to an zero vector.
2261static bool isZeroShuffle(SDNode *N) {
2262 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2263 return false;
2264
2265 SDOperand V1 = N->getOperand(0);
2266 SDOperand V2 = N->getOperand(1);
2267 SDOperand Mask = N->getOperand(2);
2268 unsigned NumElems = Mask.getNumOperands();
2269 for (unsigned i = 0; i != NumElems; ++i) {
2270 SDOperand Arg = Mask.getOperand(i);
2271 if (Arg.getOpcode() != ISD::UNDEF) {
2272 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2273 if (Idx < NumElems) {
2274 unsigned Opc = V1.Val->getOpcode();
2275 if (Opc == ISD::UNDEF)
2276 continue;
2277 if (Opc != ISD::BUILD_VECTOR ||
2278 !isZeroNode(V1.Val->getOperand(Idx)))
2279 return false;
2280 } else if (Idx >= NumElems) {
2281 unsigned Opc = V2.Val->getOpcode();
2282 if (Opc == ISD::UNDEF)
2283 continue;
2284 if (Opc != ISD::BUILD_VECTOR ||
2285 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2286 return false;
2287 }
2288 }
2289 }
2290 return true;
2291}
2292
2293/// getZeroVector - Returns a vector of specified type with all zero elements.
2294///
2295static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2296 assert(MVT::isVector(VT) && "Expected a vector type");
2297 unsigned NumElems = MVT::getVectorNumElements(VT);
2298 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2299 bool isFP = MVT::isFloatingPoint(EVT);
2300 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2301 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2302 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2303}
2304
2305/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2306/// that point to V2 points to its first element.
2307static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2308 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2309
2310 bool Changed = false;
2311 SmallVector<SDOperand, 8> MaskVec;
2312 unsigned NumElems = Mask.getNumOperands();
2313 for (unsigned i = 0; i != NumElems; ++i) {
2314 SDOperand Arg = Mask.getOperand(i);
2315 if (Arg.getOpcode() != ISD::UNDEF) {
2316 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2317 if (Val > NumElems) {
2318 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2319 Changed = true;
2320 }
2321 }
2322 MaskVec.push_back(Arg);
2323 }
2324
2325 if (Changed)
2326 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2327 &MaskVec[0], MaskVec.size());
2328 return Mask;
2329}
2330
2331/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2332/// operation of specified width.
2333static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2334 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2335 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2336
2337 SmallVector<SDOperand, 8> MaskVec;
2338 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2339 for (unsigned i = 1; i != NumElems; ++i)
2340 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2341 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2342}
2343
2344/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2345/// of specified width.
2346static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2347 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2348 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2349 SmallVector<SDOperand, 8> MaskVec;
2350 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2351 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2352 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2353 }
2354 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2355}
2356
2357/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2358/// of specified width.
2359static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2360 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2361 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2362 unsigned Half = NumElems/2;
2363 SmallVector<SDOperand, 8> MaskVec;
2364 for (unsigned i = 0; i != Half; ++i) {
2365 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2366 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2367 }
2368 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2369}
2370
2371/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2372///
2373static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2374 SDOperand V1 = Op.getOperand(0);
2375 SDOperand Mask = Op.getOperand(2);
2376 MVT::ValueType VT = Op.getValueType();
2377 unsigned NumElems = Mask.getNumOperands();
2378 Mask = getUnpacklMask(NumElems, DAG);
2379 while (NumElems != 4) {
2380 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2381 NumElems >>= 1;
2382 }
2383 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2384
2385 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2386 Mask = getZeroVector(MaskVT, DAG);
2387 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2388 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2389 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2390}
2391
2392/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2393/// vector of zero or undef vector.
2394static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2395 unsigned NumElems, unsigned Idx,
2396 bool isZero, SelectionDAG &DAG) {
2397 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2398 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2399 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2400 SDOperand Zero = DAG.getConstant(0, EVT);
2401 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2402 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2403 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2404 &MaskVec[0], MaskVec.size());
2405 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2406}
2407
2408/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2409///
2410static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2411 unsigned NumNonZero, unsigned NumZero,
2412 SelectionDAG &DAG, TargetLowering &TLI) {
2413 if (NumNonZero > 8)
2414 return SDOperand();
2415
2416 SDOperand V(0, 0);
2417 bool First = true;
2418 for (unsigned i = 0; i < 16; ++i) {
2419 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2420 if (ThisIsNonZero && First) {
2421 if (NumZero)
2422 V = getZeroVector(MVT::v8i16, DAG);
2423 else
2424 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2425 First = false;
2426 }
2427
2428 if ((i & 1) != 0) {
2429 SDOperand ThisElt(0, 0), LastElt(0, 0);
2430 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2431 if (LastIsNonZero) {
2432 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2433 }
2434 if (ThisIsNonZero) {
2435 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2436 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2437 ThisElt, DAG.getConstant(8, MVT::i8));
2438 if (LastIsNonZero)
2439 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2440 } else
2441 ThisElt = LastElt;
2442
2443 if (ThisElt.Val)
2444 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2445 DAG.getConstant(i/2, TLI.getPointerTy()));
2446 }
2447 }
2448
2449 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2450}
2451
2452/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2453///
2454static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2455 unsigned NumNonZero, unsigned NumZero,
2456 SelectionDAG &DAG, TargetLowering &TLI) {
2457 if (NumNonZero > 4)
2458 return SDOperand();
2459
2460 SDOperand V(0, 0);
2461 bool First = true;
2462 for (unsigned i = 0; i < 8; ++i) {
2463 bool isNonZero = (NonZeros & (1 << i)) != 0;
2464 if (isNonZero) {
2465 if (First) {
2466 if (NumZero)
2467 V = getZeroVector(MVT::v8i16, DAG);
2468 else
2469 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2470 First = false;
2471 }
2472 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2473 DAG.getConstant(i, TLI.getPointerTy()));
2474 }
2475 }
2476
2477 return V;
2478}
2479
2480SDOperand
2481X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2482 // All zero's are handled with pxor.
2483 if (ISD::isBuildVectorAllZeros(Op.Val))
2484 return Op;
2485
2486 // All one's are handled with pcmpeqd.
2487 if (ISD::isBuildVectorAllOnes(Op.Val))
2488 return Op;
2489
2490 MVT::ValueType VT = Op.getValueType();
2491 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2492 unsigned EVTBits = MVT::getSizeInBits(EVT);
2493
2494 unsigned NumElems = Op.getNumOperands();
2495 unsigned NumZero = 0;
2496 unsigned NumNonZero = 0;
2497 unsigned NonZeros = 0;
Dan Gohman21463242007-07-24 22:55:08 +00002498 unsigned NumNonZeroImms = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002499 std::set<SDOperand> Values;
2500 for (unsigned i = 0; i < NumElems; ++i) {
2501 SDOperand Elt = Op.getOperand(i);
2502 if (Elt.getOpcode() != ISD::UNDEF) {
2503 Values.insert(Elt);
2504 if (isZeroNode(Elt))
2505 NumZero++;
2506 else {
2507 NonZeros |= (1 << i);
2508 NumNonZero++;
Dan Gohman21463242007-07-24 22:55:08 +00002509 if (Elt.getOpcode() == ISD::Constant ||
2510 Elt.getOpcode() == ISD::ConstantFP)
2511 NumNonZeroImms++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 }
2513 }
2514 }
2515
2516 if (NumNonZero == 0) {
2517 if (NumZero == 0)
2518 // All undef vector. Return an UNDEF.
2519 return DAG.getNode(ISD::UNDEF, VT);
2520 else
2521 // A mix of zero and undef. Return a zero vector.
2522 return getZeroVector(VT, DAG);
2523 }
2524
2525 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2526 if (Values.size() == 1)
2527 return SDOperand();
2528
2529 // Special case for single non-zero element.
2530 if (NumNonZero == 1) {
2531 unsigned Idx = CountTrailingZeros_32(NonZeros);
2532 SDOperand Item = Op.getOperand(Idx);
2533 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2534 if (Idx == 0)
2535 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2536 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2537 NumZero > 0, DAG);
2538
2539 if (EVTBits == 32) {
2540 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2541 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2542 DAG);
2543 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2544 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2545 SmallVector<SDOperand, 8> MaskVec;
2546 for (unsigned i = 0; i < NumElems; i++)
2547 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2548 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2549 &MaskVec[0], MaskVec.size());
2550 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2551 DAG.getNode(ISD::UNDEF, VT), Mask);
2552 }
2553 }
2554
Dan Gohman21463242007-07-24 22:55:08 +00002555 // A vector full of immediates; various special cases are already
2556 // handled, so this is best done with a single constant-pool load.
2557 if (NumNonZero == NumNonZeroImms)
2558 return SDOperand();
2559
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560 // Let legalizer expand 2-wide build_vectors.
2561 if (EVTBits == 64)
2562 return SDOperand();
2563
2564 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2565 if (EVTBits == 8 && NumElems == 16) {
2566 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2567 *this);
2568 if (V.Val) return V;
2569 }
2570
2571 if (EVTBits == 16 && NumElems == 8) {
2572 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2573 *this);
2574 if (V.Val) return V;
2575 }
2576
2577 // If element VT is == 32 bits, turn it into a number of shuffles.
2578 SmallVector<SDOperand, 8> V;
2579 V.resize(NumElems);
2580 if (NumElems == 4 && NumZero > 0) {
2581 for (unsigned i = 0; i < 4; ++i) {
2582 bool isZero = !(NonZeros & (1 << i));
2583 if (isZero)
2584 V[i] = getZeroVector(VT, DAG);
2585 else
2586 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2587 }
2588
2589 for (unsigned i = 0; i < 2; ++i) {
2590 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2591 default: break;
2592 case 0:
2593 V[i] = V[i*2]; // Must be a zero vector.
2594 break;
2595 case 1:
2596 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2597 getMOVLMask(NumElems, DAG));
2598 break;
2599 case 2:
2600 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2601 getMOVLMask(NumElems, DAG));
2602 break;
2603 case 3:
2604 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2605 getUnpacklMask(NumElems, DAG));
2606 break;
2607 }
2608 }
2609
2610 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2611 // clears the upper bits.
2612 // FIXME: we can do the same for v4f32 case when we know both parts of
2613 // the lower half come from scalar_to_vector (loadf32). We should do
2614 // that in post legalizer dag combiner with target specific hooks.
2615 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2616 return V[0];
2617 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2618 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2619 SmallVector<SDOperand, 8> MaskVec;
2620 bool Reverse = (NonZeros & 0x3) == 2;
2621 for (unsigned i = 0; i < 2; ++i)
2622 if (Reverse)
2623 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2624 else
2625 MaskVec.push_back(DAG.getConstant(i, EVT));
2626 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2627 for (unsigned i = 0; i < 2; ++i)
2628 if (Reverse)
2629 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2630 else
2631 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2632 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2633 &MaskVec[0], MaskVec.size());
2634 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2635 }
2636
2637 if (Values.size() > 2) {
2638 // Expand into a number of unpckl*.
2639 // e.g. for v4f32
2640 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2641 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2642 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2643 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2644 for (unsigned i = 0; i < NumElems; ++i)
2645 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2646 NumElems >>= 1;
2647 while (NumElems != 0) {
2648 for (unsigned i = 0; i < NumElems; ++i)
2649 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2650 UnpckMask);
2651 NumElems >>= 1;
2652 }
2653 return V[0];
2654 }
2655
2656 return SDOperand();
2657}
2658
2659SDOperand
2660X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2661 SDOperand V1 = Op.getOperand(0);
2662 SDOperand V2 = Op.getOperand(1);
2663 SDOperand PermMask = Op.getOperand(2);
2664 MVT::ValueType VT = Op.getValueType();
2665 unsigned NumElems = PermMask.getNumOperands();
2666 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2667 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2668 bool V1IsSplat = false;
2669 bool V2IsSplat = false;
2670
2671 if (isUndefShuffle(Op.Val))
2672 return DAG.getNode(ISD::UNDEF, VT);
2673
2674 if (isZeroShuffle(Op.Val))
2675 return getZeroVector(VT, DAG);
2676
2677 if (isIdentityMask(PermMask.Val))
2678 return V1;
2679 else if (isIdentityMask(PermMask.Val, true))
2680 return V2;
2681
2682 if (isSplatMask(PermMask.Val)) {
2683 if (NumElems <= 4) return Op;
2684 // Promote it to a v4i32 splat.
2685 return PromoteSplat(Op, DAG);
2686 }
2687
2688 if (X86::isMOVLMask(PermMask.Val))
2689 return (V1IsUndef) ? V2 : Op;
2690
2691 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2692 X86::isMOVSLDUPMask(PermMask.Val) ||
2693 X86::isMOVHLPSMask(PermMask.Val) ||
2694 X86::isMOVHPMask(PermMask.Val) ||
2695 X86::isMOVLPMask(PermMask.Val))
2696 return Op;
2697
2698 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2699 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
2700 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2701
2702 bool Commuted = false;
2703 V1IsSplat = isSplatVector(V1.Val);
2704 V2IsSplat = isSplatVector(V2.Val);
2705 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
2706 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2707 std::swap(V1IsSplat, V2IsSplat);
2708 std::swap(V1IsUndef, V2IsUndef);
2709 Commuted = true;
2710 }
2711
2712 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2713 if (V2IsUndef) return V1;
2714 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2715 if (V2IsSplat) {
2716 // V2 is a splat, so the mask may be malformed. That is, it may point
2717 // to any V2 element. The instruction selectior won't like this. Get
2718 // a corrected mask and commute to form a proper MOVS{S|D}.
2719 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2720 if (NewMask.Val != PermMask.Val)
2721 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2722 }
2723 return Op;
2724 }
2725
2726 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2727 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2728 X86::isUNPCKLMask(PermMask.Val) ||
2729 X86::isUNPCKHMask(PermMask.Val))
2730 return Op;
2731
2732 if (V2IsSplat) {
2733 // Normalize mask so all entries that point to V2 points to its first
2734 // element then try to match unpck{h|l} again. If match, return a
2735 // new vector_shuffle with the corrected mask.
2736 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2737 if (NewMask.Val != PermMask.Val) {
2738 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2739 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2740 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2741 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2742 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2743 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2744 }
2745 }
2746 }
2747
2748 // Normalize the node to match x86 shuffle ops if needed
2749 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2750 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2751
2752 if (Commuted) {
2753 // Commute is back and try unpck* again.
2754 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2755 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2756 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
2757 X86::isUNPCKLMask(PermMask.Val) ||
2758 X86::isUNPCKHMask(PermMask.Val))
2759 return Op;
2760 }
2761
2762 // If VT is integer, try PSHUF* first, then SHUFP*.
2763 if (MVT::isInteger(VT)) {
2764 if (X86::isPSHUFDMask(PermMask.Val) ||
2765 X86::isPSHUFHWMask(PermMask.Val) ||
2766 X86::isPSHUFLWMask(PermMask.Val)) {
2767 if (V2.getOpcode() != ISD::UNDEF)
2768 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2769 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2770 return Op;
2771 }
2772
2773 if (X86::isSHUFPMask(PermMask.Val) &&
2774 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
2775 return Op;
2776
2777 // Handle v8i16 shuffle high / low shuffle node pair.
2778 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2779 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2780 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2781 SmallVector<SDOperand, 8> MaskVec;
2782 for (unsigned i = 0; i != 4; ++i)
2783 MaskVec.push_back(PermMask.getOperand(i));
2784 for (unsigned i = 4; i != 8; ++i)
2785 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2786 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2787 &MaskVec[0], MaskVec.size());
2788 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2789 MaskVec.clear();
2790 for (unsigned i = 0; i != 4; ++i)
2791 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2792 for (unsigned i = 4; i != 8; ++i)
2793 MaskVec.push_back(PermMask.getOperand(i));
2794 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
2795 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2796 }
2797 } else {
2798 // Floating point cases in the other order.
2799 if (X86::isSHUFPMask(PermMask.Val))
2800 return Op;
2801 if (X86::isPSHUFDMask(PermMask.Val) ||
2802 X86::isPSHUFHWMask(PermMask.Val) ||
2803 X86::isPSHUFLWMask(PermMask.Val)) {
2804 if (V2.getOpcode() != ISD::UNDEF)
2805 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2806 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2807 return Op;
2808 }
2809 }
2810
2811 if (NumElems == 4 &&
2812 // Don't do this for MMX.
2813 MVT::getSizeInBits(VT) != 64) {
2814 MVT::ValueType MaskVT = PermMask.getValueType();
2815 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2816 SmallVector<std::pair<int, int>, 8> Locs;
2817 Locs.reserve(NumElems);
2818 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2819 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2820 unsigned NumHi = 0;
2821 unsigned NumLo = 0;
2822 // If no more than two elements come from either vector. This can be
2823 // implemented with two shuffles. First shuffle gather the elements.
2824 // The second shuffle, which takes the first shuffle as both of its
2825 // vector operands, put the elements into the right order.
2826 for (unsigned i = 0; i != NumElems; ++i) {
2827 SDOperand Elt = PermMask.getOperand(i);
2828 if (Elt.getOpcode() == ISD::UNDEF) {
2829 Locs[i] = std::make_pair(-1, -1);
2830 } else {
2831 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2832 if (Val < NumElems) {
2833 Locs[i] = std::make_pair(0, NumLo);
2834 Mask1[NumLo] = Elt;
2835 NumLo++;
2836 } else {
2837 Locs[i] = std::make_pair(1, NumHi);
2838 if (2+NumHi < NumElems)
2839 Mask1[2+NumHi] = Elt;
2840 NumHi++;
2841 }
2842 }
2843 }
2844 if (NumLo <= 2 && NumHi <= 2) {
2845 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2846 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2847 &Mask1[0], Mask1.size()));
2848 for (unsigned i = 0; i != NumElems; ++i) {
2849 if (Locs[i].first == -1)
2850 continue;
2851 else {
2852 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2853 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2854 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2855 }
2856 }
2857
2858 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2859 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2860 &Mask2[0], Mask2.size()));
2861 }
2862
2863 // Break it into (shuffle shuffle_hi, shuffle_lo).
2864 Locs.clear();
2865 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2866 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2867 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
2868 unsigned MaskIdx = 0;
2869 unsigned LoIdx = 0;
2870 unsigned HiIdx = NumElems/2;
2871 for (unsigned i = 0; i != NumElems; ++i) {
2872 if (i == NumElems/2) {
2873 MaskPtr = &HiMask;
2874 MaskIdx = 1;
2875 LoIdx = 0;
2876 HiIdx = NumElems/2;
2877 }
2878 SDOperand Elt = PermMask.getOperand(i);
2879 if (Elt.getOpcode() == ISD::UNDEF) {
2880 Locs[i] = std::make_pair(-1, -1);
2881 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2882 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2883 (*MaskPtr)[LoIdx] = Elt;
2884 LoIdx++;
2885 } else {
2886 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2887 (*MaskPtr)[HiIdx] = Elt;
2888 HiIdx++;
2889 }
2890 }
2891
2892 SDOperand LoShuffle =
2893 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2894 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2895 &LoMask[0], LoMask.size()));
2896 SDOperand HiShuffle =
2897 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2898 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2899 &HiMask[0], HiMask.size()));
2900 SmallVector<SDOperand, 8> MaskOps;
2901 for (unsigned i = 0; i != NumElems; ++i) {
2902 if (Locs[i].first == -1) {
2903 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2904 } else {
2905 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2906 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2907 }
2908 }
2909 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2910 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2911 &MaskOps[0], MaskOps.size()));
2912 }
2913
2914 return SDOperand();
2915}
2916
2917SDOperand
2918X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2919 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2920 return SDOperand();
2921
2922 MVT::ValueType VT = Op.getValueType();
2923 // TODO: handle v16i8.
2924 if (MVT::getSizeInBits(VT) == 16) {
2925 // Transform it so it match pextrw which produces a 32-bit result.
2926 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2927 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2928 Op.getOperand(0), Op.getOperand(1));
2929 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2930 DAG.getValueType(VT));
2931 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2932 } else if (MVT::getSizeInBits(VT) == 32) {
2933 SDOperand Vec = Op.getOperand(0);
2934 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2935 if (Idx == 0)
2936 return Op;
2937 // SHUFPS the element to the lowest double word, then movss.
2938 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2939 SmallVector<SDOperand, 8> IdxVec;
2940 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
2941 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2942 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2943 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2944 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2945 &IdxVec[0], IdxVec.size());
2946 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2947 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2948 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2949 DAG.getConstant(0, getPointerTy()));
2950 } else if (MVT::getSizeInBits(VT) == 64) {
2951 SDOperand Vec = Op.getOperand(0);
2952 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2953 if (Idx == 0)
2954 return Op;
2955
2956 // UNPCKHPD the element to the lowest double word, then movsd.
2957 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2958 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2959 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2960 SmallVector<SDOperand, 8> IdxVec;
2961 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
2962 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2963 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2964 &IdxVec[0], IdxVec.size());
2965 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2966 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2967 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2968 DAG.getConstant(0, getPointerTy()));
2969 }
2970
2971 return SDOperand();
2972}
2973
2974SDOperand
2975X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2976 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
2977 // as its second argument.
2978 MVT::ValueType VT = Op.getValueType();
2979 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
2980 SDOperand N0 = Op.getOperand(0);
2981 SDOperand N1 = Op.getOperand(1);
2982 SDOperand N2 = Op.getOperand(2);
2983 if (MVT::getSizeInBits(BaseVT) == 16) {
2984 if (N1.getValueType() != MVT::i32)
2985 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2986 if (N2.getValueType() != MVT::i32)
2987 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
2988 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2989 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2990 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2991 if (Idx == 0) {
2992 // Use a movss.
2993 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2994 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2995 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2996 SmallVector<SDOperand, 8> MaskVec;
2997 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2998 for (unsigned i = 1; i <= 3; ++i)
2999 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3000 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3001 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3002 &MaskVec[0], MaskVec.size()));
3003 } else {
3004 // Use two pinsrw instructions to insert a 32 bit value.
3005 Idx <<= 1;
3006 if (MVT::isFloatingPoint(N1.getValueType())) {
3007 if (ISD::isNON_EXTLoad(N1.Val)) {
3008 // Just load directly from f32mem to GR32.
3009 LoadSDNode *LD = cast<LoadSDNode>(N1);
3010 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3011 LD->getSrcValue(), LD->getSrcValueOffset());
3012 } else {
3013 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3014 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3015 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3016 DAG.getConstant(0, getPointerTy()));
3017 }
3018 }
3019 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3020 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3021 DAG.getConstant(Idx, getPointerTy()));
3022 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3023 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3024 DAG.getConstant(Idx+1, getPointerTy()));
3025 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3026 }
3027 }
3028
3029 return SDOperand();
3030}
3031
3032SDOperand
3033X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3034 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3035 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3036}
3037
3038// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3039// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3040// one of the above mentioned nodes. It has to be wrapped because otherwise
3041// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3042// be used to form addressing mode. These wrapped nodes will be selected
3043// into MOV32ri.
3044SDOperand
3045X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3046 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3047 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3048 getPointerTy(),
3049 CP->getAlignment());
3050 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3051 // With PIC, the address is actually $g + Offset.
3052 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3053 !Subtarget->isPICStyleRIPRel()) {
3054 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3055 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3056 Result);
3057 }
3058
3059 return Result;
3060}
3061
3062SDOperand
3063X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3064 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3065 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3066 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3067 // With PIC, the address is actually $g + Offset.
3068 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3069 !Subtarget->isPICStyleRIPRel()) {
3070 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3071 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3072 Result);
3073 }
3074
3075 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3076 // load the value at address GV, not the value of GV itself. This means that
3077 // the GlobalAddress must be in the base or index register of the address, not
3078 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3079 // The same applies for external symbols during PIC codegen
3080 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3081 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3082
3083 return Result;
3084}
3085
3086// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3087static SDOperand
3088LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3089 const MVT::ValueType PtrVT) {
3090 SDOperand InFlag;
3091 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3092 DAG.getNode(X86ISD::GlobalBaseReg,
3093 PtrVT), InFlag);
3094 InFlag = Chain.getValue(1);
3095
3096 // emit leal symbol@TLSGD(,%ebx,1), %eax
3097 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3098 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3099 GA->getValueType(0),
3100 GA->getOffset());
3101 SDOperand Ops[] = { Chain, TGA, InFlag };
3102 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3103 InFlag = Result.getValue(2);
3104 Chain = Result.getValue(1);
3105
3106 // call ___tls_get_addr. This function receives its argument in
3107 // the register EAX.
3108 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3109 InFlag = Chain.getValue(1);
3110
3111 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3112 SDOperand Ops1[] = { Chain,
3113 DAG.getTargetExternalSymbol("___tls_get_addr",
3114 PtrVT),
3115 DAG.getRegister(X86::EAX, PtrVT),
3116 DAG.getRegister(X86::EBX, PtrVT),
3117 InFlag };
3118 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3119 InFlag = Chain.getValue(1);
3120
3121 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3122}
3123
3124// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3125// "local exec" model.
3126static SDOperand
3127LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3128 const MVT::ValueType PtrVT) {
3129 // Get the Thread Pointer
3130 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3131 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3132 // exec)
3133 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3134 GA->getValueType(0),
3135 GA->getOffset());
3136 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3137
3138 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3139 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3140
3141 // The address of the thread local variable is the add of the thread
3142 // pointer with the offset of the variable.
3143 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3144}
3145
3146SDOperand
3147X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3148 // TODO: implement the "local dynamic" model
3149 // TODO: implement the "initial exec"model for pic executables
3150 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3151 "TLS not implemented for non-ELF and 64-bit targets");
3152 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3153 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3154 // otherwise use the "Local Exec"TLS Model
3155 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3156 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3157 else
3158 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3159}
3160
3161SDOperand
3162X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3163 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3164 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3165 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3166 // With PIC, the address is actually $g + Offset.
3167 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3168 !Subtarget->isPICStyleRIPRel()) {
3169 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3170 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3171 Result);
3172 }
3173
3174 return Result;
3175}
3176
3177SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3178 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3179 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3180 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3181 // With PIC, the address is actually $g + Offset.
3182 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3183 !Subtarget->isPICStyleRIPRel()) {
3184 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3185 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3186 Result);
3187 }
3188
3189 return Result;
3190}
3191
3192SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3193 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3194 "Not an i64 shift!");
3195 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3196 SDOperand ShOpLo = Op.getOperand(0);
3197 SDOperand ShOpHi = Op.getOperand(1);
3198 SDOperand ShAmt = Op.getOperand(2);
3199 SDOperand Tmp1 = isSRA ?
3200 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3201 DAG.getConstant(0, MVT::i32);
3202
3203 SDOperand Tmp2, Tmp3;
3204 if (Op.getOpcode() == ISD::SHL_PARTS) {
3205 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3206 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3207 } else {
3208 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3209 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3210 }
3211
3212 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3213 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3214 DAG.getConstant(32, MVT::i8));
3215 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3216 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
3217
3218 SDOperand Hi, Lo;
3219 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3220
3221 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3222 SmallVector<SDOperand, 4> Ops;
3223 if (Op.getOpcode() == ISD::SHL_PARTS) {
3224 Ops.push_back(Tmp2);
3225 Ops.push_back(Tmp3);
3226 Ops.push_back(CC);
3227 Ops.push_back(InFlag);
3228 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3229 InFlag = Hi.getValue(1);
3230
3231 Ops.clear();
3232 Ops.push_back(Tmp3);
3233 Ops.push_back(Tmp1);
3234 Ops.push_back(CC);
3235 Ops.push_back(InFlag);
3236 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3237 } else {
3238 Ops.push_back(Tmp2);
3239 Ops.push_back(Tmp3);
3240 Ops.push_back(CC);
3241 Ops.push_back(InFlag);
3242 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3243 InFlag = Lo.getValue(1);
3244
3245 Ops.clear();
3246 Ops.push_back(Tmp3);
3247 Ops.push_back(Tmp1);
3248 Ops.push_back(CC);
3249 Ops.push_back(InFlag);
3250 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3251 }
3252
3253 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3254 Ops.clear();
3255 Ops.push_back(Lo);
3256 Ops.push_back(Hi);
3257 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3258}
3259
3260SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3261 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3262 Op.getOperand(0).getValueType() >= MVT::i16 &&
3263 "Unknown SINT_TO_FP to lower!");
3264
3265 SDOperand Result;
3266 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3267 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3268 MachineFunction &MF = DAG.getMachineFunction();
3269 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3270 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3271 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3272 StackSlot, NULL, 0);
3273
3274 // Build the FILD
3275 SDVTList Tys;
3276 if (X86ScalarSSE)
3277 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3278 else
3279 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
3280 SmallVector<SDOperand, 8> Ops;
3281 Ops.push_back(Chain);
3282 Ops.push_back(StackSlot);
3283 Ops.push_back(DAG.getValueType(SrcVT));
3284 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3285 Tys, &Ops[0], Ops.size());
3286
3287 if (X86ScalarSSE) {
3288 Chain = Result.getValue(1);
3289 SDOperand InFlag = Result.getValue(2);
3290
3291 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3292 // shouldn't be necessary except that RFP cannot be live across
3293 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3294 MachineFunction &MF = DAG.getMachineFunction();
3295 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3296 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3297 Tys = DAG.getVTList(MVT::Other);
3298 SmallVector<SDOperand, 8> Ops;
3299 Ops.push_back(Chain);
3300 Ops.push_back(Result);
3301 Ops.push_back(StackSlot);
3302 Ops.push_back(DAG.getValueType(Op.getValueType()));
3303 Ops.push_back(InFlag);
3304 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3305 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3306 }
3307
3308 return Result;
3309}
3310
3311SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3312 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3313 "Unknown FP_TO_SINT to lower!");
3314 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3315 // stack slot.
3316 MachineFunction &MF = DAG.getMachineFunction();
3317 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3318 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3319 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3320
3321 unsigned Opc;
3322 switch (Op.getValueType()) {
3323 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3324 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3325 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3326 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3327 }
3328
3329 SDOperand Chain = DAG.getEntryNode();
3330 SDOperand Value = Op.getOperand(0);
3331 if (X86ScalarSSE) {
3332 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3333 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3334 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
3335 SDOperand Ops[] = {
3336 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3337 };
3338 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3339 Chain = Value.getValue(1);
3340 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3341 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3342 }
3343
3344 // Build the FP_TO_INT*_IN_MEM
3345 SDOperand Ops[] = { Chain, Value, StackSlot };
3346 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3347
3348 // Load the result.
3349 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3350}
3351
3352SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3353 MVT::ValueType VT = Op.getValueType();
3354 MVT::ValueType EltVT = VT;
3355 if (MVT::isVector(VT))
3356 EltVT = MVT::getVectorElementType(VT);
3357 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3358 std::vector<Constant*> CV;
3359 if (EltVT == MVT::f64) {
3360 Constant *C = ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)));
3361 CV.push_back(C);
3362 CV.push_back(C);
3363 } else {
3364 Constant *C = ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)));
3365 CV.push_back(C);
3366 CV.push_back(C);
3367 CV.push_back(C);
3368 CV.push_back(C);
3369 }
Dan Gohman11821702007-07-27 17:16:43 +00003370 Constant *C = ConstantVector::get(CV);
3371 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3372 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3373 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3375}
3376
3377SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3378 MVT::ValueType VT = Op.getValueType();
3379 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00003380 unsigned EltNum = 1;
3381 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003382 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00003383 EltNum = MVT::getVectorNumElements(VT);
3384 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3386 std::vector<Constant*> CV;
3387 if (EltVT == MVT::f64) {
3388 Constant *C = ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63));
3389 CV.push_back(C);
3390 CV.push_back(C);
3391 } else {
3392 Constant *C = ConstantFP::get(OpNTy, BitsToFloat(1U << 31));
3393 CV.push_back(C);
3394 CV.push_back(C);
3395 CV.push_back(C);
3396 CV.push_back(C);
3397 }
Dan Gohman11821702007-07-27 17:16:43 +00003398 Constant *C = ConstantVector::get(CV);
3399 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3400 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3401 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00003402 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00003403 return DAG.getNode(ISD::BIT_CONVERT, VT,
3404 DAG.getNode(ISD::XOR, MVT::v2i64,
3405 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
3406 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
3407 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00003408 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3409 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410}
3411
3412SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
3413 SDOperand Op0 = Op.getOperand(0);
3414 SDOperand Op1 = Op.getOperand(1);
3415 MVT::ValueType VT = Op.getValueType();
3416 MVT::ValueType SrcVT = Op1.getValueType();
3417 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
3418
3419 // If second operand is smaller, extend it first.
3420 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3421 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3422 SrcVT = VT;
3423 }
3424
3425 // First get the sign bit of second operand.
3426 std::vector<Constant*> CV;
3427 if (SrcVT == MVT::f64) {
3428 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3429 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3430 } else {
3431 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3432 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3433 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3434 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3435 }
Dan Gohman11821702007-07-27 17:16:43 +00003436 Constant *C = ConstantVector::get(CV);
3437 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3438 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
3439 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003440 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
3441
3442 // Shift sign bit right or left if the two operands have different types.
3443 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3444 // Op0 is MVT::f32, Op1 is MVT::f64.
3445 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3446 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3447 DAG.getConstant(32, MVT::i32));
3448 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3449 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3450 DAG.getConstant(0, getPointerTy()));
3451 }
3452
3453 // Clear first operand sign bit.
3454 CV.clear();
3455 if (VT == MVT::f64) {
3456 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3457 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3458 } else {
3459 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3460 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3461 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3462 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3463 }
Dan Gohman11821702007-07-27 17:16:43 +00003464 C = ConstantVector::get(CV);
3465 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3466 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3467 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003468 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3469
3470 // Or the value with the sign bit.
3471 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
3472}
3473
3474SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3475 SDOperand Chain) {
3476 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3477 SDOperand Cond;
3478 SDOperand Op0 = Op.getOperand(0);
3479 SDOperand Op1 = Op.getOperand(1);
3480 SDOperand CC = Op.getOperand(2);
3481 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3482 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3483 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
3484 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3485 unsigned X86CC;
3486
3487 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3488 Op0, Op1, DAG)) {
3489 SDOperand Ops1[] = { Chain, Op0, Op1 };
3490 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
3491 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
3492 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3493 }
3494
3495 assert(isFP && "Illegal integer SetCC!");
3496
3497 SDOperand COps[] = { Chain, Op0, Op1 };
3498 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
3499
3500 switch (SetCCOpcode) {
3501 default: assert(false && "Illegal floating point SetCC!");
3502 case ISD::SETOEQ: { // !PF & ZF
3503 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
3504 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3505 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
3506 Tmp1.getValue(1) };
3507 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3508 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3509 }
3510 case ISD::SETUNE: { // PF | !ZF
3511 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
3512 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
3513 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
3514 Tmp1.getValue(1) };
3515 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
3516 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3517 }
3518 }
3519}
3520
3521SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3522 bool addTest = true;
3523 SDOperand Chain = DAG.getEntryNode();
3524 SDOperand Cond = Op.getOperand(0);
3525 SDOperand CC;
3526 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3527
3528 if (Cond.getOpcode() == ISD::SETCC)
3529 Cond = LowerSETCC(Cond, DAG, Chain);
3530
3531 if (Cond.getOpcode() == X86ISD::SETCC) {
3532 CC = Cond.getOperand(0);
3533
3534 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3535 // (since flag operand cannot be shared). Use it as the condition setting
3536 // operand in place of the X86ISD::SETCC.
3537 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3538 // to use a test instead of duplicating the X86ISD::CMP (for register
3539 // pressure reason)?
3540 SDOperand Cmp = Cond.getOperand(1);
3541 unsigned Opc = Cmp.getOpcode();
3542 bool IllegalFPCMov = !X86ScalarSSE &&
3543 MVT::isFloatingPoint(Op.getValueType()) &&
3544 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3545 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3546 !IllegalFPCMov) {
3547 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3548 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3549 addTest = false;
3550 }
3551 }
3552
3553 if (addTest) {
3554 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3555 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3556 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3557 }
3558
3559 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3560 SmallVector<SDOperand, 4> Ops;
3561 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3562 // condition is true.
3563 Ops.push_back(Op.getOperand(2));
3564 Ops.push_back(Op.getOperand(1));
3565 Ops.push_back(CC);
3566 Ops.push_back(Cond.getValue(1));
3567 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
3568}
3569
3570SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3571 bool addTest = true;
3572 SDOperand Chain = Op.getOperand(0);
3573 SDOperand Cond = Op.getOperand(1);
3574 SDOperand Dest = Op.getOperand(2);
3575 SDOperand CC;
3576 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3577
3578 if (Cond.getOpcode() == ISD::SETCC)
3579 Cond = LowerSETCC(Cond, DAG, Chain);
3580
3581 if (Cond.getOpcode() == X86ISD::SETCC) {
3582 CC = Cond.getOperand(0);
3583
3584 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3585 // (since flag operand cannot be shared). Use it as the condition setting
3586 // operand in place of the X86ISD::SETCC.
3587 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3588 // to use a test instead of duplicating the X86ISD::CMP (for register
3589 // pressure reason)?
3590 SDOperand Cmp = Cond.getOperand(1);
3591 unsigned Opc = Cmp.getOpcode();
3592 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3593 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3594 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3595 addTest = false;
3596 }
3597 }
3598
3599 if (addTest) {
3600 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3601 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3602 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
3603 }
3604 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3605 Cond, Op.getOperand(2), CC, Cond.getValue(1));
3606}
3607
3608SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3609 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3610
3611 if (Subtarget->is64Bit())
3612 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
3613 else
3614 switch (CallingConv) {
3615 default:
3616 assert(0 && "Unsupported calling convention");
3617 case CallingConv::Fast:
3618 // TODO: Implement fastcc
3619 // Falls through
3620 case CallingConv::C:
3621 case CallingConv::X86_StdCall:
3622 return LowerCCCCallTo(Op, DAG, CallingConv);
3623 case CallingConv::X86_FastCall:
3624 return LowerFastCCCallTo(Op, DAG, CallingConv);
3625 }
3626}
3627
3628
3629// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3630// Calls to _alloca is needed to probe the stack when allocating more than 4k
3631// bytes in one go. Touching the stack at 4K increments is necessary to ensure
3632// that the guard pages used by the OS virtual memory manager are allocated in
3633// correct sequence.
3634SDOperand
3635X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3636 SelectionDAG &DAG) {
3637 assert(Subtarget->isTargetCygMing() &&
3638 "This should be used only on Cygwin/Mingw targets");
3639
3640 // Get the inputs.
3641 SDOperand Chain = Op.getOperand(0);
3642 SDOperand Size = Op.getOperand(1);
3643 // FIXME: Ensure alignment here
3644
3645 SDOperand Flag;
3646
3647 MVT::ValueType IntPtr = getPointerTy();
3648 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
3649
3650 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
3651 Flag = Chain.getValue(1);
3652
3653 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3654 SDOperand Ops[] = { Chain,
3655 DAG.getTargetExternalSymbol("_alloca", IntPtr),
3656 DAG.getRegister(X86::EAX, IntPtr),
3657 Flag };
3658 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
3659 Flag = Chain.getValue(1);
3660
3661 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
3662
3663 std::vector<MVT::ValueType> Tys;
3664 Tys.push_back(SPTy);
3665 Tys.push_back(MVT::Other);
3666 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
3667 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
3668}
3669
3670SDOperand
3671X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3672 MachineFunction &MF = DAG.getMachineFunction();
3673 const Function* Fn = MF.getFunction();
3674 if (Fn->hasExternalLinkage() &&
3675 Subtarget->isTargetCygMing() &&
3676 Fn->getName() == "main")
3677 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
3678
3679 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3680 if (Subtarget->is64Bit())
3681 return LowerX86_64CCCArguments(Op, DAG);
3682 else
3683 switch(CC) {
3684 default:
3685 assert(0 && "Unsupported calling convention");
3686 case CallingConv::Fast:
3687 // TODO: implement fastcc.
3688
3689 // Falls through
3690 case CallingConv::C:
3691 return LowerCCCArguments(Op, DAG);
3692 case CallingConv::X86_StdCall:
3693 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
3694 return LowerCCCArguments(Op, DAG, true);
3695 case CallingConv::X86_FastCall:
3696 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
3697 return LowerFastCCArguments(Op, DAG);
3698 }
3699}
3700
3701SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3702 SDOperand InFlag(0, 0);
3703 SDOperand Chain = Op.getOperand(0);
3704 unsigned Align =
3705 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3706 if (Align == 0) Align = 1;
3707
3708 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3709 // If not DWORD aligned, call memset if size is less than the threshold.
3710 // It knows how to align to the right boundary first.
3711 if ((Align & 3) != 0 ||
3712 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3713 MVT::ValueType IntPtr = getPointerTy();
3714 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3715 TargetLowering::ArgListTy Args;
3716 TargetLowering::ArgListEntry Entry;
3717 Entry.Node = Op.getOperand(1);
3718 Entry.Ty = IntPtrTy;
3719 Args.push_back(Entry);
3720 // Extend the unsigned i8 argument to be an int value for the call.
3721 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3722 Entry.Ty = IntPtrTy;
3723 Args.push_back(Entry);
3724 Entry.Node = Op.getOperand(3);
3725 Args.push_back(Entry);
3726 std::pair<SDOperand,SDOperand> CallResult =
3727 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3728 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3729 return CallResult.second;
3730 }
3731
3732 MVT::ValueType AVT;
3733 SDOperand Count;
3734 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3735 unsigned BytesLeft = 0;
3736 bool TwoRepStos = false;
3737 if (ValC) {
3738 unsigned ValReg;
3739 uint64_t Val = ValC->getValue() & 255;
3740
3741 // If the value is a constant, then we can potentially use larger sets.
3742 switch (Align & 3) {
3743 case 2: // WORD aligned
3744 AVT = MVT::i16;
3745 ValReg = X86::AX;
3746 Val = (Val << 8) | Val;
3747 break;
3748 case 0: // DWORD aligned
3749 AVT = MVT::i32;
3750 ValReg = X86::EAX;
3751 Val = (Val << 8) | Val;
3752 Val = (Val << 16) | Val;
3753 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3754 AVT = MVT::i64;
3755 ValReg = X86::RAX;
3756 Val = (Val << 32) | Val;
3757 }
3758 break;
3759 default: // Byte aligned
3760 AVT = MVT::i8;
3761 ValReg = X86::AL;
3762 Count = Op.getOperand(3);
3763 break;
3764 }
3765
3766 if (AVT > MVT::i8) {
3767 if (I) {
3768 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3769 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3770 BytesLeft = I->getValue() % UBytes;
3771 } else {
3772 assert(AVT >= MVT::i32 &&
3773 "Do not use rep;stos if not at least DWORD aligned");
3774 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3775 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3776 TwoRepStos = true;
3777 }
3778 }
3779
3780 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3781 InFlag);
3782 InFlag = Chain.getValue(1);
3783 } else {
3784 AVT = MVT::i8;
3785 Count = Op.getOperand(3);
3786 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3787 InFlag = Chain.getValue(1);
3788 }
3789
3790 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3791 Count, InFlag);
3792 InFlag = Chain.getValue(1);
3793 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3794 Op.getOperand(1), InFlag);
3795 InFlag = Chain.getValue(1);
3796
3797 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3798 SmallVector<SDOperand, 8> Ops;
3799 Ops.push_back(Chain);
3800 Ops.push_back(DAG.getValueType(AVT));
3801 Ops.push_back(InFlag);
3802 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3803
3804 if (TwoRepStos) {
3805 InFlag = Chain.getValue(1);
3806 Count = Op.getOperand(3);
3807 MVT::ValueType CVT = Count.getValueType();
3808 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3809 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3810 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3811 Left, InFlag);
3812 InFlag = Chain.getValue(1);
3813 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3814 Ops.clear();
3815 Ops.push_back(Chain);
3816 Ops.push_back(DAG.getValueType(MVT::i8));
3817 Ops.push_back(InFlag);
3818 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
3819 } else if (BytesLeft) {
3820 // Issue stores for the last 1 - 7 bytes.
3821 SDOperand Value;
3822 unsigned Val = ValC->getValue() & 255;
3823 unsigned Offset = I->getValue() - BytesLeft;
3824 SDOperand DstAddr = Op.getOperand(1);
3825 MVT::ValueType AddrVT = DstAddr.getValueType();
3826 if (BytesLeft >= 4) {
3827 Val = (Val << 8) | Val;
3828 Val = (Val << 16) | Val;
3829 Value = DAG.getConstant(Val, MVT::i32);
3830 Chain = DAG.getStore(Chain, Value,
3831 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3832 DAG.getConstant(Offset, AddrVT)),
3833 NULL, 0);
3834 BytesLeft -= 4;
3835 Offset += 4;
3836 }
3837 if (BytesLeft >= 2) {
3838 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3839 Chain = DAG.getStore(Chain, Value,
3840 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3841 DAG.getConstant(Offset, AddrVT)),
3842 NULL, 0);
3843 BytesLeft -= 2;
3844 Offset += 2;
3845 }
3846 if (BytesLeft == 1) {
3847 Value = DAG.getConstant(Val, MVT::i8);
3848 Chain = DAG.getStore(Chain, Value,
3849 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3850 DAG.getConstant(Offset, AddrVT)),
3851 NULL, 0);
3852 }
3853 }
3854
3855 return Chain;
3856}
3857
3858SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3859 SDOperand Chain = Op.getOperand(0);
3860 unsigned Align =
3861 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3862 if (Align == 0) Align = 1;
3863
3864 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3865 // If not DWORD aligned, call memcpy if size is less than the threshold.
3866 // It knows how to align to the right boundary first.
3867 if ((Align & 3) != 0 ||
3868 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3869 MVT::ValueType IntPtr = getPointerTy();
3870 TargetLowering::ArgListTy Args;
3871 TargetLowering::ArgListEntry Entry;
3872 Entry.Ty = getTargetData()->getIntPtrType();
3873 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3874 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3875 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
3876 std::pair<SDOperand,SDOperand> CallResult =
3877 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
3878 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3879 return CallResult.second;
3880 }
3881
3882 MVT::ValueType AVT;
3883 SDOperand Count;
3884 unsigned BytesLeft = 0;
3885 bool TwoRepMovs = false;
3886 switch (Align & 3) {
3887 case 2: // WORD aligned
3888 AVT = MVT::i16;
3889 break;
3890 case 0: // DWORD aligned
3891 AVT = MVT::i32;
3892 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3893 AVT = MVT::i64;
3894 break;
3895 default: // Byte aligned
3896 AVT = MVT::i8;
3897 Count = Op.getOperand(3);
3898 break;
3899 }
3900
3901 if (AVT > MVT::i8) {
3902 if (I) {
3903 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3904 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3905 BytesLeft = I->getValue() % UBytes;
3906 } else {
3907 assert(AVT >= MVT::i32 &&
3908 "Do not use rep;movs if not at least DWORD aligned");
3909 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3910 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3911 TwoRepMovs = true;
3912 }
3913 }
3914
3915 SDOperand InFlag(0, 0);
3916 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3917 Count, InFlag);
3918 InFlag = Chain.getValue(1);
3919 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3920 Op.getOperand(1), InFlag);
3921 InFlag = Chain.getValue(1);
3922 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3923 Op.getOperand(2), InFlag);
3924 InFlag = Chain.getValue(1);
3925
3926 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3927 SmallVector<SDOperand, 8> Ops;
3928 Ops.push_back(Chain);
3929 Ops.push_back(DAG.getValueType(AVT));
3930 Ops.push_back(InFlag);
3931 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3932
3933 if (TwoRepMovs) {
3934 InFlag = Chain.getValue(1);
3935 Count = Op.getOperand(3);
3936 MVT::ValueType CVT = Count.getValueType();
3937 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3938 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3939 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3940 Left, InFlag);
3941 InFlag = Chain.getValue(1);
3942 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
3943 Ops.clear();
3944 Ops.push_back(Chain);
3945 Ops.push_back(DAG.getValueType(MVT::i8));
3946 Ops.push_back(InFlag);
3947 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
3948 } else if (BytesLeft) {
3949 // Issue loads and stores for the last 1 - 7 bytes.
3950 unsigned Offset = I->getValue() - BytesLeft;
3951 SDOperand DstAddr = Op.getOperand(1);
3952 MVT::ValueType DstVT = DstAddr.getValueType();
3953 SDOperand SrcAddr = Op.getOperand(2);
3954 MVT::ValueType SrcVT = SrcAddr.getValueType();
3955 SDOperand Value;
3956 if (BytesLeft >= 4) {
3957 Value = DAG.getLoad(MVT::i32, Chain,
3958 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3959 DAG.getConstant(Offset, SrcVT)),
3960 NULL, 0);
3961 Chain = Value.getValue(1);
3962 Chain = DAG.getStore(Chain, Value,
3963 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3964 DAG.getConstant(Offset, DstVT)),
3965 NULL, 0);
3966 BytesLeft -= 4;
3967 Offset += 4;
3968 }
3969 if (BytesLeft >= 2) {
3970 Value = DAG.getLoad(MVT::i16, Chain,
3971 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3972 DAG.getConstant(Offset, SrcVT)),
3973 NULL, 0);
3974 Chain = Value.getValue(1);
3975 Chain = DAG.getStore(Chain, Value,
3976 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3977 DAG.getConstant(Offset, DstVT)),
3978 NULL, 0);
3979 BytesLeft -= 2;
3980 Offset += 2;
3981 }
3982
3983 if (BytesLeft == 1) {
3984 Value = DAG.getLoad(MVT::i8, Chain,
3985 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3986 DAG.getConstant(Offset, SrcVT)),
3987 NULL, 0);
3988 Chain = Value.getValue(1);
3989 Chain = DAG.getStore(Chain, Value,
3990 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3991 DAG.getConstant(Offset, DstVT)),
3992 NULL, 0);
3993 }
3994 }
3995
3996 return Chain;
3997}
3998
3999SDOperand
4000X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4001 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4002 SDOperand TheOp = Op.getOperand(0);
4003 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
4004 if (Subtarget->is64Bit()) {
4005 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4006 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4007 MVT::i64, Copy1.getValue(2));
4008 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4009 DAG.getConstant(32, MVT::i8));
4010 SDOperand Ops[] = {
4011 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4012 };
4013
4014 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4015 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
4016 }
4017
4018 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4019 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4020 MVT::i32, Copy1.getValue(2));
4021 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4022 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4023 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
4024}
4025
4026SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4027 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4028
4029 if (!Subtarget->is64Bit()) {
4030 // vastart just stores the address of the VarArgsFrameIndex slot into the
4031 // memory location argument.
4032 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4033 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4034 SV->getOffset());
4035 }
4036
4037 // __va_list_tag:
4038 // gp_offset (0 - 6 * 8)
4039 // fp_offset (48 - 48 + 8 * 16)
4040 // overflow_arg_area (point to parameters coming in memory).
4041 // reg_save_area
4042 SmallVector<SDOperand, 8> MemOps;
4043 SDOperand FIN = Op.getOperand(1);
4044 // Store gp_offset
4045 SDOperand Store = DAG.getStore(Op.getOperand(0),
4046 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4047 FIN, SV->getValue(), SV->getOffset());
4048 MemOps.push_back(Store);
4049
4050 // Store fp_offset
4051 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4052 DAG.getConstant(4, getPointerTy()));
4053 Store = DAG.getStore(Op.getOperand(0),
4054 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4055 FIN, SV->getValue(), SV->getOffset());
4056 MemOps.push_back(Store);
4057
4058 // Store ptr to overflow_arg_area
4059 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4060 DAG.getConstant(4, getPointerTy()));
4061 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4062 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4063 SV->getOffset());
4064 MemOps.push_back(Store);
4065
4066 // Store ptr to reg_save_area.
4067 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4068 DAG.getConstant(8, getPointerTy()));
4069 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4070 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4071 SV->getOffset());
4072 MemOps.push_back(Store);
4073 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4074}
4075
4076SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4077 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4078 SDOperand Chain = Op.getOperand(0);
4079 SDOperand DstPtr = Op.getOperand(1);
4080 SDOperand SrcPtr = Op.getOperand(2);
4081 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4082 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4083
4084 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4085 SrcSV->getValue(), SrcSV->getOffset());
4086 Chain = SrcPtr.getValue(1);
4087 for (unsigned i = 0; i < 3; ++i) {
4088 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4089 SrcSV->getValue(), SrcSV->getOffset());
4090 Chain = Val.getValue(1);
4091 Chain = DAG.getStore(Chain, Val, DstPtr,
4092 DstSV->getValue(), DstSV->getOffset());
4093 if (i == 2)
4094 break;
4095 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4096 DAG.getConstant(8, getPointerTy()));
4097 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4098 DAG.getConstant(8, getPointerTy()));
4099 }
4100 return Chain;
4101}
4102
4103SDOperand
4104X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4105 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4106 switch (IntNo) {
4107 default: return SDOperand(); // Don't custom lower most intrinsics.
4108 // Comparison intrinsics.
4109 case Intrinsic::x86_sse_comieq_ss:
4110 case Intrinsic::x86_sse_comilt_ss:
4111 case Intrinsic::x86_sse_comile_ss:
4112 case Intrinsic::x86_sse_comigt_ss:
4113 case Intrinsic::x86_sse_comige_ss:
4114 case Intrinsic::x86_sse_comineq_ss:
4115 case Intrinsic::x86_sse_ucomieq_ss:
4116 case Intrinsic::x86_sse_ucomilt_ss:
4117 case Intrinsic::x86_sse_ucomile_ss:
4118 case Intrinsic::x86_sse_ucomigt_ss:
4119 case Intrinsic::x86_sse_ucomige_ss:
4120 case Intrinsic::x86_sse_ucomineq_ss:
4121 case Intrinsic::x86_sse2_comieq_sd:
4122 case Intrinsic::x86_sse2_comilt_sd:
4123 case Intrinsic::x86_sse2_comile_sd:
4124 case Intrinsic::x86_sse2_comigt_sd:
4125 case Intrinsic::x86_sse2_comige_sd:
4126 case Intrinsic::x86_sse2_comineq_sd:
4127 case Intrinsic::x86_sse2_ucomieq_sd:
4128 case Intrinsic::x86_sse2_ucomilt_sd:
4129 case Intrinsic::x86_sse2_ucomile_sd:
4130 case Intrinsic::x86_sse2_ucomigt_sd:
4131 case Intrinsic::x86_sse2_ucomige_sd:
4132 case Intrinsic::x86_sse2_ucomineq_sd: {
4133 unsigned Opc = 0;
4134 ISD::CondCode CC = ISD::SETCC_INVALID;
4135 switch (IntNo) {
4136 default: break;
4137 case Intrinsic::x86_sse_comieq_ss:
4138 case Intrinsic::x86_sse2_comieq_sd:
4139 Opc = X86ISD::COMI;
4140 CC = ISD::SETEQ;
4141 break;
4142 case Intrinsic::x86_sse_comilt_ss:
4143 case Intrinsic::x86_sse2_comilt_sd:
4144 Opc = X86ISD::COMI;
4145 CC = ISD::SETLT;
4146 break;
4147 case Intrinsic::x86_sse_comile_ss:
4148 case Intrinsic::x86_sse2_comile_sd:
4149 Opc = X86ISD::COMI;
4150 CC = ISD::SETLE;
4151 break;
4152 case Intrinsic::x86_sse_comigt_ss:
4153 case Intrinsic::x86_sse2_comigt_sd:
4154 Opc = X86ISD::COMI;
4155 CC = ISD::SETGT;
4156 break;
4157 case Intrinsic::x86_sse_comige_ss:
4158 case Intrinsic::x86_sse2_comige_sd:
4159 Opc = X86ISD::COMI;
4160 CC = ISD::SETGE;
4161 break;
4162 case Intrinsic::x86_sse_comineq_ss:
4163 case Intrinsic::x86_sse2_comineq_sd:
4164 Opc = X86ISD::COMI;
4165 CC = ISD::SETNE;
4166 break;
4167 case Intrinsic::x86_sse_ucomieq_ss:
4168 case Intrinsic::x86_sse2_ucomieq_sd:
4169 Opc = X86ISD::UCOMI;
4170 CC = ISD::SETEQ;
4171 break;
4172 case Intrinsic::x86_sse_ucomilt_ss:
4173 case Intrinsic::x86_sse2_ucomilt_sd:
4174 Opc = X86ISD::UCOMI;
4175 CC = ISD::SETLT;
4176 break;
4177 case Intrinsic::x86_sse_ucomile_ss:
4178 case Intrinsic::x86_sse2_ucomile_sd:
4179 Opc = X86ISD::UCOMI;
4180 CC = ISD::SETLE;
4181 break;
4182 case Intrinsic::x86_sse_ucomigt_ss:
4183 case Intrinsic::x86_sse2_ucomigt_sd:
4184 Opc = X86ISD::UCOMI;
4185 CC = ISD::SETGT;
4186 break;
4187 case Intrinsic::x86_sse_ucomige_ss:
4188 case Intrinsic::x86_sse2_ucomige_sd:
4189 Opc = X86ISD::UCOMI;
4190 CC = ISD::SETGE;
4191 break;
4192 case Intrinsic::x86_sse_ucomineq_ss:
4193 case Intrinsic::x86_sse2_ucomineq_sd:
4194 Opc = X86ISD::UCOMI;
4195 CC = ISD::SETNE;
4196 break;
4197 }
4198
4199 unsigned X86CC;
4200 SDOperand LHS = Op.getOperand(1);
4201 SDOperand RHS = Op.getOperand(2);
4202 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4203
4204 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4205 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4206 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4207 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4208 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4209 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4210 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4211 }
4212 }
4213}
4214
4215SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4216 // Depths > 0 not supported yet!
4217 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4218 return SDOperand();
4219
4220 // Just load the return address
4221 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4222 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4223}
4224
4225SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4226 // Depths > 0 not supported yet!
4227 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4228 return SDOperand();
4229
4230 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4231 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4232 DAG.getConstant(4, getPointerTy()));
4233}
4234
4235SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4236 SelectionDAG &DAG) {
4237 // Is not yet supported on x86-64
4238 if (Subtarget->is64Bit())
4239 return SDOperand();
4240
4241 return DAG.getConstant(8, getPointerTy());
4242}
4243
4244SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4245{
4246 assert(!Subtarget->is64Bit() &&
4247 "Lowering of eh_return builtin is not supported yet on x86-64");
4248
4249 MachineFunction &MF = DAG.getMachineFunction();
4250 SDOperand Chain = Op.getOperand(0);
4251 SDOperand Offset = Op.getOperand(1);
4252 SDOperand Handler = Op.getOperand(2);
4253
4254 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4255 getPointerTy());
4256
4257 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4258 DAG.getConstant(-4UL, getPointerTy()));
4259 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4260 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4261 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4262 MF.addLiveOut(X86::ECX);
4263
4264 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4265 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4266}
4267
4268/// LowerOperation - Provide custom lowering hooks for some operations.
4269///
4270SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4271 switch (Op.getOpcode()) {
4272 default: assert(0 && "Should not custom lower this!");
4273 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4274 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4275 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4276 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4277 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4278 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4279 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4280 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
4281 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4282 case ISD::SHL_PARTS:
4283 case ISD::SRA_PARTS:
4284 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4285 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4286 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4287 case ISD::FABS: return LowerFABS(Op, DAG);
4288 case ISD::FNEG: return LowerFNEG(Op, DAG);
4289 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
4290 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
4291 case ISD::SELECT: return LowerSELECT(Op, DAG);
4292 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4293 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4294 case ISD::CALL: return LowerCALL(Op, DAG);
4295 case ISD::RET: return LowerRET(Op, DAG);
4296 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4297 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4298 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4299 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4300 case ISD::VASTART: return LowerVASTART(Op, DAG);
4301 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
4302 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4303 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4304 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4305 case ISD::FRAME_TO_ARGS_OFFSET:
4306 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
4307 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
4308 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
4309 }
4310 return SDOperand();
4311}
4312
4313const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4314 switch (Opcode) {
4315 default: return NULL;
4316 case X86ISD::SHLD: return "X86ISD::SHLD";
4317 case X86ISD::SHRD: return "X86ISD::SHRD";
4318 case X86ISD::FAND: return "X86ISD::FAND";
4319 case X86ISD::FOR: return "X86ISD::FOR";
4320 case X86ISD::FXOR: return "X86ISD::FXOR";
4321 case X86ISD::FSRL: return "X86ISD::FSRL";
4322 case X86ISD::FILD: return "X86ISD::FILD";
4323 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4324 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4325 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4326 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4327 case X86ISD::FLD: return "X86ISD::FLD";
4328 case X86ISD::FST: return "X86ISD::FST";
4329 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
4330 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
4331 case X86ISD::CALL: return "X86ISD::CALL";
4332 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4333 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4334 case X86ISD::CMP: return "X86ISD::CMP";
4335 case X86ISD::COMI: return "X86ISD::COMI";
4336 case X86ISD::UCOMI: return "X86ISD::UCOMI";
4337 case X86ISD::SETCC: return "X86ISD::SETCC";
4338 case X86ISD::CMOV: return "X86ISD::CMOV";
4339 case X86ISD::BRCOND: return "X86ISD::BRCOND";
4340 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
4341 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4342 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
4344 case X86ISD::Wrapper: return "X86ISD::Wrapper";
4345 case X86ISD::S2VEC: return "X86ISD::S2VEC";
4346 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
4347 case X86ISD::PINSRW: return "X86ISD::PINSRW";
4348 case X86ISD::FMAX: return "X86ISD::FMAX";
4349 case X86ISD::FMIN: return "X86ISD::FMIN";
4350 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
4351 case X86ISD::FRCP: return "X86ISD::FRCP";
4352 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4353 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
4354 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
4355 }
4356}
4357
4358// isLegalAddressingMode - Return true if the addressing mode represented
4359// by AM is legal for this target, for a load/store of the specified type.
4360bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4361 const Type *Ty) const {
4362 // X86 supports extremely general addressing modes.
4363
4364 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4365 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4366 return false;
4367
4368 if (AM.BaseGV) {
4369 // X86-64 only supports addr of globals in small code model.
4370 if (Subtarget->is64Bit() &&
4371 getTargetMachine().getCodeModel() != CodeModel::Small)
4372 return false;
4373
4374 // We can only fold this if we don't need a load either.
4375 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4376 return false;
4377 }
4378
4379 switch (AM.Scale) {
4380 case 0:
4381 case 1:
4382 case 2:
4383 case 4:
4384 case 8:
4385 // These scales always work.
4386 break;
4387 case 3:
4388 case 5:
4389 case 9:
4390 // These scales are formed with basereg+scalereg. Only accept if there is
4391 // no basereg yet.
4392 if (AM.HasBaseReg)
4393 return false;
4394 break;
4395 default: // Other stuff never works.
4396 return false;
4397 }
4398
4399 return true;
4400}
4401
4402
4403/// isShuffleMaskLegal - Targets can use this to indicate that they only
4404/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4405/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4406/// are assumed to be legal.
4407bool
4408X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4409 // Only do shuffles on 128-bit vector types for now.
4410 if (MVT::getSizeInBits(VT) == 64) return false;
4411 return (Mask.Val->getNumOperands() <= 4 ||
4412 isIdentityMask(Mask.Val) ||
4413 isIdentityMask(Mask.Val, true) ||
4414 isSplatMask(Mask.Val) ||
4415 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4416 X86::isUNPCKLMask(Mask.Val) ||
4417 X86::isUNPCKHMask(Mask.Val) ||
4418 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4419 X86::isUNPCKH_v_undef_Mask(Mask.Val));
4420}
4421
4422bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4423 MVT::ValueType EVT,
4424 SelectionDAG &DAG) const {
4425 unsigned NumElts = BVOps.size();
4426 // Only do shuffles on 128-bit vector types for now.
4427 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4428 if (NumElts == 2) return true;
4429 if (NumElts == 4) {
4430 return (isMOVLMask(&BVOps[0], 4) ||
4431 isCommutedMOVL(&BVOps[0], 4, true) ||
4432 isSHUFPMask(&BVOps[0], 4) ||
4433 isCommutedSHUFP(&BVOps[0], 4));
4434 }
4435 return false;
4436}
4437
4438//===----------------------------------------------------------------------===//
4439// X86 Scheduler Hooks
4440//===----------------------------------------------------------------------===//
4441
4442MachineBasicBlock *
4443X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4444 MachineBasicBlock *BB) {
4445 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4446 switch (MI->getOpcode()) {
4447 default: assert(false && "Unexpected instr type to insert");
4448 case X86::CMOV_FR32:
4449 case X86::CMOV_FR64:
4450 case X86::CMOV_V4F32:
4451 case X86::CMOV_V2F64:
4452 case X86::CMOV_V2I64: {
4453 // To "insert" a SELECT_CC instruction, we actually have to insert the
4454 // diamond control-flow pattern. The incoming instruction knows the
4455 // destination vreg to set, the condition code register to branch on, the
4456 // true/false values to select between, and a branch opcode to use.
4457 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4458 ilist<MachineBasicBlock>::iterator It = BB;
4459 ++It;
4460
4461 // thisMBB:
4462 // ...
4463 // TrueVal = ...
4464 // cmpTY ccX, r1, r2
4465 // bCC copy1MBB
4466 // fallthrough --> copy0MBB
4467 MachineBasicBlock *thisMBB = BB;
4468 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4469 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
4470 unsigned Opc =
4471 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
4472 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
4473 MachineFunction *F = BB->getParent();
4474 F->getBasicBlockList().insert(It, copy0MBB);
4475 F->getBasicBlockList().insert(It, sinkMBB);
4476 // Update machine-CFG edges by first adding all successors of the current
4477 // block to the new block which will contain the Phi node for the select.
4478 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
4479 e = BB->succ_end(); i != e; ++i)
4480 sinkMBB->addSuccessor(*i);
4481 // Next, remove all successors of the current block, and add the true
4482 // and fallthrough blocks as its successors.
4483 while(!BB->succ_empty())
4484 BB->removeSuccessor(BB->succ_begin());
4485 BB->addSuccessor(copy0MBB);
4486 BB->addSuccessor(sinkMBB);
4487
4488 // copy0MBB:
4489 // %FalseValue = ...
4490 // # fallthrough to sinkMBB
4491 BB = copy0MBB;
4492
4493 // Update machine-CFG edges
4494 BB->addSuccessor(sinkMBB);
4495
4496 // sinkMBB:
4497 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4498 // ...
4499 BB = sinkMBB;
4500 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
4501 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4502 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4503
4504 delete MI; // The pseudo instruction is gone now.
4505 return BB;
4506 }
4507
4508 case X86::FP32_TO_INT16_IN_MEM:
4509 case X86::FP32_TO_INT32_IN_MEM:
4510 case X86::FP32_TO_INT64_IN_MEM:
4511 case X86::FP64_TO_INT16_IN_MEM:
4512 case X86::FP64_TO_INT32_IN_MEM:
4513 case X86::FP64_TO_INT64_IN_MEM: {
4514 // Change the floating point control register to use "round towards zero"
4515 // mode when truncating to an integer value.
4516 MachineFunction *F = BB->getParent();
4517 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4518 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
4519
4520 // Load the old value of the high byte of the control word...
4521 unsigned OldCW =
4522 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
4523 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
4524
4525 // Set the high part to be round to zero...
4526 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4527 .addImm(0xC7F);
4528
4529 // Reload the modified control word now...
4530 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4531
4532 // Restore the memory image of control word to original value
4533 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4534 .addReg(OldCW);
4535
4536 // Get the X86 opcode to use.
4537 unsigned Opc;
4538 switch (MI->getOpcode()) {
4539 default: assert(0 && "illegal opcode!");
4540 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
4541 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
4542 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
4543 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
4544 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
4545 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
4546 }
4547
4548 X86AddressMode AM;
4549 MachineOperand &Op = MI->getOperand(0);
4550 if (Op.isRegister()) {
4551 AM.BaseType = X86AddressMode::RegBase;
4552 AM.Base.Reg = Op.getReg();
4553 } else {
4554 AM.BaseType = X86AddressMode::FrameIndexBase;
4555 AM.Base.FrameIndex = Op.getFrameIndex();
4556 }
4557 Op = MI->getOperand(1);
4558 if (Op.isImmediate())
4559 AM.Scale = Op.getImm();
4560 Op = MI->getOperand(2);
4561 if (Op.isImmediate())
4562 AM.IndexReg = Op.getImm();
4563 Op = MI->getOperand(3);
4564 if (Op.isGlobalAddress()) {
4565 AM.GV = Op.getGlobal();
4566 } else {
4567 AM.Disp = Op.getImm();
4568 }
4569 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4570 .addReg(MI->getOperand(4).getReg());
4571
4572 // Reload the original control word now.
4573 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
4574
4575 delete MI; // The pseudo instruction is gone now.
4576 return BB;
4577 }
4578 }
4579}
4580
4581//===----------------------------------------------------------------------===//
4582// X86 Optimization Hooks
4583//===----------------------------------------------------------------------===//
4584
4585void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4586 uint64_t Mask,
4587 uint64_t &KnownZero,
4588 uint64_t &KnownOne,
4589 const SelectionDAG &DAG,
4590 unsigned Depth) const {
4591 unsigned Opc = Op.getOpcode();
4592 assert((Opc >= ISD::BUILTIN_OP_END ||
4593 Opc == ISD::INTRINSIC_WO_CHAIN ||
4594 Opc == ISD::INTRINSIC_W_CHAIN ||
4595 Opc == ISD::INTRINSIC_VOID) &&
4596 "Should use MaskedValueIsZero if you don't know whether Op"
4597 " is a target node!");
4598
4599 KnownZero = KnownOne = 0; // Don't know anything.
4600 switch (Opc) {
4601 default: break;
4602 case X86ISD::SETCC:
4603 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4604 break;
4605 }
4606}
4607
4608/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4609/// element of the result of the vector shuffle.
4610static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4611 MVT::ValueType VT = N->getValueType(0);
4612 SDOperand PermMask = N->getOperand(2);
4613 unsigned NumElems = PermMask.getNumOperands();
4614 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4615 i %= NumElems;
4616 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4617 return (i == 0)
4618 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4619 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4620 SDOperand Idx = PermMask.getOperand(i);
4621 if (Idx.getOpcode() == ISD::UNDEF)
4622 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
4623 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4624 }
4625 return SDOperand();
4626}
4627
4628/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4629/// node is a GlobalAddress + an offset.
4630static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
4631 unsigned Opc = N->getOpcode();
4632 if (Opc == X86ISD::Wrapper) {
4633 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4634 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4635 return true;
4636 }
4637 } else if (Opc == ISD::ADD) {
4638 SDOperand N1 = N->getOperand(0);
4639 SDOperand N2 = N->getOperand(1);
4640 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4641 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4642 if (V) {
4643 Offset += V->getSignExtended();
4644 return true;
4645 }
4646 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4647 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4648 if (V) {
4649 Offset += V->getSignExtended();
4650 return true;
4651 }
4652 }
4653 }
4654 return false;
4655}
4656
4657/// isConsecutiveLoad - Returns true if N is loading from an address of Base
4658/// + Dist * Size.
4659static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4660 MachineFrameInfo *MFI) {
4661 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4662 return false;
4663
4664 SDOperand Loc = N->getOperand(1);
4665 SDOperand BaseLoc = Base->getOperand(1);
4666 if (Loc.getOpcode() == ISD::FrameIndex) {
4667 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4668 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00004669 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
4670 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671 int FS = MFI->getObjectSize(FI);
4672 int BFS = MFI->getObjectSize(BFI);
4673 if (FS != BFS || FS != Size) return false;
4674 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4675 } else {
4676 GlobalValue *GV1 = NULL;
4677 GlobalValue *GV2 = NULL;
4678 int64_t Offset1 = 0;
4679 int64_t Offset2 = 0;
4680 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4681 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4682 if (isGA1 && isGA2 && GV1 == GV2)
4683 return Offset1 == (Offset2 + Dist*Size);
4684 }
4685
4686 return false;
4687}
4688
4689static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4690 const X86Subtarget *Subtarget) {
4691 GlobalValue *GV;
4692 int64_t Offset;
4693 if (isGAPlusOffset(Base, GV, Offset))
4694 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4695 else {
4696 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman53491e92007-07-23 20:24:29 +00004697 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698 if (BFI < 0)
4699 // Fixed objects do not specify alignment, however the offsets are known.
4700 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4701 (MFI->getObjectOffset(BFI) % 16) == 0);
4702 else
4703 return MFI->getObjectAlignment(BFI) >= 16;
4704 }
4705 return false;
4706}
4707
4708
4709/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4710/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4711/// if the load addresses are consecutive, non-overlapping, and in the right
4712/// order.
4713static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4714 const X86Subtarget *Subtarget) {
4715 MachineFunction &MF = DAG.getMachineFunction();
4716 MachineFrameInfo *MFI = MF.getFrameInfo();
4717 MVT::ValueType VT = N->getValueType(0);
4718 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4719 SDOperand PermMask = N->getOperand(2);
4720 int NumElems = (int)PermMask.getNumOperands();
4721 SDNode *Base = NULL;
4722 for (int i = 0; i < NumElems; ++i) {
4723 SDOperand Idx = PermMask.getOperand(i);
4724 if (Idx.getOpcode() == ISD::UNDEF) {
4725 if (!Base) return SDOperand();
4726 } else {
4727 SDOperand Arg =
4728 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
4729 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
4730 return SDOperand();
4731 if (!Base)
4732 Base = Arg.Val;
4733 else if (!isConsecutiveLoad(Arg.Val, Base,
4734 i, MVT::getSizeInBits(EVT)/8,MFI))
4735 return SDOperand();
4736 }
4737 }
4738
4739 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00004740 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004742 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00004743 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004744 } else {
Dan Gohman11821702007-07-27 17:16:43 +00004745 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4746 LD->getSrcValueOffset(), LD->isVolatile(),
4747 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748 }
4749}
4750
4751/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4752static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4753 const X86Subtarget *Subtarget) {
4754 SDOperand Cond = N->getOperand(0);
4755
4756 // If we have SSE[12] support, try to form min/max nodes.
4757 if (Subtarget->hasSSE2() &&
4758 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4759 if (Cond.getOpcode() == ISD::SETCC) {
4760 // Get the LHS/RHS of the select.
4761 SDOperand LHS = N->getOperand(1);
4762 SDOperand RHS = N->getOperand(2);
4763 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
4764
4765 unsigned Opcode = 0;
4766 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
4767 switch (CC) {
4768 default: break;
4769 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4770 case ISD::SETULE:
4771 case ISD::SETLE:
4772 if (!UnsafeFPMath) break;
4773 // FALL THROUGH.
4774 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4775 case ISD::SETLT:
4776 Opcode = X86ISD::FMIN;
4777 break;
4778
4779 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4780 case ISD::SETUGT:
4781 case ISD::SETGT:
4782 if (!UnsafeFPMath) break;
4783 // FALL THROUGH.
4784 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4785 case ISD::SETGE:
4786 Opcode = X86ISD::FMAX;
4787 break;
4788 }
4789 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
4790 switch (CC) {
4791 default: break;
4792 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4793 case ISD::SETUGT:
4794 case ISD::SETGT:
4795 if (!UnsafeFPMath) break;
4796 // FALL THROUGH.
4797 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4798 case ISD::SETGE:
4799 Opcode = X86ISD::FMIN;
4800 break;
4801
4802 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4803 case ISD::SETULE:
4804 case ISD::SETLE:
4805 if (!UnsafeFPMath) break;
4806 // FALL THROUGH.
4807 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4808 case ISD::SETLT:
4809 Opcode = X86ISD::FMAX;
4810 break;
4811 }
4812 }
4813
4814 if (Opcode)
4815 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
4816 }
4817
4818 }
4819
4820 return SDOperand();
4821}
4822
4823
4824SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
4825 DAGCombinerInfo &DCI) const {
4826 SelectionDAG &DAG = DCI.DAG;
4827 switch (N->getOpcode()) {
4828 default: break;
4829 case ISD::VECTOR_SHUFFLE:
4830 return PerformShuffleCombine(N, DAG, Subtarget);
4831 case ISD::SELECT:
4832 return PerformSELECTCombine(N, DAG, Subtarget);
4833 }
4834
4835 return SDOperand();
4836}
4837
4838//===----------------------------------------------------------------------===//
4839// X86 Inline Assembly Support
4840//===----------------------------------------------------------------------===//
4841
4842/// getConstraintType - Given a constraint letter, return the type of
4843/// constraint it is for this target.
4844X86TargetLowering::ConstraintType
4845X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4846 if (Constraint.size() == 1) {
4847 switch (Constraint[0]) {
4848 case 'A':
4849 case 'r':
4850 case 'R':
4851 case 'l':
4852 case 'q':
4853 case 'Q':
4854 case 'x':
4855 case 'Y':
4856 return C_RegisterClass;
4857 default:
4858 break;
4859 }
4860 }
4861 return TargetLowering::getConstraintType(Constraint);
4862}
4863
4864/// isOperandValidForConstraint - Return the specified operand (possibly
4865/// modified) if the specified SDOperand is valid for the specified target
4866/// constraint letter, otherwise return null.
4867SDOperand X86TargetLowering::
4868isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4869 switch (Constraint) {
4870 default: break;
4871 case 'I':
4872 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4873 if (C->getValue() <= 31)
4874 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
4875 }
4876 return SDOperand(0,0);
4877 case 'N':
4878 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4879 if (C->getValue() <= 255)
4880 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
4881 }
4882 return SDOperand(0,0);
4883 case 'i': {
4884 // Literal immediates are always ok.
4885 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op))
4886 return DAG.getTargetConstant(CST->getValue(), Op.getValueType());
4887
4888 // If we are in non-pic codegen mode, we allow the address of a global (with
4889 // an optional displacement) to be used with 'i'.
4890 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
4891 int64_t Offset = 0;
4892
4893 // Match either (GA) or (GA+C)
4894 if (GA) {
4895 Offset = GA->getOffset();
4896 } else if (Op.getOpcode() == ISD::ADD) {
4897 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4898 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4899 if (C && GA) {
4900 Offset = GA->getOffset()+C->getValue();
4901 } else {
4902 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4903 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4904 if (C && GA)
4905 Offset = GA->getOffset()+C->getValue();
4906 else
4907 C = 0, GA = 0;
4908 }
4909 }
4910
4911 if (GA) {
4912 // If addressing this global requires a load (e.g. in PIC mode), we can't
4913 // match.
4914 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
4915 false))
4916 return SDOperand(0, 0);
4917
4918 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4919 Offset);
4920 return Op;
4921 }
4922
4923 // Otherwise, not valid for this mode.
4924 return SDOperand(0, 0);
4925 }
4926 }
4927 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
4928}
4929
4930std::vector<unsigned> X86TargetLowering::
4931getRegClassForInlineAsmConstraint(const std::string &Constraint,
4932 MVT::ValueType VT) const {
4933 if (Constraint.size() == 1) {
4934 // FIXME: not handling fp-stack yet!
4935 switch (Constraint[0]) { // GCC X86 Constraint Letters
4936 default: break; // Unknown constraint letter
4937 case 'A': // EAX/EDX
4938 if (VT == MVT::i32 || VT == MVT::i64)
4939 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4940 break;
4941 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4942 case 'Q': // Q_REGS
4943 if (VT == MVT::i32)
4944 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4945 else if (VT == MVT::i16)
4946 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4947 else if (VT == MVT::i8)
4948 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4949 break;
4950 }
4951 }
4952
4953 return std::vector<unsigned>();
4954}
4955
4956std::pair<unsigned, const TargetRegisterClass*>
4957X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4958 MVT::ValueType VT) const {
4959 // First, see if this is a constraint that directly corresponds to an LLVM
4960 // register class.
4961 if (Constraint.size() == 1) {
4962 // GCC Constraint Letters
4963 switch (Constraint[0]) {
4964 default: break;
4965 case 'r': // GENERAL_REGS
4966 case 'R': // LEGACY_REGS
4967 case 'l': // INDEX_REGS
4968 if (VT == MVT::i64 && Subtarget->is64Bit())
4969 return std::make_pair(0U, X86::GR64RegisterClass);
4970 if (VT == MVT::i32)
4971 return std::make_pair(0U, X86::GR32RegisterClass);
4972 else if (VT == MVT::i16)
4973 return std::make_pair(0U, X86::GR16RegisterClass);
4974 else if (VT == MVT::i8)
4975 return std::make_pair(0U, X86::GR8RegisterClass);
4976 break;
4977 case 'y': // MMX_REGS if MMX allowed.
4978 if (!Subtarget->hasMMX()) break;
4979 return std::make_pair(0U, X86::VR64RegisterClass);
4980 break;
4981 case 'Y': // SSE_REGS if SSE2 allowed
4982 if (!Subtarget->hasSSE2()) break;
4983 // FALL THROUGH.
4984 case 'x': // SSE_REGS if SSE1 allowed
4985 if (!Subtarget->hasSSE1()) break;
4986
4987 switch (VT) {
4988 default: break;
4989 // Scalar SSE types.
4990 case MVT::f32:
4991 case MVT::i32:
4992 return std::make_pair(0U, X86::FR32RegisterClass);
4993 case MVT::f64:
4994 case MVT::i64:
4995 return std::make_pair(0U, X86::FR64RegisterClass);
4996 // Vector types.
4997 case MVT::v16i8:
4998 case MVT::v8i16:
4999 case MVT::v4i32:
5000 case MVT::v2i64:
5001 case MVT::v4f32:
5002 case MVT::v2f64:
5003 return std::make_pair(0U, X86::VR128RegisterClass);
5004 }
5005 break;
5006 }
5007 }
5008
5009 // Use the default implementation in TargetLowering to convert the register
5010 // constraint into a member of a register class.
5011 std::pair<unsigned, const TargetRegisterClass*> Res;
5012 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5013
5014 // Not found as a standard register?
5015 if (Res.second == 0) {
5016 // GCC calls "st(0)" just plain "st".
5017 if (StringsEqualNoCase("{st}", Constraint)) {
5018 Res.first = X86::ST0;
5019 Res.second = X86::RSTRegisterClass;
5020 }
5021
5022 return Res;
5023 }
5024
5025 // Otherwise, check to see if this is a register class of the wrong value
5026 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5027 // turn into {ax},{dx}.
5028 if (Res.second->hasType(VT))
5029 return Res; // Correct type already, nothing to do.
5030
5031 // All of the single-register GCC register classes map their values onto
5032 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5033 // really want an 8-bit or 32-bit register, map to the appropriate register
5034 // class and return the appropriate register.
5035 if (Res.second != X86::GR16RegisterClass)
5036 return Res;
5037
5038 if (VT == MVT::i8) {
5039 unsigned DestReg = 0;
5040 switch (Res.first) {
5041 default: break;
5042 case X86::AX: DestReg = X86::AL; break;
5043 case X86::DX: DestReg = X86::DL; break;
5044 case X86::CX: DestReg = X86::CL; break;
5045 case X86::BX: DestReg = X86::BL; break;
5046 }
5047 if (DestReg) {
5048 Res.first = DestReg;
5049 Res.second = Res.second = X86::GR8RegisterClass;
5050 }
5051 } else if (VT == MVT::i32) {
5052 unsigned DestReg = 0;
5053 switch (Res.first) {
5054 default: break;
5055 case X86::AX: DestReg = X86::EAX; break;
5056 case X86::DX: DestReg = X86::EDX; break;
5057 case X86::CX: DestReg = X86::ECX; break;
5058 case X86::BX: DestReg = X86::EBX; break;
5059 case X86::SI: DestReg = X86::ESI; break;
5060 case X86::DI: DestReg = X86::EDI; break;
5061 case X86::BP: DestReg = X86::EBP; break;
5062 case X86::SP: DestReg = X86::ESP; break;
5063 }
5064 if (DestReg) {
5065 Res.first = DestReg;
5066 Res.second = Res.second = X86::GR32RegisterClass;
5067 }
5068 } else if (VT == MVT::i64) {
5069 unsigned DestReg = 0;
5070 switch (Res.first) {
5071 default: break;
5072 case X86::AX: DestReg = X86::RAX; break;
5073 case X86::DX: DestReg = X86::RDX; break;
5074 case X86::CX: DestReg = X86::RCX; break;
5075 case X86::BX: DestReg = X86::RBX; break;
5076 case X86::SI: DestReg = X86::RSI; break;
5077 case X86::DI: DestReg = X86::RDI; break;
5078 case X86::BP: DestReg = X86::RBP; break;
5079 case X86::SP: DestReg = X86::RSP; break;
5080 }
5081 if (DestReg) {
5082 Res.first = DestReg;
5083 Res.second = Res.second = X86::GR64RegisterClass;
5084 }
5085 }
5086
5087 return Res;
5088}