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