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