blob: 8767d8d33b97d981b83cca41eeb957054b142db6 [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 Gohmanf17a25c2007-07-18 16:29:46 +0000443 // First set operation action for all vector types to expand. Then we
444 // will selectively turn on ones that can be effectively codegen'd.
445 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
446 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
447 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
448 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
449 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
450 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
451 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
452 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
453 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
454 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
455 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
456 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
457 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
458 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
459 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
460 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
461 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
464 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
465 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
466 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
467 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
468 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
469 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000470 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
471 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 }
475
476 if (Subtarget->hasMMX()) {
477 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
478 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
479 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
480 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
481
482 // FIXME: add MMX packed arithmetics
483
484 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
485 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
486 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
487 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
488
489 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
490 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
491 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
492
493 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
494 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
495
496 setOperationAction(ISD::AND, MVT::v8i8, Promote);
497 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
498 setOperationAction(ISD::AND, MVT::v4i16, Promote);
499 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
500 setOperationAction(ISD::AND, MVT::v2i32, Promote);
501 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
502 setOperationAction(ISD::AND, MVT::v1i64, Legal);
503
504 setOperationAction(ISD::OR, MVT::v8i8, Promote);
505 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
506 setOperationAction(ISD::OR, MVT::v4i16, Promote);
507 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
508 setOperationAction(ISD::OR, MVT::v2i32, Promote);
509 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
510 setOperationAction(ISD::OR, MVT::v1i64, Legal);
511
512 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
513 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
514 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
515 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
516 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
517 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
518 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
519
520 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
521 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
522 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
523 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
524 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
525 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
526 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
527
528 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
529 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
530 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
531 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
532
533 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
534 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
535 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
536 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
537
538 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
539 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
540 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
541 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
542 }
543
544 if (Subtarget->hasSSE1()) {
545 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
546
547 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
548 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
549 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
550 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
551 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
552 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
554 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
555 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
556 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
557 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
558 }
559
560 if (Subtarget->hasSSE2()) {
561 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
562 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
563 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
564 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
565 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
566
567 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
568 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
569 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
570 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
571 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
572 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
573 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
574 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
575 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
576 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
577 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
578 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
579 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
580 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
581 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582
583 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
584 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
585 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
586 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
587 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
588 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
589
590 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
591 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
592 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
593 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
594 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
595 }
596 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
597 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
598 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
599 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
600 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
601 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
602
603 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
604 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
605 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
606 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
607 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
608 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
609 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
610 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
611 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
612 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
613 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
614 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
615 }
616
617 // Custom lower v2i64 and v2f64 selects.
618 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
619 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
620 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
621 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
622 }
623
624 // We want to custom lower some of our intrinsics.
625 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
626
627 // We have target-specific dag combine patterns for the following nodes:
628 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
629 setTargetDAGCombine(ISD::SELECT);
630
631 computeRegisterProperties();
632
633 // FIXME: These should be based on subtarget info. Plus, the values should
634 // be smaller when we are in optimizing for size mode.
635 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
636 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
637 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
638 allowUnalignedMemoryAccesses = true; // x86 supports it!
639}
640
641
642//===----------------------------------------------------------------------===//
643// Return Value Calling Convention Implementation
644//===----------------------------------------------------------------------===//
645
646#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000647
648/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
649/// exists skip possible ISD:TokenFactor.
650static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
651 if (Chain.getOpcode()==X86ISD::TAILCALL) {
652 return Chain;
653 } else if (Chain.getOpcode()==ISD::TokenFactor) {
654 if (Chain.getNumOperands() &&
655 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
656 return Chain.getOperand(0);
657 }
658 return Chain;
659}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660
661/// LowerRET - Lower an ISD::RET node.
662SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
663 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
664
665 SmallVector<CCValAssign, 16> RVLocs;
666 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
667 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
668 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
669 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000670
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 // If this is the first return lowered for this function, add the regs to the
672 // liveout set for the function.
673 if (DAG.getMachineFunction().liveout_empty()) {
674 for (unsigned i = 0; i != RVLocs.size(); ++i)
675 if (RVLocs[i].isRegLoc())
676 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
677 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000680 // Handle tail call return.
681 Chain = GetPossiblePreceedingTailCall(Chain);
682 if (Chain.getOpcode() == X86ISD::TAILCALL) {
683 SDOperand TailCall = Chain;
684 SDOperand TargetAddress = TailCall.getOperand(1);
685 SDOperand StackAdjustment = TailCall.getOperand(2);
686 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
687 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
688 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
689 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
690 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
691 "Expecting an global address, external symbol, or register");
692 assert( StackAdjustment.getOpcode() == ISD::Constant &&
693 "Expecting a const value");
694
695 SmallVector<SDOperand,8> Operands;
696 Operands.push_back(Chain.getOperand(0));
697 Operands.push_back(TargetAddress);
698 Operands.push_back(StackAdjustment);
699 // Copy registers used by the call. Last operand is a flag so it is not
700 // copied.
701 for(unsigned i=3; i < TailCall.getNumOperands()-1;i++) {
702 Operands.push_back(Chain.getOperand(i));
703 }
704 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0], Operands.size());
705 }
706
707 // Regular return.
708 SDOperand Flag;
709
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 // Copy the result values into the output registers.
711 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
712 RVLocs[0].getLocReg() != X86::ST0) {
713 for (unsigned i = 0; i != RVLocs.size(); ++i) {
714 CCValAssign &VA = RVLocs[i];
715 assert(VA.isRegLoc() && "Can only return in registers!");
716 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
717 Flag);
718 Flag = Chain.getValue(1);
719 }
720 } else {
721 // We need to handle a destination of ST0 specially, because it isn't really
722 // a register.
723 SDOperand Value = Op.getOperand(1);
724
725 // If this is an FP return with ScalarSSE, we need to move the value from
726 // an XMM register onto the fp-stack.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000727 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
728 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 SDOperand MemLoc;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000730
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 // If this is a load into a scalarsse value, don't store the loaded value
732 // back to the stack, only to reload it: just replace the scalar-sse load.
733 if (ISD::isNON_EXTLoad(Value.Val) &&
734 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
735 Chain = Value.getOperand(0);
736 MemLoc = Value.getOperand(1);
737 } else {
738 // Spill the value to memory and reload it into top of stack.
739 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
740 MachineFunction &MF = DAG.getMachineFunction();
741 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
742 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
743 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
744 }
745 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
746 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
747 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
748 Chain = Value.getValue(1);
749 }
750
751 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
752 SDOperand Ops[] = { Chain, Value };
753 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
754 Flag = Chain.getValue(1);
755 }
756
757 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
758 if (Flag.Val)
759 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
760 else
761 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
762}
763
764
765/// LowerCallResult - Lower the result values of an ISD::CALL into the
766/// appropriate copies out of appropriate physical registers. This assumes that
767/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
768/// being lowered. The returns a SDNode with the same number of values as the
769/// ISD::CALL.
770SDNode *X86TargetLowering::
771LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
772 unsigned CallingConv, SelectionDAG &DAG) {
773
774 // Assign locations to each value returned by this call.
775 SmallVector<CCValAssign, 16> RVLocs;
776 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
777 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
778 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
779
780
781 SmallVector<SDOperand, 8> ResultVals;
782
783 // Copy all of the result registers out of their specified physreg.
784 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
785 for (unsigned i = 0; i != RVLocs.size(); ++i) {
786 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
787 RVLocs[i].getValVT(), InFlag).getValue(1);
788 InFlag = Chain.getValue(2);
789 ResultVals.push_back(Chain.getValue(0));
790 }
791 } else {
792 // Copies from the FP stack are special, as ST0 isn't a valid register
793 // before the fp stackifier runs.
794
795 // Copy ST0 into an RFP register with FP_GET_RESULT.
796 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
797 SDOperand GROps[] = { Chain, InFlag };
798 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
799 Chain = RetVal.getValue(1);
800 InFlag = RetVal.getValue(2);
801
802 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
803 // an XMM register.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000804 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
805 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
807 // shouldn't be necessary except that RFP cannot be live across
808 // multiple blocks. When stackifier is fixed, they can be uncoupled.
809 MachineFunction &MF = DAG.getMachineFunction();
810 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
811 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
812 SDOperand Ops[] = {
813 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
814 };
815 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
816 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
817 Chain = RetVal.getValue(1);
818 }
819 ResultVals.push_back(RetVal);
820 }
821
822 // Merge everything together with a MERGE_VALUES node.
823 ResultVals.push_back(Chain);
824 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
825 &ResultVals[0], ResultVals.size()).Val;
826}
827
828
829//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000830// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831//===----------------------------------------------------------------------===//
832// StdCall calling convention seems to be standard for many Windows' API
833// routines and around. It differs from C calling convention just a little:
834// callee should clean up the stack, not caller. Symbols should be also
835// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000836// For info on fast calling convention see Fast Calling Convention (tail call)
837// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838
839/// AddLiveIn - This helper function adds the specified physical register to the
840/// MachineFunction as a live in value. It also creates a corresponding virtual
841/// register for it.
842static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
843 const TargetRegisterClass *RC) {
844 assert(RC->contains(PReg) && "Not the correct regclass!");
845 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
846 MF.addLiveIn(PReg, VReg);
847 return VReg;
848}
849
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000850// align stack arguments according to platform alignment needed for tail calls
851unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
852
Rafael Espindola03cbeb72007-09-14 15:48:13 +0000853SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
854 const CCValAssign &VA,
855 MachineFrameInfo *MFI,
856 SDOperand Root, unsigned i) {
857 // Create the nodes corresponding to a load from this parameter slot.
858 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
859 VA.getLocMemOffset());
860 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
861
862 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
863
864 if (Flags & ISD::ParamFlags::ByVal)
865 return FIN;
866 else
867 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
868}
869
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
871 bool isStdCall) {
872 unsigned NumArgs = Op.Val->getNumValues() - 1;
873 MachineFunction &MF = DAG.getMachineFunction();
874 MachineFrameInfo *MFI = MF.getFrameInfo();
875 SDOperand Root = Op.getOperand(0);
876 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000877 unsigned CC = MF.getFunction()->getCallingConv();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 // Assign locations to all of the incoming arguments.
879 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000880 CCState CCInfo(CC, isVarArg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 getTargetMachine(), ArgLocs);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000882 // Check for possible tail call calling convention.
883 if (CC == CallingConv::Fast && PerformTailCallOpt)
884 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
885 else
886 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
887
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 SmallVector<SDOperand, 8> ArgValues;
889 unsigned LastVal = ~0U;
890 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
891 CCValAssign &VA = ArgLocs[i];
892 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
893 // places.
894 assert(VA.getValNo() != LastVal &&
895 "Don't support value assigned to multiple locs yet");
896 LastVal = VA.getValNo();
897
898 if (VA.isRegLoc()) {
899 MVT::ValueType RegVT = VA.getLocVT();
900 TargetRegisterClass *RC;
901 if (RegVT == MVT::i32)
902 RC = X86::GR32RegisterClass;
903 else {
904 assert(MVT::isVector(RegVT));
905 RC = X86::VR128RegisterClass;
906 }
907
908 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
909 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
910
911 // If this is an 8 or 16-bit value, it is really passed promoted to 32
912 // bits. Insert an assert[sz]ext to capture this, then truncate to the
913 // right size.
914 if (VA.getLocInfo() == CCValAssign::SExt)
915 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
916 DAG.getValueType(VA.getValVT()));
917 else if (VA.getLocInfo() == CCValAssign::ZExt)
918 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
919 DAG.getValueType(VA.getValVT()));
920
921 if (VA.getLocInfo() != CCValAssign::Full)
922 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
923
924 ArgValues.push_back(ArgValue);
925 } else {
926 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +0000927 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 }
929 }
930
931 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000932 // align stack specially for tail calls
933 if (CC==CallingConv::Fast)
934 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935
936 ArgValues.push_back(Root);
937
938 // If the function takes variable number of arguments, make a frame index for
939 // the start of the first vararg value... for expansion of llvm.va_start.
940 if (isVarArg)
941 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
942
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000943 // Tail call calling convention (CallingConv::Fast) does not support varargs.
944 assert( !(isVarArg && CC == CallingConv::Fast) &&
945 "CallingConv::Fast does not support varargs.");
946
947 if (isStdCall && !isVarArg &&
948 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 BytesToPopOnReturn = StackSize; // Callee pops everything..
950 BytesCallerReserves = 0;
951 } else {
952 BytesToPopOnReturn = 0; // Callee pops nothing.
953
954 // If this is an sret function, the return should pop the hidden pointer.
955 if (NumArgs &&
956 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
957 ISD::ParamFlags::StructReturn))
958 BytesToPopOnReturn = 4;
959
960 BytesCallerReserves = StackSize;
961 }
Anton Korobeynikove844e472007-08-15 17:12:32 +0000962
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964
Anton Korobeynikove844e472007-08-15 17:12:32 +0000965 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
966 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967
968 // Return the new list of results.
969 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
970 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
971}
972
973SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
974 unsigned CC) {
975 SDOperand Chain = Op.getOperand(0);
976 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 SDOperand Callee = Op.getOperand(4);
978 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000979
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980 // Analyze operands of the call, assigning locations to each operand.
981 SmallVector<CCValAssign, 16> ArgLocs;
982 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000983 if(CC==CallingConv::Fast && PerformTailCallOpt)
984 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
985 else
986 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987
988 // Get a count of how many bytes are to be pushed on the stack.
989 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000990 if (CC==CallingConv::Fast)
991 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992
993 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
994
995 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
996 SmallVector<SDOperand, 8> MemOpChains;
997
998 SDOperand StackPtr;
999
1000 // Walk the register/memloc assignments, inserting copies/loads.
1001 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1002 CCValAssign &VA = ArgLocs[i];
1003 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1004
1005 // Promote the value if needed.
1006 switch (VA.getLocInfo()) {
1007 default: assert(0 && "Unknown loc info!");
1008 case CCValAssign::Full: break;
1009 case CCValAssign::SExt:
1010 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1011 break;
1012 case CCValAssign::ZExt:
1013 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1014 break;
1015 case CCValAssign::AExt:
1016 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1017 break;
1018 }
1019
1020 if (VA.isRegLoc()) {
1021 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1022 } else {
1023 assert(VA.isMemLoc());
1024 if (StackPtr.Val == 0)
1025 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola007b7142007-09-21 15:50:22 +00001026
1027 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1028 Arg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 }
1030 }
1031
1032 // If the first argument is an sret pointer, remember it.
1033 bool isSRet = NumOps &&
1034 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
1035 ISD::ParamFlags::StructReturn);
1036
1037 if (!MemOpChains.empty())
1038 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1039 &MemOpChains[0], MemOpChains.size());
1040
1041 // Build a sequence of copy-to-reg nodes chained together with token chain
1042 // and flag operands which copy the outgoing args into registers.
1043 SDOperand InFlag;
1044 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1045 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1046 InFlag);
1047 InFlag = Chain.getValue(1);
1048 }
1049
1050 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1051 // GOT pointer.
1052 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1053 Subtarget->isPICStyleGOT()) {
1054 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1055 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1056 InFlag);
1057 InFlag = Chain.getValue(1);
1058 }
1059
1060 // If the callee is a GlobalAddress node (quite common, every direct call is)
1061 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1062 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1063 // We should use extra load for direct calls to dllimported functions in
1064 // non-JIT mode.
1065 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1066 getTargetMachine(), true))
1067 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1068 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1069 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1070
1071 // Returns a chain & a flag for retval copy to use.
1072 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1073 SmallVector<SDOperand, 8> Ops;
1074 Ops.push_back(Chain);
1075 Ops.push_back(Callee);
1076
1077 // Add argument registers to the end of the list so that they are known live
1078 // into the call.
1079 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1080 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1081 RegsToPass[i].second.getValueType()));
1082
1083 // Add an implicit use GOT pointer in EBX.
1084 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1085 Subtarget->isPICStyleGOT())
1086 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1087
1088 if (InFlag.Val)
1089 Ops.push_back(InFlag);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001090
1091 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 InFlag = Chain.getValue(1);
1093
1094 // Create the CALLSEQ_END node.
1095 unsigned NumBytesForCalleeToPush = 0;
1096
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001097 if (CC == CallingConv::X86_StdCall ||
1098 (CC == CallingConv::Fast && PerformTailCallOpt)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 if (isVarArg)
1100 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1101 else
1102 NumBytesForCalleeToPush = NumBytes;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001103 assert(!(isVarArg && CC==CallingConv::Fast) &&
1104 "CallingConv::Fast does not support varargs.");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 } else {
1106 // If this is is a call to a struct-return function, the callee
1107 // pops the hidden struct pointer, so we have to push it back.
1108 // This is common for Darwin/X86, Linux & Mingw32 targets.
1109 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1110 }
1111
1112 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1113 Ops.clear();
1114 Ops.push_back(Chain);
1115 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1116 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
1117 Ops.push_back(InFlag);
1118 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1119 InFlag = Chain.getValue(1);
1120
1121 // Handle result values, copying them out of physregs into vregs that we
1122 // return.
1123 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1124}
1125
1126
1127//===----------------------------------------------------------------------===//
1128// FastCall Calling Convention implementation
1129//===----------------------------------------------------------------------===//
1130//
1131// The X86 'fastcall' calling convention passes up to two integer arguments in
1132// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1133// and requires that the callee pop its arguments off the stack (allowing proper
1134// tail calls), and has the same return value conventions as C calling convs.
1135//
1136// This calling convention always arranges for the callee pop value to be 8n+4
1137// bytes, which is needed for tail recursion elimination and stack alignment
1138// reasons.
1139SDOperand
1140X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1141 MachineFunction &MF = DAG.getMachineFunction();
1142 MachineFrameInfo *MFI = MF.getFrameInfo();
1143 SDOperand Root = Op.getOperand(0);
1144 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1145
1146 // Assign locations to all of the incoming arguments.
1147 SmallVector<CCValAssign, 16> ArgLocs;
1148 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1149 getTargetMachine(), ArgLocs);
1150 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
1151
1152 SmallVector<SDOperand, 8> ArgValues;
1153 unsigned LastVal = ~0U;
1154 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1155 CCValAssign &VA = ArgLocs[i];
1156 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1157 // places.
1158 assert(VA.getValNo() != LastVal &&
1159 "Don't support value assigned to multiple locs yet");
1160 LastVal = VA.getValNo();
1161
1162 if (VA.isRegLoc()) {
1163 MVT::ValueType RegVT = VA.getLocVT();
1164 TargetRegisterClass *RC;
1165 if (RegVT == MVT::i32)
1166 RC = X86::GR32RegisterClass;
1167 else {
1168 assert(MVT::isVector(RegVT));
1169 RC = X86::VR128RegisterClass;
1170 }
1171
1172 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1173 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1174
1175 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1176 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1177 // right size.
1178 if (VA.getLocInfo() == CCValAssign::SExt)
1179 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1180 DAG.getValueType(VA.getValVT()));
1181 else if (VA.getLocInfo() == CCValAssign::ZExt)
1182 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1183 DAG.getValueType(VA.getValVT()));
1184
1185 if (VA.getLocInfo() != CCValAssign::Full)
1186 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1187
1188 ArgValues.push_back(ArgValue);
1189 } else {
1190 assert(VA.isMemLoc());
Rafael Espindolab53ef122007-09-21 14:55:38 +00001191 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192 }
1193 }
1194
1195 ArgValues.push_back(Root);
1196
1197 unsigned StackSize = CCInfo.getNextStackOffset();
1198
1199 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1200 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001201 // arguments and the arguments after the retaddr has been pushed are
1202 // aligned.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 if ((StackSize & 7) == 0)
1204 StackSize += 4;
1205 }
1206
1207 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1208 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
1210 BytesCallerReserves = 0;
1211
Anton Korobeynikove844e472007-08-15 17:12:32 +00001212 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1213 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214
1215 // Return the new list of results.
1216 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1217 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1218}
1219
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001220SDOperand
1221X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1222 const SDOperand &StackPtr,
1223 const CCValAssign &VA,
1224 SDOperand Chain,
1225 SDOperand Arg) {
1226 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1227 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1228 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1229 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1230 if (Flags & ISD::ParamFlags::ByVal) {
1231 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1232 ISD::ParamFlags::ByValAlignOffs);
1233
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001234 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1235 ISD::ParamFlags::ByValSizeOffs;
1236
1237 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1238 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1239
1240 return DAG.getNode(ISD::MEMCPY, MVT::Other, Chain, PtrOff, Arg, SizeNode,
1241 AlignNode);
1242 } else {
1243 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1244 }
1245}
1246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1248 unsigned CC) {
1249 SDOperand Chain = Op.getOperand(0);
1250 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1251 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1252 SDOperand Callee = Op.getOperand(4);
1253
1254 // Analyze operands of the call, assigning locations to each operand.
1255 SmallVector<CCValAssign, 16> ArgLocs;
1256 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1257 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
1258
1259 // Get a count of how many bytes are to be pushed on the stack.
1260 unsigned NumBytes = CCInfo.getNextStackOffset();
1261
1262 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1263 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001264 // arguments and the arguments after the retaddr has been pushed are
1265 // aligned.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 if ((NumBytes & 7) == 0)
1267 NumBytes += 4;
1268 }
1269
1270 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1271
1272 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1273 SmallVector<SDOperand, 8> MemOpChains;
1274
1275 SDOperand StackPtr;
1276
1277 // Walk the register/memloc assignments, inserting copies/loads.
1278 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1279 CCValAssign &VA = ArgLocs[i];
1280 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1281
1282 // Promote the value if needed.
1283 switch (VA.getLocInfo()) {
1284 default: assert(0 && "Unknown loc info!");
1285 case CCValAssign::Full: break;
1286 case CCValAssign::SExt:
1287 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1288 break;
1289 case CCValAssign::ZExt:
1290 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1291 break;
1292 case CCValAssign::AExt:
1293 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1294 break;
1295 }
1296
1297 if (VA.isRegLoc()) {
1298 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1299 } else {
1300 assert(VA.isMemLoc());
1301 if (StackPtr.Val == 0)
1302 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola007b7142007-09-21 15:50:22 +00001303
1304 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1305 Arg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 }
1307 }
1308
1309 if (!MemOpChains.empty())
1310 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1311 &MemOpChains[0], MemOpChains.size());
1312
1313 // Build a sequence of copy-to-reg nodes chained together with token chain
1314 // and flag operands which copy the outgoing args into registers.
1315 SDOperand InFlag;
1316 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1317 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1318 InFlag);
1319 InFlag = Chain.getValue(1);
1320 }
1321
1322 // If the callee is a GlobalAddress node (quite common, every direct call is)
1323 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1324 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1325 // We should use extra load for direct calls to dllimported functions in
1326 // non-JIT mode.
1327 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1328 getTargetMachine(), true))
1329 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1330 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1331 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1332
1333 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1334 // GOT pointer.
1335 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1336 Subtarget->isPICStyleGOT()) {
1337 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1338 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1339 InFlag);
1340 InFlag = Chain.getValue(1);
1341 }
1342
1343 // Returns a chain & a flag for retval copy to use.
1344 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1345 SmallVector<SDOperand, 8> Ops;
1346 Ops.push_back(Chain);
1347 Ops.push_back(Callee);
1348
1349 // Add argument registers to the end of the list so that they are known live
1350 // into the call.
1351 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1352 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1353 RegsToPass[i].second.getValueType()));
1354
1355 // Add an implicit use GOT pointer in EBX.
1356 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1357 Subtarget->isPICStyleGOT())
1358 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1359
1360 if (InFlag.Val)
1361 Ops.push_back(InFlag);
1362
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001363 assert(isTailCall==false && "no tail call here");
1364 Chain = DAG.getNode(X86ISD::CALL,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 NodeTys, &Ops[0], Ops.size());
1366 InFlag = Chain.getValue(1);
1367
1368 // Returns a flag for retval copy to use.
1369 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1370 Ops.clear();
1371 Ops.push_back(Chain);
1372 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1373 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1374 Ops.push_back(InFlag);
1375 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1376 InFlag = Chain.getValue(1);
1377
1378 // Handle result values, copying them out of physregs into vregs that we
1379 // return.
1380 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1381}
1382
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001383//===----------------------------------------------------------------------===//
1384// Fast Calling Convention (tail call) implementation
1385//===----------------------------------------------------------------------===//
1386
1387// Like std call, callee cleans arguments, convention except that ECX is
1388// reserved for storing the tail called function address. Only 2 registers are
1389// free for argument passing (inreg). Tail call optimization is performed
1390// provided:
1391// * tailcallopt is enabled
1392// * caller/callee are fastcc
1393// * elf/pic is disabled OR
1394// * elf/pic enabled + callee is in module + callee has
1395// visibility protected or hidden
1396// To ensure the stack is aligned according to platform abi pass
1397// tail-call-align-stack. This makes sure that argument delta is always
1398// multiples of stack alignment. (Dynamic linkers need this - darwin's dyld for
1399// example)
1400// If a tail called function callee has more arguments than the caller the
1401// caller needs to make sure that there is room to move the RETADDR to. This is
1402// achived by reserving an area the size of the argument delta right after the
1403// original REtADDR, but before the saved framepointer or the spilled registers
1404// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1405// stack layout:
1406// arg1
1407// arg2
1408// RETADDR
1409// [ new RETADDR
1410// move area ]
1411// (possible EBP)
1412// ESI
1413// EDI
1414// local1 ..
1415
1416/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1417/// for a 16 byte align requirement.
1418unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1419 SelectionDAG& DAG) {
1420 if (PerformTailCallOpt) {
1421 MachineFunction &MF = DAG.getMachineFunction();
1422 const TargetMachine &TM = MF.getTarget();
1423 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1424 unsigned StackAlignment = TFI.getStackAlignment();
1425 uint64_t AlignMask = StackAlignment - 1;
1426 int64_t Offset = StackSize;
1427 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1428 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1429 // Number smaller than 12 so just add the difference.
1430 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1431 } else {
1432 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1433 Offset = ((~AlignMask) & Offset) + StackAlignment +
1434 (StackAlignment-SlotSize);
1435 }
1436 StackSize = Offset;
1437 }
1438 return StackSize;
1439}
1440
1441/// IsEligibleForTailCallElimination - Check to see whether the next instruction
1442// following the call is a return. A function is eligible if caller/callee
1443// calling conventions match, currently only fastcc supports tail calls, and the
1444// function CALL is immediatly followed by a RET.
1445bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1446 SDOperand Ret,
1447 SelectionDAG& DAG) const {
1448 bool IsEligible = false;
1449
1450 // Check whether CALL node immediatly preceeds the RET node and whether the
1451 // return uses the result of the node or is a void return.
1452 if ((Ret.getNumOperands() == 1 &&
1453 (Ret.getOperand(0)== SDOperand(Call.Val,1) ||
1454 Ret.getOperand(0)== SDOperand(Call.Val,0))) ||
1455 (Ret.getOperand(0)== SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1456 Ret.getOperand(1)== SDOperand(Call.Val,0))) {
1457 MachineFunction &MF = DAG.getMachineFunction();
1458 unsigned CallerCC = MF.getFunction()->getCallingConv();
1459 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1460 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1461 SDOperand Callee = Call.getOperand(4);
1462 // On elf/pic %ebx needs to be livein.
1463 if(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1464 Subtarget->isPICStyleGOT()) {
1465 // Can only do local tail calls with PIC.
1466 GlobalValue * GV = 0;
1467 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1468 if(G != 0 &&
1469 (GV = G->getGlobal()) &&
1470 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1471 IsEligible=true;
1472 } else {
1473 IsEligible=true;
1474 }
1475 }
1476 }
1477 return IsEligible;
1478}
1479
1480SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1481 SelectionDAG &DAG,
1482 unsigned CC) {
1483 SDOperand Chain = Op.getOperand(0);
1484 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1485 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1486 SDOperand Callee = Op.getOperand(4);
1487 bool is64Bit = Subtarget->is64Bit();
1488
1489 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1490
1491 // Analyze operands of the call, assigning locations to each operand.
1492 SmallVector<CCValAssign, 16> ArgLocs;
1493 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1494 if (is64Bit)
1495 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1496 else
1497 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1498
1499
1500 // Lower arguments at fp - stackoffset + fpdiff.
1501 MachineFunction &MF = DAG.getMachineFunction();
1502
1503 unsigned NumBytesToBePushed =
1504 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1505
1506 unsigned NumBytesCallerPushed =
1507 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1508 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1509
1510 // Set the delta of movement of the returnaddr stackslot.
1511 // But only set if delta is greater than previous delta.
1512 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1513 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1514
1515 // Adjust the ret address stack slot.
1516 if (FPDiff) {
1517 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
1518 SDOperand RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1519 RetAddrFrIdx =
1520 DAG.getLoad(VT, DAG.getEntryNode(),RetAddrFrIdx, NULL, 0);
1521 // Emit a store of the saved ret value to the new location.
1522 int SlotSize = is64Bit ? 8 : 4;
1523 int NewReturnAddrFI =
1524 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1525 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1526 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1527 }
1528
1529 Chain = DAG.
1530 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1531
1532 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1533 SmallVector<SDOperand, 8> MemOpChains;
1534 SmallVector<SDOperand, 8> MemOpChains2;
1535 SDOperand FramePtr, StackPtr;
1536 SDOperand PtrOff;
1537 SDOperand FIN;
1538 int FI = 0;
1539
1540 // Walk the register/memloc assignments, inserting copies/loads. Lower
1541 // arguments first to the stack slot where they would normally - in case of a
1542 // normal function call - be.
1543 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1544 CCValAssign &VA = ArgLocs[i];
1545 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1546
1547 // Promote the value if needed.
1548 switch (VA.getLocInfo()) {
1549 default: assert(0 && "Unknown loc info!");
1550 case CCValAssign::Full: break;
1551 case CCValAssign::SExt:
1552 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1553 break;
1554 case CCValAssign::ZExt:
1555 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1556 break;
1557 case CCValAssign::AExt:
1558 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1559 break;
1560 }
1561
1562 if (VA.isRegLoc()) {
1563 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1564 } else {
1565 assert(VA.isMemLoc());
1566 if (StackPtr.Val == 0)
1567 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1568
1569 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1570 Arg));
1571 }
1572 }
1573
1574 if (!MemOpChains.empty())
1575 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1576 &MemOpChains[0], MemOpChains.size());
1577
1578 // Build a sequence of copy-to-reg nodes chained together with token chain
1579 // and flag operands which copy the outgoing args into registers.
1580 SDOperand InFlag;
1581 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1582 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1583 InFlag);
1584 InFlag = Chain.getValue(1);
1585 }
1586 InFlag = SDOperand();
1587 // Copy from stack slots to stack slot of a tail called function. This needs
1588 // to be done because if we would lower the arguments directly to their real
1589 // stack slot we might end up overwriting each other.
1590 // TODO: To make this more efficient (sometimes saving a store/load) we could
1591 // analyse the arguments and emit this store/load/store sequence only for
1592 // arguments which would be overwritten otherwise.
1593 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1594 CCValAssign &VA = ArgLocs[i];
1595 if (!VA.isRegLoc()) {
1596 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1597 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1598
1599 // Get source stack slot.
1600 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1601 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1602 // Create frame index.
1603 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1604 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1605 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1606 FIN = DAG.getFrameIndex(FI, MVT::i32);
1607 if (Flags & ISD::ParamFlags::ByVal) {
1608 // Copy relative to framepointer.
1609 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1610 ISD::ParamFlags::ByValAlignOffs);
1611
1612 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1613 ISD::ParamFlags::ByValSizeOffs;
1614
1615 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1616 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1617 // Copy relative to framepointer.
1618 MemOpChains2.push_back(DAG.getNode(ISD::MEMCPY, MVT::Other, Chain, FIN,
1619 PtrOff, SizeNode, AlignNode));
1620 } else {
1621 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1622 // Store relative to framepointer.
1623 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1624 }
1625 }
1626 }
1627
1628 if (!MemOpChains2.empty())
1629 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1630 &MemOpChains2[0], MemOpChains.size());
1631
1632 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1633 // GOT pointer.
1634 // Does not work with tail call since ebx is not restored correctly by
1635 // tailcaller. TODO: at least for x86 - verify for x86-64
1636
1637 // If the callee is a GlobalAddress node (quite common, every direct call is)
1638 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1639 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1640 // We should use extra load for direct calls to dllimported functions in
1641 // non-JIT mode.
1642 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1643 getTargetMachine(), true))
1644 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1645 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1646 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1647 else {
1648 assert(Callee.getOpcode() == ISD::LOAD &&
1649 "Function destination must be loaded into virtual register");
1650 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1651
1652 Chain = DAG.getCopyToReg(Chain,
1653 DAG.getRegister(Opc, getPointerTy()) ,
1654 Callee,InFlag);
1655 Callee = DAG.getRegister(Opc, getPointerTy());
1656 // Add register as live out.
1657 DAG.getMachineFunction().addLiveOut(Opc);
1658 }
1659
1660 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1661 SmallVector<SDOperand, 8> Ops;
1662
1663 Ops.push_back(Chain);
1664 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1665 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1666 if (InFlag.Val)
1667 Ops.push_back(InFlag);
1668 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1669 InFlag = Chain.getValue(1);
1670
1671 // Returns a chain & a flag for retval copy to use.
1672 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1673 Ops.clear();
1674 Ops.push_back(Chain);
1675 Ops.push_back(Callee);
1676 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1677 // Add argument registers to the end of the list so that they are known live
1678 // into the call.
1679 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1680 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1681 RegsToPass[i].second.getValueType()));
1682 if (InFlag.Val)
1683 Ops.push_back(InFlag);
1684 assert(InFlag.Val &&
1685 "Flag must be set. Depend on flag being set in LowerRET");
1686 Chain = DAG.getNode(X86ISD::TAILCALL,
1687 Op.Val->getVTList(), &Ops[0], Ops.size());
1688
1689 return SDOperand(Chain.Val, Op.ResNo);
1690}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691
1692//===----------------------------------------------------------------------===//
1693// X86-64 C Calling Convention implementation
1694//===----------------------------------------------------------------------===//
1695
1696SDOperand
1697X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1698 MachineFunction &MF = DAG.getMachineFunction();
1699 MachineFrameInfo *MFI = MF.getFrameInfo();
1700 SDOperand Root = Op.getOperand(0);
1701 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001702 unsigned CC= MF.getFunction()->getCallingConv();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703
1704 static const unsigned GPR64ArgRegs[] = {
1705 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1706 };
1707 static const unsigned XMMArgRegs[] = {
1708 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1709 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1710 };
1711
1712
1713 // Assign locations to all of the incoming arguments.
1714 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001715 CCState CCInfo(CC, isVarArg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001716 getTargetMachine(), ArgLocs);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001717 if (CC == CallingConv::Fast && PerformTailCallOpt)
1718 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1719 else
1720 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001721
1722 SmallVector<SDOperand, 8> ArgValues;
1723 unsigned LastVal = ~0U;
1724 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1725 CCValAssign &VA = ArgLocs[i];
1726 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1727 // places.
1728 assert(VA.getValNo() != LastVal &&
1729 "Don't support value assigned to multiple locs yet");
1730 LastVal = VA.getValNo();
1731
1732 if (VA.isRegLoc()) {
1733 MVT::ValueType RegVT = VA.getLocVT();
1734 TargetRegisterClass *RC;
1735 if (RegVT == MVT::i32)
1736 RC = X86::GR32RegisterClass;
1737 else if (RegVT == MVT::i64)
1738 RC = X86::GR64RegisterClass;
1739 else if (RegVT == MVT::f32)
1740 RC = X86::FR32RegisterClass;
1741 else if (RegVT == MVT::f64)
1742 RC = X86::FR64RegisterClass;
1743 else {
1744 assert(MVT::isVector(RegVT));
1745 if (MVT::getSizeInBits(RegVT) == 64) {
1746 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1747 RegVT = MVT::i64;
1748 } else
1749 RC = X86::VR128RegisterClass;
1750 }
1751
1752 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1753 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1754
1755 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1756 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1757 // right size.
1758 if (VA.getLocInfo() == CCValAssign::SExt)
1759 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1760 DAG.getValueType(VA.getValVT()));
1761 else if (VA.getLocInfo() == CCValAssign::ZExt)
1762 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1763 DAG.getValueType(VA.getValVT()));
1764
1765 if (VA.getLocInfo() != CCValAssign::Full)
1766 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1767
1768 // Handle MMX values passed in GPRs.
1769 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1770 MVT::getSizeInBits(RegVT) == 64)
1771 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1772
1773 ArgValues.push_back(ArgValue);
1774 } else {
1775 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001776 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001777 }
1778 }
1779
1780 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001781 if (CC==CallingConv::Fast)
1782 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001783
1784 // If the function takes variable number of arguments, make a frame index for
1785 // the start of the first vararg value... for expansion of llvm.va_start.
1786 if (isVarArg) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001787 assert(CC!=CallingConv::Fast
1788 && "Var arg not supported with calling convention fastcc");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1790 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1791
1792 // For X86-64, if there are vararg parameters that are passed via
1793 // registers, then we must store them to their spots on the stack so they
1794 // may be loaded by deferencing the result of va_next.
1795 VarArgsGPOffset = NumIntRegs * 8;
1796 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1797 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1798 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1799
1800 // Store the integer parameter registers.
1801 SmallVector<SDOperand, 8> MemOps;
1802 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1803 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1804 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1805 for (; NumIntRegs != 6; ++NumIntRegs) {
1806 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1807 X86::GR64RegisterClass);
1808 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1809 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1810 MemOps.push_back(Store);
1811 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1812 DAG.getConstant(8, getPointerTy()));
1813 }
1814
1815 // Now store the XMM (fp + vector) parameter registers.
1816 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1817 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1818 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1819 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1820 X86::VR128RegisterClass);
1821 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1822 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1823 MemOps.push_back(Store);
1824 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1825 DAG.getConstant(16, getPointerTy()));
1826 }
1827 if (!MemOps.empty())
1828 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1829 &MemOps[0], MemOps.size());
1830 }
1831
1832 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001833 // Tail call convention (fastcc) needs callee pop.
1834 if (CC == CallingConv::Fast && PerformTailCallOpt){
1835 BytesToPopOnReturn = StackSize; // Callee pops everything.
1836 BytesCallerReserves = 0;
1837 } else {
1838 BytesToPopOnReturn = 0; // Callee pops nothing.
1839 BytesCallerReserves = StackSize;
1840 }
Anton Korobeynikove844e472007-08-15 17:12:32 +00001841 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1842 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1843
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001844 // Return the new list of results.
1845 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1846 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1847}
1848
1849SDOperand
1850X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1851 unsigned CC) {
1852 SDOperand Chain = Op.getOperand(0);
1853 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001854 SDOperand Callee = Op.getOperand(4);
1855
1856 // Analyze operands of the call, assigning locations to each operand.
1857 SmallVector<CCValAssign, 16> ArgLocs;
1858 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001859 if (CC==CallingConv::Fast)
1860 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1861 else
1862 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001863
1864 // Get a count of how many bytes are to be pushed on the stack.
1865 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001866 if (CC == CallingConv::Fast)
1867 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1868
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1870
1871 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1872 SmallVector<SDOperand, 8> MemOpChains;
1873
1874 SDOperand StackPtr;
1875
1876 // Walk the register/memloc assignments, inserting copies/loads.
1877 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1878 CCValAssign &VA = ArgLocs[i];
1879 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1880
1881 // Promote the value if needed.
1882 switch (VA.getLocInfo()) {
1883 default: assert(0 && "Unknown loc info!");
1884 case CCValAssign::Full: break;
1885 case CCValAssign::SExt:
1886 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1887 break;
1888 case CCValAssign::ZExt:
1889 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1890 break;
1891 case CCValAssign::AExt:
1892 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1893 break;
1894 }
1895
1896 if (VA.isRegLoc()) {
1897 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1898 } else {
1899 assert(VA.isMemLoc());
1900 if (StackPtr.Val == 0)
1901 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolab8bcfcd2007-08-20 15:18:24 +00001902
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001903 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1904 Arg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001905 }
1906 }
1907
1908 if (!MemOpChains.empty())
1909 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1910 &MemOpChains[0], MemOpChains.size());
1911
1912 // Build a sequence of copy-to-reg nodes chained together with token chain
1913 // and flag operands which copy the outgoing args into registers.
1914 SDOperand InFlag;
1915 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1916 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1917 InFlag);
1918 InFlag = Chain.getValue(1);
1919 }
1920
1921 if (isVarArg) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001922 assert ( CallingConv::Fast != CC &&
1923 "Var args not supported with calling convention fastcc");
1924
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 // From AMD64 ABI document:
1926 // For calls that may call functions that use varargs or stdargs
1927 // (prototype-less calls or calls to functions containing ellipsis (...) in
1928 // the declaration) %al is used as hidden argument to specify the number
1929 // of SSE registers used. The contents of %al do not need to match exactly
1930 // the number of registers, but must be an ubound on the number of SSE
1931 // registers used and is in the range 0 - 8 inclusive.
1932
1933 // Count the number of XMM registers allocated.
1934 static const unsigned XMMArgRegs[] = {
1935 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1936 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1937 };
1938 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1939
1940 Chain = DAG.getCopyToReg(Chain, X86::AL,
1941 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1942 InFlag = Chain.getValue(1);
1943 }
1944
1945 // If the callee is a GlobalAddress node (quite common, every direct call is)
1946 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1947 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1948 // We should use extra load for direct calls to dllimported functions in
1949 // non-JIT mode.
1950 if (getTargetMachine().getCodeModel() != CodeModel::Large
1951 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1952 getTargetMachine(), true))
1953 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1954 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1955 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1956 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1957
1958 // Returns a chain & a flag for retval copy to use.
1959 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1960 SmallVector<SDOperand, 8> Ops;
1961 Ops.push_back(Chain);
1962 Ops.push_back(Callee);
1963
1964 // Add argument registers to the end of the list so that they are known live
1965 // into the call.
1966 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1967 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1968 RegsToPass[i].second.getValueType()));
1969
1970 if (InFlag.Val)
1971 Ops.push_back(InFlag);
1972
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001973 Chain = DAG.getNode(X86ISD::CALL,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001974 NodeTys, &Ops[0], Ops.size());
1975 InFlag = Chain.getValue(1);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001976 int NumBytesForCalleeToPush = 0;
1977 if (CC==CallingConv::Fast) {
1978 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
1979
1980 } else {
1981 NumBytesForCalleeToPush = 0; // Callee pops nothing.
1982 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001983 // Returns a flag for retval copy to use.
1984 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1985 Ops.clear();
1986 Ops.push_back(Chain);
1987 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001988 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001989 Ops.push_back(InFlag);
1990 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1991 InFlag = Chain.getValue(1);
1992
1993 // Handle result values, copying them out of physregs into vregs that we
1994 // return.
1995 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1996}
1997
1998
1999//===----------------------------------------------------------------------===//
2000// Other Lowering Hooks
2001//===----------------------------------------------------------------------===//
2002
2003
2004SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00002005 MachineFunction &MF = DAG.getMachineFunction();
2006 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2007 int ReturnAddrIndex = FuncInfo->getRAIndex();
2008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009 if (ReturnAddrIndex == 0) {
2010 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 if (Subtarget->is64Bit())
2012 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2013 else
2014 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00002015
2016 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002017 }
2018
2019 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2020}
2021
2022
2023
2024/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2025/// specific condition code. It returns a false if it cannot do a direct
2026/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2027/// needed.
2028static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2029 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2030 SelectionDAG &DAG) {
2031 X86CC = X86::COND_INVALID;
2032 if (!isFP) {
2033 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2034 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2035 // X > -1 -> X == 0, jump !sign.
2036 RHS = DAG.getConstant(0, RHS.getValueType());
2037 X86CC = X86::COND_NS;
2038 return true;
2039 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2040 // X < 0 -> X == 0, jump on sign.
2041 X86CC = X86::COND_S;
2042 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00002043 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2044 // X < 1 -> X <= 0
2045 RHS = DAG.getConstant(0, RHS.getValueType());
2046 X86CC = X86::COND_LE;
2047 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048 }
2049 }
2050
2051 switch (SetCCOpcode) {
2052 default: break;
2053 case ISD::SETEQ: X86CC = X86::COND_E; break;
2054 case ISD::SETGT: X86CC = X86::COND_G; break;
2055 case ISD::SETGE: X86CC = X86::COND_GE; break;
2056 case ISD::SETLT: X86CC = X86::COND_L; break;
2057 case ISD::SETLE: X86CC = X86::COND_LE; break;
2058 case ISD::SETNE: X86CC = X86::COND_NE; break;
2059 case ISD::SETULT: X86CC = X86::COND_B; break;
2060 case ISD::SETUGT: X86CC = X86::COND_A; break;
2061 case ISD::SETULE: X86CC = X86::COND_BE; break;
2062 case ISD::SETUGE: X86CC = X86::COND_AE; break;
2063 }
2064 } else {
2065 // On a floating point condition, the flags are set as follows:
2066 // ZF PF CF op
2067 // 0 | 0 | 0 | X > Y
2068 // 0 | 0 | 1 | X < Y
2069 // 1 | 0 | 0 | X == Y
2070 // 1 | 1 | 1 | unordered
2071 bool Flip = false;
2072 switch (SetCCOpcode) {
2073 default: break;
2074 case ISD::SETUEQ:
2075 case ISD::SETEQ: X86CC = X86::COND_E; break;
2076 case ISD::SETOLT: Flip = true; // Fallthrough
2077 case ISD::SETOGT:
2078 case ISD::SETGT: X86CC = X86::COND_A; break;
2079 case ISD::SETOLE: Flip = true; // Fallthrough
2080 case ISD::SETOGE:
2081 case ISD::SETGE: X86CC = X86::COND_AE; break;
2082 case ISD::SETUGT: Flip = true; // Fallthrough
2083 case ISD::SETULT:
2084 case ISD::SETLT: X86CC = X86::COND_B; break;
2085 case ISD::SETUGE: Flip = true; // Fallthrough
2086 case ISD::SETULE:
2087 case ISD::SETLE: X86CC = X86::COND_BE; break;
2088 case ISD::SETONE:
2089 case ISD::SETNE: X86CC = X86::COND_NE; break;
2090 case ISD::SETUO: X86CC = X86::COND_P; break;
2091 case ISD::SETO: X86CC = X86::COND_NP; break;
2092 }
2093 if (Flip)
2094 std::swap(LHS, RHS);
2095 }
2096
2097 return X86CC != X86::COND_INVALID;
2098}
2099
2100/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2101/// code. Current x86 isa includes the following FP cmov instructions:
2102/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2103static bool hasFPCMov(unsigned X86CC) {
2104 switch (X86CC) {
2105 default:
2106 return false;
2107 case X86::COND_B:
2108 case X86::COND_BE:
2109 case X86::COND_E:
2110 case X86::COND_P:
2111 case X86::COND_A:
2112 case X86::COND_AE:
2113 case X86::COND_NE:
2114 case X86::COND_NP:
2115 return true;
2116 }
2117}
2118
2119/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2120/// true if Op is undef or if its value falls within the specified range (L, H].
2121static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2122 if (Op.getOpcode() == ISD::UNDEF)
2123 return true;
2124
2125 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2126 return (Val >= Low && Val < Hi);
2127}
2128
2129/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2130/// true if Op is undef or if its value equal to the specified value.
2131static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2132 if (Op.getOpcode() == ISD::UNDEF)
2133 return true;
2134 return cast<ConstantSDNode>(Op)->getValue() == Val;
2135}
2136
2137/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2138/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2139bool X86::isPSHUFDMask(SDNode *N) {
2140 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2141
Dan Gohman7dc19012007-08-02 21:17:01 +00002142 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002143 return false;
2144
2145 // Check if the value doesn't reference the second vector.
2146 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2147 SDOperand Arg = N->getOperand(i);
2148 if (Arg.getOpcode() == ISD::UNDEF) continue;
2149 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002150 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002151 return false;
2152 }
2153
2154 return true;
2155}
2156
2157/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2158/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2159bool X86::isPSHUFHWMask(SDNode *N) {
2160 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2161
2162 if (N->getNumOperands() != 8)
2163 return false;
2164
2165 // Lower quadword copied in order.
2166 for (unsigned i = 0; i != 4; ++i) {
2167 SDOperand Arg = N->getOperand(i);
2168 if (Arg.getOpcode() == ISD::UNDEF) continue;
2169 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2170 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2171 return false;
2172 }
2173
2174 // Upper quadword shuffled.
2175 for (unsigned i = 4; i != 8; ++i) {
2176 SDOperand Arg = N->getOperand(i);
2177 if (Arg.getOpcode() == ISD::UNDEF) continue;
2178 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2179 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2180 if (Val < 4 || Val > 7)
2181 return false;
2182 }
2183
2184 return true;
2185}
2186
2187/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2188/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2189bool X86::isPSHUFLWMask(SDNode *N) {
2190 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2191
2192 if (N->getNumOperands() != 8)
2193 return false;
2194
2195 // Upper quadword copied in order.
2196 for (unsigned i = 4; i != 8; ++i)
2197 if (!isUndefOrEqual(N->getOperand(i), i))
2198 return false;
2199
2200 // Lower quadword shuffled.
2201 for (unsigned i = 0; i != 4; ++i)
2202 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2203 return false;
2204
2205 return true;
2206}
2207
2208/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2209/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2210static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2211 if (NumElems != 2 && NumElems != 4) return false;
2212
2213 unsigned Half = NumElems / 2;
2214 for (unsigned i = 0; i < Half; ++i)
2215 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2216 return false;
2217 for (unsigned i = Half; i < NumElems; ++i)
2218 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2219 return false;
2220
2221 return true;
2222}
2223
2224bool X86::isSHUFPMask(SDNode *N) {
2225 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2226 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2227}
2228
2229/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2230/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2231/// half elements to come from vector 1 (which would equal the dest.) and
2232/// the upper half to come from vector 2.
2233static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2234 if (NumOps != 2 && NumOps != 4) return false;
2235
2236 unsigned Half = NumOps / 2;
2237 for (unsigned i = 0; i < Half; ++i)
2238 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2239 return false;
2240 for (unsigned i = Half; i < NumOps; ++i)
2241 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2242 return false;
2243 return true;
2244}
2245
2246static bool isCommutedSHUFP(SDNode *N) {
2247 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2248 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2249}
2250
2251/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2252/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2253bool X86::isMOVHLPSMask(SDNode *N) {
2254 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2255
2256 if (N->getNumOperands() != 4)
2257 return false;
2258
2259 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2260 return isUndefOrEqual(N->getOperand(0), 6) &&
2261 isUndefOrEqual(N->getOperand(1), 7) &&
2262 isUndefOrEqual(N->getOperand(2), 2) &&
2263 isUndefOrEqual(N->getOperand(3), 3);
2264}
2265
2266/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2267/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2268/// <2, 3, 2, 3>
2269bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2270 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2271
2272 if (N->getNumOperands() != 4)
2273 return false;
2274
2275 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2276 return isUndefOrEqual(N->getOperand(0), 2) &&
2277 isUndefOrEqual(N->getOperand(1), 3) &&
2278 isUndefOrEqual(N->getOperand(2), 2) &&
2279 isUndefOrEqual(N->getOperand(3), 3);
2280}
2281
2282/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2283/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2284bool X86::isMOVLPMask(SDNode *N) {
2285 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2286
2287 unsigned NumElems = N->getNumOperands();
2288 if (NumElems != 2 && NumElems != 4)
2289 return false;
2290
2291 for (unsigned i = 0; i < NumElems/2; ++i)
2292 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2293 return false;
2294
2295 for (unsigned i = NumElems/2; i < NumElems; ++i)
2296 if (!isUndefOrEqual(N->getOperand(i), i))
2297 return false;
2298
2299 return true;
2300}
2301
2302/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2303/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2304/// and MOVLHPS.
2305bool X86::isMOVHPMask(SDNode *N) {
2306 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2307
2308 unsigned NumElems = N->getNumOperands();
2309 if (NumElems != 2 && NumElems != 4)
2310 return false;
2311
2312 for (unsigned i = 0; i < NumElems/2; ++i)
2313 if (!isUndefOrEqual(N->getOperand(i), i))
2314 return false;
2315
2316 for (unsigned i = 0; i < NumElems/2; ++i) {
2317 SDOperand Arg = N->getOperand(i + NumElems/2);
2318 if (!isUndefOrEqual(Arg, i + NumElems))
2319 return false;
2320 }
2321
2322 return true;
2323}
2324
2325/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2326/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2327bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2328 bool V2IsSplat = false) {
2329 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2330 return false;
2331
2332 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2333 SDOperand BitI = Elts[i];
2334 SDOperand BitI1 = Elts[i+1];
2335 if (!isUndefOrEqual(BitI, j))
2336 return false;
2337 if (V2IsSplat) {
2338 if (isUndefOrEqual(BitI1, NumElts))
2339 return false;
2340 } else {
2341 if (!isUndefOrEqual(BitI1, j + NumElts))
2342 return false;
2343 }
2344 }
2345
2346 return true;
2347}
2348
2349bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2350 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2351 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2352}
2353
2354/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2355/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2356bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2357 bool V2IsSplat = false) {
2358 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2359 return false;
2360
2361 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2362 SDOperand BitI = Elts[i];
2363 SDOperand BitI1 = Elts[i+1];
2364 if (!isUndefOrEqual(BitI, j + NumElts/2))
2365 return false;
2366 if (V2IsSplat) {
2367 if (isUndefOrEqual(BitI1, NumElts))
2368 return false;
2369 } else {
2370 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2371 return false;
2372 }
2373 }
2374
2375 return true;
2376}
2377
2378bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2379 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2380 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2381}
2382
2383/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2384/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2385/// <0, 0, 1, 1>
2386bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2387 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2388
2389 unsigned NumElems = N->getNumOperands();
2390 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2391 return false;
2392
2393 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2394 SDOperand BitI = N->getOperand(i);
2395 SDOperand BitI1 = N->getOperand(i+1);
2396
2397 if (!isUndefOrEqual(BitI, j))
2398 return false;
2399 if (!isUndefOrEqual(BitI1, j))
2400 return false;
2401 }
2402
2403 return true;
2404}
2405
2406/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2407/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2408/// <2, 2, 3, 3>
2409bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2410 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2411
2412 unsigned NumElems = N->getNumOperands();
2413 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2414 return false;
2415
2416 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2417 SDOperand BitI = N->getOperand(i);
2418 SDOperand BitI1 = N->getOperand(i + 1);
2419
2420 if (!isUndefOrEqual(BitI, j))
2421 return false;
2422 if (!isUndefOrEqual(BitI1, j))
2423 return false;
2424 }
2425
2426 return true;
2427}
2428
2429/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2430/// specifies a shuffle of elements that is suitable for input to MOVSS,
2431/// MOVSD, and MOVD, i.e. setting the lowest element.
2432static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2433 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2434 return false;
2435
2436 if (!isUndefOrEqual(Elts[0], NumElts))
2437 return false;
2438
2439 for (unsigned i = 1; i < NumElts; ++i) {
2440 if (!isUndefOrEqual(Elts[i], i))
2441 return false;
2442 }
2443
2444 return true;
2445}
2446
2447bool X86::isMOVLMask(SDNode *N) {
2448 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2449 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2450}
2451
2452/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2453/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2454/// element of vector 2 and the other elements to come from vector 1 in order.
2455static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2456 bool V2IsSplat = false,
2457 bool V2IsUndef = false) {
2458 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2459 return false;
2460
2461 if (!isUndefOrEqual(Ops[0], 0))
2462 return false;
2463
2464 for (unsigned i = 1; i < NumOps; ++i) {
2465 SDOperand Arg = Ops[i];
2466 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2467 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2468 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2469 return false;
2470 }
2471
2472 return true;
2473}
2474
2475static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2476 bool V2IsUndef = false) {
2477 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2478 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2479 V2IsSplat, V2IsUndef);
2480}
2481
2482/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2483/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2484bool X86::isMOVSHDUPMask(SDNode *N) {
2485 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2486
2487 if (N->getNumOperands() != 4)
2488 return false;
2489
2490 // Expect 1, 1, 3, 3
2491 for (unsigned i = 0; i < 2; ++i) {
2492 SDOperand Arg = N->getOperand(i);
2493 if (Arg.getOpcode() == ISD::UNDEF) continue;
2494 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2495 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2496 if (Val != 1) return false;
2497 }
2498
2499 bool HasHi = false;
2500 for (unsigned i = 2; i < 4; ++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 != 3) return false;
2506 HasHi = true;
2507 }
2508
2509 // Don't use movshdup if it can be done with a shufps.
2510 return HasHi;
2511}
2512
2513/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2514/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2515bool X86::isMOVSLDUPMask(SDNode *N) {
2516 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2517
2518 if (N->getNumOperands() != 4)
2519 return false;
2520
2521 // Expect 0, 0, 2, 2
2522 for (unsigned i = 0; i < 2; ++i) {
2523 SDOperand Arg = N->getOperand(i);
2524 if (Arg.getOpcode() == ISD::UNDEF) continue;
2525 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2526 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2527 if (Val != 0) return false;
2528 }
2529
2530 bool HasHi = false;
2531 for (unsigned i = 2; i < 4; ++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 != 2) return false;
2537 HasHi = true;
2538 }
2539
2540 // Don't use movshdup if it can be done with a shufps.
2541 return HasHi;
2542}
2543
2544/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2545/// specifies a identity operation on the LHS or RHS.
2546static bool isIdentityMask(SDNode *N, bool RHS = false) {
2547 unsigned NumElems = N->getNumOperands();
2548 for (unsigned i = 0; i < NumElems; ++i)
2549 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2550 return false;
2551 return true;
2552}
2553
2554/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2555/// a splat of a single element.
2556static bool isSplatMask(SDNode *N) {
2557 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2558
2559 // This is a splat operation if each element of the permute is the same, and
2560 // if the value doesn't reference the second vector.
2561 unsigned NumElems = N->getNumOperands();
2562 SDOperand ElementBase;
2563 unsigned i = 0;
2564 for (; i != NumElems; ++i) {
2565 SDOperand Elt = N->getOperand(i);
2566 if (isa<ConstantSDNode>(Elt)) {
2567 ElementBase = Elt;
2568 break;
2569 }
2570 }
2571
2572 if (!ElementBase.Val)
2573 return false;
2574
2575 for (; i != NumElems; ++i) {
2576 SDOperand Arg = N->getOperand(i);
2577 if (Arg.getOpcode() == ISD::UNDEF) continue;
2578 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2579 if (Arg != ElementBase) return false;
2580 }
2581
2582 // Make sure it is a splat of the first vector operand.
2583 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2584}
2585
2586/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2587/// a splat of a single element and it's a 2 or 4 element mask.
2588bool X86::isSplatMask(SDNode *N) {
2589 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2590
2591 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2592 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2593 return false;
2594 return ::isSplatMask(N);
2595}
2596
2597/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2598/// specifies a splat of zero element.
2599bool X86::isSplatLoMask(SDNode *N) {
2600 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2601
2602 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2603 if (!isUndefOrEqual(N->getOperand(i), 0))
2604 return false;
2605 return true;
2606}
2607
2608/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2609/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2610/// instructions.
2611unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2612 unsigned NumOperands = N->getNumOperands();
2613 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2614 unsigned Mask = 0;
2615 for (unsigned i = 0; i < NumOperands; ++i) {
2616 unsigned Val = 0;
2617 SDOperand Arg = N->getOperand(NumOperands-i-1);
2618 if (Arg.getOpcode() != ISD::UNDEF)
2619 Val = cast<ConstantSDNode>(Arg)->getValue();
2620 if (Val >= NumOperands) Val -= NumOperands;
2621 Mask |= Val;
2622 if (i != NumOperands - 1)
2623 Mask <<= Shift;
2624 }
2625
2626 return Mask;
2627}
2628
2629/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2630/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2631/// instructions.
2632unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2633 unsigned Mask = 0;
2634 // 8 nodes, but we only care about the last 4.
2635 for (unsigned i = 7; i >= 4; --i) {
2636 unsigned Val = 0;
2637 SDOperand Arg = N->getOperand(i);
2638 if (Arg.getOpcode() != ISD::UNDEF)
2639 Val = cast<ConstantSDNode>(Arg)->getValue();
2640 Mask |= (Val - 4);
2641 if (i != 4)
2642 Mask <<= 2;
2643 }
2644
2645 return Mask;
2646}
2647
2648/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2649/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2650/// instructions.
2651unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2652 unsigned Mask = 0;
2653 // 8 nodes, but we only care about the first 4.
2654 for (int i = 3; i >= 0; --i) {
2655 unsigned Val = 0;
2656 SDOperand Arg = N->getOperand(i);
2657 if (Arg.getOpcode() != ISD::UNDEF)
2658 Val = cast<ConstantSDNode>(Arg)->getValue();
2659 Mask |= Val;
2660 if (i != 0)
2661 Mask <<= 2;
2662 }
2663
2664 return Mask;
2665}
2666
2667/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2668/// specifies a 8 element shuffle that can be broken into a pair of
2669/// PSHUFHW and PSHUFLW.
2670static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2671 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2672
2673 if (N->getNumOperands() != 8)
2674 return false;
2675
2676 // Lower quadword shuffled.
2677 for (unsigned i = 0; i != 4; ++i) {
2678 SDOperand Arg = N->getOperand(i);
2679 if (Arg.getOpcode() == ISD::UNDEF) continue;
2680 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2681 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2682 if (Val > 4)
2683 return false;
2684 }
2685
2686 // Upper quadword shuffled.
2687 for (unsigned i = 4; i != 8; ++i) {
2688 SDOperand Arg = N->getOperand(i);
2689 if (Arg.getOpcode() == ISD::UNDEF) continue;
2690 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2691 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2692 if (Val < 4 || Val > 7)
2693 return false;
2694 }
2695
2696 return true;
2697}
2698
2699/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2700/// values in ther permute mask.
2701static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2702 SDOperand &V2, SDOperand &Mask,
2703 SelectionDAG &DAG) {
2704 MVT::ValueType VT = Op.getValueType();
2705 MVT::ValueType MaskVT = Mask.getValueType();
2706 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2707 unsigned NumElems = Mask.getNumOperands();
2708 SmallVector<SDOperand, 8> MaskVec;
2709
2710 for (unsigned i = 0; i != NumElems; ++i) {
2711 SDOperand Arg = Mask.getOperand(i);
2712 if (Arg.getOpcode() == ISD::UNDEF) {
2713 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2714 continue;
2715 }
2716 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2717 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2718 if (Val < NumElems)
2719 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2720 else
2721 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2722 }
2723
2724 std::swap(V1, V2);
2725 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2726 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2727}
2728
2729/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2730/// match movhlps. The lower half elements should come from upper half of
2731/// V1 (and in order), and the upper half elements should come from the upper
2732/// half of V2 (and in order).
2733static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2734 unsigned NumElems = Mask->getNumOperands();
2735 if (NumElems != 4)
2736 return false;
2737 for (unsigned i = 0, e = 2; i != e; ++i)
2738 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2739 return false;
2740 for (unsigned i = 2; i != 4; ++i)
2741 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2742 return false;
2743 return true;
2744}
2745
2746/// isScalarLoadToVector - Returns true if the node is a scalar load that
2747/// is promoted to a vector.
2748static inline bool isScalarLoadToVector(SDNode *N) {
2749 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2750 N = N->getOperand(0).Val;
2751 return ISD::isNON_EXTLoad(N);
2752 }
2753 return false;
2754}
2755
2756/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2757/// match movlp{s|d}. The lower half elements should come from lower half of
2758/// V1 (and in order), and the upper half elements should come from the upper
2759/// half of V2 (and in order). And since V1 will become the source of the
2760/// MOVLP, it must be either a vector load or a scalar load to vector.
2761static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2762 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2763 return false;
2764 // Is V2 is a vector load, don't do this transformation. We will try to use
2765 // load folding shufps op.
2766 if (ISD::isNON_EXTLoad(V2))
2767 return false;
2768
2769 unsigned NumElems = Mask->getNumOperands();
2770 if (NumElems != 2 && NumElems != 4)
2771 return false;
2772 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2773 if (!isUndefOrEqual(Mask->getOperand(i), i))
2774 return false;
2775 for (unsigned i = NumElems/2; i != NumElems; ++i)
2776 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2777 return false;
2778 return true;
2779}
2780
2781/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2782/// all the same.
2783static bool isSplatVector(SDNode *N) {
2784 if (N->getOpcode() != ISD::BUILD_VECTOR)
2785 return false;
2786
2787 SDOperand SplatValue = N->getOperand(0);
2788 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2789 if (N->getOperand(i) != SplatValue)
2790 return false;
2791 return true;
2792}
2793
2794/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2795/// to an undef.
2796static bool isUndefShuffle(SDNode *N) {
2797 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2798 return false;
2799
2800 SDOperand V1 = N->getOperand(0);
2801 SDOperand V2 = N->getOperand(1);
2802 SDOperand Mask = N->getOperand(2);
2803 unsigned NumElems = Mask.getNumOperands();
2804 for (unsigned i = 0; i != NumElems; ++i) {
2805 SDOperand Arg = Mask.getOperand(i);
2806 if (Arg.getOpcode() != ISD::UNDEF) {
2807 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2808 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2809 return false;
2810 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2811 return false;
2812 }
2813 }
2814 return true;
2815}
2816
2817/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2818/// constant +0.0.
2819static inline bool isZeroNode(SDOperand Elt) {
2820 return ((isa<ConstantSDNode>(Elt) &&
2821 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2822 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002823 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002824}
2825
2826/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2827/// to an zero vector.
2828static bool isZeroShuffle(SDNode *N) {
2829 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2830 return false;
2831
2832 SDOperand V1 = N->getOperand(0);
2833 SDOperand V2 = N->getOperand(1);
2834 SDOperand Mask = N->getOperand(2);
2835 unsigned NumElems = Mask.getNumOperands();
2836 for (unsigned i = 0; i != NumElems; ++i) {
2837 SDOperand Arg = Mask.getOperand(i);
2838 if (Arg.getOpcode() != ISD::UNDEF) {
2839 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2840 if (Idx < NumElems) {
2841 unsigned Opc = V1.Val->getOpcode();
2842 if (Opc == ISD::UNDEF)
2843 continue;
2844 if (Opc != ISD::BUILD_VECTOR ||
2845 !isZeroNode(V1.Val->getOperand(Idx)))
2846 return false;
2847 } else if (Idx >= NumElems) {
2848 unsigned Opc = V2.Val->getOpcode();
2849 if (Opc == ISD::UNDEF)
2850 continue;
2851 if (Opc != ISD::BUILD_VECTOR ||
2852 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2853 return false;
2854 }
2855 }
2856 }
2857 return true;
2858}
2859
2860/// getZeroVector - Returns a vector of specified type with all zero elements.
2861///
2862static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2863 assert(MVT::isVector(VT) && "Expected a vector type");
2864 unsigned NumElems = MVT::getVectorNumElements(VT);
2865 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2866 bool isFP = MVT::isFloatingPoint(EVT);
2867 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2868 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2869 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2870}
2871
2872/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2873/// that point to V2 points to its first element.
2874static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2875 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2876
2877 bool Changed = false;
2878 SmallVector<SDOperand, 8> MaskVec;
2879 unsigned NumElems = Mask.getNumOperands();
2880 for (unsigned i = 0; i != NumElems; ++i) {
2881 SDOperand Arg = Mask.getOperand(i);
2882 if (Arg.getOpcode() != ISD::UNDEF) {
2883 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2884 if (Val > NumElems) {
2885 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2886 Changed = true;
2887 }
2888 }
2889 MaskVec.push_back(Arg);
2890 }
2891
2892 if (Changed)
2893 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2894 &MaskVec[0], MaskVec.size());
2895 return Mask;
2896}
2897
2898/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2899/// operation of specified width.
2900static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2901 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2902 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2903
2904 SmallVector<SDOperand, 8> MaskVec;
2905 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2906 for (unsigned i = 1; i != NumElems; ++i)
2907 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2908 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2909}
2910
2911/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2912/// of specified width.
2913static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2914 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2915 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2916 SmallVector<SDOperand, 8> MaskVec;
2917 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2918 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2919 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2920 }
2921 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2922}
2923
2924/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2925/// of specified width.
2926static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2927 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2928 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2929 unsigned Half = NumElems/2;
2930 SmallVector<SDOperand, 8> MaskVec;
2931 for (unsigned i = 0; i != Half; ++i) {
2932 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2933 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2934 }
2935 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2936}
2937
2938/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2939///
2940static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2941 SDOperand V1 = Op.getOperand(0);
2942 SDOperand Mask = Op.getOperand(2);
2943 MVT::ValueType VT = Op.getValueType();
2944 unsigned NumElems = Mask.getNumOperands();
2945 Mask = getUnpacklMask(NumElems, DAG);
2946 while (NumElems != 4) {
2947 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2948 NumElems >>= 1;
2949 }
2950 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2951
2952 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2953 Mask = getZeroVector(MaskVT, DAG);
2954 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2955 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2956 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2957}
2958
2959/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2960/// vector of zero or undef vector.
2961static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2962 unsigned NumElems, unsigned Idx,
2963 bool isZero, SelectionDAG &DAG) {
2964 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2965 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2966 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2967 SDOperand Zero = DAG.getConstant(0, EVT);
2968 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
2969 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2970 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2971 &MaskVec[0], MaskVec.size());
2972 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2973}
2974
2975/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2976///
2977static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2978 unsigned NumNonZero, unsigned NumZero,
2979 SelectionDAG &DAG, TargetLowering &TLI) {
2980 if (NumNonZero > 8)
2981 return SDOperand();
2982
2983 SDOperand V(0, 0);
2984 bool First = true;
2985 for (unsigned i = 0; i < 16; ++i) {
2986 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2987 if (ThisIsNonZero && First) {
2988 if (NumZero)
2989 V = getZeroVector(MVT::v8i16, DAG);
2990 else
2991 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2992 First = false;
2993 }
2994
2995 if ((i & 1) != 0) {
2996 SDOperand ThisElt(0, 0), LastElt(0, 0);
2997 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2998 if (LastIsNonZero) {
2999 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3000 }
3001 if (ThisIsNonZero) {
3002 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3003 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3004 ThisElt, DAG.getConstant(8, MVT::i8));
3005 if (LastIsNonZero)
3006 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3007 } else
3008 ThisElt = LastElt;
3009
3010 if (ThisElt.Val)
3011 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
3012 DAG.getConstant(i/2, TLI.getPointerTy()));
3013 }
3014 }
3015
3016 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3017}
3018
3019/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3020///
3021static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3022 unsigned NumNonZero, unsigned NumZero,
3023 SelectionDAG &DAG, TargetLowering &TLI) {
3024 if (NumNonZero > 4)
3025 return SDOperand();
3026
3027 SDOperand V(0, 0);
3028 bool First = true;
3029 for (unsigned i = 0; i < 8; ++i) {
3030 bool isNonZero = (NonZeros & (1 << i)) != 0;
3031 if (isNonZero) {
3032 if (First) {
3033 if (NumZero)
3034 V = getZeroVector(MVT::v8i16, DAG);
3035 else
3036 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3037 First = false;
3038 }
3039 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
3040 DAG.getConstant(i, TLI.getPointerTy()));
3041 }
3042 }
3043
3044 return V;
3045}
3046
3047SDOperand
3048X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3049 // All zero's are handled with pxor.
3050 if (ISD::isBuildVectorAllZeros(Op.Val))
3051 return Op;
3052
3053 // All one's are handled with pcmpeqd.
3054 if (ISD::isBuildVectorAllOnes(Op.Val))
3055 return Op;
3056
3057 MVT::ValueType VT = Op.getValueType();
3058 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3059 unsigned EVTBits = MVT::getSizeInBits(EVT);
3060
3061 unsigned NumElems = Op.getNumOperands();
3062 unsigned NumZero = 0;
3063 unsigned NumNonZero = 0;
3064 unsigned NonZeros = 0;
Dan Gohman21463242007-07-24 22:55:08 +00003065 unsigned NumNonZeroImms = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066 std::set<SDOperand> Values;
3067 for (unsigned i = 0; i < NumElems; ++i) {
3068 SDOperand Elt = Op.getOperand(i);
3069 if (Elt.getOpcode() != ISD::UNDEF) {
3070 Values.insert(Elt);
3071 if (isZeroNode(Elt))
3072 NumZero++;
3073 else {
3074 NonZeros |= (1 << i);
3075 NumNonZero++;
Dan Gohman21463242007-07-24 22:55:08 +00003076 if (Elt.getOpcode() == ISD::Constant ||
3077 Elt.getOpcode() == ISD::ConstantFP)
3078 NumNonZeroImms++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003079 }
3080 }
3081 }
3082
3083 if (NumNonZero == 0) {
3084 if (NumZero == 0)
3085 // All undef vector. Return an UNDEF.
3086 return DAG.getNode(ISD::UNDEF, VT);
3087 else
3088 // A mix of zero and undef. Return a zero vector.
3089 return getZeroVector(VT, DAG);
3090 }
3091
3092 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3093 if (Values.size() == 1)
3094 return SDOperand();
3095
3096 // Special case for single non-zero element.
3097 if (NumNonZero == 1) {
3098 unsigned Idx = CountTrailingZeros_32(NonZeros);
3099 SDOperand Item = Op.getOperand(Idx);
3100 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3101 if (Idx == 0)
3102 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3103 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3104 NumZero > 0, DAG);
3105
3106 if (EVTBits == 32) {
3107 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3108 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3109 DAG);
3110 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3111 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3112 SmallVector<SDOperand, 8> MaskVec;
3113 for (unsigned i = 0; i < NumElems; i++)
3114 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3115 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3116 &MaskVec[0], MaskVec.size());
3117 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3118 DAG.getNode(ISD::UNDEF, VT), Mask);
3119 }
3120 }
3121
Dan Gohman21463242007-07-24 22:55:08 +00003122 // A vector full of immediates; various special cases are already
3123 // handled, so this is best done with a single constant-pool load.
3124 if (NumNonZero == NumNonZeroImms)
3125 return SDOperand();
3126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127 // Let legalizer expand 2-wide build_vectors.
3128 if (EVTBits == 64)
3129 return SDOperand();
3130
3131 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3132 if (EVTBits == 8 && NumElems == 16) {
3133 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3134 *this);
3135 if (V.Val) return V;
3136 }
3137
3138 if (EVTBits == 16 && NumElems == 8) {
3139 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3140 *this);
3141 if (V.Val) return V;
3142 }
3143
3144 // If element VT is == 32 bits, turn it into a number of shuffles.
3145 SmallVector<SDOperand, 8> V;
3146 V.resize(NumElems);
3147 if (NumElems == 4 && NumZero > 0) {
3148 for (unsigned i = 0; i < 4; ++i) {
3149 bool isZero = !(NonZeros & (1 << i));
3150 if (isZero)
3151 V[i] = getZeroVector(VT, DAG);
3152 else
3153 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3154 }
3155
3156 for (unsigned i = 0; i < 2; ++i) {
3157 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3158 default: break;
3159 case 0:
3160 V[i] = V[i*2]; // Must be a zero vector.
3161 break;
3162 case 1:
3163 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3164 getMOVLMask(NumElems, DAG));
3165 break;
3166 case 2:
3167 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3168 getMOVLMask(NumElems, DAG));
3169 break;
3170 case 3:
3171 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3172 getUnpacklMask(NumElems, DAG));
3173 break;
3174 }
3175 }
3176
3177 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3178 // clears the upper bits.
3179 // FIXME: we can do the same for v4f32 case when we know both parts of
3180 // the lower half come from scalar_to_vector (loadf32). We should do
3181 // that in post legalizer dag combiner with target specific hooks.
3182 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3183 return V[0];
3184 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3185 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3186 SmallVector<SDOperand, 8> MaskVec;
3187 bool Reverse = (NonZeros & 0x3) == 2;
3188 for (unsigned i = 0; i < 2; ++i)
3189 if (Reverse)
3190 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3191 else
3192 MaskVec.push_back(DAG.getConstant(i, EVT));
3193 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3194 for (unsigned i = 0; i < 2; ++i)
3195 if (Reverse)
3196 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3197 else
3198 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3199 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3200 &MaskVec[0], MaskVec.size());
3201 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3202 }
3203
3204 if (Values.size() > 2) {
3205 // Expand into a number of unpckl*.
3206 // e.g. for v4f32
3207 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3208 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3209 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3210 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3211 for (unsigned i = 0; i < NumElems; ++i)
3212 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3213 NumElems >>= 1;
3214 while (NumElems != 0) {
3215 for (unsigned i = 0; i < NumElems; ++i)
3216 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3217 UnpckMask);
3218 NumElems >>= 1;
3219 }
3220 return V[0];
3221 }
3222
3223 return SDOperand();
3224}
3225
3226SDOperand
3227X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3228 SDOperand V1 = Op.getOperand(0);
3229 SDOperand V2 = Op.getOperand(1);
3230 SDOperand PermMask = Op.getOperand(2);
3231 MVT::ValueType VT = Op.getValueType();
3232 unsigned NumElems = PermMask.getNumOperands();
3233 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3234 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3235 bool V1IsSplat = false;
3236 bool V2IsSplat = false;
3237
3238 if (isUndefShuffle(Op.Val))
3239 return DAG.getNode(ISD::UNDEF, VT);
3240
3241 if (isZeroShuffle(Op.Val))
3242 return getZeroVector(VT, DAG);
3243
3244 if (isIdentityMask(PermMask.Val))
3245 return V1;
3246 else if (isIdentityMask(PermMask.Val, true))
3247 return V2;
3248
3249 if (isSplatMask(PermMask.Val)) {
3250 if (NumElems <= 4) return Op;
3251 // Promote it to a v4i32 splat.
3252 return PromoteSplat(Op, DAG);
3253 }
3254
3255 if (X86::isMOVLMask(PermMask.Val))
3256 return (V1IsUndef) ? V2 : Op;
3257
3258 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3259 X86::isMOVSLDUPMask(PermMask.Val) ||
3260 X86::isMOVHLPSMask(PermMask.Val) ||
3261 X86::isMOVHPMask(PermMask.Val) ||
3262 X86::isMOVLPMask(PermMask.Val))
3263 return Op;
3264
3265 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3266 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3267 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3268
3269 bool Commuted = false;
3270 V1IsSplat = isSplatVector(V1.Val);
3271 V2IsSplat = isSplatVector(V2.Val);
3272 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3273 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3274 std::swap(V1IsSplat, V2IsSplat);
3275 std::swap(V1IsUndef, V2IsUndef);
3276 Commuted = true;
3277 }
3278
3279 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3280 if (V2IsUndef) return V1;
3281 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3282 if (V2IsSplat) {
3283 // V2 is a splat, so the mask may be malformed. That is, it may point
3284 // to any V2 element. The instruction selectior won't like this. Get
3285 // a corrected mask and commute to form a proper MOVS{S|D}.
3286 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3287 if (NewMask.Val != PermMask.Val)
3288 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3289 }
3290 return Op;
3291 }
3292
3293 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3294 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3295 X86::isUNPCKLMask(PermMask.Val) ||
3296 X86::isUNPCKHMask(PermMask.Val))
3297 return Op;
3298
3299 if (V2IsSplat) {
3300 // Normalize mask so all entries that point to V2 points to its first
3301 // element then try to match unpck{h|l} again. If match, return a
3302 // new vector_shuffle with the corrected mask.
3303 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3304 if (NewMask.Val != PermMask.Val) {
3305 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3306 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3307 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3308 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3309 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3310 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3311 }
3312 }
3313 }
3314
3315 // Normalize the node to match x86 shuffle ops if needed
3316 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3317 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3318
3319 if (Commuted) {
3320 // Commute is back and try unpck* again.
3321 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3322 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3323 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3324 X86::isUNPCKLMask(PermMask.Val) ||
3325 X86::isUNPCKHMask(PermMask.Val))
3326 return Op;
3327 }
3328
3329 // If VT is integer, try PSHUF* first, then SHUFP*.
3330 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003331 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3332 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3333 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3334 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003335 X86::isPSHUFHWMask(PermMask.Val) ||
3336 X86::isPSHUFLWMask(PermMask.Val)) {
3337 if (V2.getOpcode() != ISD::UNDEF)
3338 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3339 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3340 return Op;
3341 }
3342
3343 if (X86::isSHUFPMask(PermMask.Val) &&
3344 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3345 return Op;
3346
3347 // Handle v8i16 shuffle high / low shuffle node pair.
3348 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3349 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3350 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3351 SmallVector<SDOperand, 8> MaskVec;
3352 for (unsigned i = 0; i != 4; ++i)
3353 MaskVec.push_back(PermMask.getOperand(i));
3354 for (unsigned i = 4; i != 8; ++i)
3355 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3356 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3357 &MaskVec[0], MaskVec.size());
3358 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3359 MaskVec.clear();
3360 for (unsigned i = 0; i != 4; ++i)
3361 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3362 for (unsigned i = 4; i != 8; ++i)
3363 MaskVec.push_back(PermMask.getOperand(i));
3364 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
3365 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3366 }
3367 } else {
3368 // Floating point cases in the other order.
3369 if (X86::isSHUFPMask(PermMask.Val))
3370 return Op;
3371 if (X86::isPSHUFDMask(PermMask.Val) ||
3372 X86::isPSHUFHWMask(PermMask.Val) ||
3373 X86::isPSHUFLWMask(PermMask.Val)) {
3374 if (V2.getOpcode() != ISD::UNDEF)
3375 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3376 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3377 return Op;
3378 }
3379 }
3380
3381 if (NumElems == 4 &&
3382 // Don't do this for MMX.
3383 MVT::getSizeInBits(VT) != 64) {
3384 MVT::ValueType MaskVT = PermMask.getValueType();
3385 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3386 SmallVector<std::pair<int, int>, 8> Locs;
3387 Locs.reserve(NumElems);
3388 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3389 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3390 unsigned NumHi = 0;
3391 unsigned NumLo = 0;
3392 // If no more than two elements come from either vector. This can be
3393 // implemented with two shuffles. First shuffle gather the elements.
3394 // The second shuffle, which takes the first shuffle as both of its
3395 // vector operands, put the elements into the right order.
3396 for (unsigned i = 0; i != NumElems; ++i) {
3397 SDOperand Elt = PermMask.getOperand(i);
3398 if (Elt.getOpcode() == ISD::UNDEF) {
3399 Locs[i] = std::make_pair(-1, -1);
3400 } else {
3401 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3402 if (Val < NumElems) {
3403 Locs[i] = std::make_pair(0, NumLo);
3404 Mask1[NumLo] = Elt;
3405 NumLo++;
3406 } else {
3407 Locs[i] = std::make_pair(1, NumHi);
3408 if (2+NumHi < NumElems)
3409 Mask1[2+NumHi] = Elt;
3410 NumHi++;
3411 }
3412 }
3413 }
3414 if (NumLo <= 2 && NumHi <= 2) {
3415 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3416 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3417 &Mask1[0], Mask1.size()));
3418 for (unsigned i = 0; i != NumElems; ++i) {
3419 if (Locs[i].first == -1)
3420 continue;
3421 else {
3422 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3423 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3424 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3425 }
3426 }
3427
3428 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3429 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3430 &Mask2[0], Mask2.size()));
3431 }
3432
3433 // Break it into (shuffle shuffle_hi, shuffle_lo).
3434 Locs.clear();
3435 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3436 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3437 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3438 unsigned MaskIdx = 0;
3439 unsigned LoIdx = 0;
3440 unsigned HiIdx = NumElems/2;
3441 for (unsigned i = 0; i != NumElems; ++i) {
3442 if (i == NumElems/2) {
3443 MaskPtr = &HiMask;
3444 MaskIdx = 1;
3445 LoIdx = 0;
3446 HiIdx = NumElems/2;
3447 }
3448 SDOperand Elt = PermMask.getOperand(i);
3449 if (Elt.getOpcode() == ISD::UNDEF) {
3450 Locs[i] = std::make_pair(-1, -1);
3451 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3452 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3453 (*MaskPtr)[LoIdx] = Elt;
3454 LoIdx++;
3455 } else {
3456 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3457 (*MaskPtr)[HiIdx] = Elt;
3458 HiIdx++;
3459 }
3460 }
3461
3462 SDOperand LoShuffle =
3463 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3464 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3465 &LoMask[0], LoMask.size()));
3466 SDOperand HiShuffle =
3467 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3468 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3469 &HiMask[0], HiMask.size()));
3470 SmallVector<SDOperand, 8> MaskOps;
3471 for (unsigned i = 0; i != NumElems; ++i) {
3472 if (Locs[i].first == -1) {
3473 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3474 } else {
3475 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3476 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3477 }
3478 }
3479 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3480 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3481 &MaskOps[0], MaskOps.size()));
3482 }
3483
3484 return SDOperand();
3485}
3486
3487SDOperand
3488X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3489 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3490 return SDOperand();
3491
3492 MVT::ValueType VT = Op.getValueType();
3493 // TODO: handle v16i8.
3494 if (MVT::getSizeInBits(VT) == 16) {
3495 // Transform it so it match pextrw which produces a 32-bit result.
3496 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3497 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3498 Op.getOperand(0), Op.getOperand(1));
3499 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3500 DAG.getValueType(VT));
3501 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3502 } else if (MVT::getSizeInBits(VT) == 32) {
3503 SDOperand Vec = Op.getOperand(0);
3504 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3505 if (Idx == 0)
3506 return Op;
3507 // SHUFPS the element to the lowest double word, then movss.
3508 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3509 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003510 IdxVec.
3511 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3512 IdxVec.
3513 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3514 IdxVec.
3515 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3516 IdxVec.
3517 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003518 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3519 &IdxVec[0], IdxVec.size());
3520 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3521 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3522 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3523 DAG.getConstant(0, getPointerTy()));
3524 } else if (MVT::getSizeInBits(VT) == 64) {
3525 SDOperand Vec = Op.getOperand(0);
3526 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3527 if (Idx == 0)
3528 return Op;
3529
3530 // UNPCKHPD the element to the lowest double word, then movsd.
3531 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3532 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3533 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3534 SmallVector<SDOperand, 8> IdxVec;
3535 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003536 IdxVec.
3537 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003538 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3539 &IdxVec[0], IdxVec.size());
3540 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3541 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3542 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3543 DAG.getConstant(0, getPointerTy()));
3544 }
3545
3546 return SDOperand();
3547}
3548
3549SDOperand
3550X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3551 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3552 // as its second argument.
3553 MVT::ValueType VT = Op.getValueType();
3554 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
3555 SDOperand N0 = Op.getOperand(0);
3556 SDOperand N1 = Op.getOperand(1);
3557 SDOperand N2 = Op.getOperand(2);
3558 if (MVT::getSizeInBits(BaseVT) == 16) {
3559 if (N1.getValueType() != MVT::i32)
3560 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3561 if (N2.getValueType() != MVT::i32)
3562 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
3563 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3564 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3565 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3566 if (Idx == 0) {
3567 // Use a movss.
3568 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3569 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3570 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3571 SmallVector<SDOperand, 8> MaskVec;
3572 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3573 for (unsigned i = 1; i <= 3; ++i)
3574 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3575 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3576 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3577 &MaskVec[0], MaskVec.size()));
3578 } else {
3579 // Use two pinsrw instructions to insert a 32 bit value.
3580 Idx <<= 1;
3581 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng1eea6752007-07-31 06:21:44 +00003582 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3583 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3584 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3585 DAG.getConstant(0, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003586 }
3587 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3588 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3589 DAG.getConstant(Idx, getPointerTy()));
3590 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3591 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3592 DAG.getConstant(Idx+1, getPointerTy()));
3593 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3594 }
3595 }
3596
3597 return SDOperand();
3598}
3599
3600SDOperand
3601X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3602 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3603 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3604}
3605
3606// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3607// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3608// one of the above mentioned nodes. It has to be wrapped because otherwise
3609// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3610// be used to form addressing mode. These wrapped nodes will be selected
3611// into MOV32ri.
3612SDOperand
3613X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3614 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3615 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3616 getPointerTy(),
3617 CP->getAlignment());
3618 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3619 // With PIC, the address is actually $g + Offset.
3620 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3621 !Subtarget->isPICStyleRIPRel()) {
3622 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3623 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3624 Result);
3625 }
3626
3627 return Result;
3628}
3629
3630SDOperand
3631X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3632 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3633 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3634 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3635 // With PIC, the address is actually $g + Offset.
3636 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3637 !Subtarget->isPICStyleRIPRel()) {
3638 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3639 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3640 Result);
3641 }
3642
3643 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3644 // load the value at address GV, not the value of GV itself. This means that
3645 // the GlobalAddress must be in the base or index register of the address, not
3646 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3647 // The same applies for external symbols during PIC codegen
3648 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3649 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3650
3651 return Result;
3652}
3653
3654// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3655static SDOperand
3656LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3657 const MVT::ValueType PtrVT) {
3658 SDOperand InFlag;
3659 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3660 DAG.getNode(X86ISD::GlobalBaseReg,
3661 PtrVT), InFlag);
3662 InFlag = Chain.getValue(1);
3663
3664 // emit leal symbol@TLSGD(,%ebx,1), %eax
3665 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3666 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3667 GA->getValueType(0),
3668 GA->getOffset());
3669 SDOperand Ops[] = { Chain, TGA, InFlag };
3670 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3671 InFlag = Result.getValue(2);
3672 Chain = Result.getValue(1);
3673
3674 // call ___tls_get_addr. This function receives its argument in
3675 // the register EAX.
3676 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3677 InFlag = Chain.getValue(1);
3678
3679 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3680 SDOperand Ops1[] = { Chain,
3681 DAG.getTargetExternalSymbol("___tls_get_addr",
3682 PtrVT),
3683 DAG.getRegister(X86::EAX, PtrVT),
3684 DAG.getRegister(X86::EBX, PtrVT),
3685 InFlag };
3686 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3687 InFlag = Chain.getValue(1);
3688
3689 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3690}
3691
3692// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3693// "local exec" model.
3694static SDOperand
3695LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3696 const MVT::ValueType PtrVT) {
3697 // Get the Thread Pointer
3698 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3699 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3700 // exec)
3701 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3702 GA->getValueType(0),
3703 GA->getOffset());
3704 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3705
3706 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3707 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3708
3709 // The address of the thread local variable is the add of the thread
3710 // pointer with the offset of the variable.
3711 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3712}
3713
3714SDOperand
3715X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3716 // TODO: implement the "local dynamic" model
3717 // TODO: implement the "initial exec"model for pic executables
3718 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3719 "TLS not implemented for non-ELF and 64-bit targets");
3720 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3721 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3722 // otherwise use the "Local Exec"TLS Model
3723 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3724 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3725 else
3726 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3727}
3728
3729SDOperand
3730X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3731 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3732 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3733 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3734 // With PIC, the address is actually $g + Offset.
3735 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3736 !Subtarget->isPICStyleRIPRel()) {
3737 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3738 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3739 Result);
3740 }
3741
3742 return Result;
3743}
3744
3745SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3746 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3747 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3748 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3749 // With PIC, the address is actually $g + Offset.
3750 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3751 !Subtarget->isPICStyleRIPRel()) {
3752 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3753 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3754 Result);
3755 }
3756
3757 return Result;
3758}
3759
3760SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
3761 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3762 "Not an i64 shift!");
3763 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3764 SDOperand ShOpLo = Op.getOperand(0);
3765 SDOperand ShOpHi = Op.getOperand(1);
3766 SDOperand ShAmt = Op.getOperand(2);
3767 SDOperand Tmp1 = isSRA ?
3768 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3769 DAG.getConstant(0, MVT::i32);
3770
3771 SDOperand Tmp2, Tmp3;
3772 if (Op.getOpcode() == ISD::SHL_PARTS) {
3773 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3774 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3775 } else {
3776 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3777 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3778 }
3779
3780 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3781 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3782 DAG.getConstant(32, MVT::i8));
Evan Cheng621216e2007-09-29 00:00:36 +00003783 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3784 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785
3786 SDOperand Hi, Lo;
3787 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00003788 unsigned Opc = X86ISD::CMOV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3790 SmallVector<SDOperand, 4> Ops;
3791 if (Op.getOpcode() == ISD::SHL_PARTS) {
3792 Ops.push_back(Tmp2);
3793 Ops.push_back(Tmp3);
3794 Ops.push_back(CC);
Evan Cheng950aac02007-09-25 01:57:46 +00003795 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00003796 Hi = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003797
3798 Ops.clear();
3799 Ops.push_back(Tmp3);
3800 Ops.push_back(Tmp1);
3801 Ops.push_back(CC);
Evan Cheng950aac02007-09-25 01:57:46 +00003802 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00003803 Lo = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003804 } else {
3805 Ops.push_back(Tmp2);
3806 Ops.push_back(Tmp3);
3807 Ops.push_back(CC);
Evan Cheng950aac02007-09-25 01:57:46 +00003808 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00003809 Lo = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810
3811 Ops.clear();
3812 Ops.push_back(Tmp3);
3813 Ops.push_back(Tmp1);
3814 Ops.push_back(CC);
Evan Cheng950aac02007-09-25 01:57:46 +00003815 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00003816 Hi = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003817 }
3818
3819 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3820 Ops.clear();
3821 Ops.push_back(Lo);
3822 Ops.push_back(Hi);
3823 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
3824}
3825
3826SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3827 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3828 Op.getOperand(0).getValueType() >= MVT::i16 &&
3829 "Unknown SINT_TO_FP to lower!");
3830
3831 SDOperand Result;
3832 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3833 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3834 MachineFunction &MF = DAG.getMachineFunction();
3835 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3836 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3837 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3838 StackSlot, NULL, 0);
3839
Dale Johannesen2fc20782007-09-14 22:26:36 +00003840 // These are really Legal; caller falls through into that case.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003841 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
3842 return Result;
3843 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen2fc20782007-09-14 22:26:36 +00003844 return Result;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003845 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
3846 Subtarget->is64Bit())
3847 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00003848
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003849 // Build the FILD
3850 SDVTList Tys;
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003851 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
3852 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen2fc20782007-09-14 22:26:36 +00003853 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003854 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3855 else
3856 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
3857 SmallVector<SDOperand, 8> Ops;
3858 Ops.push_back(Chain);
3859 Ops.push_back(StackSlot);
3860 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00003861 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862 Tys, &Ops[0], Ops.size());
3863
Dale Johannesen2fc20782007-09-14 22:26:36 +00003864 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 Chain = Result.getValue(1);
3866 SDOperand InFlag = Result.getValue(2);
3867
3868 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3869 // shouldn't be necessary except that RFP cannot be live across
3870 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3871 MachineFunction &MF = DAG.getMachineFunction();
3872 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3873 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3874 Tys = DAG.getVTList(MVT::Other);
3875 SmallVector<SDOperand, 8> Ops;
3876 Ops.push_back(Chain);
3877 Ops.push_back(Result);
3878 Ops.push_back(StackSlot);
3879 Ops.push_back(DAG.getValueType(Op.getValueType()));
3880 Ops.push_back(InFlag);
3881 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3882 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3883 }
3884
3885 return Result;
3886}
3887
3888SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3889 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3890 "Unknown FP_TO_SINT to lower!");
3891 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3892 // stack slot.
Dale Johannesen2fc20782007-09-14 22:26:36 +00003893 SDOperand Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003894 MachineFunction &MF = DAG.getMachineFunction();
3895 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3896 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3897 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3898
Dale Johannesen2fc20782007-09-14 22:26:36 +00003899 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003900 if (Op.getValueType() == MVT::i32 &&
3901 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
3902 return Result;
3903 if (Op.getValueType() == MVT::i32 &&
3904 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Dale Johannesen2fc20782007-09-14 22:26:36 +00003905 return Result;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003906 if (Subtarget->is64Bit() &&
3907 Op.getValueType() == MVT::i64 &&
3908 Op.getOperand(0).getValueType() != MVT::f80)
3909 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00003910
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911 unsigned Opc;
3912 switch (Op.getValueType()) {
3913 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3914 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3915 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3916 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
3917 }
3918
3919 SDOperand Chain = DAG.getEntryNode();
3920 SDOperand Value = Op.getOperand(0);
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003921 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
3922 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3924 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3925 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
3926 SDOperand Ops[] = {
3927 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3928 };
3929 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3930 Chain = Value.getValue(1);
3931 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3932 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3933 }
3934
3935 // Build the FP_TO_INT*_IN_MEM
3936 SDOperand Ops[] = { Chain, Value, StackSlot };
3937 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3938
3939 // Load the result.
3940 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3941}
3942
3943SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3944 MVT::ValueType VT = Op.getValueType();
3945 MVT::ValueType EltVT = VT;
3946 if (MVT::isVector(VT))
3947 EltVT = MVT::getVectorElementType(VT);
3948 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3949 std::vector<Constant*> CV;
3950 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00003951 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003952 CV.push_back(C);
3953 CV.push_back(C);
3954 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00003955 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003956 CV.push_back(C);
3957 CV.push_back(C);
3958 CV.push_back(C);
3959 CV.push_back(C);
3960 }
Dan Gohman11821702007-07-27 17:16:43 +00003961 Constant *C = ConstantVector::get(CV);
3962 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3963 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3964 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003965 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3966}
3967
3968SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3969 MVT::ValueType VT = Op.getValueType();
3970 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00003971 unsigned EltNum = 1;
3972 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00003974 EltNum = MVT::getVectorNumElements(VT);
3975 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003976 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3977 std::vector<Constant*> CV;
3978 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00003979 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 CV.push_back(C);
3981 CV.push_back(C);
3982 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00003983 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003984 CV.push_back(C);
3985 CV.push_back(C);
3986 CV.push_back(C);
3987 CV.push_back(C);
3988 }
Dan Gohman11821702007-07-27 17:16:43 +00003989 Constant *C = ConstantVector::get(CV);
3990 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3991 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3992 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00003993 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00003994 return DAG.getNode(ISD::BIT_CONVERT, VT,
3995 DAG.getNode(ISD::XOR, MVT::v2i64,
3996 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
3997 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
3998 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00003999 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4000 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004001}
4002
4003SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4004 SDOperand Op0 = Op.getOperand(0);
4005 SDOperand Op1 = Op.getOperand(1);
4006 MVT::ValueType VT = Op.getValueType();
4007 MVT::ValueType SrcVT = Op1.getValueType();
4008 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4009
4010 // If second operand is smaller, extend it first.
4011 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4012 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4013 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004014 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004015 }
4016
4017 // First get the sign bit of second operand.
4018 std::vector<Constant*> CV;
4019 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004020 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4021 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004023 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4024 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4025 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4026 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027 }
Dan Gohman11821702007-07-27 17:16:43 +00004028 Constant *C = ConstantVector::get(CV);
4029 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4030 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4031 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004032 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4033
4034 // Shift sign bit right or left if the two operands have different types.
4035 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4036 // Op0 is MVT::f32, Op1 is MVT::f64.
4037 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4038 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4039 DAG.getConstant(32, MVT::i32));
4040 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4041 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4042 DAG.getConstant(0, getPointerTy()));
4043 }
4044
4045 // Clear first operand sign bit.
4046 CV.clear();
4047 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004048 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4049 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004051 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4052 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4053 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4054 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 }
Dan Gohman11821702007-07-27 17:16:43 +00004056 C = ConstantVector::get(CV);
4057 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4058 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4059 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004060 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4061
4062 // Or the value with the sign bit.
4063 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4064}
4065
Evan Cheng621216e2007-09-29 00:00:36 +00004066SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004067 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004068 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004069 SDOperand Op0 = Op.getOperand(0);
4070 SDOperand Op1 = Op.getOperand(1);
4071 SDOperand CC = Op.getOperand(2);
4072 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4073 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4074 unsigned X86CC;
4075
Evan Cheng950aac02007-09-25 01:57:46 +00004076 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004077 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004078 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4079 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004080 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004081 }
Evan Cheng950aac02007-09-25 01:57:46 +00004082
4083 assert(isFP && "Illegal integer SetCC!");
4084
Evan Cheng621216e2007-09-29 00:00:36 +00004085 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004086 switch (SetCCOpcode) {
4087 default: assert(false && "Illegal floating point SetCC!");
4088 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004089 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004090 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004091 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004092 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4093 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4094 }
4095 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004096 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004097 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004098 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004099 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4100 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4101 }
4102 }
4103}
4104
4105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4107 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108 SDOperand Cond = Op.getOperand(0);
4109 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110
4111 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004112 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113
Evan Cheng50d37ab2007-10-08 22:16:29 +00004114 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4115 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004116 if (Cond.getOpcode() == X86ISD::SETCC) {
4117 CC = Cond.getOperand(0);
4118
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 SDOperand Cmp = Cond.getOperand(1);
4120 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004121 MVT::ValueType VT = Op.getValueType();
4122 bool IllegalFPCMov = false;
4123 if (VT == MVT::f32 && !X86ScalarSSEf32)
4124 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4125 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4126 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng621216e2007-09-29 00:00:36 +00004127 if ((Opc == X86ISD::CMP ||
4128 Opc == X86ISD::COMI ||
4129 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004130 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004131 addTest = false;
4132 }
4133 }
4134
4135 if (addTest) {
4136 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004137 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004138 }
4139
4140 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4141 MVT::Flag);
4142 SmallVector<SDOperand, 4> Ops;
4143 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4144 // condition is true.
4145 Ops.push_back(Op.getOperand(2));
4146 Ops.push_back(Op.getOperand(1));
4147 Ops.push_back(CC);
4148 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004149 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004150}
4151
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4153 bool addTest = true;
4154 SDOperand Chain = Op.getOperand(0);
4155 SDOperand Cond = Op.getOperand(1);
4156 SDOperand Dest = Op.getOperand(2);
4157 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158
4159 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004160 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004161
Evan Cheng50d37ab2007-10-08 22:16:29 +00004162 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4163 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164 if (Cond.getOpcode() == X86ISD::SETCC) {
4165 CC = Cond.getOperand(0);
4166
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 SDOperand Cmp = Cond.getOperand(1);
4168 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004169 if (Opc == X86ISD::CMP ||
4170 Opc == X86ISD::COMI ||
4171 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004172 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004173 addTest = false;
4174 }
4175 }
4176
4177 if (addTest) {
4178 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004179 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004180 }
Evan Cheng621216e2007-09-29 00:00:36 +00004181 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004182 Chain, Op.getOperand(2), CC, Cond);
4183}
4184
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004186 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4187 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004189 if (Subtarget->is64Bit())
4190 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4191 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4192 else
4193 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194 else
4195 switch (CallingConv) {
4196 default:
4197 assert(0 && "Unsupported calling convention");
4198 case CallingConv::Fast:
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004199 if (isTailCall && PerformTailCallOpt)
4200 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4201 else
4202 return LowerCCCCallTo(Op,DAG, CallingConv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203 case CallingConv::C:
4204 case CallingConv::X86_StdCall:
4205 return LowerCCCCallTo(Op, DAG, CallingConv);
4206 case CallingConv::X86_FastCall:
4207 return LowerFastCCCallTo(Op, DAG, CallingConv);
4208 }
4209}
4210
4211
4212// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4213// Calls to _alloca is needed to probe the stack when allocating more than 4k
4214// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4215// that the guard pages used by the OS virtual memory manager are allocated in
4216// correct sequence.
4217SDOperand
4218X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4219 SelectionDAG &DAG) {
4220 assert(Subtarget->isTargetCygMing() &&
4221 "This should be used only on Cygwin/Mingw targets");
4222
4223 // Get the inputs.
4224 SDOperand Chain = Op.getOperand(0);
4225 SDOperand Size = Op.getOperand(1);
4226 // FIXME: Ensure alignment here
4227
4228 SDOperand Flag;
4229
4230 MVT::ValueType IntPtr = getPointerTy();
4231 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
4232
4233 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4234 Flag = Chain.getValue(1);
4235
4236 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4237 SDOperand Ops[] = { Chain,
4238 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4239 DAG.getRegister(X86::EAX, IntPtr),
4240 Flag };
4241 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4242 Flag = Chain.getValue(1);
4243
4244 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4245
4246 std::vector<MVT::ValueType> Tys;
4247 Tys.push_back(SPTy);
4248 Tys.push_back(MVT::Other);
4249 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4250 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4251}
4252
4253SDOperand
4254X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
4255 MachineFunction &MF = DAG.getMachineFunction();
4256 const Function* Fn = MF.getFunction();
4257 if (Fn->hasExternalLinkage() &&
4258 Subtarget->isTargetCygMing() &&
4259 Fn->getName() == "main")
4260 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
4261
4262 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4263 if (Subtarget->is64Bit())
4264 return LowerX86_64CCCArguments(Op, DAG);
4265 else
4266 switch(CC) {
4267 default:
4268 assert(0 && "Unsupported calling convention");
4269 case CallingConv::Fast:
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004270 return LowerCCCArguments(Op,DAG, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271 // Falls through
4272 case CallingConv::C:
4273 return LowerCCCArguments(Op, DAG);
4274 case CallingConv::X86_StdCall:
4275 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
4276 return LowerCCCArguments(Op, DAG, true);
4277 case CallingConv::X86_FastCall:
4278 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
4279 return LowerFastCCArguments(Op, DAG);
4280 }
4281}
4282
4283SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4284 SDOperand InFlag(0, 0);
4285 SDOperand Chain = Op.getOperand(0);
4286 unsigned Align =
4287 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4288 if (Align == 0) Align = 1;
4289
4290 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004291 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004292 // The libc version is likely to be faster for these cases. It can use the
4293 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004294 if ((Align & 3) != 0 ||
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004295 (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 MVT::ValueType IntPtr = getPointerTy();
4297 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4298 TargetLowering::ArgListTy Args;
4299 TargetLowering::ArgListEntry Entry;
4300 Entry.Node = Op.getOperand(1);
4301 Entry.Ty = IntPtrTy;
4302 Args.push_back(Entry);
4303 // Extend the unsigned i8 argument to be an int value for the call.
4304 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4305 Entry.Ty = IntPtrTy;
4306 Args.push_back(Entry);
4307 Entry.Node = Op.getOperand(3);
4308 Args.push_back(Entry);
4309 std::pair<SDOperand,SDOperand> CallResult =
4310 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4311 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4312 return CallResult.second;
4313 }
4314
4315 MVT::ValueType AVT;
4316 SDOperand Count;
4317 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4318 unsigned BytesLeft = 0;
4319 bool TwoRepStos = false;
4320 if (ValC) {
4321 unsigned ValReg;
4322 uint64_t Val = ValC->getValue() & 255;
4323
4324 // If the value is a constant, then we can potentially use larger sets.
4325 switch (Align & 3) {
4326 case 2: // WORD aligned
4327 AVT = MVT::i16;
4328 ValReg = X86::AX;
4329 Val = (Val << 8) | Val;
4330 break;
4331 case 0: // DWORD aligned
4332 AVT = MVT::i32;
4333 ValReg = X86::EAX;
4334 Val = (Val << 8) | Val;
4335 Val = (Val << 16) | Val;
4336 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4337 AVT = MVT::i64;
4338 ValReg = X86::RAX;
4339 Val = (Val << 32) | Val;
4340 }
4341 break;
4342 default: // Byte aligned
4343 AVT = MVT::i8;
4344 ValReg = X86::AL;
4345 Count = Op.getOperand(3);
4346 break;
4347 }
4348
4349 if (AVT > MVT::i8) {
4350 if (I) {
4351 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4352 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4353 BytesLeft = I->getValue() % UBytes;
4354 } else {
4355 assert(AVT >= MVT::i32 &&
4356 "Do not use rep;stos if not at least DWORD aligned");
4357 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4358 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4359 TwoRepStos = true;
4360 }
4361 }
4362
4363 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4364 InFlag);
4365 InFlag = Chain.getValue(1);
4366 } else {
4367 AVT = MVT::i8;
4368 Count = Op.getOperand(3);
4369 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4370 InFlag = Chain.getValue(1);
4371 }
4372
4373 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4374 Count, InFlag);
4375 InFlag = Chain.getValue(1);
4376 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4377 Op.getOperand(1), InFlag);
4378 InFlag = Chain.getValue(1);
4379
4380 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4381 SmallVector<SDOperand, 8> Ops;
4382 Ops.push_back(Chain);
4383 Ops.push_back(DAG.getValueType(AVT));
4384 Ops.push_back(InFlag);
4385 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4386
4387 if (TwoRepStos) {
4388 InFlag = Chain.getValue(1);
4389 Count = Op.getOperand(3);
4390 MVT::ValueType CVT = Count.getValueType();
4391 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4392 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4393 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4394 Left, InFlag);
4395 InFlag = Chain.getValue(1);
4396 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4397 Ops.clear();
4398 Ops.push_back(Chain);
4399 Ops.push_back(DAG.getValueType(MVT::i8));
4400 Ops.push_back(InFlag);
4401 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4402 } else if (BytesLeft) {
4403 // Issue stores for the last 1 - 7 bytes.
4404 SDOperand Value;
4405 unsigned Val = ValC->getValue() & 255;
4406 unsigned Offset = I->getValue() - BytesLeft;
4407 SDOperand DstAddr = Op.getOperand(1);
4408 MVT::ValueType AddrVT = DstAddr.getValueType();
4409 if (BytesLeft >= 4) {
4410 Val = (Val << 8) | Val;
4411 Val = (Val << 16) | Val;
4412 Value = DAG.getConstant(Val, MVT::i32);
4413 Chain = DAG.getStore(Chain, Value,
4414 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4415 DAG.getConstant(Offset, AddrVT)),
4416 NULL, 0);
4417 BytesLeft -= 4;
4418 Offset += 4;
4419 }
4420 if (BytesLeft >= 2) {
4421 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4422 Chain = DAG.getStore(Chain, Value,
4423 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4424 DAG.getConstant(Offset, AddrVT)),
4425 NULL, 0);
4426 BytesLeft -= 2;
4427 Offset += 2;
4428 }
4429 if (BytesLeft == 1) {
4430 Value = DAG.getConstant(Val, MVT::i8);
4431 Chain = DAG.getStore(Chain, Value,
4432 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4433 DAG.getConstant(Offset, AddrVT)),
4434 NULL, 0);
4435 }
4436 }
4437
4438 return Chain;
4439}
4440
4441SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004442 SDOperand ChainOp = Op.getOperand(0);
4443 SDOperand DestOp = Op.getOperand(1);
4444 SDOperand SourceOp = Op.getOperand(2);
4445 SDOperand CountOp = Op.getOperand(3);
4446 SDOperand AlignOp = Op.getOperand(4);
4447 unsigned Align = (unsigned)cast<ConstantSDNode>(AlignOp)->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 if (Align == 0) Align = 1;
4449
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004450 // The libc version is likely to be faster for the following cases. It can
4451 // use the address value and run time information about the CPU.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004452 // 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 +00004453
4454 // If not DWORD aligned, call memcpy.
4455 if ((Align & 3) != 0)
4456 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4457
4458 // If size is unknown, call memcpy.
4459 ConstantSDNode *I = dyn_cast<ConstantSDNode>(CountOp);
4460 if (!I)
4461 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4462
4463 // If size is more than the threshold, call memcpy.
4464 unsigned Size = I->getValue();
4465 if (Size > Subtarget->getMinRepStrSizeThreshold())
4466 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4467
4468 return LowerMEMCPYInline(ChainOp, DestOp, SourceOp, Size, Align, DAG);
4469}
4470
4471SDOperand X86TargetLowering::LowerMEMCPYCall(SDOperand Chain,
4472 SDOperand Dest,
4473 SDOperand Source,
4474 SDOperand Count,
4475 SelectionDAG &DAG) {
4476 MVT::ValueType IntPtr = getPointerTy();
4477 TargetLowering::ArgListTy Args;
4478 TargetLowering::ArgListEntry Entry;
4479 Entry.Ty = getTargetData()->getIntPtrType();
4480 Entry.Node = Dest; Args.push_back(Entry);
4481 Entry.Node = Source; Args.push_back(Entry);
4482 Entry.Node = Count; Args.push_back(Entry);
4483 std::pair<SDOperand,SDOperand> CallResult =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4485 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004486 return CallResult.second;
4487}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004489SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4490 SDOperand Dest,
4491 SDOperand Source,
4492 unsigned Size,
4493 unsigned Align,
4494 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004496 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 switch (Align & 3) {
4498 case 2: // WORD aligned
4499 AVT = MVT::i16;
4500 break;
4501 case 0: // DWORD aligned
4502 AVT = MVT::i32;
4503 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4504 AVT = MVT::i64;
4505 break;
4506 default: // Byte aligned
4507 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508 break;
4509 }
4510
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004511 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4512 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4513 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004514
4515 SDOperand InFlag(0, 0);
4516 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4517 Count, InFlag);
4518 InFlag = Chain.getValue(1);
4519 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004520 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004521 InFlag = Chain.getValue(1);
4522 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004523 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004524 InFlag = Chain.getValue(1);
4525
4526 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4527 SmallVector<SDOperand, 8> Ops;
4528 Ops.push_back(Chain);
4529 Ops.push_back(DAG.getValueType(AVT));
4530 Ops.push_back(InFlag);
4531 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4532
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004533 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004535 unsigned Offset = Size - BytesLeft;
4536 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004538 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539 MVT::ValueType SrcVT = SrcAddr.getValueType();
4540 SDOperand Value;
4541 if (BytesLeft >= 4) {
4542 Value = DAG.getLoad(MVT::i32, Chain,
4543 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4544 DAG.getConstant(Offset, SrcVT)),
4545 NULL, 0);
4546 Chain = Value.getValue(1);
4547 Chain = DAG.getStore(Chain, Value,
4548 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4549 DAG.getConstant(Offset, DstVT)),
4550 NULL, 0);
4551 BytesLeft -= 4;
4552 Offset += 4;
4553 }
4554 if (BytesLeft >= 2) {
4555 Value = DAG.getLoad(MVT::i16, Chain,
4556 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4557 DAG.getConstant(Offset, SrcVT)),
4558 NULL, 0);
4559 Chain = Value.getValue(1);
4560 Chain = DAG.getStore(Chain, Value,
4561 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4562 DAG.getConstant(Offset, DstVT)),
4563 NULL, 0);
4564 BytesLeft -= 2;
4565 Offset += 2;
4566 }
4567
4568 if (BytesLeft == 1) {
4569 Value = DAG.getLoad(MVT::i8, Chain,
4570 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4571 DAG.getConstant(Offset, SrcVT)),
4572 NULL, 0);
4573 Chain = Value.getValue(1);
4574 Chain = DAG.getStore(Chain, Value,
4575 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4576 DAG.getConstant(Offset, DstVT)),
4577 NULL, 0);
4578 }
4579 }
4580
4581 return Chain;
4582}
4583
4584SDOperand
4585X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4586 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4587 SDOperand TheOp = Op.getOperand(0);
4588 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
4589 if (Subtarget->is64Bit()) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004590 SDOperand Copy1 =
4591 DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4593 MVT::i64, Copy1.getValue(2));
4594 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4595 DAG.getConstant(32, MVT::i8));
4596 SDOperand Ops[] = {
4597 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4598 };
4599
4600 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4601 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
4602 }
4603
4604 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4605 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4606 MVT::i32, Copy1.getValue(2));
4607 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4608 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4609 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
4610}
4611
4612SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4613 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4614
4615 if (!Subtarget->is64Bit()) {
4616 // vastart just stores the address of the VarArgsFrameIndex slot into the
4617 // memory location argument.
4618 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4619 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4620 SV->getOffset());
4621 }
4622
4623 // __va_list_tag:
4624 // gp_offset (0 - 6 * 8)
4625 // fp_offset (48 - 48 + 8 * 16)
4626 // overflow_arg_area (point to parameters coming in memory).
4627 // reg_save_area
4628 SmallVector<SDOperand, 8> MemOps;
4629 SDOperand FIN = Op.getOperand(1);
4630 // Store gp_offset
4631 SDOperand Store = DAG.getStore(Op.getOperand(0),
4632 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4633 FIN, SV->getValue(), SV->getOffset());
4634 MemOps.push_back(Store);
4635
4636 // Store fp_offset
4637 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4638 DAG.getConstant(4, getPointerTy()));
4639 Store = DAG.getStore(Op.getOperand(0),
4640 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4641 FIN, SV->getValue(), SV->getOffset());
4642 MemOps.push_back(Store);
4643
4644 // Store ptr to overflow_arg_area
4645 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4646 DAG.getConstant(4, getPointerTy()));
4647 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4648 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4649 SV->getOffset());
4650 MemOps.push_back(Store);
4651
4652 // Store ptr to reg_save_area.
4653 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4654 DAG.getConstant(8, getPointerTy()));
4655 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4656 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4657 SV->getOffset());
4658 MemOps.push_back(Store);
4659 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4660}
4661
4662SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4663 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4664 SDOperand Chain = Op.getOperand(0);
4665 SDOperand DstPtr = Op.getOperand(1);
4666 SDOperand SrcPtr = Op.getOperand(2);
4667 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4668 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4669
4670 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4671 SrcSV->getValue(), SrcSV->getOffset());
4672 Chain = SrcPtr.getValue(1);
4673 for (unsigned i = 0; i < 3; ++i) {
4674 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4675 SrcSV->getValue(), SrcSV->getOffset());
4676 Chain = Val.getValue(1);
4677 Chain = DAG.getStore(Chain, Val, DstPtr,
4678 DstSV->getValue(), DstSV->getOffset());
4679 if (i == 2)
4680 break;
4681 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4682 DAG.getConstant(8, getPointerTy()));
4683 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4684 DAG.getConstant(8, getPointerTy()));
4685 }
4686 return Chain;
4687}
4688
4689SDOperand
4690X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4691 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4692 switch (IntNo) {
4693 default: return SDOperand(); // Don't custom lower most intrinsics.
4694 // Comparison intrinsics.
4695 case Intrinsic::x86_sse_comieq_ss:
4696 case Intrinsic::x86_sse_comilt_ss:
4697 case Intrinsic::x86_sse_comile_ss:
4698 case Intrinsic::x86_sse_comigt_ss:
4699 case Intrinsic::x86_sse_comige_ss:
4700 case Intrinsic::x86_sse_comineq_ss:
4701 case Intrinsic::x86_sse_ucomieq_ss:
4702 case Intrinsic::x86_sse_ucomilt_ss:
4703 case Intrinsic::x86_sse_ucomile_ss:
4704 case Intrinsic::x86_sse_ucomigt_ss:
4705 case Intrinsic::x86_sse_ucomige_ss:
4706 case Intrinsic::x86_sse_ucomineq_ss:
4707 case Intrinsic::x86_sse2_comieq_sd:
4708 case Intrinsic::x86_sse2_comilt_sd:
4709 case Intrinsic::x86_sse2_comile_sd:
4710 case Intrinsic::x86_sse2_comigt_sd:
4711 case Intrinsic::x86_sse2_comige_sd:
4712 case Intrinsic::x86_sse2_comineq_sd:
4713 case Intrinsic::x86_sse2_ucomieq_sd:
4714 case Intrinsic::x86_sse2_ucomilt_sd:
4715 case Intrinsic::x86_sse2_ucomile_sd:
4716 case Intrinsic::x86_sse2_ucomigt_sd:
4717 case Intrinsic::x86_sse2_ucomige_sd:
4718 case Intrinsic::x86_sse2_ucomineq_sd: {
4719 unsigned Opc = 0;
4720 ISD::CondCode CC = ISD::SETCC_INVALID;
4721 switch (IntNo) {
4722 default: break;
4723 case Intrinsic::x86_sse_comieq_ss:
4724 case Intrinsic::x86_sse2_comieq_sd:
4725 Opc = X86ISD::COMI;
4726 CC = ISD::SETEQ;
4727 break;
4728 case Intrinsic::x86_sse_comilt_ss:
4729 case Intrinsic::x86_sse2_comilt_sd:
4730 Opc = X86ISD::COMI;
4731 CC = ISD::SETLT;
4732 break;
4733 case Intrinsic::x86_sse_comile_ss:
4734 case Intrinsic::x86_sse2_comile_sd:
4735 Opc = X86ISD::COMI;
4736 CC = ISD::SETLE;
4737 break;
4738 case Intrinsic::x86_sse_comigt_ss:
4739 case Intrinsic::x86_sse2_comigt_sd:
4740 Opc = X86ISD::COMI;
4741 CC = ISD::SETGT;
4742 break;
4743 case Intrinsic::x86_sse_comige_ss:
4744 case Intrinsic::x86_sse2_comige_sd:
4745 Opc = X86ISD::COMI;
4746 CC = ISD::SETGE;
4747 break;
4748 case Intrinsic::x86_sse_comineq_ss:
4749 case Intrinsic::x86_sse2_comineq_sd:
4750 Opc = X86ISD::COMI;
4751 CC = ISD::SETNE;
4752 break;
4753 case Intrinsic::x86_sse_ucomieq_ss:
4754 case Intrinsic::x86_sse2_ucomieq_sd:
4755 Opc = X86ISD::UCOMI;
4756 CC = ISD::SETEQ;
4757 break;
4758 case Intrinsic::x86_sse_ucomilt_ss:
4759 case Intrinsic::x86_sse2_ucomilt_sd:
4760 Opc = X86ISD::UCOMI;
4761 CC = ISD::SETLT;
4762 break;
4763 case Intrinsic::x86_sse_ucomile_ss:
4764 case Intrinsic::x86_sse2_ucomile_sd:
4765 Opc = X86ISD::UCOMI;
4766 CC = ISD::SETLE;
4767 break;
4768 case Intrinsic::x86_sse_ucomigt_ss:
4769 case Intrinsic::x86_sse2_ucomigt_sd:
4770 Opc = X86ISD::UCOMI;
4771 CC = ISD::SETGT;
4772 break;
4773 case Intrinsic::x86_sse_ucomige_ss:
4774 case Intrinsic::x86_sse2_ucomige_sd:
4775 Opc = X86ISD::UCOMI;
4776 CC = ISD::SETGE;
4777 break;
4778 case Intrinsic::x86_sse_ucomineq_ss:
4779 case Intrinsic::x86_sse2_ucomineq_sd:
4780 Opc = X86ISD::UCOMI;
4781 CC = ISD::SETNE;
4782 break;
4783 }
4784
4785 unsigned X86CC;
4786 SDOperand LHS = Op.getOperand(1);
4787 SDOperand RHS = Op.getOperand(2);
4788 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4789
Evan Cheng621216e2007-09-29 00:00:36 +00004790 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4791 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4792 DAG.getConstant(X86CC, MVT::i8), Cond);
4793 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 }
4795 }
4796}
4797
4798SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4799 // Depths > 0 not supported yet!
4800 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4801 return SDOperand();
4802
4803 // Just load the return address
4804 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4805 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4806}
4807
4808SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4809 // Depths > 0 not supported yet!
4810 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4811 return SDOperand();
4812
4813 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4814 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4815 DAG.getConstant(4, getPointerTy()));
4816}
4817
4818SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4819 SelectionDAG &DAG) {
4820 // Is not yet supported on x86-64
4821 if (Subtarget->is64Bit())
4822 return SDOperand();
4823
4824 return DAG.getConstant(8, getPointerTy());
4825}
4826
4827SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4828{
4829 assert(!Subtarget->is64Bit() &&
4830 "Lowering of eh_return builtin is not supported yet on x86-64");
4831
4832 MachineFunction &MF = DAG.getMachineFunction();
4833 SDOperand Chain = Op.getOperand(0);
4834 SDOperand Offset = Op.getOperand(1);
4835 SDOperand Handler = Op.getOperand(2);
4836
4837 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4838 getPointerTy());
4839
4840 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4841 DAG.getConstant(-4UL, getPointerTy()));
4842 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4843 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4844 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4845 MF.addLiveOut(X86::ECX);
4846
4847 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4848 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4849}
4850
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004851SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4852 SelectionDAG &DAG) {
4853 SDOperand Root = Op.getOperand(0);
4854 SDOperand Trmp = Op.getOperand(1); // trampoline
4855 SDOperand FPtr = Op.getOperand(2); // nested function
4856 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4857
4858 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4859
4860 if (Subtarget->is64Bit()) {
4861 return SDOperand(); // not yet supported
4862 } else {
4863 Function *Func = (Function *)
4864 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4865 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00004866 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004867
4868 switch (CC) {
4869 default:
4870 assert(0 && "Unsupported calling convention");
4871 case CallingConv::C:
4872 case CallingConv::Fast:
4873 case CallingConv::X86_StdCall: {
4874 // Pass 'nest' parameter in ECX.
4875 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00004876 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004877
4878 // Check that ECX wasn't needed by an 'inreg' parameter.
4879 const FunctionType *FTy = Func->getFunctionType();
4880 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4881
4882 if (Attrs && !Func->isVarArg()) {
4883 unsigned InRegCount = 0;
4884 unsigned Idx = 1;
4885
4886 for (FunctionType::param_iterator I = FTy->param_begin(),
4887 E = FTy->param_end(); I != E; ++I, ++Idx)
4888 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4889 // FIXME: should only count parameters that are lowered to integers.
4890 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4891
4892 if (InRegCount > 2) {
4893 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4894 abort();
4895 }
4896 }
4897 break;
4898 }
4899 case CallingConv::X86_FastCall:
4900 // Pass 'nest' parameter in EAX.
4901 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00004902 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004903 break;
4904 }
4905
Duncan Sands466eadd2007-08-29 19:01:20 +00004906 const X86InstrInfo *TII =
4907 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4908
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004909 SDOperand OutChains[4];
4910 SDOperand Addr, Disp;
4911
4912 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4913 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4914
Duncan Sands466eadd2007-08-29 19:01:20 +00004915 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
4916 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
4917 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004918 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4919
4920 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4921 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4922 TrmpSV->getOffset() + 1, false, 1);
4923
Duncan Sands466eadd2007-08-29 19:01:20 +00004924 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004925 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4926 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4927 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4928
4929 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4930 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4931 TrmpSV->getOffset() + 6, false, 1);
4932
Duncan Sands7407a9f2007-09-11 14:10:23 +00004933 SDOperand Ops[] =
4934 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4935 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004936 }
4937}
4938
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004939/// LowerOperation - Provide custom lowering hooks for some operations.
4940///
4941SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4942 switch (Op.getOpcode()) {
4943 default: assert(0 && "Should not custom lower this!");
4944 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4945 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4946 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4947 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4948 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4949 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4950 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4951 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
4952 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4953 case ISD::SHL_PARTS:
4954 case ISD::SRA_PARTS:
4955 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4956 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4957 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4958 case ISD::FABS: return LowerFABS(Op, DAG);
4959 case ISD::FNEG: return LowerFNEG(Op, DAG);
4960 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00004961 case ISD::SETCC: return LowerSETCC(Op, DAG);
4962 case ISD::SELECT: return LowerSELECT(Op, DAG);
4963 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004964 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
4965 case ISD::CALL: return LowerCALL(Op, DAG);
4966 case ISD::RET: return LowerRET(Op, DAG);
4967 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
4968 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4969 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4970 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4971 case ISD::VASTART: return LowerVASTART(Op, DAG);
4972 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
4973 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4974 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4975 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
4976 case ISD::FRAME_TO_ARGS_OFFSET:
4977 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
4978 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
4979 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004980 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 }
4982 return SDOperand();
4983}
4984
4985const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4986 switch (Opcode) {
4987 default: return NULL;
4988 case X86ISD::SHLD: return "X86ISD::SHLD";
4989 case X86ISD::SHRD: return "X86ISD::SHRD";
4990 case X86ISD::FAND: return "X86ISD::FAND";
4991 case X86ISD::FOR: return "X86ISD::FOR";
4992 case X86ISD::FXOR: return "X86ISD::FXOR";
4993 case X86ISD::FSRL: return "X86ISD::FSRL";
4994 case X86ISD::FILD: return "X86ISD::FILD";
4995 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
4996 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4997 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4998 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
4999 case X86ISD::FLD: return "X86ISD::FLD";
5000 case X86ISD::FST: return "X86ISD::FST";
5001 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
5002 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5003 case X86ISD::CALL: return "X86ISD::CALL";
5004 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5005 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5006 case X86ISD::CMP: return "X86ISD::CMP";
5007 case X86ISD::COMI: return "X86ISD::COMI";
5008 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5009 case X86ISD::SETCC: return "X86ISD::SETCC";
5010 case X86ISD::CMOV: return "X86ISD::CMOV";
5011 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5012 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5013 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5014 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5016 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5017 case X86ISD::S2VEC: return "X86ISD::S2VEC";
5018 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5019 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5020 case X86ISD::FMAX: return "X86ISD::FMAX";
5021 case X86ISD::FMIN: return "X86ISD::FMIN";
5022 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5023 case X86ISD::FRCP: return "X86ISD::FRCP";
5024 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5025 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5026 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005027 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028 }
5029}
5030
5031// isLegalAddressingMode - Return true if the addressing mode represented
5032// by AM is legal for this target, for a load/store of the specified type.
5033bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5034 const Type *Ty) const {
5035 // X86 supports extremely general addressing modes.
5036
5037 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5038 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5039 return false;
5040
5041 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005042 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005043 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5044 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005045
5046 // X86-64 only supports addr of globals in small code model.
5047 if (Subtarget->is64Bit()) {
5048 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5049 return false;
5050 // If lower 4G is not available, then we must use rip-relative addressing.
5051 if (AM.BaseOffs || AM.Scale > 1)
5052 return false;
5053 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005054 }
5055
5056 switch (AM.Scale) {
5057 case 0:
5058 case 1:
5059 case 2:
5060 case 4:
5061 case 8:
5062 // These scales always work.
5063 break;
5064 case 3:
5065 case 5:
5066 case 9:
5067 // These scales are formed with basereg+scalereg. Only accept if there is
5068 // no basereg yet.
5069 if (AM.HasBaseReg)
5070 return false;
5071 break;
5072 default: // Other stuff never works.
5073 return false;
5074 }
5075
5076 return true;
5077}
5078
5079
5080/// isShuffleMaskLegal - Targets can use this to indicate that they only
5081/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5082/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5083/// are assumed to be legal.
5084bool
5085X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5086 // Only do shuffles on 128-bit vector types for now.
5087 if (MVT::getSizeInBits(VT) == 64) return false;
5088 return (Mask.Val->getNumOperands() <= 4 ||
5089 isIdentityMask(Mask.Val) ||
5090 isIdentityMask(Mask.Val, true) ||
5091 isSplatMask(Mask.Val) ||
5092 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5093 X86::isUNPCKLMask(Mask.Val) ||
5094 X86::isUNPCKHMask(Mask.Val) ||
5095 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5096 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5097}
5098
5099bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5100 MVT::ValueType EVT,
5101 SelectionDAG &DAG) const {
5102 unsigned NumElts = BVOps.size();
5103 // Only do shuffles on 128-bit vector types for now.
5104 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5105 if (NumElts == 2) return true;
5106 if (NumElts == 4) {
5107 return (isMOVLMask(&BVOps[0], 4) ||
5108 isCommutedMOVL(&BVOps[0], 4, true) ||
5109 isSHUFPMask(&BVOps[0], 4) ||
5110 isCommutedSHUFP(&BVOps[0], 4));
5111 }
5112 return false;
5113}
5114
5115//===----------------------------------------------------------------------===//
5116// X86 Scheduler Hooks
5117//===----------------------------------------------------------------------===//
5118
5119MachineBasicBlock *
5120X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5121 MachineBasicBlock *BB) {
5122 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5123 switch (MI->getOpcode()) {
5124 default: assert(false && "Unexpected instr type to insert");
5125 case X86::CMOV_FR32:
5126 case X86::CMOV_FR64:
5127 case X86::CMOV_V4F32:
5128 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005129 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005130 // To "insert" a SELECT_CC instruction, we actually have to insert the
5131 // diamond control-flow pattern. The incoming instruction knows the
5132 // destination vreg to set, the condition code register to branch on, the
5133 // true/false values to select between, and a branch opcode to use.
5134 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5135 ilist<MachineBasicBlock>::iterator It = BB;
5136 ++It;
5137
5138 // thisMBB:
5139 // ...
5140 // TrueVal = ...
5141 // cmpTY ccX, r1, r2
5142 // bCC copy1MBB
5143 // fallthrough --> copy0MBB
5144 MachineBasicBlock *thisMBB = BB;
5145 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5146 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5147 unsigned Opc =
5148 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5149 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5150 MachineFunction *F = BB->getParent();
5151 F->getBasicBlockList().insert(It, copy0MBB);
5152 F->getBasicBlockList().insert(It, sinkMBB);
5153 // Update machine-CFG edges by first adding all successors of the current
5154 // block to the new block which will contain the Phi node for the select.
5155 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5156 e = BB->succ_end(); i != e; ++i)
5157 sinkMBB->addSuccessor(*i);
5158 // Next, remove all successors of the current block, and add the true
5159 // and fallthrough blocks as its successors.
5160 while(!BB->succ_empty())
5161 BB->removeSuccessor(BB->succ_begin());
5162 BB->addSuccessor(copy0MBB);
5163 BB->addSuccessor(sinkMBB);
5164
5165 // copy0MBB:
5166 // %FalseValue = ...
5167 // # fallthrough to sinkMBB
5168 BB = copy0MBB;
5169
5170 // Update machine-CFG edges
5171 BB->addSuccessor(sinkMBB);
5172
5173 // sinkMBB:
5174 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5175 // ...
5176 BB = sinkMBB;
5177 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5178 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5179 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5180
5181 delete MI; // The pseudo instruction is gone now.
5182 return BB;
5183 }
5184
5185 case X86::FP32_TO_INT16_IN_MEM:
5186 case X86::FP32_TO_INT32_IN_MEM:
5187 case X86::FP32_TO_INT64_IN_MEM:
5188 case X86::FP64_TO_INT16_IN_MEM:
5189 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005190 case X86::FP64_TO_INT64_IN_MEM:
5191 case X86::FP80_TO_INT16_IN_MEM:
5192 case X86::FP80_TO_INT32_IN_MEM:
5193 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 // Change the floating point control register to use "round towards zero"
5195 // mode when truncating to an integer value.
5196 MachineFunction *F = BB->getParent();
5197 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5198 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5199
5200 // Load the old value of the high byte of the control word...
5201 unsigned OldCW =
5202 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
5203 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5204
5205 // Set the high part to be round to zero...
5206 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5207 .addImm(0xC7F);
5208
5209 // Reload the modified control word now...
5210 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5211
5212 // Restore the memory image of control word to original value
5213 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5214 .addReg(OldCW);
5215
5216 // Get the X86 opcode to use.
5217 unsigned Opc;
5218 switch (MI->getOpcode()) {
5219 default: assert(0 && "illegal opcode!");
5220 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5221 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5222 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5223 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5224 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5225 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005226 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5227 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5228 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 }
5230
5231 X86AddressMode AM;
5232 MachineOperand &Op = MI->getOperand(0);
5233 if (Op.isRegister()) {
5234 AM.BaseType = X86AddressMode::RegBase;
5235 AM.Base.Reg = Op.getReg();
5236 } else {
5237 AM.BaseType = X86AddressMode::FrameIndexBase;
5238 AM.Base.FrameIndex = Op.getFrameIndex();
5239 }
5240 Op = MI->getOperand(1);
5241 if (Op.isImmediate())
5242 AM.Scale = Op.getImm();
5243 Op = MI->getOperand(2);
5244 if (Op.isImmediate())
5245 AM.IndexReg = Op.getImm();
5246 Op = MI->getOperand(3);
5247 if (Op.isGlobalAddress()) {
5248 AM.GV = Op.getGlobal();
5249 } else {
5250 AM.Disp = Op.getImm();
5251 }
5252 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5253 .addReg(MI->getOperand(4).getReg());
5254
5255 // Reload the original control word now.
5256 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5257
5258 delete MI; // The pseudo instruction is gone now.
5259 return BB;
5260 }
5261 }
5262}
5263
5264//===----------------------------------------------------------------------===//
5265// X86 Optimization Hooks
5266//===----------------------------------------------------------------------===//
5267
5268void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5269 uint64_t Mask,
5270 uint64_t &KnownZero,
5271 uint64_t &KnownOne,
5272 const SelectionDAG &DAG,
5273 unsigned Depth) const {
5274 unsigned Opc = Op.getOpcode();
5275 assert((Opc >= ISD::BUILTIN_OP_END ||
5276 Opc == ISD::INTRINSIC_WO_CHAIN ||
5277 Opc == ISD::INTRINSIC_W_CHAIN ||
5278 Opc == ISD::INTRINSIC_VOID) &&
5279 "Should use MaskedValueIsZero if you don't know whether Op"
5280 " is a target node!");
5281
5282 KnownZero = KnownOne = 0; // Don't know anything.
5283 switch (Opc) {
5284 default: break;
5285 case X86ISD::SETCC:
5286 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5287 break;
5288 }
5289}
5290
5291/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5292/// element of the result of the vector shuffle.
5293static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5294 MVT::ValueType VT = N->getValueType(0);
5295 SDOperand PermMask = N->getOperand(2);
5296 unsigned NumElems = PermMask.getNumOperands();
5297 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5298 i %= NumElems;
5299 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5300 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005301 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5303 SDOperand Idx = PermMask.getOperand(i);
5304 if (Idx.getOpcode() == ISD::UNDEF)
5305 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5306 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5307 }
5308 return SDOperand();
5309}
5310
5311/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5312/// node is a GlobalAddress + an offset.
5313static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5314 unsigned Opc = N->getOpcode();
5315 if (Opc == X86ISD::Wrapper) {
5316 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5317 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5318 return true;
5319 }
5320 } else if (Opc == ISD::ADD) {
5321 SDOperand N1 = N->getOperand(0);
5322 SDOperand N2 = N->getOperand(1);
5323 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5324 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5325 if (V) {
5326 Offset += V->getSignExtended();
5327 return true;
5328 }
5329 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5330 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5331 if (V) {
5332 Offset += V->getSignExtended();
5333 return true;
5334 }
5335 }
5336 }
5337 return false;
5338}
5339
5340/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5341/// + Dist * Size.
5342static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5343 MachineFrameInfo *MFI) {
5344 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5345 return false;
5346
5347 SDOperand Loc = N->getOperand(1);
5348 SDOperand BaseLoc = Base->getOperand(1);
5349 if (Loc.getOpcode() == ISD::FrameIndex) {
5350 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5351 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005352 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5353 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005354 int FS = MFI->getObjectSize(FI);
5355 int BFS = MFI->getObjectSize(BFI);
5356 if (FS != BFS || FS != Size) return false;
5357 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5358 } else {
5359 GlobalValue *GV1 = NULL;
5360 GlobalValue *GV2 = NULL;
5361 int64_t Offset1 = 0;
5362 int64_t Offset2 = 0;
5363 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5364 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5365 if (isGA1 && isGA2 && GV1 == GV2)
5366 return Offset1 == (Offset2 + Dist*Size);
5367 }
5368
5369 return false;
5370}
5371
5372static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5373 const X86Subtarget *Subtarget) {
5374 GlobalValue *GV;
5375 int64_t Offset;
5376 if (isGAPlusOffset(Base, GV, Offset))
5377 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5378 else {
5379 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman53491e92007-07-23 20:24:29 +00005380 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005381 if (BFI < 0)
5382 // Fixed objects do not specify alignment, however the offsets are known.
5383 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5384 (MFI->getObjectOffset(BFI) % 16) == 0);
5385 else
5386 return MFI->getObjectAlignment(BFI) >= 16;
5387 }
5388 return false;
5389}
5390
5391
5392/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5393/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5394/// if the load addresses are consecutive, non-overlapping, and in the right
5395/// order.
5396static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5397 const X86Subtarget *Subtarget) {
5398 MachineFunction &MF = DAG.getMachineFunction();
5399 MachineFrameInfo *MFI = MF.getFrameInfo();
5400 MVT::ValueType VT = N->getValueType(0);
5401 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5402 SDOperand PermMask = N->getOperand(2);
5403 int NumElems = (int)PermMask.getNumOperands();
5404 SDNode *Base = NULL;
5405 for (int i = 0; i < NumElems; ++i) {
5406 SDOperand Idx = PermMask.getOperand(i);
5407 if (Idx.getOpcode() == ISD::UNDEF) {
5408 if (!Base) return SDOperand();
5409 } else {
5410 SDOperand Arg =
5411 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5412 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5413 return SDOperand();
5414 if (!Base)
5415 Base = Arg.Val;
5416 else if (!isConsecutiveLoad(Arg.Val, Base,
5417 i, MVT::getSizeInBits(EVT)/8,MFI))
5418 return SDOperand();
5419 }
5420 }
5421
5422 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005423 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005424 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005425 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005426 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005428 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5429 LD->getSrcValueOffset(), LD->isVolatile(),
5430 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005431 }
5432}
5433
5434/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5435static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5436 const X86Subtarget *Subtarget) {
5437 SDOperand Cond = N->getOperand(0);
5438
5439 // If we have SSE[12] support, try to form min/max nodes.
5440 if (Subtarget->hasSSE2() &&
5441 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5442 if (Cond.getOpcode() == ISD::SETCC) {
5443 // Get the LHS/RHS of the select.
5444 SDOperand LHS = N->getOperand(1);
5445 SDOperand RHS = N->getOperand(2);
5446 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5447
5448 unsigned Opcode = 0;
5449 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5450 switch (CC) {
5451 default: break;
5452 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5453 case ISD::SETULE:
5454 case ISD::SETLE:
5455 if (!UnsafeFPMath) break;
5456 // FALL THROUGH.
5457 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5458 case ISD::SETLT:
5459 Opcode = X86ISD::FMIN;
5460 break;
5461
5462 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5463 case ISD::SETUGT:
5464 case ISD::SETGT:
5465 if (!UnsafeFPMath) break;
5466 // FALL THROUGH.
5467 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5468 case ISD::SETGE:
5469 Opcode = X86ISD::FMAX;
5470 break;
5471 }
5472 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5473 switch (CC) {
5474 default: break;
5475 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5476 case ISD::SETUGT:
5477 case ISD::SETGT:
5478 if (!UnsafeFPMath) break;
5479 // FALL THROUGH.
5480 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5481 case ISD::SETGE:
5482 Opcode = X86ISD::FMIN;
5483 break;
5484
5485 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5486 case ISD::SETULE:
5487 case ISD::SETLE:
5488 if (!UnsafeFPMath) break;
5489 // FALL THROUGH.
5490 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5491 case ISD::SETLT:
5492 Opcode = X86ISD::FMAX;
5493 break;
5494 }
5495 }
5496
5497 if (Opcode)
5498 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5499 }
5500
5501 }
5502
5503 return SDOperand();
5504}
5505
5506
5507SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5508 DAGCombinerInfo &DCI) const {
5509 SelectionDAG &DAG = DCI.DAG;
5510 switch (N->getOpcode()) {
5511 default: break;
5512 case ISD::VECTOR_SHUFFLE:
5513 return PerformShuffleCombine(N, DAG, Subtarget);
5514 case ISD::SELECT:
5515 return PerformSELECTCombine(N, DAG, Subtarget);
5516 }
5517
5518 return SDOperand();
5519}
5520
5521//===----------------------------------------------------------------------===//
5522// X86 Inline Assembly Support
5523//===----------------------------------------------------------------------===//
5524
5525/// getConstraintType - Given a constraint letter, return the type of
5526/// constraint it is for this target.
5527X86TargetLowering::ConstraintType
5528X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5529 if (Constraint.size() == 1) {
5530 switch (Constraint[0]) {
5531 case 'A':
5532 case 'r':
5533 case 'R':
5534 case 'l':
5535 case 'q':
5536 case 'Q':
5537 case 'x':
5538 case 'Y':
5539 return C_RegisterClass;
5540 default:
5541 break;
5542 }
5543 }
5544 return TargetLowering::getConstraintType(Constraint);
5545}
5546
Chris Lattnera531abc2007-08-25 00:47:38 +00005547/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5548/// vector. If it is invalid, don't add anything to Ops.
5549void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5550 char Constraint,
5551 std::vector<SDOperand>&Ops,
5552 SelectionDAG &DAG) {
5553 SDOperand Result(0, 0);
5554
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555 switch (Constraint) {
5556 default: break;
5557 case 'I':
5558 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005559 if (C->getValue() <= 31) {
5560 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5561 break;
5562 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005564 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005565 case 'N':
5566 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005567 if (C->getValue() <= 255) {
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 'i': {
5574 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00005575 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5576 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5577 break;
5578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579
5580 // If we are in non-pic codegen mode, we allow the address of a global (with
5581 // an optional displacement) to be used with 'i'.
5582 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5583 int64_t Offset = 0;
5584
5585 // Match either (GA) or (GA+C)
5586 if (GA) {
5587 Offset = GA->getOffset();
5588 } else if (Op.getOpcode() == ISD::ADD) {
5589 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5590 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5591 if (C && GA) {
5592 Offset = GA->getOffset()+C->getValue();
5593 } else {
5594 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5595 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5596 if (C && GA)
5597 Offset = GA->getOffset()+C->getValue();
5598 else
5599 C = 0, GA = 0;
5600 }
5601 }
5602
5603 if (GA) {
5604 // If addressing this global requires a load (e.g. in PIC mode), we can't
5605 // match.
5606 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5607 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00005608 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005609
5610 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5611 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00005612 Result = Op;
5613 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614 }
5615
5616 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00005617 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005618 }
5619 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005620
5621 if (Result.Val) {
5622 Ops.push_back(Result);
5623 return;
5624 }
5625 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005626}
5627
5628std::vector<unsigned> X86TargetLowering::
5629getRegClassForInlineAsmConstraint(const std::string &Constraint,
5630 MVT::ValueType VT) const {
5631 if (Constraint.size() == 1) {
5632 // FIXME: not handling fp-stack yet!
5633 switch (Constraint[0]) { // GCC X86 Constraint Letters
5634 default: break; // Unknown constraint letter
5635 case 'A': // EAX/EDX
5636 if (VT == MVT::i32 || VT == MVT::i64)
5637 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5638 break;
5639 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5640 case 'Q': // Q_REGS
5641 if (VT == MVT::i32)
5642 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5643 else if (VT == MVT::i16)
5644 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5645 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00005646 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005647 break;
5648 }
5649 }
5650
5651 return std::vector<unsigned>();
5652}
5653
5654std::pair<unsigned, const TargetRegisterClass*>
5655X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5656 MVT::ValueType VT) const {
5657 // First, see if this is a constraint that directly corresponds to an LLVM
5658 // register class.
5659 if (Constraint.size() == 1) {
5660 // GCC Constraint Letters
5661 switch (Constraint[0]) {
5662 default: break;
5663 case 'r': // GENERAL_REGS
5664 case 'R': // LEGACY_REGS
5665 case 'l': // INDEX_REGS
5666 if (VT == MVT::i64 && Subtarget->is64Bit())
5667 return std::make_pair(0U, X86::GR64RegisterClass);
5668 if (VT == MVT::i32)
5669 return std::make_pair(0U, X86::GR32RegisterClass);
5670 else if (VT == MVT::i16)
5671 return std::make_pair(0U, X86::GR16RegisterClass);
5672 else if (VT == MVT::i8)
5673 return std::make_pair(0U, X86::GR8RegisterClass);
5674 break;
5675 case 'y': // MMX_REGS if MMX allowed.
5676 if (!Subtarget->hasMMX()) break;
5677 return std::make_pair(0U, X86::VR64RegisterClass);
5678 break;
5679 case 'Y': // SSE_REGS if SSE2 allowed
5680 if (!Subtarget->hasSSE2()) break;
5681 // FALL THROUGH.
5682 case 'x': // SSE_REGS if SSE1 allowed
5683 if (!Subtarget->hasSSE1()) break;
5684
5685 switch (VT) {
5686 default: break;
5687 // Scalar SSE types.
5688 case MVT::f32:
5689 case MVT::i32:
5690 return std::make_pair(0U, X86::FR32RegisterClass);
5691 case MVT::f64:
5692 case MVT::i64:
5693 return std::make_pair(0U, X86::FR64RegisterClass);
5694 // Vector types.
5695 case MVT::v16i8:
5696 case MVT::v8i16:
5697 case MVT::v4i32:
5698 case MVT::v2i64:
5699 case MVT::v4f32:
5700 case MVT::v2f64:
5701 return std::make_pair(0U, X86::VR128RegisterClass);
5702 }
5703 break;
5704 }
5705 }
5706
5707 // Use the default implementation in TargetLowering to convert the register
5708 // constraint into a member of a register class.
5709 std::pair<unsigned, const TargetRegisterClass*> Res;
5710 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5711
5712 // Not found as a standard register?
5713 if (Res.second == 0) {
5714 // GCC calls "st(0)" just plain "st".
5715 if (StringsEqualNoCase("{st}", Constraint)) {
5716 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00005717 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 }
5719
5720 return Res;
5721 }
5722
5723 // Otherwise, check to see if this is a register class of the wrong value
5724 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5725 // turn into {ax},{dx}.
5726 if (Res.second->hasType(VT))
5727 return Res; // Correct type already, nothing to do.
5728
5729 // All of the single-register GCC register classes map their values onto
5730 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5731 // really want an 8-bit or 32-bit register, map to the appropriate register
5732 // class and return the appropriate register.
5733 if (Res.second != X86::GR16RegisterClass)
5734 return Res;
5735
5736 if (VT == MVT::i8) {
5737 unsigned DestReg = 0;
5738 switch (Res.first) {
5739 default: break;
5740 case X86::AX: DestReg = X86::AL; break;
5741 case X86::DX: DestReg = X86::DL; break;
5742 case X86::CX: DestReg = X86::CL; break;
5743 case X86::BX: DestReg = X86::BL; break;
5744 }
5745 if (DestReg) {
5746 Res.first = DestReg;
5747 Res.second = Res.second = X86::GR8RegisterClass;
5748 }
5749 } else if (VT == MVT::i32) {
5750 unsigned DestReg = 0;
5751 switch (Res.first) {
5752 default: break;
5753 case X86::AX: DestReg = X86::EAX; break;
5754 case X86::DX: DestReg = X86::EDX; break;
5755 case X86::CX: DestReg = X86::ECX; break;
5756 case X86::BX: DestReg = X86::EBX; break;
5757 case X86::SI: DestReg = X86::ESI; break;
5758 case X86::DI: DestReg = X86::EDI; break;
5759 case X86::BP: DestReg = X86::EBP; break;
5760 case X86::SP: DestReg = X86::ESP; break;
5761 }
5762 if (DestReg) {
5763 Res.first = DestReg;
5764 Res.second = Res.second = X86::GR32RegisterClass;
5765 }
5766 } else if (VT == MVT::i64) {
5767 unsigned DestReg = 0;
5768 switch (Res.first) {
5769 default: break;
5770 case X86::AX: DestReg = X86::RAX; break;
5771 case X86::DX: DestReg = X86::RDX; break;
5772 case X86::CX: DestReg = X86::RCX; break;
5773 case X86::BX: DestReg = X86::RBX; break;
5774 case X86::SI: DestReg = X86::RSI; break;
5775 case X86::DI: DestReg = X86::RDI; break;
5776 case X86::BP: DestReg = X86::RBP; break;
5777 case X86::SP: DestReg = X86::RSP; break;
5778 }
5779 if (DestReg) {
5780 Res.first = DestReg;
5781 Res.second = Res.second = X86::GR64RegisterClass;
5782 }
5783 }
5784
5785 return Res;
5786}