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