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