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