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