blob: 1d64b1d859c37561d582fade6a2e99414139d8ed [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
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"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
29#include "llvm/CodeGen/CallingConvLower.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
33#include "llvm/CodeGen/SelectionDAG.h"
34#include "llvm/CodeGen/SSARegMap.h"
35#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/StringExtras.h"
Duncan Sandsd8455ca2007-07-27 20:02:49 +000041#include "llvm/ParameterAttributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
44X86TargetLowering::X86TargetLowering(TargetMachine &TM)
45 : TargetLowering(TM) {
46 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000047 X86ScalarSSEf64 = Subtarget->hasSSE2();
48 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000050
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52 RegInfo = TM.getRegisterInfo();
53
54 // Set up the TargetLowering object.
55
56 // X86 is weird, it always uses i8 for shift amounts and setcc results.
57 setShiftAmountType(MVT::i8);
58 setSetCCResultType(MVT::i8);
59 setSetCCResultContents(ZeroOrOneSetCCResult);
60 setSchedulingPreference(SchedulingForRegPressure);
61 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
62 setStackPointerRegisterToSaveRestore(X86StackPtr);
63
64 if (Subtarget->isTargetDarwin()) {
65 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
66 setUseUnderscoreSetJmp(false);
67 setUseUnderscoreLongJmp(false);
68 } else if (Subtarget->isTargetMingw()) {
69 // MS runtime is weird: it exports _setjmp, but longjmp!
70 setUseUnderscoreSetJmp(true);
71 setUseUnderscoreLongJmp(false);
72 } else {
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
75 }
76
77 // Set up the register classes.
78 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
79 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
80 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
81 if (Subtarget->is64Bit())
82 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
83
84 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
85
86 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
87 // operation.
88 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
89 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
90 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
91
92 if (Subtarget->is64Bit()) {
93 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
94 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
95 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +000096 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
99 else
100 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
101 }
102
103 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
104 // this operation.
105 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
106 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
107 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000108 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000110 // f32 and f64 cases are Legal, f80 case is not
111 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
112 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
114 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
115 }
116
Dale Johannesen958b08b2007-09-19 23:55:34 +0000117 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
118 // are Legal, f80 is custom lowered.
119 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
120 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
123 // this operation.
124 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
125 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
126
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000127 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000129 // f32 and f64 cases are Legal, f80 case is not
130 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 } else {
132 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
133 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
134 }
135
136 // Handle FP_TO_UINT by promoting the destination to a larger signed
137 // conversion.
138 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
140 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
141
142 if (Subtarget->is64Bit()) {
143 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
144 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
145 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000146 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 // Expand FP_TO_UINT into a select.
148 // FIXME: We would like to use a Custom expander here eventually to do
149 // the optimal thing for SSE vs. the default expansion in the legalizer.
150 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
151 else
152 // With SSE3 we can use fisttpll to convert to a signed i64.
153 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
154 }
155
156 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000157 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
159 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
160 }
161
Dan Gohman5a199552007-10-08 18:33:35 +0000162 // Scalar integer multiply, multiply-high, divide, and remainder are
163 // lowered to use operations that produce two results, to match the
164 // available instructions. This exposes the two-result form to trivial
165 // CSE, which is able to combine x/y and x%y into a single instruction,
166 // for example. The single-result multiply instructions are introduced
167 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
168 // is not needed.
169 setOperationAction(ISD::MUL , MVT::i8 , Expand);
170 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
171 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
172 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
173 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
174 setOperationAction(ISD::SREM , MVT::i8 , Expand);
175 setOperationAction(ISD::UREM , MVT::i8 , Expand);
176 setOperationAction(ISD::MUL , MVT::i16 , Expand);
177 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
178 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
179 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
180 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
181 setOperationAction(ISD::SREM , MVT::i16 , Expand);
182 setOperationAction(ISD::UREM , MVT::i16 , Expand);
183 setOperationAction(ISD::MUL , MVT::i32 , Expand);
184 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
185 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
186 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
187 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
188 setOperationAction(ISD::SREM , MVT::i32 , Expand);
189 setOperationAction(ISD::UREM , MVT::i32 , Expand);
190 setOperationAction(ISD::MUL , MVT::i64 , Expand);
191 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
192 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
193 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
194 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
195 setOperationAction(ISD::SREM , MVT::i64 , Expand);
196 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000197
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
199 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
200 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
201 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
202 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
203 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
206 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
208 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
209 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000210 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
211
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000213 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
214 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000216 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
217 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000219 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
220 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 if (Subtarget->is64Bit()) {
222 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 }
226
227 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
228 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
229
230 // These should be promoted to a larger select which is supported.
231 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
232 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
233 // X86 wants to expand cmov itself.
234 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
235 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
236 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
237 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000238 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
240 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
241 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
242 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
243 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000244 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 if (Subtarget->is64Bit()) {
246 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
247 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
248 }
249 // X86 ret instruction may pop stack.
250 setOperationAction(ISD::RET , MVT::Other, Custom);
251 if (!Subtarget->is64Bit())
252 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
253
254 // Darwin ABI issue.
255 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
256 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
257 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
258 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
259 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
260 if (Subtarget->is64Bit()) {
261 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
262 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
263 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
264 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
265 }
266 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
267 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
268 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
269 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
270 // X86 wants to expand memset / memcpy itself.
271 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
272 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
273
Dan Gohman21442852007-09-25 15:10:49 +0000274 // Use the default ISD::LOCATION expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 // FIXME - use subtarget debug flags
277 if (!Subtarget->isTargetDarwin() &&
278 !Subtarget->isTargetELF() &&
279 !Subtarget->isTargetCygMing())
280 setOperationAction(ISD::LABEL, MVT::Other, Expand);
281
282 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
283 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
284 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
285 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
286 if (Subtarget->is64Bit()) {
287 // FIXME: Verify
288 setExceptionPointerRegister(X86::RAX);
289 setExceptionSelectorRegister(X86::RDX);
290 } else {
291 setExceptionPointerRegister(X86::EAX);
292 setExceptionSelectorRegister(X86::EDX);
293 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000294 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295
Duncan Sands7407a9f2007-09-11 14:10:23 +0000296 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
299 setOperationAction(ISD::VASTART , MVT::Other, Custom);
300 setOperationAction(ISD::VAARG , MVT::Other, Expand);
301 setOperationAction(ISD::VAEND , MVT::Other, Expand);
302 if (Subtarget->is64Bit())
303 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
304 else
305 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
306
307 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
308 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
309 if (Subtarget->is64Bit())
310 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
311 if (Subtarget->isTargetCygMing())
312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
313 else
314 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
315
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000316 if (X86ScalarSSEf64) {
317 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 // Set up the FP register classes.
319 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
320 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
321
322 // Use ANDPD to simulate FABS.
323 setOperationAction(ISD::FABS , MVT::f64, Custom);
324 setOperationAction(ISD::FABS , MVT::f32, Custom);
325
326 // Use XORP to simulate FNEG.
327 setOperationAction(ISD::FNEG , MVT::f64, Custom);
328 setOperationAction(ISD::FNEG , MVT::f32, Custom);
329
330 // Use ANDPD and ORPD to simulate FCOPYSIGN.
331 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
332 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
333
334 // We don't support sin/cos/fmod
335 setOperationAction(ISD::FSIN , MVT::f64, Expand);
336 setOperationAction(ISD::FCOS , MVT::f64, Expand);
337 setOperationAction(ISD::FREM , MVT::f64, Expand);
338 setOperationAction(ISD::FSIN , MVT::f32, Expand);
339 setOperationAction(ISD::FCOS , MVT::f32, Expand);
340 setOperationAction(ISD::FREM , MVT::f32, Expand);
341
342 // Expand FP immediates into loads from the stack, except for the special
343 // cases we handle.
344 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
345 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000346 addLegalFPImmediate(APFloat(+0.0)); // xorpd
347 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000348
349 // Conversions to long double (in X87) go through memory.
350 setConvertAction(MVT::f32, MVT::f80, Expand);
351 setConvertAction(MVT::f64, MVT::f80, Expand);
352
353 // Conversions from long double (in X87) go through memory.
354 setConvertAction(MVT::f80, MVT::f32, Expand);
355 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000356 } else if (X86ScalarSSEf32) {
357 // Use SSE for f32, x87 for f64.
358 // Set up the FP register classes.
359 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
360 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
361
362 // Use ANDPS to simulate FABS.
363 setOperationAction(ISD::FABS , MVT::f32, Custom);
364
365 // Use XORP to simulate FNEG.
366 setOperationAction(ISD::FNEG , MVT::f32, Custom);
367
368 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
369
370 // Use ANDPS and ORPS to simulate FCOPYSIGN.
371 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
372 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
373
374 // We don't support sin/cos/fmod
375 setOperationAction(ISD::FSIN , MVT::f32, Expand);
376 setOperationAction(ISD::FCOS , MVT::f32, Expand);
377 setOperationAction(ISD::FREM , MVT::f32, Expand);
378
379 // Expand FP immediates into loads from the stack, except for the special
380 // cases we handle.
381 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
382 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
383 addLegalFPImmediate(APFloat(+0.0f)); // xorps
384 addLegalFPImmediate(APFloat(+0.0)); // FLD0
385 addLegalFPImmediate(APFloat(+1.0)); // FLD1
386 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
387 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
388
389 // SSE->x87 conversions go through memory.
390 setConvertAction(MVT::f32, MVT::f64, Expand);
391 setConvertAction(MVT::f32, MVT::f80, Expand);
392
393 // x87->SSE truncations need to go through memory.
394 setConvertAction(MVT::f80, MVT::f32, Expand);
395 setConvertAction(MVT::f64, MVT::f32, Expand);
396 // And x87->x87 truncations also.
397 setConvertAction(MVT::f80, MVT::f64, Expand);
398
399 if (!UnsafeFPMath) {
400 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
401 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
402 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000404 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 // Set up the FP register classes.
406 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
407 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
408
409 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
410 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
411 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
412 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000413
414 // Floating truncations need to go through memory.
415 setConvertAction(MVT::f80, MVT::f32, Expand);
416 setConvertAction(MVT::f64, MVT::f32, Expand);
417 setConvertAction(MVT::f80, MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418
419 if (!UnsafeFPMath) {
420 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
421 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
422 }
423
424 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
425 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000426 addLegalFPImmediate(APFloat(+0.0)); // FLD0
427 addLegalFPImmediate(APFloat(+1.0)); // FLD1
428 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
429 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000430 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
431 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
432 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
433 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 }
435
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000436 // Long double always uses X87.
437 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000438 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
439 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
440 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000441 if (!UnsafeFPMath) {
442 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
443 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
444 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000445
Dan Gohman2f7b1982007-10-11 23:21:31 +0000446 // Always use a library call for pow.
447 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
448 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
449 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
450
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 // First set operation action for all vector types to expand. Then we
452 // will selectively turn on ones that can be effectively codegen'd.
453 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
454 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
455 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
456 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
457 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
458 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
459 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
460 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
461 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
464 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
465 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
466 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
467 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
468 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
469 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
470 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
471 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
475 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
476 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
477 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000478 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
479 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
480 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
481 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000482 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000483 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
485 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000486 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 }
493
494 if (Subtarget->hasMMX()) {
495 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
496 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
497 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
498 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
499
500 // FIXME: add MMX packed arithmetics
501
502 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
503 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
504 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
505 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
506
507 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
508 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
509 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000510 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511
512 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
513 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
514
515 setOperationAction(ISD::AND, MVT::v8i8, Promote);
516 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
517 setOperationAction(ISD::AND, MVT::v4i16, Promote);
518 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
519 setOperationAction(ISD::AND, MVT::v2i32, Promote);
520 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
521 setOperationAction(ISD::AND, MVT::v1i64, Legal);
522
523 setOperationAction(ISD::OR, MVT::v8i8, Promote);
524 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
525 setOperationAction(ISD::OR, MVT::v4i16, Promote);
526 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
527 setOperationAction(ISD::OR, MVT::v2i32, Promote);
528 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
529 setOperationAction(ISD::OR, MVT::v1i64, Legal);
530
531 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
532 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
533 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
534 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
535 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
536 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
537 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
538
539 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
540 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
541 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
542 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
543 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
544 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
545 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
546
547 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
548 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
549 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
550 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
551
552 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
553 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
554 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
555 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
556
557 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
558 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
559 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
560 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
561 }
562
563 if (Subtarget->hasSSE1()) {
564 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
565
566 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
567 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
568 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
569 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
570 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
571 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
573 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
574 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
575 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
576 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
577 }
578
579 if (Subtarget->hasSSE2()) {
580 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
581 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
582 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
583 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
584 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
585
586 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
587 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
588 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
589 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
590 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
591 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
592 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
593 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
594 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
595 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
596 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
597 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
598 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
599 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
600 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601
602 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
603 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
604 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
605 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
606 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
607 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
608
609 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
610 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000611 // Do not attempt to custom lower non-power-of-2 vectors
612 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
613 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
615 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
616 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
617 }
618 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
619 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
621 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
622 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000623 if (Subtarget->is64Bit())
624 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625
626 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
627 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
628 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
629 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
630 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
631 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
632 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
633 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
634 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
635 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
636 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
637 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
638 }
639
640 // Custom lower v2i64 and v2f64 selects.
641 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
642 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
643 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
644 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
645 }
646
647 // We want to custom lower some of our intrinsics.
648 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
649
650 // We have target-specific dag combine patterns for the following nodes:
651 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
652 setTargetDAGCombine(ISD::SELECT);
653
654 computeRegisterProperties();
655
656 // FIXME: These should be based on subtarget info. Plus, the values should
657 // be smaller when we are in optimizing for size mode.
658 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
659 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
660 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
661 allowUnalignedMemoryAccesses = true; // x86 supports it!
662}
663
664
Evan Cheng6fb06762007-11-09 01:32:10 +0000665/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
666/// jumptable.
667SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
668 SelectionDAG &DAG) const {
669 if (usesGlobalOffsetTable())
670 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
671 if (!Subtarget->isPICStyleRIPRel())
672 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
673 return Table;
674}
675
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676//===----------------------------------------------------------------------===//
677// Return Value Calling Convention Implementation
678//===----------------------------------------------------------------------===//
679
680#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000681
682/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
683/// exists skip possible ISD:TokenFactor.
684static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
685 if (Chain.getOpcode()==X86ISD::TAILCALL) {
686 return Chain;
687 } else if (Chain.getOpcode()==ISD::TokenFactor) {
688 if (Chain.getNumOperands() &&
689 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
690 return Chain.getOperand(0);
691 }
692 return Chain;
693}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694
695/// LowerRET - Lower an ISD::RET node.
696SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
697 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
698
699 SmallVector<CCValAssign, 16> RVLocs;
700 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
701 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
702 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
703 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000704
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 // If this is the first return lowered for this function, add the regs to the
706 // liveout set for the function.
707 if (DAG.getMachineFunction().liveout_empty()) {
708 for (unsigned i = 0; i != RVLocs.size(); ++i)
709 if (RVLocs[i].isRegLoc())
710 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
711 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000714 // Handle tail call return.
715 Chain = GetPossiblePreceedingTailCall(Chain);
716 if (Chain.getOpcode() == X86ISD::TAILCALL) {
717 SDOperand TailCall = Chain;
718 SDOperand TargetAddress = TailCall.getOperand(1);
719 SDOperand StackAdjustment = TailCall.getOperand(2);
720 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
721 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
722 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
723 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
724 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
725 "Expecting an global address, external symbol, or register");
726 assert( StackAdjustment.getOpcode() == ISD::Constant &&
727 "Expecting a const value");
728
729 SmallVector<SDOperand,8> Operands;
730 Operands.push_back(Chain.getOperand(0));
731 Operands.push_back(TargetAddress);
732 Operands.push_back(StackAdjustment);
733 // Copy registers used by the call. Last operand is a flag so it is not
734 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000735 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000736 Operands.push_back(Chain.getOperand(i));
737 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000738 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
739 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000740 }
741
742 // Regular return.
743 SDOperand Flag;
744
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 // Copy the result values into the output registers.
746 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
747 RVLocs[0].getLocReg() != X86::ST0) {
748 for (unsigned i = 0; i != RVLocs.size(); ++i) {
749 CCValAssign &VA = RVLocs[i];
750 assert(VA.isRegLoc() && "Can only return in registers!");
751 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
752 Flag);
753 Flag = Chain.getValue(1);
754 }
755 } else {
756 // We need to handle a destination of ST0 specially, because it isn't really
757 // a register.
758 SDOperand Value = Op.getOperand(1);
759
760 // If this is an FP return with ScalarSSE, we need to move the value from
761 // an XMM register onto the fp-stack.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000762 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
763 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 SDOperand MemLoc;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000765
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 // If this is a load into a scalarsse value, don't store the loaded value
767 // back to the stack, only to reload it: just replace the scalar-sse load.
768 if (ISD::isNON_EXTLoad(Value.Val) &&
769 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
770 Chain = Value.getOperand(0);
771 MemLoc = Value.getOperand(1);
772 } else {
773 // Spill the value to memory and reload it into top of stack.
774 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
775 MachineFunction &MF = DAG.getMachineFunction();
776 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
777 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
778 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
779 }
780 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
781 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
782 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
783 Chain = Value.getValue(1);
784 }
785
786 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
787 SDOperand Ops[] = { Chain, Value };
788 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
789 Flag = Chain.getValue(1);
790 }
791
792 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
793 if (Flag.Val)
794 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
795 else
796 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
797}
798
799
800/// LowerCallResult - Lower the result values of an ISD::CALL into the
801/// appropriate copies out of appropriate physical registers. This assumes that
802/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
803/// being lowered. The returns a SDNode with the same number of values as the
804/// ISD::CALL.
805SDNode *X86TargetLowering::
806LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
807 unsigned CallingConv, SelectionDAG &DAG) {
808
809 // Assign locations to each value returned by this call.
810 SmallVector<CCValAssign, 16> RVLocs;
811 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
812 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
813 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
814
815
816 SmallVector<SDOperand, 8> ResultVals;
817
818 // Copy all of the result registers out of their specified physreg.
819 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
820 for (unsigned i = 0; i != RVLocs.size(); ++i) {
821 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
822 RVLocs[i].getValVT(), InFlag).getValue(1);
823 InFlag = Chain.getValue(2);
824 ResultVals.push_back(Chain.getValue(0));
825 }
826 } else {
827 // Copies from the FP stack are special, as ST0 isn't a valid register
828 // before the fp stackifier runs.
829
830 // Copy ST0 into an RFP register with FP_GET_RESULT.
831 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
832 SDOperand GROps[] = { Chain, InFlag };
833 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
834 Chain = RetVal.getValue(1);
835 InFlag = RetVal.getValue(2);
836
837 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
838 // an XMM register.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000839 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
840 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
842 // shouldn't be necessary except that RFP cannot be live across
843 // multiple blocks. When stackifier is fixed, they can be uncoupled.
844 MachineFunction &MF = DAG.getMachineFunction();
845 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
846 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
847 SDOperand Ops[] = {
848 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
849 };
850 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
851 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
852 Chain = RetVal.getValue(1);
853 }
854 ResultVals.push_back(RetVal);
855 }
856
857 // Merge everything together with a MERGE_VALUES node.
858 ResultVals.push_back(Chain);
859 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
860 &ResultVals[0], ResultVals.size()).Val;
861}
862
863
864//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000865// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866//===----------------------------------------------------------------------===//
867// StdCall calling convention seems to be standard for many Windows' API
868// routines and around. It differs from C calling convention just a little:
869// callee should clean up the stack, not caller. Symbols should be also
870// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000871// For info on fast calling convention see Fast Calling Convention (tail call)
872// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873
874/// AddLiveIn - This helper function adds the specified physical register to the
875/// MachineFunction as a live in value. It also creates a corresponding virtual
876/// register for it.
877static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
878 const TargetRegisterClass *RC) {
879 assert(RC->contains(PReg) && "Not the correct regclass!");
880 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
881 MF.addLiveIn(PReg, VReg);
882 return VReg;
883}
884
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000885// align stack arguments according to platform alignment needed for tail calls
886unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
887
Rafael Espindola03cbeb72007-09-14 15:48:13 +0000888SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
889 const CCValAssign &VA,
890 MachineFrameInfo *MFI,
891 SDOperand Root, unsigned i) {
892 // Create the nodes corresponding to a load from this parameter slot.
893 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
894 VA.getLocMemOffset());
895 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
896
897 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
898
899 if (Flags & ISD::ParamFlags::ByVal)
900 return FIN;
901 else
902 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
903}
904
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
906 bool isStdCall) {
907 unsigned NumArgs = Op.Val->getNumValues() - 1;
908 MachineFunction &MF = DAG.getMachineFunction();
909 MachineFrameInfo *MFI = MF.getFrameInfo();
910 SDOperand Root = Op.getOperand(0);
911 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000912 unsigned CC = MF.getFunction()->getCallingConv();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 // Assign locations to all of the incoming arguments.
914 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000915 CCState CCInfo(CC, isVarArg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 getTargetMachine(), ArgLocs);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000917 // Check for possible tail call calling convention.
918 if (CC == CallingConv::Fast && PerformTailCallOpt)
919 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
920 else
921 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
922
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 SmallVector<SDOperand, 8> ArgValues;
924 unsigned LastVal = ~0U;
925 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
926 CCValAssign &VA = ArgLocs[i];
927 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
928 // places.
929 assert(VA.getValNo() != LastVal &&
930 "Don't support value assigned to multiple locs yet");
931 LastVal = VA.getValNo();
932
933 if (VA.isRegLoc()) {
934 MVT::ValueType RegVT = VA.getLocVT();
935 TargetRegisterClass *RC;
936 if (RegVT == MVT::i32)
937 RC = X86::GR32RegisterClass;
938 else {
939 assert(MVT::isVector(RegVT));
940 RC = X86::VR128RegisterClass;
941 }
942
943 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
944 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
945
946 // If this is an 8 or 16-bit value, it is really passed promoted to 32
947 // bits. Insert an assert[sz]ext to capture this, then truncate to the
948 // right size.
949 if (VA.getLocInfo() == CCValAssign::SExt)
950 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
951 DAG.getValueType(VA.getValVT()));
952 else if (VA.getLocInfo() == CCValAssign::ZExt)
953 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
954 DAG.getValueType(VA.getValVT()));
955
956 if (VA.getLocInfo() != CCValAssign::Full)
957 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
958
959 ArgValues.push_back(ArgValue);
960 } else {
961 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +0000962 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 }
964 }
965
966 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000967 // align stack specially for tail calls
968 if (CC==CallingConv::Fast)
969 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970
971 ArgValues.push_back(Root);
972
973 // If the function takes variable number of arguments, make a frame index for
974 // the start of the first vararg value... for expansion of llvm.va_start.
975 if (isVarArg)
976 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
977
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000978 // Tail call calling convention (CallingConv::Fast) does not support varargs.
979 assert( !(isVarArg && CC == CallingConv::Fast) &&
980 "CallingConv::Fast does not support varargs.");
981
982 if (isStdCall && !isVarArg &&
983 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 BytesToPopOnReturn = StackSize; // Callee pops everything..
985 BytesCallerReserves = 0;
986 } else {
987 BytesToPopOnReturn = 0; // Callee pops nothing.
988
989 // If this is an sret function, the return should pop the hidden pointer.
990 if (NumArgs &&
991 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
992 ISD::ParamFlags::StructReturn))
993 BytesToPopOnReturn = 4;
994
995 BytesCallerReserves = StackSize;
996 }
Anton Korobeynikove844e472007-08-15 17:12:32 +0000997
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999
Anton Korobeynikove844e472007-08-15 17:12:32 +00001000 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1001 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002
1003 // Return the new list of results.
1004 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1005 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1006}
1007
1008SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
1009 unsigned CC) {
1010 SDOperand Chain = Op.getOperand(0);
1011 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 SDOperand Callee = Op.getOperand(4);
1013 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 // Analyze operands of the call, assigning locations to each operand.
1016 SmallVector<CCValAssign, 16> ArgLocs;
1017 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001018 if(CC==CallingConv::Fast && PerformTailCallOpt)
1019 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1020 else
1021 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022
1023 // Get a count of how many bytes are to be pushed on the stack.
1024 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001025 if (CC==CallingConv::Fast)
1026 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027
1028 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1029
1030 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1031 SmallVector<SDOperand, 8> MemOpChains;
1032
1033 SDOperand StackPtr;
1034
1035 // Walk the register/memloc assignments, inserting copies/loads.
1036 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1037 CCValAssign &VA = ArgLocs[i];
1038 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1039
1040 // Promote the value if needed.
1041 switch (VA.getLocInfo()) {
1042 default: assert(0 && "Unknown loc info!");
1043 case CCValAssign::Full: break;
1044 case CCValAssign::SExt:
1045 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1046 break;
1047 case CCValAssign::ZExt:
1048 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1049 break;
1050 case CCValAssign::AExt:
1051 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1052 break;
1053 }
1054
1055 if (VA.isRegLoc()) {
1056 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1057 } else {
1058 assert(VA.isMemLoc());
1059 if (StackPtr.Val == 0)
1060 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola007b7142007-09-21 15:50:22 +00001061
1062 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1063 Arg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 }
1065 }
1066
1067 // If the first argument is an sret pointer, remember it.
1068 bool isSRet = NumOps &&
1069 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
1070 ISD::ParamFlags::StructReturn);
1071
1072 if (!MemOpChains.empty())
1073 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1074 &MemOpChains[0], MemOpChains.size());
1075
1076 // Build a sequence of copy-to-reg nodes chained together with token chain
1077 // and flag operands which copy the outgoing args into registers.
1078 SDOperand InFlag;
1079 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1080 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1081 InFlag);
1082 InFlag = Chain.getValue(1);
1083 }
1084
1085 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1086 // GOT pointer.
1087 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1088 Subtarget->isPICStyleGOT()) {
1089 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1090 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1091 InFlag);
1092 InFlag = Chain.getValue(1);
1093 }
1094
1095 // If the callee is a GlobalAddress node (quite common, every direct call is)
1096 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1097 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1098 // We should use extra load for direct calls to dllimported functions in
1099 // non-JIT mode.
1100 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1101 getTargetMachine(), true))
1102 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1103 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1104 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1105
1106 // Returns a chain & a flag for retval copy to use.
1107 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1108 SmallVector<SDOperand, 8> Ops;
1109 Ops.push_back(Chain);
1110 Ops.push_back(Callee);
1111
1112 // Add argument registers to the end of the list so that they are known live
1113 // into the call.
1114 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1115 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1116 RegsToPass[i].second.getValueType()));
1117
1118 // Add an implicit use GOT pointer in EBX.
1119 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1120 Subtarget->isPICStyleGOT())
1121 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1122
1123 if (InFlag.Val)
1124 Ops.push_back(InFlag);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001125
1126 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127 InFlag = Chain.getValue(1);
1128
1129 // Create the CALLSEQ_END node.
1130 unsigned NumBytesForCalleeToPush = 0;
1131
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001132 if (CC == CallingConv::X86_StdCall ||
1133 (CC == CallingConv::Fast && PerformTailCallOpt)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 if (isVarArg)
1135 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1136 else
1137 NumBytesForCalleeToPush = NumBytes;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001138 assert(!(isVarArg && CC==CallingConv::Fast) &&
1139 "CallingConv::Fast does not support varargs.");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 } else {
1141 // If this is is a call to a struct-return function, the callee
1142 // pops the hidden struct pointer, so we have to push it back.
1143 // This is common for Darwin/X86, Linux & Mingw32 targets.
1144 NumBytesForCalleeToPush = isSRet ? 4 : 0;
1145 }
Bill Wendling22f8deb2007-11-13 00:44:25 +00001146
1147 Chain = DAG.getCALLSEQ_END(Chain,
1148 DAG.getConstant(NumBytes, getPointerTy()),
1149 DAG.getConstant(NumBytesForCalleeToPush,
1150 getPointerTy()),
1151 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 InFlag = Chain.getValue(1);
1153
1154 // Handle result values, copying them out of physregs into vregs that we
1155 // return.
1156 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1157}
1158
1159
1160//===----------------------------------------------------------------------===//
1161// FastCall Calling Convention implementation
1162//===----------------------------------------------------------------------===//
1163//
1164// The X86 'fastcall' calling convention passes up to two integer arguments in
1165// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1166// and requires that the callee pop its arguments off the stack (allowing proper
1167// tail calls), and has the same return value conventions as C calling convs.
1168//
1169// This calling convention always arranges for the callee pop value to be 8n+4
1170// bytes, which is needed for tail recursion elimination and stack alignment
1171// reasons.
1172SDOperand
1173X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
1174 MachineFunction &MF = DAG.getMachineFunction();
1175 MachineFrameInfo *MFI = MF.getFrameInfo();
1176 SDOperand Root = Op.getOperand(0);
1177 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1178
1179 // Assign locations to all of the incoming arguments.
1180 SmallVector<CCValAssign, 16> ArgLocs;
1181 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1182 getTargetMachine(), ArgLocs);
1183 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
1184
1185 SmallVector<SDOperand, 8> ArgValues;
1186 unsigned LastVal = ~0U;
1187 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1188 CCValAssign &VA = ArgLocs[i];
1189 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1190 // places.
1191 assert(VA.getValNo() != LastVal &&
1192 "Don't support value assigned to multiple locs yet");
1193 LastVal = VA.getValNo();
1194
1195 if (VA.isRegLoc()) {
1196 MVT::ValueType RegVT = VA.getLocVT();
1197 TargetRegisterClass *RC;
1198 if (RegVT == MVT::i32)
1199 RC = X86::GR32RegisterClass;
1200 else {
1201 assert(MVT::isVector(RegVT));
1202 RC = X86::VR128RegisterClass;
1203 }
1204
1205 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1206 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1207
1208 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1209 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1210 // right size.
1211 if (VA.getLocInfo() == CCValAssign::SExt)
1212 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1213 DAG.getValueType(VA.getValVT()));
1214 else if (VA.getLocInfo() == CCValAssign::ZExt)
1215 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1216 DAG.getValueType(VA.getValVT()));
1217
1218 if (VA.getLocInfo() != CCValAssign::Full)
1219 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1220
1221 ArgValues.push_back(ArgValue);
1222 } else {
1223 assert(VA.isMemLoc());
Rafael Espindolab53ef122007-09-21 14:55:38 +00001224 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225 }
1226 }
1227
1228 ArgValues.push_back(Root);
1229
1230 unsigned StackSize = CCInfo.getNextStackOffset();
1231
1232 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1233 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001234 // arguments and the arguments after the retaddr has been pushed are
1235 // aligned.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236 if ((StackSize & 7) == 0)
1237 StackSize += 4;
1238 }
1239
1240 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1241 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001242 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
1243 BytesCallerReserves = 0;
1244
Anton Korobeynikove844e472007-08-15 17:12:32 +00001245 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1246 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247
1248 // Return the new list of results.
1249 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1250 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1251}
1252
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001253SDOperand
1254X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1255 const SDOperand &StackPtr,
1256 const CCValAssign &VA,
1257 SDOperand Chain,
1258 SDOperand Arg) {
1259 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1260 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1261 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1262 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1263 if (Flags & ISD::ParamFlags::ByVal) {
1264 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1265 ISD::ParamFlags::ByValAlignOffs);
1266
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001267 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1268 ISD::ParamFlags::ByValSizeOffs;
1269
1270 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1271 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00001272 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001273
Rafael Espindola80825902007-10-19 10:41:11 +00001274 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1275 AlwaysInline);
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001276 } else {
1277 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1278 }
1279}
1280
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1282 unsigned CC) {
1283 SDOperand Chain = Op.getOperand(0);
1284 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1285 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1286 SDOperand Callee = Op.getOperand(4);
1287
1288 // Analyze operands of the call, assigning locations to each operand.
1289 SmallVector<CCValAssign, 16> ArgLocs;
1290 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1291 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
1292
1293 // Get a count of how many bytes are to be pushed on the stack.
1294 unsigned NumBytes = CCInfo.getNextStackOffset();
1295
1296 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
1297 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001298 // arguments and the arguments after the retaddr has been pushed are
1299 // aligned.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 if ((NumBytes & 7) == 0)
1301 NumBytes += 4;
1302 }
1303
1304 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1305
1306 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1307 SmallVector<SDOperand, 8> MemOpChains;
1308
1309 SDOperand StackPtr;
1310
1311 // Walk the register/memloc assignments, inserting copies/loads.
1312 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1313 CCValAssign &VA = ArgLocs[i];
1314 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1315
1316 // Promote the value if needed.
1317 switch (VA.getLocInfo()) {
1318 default: assert(0 && "Unknown loc info!");
1319 case CCValAssign::Full: break;
1320 case CCValAssign::SExt:
1321 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1322 break;
1323 case CCValAssign::ZExt:
1324 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1325 break;
1326 case CCValAssign::AExt:
1327 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1328 break;
1329 }
1330
1331 if (VA.isRegLoc()) {
1332 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1333 } else {
1334 assert(VA.isMemLoc());
1335 if (StackPtr.Val == 0)
1336 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola007b7142007-09-21 15:50:22 +00001337
1338 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1339 Arg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001340 }
1341 }
1342
1343 if (!MemOpChains.empty())
1344 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1345 &MemOpChains[0], MemOpChains.size());
1346
1347 // Build a sequence of copy-to-reg nodes chained together with token chain
1348 // and flag operands which copy the outgoing args into registers.
1349 SDOperand InFlag;
1350 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1351 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1352 InFlag);
1353 InFlag = Chain.getValue(1);
1354 }
1355
1356 // If the callee is a GlobalAddress node (quite common, every direct call is)
1357 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1358 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1359 // We should use extra load for direct calls to dllimported functions in
1360 // non-JIT mode.
1361 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1362 getTargetMachine(), true))
1363 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1364 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1365 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1366
1367 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1368 // GOT pointer.
1369 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1370 Subtarget->isPICStyleGOT()) {
1371 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1372 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1373 InFlag);
1374 InFlag = Chain.getValue(1);
1375 }
1376
1377 // Returns a chain & a flag for retval copy to use.
1378 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1379 SmallVector<SDOperand, 8> Ops;
1380 Ops.push_back(Chain);
1381 Ops.push_back(Callee);
1382
1383 // Add argument registers to the end of the list so that they are known live
1384 // into the call.
1385 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1386 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1387 RegsToPass[i].second.getValueType()));
1388
1389 // Add an implicit use GOT pointer in EBX.
1390 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1391 Subtarget->isPICStyleGOT())
1392 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1393
1394 if (InFlag.Val)
1395 Ops.push_back(InFlag);
1396
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001397 assert(isTailCall==false && "no tail call here");
1398 Chain = DAG.getNode(X86ISD::CALL,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001399 NodeTys, &Ops[0], Ops.size());
1400 InFlag = Chain.getValue(1);
1401
1402 // Returns a flag for retval copy to use.
1403 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1404 Ops.clear();
1405 Ops.push_back(Chain);
1406 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1407 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1408 Ops.push_back(InFlag);
1409 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1410 InFlag = Chain.getValue(1);
1411
1412 // Handle result values, copying them out of physregs into vregs that we
1413 // return.
1414 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1415}
1416
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001417//===----------------------------------------------------------------------===//
1418// Fast Calling Convention (tail call) implementation
1419//===----------------------------------------------------------------------===//
1420
1421// Like std call, callee cleans arguments, convention except that ECX is
1422// reserved for storing the tail called function address. Only 2 registers are
1423// free for argument passing (inreg). Tail call optimization is performed
1424// provided:
1425// * tailcallopt is enabled
1426// * caller/callee are fastcc
1427// * elf/pic is disabled OR
1428// * elf/pic enabled + callee is in module + callee has
1429// visibility protected or hidden
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001430// To keep the stack aligned according to platform abi the function
1431// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1432// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001433// If a tail called function callee has more arguments than the caller the
1434// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001435// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001436// original REtADDR, but before the saved framepointer or the spilled registers
1437// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1438// stack layout:
1439// arg1
1440// arg2
1441// RETADDR
1442// [ new RETADDR
1443// move area ]
1444// (possible EBP)
1445// ESI
1446// EDI
1447// local1 ..
1448
1449/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1450/// for a 16 byte align requirement.
1451unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1452 SelectionDAG& DAG) {
1453 if (PerformTailCallOpt) {
1454 MachineFunction &MF = DAG.getMachineFunction();
1455 const TargetMachine &TM = MF.getTarget();
1456 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1457 unsigned StackAlignment = TFI.getStackAlignment();
1458 uint64_t AlignMask = StackAlignment - 1;
1459 int64_t Offset = StackSize;
1460 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1461 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1462 // Number smaller than 12 so just add the difference.
1463 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1464 } else {
1465 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1466 Offset = ((~AlignMask) & Offset) + StackAlignment +
1467 (StackAlignment-SlotSize);
1468 }
1469 StackSize = Offset;
1470 }
1471 return StackSize;
1472}
1473
1474/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001475/// following the call is a return. A function is eligible if caller/callee
1476/// calling conventions match, currently only fastcc supports tail calls, and
1477/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001478bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1479 SDOperand Ret,
1480 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001481 if (!PerformTailCallOpt)
1482 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001483
1484 // Check whether CALL node immediatly preceeds the RET node and whether the
1485 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001486 unsigned NumOps = Ret.getNumOperands();
1487 if ((NumOps == 1 &&
1488 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1489 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001490 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001491 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1492 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001493 MachineFunction &MF = DAG.getMachineFunction();
1494 unsigned CallerCC = MF.getFunction()->getCallingConv();
1495 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1496 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1497 SDOperand Callee = Call.getOperand(4);
1498 // On elf/pic %ebx needs to be livein.
Evan Chenge7a87392007-11-02 01:26:22 +00001499 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1500 !Subtarget->isPICStyleGOT())
1501 return true;
1502
1503 // Can only do local tail calls with PIC.
1504 GlobalValue * GV = 0;
1505 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1506 if(G != 0 &&
1507 (GV = G->getGlobal()) &&
1508 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1509 return true;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001510 }
1511 }
Evan Chenge7a87392007-11-02 01:26:22 +00001512
1513 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001514}
1515
1516SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1517 SelectionDAG &DAG,
1518 unsigned CC) {
1519 SDOperand Chain = Op.getOperand(0);
1520 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1521 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1522 SDOperand Callee = Op.getOperand(4);
1523 bool is64Bit = Subtarget->is64Bit();
1524
1525 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1526
1527 // Analyze operands of the call, assigning locations to each operand.
1528 SmallVector<CCValAssign, 16> ArgLocs;
1529 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1530 if (is64Bit)
1531 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1532 else
1533 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1534
1535
1536 // Lower arguments at fp - stackoffset + fpdiff.
1537 MachineFunction &MF = DAG.getMachineFunction();
1538
1539 unsigned NumBytesToBePushed =
1540 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1541
1542 unsigned NumBytesCallerPushed =
1543 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1544 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1545
1546 // Set the delta of movement of the returnaddr stackslot.
1547 // But only set if delta is greater than previous delta.
1548 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1549 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1550
Arnold Schwaighofer10202b32007-10-16 09:05:00 +00001551 Chain = DAG.
1552 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1553
1554 // Adjust the Return address stack slot.
1555 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001556 if (FPDiff) {
1557 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofer10202b32007-10-16 09:05:00 +00001558 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1559 // Load the "old" Return address.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001560 RetAddrFrIdx =
Arnold Schwaighofer10202b32007-10-16 09:05:00 +00001561 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1562 // Calculate the new stack slot for the return address.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001563 int SlotSize = is64Bit ? 8 : 4;
1564 int NewReturnAddrFI =
1565 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Arnold Schwaighofer10202b32007-10-16 09:05:00 +00001566 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1567 Chain = SDOperand(RetAddrFrIdx.Val, 1);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001568 }
1569
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001570 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1571 SmallVector<SDOperand, 8> MemOpChains;
1572 SmallVector<SDOperand, 8> MemOpChains2;
1573 SDOperand FramePtr, StackPtr;
1574 SDOperand PtrOff;
1575 SDOperand FIN;
1576 int FI = 0;
1577
1578 // Walk the register/memloc assignments, inserting copies/loads. Lower
1579 // arguments first to the stack slot where they would normally - in case of a
1580 // normal function call - be.
1581 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1582 CCValAssign &VA = ArgLocs[i];
1583 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1584
1585 // Promote the value if needed.
1586 switch (VA.getLocInfo()) {
1587 default: assert(0 && "Unknown loc info!");
1588 case CCValAssign::Full: break;
1589 case CCValAssign::SExt:
1590 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1591 break;
1592 case CCValAssign::ZExt:
1593 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1594 break;
1595 case CCValAssign::AExt:
1596 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1597 break;
1598 }
1599
1600 if (VA.isRegLoc()) {
1601 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1602 } else {
1603 assert(VA.isMemLoc());
1604 if (StackPtr.Val == 0)
1605 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1606
1607 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1608 Arg));
1609 }
1610 }
1611
1612 if (!MemOpChains.empty())
1613 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1614 &MemOpChains[0], MemOpChains.size());
1615
1616 // Build a sequence of copy-to-reg nodes chained together with token chain
1617 // and flag operands which copy the outgoing args into registers.
1618 SDOperand InFlag;
1619 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1620 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1621 InFlag);
1622 InFlag = Chain.getValue(1);
1623 }
1624 InFlag = SDOperand();
Arnold Schwaighofer10202b32007-10-16 09:05:00 +00001625
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001626 // Copy from stack slots to stack slot of a tail called function. This needs
1627 // to be done because if we would lower the arguments directly to their real
1628 // stack slot we might end up overwriting each other.
1629 // TODO: To make this more efficient (sometimes saving a store/load) we could
1630 // analyse the arguments and emit this store/load/store sequence only for
1631 // arguments which would be overwritten otherwise.
1632 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1633 CCValAssign &VA = ArgLocs[i];
1634 if (!VA.isRegLoc()) {
1635 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1636 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1637
1638 // Get source stack slot.
1639 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1640 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1641 // Create frame index.
1642 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1643 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1644 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1645 FIN = DAG.getFrameIndex(FI, MVT::i32);
1646 if (Flags & ISD::ParamFlags::ByVal) {
1647 // Copy relative to framepointer.
1648 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1649 ISD::ParamFlags::ByValAlignOffs);
1650
1651 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1652 ISD::ParamFlags::ByValSizeOffs;
1653
1654 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1655 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer97794942007-11-10 10:48:01 +00001656 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
1657
1658 MemOpChains2.push_back(DAG.getMemcpy(Chain, FIN, PtrOff, SizeNode,
1659 AlignNode,AlwaysInline));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001660 } else {
1661 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1662 // Store relative to framepointer.
1663 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1664 }
1665 }
1666 }
1667
1668 if (!MemOpChains2.empty())
1669 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1670 &MemOpChains2[0], MemOpChains.size());
1671
Arnold Schwaighofer10202b32007-10-16 09:05:00 +00001672 // Store the return address to the appropriate stack slot.
1673 if (FPDiff)
1674 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1675
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001676 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1677 // GOT pointer.
1678 // Does not work with tail call since ebx is not restored correctly by
1679 // tailcaller. TODO: at least for x86 - verify for x86-64
1680
1681 // If the callee is a GlobalAddress node (quite common, every direct call is)
1682 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1683 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1684 // We should use extra load for direct calls to dllimported functions in
1685 // non-JIT mode.
1686 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1687 getTargetMachine(), true))
1688 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1689 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1690 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1691 else {
1692 assert(Callee.getOpcode() == ISD::LOAD &&
1693 "Function destination must be loaded into virtual register");
1694 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1695
1696 Chain = DAG.getCopyToReg(Chain,
1697 DAG.getRegister(Opc, getPointerTy()) ,
1698 Callee,InFlag);
1699 Callee = DAG.getRegister(Opc, getPointerTy());
1700 // Add register as live out.
1701 DAG.getMachineFunction().addLiveOut(Opc);
1702 }
1703
1704 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1705 SmallVector<SDOperand, 8> Ops;
1706
1707 Ops.push_back(Chain);
1708 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1709 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1710 if (InFlag.Val)
1711 Ops.push_back(InFlag);
1712 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1713 InFlag = Chain.getValue(1);
1714
1715 // Returns a chain & a flag for retval copy to use.
1716 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1717 Ops.clear();
1718 Ops.push_back(Chain);
1719 Ops.push_back(Callee);
1720 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1721 // Add argument registers to the end of the list so that they are known live
1722 // into the call.
1723 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1724 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1725 RegsToPass[i].second.getValueType()));
1726 if (InFlag.Val)
1727 Ops.push_back(InFlag);
1728 assert(InFlag.Val &&
1729 "Flag must be set. Depend on flag being set in LowerRET");
1730 Chain = DAG.getNode(X86ISD::TAILCALL,
1731 Op.Val->getVTList(), &Ops[0], Ops.size());
1732
1733 return SDOperand(Chain.Val, Op.ResNo);
1734}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735
1736//===----------------------------------------------------------------------===//
1737// X86-64 C Calling Convention implementation
1738//===----------------------------------------------------------------------===//
1739
1740SDOperand
1741X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1742 MachineFunction &MF = DAG.getMachineFunction();
1743 MachineFrameInfo *MFI = MF.getFrameInfo();
1744 SDOperand Root = Op.getOperand(0);
1745 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001746 unsigned CC= MF.getFunction()->getCallingConv();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001747
1748 static const unsigned GPR64ArgRegs[] = {
1749 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1750 };
1751 static const unsigned XMMArgRegs[] = {
1752 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1753 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1754 };
1755
1756
1757 // Assign locations to all of the incoming arguments.
1758 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001759 CCState CCInfo(CC, isVarArg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001760 getTargetMachine(), ArgLocs);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001761 if (CC == CallingConv::Fast && PerformTailCallOpt)
1762 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1763 else
1764 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001765
1766 SmallVector<SDOperand, 8> ArgValues;
1767 unsigned LastVal = ~0U;
1768 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1769 CCValAssign &VA = ArgLocs[i];
1770 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1771 // places.
1772 assert(VA.getValNo() != LastVal &&
1773 "Don't support value assigned to multiple locs yet");
1774 LastVal = VA.getValNo();
1775
1776 if (VA.isRegLoc()) {
1777 MVT::ValueType RegVT = VA.getLocVT();
1778 TargetRegisterClass *RC;
1779 if (RegVT == MVT::i32)
1780 RC = X86::GR32RegisterClass;
1781 else if (RegVT == MVT::i64)
1782 RC = X86::GR64RegisterClass;
1783 else if (RegVT == MVT::f32)
1784 RC = X86::FR32RegisterClass;
1785 else if (RegVT == MVT::f64)
1786 RC = X86::FR64RegisterClass;
1787 else {
1788 assert(MVT::isVector(RegVT));
1789 if (MVT::getSizeInBits(RegVT) == 64) {
1790 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1791 RegVT = MVT::i64;
1792 } else
1793 RC = X86::VR128RegisterClass;
1794 }
1795
1796 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1797 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1798
1799 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1800 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1801 // right size.
1802 if (VA.getLocInfo() == CCValAssign::SExt)
1803 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1804 DAG.getValueType(VA.getValVT()));
1805 else if (VA.getLocInfo() == CCValAssign::ZExt)
1806 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1807 DAG.getValueType(VA.getValVT()));
1808
1809 if (VA.getLocInfo() != CCValAssign::Full)
1810 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1811
1812 // Handle MMX values passed in GPRs.
1813 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1814 MVT::getSizeInBits(RegVT) == 64)
1815 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1816
1817 ArgValues.push_back(ArgValue);
1818 } else {
1819 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001820 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001821 }
1822 }
1823
1824 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001825 if (CC==CallingConv::Fast)
1826 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001827
1828 // If the function takes variable number of arguments, make a frame index for
1829 // the start of the first vararg value... for expansion of llvm.va_start.
1830 if (isVarArg) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001831 assert(CC!=CallingConv::Fast
1832 && "Var arg not supported with calling convention fastcc");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1834 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1835
1836 // For X86-64, if there are vararg parameters that are passed via
1837 // registers, then we must store them to their spots on the stack so they
1838 // may be loaded by deferencing the result of va_next.
1839 VarArgsGPOffset = NumIntRegs * 8;
1840 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1841 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1842 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1843
1844 // Store the integer parameter registers.
1845 SmallVector<SDOperand, 8> MemOps;
1846 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1847 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1848 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1849 for (; NumIntRegs != 6; ++NumIntRegs) {
1850 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1851 X86::GR64RegisterClass);
1852 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1853 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1854 MemOps.push_back(Store);
1855 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1856 DAG.getConstant(8, getPointerTy()));
1857 }
1858
1859 // Now store the XMM (fp + vector) parameter registers.
1860 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1861 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1862 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1863 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1864 X86::VR128RegisterClass);
1865 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1866 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1867 MemOps.push_back(Store);
1868 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1869 DAG.getConstant(16, getPointerTy()));
1870 }
1871 if (!MemOps.empty())
1872 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1873 &MemOps[0], MemOps.size());
1874 }
1875
1876 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001877 // Tail call convention (fastcc) needs callee pop.
Evan Cheng778fa0f2007-10-14 10:09:39 +00001878 if (CC == CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001879 BytesToPopOnReturn = StackSize; // Callee pops everything.
1880 BytesCallerReserves = 0;
1881 } else {
1882 BytesToPopOnReturn = 0; // Callee pops nothing.
1883 BytesCallerReserves = StackSize;
1884 }
Anton Korobeynikove844e472007-08-15 17:12:32 +00001885 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1886 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1887
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001888 // Return the new list of results.
1889 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1890 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1891}
1892
1893SDOperand
1894X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1895 unsigned CC) {
1896 SDOperand Chain = Op.getOperand(0);
1897 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 SDOperand Callee = Op.getOperand(4);
1899
1900 // Analyze operands of the call, assigning locations to each operand.
1901 SmallVector<CCValAssign, 16> ArgLocs;
1902 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng778fa0f2007-10-14 10:09:39 +00001903 if (CC==CallingConv::Fast && PerformTailCallOpt)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001904 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1905 else
1906 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001907
1908 // Get a count of how many bytes are to be pushed on the stack.
1909 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001910 if (CC == CallingConv::Fast)
1911 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1912
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001913 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1914
1915 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1916 SmallVector<SDOperand, 8> MemOpChains;
1917
1918 SDOperand StackPtr;
1919
1920 // Walk the register/memloc assignments, inserting copies/loads.
1921 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1922 CCValAssign &VA = ArgLocs[i];
1923 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1924
1925 // Promote the value if needed.
1926 switch (VA.getLocInfo()) {
1927 default: assert(0 && "Unknown loc info!");
1928 case CCValAssign::Full: break;
1929 case CCValAssign::SExt:
1930 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1931 break;
1932 case CCValAssign::ZExt:
1933 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1934 break;
1935 case CCValAssign::AExt:
1936 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1937 break;
1938 }
1939
1940 if (VA.isRegLoc()) {
1941 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1942 } else {
1943 assert(VA.isMemLoc());
1944 if (StackPtr.Val == 0)
1945 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolab8bcfcd2007-08-20 15:18:24 +00001946
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001947 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1948 Arg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001949 }
1950 }
1951
1952 if (!MemOpChains.empty())
1953 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1954 &MemOpChains[0], MemOpChains.size());
1955
1956 // Build a sequence of copy-to-reg nodes chained together with token chain
1957 // and flag operands which copy the outgoing args into registers.
1958 SDOperand InFlag;
1959 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1960 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1961 InFlag);
1962 InFlag = Chain.getValue(1);
1963 }
1964
1965 if (isVarArg) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001966 assert ( CallingConv::Fast != CC &&
1967 "Var args not supported with calling convention fastcc");
1968
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 // From AMD64 ABI document:
1970 // For calls that may call functions that use varargs or stdargs
1971 // (prototype-less calls or calls to functions containing ellipsis (...) in
1972 // the declaration) %al is used as hidden argument to specify the number
1973 // of SSE registers used. The contents of %al do not need to match exactly
1974 // the number of registers, but must be an ubound on the number of SSE
1975 // registers used and is in the range 0 - 8 inclusive.
1976
1977 // Count the number of XMM registers allocated.
1978 static const unsigned XMMArgRegs[] = {
1979 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1980 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1981 };
1982 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1983
1984 Chain = DAG.getCopyToReg(Chain, X86::AL,
1985 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1986 InFlag = Chain.getValue(1);
1987 }
1988
1989 // If the callee is a GlobalAddress node (quite common, every direct call is)
1990 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1991 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1992 // We should use extra load for direct calls to dllimported functions in
1993 // non-JIT mode.
1994 if (getTargetMachine().getCodeModel() != CodeModel::Large
1995 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1996 getTargetMachine(), true))
1997 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1998 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1999 if (getTargetMachine().getCodeModel() != CodeModel::Large)
2000 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
2001
2002 // Returns a chain & a flag for retval copy to use.
2003 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2004 SmallVector<SDOperand, 8> Ops;
2005 Ops.push_back(Chain);
2006 Ops.push_back(Callee);
2007
2008 // Add argument registers to the end of the list so that they are known live
2009 // into the call.
2010 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2011 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2012 RegsToPass[i].second.getValueType()));
2013
2014 if (InFlag.Val)
2015 Ops.push_back(InFlag);
2016
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002017 Chain = DAG.getNode(X86ISD::CALL,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002018 NodeTys, &Ops[0], Ops.size());
2019 InFlag = Chain.getValue(1);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002020 int NumBytesForCalleeToPush = 0;
Evan Cheng778fa0f2007-10-14 10:09:39 +00002021 if (CC==CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002022 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002023 } else {
2024 NumBytesForCalleeToPush = 0; // Callee pops nothing.
2025 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 // Returns a flag for retval copy to use.
2027 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2028 Ops.clear();
2029 Ops.push_back(Chain);
2030 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002031 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002032 Ops.push_back(InFlag);
2033 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2034 InFlag = Chain.getValue(1);
2035
2036 // Handle result values, copying them out of physregs into vregs that we
2037 // return.
2038 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
2039}
2040
2041
2042//===----------------------------------------------------------------------===//
2043// Other Lowering Hooks
2044//===----------------------------------------------------------------------===//
2045
2046
2047SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00002048 MachineFunction &MF = DAG.getMachineFunction();
2049 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2050 int ReturnAddrIndex = FuncInfo->getRAIndex();
2051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002052 if (ReturnAddrIndex == 0) {
2053 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054 if (Subtarget->is64Bit())
2055 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2056 else
2057 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00002058
2059 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 }
2061
2062 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2063}
2064
2065
2066
2067/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2068/// specific condition code. It returns a false if it cannot do a direct
2069/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2070/// needed.
2071static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2072 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2073 SelectionDAG &DAG) {
2074 X86CC = X86::COND_INVALID;
2075 if (!isFP) {
2076 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2077 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2078 // X > -1 -> X == 0, jump !sign.
2079 RHS = DAG.getConstant(0, RHS.getValueType());
2080 X86CC = X86::COND_NS;
2081 return true;
2082 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2083 // X < 0 -> X == 0, jump on sign.
2084 X86CC = X86::COND_S;
2085 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00002086 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2087 // X < 1 -> X <= 0
2088 RHS = DAG.getConstant(0, RHS.getValueType());
2089 X86CC = X86::COND_LE;
2090 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 }
2092 }
2093
2094 switch (SetCCOpcode) {
2095 default: break;
2096 case ISD::SETEQ: X86CC = X86::COND_E; break;
2097 case ISD::SETGT: X86CC = X86::COND_G; break;
2098 case ISD::SETGE: X86CC = X86::COND_GE; break;
2099 case ISD::SETLT: X86CC = X86::COND_L; break;
2100 case ISD::SETLE: X86CC = X86::COND_LE; break;
2101 case ISD::SETNE: X86CC = X86::COND_NE; break;
2102 case ISD::SETULT: X86CC = X86::COND_B; break;
2103 case ISD::SETUGT: X86CC = X86::COND_A; break;
2104 case ISD::SETULE: X86CC = X86::COND_BE; break;
2105 case ISD::SETUGE: X86CC = X86::COND_AE; break;
2106 }
2107 } else {
2108 // On a floating point condition, the flags are set as follows:
2109 // ZF PF CF op
2110 // 0 | 0 | 0 | X > Y
2111 // 0 | 0 | 1 | X < Y
2112 // 1 | 0 | 0 | X == Y
2113 // 1 | 1 | 1 | unordered
2114 bool Flip = false;
2115 switch (SetCCOpcode) {
2116 default: break;
2117 case ISD::SETUEQ:
2118 case ISD::SETEQ: X86CC = X86::COND_E; break;
2119 case ISD::SETOLT: Flip = true; // Fallthrough
2120 case ISD::SETOGT:
2121 case ISD::SETGT: X86CC = X86::COND_A; break;
2122 case ISD::SETOLE: Flip = true; // Fallthrough
2123 case ISD::SETOGE:
2124 case ISD::SETGE: X86CC = X86::COND_AE; break;
2125 case ISD::SETUGT: Flip = true; // Fallthrough
2126 case ISD::SETULT:
2127 case ISD::SETLT: X86CC = X86::COND_B; break;
2128 case ISD::SETUGE: Flip = true; // Fallthrough
2129 case ISD::SETULE:
2130 case ISD::SETLE: X86CC = X86::COND_BE; break;
2131 case ISD::SETONE:
2132 case ISD::SETNE: X86CC = X86::COND_NE; break;
2133 case ISD::SETUO: X86CC = X86::COND_P; break;
2134 case ISD::SETO: X86CC = X86::COND_NP; break;
2135 }
2136 if (Flip)
2137 std::swap(LHS, RHS);
2138 }
2139
2140 return X86CC != X86::COND_INVALID;
2141}
2142
2143/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2144/// code. Current x86 isa includes the following FP cmov instructions:
2145/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2146static bool hasFPCMov(unsigned X86CC) {
2147 switch (X86CC) {
2148 default:
2149 return false;
2150 case X86::COND_B:
2151 case X86::COND_BE:
2152 case X86::COND_E:
2153 case X86::COND_P:
2154 case X86::COND_A:
2155 case X86::COND_AE:
2156 case X86::COND_NE:
2157 case X86::COND_NP:
2158 return true;
2159 }
2160}
2161
2162/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2163/// true if Op is undef or if its value falls within the specified range (L, H].
2164static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2165 if (Op.getOpcode() == ISD::UNDEF)
2166 return true;
2167
2168 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2169 return (Val >= Low && Val < Hi);
2170}
2171
2172/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2173/// true if Op is undef or if its value equal to the specified value.
2174static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2175 if (Op.getOpcode() == ISD::UNDEF)
2176 return true;
2177 return cast<ConstantSDNode>(Op)->getValue() == Val;
2178}
2179
2180/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2181/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2182bool X86::isPSHUFDMask(SDNode *N) {
2183 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2184
Dan Gohman7dc19012007-08-02 21:17:01 +00002185 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002186 return false;
2187
2188 // Check if the value doesn't reference the second vector.
2189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2190 SDOperand Arg = N->getOperand(i);
2191 if (Arg.getOpcode() == ISD::UNDEF) continue;
2192 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002193 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 return false;
2195 }
2196
2197 return true;
2198}
2199
2200/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2201/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2202bool X86::isPSHUFHWMask(SDNode *N) {
2203 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2204
2205 if (N->getNumOperands() != 8)
2206 return false;
2207
2208 // Lower quadword copied in order.
2209 for (unsigned i = 0; i != 4; ++i) {
2210 SDOperand Arg = N->getOperand(i);
2211 if (Arg.getOpcode() == ISD::UNDEF) continue;
2212 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2213 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2214 return false;
2215 }
2216
2217 // Upper quadword shuffled.
2218 for (unsigned i = 4; i != 8; ++i) {
2219 SDOperand Arg = N->getOperand(i);
2220 if (Arg.getOpcode() == ISD::UNDEF) continue;
2221 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2222 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2223 if (Val < 4 || Val > 7)
2224 return false;
2225 }
2226
2227 return true;
2228}
2229
2230/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2231/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2232bool X86::isPSHUFLWMask(SDNode *N) {
2233 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2234
2235 if (N->getNumOperands() != 8)
2236 return false;
2237
2238 // Upper quadword copied in order.
2239 for (unsigned i = 4; i != 8; ++i)
2240 if (!isUndefOrEqual(N->getOperand(i), i))
2241 return false;
2242
2243 // Lower quadword shuffled.
2244 for (unsigned i = 0; i != 4; ++i)
2245 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2246 return false;
2247
2248 return true;
2249}
2250
2251/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2252/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2253static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2254 if (NumElems != 2 && NumElems != 4) return false;
2255
2256 unsigned Half = NumElems / 2;
2257 for (unsigned i = 0; i < Half; ++i)
2258 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2259 return false;
2260 for (unsigned i = Half; i < NumElems; ++i)
2261 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2262 return false;
2263
2264 return true;
2265}
2266
2267bool X86::isSHUFPMask(SDNode *N) {
2268 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2269 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2270}
2271
2272/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2273/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2274/// half elements to come from vector 1 (which would equal the dest.) and
2275/// the upper half to come from vector 2.
2276static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2277 if (NumOps != 2 && NumOps != 4) return false;
2278
2279 unsigned Half = NumOps / 2;
2280 for (unsigned i = 0; i < Half; ++i)
2281 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2282 return false;
2283 for (unsigned i = Half; i < NumOps; ++i)
2284 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2285 return false;
2286 return true;
2287}
2288
2289static bool isCommutedSHUFP(SDNode *N) {
2290 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2291 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2292}
2293
2294/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2295/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2296bool X86::isMOVHLPSMask(SDNode *N) {
2297 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2298
2299 if (N->getNumOperands() != 4)
2300 return false;
2301
2302 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2303 return isUndefOrEqual(N->getOperand(0), 6) &&
2304 isUndefOrEqual(N->getOperand(1), 7) &&
2305 isUndefOrEqual(N->getOperand(2), 2) &&
2306 isUndefOrEqual(N->getOperand(3), 3);
2307}
2308
2309/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2310/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2311/// <2, 3, 2, 3>
2312bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2313 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2314
2315 if (N->getNumOperands() != 4)
2316 return false;
2317
2318 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2319 return isUndefOrEqual(N->getOperand(0), 2) &&
2320 isUndefOrEqual(N->getOperand(1), 3) &&
2321 isUndefOrEqual(N->getOperand(2), 2) &&
2322 isUndefOrEqual(N->getOperand(3), 3);
2323}
2324
2325/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2326/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2327bool X86::isMOVLPMask(SDNode *N) {
2328 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2329
2330 unsigned NumElems = N->getNumOperands();
2331 if (NumElems != 2 && NumElems != 4)
2332 return false;
2333
2334 for (unsigned i = 0; i < NumElems/2; ++i)
2335 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2336 return false;
2337
2338 for (unsigned i = NumElems/2; i < NumElems; ++i)
2339 if (!isUndefOrEqual(N->getOperand(i), i))
2340 return false;
2341
2342 return true;
2343}
2344
2345/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2346/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2347/// and MOVLHPS.
2348bool X86::isMOVHPMask(SDNode *N) {
2349 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2350
2351 unsigned NumElems = N->getNumOperands();
2352 if (NumElems != 2 && NumElems != 4)
2353 return false;
2354
2355 for (unsigned i = 0; i < NumElems/2; ++i)
2356 if (!isUndefOrEqual(N->getOperand(i), i))
2357 return false;
2358
2359 for (unsigned i = 0; i < NumElems/2; ++i) {
2360 SDOperand Arg = N->getOperand(i + NumElems/2);
2361 if (!isUndefOrEqual(Arg, i + NumElems))
2362 return false;
2363 }
2364
2365 return true;
2366}
2367
2368/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2369/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2370bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2371 bool V2IsSplat = false) {
2372 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2373 return false;
2374
2375 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2376 SDOperand BitI = Elts[i];
2377 SDOperand BitI1 = Elts[i+1];
2378 if (!isUndefOrEqual(BitI, j))
2379 return false;
2380 if (V2IsSplat) {
2381 if (isUndefOrEqual(BitI1, NumElts))
2382 return false;
2383 } else {
2384 if (!isUndefOrEqual(BitI1, j + NumElts))
2385 return false;
2386 }
2387 }
2388
2389 return true;
2390}
2391
2392bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2393 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2394 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2395}
2396
2397/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2398/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2399bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2400 bool V2IsSplat = false) {
2401 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2402 return false;
2403
2404 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2405 SDOperand BitI = Elts[i];
2406 SDOperand BitI1 = Elts[i+1];
2407 if (!isUndefOrEqual(BitI, j + NumElts/2))
2408 return false;
2409 if (V2IsSplat) {
2410 if (isUndefOrEqual(BitI1, NumElts))
2411 return false;
2412 } else {
2413 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2414 return false;
2415 }
2416 }
2417
2418 return true;
2419}
2420
2421bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2422 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2423 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2424}
2425
2426/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2427/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2428/// <0, 0, 1, 1>
2429bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2430 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2431
2432 unsigned NumElems = N->getNumOperands();
2433 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2434 return false;
2435
2436 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2437 SDOperand BitI = N->getOperand(i);
2438 SDOperand BitI1 = N->getOperand(i+1);
2439
2440 if (!isUndefOrEqual(BitI, j))
2441 return false;
2442 if (!isUndefOrEqual(BitI1, j))
2443 return false;
2444 }
2445
2446 return true;
2447}
2448
2449/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2450/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2451/// <2, 2, 3, 3>
2452bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2453 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2454
2455 unsigned NumElems = N->getNumOperands();
2456 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2457 return false;
2458
2459 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2460 SDOperand BitI = N->getOperand(i);
2461 SDOperand BitI1 = N->getOperand(i + 1);
2462
2463 if (!isUndefOrEqual(BitI, j))
2464 return false;
2465 if (!isUndefOrEqual(BitI1, j))
2466 return false;
2467 }
2468
2469 return true;
2470}
2471
2472/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2473/// specifies a shuffle of elements that is suitable for input to MOVSS,
2474/// MOVSD, and MOVD, i.e. setting the lowest element.
2475static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002476 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 return false;
2478
2479 if (!isUndefOrEqual(Elts[0], NumElts))
2480 return false;
2481
2482 for (unsigned i = 1; i < NumElts; ++i) {
2483 if (!isUndefOrEqual(Elts[i], i))
2484 return false;
2485 }
2486
2487 return true;
2488}
2489
2490bool X86::isMOVLMask(SDNode *N) {
2491 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2492 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2493}
2494
2495/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2496/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2497/// element of vector 2 and the other elements to come from vector 1 in order.
2498static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2499 bool V2IsSplat = false,
2500 bool V2IsUndef = false) {
2501 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2502 return false;
2503
2504 if (!isUndefOrEqual(Ops[0], 0))
2505 return false;
2506
2507 for (unsigned i = 1; i < NumOps; ++i) {
2508 SDOperand Arg = Ops[i];
2509 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2510 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2511 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2512 return false;
2513 }
2514
2515 return true;
2516}
2517
2518static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2519 bool V2IsUndef = false) {
2520 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2521 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2522 V2IsSplat, V2IsUndef);
2523}
2524
2525/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2526/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2527bool X86::isMOVSHDUPMask(SDNode *N) {
2528 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2529
2530 if (N->getNumOperands() != 4)
2531 return false;
2532
2533 // Expect 1, 1, 3, 3
2534 for (unsigned i = 0; i < 2; ++i) {
2535 SDOperand Arg = N->getOperand(i);
2536 if (Arg.getOpcode() == ISD::UNDEF) continue;
2537 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2538 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2539 if (Val != 1) return false;
2540 }
2541
2542 bool HasHi = false;
2543 for (unsigned i = 2; i < 4; ++i) {
2544 SDOperand Arg = N->getOperand(i);
2545 if (Arg.getOpcode() == ISD::UNDEF) continue;
2546 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2547 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2548 if (Val != 3) return false;
2549 HasHi = true;
2550 }
2551
2552 // Don't use movshdup if it can be done with a shufps.
2553 return HasHi;
2554}
2555
2556/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2557/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2558bool X86::isMOVSLDUPMask(SDNode *N) {
2559 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2560
2561 if (N->getNumOperands() != 4)
2562 return false;
2563
2564 // Expect 0, 0, 2, 2
2565 for (unsigned i = 0; i < 2; ++i) {
2566 SDOperand Arg = N->getOperand(i);
2567 if (Arg.getOpcode() == ISD::UNDEF) continue;
2568 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2569 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2570 if (Val != 0) return false;
2571 }
2572
2573 bool HasHi = false;
2574 for (unsigned i = 2; i < 4; ++i) {
2575 SDOperand Arg = N->getOperand(i);
2576 if (Arg.getOpcode() == ISD::UNDEF) continue;
2577 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2578 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2579 if (Val != 2) return false;
2580 HasHi = true;
2581 }
2582
2583 // Don't use movshdup if it can be done with a shufps.
2584 return HasHi;
2585}
2586
2587/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2588/// specifies a identity operation on the LHS or RHS.
2589static bool isIdentityMask(SDNode *N, bool RHS = false) {
2590 unsigned NumElems = N->getNumOperands();
2591 for (unsigned i = 0; i < NumElems; ++i)
2592 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2593 return false;
2594 return true;
2595}
2596
2597/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2598/// a splat of a single element.
2599static bool isSplatMask(SDNode *N) {
2600 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2601
2602 // This is a splat operation if each element of the permute is the same, and
2603 // if the value doesn't reference the second vector.
2604 unsigned NumElems = N->getNumOperands();
2605 SDOperand ElementBase;
2606 unsigned i = 0;
2607 for (; i != NumElems; ++i) {
2608 SDOperand Elt = N->getOperand(i);
2609 if (isa<ConstantSDNode>(Elt)) {
2610 ElementBase = Elt;
2611 break;
2612 }
2613 }
2614
2615 if (!ElementBase.Val)
2616 return false;
2617
2618 for (; i != NumElems; ++i) {
2619 SDOperand Arg = N->getOperand(i);
2620 if (Arg.getOpcode() == ISD::UNDEF) continue;
2621 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2622 if (Arg != ElementBase) return false;
2623 }
2624
2625 // Make sure it is a splat of the first vector operand.
2626 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2627}
2628
2629/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2630/// a splat of a single element and it's a 2 or 4 element mask.
2631bool X86::isSplatMask(SDNode *N) {
2632 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2633
2634 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2635 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2636 return false;
2637 return ::isSplatMask(N);
2638}
2639
2640/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2641/// specifies a splat of zero element.
2642bool X86::isSplatLoMask(SDNode *N) {
2643 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2644
2645 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2646 if (!isUndefOrEqual(N->getOperand(i), 0))
2647 return false;
2648 return true;
2649}
2650
2651/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2652/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2653/// instructions.
2654unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2655 unsigned NumOperands = N->getNumOperands();
2656 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2657 unsigned Mask = 0;
2658 for (unsigned i = 0; i < NumOperands; ++i) {
2659 unsigned Val = 0;
2660 SDOperand Arg = N->getOperand(NumOperands-i-1);
2661 if (Arg.getOpcode() != ISD::UNDEF)
2662 Val = cast<ConstantSDNode>(Arg)->getValue();
2663 if (Val >= NumOperands) Val -= NumOperands;
2664 Mask |= Val;
2665 if (i != NumOperands - 1)
2666 Mask <<= Shift;
2667 }
2668
2669 return Mask;
2670}
2671
2672/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2673/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2674/// instructions.
2675unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2676 unsigned Mask = 0;
2677 // 8 nodes, but we only care about the last 4.
2678 for (unsigned i = 7; i >= 4; --i) {
2679 unsigned Val = 0;
2680 SDOperand Arg = N->getOperand(i);
2681 if (Arg.getOpcode() != ISD::UNDEF)
2682 Val = cast<ConstantSDNode>(Arg)->getValue();
2683 Mask |= (Val - 4);
2684 if (i != 4)
2685 Mask <<= 2;
2686 }
2687
2688 return Mask;
2689}
2690
2691/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2692/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2693/// instructions.
2694unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2695 unsigned Mask = 0;
2696 // 8 nodes, but we only care about the first 4.
2697 for (int i = 3; i >= 0; --i) {
2698 unsigned Val = 0;
2699 SDOperand Arg = N->getOperand(i);
2700 if (Arg.getOpcode() != ISD::UNDEF)
2701 Val = cast<ConstantSDNode>(Arg)->getValue();
2702 Mask |= Val;
2703 if (i != 0)
2704 Mask <<= 2;
2705 }
2706
2707 return Mask;
2708}
2709
2710/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2711/// specifies a 8 element shuffle that can be broken into a pair of
2712/// PSHUFHW and PSHUFLW.
2713static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2714 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2715
2716 if (N->getNumOperands() != 8)
2717 return false;
2718
2719 // Lower quadword shuffled.
2720 for (unsigned i = 0; i != 4; ++i) {
2721 SDOperand Arg = N->getOperand(i);
2722 if (Arg.getOpcode() == ISD::UNDEF) continue;
2723 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2724 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002725 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 return false;
2727 }
2728
2729 // Upper quadword shuffled.
2730 for (unsigned i = 4; i != 8; ++i) {
2731 SDOperand Arg = N->getOperand(i);
2732 if (Arg.getOpcode() == ISD::UNDEF) continue;
2733 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2734 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2735 if (Val < 4 || Val > 7)
2736 return false;
2737 }
2738
2739 return true;
2740}
2741
Chris Lattnere6aa3862007-11-25 00:24:49 +00002742/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002743/// values in ther permute mask.
2744static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2745 SDOperand &V2, SDOperand &Mask,
2746 SelectionDAG &DAG) {
2747 MVT::ValueType VT = Op.getValueType();
2748 MVT::ValueType MaskVT = Mask.getValueType();
2749 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2750 unsigned NumElems = Mask.getNumOperands();
2751 SmallVector<SDOperand, 8> MaskVec;
2752
2753 for (unsigned i = 0; i != NumElems; ++i) {
2754 SDOperand Arg = Mask.getOperand(i);
2755 if (Arg.getOpcode() == ISD::UNDEF) {
2756 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2757 continue;
2758 }
2759 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2760 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2761 if (Val < NumElems)
2762 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2763 else
2764 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2765 }
2766
2767 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002768 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002769 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2770}
2771
Evan Chenga6769df2007-12-07 21:30:01 +00002772/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2773/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002774static
2775SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2776 MVT::ValueType MaskVT = Mask.getValueType();
2777 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2778 unsigned NumElems = Mask.getNumOperands();
2779 SmallVector<SDOperand, 8> MaskVec;
2780 for (unsigned i = 0; i != NumElems; ++i) {
2781 SDOperand Arg = Mask.getOperand(i);
2782 if (Arg.getOpcode() == ISD::UNDEF) {
2783 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2784 continue;
2785 }
2786 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2787 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2788 if (Val < NumElems)
2789 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2790 else
2791 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2792 }
2793 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2794}
2795
2796
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2798/// match movhlps. The lower half elements should come from upper half of
2799/// V1 (and in order), and the upper half elements should come from the upper
2800/// half of V2 (and in order).
2801static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2802 unsigned NumElems = Mask->getNumOperands();
2803 if (NumElems != 4)
2804 return false;
2805 for (unsigned i = 0, e = 2; i != e; ++i)
2806 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2807 return false;
2808 for (unsigned i = 2; i != 4; ++i)
2809 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2810 return false;
2811 return true;
2812}
2813
2814/// isScalarLoadToVector - Returns true if the node is a scalar load that
2815/// is promoted to a vector.
2816static inline bool isScalarLoadToVector(SDNode *N) {
2817 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2818 N = N->getOperand(0).Val;
2819 return ISD::isNON_EXTLoad(N);
2820 }
2821 return false;
2822}
2823
2824/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2825/// match movlp{s|d}. The lower half elements should come from lower half of
2826/// V1 (and in order), and the upper half elements should come from the upper
2827/// half of V2 (and in order). And since V1 will become the source of the
2828/// MOVLP, it must be either a vector load or a scalar load to vector.
2829static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2830 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2831 return false;
2832 // Is V2 is a vector load, don't do this transformation. We will try to use
2833 // load folding shufps op.
2834 if (ISD::isNON_EXTLoad(V2))
2835 return false;
2836
2837 unsigned NumElems = Mask->getNumOperands();
2838 if (NumElems != 2 && NumElems != 4)
2839 return false;
2840 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2841 if (!isUndefOrEqual(Mask->getOperand(i), i))
2842 return false;
2843 for (unsigned i = NumElems/2; i != NumElems; ++i)
2844 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2845 return false;
2846 return true;
2847}
2848
2849/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2850/// all the same.
2851static bool isSplatVector(SDNode *N) {
2852 if (N->getOpcode() != ISD::BUILD_VECTOR)
2853 return false;
2854
2855 SDOperand SplatValue = N->getOperand(0);
2856 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2857 if (N->getOperand(i) != SplatValue)
2858 return false;
2859 return true;
2860}
2861
2862/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2863/// to an undef.
2864static bool isUndefShuffle(SDNode *N) {
2865 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2866 return false;
2867
2868 SDOperand V1 = N->getOperand(0);
2869 SDOperand V2 = N->getOperand(1);
2870 SDOperand Mask = N->getOperand(2);
2871 unsigned NumElems = Mask.getNumOperands();
2872 for (unsigned i = 0; i != NumElems; ++i) {
2873 SDOperand Arg = Mask.getOperand(i);
2874 if (Arg.getOpcode() != ISD::UNDEF) {
2875 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2876 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2877 return false;
2878 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2879 return false;
2880 }
2881 }
2882 return true;
2883}
2884
2885/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2886/// constant +0.0.
2887static inline bool isZeroNode(SDOperand Elt) {
2888 return ((isa<ConstantSDNode>(Elt) &&
2889 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2890 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002891 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002892}
2893
2894/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2895/// to an zero vector.
2896static bool isZeroShuffle(SDNode *N) {
2897 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2898 return false;
2899
2900 SDOperand V1 = N->getOperand(0);
2901 SDOperand V2 = N->getOperand(1);
2902 SDOperand Mask = N->getOperand(2);
2903 unsigned NumElems = Mask.getNumOperands();
2904 for (unsigned i = 0; i != NumElems; ++i) {
2905 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002906 if (Arg.getOpcode() == ISD::UNDEF)
2907 continue;
2908
2909 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2910 if (Idx < NumElems) {
2911 unsigned Opc = V1.Val->getOpcode();
2912 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2913 continue;
2914 if (Opc != ISD::BUILD_VECTOR ||
2915 !isZeroNode(V1.Val->getOperand(Idx)))
2916 return false;
2917 } else if (Idx >= NumElems) {
2918 unsigned Opc = V2.Val->getOpcode();
2919 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2920 continue;
2921 if (Opc != ISD::BUILD_VECTOR ||
2922 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2923 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002924 }
2925 }
2926 return true;
2927}
2928
2929/// getZeroVector - Returns a vector of specified type with all zero elements.
2930///
2931static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2932 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002933
2934 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2935 // type. This ensures they get CSE'd.
2936 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2937 SDOperand Vec;
2938 if (MVT::getSizeInBits(VT) == 64) // MMX
2939 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2940 else // SSE
2941 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2942 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943}
2944
Chris Lattnere6aa3862007-11-25 00:24:49 +00002945/// getOnesVector - Returns a vector of specified type with all bits set.
2946///
2947static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2948 assert(MVT::isVector(VT) && "Expected a vector type");
2949
2950 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2951 // type. This ensures they get CSE'd.
2952 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2953 SDOperand Vec;
2954 if (MVT::getSizeInBits(VT) == 64) // MMX
2955 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2956 else // SSE
2957 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2958 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2959}
2960
2961
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002962/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2963/// that point to V2 points to its first element.
2964static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2965 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2966
2967 bool Changed = false;
2968 SmallVector<SDOperand, 8> MaskVec;
2969 unsigned NumElems = Mask.getNumOperands();
2970 for (unsigned i = 0; i != NumElems; ++i) {
2971 SDOperand Arg = Mask.getOperand(i);
2972 if (Arg.getOpcode() != ISD::UNDEF) {
2973 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2974 if (Val > NumElems) {
2975 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2976 Changed = true;
2977 }
2978 }
2979 MaskVec.push_back(Arg);
2980 }
2981
2982 if (Changed)
2983 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2984 &MaskVec[0], MaskVec.size());
2985 return Mask;
2986}
2987
2988/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2989/// operation of specified width.
2990static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2991 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2992 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2993
2994 SmallVector<SDOperand, 8> MaskVec;
2995 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2996 for (unsigned i = 1; i != NumElems; ++i)
2997 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2998 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2999}
3000
3001/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
3002/// of specified width.
3003static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
3004 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3005 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3006 SmallVector<SDOperand, 8> MaskVec;
3007 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
3008 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3009 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
3010 }
3011 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3012}
3013
3014/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3015/// of specified width.
3016static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
3017 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3018 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
3019 unsigned Half = NumElems/2;
3020 SmallVector<SDOperand, 8> MaskVec;
3021 for (unsigned i = 0; i != Half; ++i) {
3022 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3023 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3024 }
3025 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3026}
3027
3028/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
3029///
3030static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
3031 SDOperand V1 = Op.getOperand(0);
3032 SDOperand Mask = Op.getOperand(2);
3033 MVT::ValueType VT = Op.getValueType();
3034 unsigned NumElems = Mask.getNumOperands();
3035 Mask = getUnpacklMask(NumElems, DAG);
3036 while (NumElems != 4) {
3037 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3038 NumElems >>= 1;
3039 }
3040 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3041
Chris Lattnere6aa3862007-11-25 00:24:49 +00003042 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
3044 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
3045 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3046}
3047
3048/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003049/// vector of zero or undef vector. This produces a shuffle where the low
3050/// element of V2 is swizzled into the zero/undef vector, landing at element
3051/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003052static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
3053 unsigned NumElems, unsigned Idx,
3054 bool isZero, SelectionDAG &DAG) {
3055 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
3056 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3057 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003058 SmallVector<SDOperand, 16> MaskVec;
3059 for (unsigned i = 0; i != NumElems; ++i)
3060 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3061 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3062 else
3063 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3065 &MaskVec[0], MaskVec.size());
3066 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3067}
3068
3069/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3070///
3071static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3072 unsigned NumNonZero, unsigned NumZero,
3073 SelectionDAG &DAG, TargetLowering &TLI) {
3074 if (NumNonZero > 8)
3075 return SDOperand();
3076
3077 SDOperand V(0, 0);
3078 bool First = true;
3079 for (unsigned i = 0; i < 16; ++i) {
3080 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3081 if (ThisIsNonZero && First) {
3082 if (NumZero)
3083 V = getZeroVector(MVT::v8i16, DAG);
3084 else
3085 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3086 First = false;
3087 }
3088
3089 if ((i & 1) != 0) {
3090 SDOperand ThisElt(0, 0), LastElt(0, 0);
3091 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3092 if (LastIsNonZero) {
3093 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3094 }
3095 if (ThisIsNonZero) {
3096 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3097 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3098 ThisElt, DAG.getConstant(8, MVT::i8));
3099 if (LastIsNonZero)
3100 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3101 } else
3102 ThisElt = LastElt;
3103
3104 if (ThisElt.Val)
3105 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
3106 DAG.getConstant(i/2, TLI.getPointerTy()));
3107 }
3108 }
3109
3110 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3111}
3112
3113/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3114///
3115static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3116 unsigned NumNonZero, unsigned NumZero,
3117 SelectionDAG &DAG, TargetLowering &TLI) {
3118 if (NumNonZero > 4)
3119 return SDOperand();
3120
3121 SDOperand V(0, 0);
3122 bool First = true;
3123 for (unsigned i = 0; i < 8; ++i) {
3124 bool isNonZero = (NonZeros & (1 << i)) != 0;
3125 if (isNonZero) {
3126 if (First) {
3127 if (NumZero)
3128 V = getZeroVector(MVT::v8i16, DAG);
3129 else
3130 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3131 First = false;
3132 }
3133 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
3134 DAG.getConstant(i, TLI.getPointerTy()));
3135 }
3136 }
3137
3138 return V;
3139}
3140
Evan Cheng75184a92007-12-11 01:46:18 +00003141/// is4WideVector - Returns true if the specific v8i16 or v16i8 vector is
3142/// actually just a 4 wide vector. e.g. <a, a, y, y, d, d, x, x>
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143SDOperand
3144X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003145 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3146 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3147 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3148 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3149 // eliminated on x86-32 hosts.
3150 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3151 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003152
Chris Lattnere6aa3862007-11-25 00:24:49 +00003153 if (ISD::isBuildVectorAllOnes(Op.Val))
3154 return getOnesVector(Op.getValueType(), DAG);
3155 return getZeroVector(Op.getValueType(), DAG);
3156 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157
3158 MVT::ValueType VT = Op.getValueType();
3159 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3160 unsigned EVTBits = MVT::getSizeInBits(EVT);
3161
3162 unsigned NumElems = Op.getNumOperands();
3163 unsigned NumZero = 0;
3164 unsigned NumNonZero = 0;
3165 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00003166 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00003167 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003168 for (unsigned i = 0; i < NumElems; ++i) {
3169 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003170 if (Elt.getOpcode() == ISD::UNDEF)
3171 continue;
3172 Values.insert(Elt);
3173 if (Elt.getOpcode() != ISD::Constant &&
3174 Elt.getOpcode() != ISD::ConstantFP)
3175 HasNonImms = true;
3176 if (isZeroNode(Elt))
3177 NumZero++;
3178 else {
3179 NonZeros |= (1 << i);
3180 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 }
3182 }
3183
3184 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003185 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3186 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003187 }
3188
3189 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3190 if (Values.size() == 1)
3191 return SDOperand();
3192
3193 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00003194 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003195 unsigned Idx = CountTrailingZeros_32(NonZeros);
3196 SDOperand Item = Op.getOperand(Idx);
3197 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3198 if (Idx == 0)
3199 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3200 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3201 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00003202 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
3203 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003204
3205 if (EVTBits == 32) {
3206 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3207 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3208 DAG);
3209 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3210 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3211 SmallVector<SDOperand, 8> MaskVec;
3212 for (unsigned i = 0; i < NumElems; i++)
3213 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3214 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3215 &MaskVec[0], MaskVec.size());
3216 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3217 DAG.getNode(ISD::UNDEF, VT), Mask);
3218 }
3219 }
3220
Dan Gohman21463242007-07-24 22:55:08 +00003221 // A vector full of immediates; various special cases are already
3222 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00003223 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00003224 return SDOperand();
3225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003226 // Let legalizer expand 2-wide build_vectors.
3227 if (EVTBits == 64)
3228 return SDOperand();
3229
3230 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3231 if (EVTBits == 8 && NumElems == 16) {
3232 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3233 *this);
3234 if (V.Val) return V;
3235 }
3236
3237 if (EVTBits == 16 && NumElems == 8) {
3238 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3239 *this);
3240 if (V.Val) return V;
3241 }
3242
3243 // If element VT is == 32 bits, turn it into a number of shuffles.
3244 SmallVector<SDOperand, 8> V;
3245 V.resize(NumElems);
3246 if (NumElems == 4 && NumZero > 0) {
3247 for (unsigned i = 0; i < 4; ++i) {
3248 bool isZero = !(NonZeros & (1 << i));
3249 if (isZero)
3250 V[i] = getZeroVector(VT, DAG);
3251 else
3252 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3253 }
3254
3255 for (unsigned i = 0; i < 2; ++i) {
3256 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3257 default: break;
3258 case 0:
3259 V[i] = V[i*2]; // Must be a zero vector.
3260 break;
3261 case 1:
3262 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3263 getMOVLMask(NumElems, DAG));
3264 break;
3265 case 2:
3266 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3267 getMOVLMask(NumElems, DAG));
3268 break;
3269 case 3:
3270 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3271 getUnpacklMask(NumElems, DAG));
3272 break;
3273 }
3274 }
3275
3276 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3277 // clears the upper bits.
3278 // FIXME: we can do the same for v4f32 case when we know both parts of
3279 // the lower half come from scalar_to_vector (loadf32). We should do
3280 // that in post legalizer dag combiner with target specific hooks.
3281 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3282 return V[0];
3283 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3284 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3285 SmallVector<SDOperand, 8> MaskVec;
3286 bool Reverse = (NonZeros & 0x3) == 2;
3287 for (unsigned i = 0; i < 2; ++i)
3288 if (Reverse)
3289 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3290 else
3291 MaskVec.push_back(DAG.getConstant(i, EVT));
3292 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3293 for (unsigned i = 0; i < 2; ++i)
3294 if (Reverse)
3295 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3296 else
3297 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3298 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3299 &MaskVec[0], MaskVec.size());
3300 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3301 }
3302
3303 if (Values.size() > 2) {
3304 // Expand into a number of unpckl*.
3305 // e.g. for v4f32
3306 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3307 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3308 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3309 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3310 for (unsigned i = 0; i < NumElems; ++i)
3311 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3312 NumElems >>= 1;
3313 while (NumElems != 0) {
3314 for (unsigned i = 0; i < NumElems; ++i)
3315 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3316 UnpckMask);
3317 NumElems >>= 1;
3318 }
3319 return V[0];
3320 }
3321
3322 return SDOperand();
3323}
3324
Evan Chengfca29242007-12-07 08:07:39 +00003325static
3326SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3327 SDOperand PermMask, SelectionDAG &DAG,
3328 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003329 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003330 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3331 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003332 MVT::ValueType PtrVT = TLI.getPointerTy();
3333 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3334 PermMask.Val->op_end());
3335
3336 // First record which half of which vector the low elements come from.
3337 SmallVector<unsigned, 4> LowQuad(4);
3338 for (unsigned i = 0; i < 4; ++i) {
3339 SDOperand Elt = MaskElts[i];
3340 if (Elt.getOpcode() == ISD::UNDEF)
3341 continue;
3342 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3343 int QuadIdx = EltIdx / 4;
3344 ++LowQuad[QuadIdx];
3345 }
3346 int BestLowQuad = -1;
3347 unsigned MaxQuad = 1;
3348 for (unsigned i = 0; i < 4; ++i) {
3349 if (LowQuad[i] > MaxQuad) {
3350 BestLowQuad = i;
3351 MaxQuad = LowQuad[i];
3352 }
Evan Chengfca29242007-12-07 08:07:39 +00003353 }
3354
Evan Cheng75184a92007-12-11 01:46:18 +00003355 // Record which half of which vector the high elements come from.
3356 SmallVector<unsigned, 4> HighQuad(4);
3357 for (unsigned i = 4; i < 8; ++i) {
3358 SDOperand Elt = MaskElts[i];
3359 if (Elt.getOpcode() == ISD::UNDEF)
3360 continue;
3361 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3362 int QuadIdx = EltIdx / 4;
3363 ++HighQuad[QuadIdx];
3364 }
3365 int BestHighQuad = -1;
3366 MaxQuad = 1;
3367 for (unsigned i = 0; i < 4; ++i) {
3368 if (HighQuad[i] > MaxQuad) {
3369 BestHighQuad = i;
3370 MaxQuad = HighQuad[i];
3371 }
3372 }
3373
3374 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3375 if (BestLowQuad != -1 || BestHighQuad != -1) {
3376 // First sort the 4 chunks in order using shufpd.
3377 SmallVector<SDOperand, 8> MaskVec;
3378 if (BestLowQuad != -1)
3379 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3380 else
3381 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3382 if (BestHighQuad != -1)
3383 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3384 else
3385 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3386 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3387 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3388 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3389 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3390 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3391
3392 // Now sort high and low parts separately.
3393 BitVector InOrder(8);
3394 if (BestLowQuad != -1) {
3395 // Sort lower half in order using PSHUFLW.
3396 MaskVec.clear();
3397 bool AnyOutOrder = false;
3398 for (unsigned i = 0; i != 4; ++i) {
3399 SDOperand Elt = MaskElts[i];
3400 if (Elt.getOpcode() == ISD::UNDEF) {
3401 MaskVec.push_back(Elt);
3402 InOrder.set(i);
3403 } else {
3404 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3405 if (EltIdx != i)
3406 AnyOutOrder = true;
3407 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3408 // If this element is in the right place after this shuffle, then
3409 // remember it.
3410 if ((int)(EltIdx / 4) == BestLowQuad)
3411 InOrder.set(i);
3412 }
3413 }
3414 if (AnyOutOrder) {
3415 for (unsigned i = 4; i != 8; ++i)
3416 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3417 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3418 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3419 }
3420 }
3421
3422 if (BestHighQuad != -1) {
3423 // Sort high half in order using PSHUFHW if possible.
3424 MaskVec.clear();
3425 for (unsigned i = 0; i != 4; ++i)
3426 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3427 bool AnyOutOrder = false;
3428 for (unsigned i = 4; i != 8; ++i) {
3429 SDOperand Elt = MaskElts[i];
3430 if (Elt.getOpcode() == ISD::UNDEF) {
3431 MaskVec.push_back(Elt);
3432 InOrder.set(i);
3433 } else {
3434 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3435 if (EltIdx != i)
3436 AnyOutOrder = true;
3437 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3438 // If this element is in the right place after this shuffle, then
3439 // remember it.
3440 if ((int)(EltIdx / 4) == BestHighQuad)
3441 InOrder.set(i);
3442 }
3443 }
3444 if (AnyOutOrder) {
3445 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3446 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3447 }
3448 }
3449
3450 // The other elements are put in the right place using pextrw and pinsrw.
3451 for (unsigned i = 0; i != 8; ++i) {
3452 if (InOrder[i])
3453 continue;
3454 SDOperand Elt = MaskElts[i];
3455 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3456 if (EltIdx == i)
3457 continue;
3458 SDOperand ExtOp = (EltIdx < 8)
3459 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3460 DAG.getConstant(EltIdx, PtrVT))
3461 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3462 DAG.getConstant(EltIdx - 8, PtrVT));
3463 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3464 DAG.getConstant(i, PtrVT));
3465 }
3466 return NewV;
3467 }
3468
3469 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3470 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003471 // First, let's find out how many elements are already in the right order.
3472 unsigned V1InOrder = 0;
3473 unsigned V1FromV1 = 0;
3474 unsigned V2InOrder = 0;
3475 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003476 SmallVector<SDOperand, 8> V1Elts;
3477 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003478 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003479 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003480 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003481 V1Elts.push_back(Elt);
3482 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003483 ++V1InOrder;
3484 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003485 continue;
3486 }
3487 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3488 if (EltIdx == i) {
3489 V1Elts.push_back(Elt);
3490 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3491 ++V1InOrder;
3492 } else if (EltIdx == i+8) {
3493 V1Elts.push_back(Elt);
3494 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3495 ++V2InOrder;
3496 } else if (EltIdx < 8) {
3497 V1Elts.push_back(Elt);
3498 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003499 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003500 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3501 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003502 }
3503 }
3504
3505 if (V2InOrder > V1InOrder) {
3506 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3507 std::swap(V1, V2);
3508 std::swap(V1Elts, V2Elts);
3509 std::swap(V1FromV1, V2FromV2);
3510 }
3511
Evan Cheng75184a92007-12-11 01:46:18 +00003512 if ((V1FromV1 + V1InOrder) != 8) {
3513 // Some elements are from V2.
3514 if (V1FromV1) {
3515 // If there are elements that are from V1 but out of place,
3516 // then first sort them in place
3517 SmallVector<SDOperand, 8> MaskVec;
3518 for (unsigned i = 0; i < 8; ++i) {
3519 SDOperand Elt = V1Elts[i];
3520 if (Elt.getOpcode() == ISD::UNDEF) {
3521 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3522 continue;
3523 }
3524 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3525 if (EltIdx >= 8)
3526 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3527 else
3528 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3529 }
3530 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3531 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003532 }
Evan Cheng75184a92007-12-11 01:46:18 +00003533
3534 NewV = V1;
3535 for (unsigned i = 0; i < 8; ++i) {
3536 SDOperand Elt = V1Elts[i];
3537 if (Elt.getOpcode() == ISD::UNDEF)
3538 continue;
3539 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3540 if (EltIdx < 8)
3541 continue;
3542 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3543 DAG.getConstant(EltIdx - 8, PtrVT));
3544 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3545 DAG.getConstant(i, PtrVT));
3546 }
3547 return NewV;
3548 } else {
3549 // All elements are from V1.
3550 NewV = V1;
3551 for (unsigned i = 0; i < 8; ++i) {
3552 SDOperand Elt = V1Elts[i];
3553 if (Elt.getOpcode() == ISD::UNDEF)
3554 continue;
3555 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3556 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3557 DAG.getConstant(EltIdx, PtrVT));
3558 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3559 DAG.getConstant(i, PtrVT));
3560 }
3561 return NewV;
3562 }
3563}
3564
3565/// RewriteAs4WideShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3566/// ones if possible. This can be done when every pair / quad of shuffle mask
3567/// elements point to elements in the right sequence. e.g.
3568/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3569static
3570SDOperand RewriteAs4WideShuffle(SDOperand V1, SDOperand V2,
3571 SDOperand PermMask, SelectionDAG &DAG,
3572 TargetLowering &TLI) {
3573 unsigned NumElems = PermMask.getNumOperands();
3574 unsigned Scale = NumElems / 4;
3575 SmallVector<SDOperand, 4> MaskVec;
3576 for (unsigned i = 0; i < NumElems; i += Scale) {
3577 unsigned StartIdx = ~0U;
3578 for (unsigned j = 0; j < Scale; ++j) {
3579 SDOperand Elt = PermMask.getOperand(i+j);
3580 if (Elt.getOpcode() == ISD::UNDEF)
3581 continue;
3582 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3583 if (StartIdx == ~0U)
3584 StartIdx = EltIdx - (EltIdx % Scale);
3585 if (EltIdx != StartIdx + j)
3586 return SDOperand();
3587 }
3588 if (StartIdx == ~0U)
3589 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3590 else
3591 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003592 }
3593
Evan Cheng75184a92007-12-11 01:46:18 +00003594 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3595 V2 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V2);
3596 return DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1, V2,
3597 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],4));
Evan Chengfca29242007-12-07 08:07:39 +00003598}
3599
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003600SDOperand
3601X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3602 SDOperand V1 = Op.getOperand(0);
3603 SDOperand V2 = Op.getOperand(1);
3604 SDOperand PermMask = Op.getOperand(2);
3605 MVT::ValueType VT = Op.getValueType();
3606 unsigned NumElems = PermMask.getNumOperands();
3607 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3608 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3609 bool V1IsSplat = false;
3610 bool V2IsSplat = false;
3611
3612 if (isUndefShuffle(Op.Val))
3613 return DAG.getNode(ISD::UNDEF, VT);
3614
3615 if (isZeroShuffle(Op.Val))
3616 return getZeroVector(VT, DAG);
3617
3618 if (isIdentityMask(PermMask.Val))
3619 return V1;
3620 else if (isIdentityMask(PermMask.Val, true))
3621 return V2;
3622
3623 if (isSplatMask(PermMask.Val)) {
3624 if (NumElems <= 4) return Op;
3625 // Promote it to a v4i32 splat.
3626 return PromoteSplat(Op, DAG);
3627 }
3628
3629 if (X86::isMOVLMask(PermMask.Val))
3630 return (V1IsUndef) ? V2 : Op;
3631
3632 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3633 X86::isMOVSLDUPMask(PermMask.Val) ||
3634 X86::isMOVHLPSMask(PermMask.Val) ||
3635 X86::isMOVHPMask(PermMask.Val) ||
3636 X86::isMOVLPMask(PermMask.Val))
3637 return Op;
3638
3639 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3640 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3641 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3642
3643 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003644 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3645 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003646 V1IsSplat = isSplatVector(V1.Val);
3647 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003648
3649 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003650 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3651 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3652 std::swap(V1IsSplat, V2IsSplat);
3653 std::swap(V1IsUndef, V2IsUndef);
3654 Commuted = true;
3655 }
3656
3657 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3658 if (V2IsUndef) return V1;
3659 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3660 if (V2IsSplat) {
3661 // V2 is a splat, so the mask may be malformed. That is, it may point
3662 // to any V2 element. The instruction selectior won't like this. Get
3663 // a corrected mask and commute to form a proper MOVS{S|D}.
3664 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3665 if (NewMask.Val != PermMask.Val)
3666 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3667 }
3668 return Op;
3669 }
3670
3671 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3672 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3673 X86::isUNPCKLMask(PermMask.Val) ||
3674 X86::isUNPCKHMask(PermMask.Val))
3675 return Op;
3676
3677 if (V2IsSplat) {
3678 // Normalize mask so all entries that point to V2 points to its first
3679 // element then try to match unpck{h|l} again. If match, return a
3680 // new vector_shuffle with the corrected mask.
3681 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3682 if (NewMask.Val != PermMask.Val) {
3683 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3684 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3685 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3686 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3687 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3688 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3689 }
3690 }
3691 }
3692
3693 // Normalize the node to match x86 shuffle ops if needed
3694 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3695 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3696
3697 if (Commuted) {
3698 // Commute is back and try unpck* again.
3699 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3700 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3701 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3702 X86::isUNPCKLMask(PermMask.Val) ||
3703 X86::isUNPCKHMask(PermMask.Val))
3704 return Op;
3705 }
3706
3707 // If VT is integer, try PSHUF* first, then SHUFP*.
3708 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003709 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3710 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3711 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3712 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003713 X86::isPSHUFHWMask(PermMask.Val) ||
3714 X86::isPSHUFLWMask(PermMask.Val)) {
3715 if (V2.getOpcode() != ISD::UNDEF)
3716 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3717 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3718 return Op;
3719 }
3720
3721 if (X86::isSHUFPMask(PermMask.Val) &&
3722 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3723 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003724 } else {
3725 // Floating point cases in the other order.
3726 if (X86::isSHUFPMask(PermMask.Val))
3727 return Op;
3728 if (X86::isPSHUFDMask(PermMask.Val) ||
3729 X86::isPSHUFHWMask(PermMask.Val) ||
3730 X86::isPSHUFLWMask(PermMask.Val)) {
3731 if (V2.getOpcode() != ISD::UNDEF)
3732 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3733 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3734 return Op;
3735 }
3736 }
3737
Evan Cheng75184a92007-12-11 01:46:18 +00003738 // If the shuffle can be rewritten as a 4 wide shuffle, then do it!
3739 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3740 SDOperand NewOp = RewriteAs4WideShuffle(V1, V2, PermMask, DAG, *this);
3741 if (NewOp.Val)
3742 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3743 }
Evan Chengfca29242007-12-07 08:07:39 +00003744
Evan Cheng75184a92007-12-11 01:46:18 +00003745 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3746 if (VT == MVT::v8i16) {
3747 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3748 if (NewOp.Val)
3749 return NewOp;
3750 }
3751
3752 // Handle all 4 wide cases with a number of shuffles.
3753 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003754 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003755 MVT::ValueType MaskVT = PermMask.getValueType();
3756 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3757 SmallVector<std::pair<int, int>, 8> Locs;
3758 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003759 SmallVector<SDOperand, 8> Mask1(NumElems,
3760 DAG.getNode(ISD::UNDEF, MaskEVT));
3761 SmallVector<SDOperand, 8> Mask2(NumElems,
3762 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003763 unsigned NumHi = 0;
3764 unsigned NumLo = 0;
3765 // If no more than two elements come from either vector. This can be
3766 // implemented with two shuffles. First shuffle gather the elements.
3767 // The second shuffle, which takes the first shuffle as both of its
3768 // vector operands, put the elements into the right order.
3769 for (unsigned i = 0; i != NumElems; ++i) {
3770 SDOperand Elt = PermMask.getOperand(i);
3771 if (Elt.getOpcode() == ISD::UNDEF) {
3772 Locs[i] = std::make_pair(-1, -1);
3773 } else {
3774 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3775 if (Val < NumElems) {
3776 Locs[i] = std::make_pair(0, NumLo);
3777 Mask1[NumLo] = Elt;
3778 NumLo++;
3779 } else {
3780 Locs[i] = std::make_pair(1, NumHi);
3781 if (2+NumHi < NumElems)
3782 Mask1[2+NumHi] = Elt;
3783 NumHi++;
3784 }
3785 }
3786 }
3787 if (NumLo <= 2 && NumHi <= 2) {
3788 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3789 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3790 &Mask1[0], Mask1.size()));
3791 for (unsigned i = 0; i != NumElems; ++i) {
3792 if (Locs[i].first == -1)
3793 continue;
3794 else {
3795 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3796 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3797 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3798 }
3799 }
3800
3801 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3802 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3803 &Mask2[0], Mask2.size()));
3804 }
3805
3806 // Break it into (shuffle shuffle_hi, shuffle_lo).
3807 Locs.clear();
3808 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3809 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3810 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3811 unsigned MaskIdx = 0;
3812 unsigned LoIdx = 0;
3813 unsigned HiIdx = NumElems/2;
3814 for (unsigned i = 0; i != NumElems; ++i) {
3815 if (i == NumElems/2) {
3816 MaskPtr = &HiMask;
3817 MaskIdx = 1;
3818 LoIdx = 0;
3819 HiIdx = NumElems/2;
3820 }
3821 SDOperand Elt = PermMask.getOperand(i);
3822 if (Elt.getOpcode() == ISD::UNDEF) {
3823 Locs[i] = std::make_pair(-1, -1);
3824 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3825 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3826 (*MaskPtr)[LoIdx] = Elt;
3827 LoIdx++;
3828 } else {
3829 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3830 (*MaskPtr)[HiIdx] = Elt;
3831 HiIdx++;
3832 }
3833 }
3834
3835 SDOperand LoShuffle =
3836 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3837 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3838 &LoMask[0], LoMask.size()));
3839 SDOperand HiShuffle =
3840 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3841 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3842 &HiMask[0], HiMask.size()));
3843 SmallVector<SDOperand, 8> MaskOps;
3844 for (unsigned i = 0; i != NumElems; ++i) {
3845 if (Locs[i].first == -1) {
3846 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3847 } else {
3848 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3849 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3850 }
3851 }
3852 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3853 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3854 &MaskOps[0], MaskOps.size()));
3855 }
3856
3857 return SDOperand();
3858}
3859
3860SDOperand
3861X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3862 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3863 return SDOperand();
3864
3865 MVT::ValueType VT = Op.getValueType();
3866 // TODO: handle v16i8.
3867 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003868 SDOperand Vec = Op.getOperand(0);
3869 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3870 if (Idx == 0)
3871 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3872 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3873 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3874 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 // Transform it so it match pextrw which produces a 32-bit result.
3876 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3877 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3878 Op.getOperand(0), Op.getOperand(1));
3879 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3880 DAG.getValueType(VT));
3881 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3882 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003883 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3884 if (Idx == 0)
3885 return Op;
3886 // SHUFPS the element to the lowest double word, then movss.
3887 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3888 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003889 IdxVec.
3890 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3891 IdxVec.
3892 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3893 IdxVec.
3894 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3895 IdxVec.
3896 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003897 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3898 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003899 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3901 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3902 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3903 DAG.getConstant(0, getPointerTy()));
3904 } else if (MVT::getSizeInBits(VT) == 64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3906 if (Idx == 0)
3907 return Op;
3908
3909 // UNPCKHPD the element to the lowest double word, then movsd.
3910 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3911 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3912 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3913 SmallVector<SDOperand, 8> IdxVec;
3914 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003915 IdxVec.
3916 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3918 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003919 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3921 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3922 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3923 DAG.getConstant(0, getPointerTy()));
3924 }
3925
3926 return SDOperand();
3927}
3928
3929SDOperand
3930X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003931 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003932 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3933 if (EVT == MVT::i8)
3934 return SDOperand();
3935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003936 SDOperand N0 = Op.getOperand(0);
3937 SDOperand N1 = Op.getOperand(1);
3938 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003939
3940 if (MVT::getSizeInBits(EVT) == 16) {
3941 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3942 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003943 if (N1.getValueType() != MVT::i32)
3944 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3945 if (N2.getValueType() != MVT::i32)
3946 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
3947 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948 }
3949
Evan Chenge12a7eb2007-12-12 07:55:34 +00003950 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3951 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3952 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3953 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3954 SmallVector<SDOperand, 4> MaskVec;
3955 for (unsigned i = 0; i < 4; ++i)
3956 MaskVec.push_back(DAG.getConstant((i == Idx) ? i+4 : i, MaskEVT));
3957 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3958 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3959 &MaskVec[0], MaskVec.size()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960}
3961
3962SDOperand
3963X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3964 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3965 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3966}
3967
3968// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3969// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3970// one of the above mentioned nodes. It has to be wrapped because otherwise
3971// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3972// be used to form addressing mode. These wrapped nodes will be selected
3973// into MOV32ri.
3974SDOperand
3975X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3976 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3977 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3978 getPointerTy(),
3979 CP->getAlignment());
3980 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3981 // With PIC, the address is actually $g + Offset.
3982 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3983 !Subtarget->isPICStyleRIPRel()) {
3984 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3985 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3986 Result);
3987 }
3988
3989 return Result;
3990}
3991
3992SDOperand
3993X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3994 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3995 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3996 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3997 // With PIC, the address is actually $g + Offset.
3998 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3999 !Subtarget->isPICStyleRIPRel()) {
4000 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4001 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4002 Result);
4003 }
4004
4005 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4006 // load the value at address GV, not the value of GV itself. This means that
4007 // the GlobalAddress must be in the base or index register of the address, not
4008 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4009 // The same applies for external symbols during PIC codegen
4010 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
4011 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
4012
4013 return Result;
4014}
4015
4016// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4017static SDOperand
4018LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4019 const MVT::ValueType PtrVT) {
4020 SDOperand InFlag;
4021 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4022 DAG.getNode(X86ISD::GlobalBaseReg,
4023 PtrVT), InFlag);
4024 InFlag = Chain.getValue(1);
4025
4026 // emit leal symbol@TLSGD(,%ebx,1), %eax
4027 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4028 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4029 GA->getValueType(0),
4030 GA->getOffset());
4031 SDOperand Ops[] = { Chain, TGA, InFlag };
4032 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4033 InFlag = Result.getValue(2);
4034 Chain = Result.getValue(1);
4035
4036 // call ___tls_get_addr. This function receives its argument in
4037 // the register EAX.
4038 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4039 InFlag = Chain.getValue(1);
4040
4041 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4042 SDOperand Ops1[] = { Chain,
4043 DAG.getTargetExternalSymbol("___tls_get_addr",
4044 PtrVT),
4045 DAG.getRegister(X86::EAX, PtrVT),
4046 DAG.getRegister(X86::EBX, PtrVT),
4047 InFlag };
4048 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4049 InFlag = Chain.getValue(1);
4050
4051 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4052}
4053
4054// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4055// "local exec" model.
4056static SDOperand
4057LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4058 const MVT::ValueType PtrVT) {
4059 // Get the Thread Pointer
4060 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4061 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4062 // exec)
4063 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4064 GA->getValueType(0),
4065 GA->getOffset());
4066 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4067
4068 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
4069 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
4070
4071 // The address of the thread local variable is the add of the thread
4072 // pointer with the offset of the variable.
4073 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4074}
4075
4076SDOperand
4077X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4078 // TODO: implement the "local dynamic" model
4079 // TODO: implement the "initial exec"model for pic executables
4080 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4081 "TLS not implemented for non-ELF and 64-bit targets");
4082 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4083 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4084 // otherwise use the "Local Exec"TLS Model
4085 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4086 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4087 else
4088 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4089}
4090
4091SDOperand
4092X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4093 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4094 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4095 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4096 // With PIC, the address is actually $g + Offset.
4097 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4098 !Subtarget->isPICStyleRIPRel()) {
4099 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4100 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4101 Result);
4102 }
4103
4104 return Result;
4105}
4106
4107SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4108 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4109 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4110 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4111 // With PIC, the address is actually $g + Offset.
4112 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4113 !Subtarget->isPICStyleRIPRel()) {
4114 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4115 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4116 Result);
4117 }
4118
4119 return Result;
4120}
4121
Chris Lattner62814a32007-10-17 06:02:13 +00004122/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4123/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00004125 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4126 "Not an i64 shift!");
4127 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4128 SDOperand ShOpLo = Op.getOperand(0);
4129 SDOperand ShOpHi = Op.getOperand(1);
4130 SDOperand ShAmt = Op.getOperand(2);
4131 SDOperand Tmp1 = isSRA ?
4132 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4133 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134
Chris Lattner62814a32007-10-17 06:02:13 +00004135 SDOperand Tmp2, Tmp3;
4136 if (Op.getOpcode() == ISD::SHL_PARTS) {
4137 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4138 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4139 } else {
4140 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4141 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4142 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004143
Chris Lattner62814a32007-10-17 06:02:13 +00004144 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4145 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4146 DAG.getConstant(32, MVT::i8));
4147 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4148 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149
Chris Lattner62814a32007-10-17 06:02:13 +00004150 SDOperand Hi, Lo;
4151 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4152 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4153 SmallVector<SDOperand, 4> Ops;
4154 if (Op.getOpcode() == ISD::SHL_PARTS) {
4155 Ops.push_back(Tmp2);
4156 Ops.push_back(Tmp3);
4157 Ops.push_back(CC);
4158 Ops.push_back(Cond);
4159 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004161 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004162 Ops.push_back(Tmp3);
4163 Ops.push_back(Tmp1);
4164 Ops.push_back(CC);
4165 Ops.push_back(Cond);
4166 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4167 } else {
4168 Ops.push_back(Tmp2);
4169 Ops.push_back(Tmp3);
4170 Ops.push_back(CC);
4171 Ops.push_back(Cond);
4172 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4173
4174 Ops.clear();
4175 Ops.push_back(Tmp3);
4176 Ops.push_back(Tmp1);
4177 Ops.push_back(CC);
4178 Ops.push_back(Cond);
4179 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4180 }
4181
4182 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4183 Ops.clear();
4184 Ops.push_back(Lo);
4185 Ops.push_back(Hi);
4186 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004187}
4188
4189SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4190 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4191 Op.getOperand(0).getValueType() >= MVT::i16 &&
4192 "Unknown SINT_TO_FP to lower!");
4193
4194 SDOperand Result;
4195 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4196 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4197 MachineFunction &MF = DAG.getMachineFunction();
4198 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4199 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4200 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
4201 StackSlot, NULL, 0);
4202
Dale Johannesen2fc20782007-09-14 22:26:36 +00004203 // These are really Legal; caller falls through into that case.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004204 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
4205 return Result;
4206 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen2fc20782007-09-14 22:26:36 +00004207 return Result;
Dale Johannesen958b08b2007-09-19 23:55:34 +00004208 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
4209 Subtarget->is64Bit())
4210 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00004211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004212 // Build the FILD
4213 SDVTList Tys;
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004214 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
4215 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen2fc20782007-09-14 22:26:36 +00004216 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004217 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4218 else
4219 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4220 SmallVector<SDOperand, 8> Ops;
4221 Ops.push_back(Chain);
4222 Ops.push_back(StackSlot);
4223 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00004224 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004225 Tys, &Ops[0], Ops.size());
4226
Dale Johannesen2fc20782007-09-14 22:26:36 +00004227 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 Chain = Result.getValue(1);
4229 SDOperand InFlag = Result.getValue(2);
4230
4231 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4232 // shouldn't be necessary except that RFP cannot be live across
4233 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4234 MachineFunction &MF = DAG.getMachineFunction();
4235 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4236 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4237 Tys = DAG.getVTList(MVT::Other);
4238 SmallVector<SDOperand, 8> Ops;
4239 Ops.push_back(Chain);
4240 Ops.push_back(Result);
4241 Ops.push_back(StackSlot);
4242 Ops.push_back(DAG.getValueType(Op.getValueType()));
4243 Ops.push_back(InFlag);
4244 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
4245 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
4246 }
4247
4248 return Result;
4249}
4250
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004251std::pair<SDOperand,SDOperand> X86TargetLowering::
4252FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4254 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004255
Dale Johannesen2fc20782007-09-14 22:26:36 +00004256 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004257 if (Op.getValueType() == MVT::i32 &&
4258 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004259 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004260 if (Op.getValueType() == MVT::i32 &&
4261 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004262 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004263 if (Subtarget->is64Bit() &&
4264 Op.getValueType() == MVT::i64 &&
4265 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004266 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004267
Evan Cheng05441e62007-10-15 20:11:21 +00004268 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4269 // stack slot.
4270 MachineFunction &MF = DAG.getMachineFunction();
4271 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4272 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4273 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274 unsigned Opc;
4275 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004276 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4277 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4278 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4279 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004280 }
4281
4282 SDOperand Chain = DAG.getEntryNode();
4283 SDOperand Value = Op.getOperand(0);
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004284 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
4285 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004286 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
4287 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
4288 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4289 SDOperand Ops[] = {
4290 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4291 };
4292 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4293 Chain = Value.getValue(1);
4294 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4295 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4296 }
4297
4298 // Build the FP_TO_INT*_IN_MEM
4299 SDOperand Ops[] = { Chain, Value, StackSlot };
4300 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4301
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004302 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303}
4304
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004305SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004306 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4307 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4308 if (FIST.Val == 0) return SDOperand();
4309
4310 // Load the result.
4311 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4312}
4313
4314SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4315 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4316 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4317 if (FIST.Val == 0) return 0;
4318
4319 // Return an i64 load from the stack slot.
4320 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4321
4322 // Use a MERGE_VALUES node to drop the chain result value.
4323 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4324}
4325
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004326SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4327 MVT::ValueType VT = Op.getValueType();
4328 MVT::ValueType EltVT = VT;
4329 if (MVT::isVector(VT))
4330 EltVT = MVT::getVectorElementType(VT);
4331 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4332 std::vector<Constant*> CV;
4333 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004334 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004335 CV.push_back(C);
4336 CV.push_back(C);
4337 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004338 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339 CV.push_back(C);
4340 CV.push_back(C);
4341 CV.push_back(C);
4342 CV.push_back(C);
4343 }
Dan Gohman11821702007-07-27 17:16:43 +00004344 Constant *C = ConstantVector::get(CV);
4345 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4346 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4347 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004348 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4349}
4350
4351SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4352 MVT::ValueType VT = Op.getValueType();
4353 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004354 unsigned EltNum = 1;
4355 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004357 EltNum = MVT::getVectorNumElements(VT);
4358 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4360 std::vector<Constant*> CV;
4361 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004362 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363 CV.push_back(C);
4364 CV.push_back(C);
4365 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004366 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004367 CV.push_back(C);
4368 CV.push_back(C);
4369 CV.push_back(C);
4370 CV.push_back(C);
4371 }
Dan Gohman11821702007-07-27 17:16:43 +00004372 Constant *C = ConstantVector::get(CV);
4373 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4374 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4375 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004376 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004377 return DAG.getNode(ISD::BIT_CONVERT, VT,
4378 DAG.getNode(ISD::XOR, MVT::v2i64,
4379 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4380 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4381 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004382 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4383 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384}
4385
4386SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4387 SDOperand Op0 = Op.getOperand(0);
4388 SDOperand Op1 = Op.getOperand(1);
4389 MVT::ValueType VT = Op.getValueType();
4390 MVT::ValueType SrcVT = Op1.getValueType();
4391 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4392
4393 // If second operand is smaller, extend it first.
4394 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4395 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4396 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004397 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004399 // And if it is bigger, shrink it first.
4400 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4401 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1);
4402 SrcVT = VT;
4403 SrcTy = MVT::getTypeForValueType(SrcVT);
4404 }
4405
4406 // At this point the operands and the result should have the same
4407 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408
4409 // First get the sign bit of second operand.
4410 std::vector<Constant*> CV;
4411 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004412 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4413 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004415 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4416 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4417 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4418 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004419 }
Dan Gohman11821702007-07-27 17:16:43 +00004420 Constant *C = ConstantVector::get(CV);
4421 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4422 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4423 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4425
4426 // Shift sign bit right or left if the two operands have different types.
4427 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4428 // Op0 is MVT::f32, Op1 is MVT::f64.
4429 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4430 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4431 DAG.getConstant(32, MVT::i32));
4432 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4433 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4434 DAG.getConstant(0, getPointerTy()));
4435 }
4436
4437 // Clear first operand sign bit.
4438 CV.clear();
4439 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004440 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4441 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004443 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4444 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4445 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4446 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447 }
Dan Gohman11821702007-07-27 17:16:43 +00004448 C = ConstantVector::get(CV);
4449 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4450 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4451 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4453
4454 // Or the value with the sign bit.
4455 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4456}
4457
Evan Cheng621216e2007-09-29 00:00:36 +00004458SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004459 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004460 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004461 SDOperand Op0 = Op.getOperand(0);
4462 SDOperand Op1 = Op.getOperand(1);
4463 SDOperand CC = Op.getOperand(2);
4464 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4465 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4466 unsigned X86CC;
4467
Evan Cheng950aac02007-09-25 01:57:46 +00004468 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004469 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004470 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4471 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004472 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004473 }
Evan Cheng950aac02007-09-25 01:57:46 +00004474
4475 assert(isFP && "Illegal integer SetCC!");
4476
Evan Cheng621216e2007-09-29 00:00:36 +00004477 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004478 switch (SetCCOpcode) {
4479 default: assert(false && "Illegal floating point SetCC!");
4480 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004481 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004482 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004483 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004484 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4485 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4486 }
4487 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004488 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004489 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004490 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004491 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4492 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4493 }
4494 }
4495}
4496
4497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4499 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500 SDOperand Cond = Op.getOperand(0);
4501 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502
4503 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004504 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505
Evan Cheng50d37ab2007-10-08 22:16:29 +00004506 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4507 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508 if (Cond.getOpcode() == X86ISD::SETCC) {
4509 CC = Cond.getOperand(0);
4510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 SDOperand Cmp = Cond.getOperand(1);
4512 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004513 MVT::ValueType VT = Op.getValueType();
4514 bool IllegalFPCMov = false;
4515 if (VT == MVT::f32 && !X86ScalarSSEf32)
4516 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4517 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4518 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Dale Johannesen3b955db2007-10-16 18:09:08 +00004519 else if (VT == MVT::f80)
4520 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng621216e2007-09-29 00:00:36 +00004521 if ((Opc == X86ISD::CMP ||
4522 Opc == X86ISD::COMI ||
4523 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004524 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004525 addTest = false;
4526 }
4527 }
4528
4529 if (addTest) {
4530 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004531 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004532 }
4533
4534 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4535 MVT::Flag);
4536 SmallVector<SDOperand, 4> Ops;
4537 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4538 // condition is true.
4539 Ops.push_back(Op.getOperand(2));
4540 Ops.push_back(Op.getOperand(1));
4541 Ops.push_back(CC);
4542 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004543 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004544}
4545
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4547 bool addTest = true;
4548 SDOperand Chain = Op.getOperand(0);
4549 SDOperand Cond = Op.getOperand(1);
4550 SDOperand Dest = Op.getOperand(2);
4551 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552
4553 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004554 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004555
Evan Cheng50d37ab2007-10-08 22:16:29 +00004556 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4557 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558 if (Cond.getOpcode() == X86ISD::SETCC) {
4559 CC = Cond.getOperand(0);
4560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 SDOperand Cmp = Cond.getOperand(1);
4562 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004563 if (Opc == X86ISD::CMP ||
4564 Opc == X86ISD::COMI ||
4565 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004566 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004567 addTest = false;
4568 }
4569 }
4570
4571 if (addTest) {
4572 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004573 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004574 }
Evan Cheng621216e2007-09-29 00:00:36 +00004575 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004576 Chain, Op.getOperand(2), CC, Cond);
4577}
4578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004580 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4581 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004583 if (Subtarget->is64Bit())
4584 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4585 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4586 else
4587 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004588 else
4589 switch (CallingConv) {
4590 default:
4591 assert(0 && "Unsupported calling convention");
4592 case CallingConv::Fast:
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004593 if (isTailCall && PerformTailCallOpt)
4594 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4595 else
4596 return LowerCCCCallTo(Op,DAG, CallingConv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004597 case CallingConv::C:
4598 case CallingConv::X86_StdCall:
4599 return LowerCCCCallTo(Op, DAG, CallingConv);
4600 case CallingConv::X86_FastCall:
4601 return LowerFastCCCallTo(Op, DAG, CallingConv);
4602 }
4603}
4604
4605
4606// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4607// Calls to _alloca is needed to probe the stack when allocating more than 4k
4608// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4609// that the guard pages used by the OS virtual memory manager are allocated in
4610// correct sequence.
4611SDOperand
4612X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4613 SelectionDAG &DAG) {
4614 assert(Subtarget->isTargetCygMing() &&
4615 "This should be used only on Cygwin/Mingw targets");
4616
4617 // Get the inputs.
4618 SDOperand Chain = Op.getOperand(0);
4619 SDOperand Size = Op.getOperand(1);
4620 // FIXME: Ensure alignment here
4621
4622 SDOperand Flag;
4623
4624 MVT::ValueType IntPtr = getPointerTy();
4625 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
4626
4627 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4628 Flag = Chain.getValue(1);
4629
4630 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4631 SDOperand Ops[] = { Chain,
4632 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4633 DAG.getRegister(X86::EAX, IntPtr),
4634 Flag };
4635 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4636 Flag = Chain.getValue(1);
4637
4638 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4639
4640 std::vector<MVT::ValueType> Tys;
4641 Tys.push_back(SPTy);
4642 Tys.push_back(MVT::Other);
4643 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4644 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4645}
4646
4647SDOperand
4648X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
4649 MachineFunction &MF = DAG.getMachineFunction();
4650 const Function* Fn = MF.getFunction();
4651 if (Fn->hasExternalLinkage() &&
4652 Subtarget->isTargetCygMing() &&
4653 Fn->getName() == "main")
4654 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
4655
4656 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4657 if (Subtarget->is64Bit())
4658 return LowerX86_64CCCArguments(Op, DAG);
4659 else
4660 switch(CC) {
4661 default:
4662 assert(0 && "Unsupported calling convention");
4663 case CallingConv::Fast:
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004664 return LowerCCCArguments(Op,DAG, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665 // Falls through
4666 case CallingConv::C:
4667 return LowerCCCArguments(Op, DAG);
4668 case CallingConv::X86_StdCall:
4669 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
4670 return LowerCCCArguments(Op, DAG, true);
4671 case CallingConv::X86_FastCall:
4672 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
4673 return LowerFastCCArguments(Op, DAG);
4674 }
4675}
4676
4677SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4678 SDOperand InFlag(0, 0);
4679 SDOperand Chain = Op.getOperand(0);
4680 unsigned Align =
4681 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4682 if (Align == 0) Align = 1;
4683
4684 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004685 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004686 // The libc version is likely to be faster for these cases. It can use the
4687 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004689 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004690 MVT::ValueType IntPtr = getPointerTy();
4691 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4692 TargetLowering::ArgListTy Args;
4693 TargetLowering::ArgListEntry Entry;
4694 Entry.Node = Op.getOperand(1);
4695 Entry.Ty = IntPtrTy;
4696 Args.push_back(Entry);
4697 // Extend the unsigned i8 argument to be an int value for the call.
4698 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4699 Entry.Ty = IntPtrTy;
4700 Args.push_back(Entry);
4701 Entry.Node = Op.getOperand(3);
4702 Args.push_back(Entry);
4703 std::pair<SDOperand,SDOperand> CallResult =
4704 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4705 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4706 return CallResult.second;
4707 }
4708
4709 MVT::ValueType AVT;
4710 SDOperand Count;
4711 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4712 unsigned BytesLeft = 0;
4713 bool TwoRepStos = false;
4714 if (ValC) {
4715 unsigned ValReg;
4716 uint64_t Val = ValC->getValue() & 255;
4717
4718 // If the value is a constant, then we can potentially use larger sets.
4719 switch (Align & 3) {
4720 case 2: // WORD aligned
4721 AVT = MVT::i16;
4722 ValReg = X86::AX;
4723 Val = (Val << 8) | Val;
4724 break;
4725 case 0: // DWORD aligned
4726 AVT = MVT::i32;
4727 ValReg = X86::EAX;
4728 Val = (Val << 8) | Val;
4729 Val = (Val << 16) | Val;
4730 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4731 AVT = MVT::i64;
4732 ValReg = X86::RAX;
4733 Val = (Val << 32) | Val;
4734 }
4735 break;
4736 default: // Byte aligned
4737 AVT = MVT::i8;
4738 ValReg = X86::AL;
4739 Count = Op.getOperand(3);
4740 break;
4741 }
4742
4743 if (AVT > MVT::i8) {
4744 if (I) {
4745 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4746 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4747 BytesLeft = I->getValue() % UBytes;
4748 } else {
4749 assert(AVT >= MVT::i32 &&
4750 "Do not use rep;stos if not at least DWORD aligned");
4751 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4752 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4753 TwoRepStos = true;
4754 }
4755 }
4756
4757 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4758 InFlag);
4759 InFlag = Chain.getValue(1);
4760 } else {
4761 AVT = MVT::i8;
4762 Count = Op.getOperand(3);
4763 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4764 InFlag = Chain.getValue(1);
4765 }
4766
4767 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4768 Count, InFlag);
4769 InFlag = Chain.getValue(1);
4770 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4771 Op.getOperand(1), InFlag);
4772 InFlag = Chain.getValue(1);
4773
4774 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4775 SmallVector<SDOperand, 8> Ops;
4776 Ops.push_back(Chain);
4777 Ops.push_back(DAG.getValueType(AVT));
4778 Ops.push_back(InFlag);
4779 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4780
4781 if (TwoRepStos) {
4782 InFlag = Chain.getValue(1);
4783 Count = Op.getOperand(3);
4784 MVT::ValueType CVT = Count.getValueType();
4785 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4786 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4787 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4788 Left, InFlag);
4789 InFlag = Chain.getValue(1);
4790 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4791 Ops.clear();
4792 Ops.push_back(Chain);
4793 Ops.push_back(DAG.getValueType(MVT::i8));
4794 Ops.push_back(InFlag);
4795 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4796 } else if (BytesLeft) {
4797 // Issue stores for the last 1 - 7 bytes.
4798 SDOperand Value;
4799 unsigned Val = ValC->getValue() & 255;
4800 unsigned Offset = I->getValue() - BytesLeft;
4801 SDOperand DstAddr = Op.getOperand(1);
4802 MVT::ValueType AddrVT = DstAddr.getValueType();
4803 if (BytesLeft >= 4) {
4804 Val = (Val << 8) | Val;
4805 Val = (Val << 16) | Val;
4806 Value = DAG.getConstant(Val, MVT::i32);
4807 Chain = DAG.getStore(Chain, Value,
4808 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4809 DAG.getConstant(Offset, AddrVT)),
4810 NULL, 0);
4811 BytesLeft -= 4;
4812 Offset += 4;
4813 }
4814 if (BytesLeft >= 2) {
4815 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4816 Chain = DAG.getStore(Chain, Value,
4817 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4818 DAG.getConstant(Offset, AddrVT)),
4819 NULL, 0);
4820 BytesLeft -= 2;
4821 Offset += 2;
4822 }
4823 if (BytesLeft == 1) {
4824 Value = DAG.getConstant(Val, MVT::i8);
4825 Chain = DAG.getStore(Chain, Value,
4826 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4827 DAG.getConstant(Offset, AddrVT)),
4828 NULL, 0);
4829 }
4830 }
4831
4832 return Chain;
4833}
4834
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004835SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4836 SDOperand Dest,
4837 SDOperand Source,
4838 unsigned Size,
4839 unsigned Align,
4840 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004842 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843 switch (Align & 3) {
4844 case 2: // WORD aligned
4845 AVT = MVT::i16;
4846 break;
4847 case 0: // DWORD aligned
4848 AVT = MVT::i32;
4849 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4850 AVT = MVT::i64;
4851 break;
4852 default: // Byte aligned
4853 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004854 break;
4855 }
4856
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004857 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4858 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4859 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004860
4861 SDOperand InFlag(0, 0);
4862 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4863 Count, InFlag);
4864 InFlag = Chain.getValue(1);
4865 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004866 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004867 InFlag = Chain.getValue(1);
4868 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004869 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870 InFlag = Chain.getValue(1);
4871
4872 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4873 SmallVector<SDOperand, 8> Ops;
4874 Ops.push_back(Chain);
4875 Ops.push_back(DAG.getValueType(AVT));
4876 Ops.push_back(InFlag);
4877 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4878
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004879 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004881 unsigned Offset = Size - BytesLeft;
4882 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004884 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004885 MVT::ValueType SrcVT = SrcAddr.getValueType();
4886 SDOperand Value;
4887 if (BytesLeft >= 4) {
4888 Value = DAG.getLoad(MVT::i32, Chain,
4889 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4890 DAG.getConstant(Offset, SrcVT)),
4891 NULL, 0);
4892 Chain = Value.getValue(1);
4893 Chain = DAG.getStore(Chain, Value,
4894 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4895 DAG.getConstant(Offset, DstVT)),
4896 NULL, 0);
4897 BytesLeft -= 4;
4898 Offset += 4;
4899 }
4900 if (BytesLeft >= 2) {
4901 Value = DAG.getLoad(MVT::i16, Chain,
4902 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4903 DAG.getConstant(Offset, SrcVT)),
4904 NULL, 0);
4905 Chain = Value.getValue(1);
4906 Chain = DAG.getStore(Chain, Value,
4907 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4908 DAG.getConstant(Offset, DstVT)),
4909 NULL, 0);
4910 BytesLeft -= 2;
4911 Offset += 2;
4912 }
4913
4914 if (BytesLeft == 1) {
4915 Value = DAG.getLoad(MVT::i8, Chain,
4916 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4917 DAG.getConstant(Offset, SrcVT)),
4918 NULL, 0);
4919 Chain = Value.getValue(1);
4920 Chain = DAG.getStore(Chain, Value,
4921 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4922 DAG.getConstant(Offset, DstVT)),
4923 NULL, 0);
4924 }
4925 }
4926
4927 return Chain;
4928}
4929
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004930/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4931SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004932 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004933 SDOperand TheChain = N->getOperand(0);
4934 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004936 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4937 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4938 MVT::i64, rax.getValue(2));
4939 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 DAG.getConstant(32, MVT::i8));
4941 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004942 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004943 };
4944
4945 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004946 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947 }
4948
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004949 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4950 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4951 MVT::i32, eax.getValue(2));
4952 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4953 SDOperand Ops[] = { eax, edx };
4954 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4955
4956 // Use a MERGE_VALUES to return the value and chain.
4957 Ops[1] = edx.getValue(1);
4958 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4959 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960}
4961
4962SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4963 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4964
4965 if (!Subtarget->is64Bit()) {
4966 // vastart just stores the address of the VarArgsFrameIndex slot into the
4967 // memory location argument.
4968 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4969 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4970 SV->getOffset());
4971 }
4972
4973 // __va_list_tag:
4974 // gp_offset (0 - 6 * 8)
4975 // fp_offset (48 - 48 + 8 * 16)
4976 // overflow_arg_area (point to parameters coming in memory).
4977 // reg_save_area
4978 SmallVector<SDOperand, 8> MemOps;
4979 SDOperand FIN = Op.getOperand(1);
4980 // Store gp_offset
4981 SDOperand Store = DAG.getStore(Op.getOperand(0),
4982 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4983 FIN, SV->getValue(), SV->getOffset());
4984 MemOps.push_back(Store);
4985
4986 // Store fp_offset
4987 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4988 DAG.getConstant(4, getPointerTy()));
4989 Store = DAG.getStore(Op.getOperand(0),
4990 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4991 FIN, SV->getValue(), SV->getOffset());
4992 MemOps.push_back(Store);
4993
4994 // Store ptr to overflow_arg_area
4995 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4996 DAG.getConstant(4, getPointerTy()));
4997 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4998 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4999 SV->getOffset());
5000 MemOps.push_back(Store);
5001
5002 // Store ptr to reg_save_area.
5003 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5004 DAG.getConstant(8, getPointerTy()));
5005 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
5006 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
5007 SV->getOffset());
5008 MemOps.push_back(Store);
5009 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5010}
5011
5012SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5013 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5014 SDOperand Chain = Op.getOperand(0);
5015 SDOperand DstPtr = Op.getOperand(1);
5016 SDOperand SrcPtr = Op.getOperand(2);
5017 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
5018 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
5019
5020 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
5021 SrcSV->getValue(), SrcSV->getOffset());
5022 Chain = SrcPtr.getValue(1);
5023 for (unsigned i = 0; i < 3; ++i) {
5024 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
5025 SrcSV->getValue(), SrcSV->getOffset());
5026 Chain = Val.getValue(1);
5027 Chain = DAG.getStore(Chain, Val, DstPtr,
5028 DstSV->getValue(), DstSV->getOffset());
5029 if (i == 2)
5030 break;
5031 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
5032 DAG.getConstant(8, getPointerTy()));
5033 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
5034 DAG.getConstant(8, getPointerTy()));
5035 }
5036 return Chain;
5037}
5038
5039SDOperand
5040X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5041 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5042 switch (IntNo) {
5043 default: return SDOperand(); // Don't custom lower most intrinsics.
5044 // Comparison intrinsics.
5045 case Intrinsic::x86_sse_comieq_ss:
5046 case Intrinsic::x86_sse_comilt_ss:
5047 case Intrinsic::x86_sse_comile_ss:
5048 case Intrinsic::x86_sse_comigt_ss:
5049 case Intrinsic::x86_sse_comige_ss:
5050 case Intrinsic::x86_sse_comineq_ss:
5051 case Intrinsic::x86_sse_ucomieq_ss:
5052 case Intrinsic::x86_sse_ucomilt_ss:
5053 case Intrinsic::x86_sse_ucomile_ss:
5054 case Intrinsic::x86_sse_ucomigt_ss:
5055 case Intrinsic::x86_sse_ucomige_ss:
5056 case Intrinsic::x86_sse_ucomineq_ss:
5057 case Intrinsic::x86_sse2_comieq_sd:
5058 case Intrinsic::x86_sse2_comilt_sd:
5059 case Intrinsic::x86_sse2_comile_sd:
5060 case Intrinsic::x86_sse2_comigt_sd:
5061 case Intrinsic::x86_sse2_comige_sd:
5062 case Intrinsic::x86_sse2_comineq_sd:
5063 case Intrinsic::x86_sse2_ucomieq_sd:
5064 case Intrinsic::x86_sse2_ucomilt_sd:
5065 case Intrinsic::x86_sse2_ucomile_sd:
5066 case Intrinsic::x86_sse2_ucomigt_sd:
5067 case Intrinsic::x86_sse2_ucomige_sd:
5068 case Intrinsic::x86_sse2_ucomineq_sd: {
5069 unsigned Opc = 0;
5070 ISD::CondCode CC = ISD::SETCC_INVALID;
5071 switch (IntNo) {
5072 default: break;
5073 case Intrinsic::x86_sse_comieq_ss:
5074 case Intrinsic::x86_sse2_comieq_sd:
5075 Opc = X86ISD::COMI;
5076 CC = ISD::SETEQ;
5077 break;
5078 case Intrinsic::x86_sse_comilt_ss:
5079 case Intrinsic::x86_sse2_comilt_sd:
5080 Opc = X86ISD::COMI;
5081 CC = ISD::SETLT;
5082 break;
5083 case Intrinsic::x86_sse_comile_ss:
5084 case Intrinsic::x86_sse2_comile_sd:
5085 Opc = X86ISD::COMI;
5086 CC = ISD::SETLE;
5087 break;
5088 case Intrinsic::x86_sse_comigt_ss:
5089 case Intrinsic::x86_sse2_comigt_sd:
5090 Opc = X86ISD::COMI;
5091 CC = ISD::SETGT;
5092 break;
5093 case Intrinsic::x86_sse_comige_ss:
5094 case Intrinsic::x86_sse2_comige_sd:
5095 Opc = X86ISD::COMI;
5096 CC = ISD::SETGE;
5097 break;
5098 case Intrinsic::x86_sse_comineq_ss:
5099 case Intrinsic::x86_sse2_comineq_sd:
5100 Opc = X86ISD::COMI;
5101 CC = ISD::SETNE;
5102 break;
5103 case Intrinsic::x86_sse_ucomieq_ss:
5104 case Intrinsic::x86_sse2_ucomieq_sd:
5105 Opc = X86ISD::UCOMI;
5106 CC = ISD::SETEQ;
5107 break;
5108 case Intrinsic::x86_sse_ucomilt_ss:
5109 case Intrinsic::x86_sse2_ucomilt_sd:
5110 Opc = X86ISD::UCOMI;
5111 CC = ISD::SETLT;
5112 break;
5113 case Intrinsic::x86_sse_ucomile_ss:
5114 case Intrinsic::x86_sse2_ucomile_sd:
5115 Opc = X86ISD::UCOMI;
5116 CC = ISD::SETLE;
5117 break;
5118 case Intrinsic::x86_sse_ucomigt_ss:
5119 case Intrinsic::x86_sse2_ucomigt_sd:
5120 Opc = X86ISD::UCOMI;
5121 CC = ISD::SETGT;
5122 break;
5123 case Intrinsic::x86_sse_ucomige_ss:
5124 case Intrinsic::x86_sse2_ucomige_sd:
5125 Opc = X86ISD::UCOMI;
5126 CC = ISD::SETGE;
5127 break;
5128 case Intrinsic::x86_sse_ucomineq_ss:
5129 case Intrinsic::x86_sse2_ucomineq_sd:
5130 Opc = X86ISD::UCOMI;
5131 CC = ISD::SETNE;
5132 break;
5133 }
5134
5135 unsigned X86CC;
5136 SDOperand LHS = Op.getOperand(1);
5137 SDOperand RHS = Op.getOperand(2);
5138 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5139
Evan Cheng621216e2007-09-29 00:00:36 +00005140 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5141 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5142 DAG.getConstant(X86CC, MVT::i8), Cond);
5143 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005144 }
5145 }
5146}
5147
5148SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5149 // Depths > 0 not supported yet!
5150 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5151 return SDOperand();
5152
5153 // Just load the return address
5154 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5155 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5156}
5157
5158SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5159 // Depths > 0 not supported yet!
5160 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5161 return SDOperand();
5162
5163 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5164 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
5165 DAG.getConstant(4, getPointerTy()));
5166}
5167
5168SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5169 SelectionDAG &DAG) {
5170 // Is not yet supported on x86-64
5171 if (Subtarget->is64Bit())
5172 return SDOperand();
5173
5174 return DAG.getConstant(8, getPointerTy());
5175}
5176
5177SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5178{
5179 assert(!Subtarget->is64Bit() &&
5180 "Lowering of eh_return builtin is not supported yet on x86-64");
5181
5182 MachineFunction &MF = DAG.getMachineFunction();
5183 SDOperand Chain = Op.getOperand(0);
5184 SDOperand Offset = Op.getOperand(1);
5185 SDOperand Handler = Op.getOperand(2);
5186
5187 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5188 getPointerTy());
5189
5190 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
5191 DAG.getConstant(-4UL, getPointerTy()));
5192 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5193 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5194 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
5195 MF.addLiveOut(X86::ECX);
5196
5197 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5198 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5199}
5200
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005201SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5202 SelectionDAG &DAG) {
5203 SDOperand Root = Op.getOperand(0);
5204 SDOperand Trmp = Op.getOperand(1); // trampoline
5205 SDOperand FPtr = Op.getOperand(2); // nested function
5206 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5207
5208 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
5209
5210 if (Subtarget->is64Bit()) {
5211 return SDOperand(); // not yet supported
5212 } else {
5213 Function *Func = (Function *)
5214 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5215 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005216 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005217
5218 switch (CC) {
5219 default:
5220 assert(0 && "Unsupported calling convention");
5221 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005222 case CallingConv::X86_StdCall: {
5223 // Pass 'nest' parameter in ECX.
5224 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005225 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005226
5227 // Check that ECX wasn't needed by an 'inreg' parameter.
5228 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005229 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005230
5231 if (Attrs && !Func->isVarArg()) {
5232 unsigned InRegCount = 0;
5233 unsigned Idx = 1;
5234
5235 for (FunctionType::param_iterator I = FTy->param_begin(),
5236 E = FTy->param_end(); I != E; ++I, ++Idx)
5237 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5238 // FIXME: should only count parameters that are lowered to integers.
5239 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5240
5241 if (InRegCount > 2) {
5242 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5243 abort();
5244 }
5245 }
5246 break;
5247 }
5248 case CallingConv::X86_FastCall:
5249 // Pass 'nest' parameter in EAX.
5250 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005251 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005252 break;
5253 }
5254
Duncan Sands466eadd2007-08-29 19:01:20 +00005255 const X86InstrInfo *TII =
5256 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5257
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005258 SDOperand OutChains[4];
5259 SDOperand Addr, Disp;
5260
5261 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5262 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5263
Duncan Sands466eadd2007-08-29 19:01:20 +00005264 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5265 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
5266 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005267 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
5268
5269 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5270 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
5271 TrmpSV->getOffset() + 1, false, 1);
5272
Duncan Sands466eadd2007-08-29 19:01:20 +00005273 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005274 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5275 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5276 TrmpSV->getValue() + 5, TrmpSV->getOffset());
5277
5278 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5279 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
5280 TrmpSV->getOffset() + 6, false, 1);
5281
Duncan Sands7407a9f2007-09-11 14:10:23 +00005282 SDOperand Ops[] =
5283 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5284 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005285 }
5286}
5287
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005288SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
5289 /*
5290 The rounding mode is in bits 11:10 of FPSR, and has the following
5291 settings:
5292 00 Round to nearest
5293 01 Round to -inf
5294 10 Round to +inf
5295 11 Round to 0
5296
5297 FLT_ROUNDS, on the other hand, expects the following:
5298 -1 Undefined
5299 0 Round to 0
5300 1 Round to nearest
5301 2 Round to +inf
5302 3 Round to -inf
5303
5304 To perform the conversion, we do:
5305 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5306 */
5307
5308 MachineFunction &MF = DAG.getMachineFunction();
5309 const TargetMachine &TM = MF.getTarget();
5310 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5311 unsigned StackAlignment = TFI.getStackAlignment();
5312 MVT::ValueType VT = Op.getValueType();
5313
5314 // Save FP Control Word to stack slot
5315 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5316 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5317
5318 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5319 DAG.getEntryNode(), StackSlot);
5320
5321 // Load FP Control Word from stack slot
5322 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5323
5324 // Transform as necessary
5325 SDOperand CWD1 =
5326 DAG.getNode(ISD::SRL, MVT::i16,
5327 DAG.getNode(ISD::AND, MVT::i16,
5328 CWD, DAG.getConstant(0x800, MVT::i16)),
5329 DAG.getConstant(11, MVT::i8));
5330 SDOperand CWD2 =
5331 DAG.getNode(ISD::SRL, MVT::i16,
5332 DAG.getNode(ISD::AND, MVT::i16,
5333 CWD, DAG.getConstant(0x400, MVT::i16)),
5334 DAG.getConstant(9, MVT::i8));
5335
5336 SDOperand RetVal =
5337 DAG.getNode(ISD::AND, MVT::i16,
5338 DAG.getNode(ISD::ADD, MVT::i16,
5339 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5340 DAG.getConstant(1, MVT::i16)),
5341 DAG.getConstant(3, MVT::i16));
5342
5343
5344 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5345 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5346}
5347
Evan Cheng48679f42007-12-14 02:13:44 +00005348SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5349 MVT::ValueType VT = Op.getValueType();
5350 MVT::ValueType OpVT = VT;
5351 unsigned NumBits = MVT::getSizeInBits(VT);
5352
5353 Op = Op.getOperand(0);
5354 if (VT == MVT::i8) {
5355 OpVT = MVT::i32;
5356 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5357 }
5358 if (VT == MVT::i32 || VT == MVT::i64)
5359 return DAG.getNode(ISD::XOR, OpVT, DAG.getNode(X86ISD::BSR, OpVT, Op),
5360 DAG.getConstant(NumBits-1, OpVT));
5361
5362 Op = DAG.getNode(ISD::SUB, OpVT, DAG.getConstant(NumBits-1, OpVT),
5363 DAG.getNode(X86ISD::BSR, OpVT, Op));
5364 if (VT == MVT::i8)
5365 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5366 return Op;
5367}
5368
5369SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5370 MVT::ValueType VT = Op.getValueType();
5371 MVT::ValueType OpVT = VT;
5372
5373 Op = Op.getOperand(0);
5374 if (VT == MVT::i8) {
5375 OpVT = MVT::i32;
5376 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5377 }
5378 Op = DAG.getNode(X86ISD::BSF, OpVT, Op);
5379 if (VT == MVT::i8)
5380 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5381 return Op;
5382}
5383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384/// LowerOperation - Provide custom lowering hooks for some operations.
5385///
5386SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5387 switch (Op.getOpcode()) {
5388 default: assert(0 && "Should not custom lower this!");
5389 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5390 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5391 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5392 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5393 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5394 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5395 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5396 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5397 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5398 case ISD::SHL_PARTS:
5399 case ISD::SRA_PARTS:
5400 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5401 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5402 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5403 case ISD::FABS: return LowerFABS(Op, DAG);
5404 case ISD::FNEG: return LowerFNEG(Op, DAG);
5405 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005406 case ISD::SETCC: return LowerSETCC(Op, DAG);
5407 case ISD::SELECT: return LowerSELECT(Op, DAG);
5408 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5410 case ISD::CALL: return LowerCALL(Op, DAG);
5411 case ISD::RET: return LowerRET(Op, DAG);
5412 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5413 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5414 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005415 case ISD::VASTART: return LowerVASTART(Op, DAG);
5416 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5417 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5418 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5419 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5420 case ISD::FRAME_TO_ARGS_OFFSET:
5421 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5422 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5423 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005424 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005425 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005426 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5427 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005428
5429 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5430 case ISD::READCYCLECOUNTER:
5431 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005432 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005433}
5434
5435/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5436SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5437 switch (N->getOpcode()) {
5438 default: assert(0 && "Should not custom lower this!");
5439 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5440 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5441 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005442}
5443
5444const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5445 switch (Opcode) {
5446 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005447 case X86ISD::BSF: return "X86ISD::BSF";
5448 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449 case X86ISD::SHLD: return "X86ISD::SHLD";
5450 case X86ISD::SHRD: return "X86ISD::SHRD";
5451 case X86ISD::FAND: return "X86ISD::FAND";
5452 case X86ISD::FOR: return "X86ISD::FOR";
5453 case X86ISD::FXOR: return "X86ISD::FXOR";
5454 case X86ISD::FSRL: return "X86ISD::FSRL";
5455 case X86ISD::FILD: return "X86ISD::FILD";
5456 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5457 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5458 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5459 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5460 case X86ISD::FLD: return "X86ISD::FLD";
5461 case X86ISD::FST: return "X86ISD::FST";
5462 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
5463 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5464 case X86ISD::CALL: return "X86ISD::CALL";
5465 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5466 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5467 case X86ISD::CMP: return "X86ISD::CMP";
5468 case X86ISD::COMI: return "X86ISD::COMI";
5469 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5470 case X86ISD::SETCC: return "X86ISD::SETCC";
5471 case X86ISD::CMOV: return "X86ISD::CMOV";
5472 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5473 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5474 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5475 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5477 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5478 case X86ISD::S2VEC: return "X86ISD::S2VEC";
5479 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5480 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5481 case X86ISD::FMAX: return "X86ISD::FMAX";
5482 case X86ISD::FMIN: return "X86ISD::FMIN";
5483 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5484 case X86ISD::FRCP: return "X86ISD::FRCP";
5485 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5486 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5487 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005488 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005489 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005490 }
5491}
5492
5493// isLegalAddressingMode - Return true if the addressing mode represented
5494// by AM is legal for this target, for a load/store of the specified type.
5495bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5496 const Type *Ty) const {
5497 // X86 supports extremely general addressing modes.
5498
5499 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5500 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5501 return false;
5502
5503 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005504 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005505 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5506 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005507
5508 // X86-64 only supports addr of globals in small code model.
5509 if (Subtarget->is64Bit()) {
5510 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5511 return false;
5512 // If lower 4G is not available, then we must use rip-relative addressing.
5513 if (AM.BaseOffs || AM.Scale > 1)
5514 return false;
5515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005516 }
5517
5518 switch (AM.Scale) {
5519 case 0:
5520 case 1:
5521 case 2:
5522 case 4:
5523 case 8:
5524 // These scales always work.
5525 break;
5526 case 3:
5527 case 5:
5528 case 9:
5529 // These scales are formed with basereg+scalereg. Only accept if there is
5530 // no basereg yet.
5531 if (AM.HasBaseReg)
5532 return false;
5533 break;
5534 default: // Other stuff never works.
5535 return false;
5536 }
5537
5538 return true;
5539}
5540
5541
Evan Cheng27a820a2007-10-26 01:56:11 +00005542bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5543 if (!Ty1->isInteger() || !Ty2->isInteger())
5544 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005545 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5546 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5547 if (NumBits1 <= NumBits2)
5548 return false;
5549 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005550}
5551
Evan Cheng9decb332007-10-29 19:58:20 +00005552bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5553 MVT::ValueType VT2) const {
5554 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5555 return false;
5556 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5557 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5558 if (NumBits1 <= NumBits2)
5559 return false;
5560 return Subtarget->is64Bit() || NumBits1 < 64;
5561}
Evan Cheng27a820a2007-10-26 01:56:11 +00005562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563/// isShuffleMaskLegal - Targets can use this to indicate that they only
5564/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5565/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5566/// are assumed to be legal.
5567bool
5568X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5569 // Only do shuffles on 128-bit vector types for now.
5570 if (MVT::getSizeInBits(VT) == 64) return false;
5571 return (Mask.Val->getNumOperands() <= 4 ||
5572 isIdentityMask(Mask.Val) ||
5573 isIdentityMask(Mask.Val, true) ||
5574 isSplatMask(Mask.Val) ||
5575 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5576 X86::isUNPCKLMask(Mask.Val) ||
5577 X86::isUNPCKHMask(Mask.Val) ||
5578 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5579 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5580}
5581
5582bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5583 MVT::ValueType EVT,
5584 SelectionDAG &DAG) const {
5585 unsigned NumElts = BVOps.size();
5586 // Only do shuffles on 128-bit vector types for now.
5587 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5588 if (NumElts == 2) return true;
5589 if (NumElts == 4) {
5590 return (isMOVLMask(&BVOps[0], 4) ||
5591 isCommutedMOVL(&BVOps[0], 4, true) ||
5592 isSHUFPMask(&BVOps[0], 4) ||
5593 isCommutedSHUFP(&BVOps[0], 4));
5594 }
5595 return false;
5596}
5597
5598//===----------------------------------------------------------------------===//
5599// X86 Scheduler Hooks
5600//===----------------------------------------------------------------------===//
5601
5602MachineBasicBlock *
5603X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5604 MachineBasicBlock *BB) {
5605 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5606 switch (MI->getOpcode()) {
5607 default: assert(false && "Unexpected instr type to insert");
5608 case X86::CMOV_FR32:
5609 case X86::CMOV_FR64:
5610 case X86::CMOV_V4F32:
5611 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005612 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005613 // To "insert" a SELECT_CC instruction, we actually have to insert the
5614 // diamond control-flow pattern. The incoming instruction knows the
5615 // destination vreg to set, the condition code register to branch on, the
5616 // true/false values to select between, and a branch opcode to use.
5617 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5618 ilist<MachineBasicBlock>::iterator It = BB;
5619 ++It;
5620
5621 // thisMBB:
5622 // ...
5623 // TrueVal = ...
5624 // cmpTY ccX, r1, r2
5625 // bCC copy1MBB
5626 // fallthrough --> copy0MBB
5627 MachineBasicBlock *thisMBB = BB;
5628 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5629 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5630 unsigned Opc =
5631 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5632 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5633 MachineFunction *F = BB->getParent();
5634 F->getBasicBlockList().insert(It, copy0MBB);
5635 F->getBasicBlockList().insert(It, sinkMBB);
5636 // Update machine-CFG edges by first adding all successors of the current
5637 // block to the new block which will contain the Phi node for the select.
5638 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5639 e = BB->succ_end(); i != e; ++i)
5640 sinkMBB->addSuccessor(*i);
5641 // Next, remove all successors of the current block, and add the true
5642 // and fallthrough blocks as its successors.
5643 while(!BB->succ_empty())
5644 BB->removeSuccessor(BB->succ_begin());
5645 BB->addSuccessor(copy0MBB);
5646 BB->addSuccessor(sinkMBB);
5647
5648 // copy0MBB:
5649 // %FalseValue = ...
5650 // # fallthrough to sinkMBB
5651 BB = copy0MBB;
5652
5653 // Update machine-CFG edges
5654 BB->addSuccessor(sinkMBB);
5655
5656 // sinkMBB:
5657 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5658 // ...
5659 BB = sinkMBB;
5660 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5661 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5662 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5663
5664 delete MI; // The pseudo instruction is gone now.
5665 return BB;
5666 }
5667
5668 case X86::FP32_TO_INT16_IN_MEM:
5669 case X86::FP32_TO_INT32_IN_MEM:
5670 case X86::FP32_TO_INT64_IN_MEM:
5671 case X86::FP64_TO_INT16_IN_MEM:
5672 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005673 case X86::FP64_TO_INT64_IN_MEM:
5674 case X86::FP80_TO_INT16_IN_MEM:
5675 case X86::FP80_TO_INT32_IN_MEM:
5676 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005677 // Change the floating point control register to use "round towards zero"
5678 // mode when truncating to an integer value.
5679 MachineFunction *F = BB->getParent();
5680 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5681 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5682
5683 // Load the old value of the high byte of the control word...
5684 unsigned OldCW =
5685 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
5686 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5687
5688 // Set the high part to be round to zero...
5689 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5690 .addImm(0xC7F);
5691
5692 // Reload the modified control word now...
5693 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5694
5695 // Restore the memory image of control word to original value
5696 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5697 .addReg(OldCW);
5698
5699 // Get the X86 opcode to use.
5700 unsigned Opc;
5701 switch (MI->getOpcode()) {
5702 default: assert(0 && "illegal opcode!");
5703 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5704 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5705 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5706 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5707 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5708 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005709 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5710 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5711 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005712 }
5713
5714 X86AddressMode AM;
5715 MachineOperand &Op = MI->getOperand(0);
5716 if (Op.isRegister()) {
5717 AM.BaseType = X86AddressMode::RegBase;
5718 AM.Base.Reg = Op.getReg();
5719 } else {
5720 AM.BaseType = X86AddressMode::FrameIndexBase;
5721 AM.Base.FrameIndex = Op.getFrameIndex();
5722 }
5723 Op = MI->getOperand(1);
5724 if (Op.isImmediate())
5725 AM.Scale = Op.getImm();
5726 Op = MI->getOperand(2);
5727 if (Op.isImmediate())
5728 AM.IndexReg = Op.getImm();
5729 Op = MI->getOperand(3);
5730 if (Op.isGlobalAddress()) {
5731 AM.GV = Op.getGlobal();
5732 } else {
5733 AM.Disp = Op.getImm();
5734 }
5735 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5736 .addReg(MI->getOperand(4).getReg());
5737
5738 // Reload the original control word now.
5739 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5740
5741 delete MI; // The pseudo instruction is gone now.
5742 return BB;
5743 }
5744 }
5745}
5746
5747//===----------------------------------------------------------------------===//
5748// X86 Optimization Hooks
5749//===----------------------------------------------------------------------===//
5750
5751void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5752 uint64_t Mask,
5753 uint64_t &KnownZero,
5754 uint64_t &KnownOne,
5755 const SelectionDAG &DAG,
5756 unsigned Depth) const {
5757 unsigned Opc = Op.getOpcode();
5758 assert((Opc >= ISD::BUILTIN_OP_END ||
5759 Opc == ISD::INTRINSIC_WO_CHAIN ||
5760 Opc == ISD::INTRINSIC_W_CHAIN ||
5761 Opc == ISD::INTRINSIC_VOID) &&
5762 "Should use MaskedValueIsZero if you don't know whether Op"
5763 " is a target node!");
5764
5765 KnownZero = KnownOne = 0; // Don't know anything.
5766 switch (Opc) {
5767 default: break;
5768 case X86ISD::SETCC:
5769 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5770 break;
5771 }
5772}
5773
5774/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5775/// element of the result of the vector shuffle.
5776static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5777 MVT::ValueType VT = N->getValueType(0);
5778 SDOperand PermMask = N->getOperand(2);
5779 unsigned NumElems = PermMask.getNumOperands();
5780 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5781 i %= NumElems;
5782 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5783 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005784 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005785 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5786 SDOperand Idx = PermMask.getOperand(i);
5787 if (Idx.getOpcode() == ISD::UNDEF)
5788 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5789 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5790 }
5791 return SDOperand();
5792}
5793
5794/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5795/// node is a GlobalAddress + an offset.
5796static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5797 unsigned Opc = N->getOpcode();
5798 if (Opc == X86ISD::Wrapper) {
5799 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5800 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5801 return true;
5802 }
5803 } else if (Opc == ISD::ADD) {
5804 SDOperand N1 = N->getOperand(0);
5805 SDOperand N2 = N->getOperand(1);
5806 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5807 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5808 if (V) {
5809 Offset += V->getSignExtended();
5810 return true;
5811 }
5812 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5813 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5814 if (V) {
5815 Offset += V->getSignExtended();
5816 return true;
5817 }
5818 }
5819 }
5820 return false;
5821}
5822
5823/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5824/// + Dist * Size.
5825static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5826 MachineFrameInfo *MFI) {
5827 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5828 return false;
5829
5830 SDOperand Loc = N->getOperand(1);
5831 SDOperand BaseLoc = Base->getOperand(1);
5832 if (Loc.getOpcode() == ISD::FrameIndex) {
5833 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5834 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005835 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5836 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005837 int FS = MFI->getObjectSize(FI);
5838 int BFS = MFI->getObjectSize(BFI);
5839 if (FS != BFS || FS != Size) return false;
5840 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5841 } else {
5842 GlobalValue *GV1 = NULL;
5843 GlobalValue *GV2 = NULL;
5844 int64_t Offset1 = 0;
5845 int64_t Offset2 = 0;
5846 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5847 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5848 if (isGA1 && isGA2 && GV1 == GV2)
5849 return Offset1 == (Offset2 + Dist*Size);
5850 }
5851
5852 return false;
5853}
5854
5855static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5856 const X86Subtarget *Subtarget) {
5857 GlobalValue *GV;
5858 int64_t Offset;
5859 if (isGAPlusOffset(Base, GV, Offset))
5860 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5861 else {
5862 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman53491e92007-07-23 20:24:29 +00005863 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005864 if (BFI < 0)
5865 // Fixed objects do not specify alignment, however the offsets are known.
5866 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5867 (MFI->getObjectOffset(BFI) % 16) == 0);
5868 else
5869 return MFI->getObjectAlignment(BFI) >= 16;
5870 }
5871 return false;
5872}
5873
5874
5875/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5876/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5877/// if the load addresses are consecutive, non-overlapping, and in the right
5878/// order.
5879static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5880 const X86Subtarget *Subtarget) {
5881 MachineFunction &MF = DAG.getMachineFunction();
5882 MachineFrameInfo *MFI = MF.getFrameInfo();
5883 MVT::ValueType VT = N->getValueType(0);
5884 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5885 SDOperand PermMask = N->getOperand(2);
5886 int NumElems = (int)PermMask.getNumOperands();
5887 SDNode *Base = NULL;
5888 for (int i = 0; i < NumElems; ++i) {
5889 SDOperand Idx = PermMask.getOperand(i);
5890 if (Idx.getOpcode() == ISD::UNDEF) {
5891 if (!Base) return SDOperand();
5892 } else {
5893 SDOperand Arg =
5894 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5895 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5896 return SDOperand();
5897 if (!Base)
5898 Base = Arg.Val;
5899 else if (!isConsecutiveLoad(Arg.Val, Base,
5900 i, MVT::getSizeInBits(EVT)/8,MFI))
5901 return SDOperand();
5902 }
5903 }
5904
5905 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005906 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005907 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005908 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005909 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005910 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005911 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5912 LD->getSrcValueOffset(), LD->isVolatile(),
5913 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005914 }
5915}
5916
5917/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5918static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5919 const X86Subtarget *Subtarget) {
5920 SDOperand Cond = N->getOperand(0);
5921
5922 // If we have SSE[12] support, try to form min/max nodes.
5923 if (Subtarget->hasSSE2() &&
5924 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5925 if (Cond.getOpcode() == ISD::SETCC) {
5926 // Get the LHS/RHS of the select.
5927 SDOperand LHS = N->getOperand(1);
5928 SDOperand RHS = N->getOperand(2);
5929 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5930
5931 unsigned Opcode = 0;
5932 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5933 switch (CC) {
5934 default: break;
5935 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5936 case ISD::SETULE:
5937 case ISD::SETLE:
5938 if (!UnsafeFPMath) break;
5939 // FALL THROUGH.
5940 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5941 case ISD::SETLT:
5942 Opcode = X86ISD::FMIN;
5943 break;
5944
5945 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5946 case ISD::SETUGT:
5947 case ISD::SETGT:
5948 if (!UnsafeFPMath) break;
5949 // FALL THROUGH.
5950 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5951 case ISD::SETGE:
5952 Opcode = X86ISD::FMAX;
5953 break;
5954 }
5955 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5956 switch (CC) {
5957 default: break;
5958 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5959 case ISD::SETUGT:
5960 case ISD::SETGT:
5961 if (!UnsafeFPMath) break;
5962 // FALL THROUGH.
5963 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5964 case ISD::SETGE:
5965 Opcode = X86ISD::FMIN;
5966 break;
5967
5968 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5969 case ISD::SETULE:
5970 case ISD::SETLE:
5971 if (!UnsafeFPMath) break;
5972 // FALL THROUGH.
5973 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5974 case ISD::SETLT:
5975 Opcode = X86ISD::FMAX;
5976 break;
5977 }
5978 }
5979
5980 if (Opcode)
5981 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5982 }
5983
5984 }
5985
5986 return SDOperand();
5987}
5988
5989
5990SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5991 DAGCombinerInfo &DCI) const {
5992 SelectionDAG &DAG = DCI.DAG;
5993 switch (N->getOpcode()) {
5994 default: break;
5995 case ISD::VECTOR_SHUFFLE:
5996 return PerformShuffleCombine(N, DAG, Subtarget);
5997 case ISD::SELECT:
5998 return PerformSELECTCombine(N, DAG, Subtarget);
5999 }
6000
6001 return SDOperand();
6002}
6003
6004//===----------------------------------------------------------------------===//
6005// X86 Inline Assembly Support
6006//===----------------------------------------------------------------------===//
6007
6008/// getConstraintType - Given a constraint letter, return the type of
6009/// constraint it is for this target.
6010X86TargetLowering::ConstraintType
6011X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6012 if (Constraint.size() == 1) {
6013 switch (Constraint[0]) {
6014 case 'A':
6015 case 'r':
6016 case 'R':
6017 case 'l':
6018 case 'q':
6019 case 'Q':
6020 case 'x':
6021 case 'Y':
6022 return C_RegisterClass;
6023 default:
6024 break;
6025 }
6026 }
6027 return TargetLowering::getConstraintType(Constraint);
6028}
6029
Chris Lattnera531abc2007-08-25 00:47:38 +00006030/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6031/// vector. If it is invalid, don't add anything to Ops.
6032void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6033 char Constraint,
6034 std::vector<SDOperand>&Ops,
6035 SelectionDAG &DAG) {
6036 SDOperand Result(0, 0);
6037
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006038 switch (Constraint) {
6039 default: break;
6040 case 'I':
6041 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006042 if (C->getValue() <= 31) {
6043 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6044 break;
6045 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006046 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006047 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006048 case 'N':
6049 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006050 if (C->getValue() <= 255) {
6051 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6052 break;
6053 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006054 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006055 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006056 case 'i': {
6057 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006058 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6059 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6060 break;
6061 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006062
6063 // If we are in non-pic codegen mode, we allow the address of a global (with
6064 // an optional displacement) to be used with 'i'.
6065 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6066 int64_t Offset = 0;
6067
6068 // Match either (GA) or (GA+C)
6069 if (GA) {
6070 Offset = GA->getOffset();
6071 } else if (Op.getOpcode() == ISD::ADD) {
6072 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6073 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6074 if (C && GA) {
6075 Offset = GA->getOffset()+C->getValue();
6076 } else {
6077 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6078 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6079 if (C && GA)
6080 Offset = GA->getOffset()+C->getValue();
6081 else
6082 C = 0, GA = 0;
6083 }
6084 }
6085
6086 if (GA) {
6087 // If addressing this global requires a load (e.g. in PIC mode), we can't
6088 // match.
6089 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6090 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006091 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006092
6093 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6094 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006095 Result = Op;
6096 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006097 }
6098
6099 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006100 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006101 }
6102 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006103
6104 if (Result.Val) {
6105 Ops.push_back(Result);
6106 return;
6107 }
6108 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006109}
6110
6111std::vector<unsigned> X86TargetLowering::
6112getRegClassForInlineAsmConstraint(const std::string &Constraint,
6113 MVT::ValueType VT) const {
6114 if (Constraint.size() == 1) {
6115 // FIXME: not handling fp-stack yet!
6116 switch (Constraint[0]) { // GCC X86 Constraint Letters
6117 default: break; // Unknown constraint letter
6118 case 'A': // EAX/EDX
6119 if (VT == MVT::i32 || VT == MVT::i64)
6120 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6121 break;
6122 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6123 case 'Q': // Q_REGS
6124 if (VT == MVT::i32)
6125 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6126 else if (VT == MVT::i16)
6127 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6128 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006129 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006130 else if (VT == MVT::i64)
6131 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6132 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006133 }
6134 }
6135
6136 return std::vector<unsigned>();
6137}
6138
6139std::pair<unsigned, const TargetRegisterClass*>
6140X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6141 MVT::ValueType VT) const {
6142 // First, see if this is a constraint that directly corresponds to an LLVM
6143 // register class.
6144 if (Constraint.size() == 1) {
6145 // GCC Constraint Letters
6146 switch (Constraint[0]) {
6147 default: break;
6148 case 'r': // GENERAL_REGS
6149 case 'R': // LEGACY_REGS
6150 case 'l': // INDEX_REGS
6151 if (VT == MVT::i64 && Subtarget->is64Bit())
6152 return std::make_pair(0U, X86::GR64RegisterClass);
6153 if (VT == MVT::i32)
6154 return std::make_pair(0U, X86::GR32RegisterClass);
6155 else if (VT == MVT::i16)
6156 return std::make_pair(0U, X86::GR16RegisterClass);
6157 else if (VT == MVT::i8)
6158 return std::make_pair(0U, X86::GR8RegisterClass);
6159 break;
6160 case 'y': // MMX_REGS if MMX allowed.
6161 if (!Subtarget->hasMMX()) break;
6162 return std::make_pair(0U, X86::VR64RegisterClass);
6163 break;
6164 case 'Y': // SSE_REGS if SSE2 allowed
6165 if (!Subtarget->hasSSE2()) break;
6166 // FALL THROUGH.
6167 case 'x': // SSE_REGS if SSE1 allowed
6168 if (!Subtarget->hasSSE1()) break;
6169
6170 switch (VT) {
6171 default: break;
6172 // Scalar SSE types.
6173 case MVT::f32:
6174 case MVT::i32:
6175 return std::make_pair(0U, X86::FR32RegisterClass);
6176 case MVT::f64:
6177 case MVT::i64:
6178 return std::make_pair(0U, X86::FR64RegisterClass);
6179 // Vector types.
6180 case MVT::v16i8:
6181 case MVT::v8i16:
6182 case MVT::v4i32:
6183 case MVT::v2i64:
6184 case MVT::v4f32:
6185 case MVT::v2f64:
6186 return std::make_pair(0U, X86::VR128RegisterClass);
6187 }
6188 break;
6189 }
6190 }
6191
6192 // Use the default implementation in TargetLowering to convert the register
6193 // constraint into a member of a register class.
6194 std::pair<unsigned, const TargetRegisterClass*> Res;
6195 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6196
6197 // Not found as a standard register?
6198 if (Res.second == 0) {
6199 // GCC calls "st(0)" just plain "st".
6200 if (StringsEqualNoCase("{st}", Constraint)) {
6201 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006202 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006203 }
6204
6205 return Res;
6206 }
6207
6208 // Otherwise, check to see if this is a register class of the wrong value
6209 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6210 // turn into {ax},{dx}.
6211 if (Res.second->hasType(VT))
6212 return Res; // Correct type already, nothing to do.
6213
6214 // All of the single-register GCC register classes map their values onto
6215 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6216 // really want an 8-bit or 32-bit register, map to the appropriate register
6217 // class and return the appropriate register.
6218 if (Res.second != X86::GR16RegisterClass)
6219 return Res;
6220
6221 if (VT == MVT::i8) {
6222 unsigned DestReg = 0;
6223 switch (Res.first) {
6224 default: break;
6225 case X86::AX: DestReg = X86::AL; break;
6226 case X86::DX: DestReg = X86::DL; break;
6227 case X86::CX: DestReg = X86::CL; break;
6228 case X86::BX: DestReg = X86::BL; break;
6229 }
6230 if (DestReg) {
6231 Res.first = DestReg;
6232 Res.second = Res.second = X86::GR8RegisterClass;
6233 }
6234 } else if (VT == MVT::i32) {
6235 unsigned DestReg = 0;
6236 switch (Res.first) {
6237 default: break;
6238 case X86::AX: DestReg = X86::EAX; break;
6239 case X86::DX: DestReg = X86::EDX; break;
6240 case X86::CX: DestReg = X86::ECX; break;
6241 case X86::BX: DestReg = X86::EBX; break;
6242 case X86::SI: DestReg = X86::ESI; break;
6243 case X86::DI: DestReg = X86::EDI; break;
6244 case X86::BP: DestReg = X86::EBP; break;
6245 case X86::SP: DestReg = X86::ESP; break;
6246 }
6247 if (DestReg) {
6248 Res.first = DestReg;
6249 Res.second = Res.second = X86::GR32RegisterClass;
6250 }
6251 } else if (VT == MVT::i64) {
6252 unsigned DestReg = 0;
6253 switch (Res.first) {
6254 default: break;
6255 case X86::AX: DestReg = X86::RAX; break;
6256 case X86::DX: DestReg = X86::RDX; break;
6257 case X86::CX: DestReg = X86::RCX; break;
6258 case X86::BX: DestReg = X86::RBX; break;
6259 case X86::SI: DestReg = X86::RSI; break;
6260 case X86::DI: DestReg = X86::RDI; break;
6261 case X86::BP: DestReg = X86::RBP; break;
6262 case X86::SP: DestReg = X86::RSP; break;
6263 }
6264 if (DestReg) {
6265 Res.first = DestReg;
6266 Res.second = Res.second = X86::GR64RegisterClass;
6267 }
6268 }
6269
6270 return Res;
6271}