blob: 627c0ce5bf17562d656c2634a0327d1d5aff462c [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//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Chris Lattner1b989192007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000036#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000038#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/ADT/StringExtras.h"
Duncan Sandsd8455ca2007-07-27 20:02:49 +000040#include "llvm/ParameterAttributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041using namespace llvm;
42
43X86TargetLowering::X86TargetLowering(TargetMachine &TM)
44 : TargetLowering(TM) {
45 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000046 X86ScalarSSEf64 = Subtarget->hasSSE2();
47 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000049
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
51 RegInfo = TM.getRegisterInfo();
52
53 // Set up the TargetLowering object.
54
55 // X86 is weird, it always uses i8 for shift amounts and setcc results.
56 setShiftAmountType(MVT::i8);
57 setSetCCResultType(MVT::i8);
58 setSetCCResultContents(ZeroOrOneSetCCResult);
59 setSchedulingPreference(SchedulingForRegPressure);
60 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
61 setStackPointerRegisterToSaveRestore(X86StackPtr);
62
63 if (Subtarget->isTargetDarwin()) {
64 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
65 setUseUnderscoreSetJmp(false);
66 setUseUnderscoreLongJmp(false);
67 } else if (Subtarget->isTargetMingw()) {
68 // MS runtime is weird: it exports _setjmp, but longjmp!
69 setUseUnderscoreSetJmp(true);
70 setUseUnderscoreLongJmp(false);
71 } else {
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(true);
74 }
75
76 // Set up the register classes.
77 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
78 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
79 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
80 if (Subtarget->is64Bit())
81 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
82
83 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
84
85 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
86 // operation.
87 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
88 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
89 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
90
91 if (Subtarget->is64Bit()) {
92 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
93 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
94 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +000095 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
97 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
98 else
99 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
100 }
101
102 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
103 // this operation.
104 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
105 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
106 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000107 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000109 // f32 and f64 cases are Legal, f80 case is not
110 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
111 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
113 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
114 }
115
Dale Johannesen958b08b2007-09-19 23:55:34 +0000116 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
117 // are Legal, f80 is custom lowered.
118 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
119 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
121 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
122 // this operation.
123 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
124 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
125
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000126 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000128 // f32 and f64 cases are Legal, f80 case is not
129 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 } else {
131 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
132 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
133 }
134
135 // Handle FP_TO_UINT by promoting the destination to a larger signed
136 // conversion.
137 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
138 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
140
141 if (Subtarget->is64Bit()) {
142 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
143 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
144 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000145 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 // Expand FP_TO_UINT into a select.
147 // FIXME: We would like to use a Custom expander here eventually to do
148 // the optimal thing for SSE vs. the default expansion in the legalizer.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
150 else
151 // With SSE3 we can use fisttpll to convert to a signed i64.
152 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
153 }
154
155 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000156 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
158 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
159 }
160
Dan Gohman5a199552007-10-08 18:33:35 +0000161 // Scalar integer multiply, multiply-high, divide, and remainder are
162 // lowered to use operations that produce two results, to match the
163 // available instructions. This exposes the two-result form to trivial
164 // CSE, which is able to combine x/y and x%y into a single instruction,
165 // for example. The single-result multiply instructions are introduced
166 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
167 // is not needed.
168 setOperationAction(ISD::MUL , MVT::i8 , Expand);
169 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
170 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
171 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
172 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
173 setOperationAction(ISD::SREM , MVT::i8 , Expand);
174 setOperationAction(ISD::UREM , MVT::i8 , Expand);
175 setOperationAction(ISD::MUL , MVT::i16 , Expand);
176 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
177 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
178 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
179 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
180 setOperationAction(ISD::SREM , MVT::i16 , Expand);
181 setOperationAction(ISD::UREM , MVT::i16 , Expand);
182 setOperationAction(ISD::MUL , MVT::i32 , Expand);
183 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
184 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
185 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
186 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
187 setOperationAction(ISD::SREM , MVT::i32 , Expand);
188 setOperationAction(ISD::UREM , MVT::i32 , Expand);
189 setOperationAction(ISD::MUL , MVT::i64 , Expand);
190 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
191 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
192 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
193 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
194 setOperationAction(ISD::SREM , MVT::i64 , Expand);
195 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
198 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
199 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
200 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
201 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
202 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000203 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
207 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
208 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000209 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000212 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
213 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000215 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
216 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000218 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
219 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 if (Subtarget->is64Bit()) {
221 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000222 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
223 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 }
225
226 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
227 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
228
229 // These should be promoted to a larger select which is supported.
230 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
231 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
232 // X86 wants to expand cmov itself.
233 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
234 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
235 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
236 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000237 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
239 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
240 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
241 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
242 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000243 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 if (Subtarget->is64Bit()) {
245 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
246 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
247 }
248 // X86 ret instruction may pop stack.
249 setOperationAction(ISD::RET , MVT::Other, Custom);
250 if (!Subtarget->is64Bit())
251 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
252
253 // Darwin ABI issue.
254 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
255 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
256 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
257 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
258 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
259 if (Subtarget->is64Bit()) {
260 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
261 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
262 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
263 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
264 }
265 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
266 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
267 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
268 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
269 // X86 wants to expand memset / memcpy itself.
270 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
271 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
272
Dan Gohman21442852007-09-25 15:10:49 +0000273 // Use the default ISD::LOCATION expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 // FIXME - use subtarget debug flags
276 if (!Subtarget->isTargetDarwin() &&
277 !Subtarget->isTargetELF() &&
278 !Subtarget->isTargetCygMing())
279 setOperationAction(ISD::LABEL, MVT::Other, Expand);
280
281 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
282 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
283 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
284 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
285 if (Subtarget->is64Bit()) {
286 // FIXME: Verify
287 setExceptionPointerRegister(X86::RAX);
288 setExceptionSelectorRegister(X86::RDX);
289 } else {
290 setExceptionPointerRegister(X86::EAX);
291 setExceptionSelectorRegister(X86::EDX);
292 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000293 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
Duncan Sands7407a9f2007-09-11 14:10:23 +0000295 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000296
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
298 setOperationAction(ISD::VASTART , MVT::Other, Custom);
299 setOperationAction(ISD::VAARG , MVT::Other, Expand);
300 setOperationAction(ISD::VAEND , MVT::Other, Expand);
301 if (Subtarget->is64Bit())
302 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
303 else
304 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
305
306 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
307 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
308 if (Subtarget->is64Bit())
309 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
310 if (Subtarget->isTargetCygMing())
311 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
312 else
313 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
314
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000315 if (X86ScalarSSEf64) {
316 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 // Set up the FP register classes.
318 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
319 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
320
321 // Use ANDPD to simulate FABS.
322 setOperationAction(ISD::FABS , MVT::f64, Custom);
323 setOperationAction(ISD::FABS , MVT::f32, Custom);
324
325 // Use XORP to simulate FNEG.
326 setOperationAction(ISD::FNEG , MVT::f64, Custom);
327 setOperationAction(ISD::FNEG , MVT::f32, Custom);
328
329 // Use ANDPD and ORPD to simulate FCOPYSIGN.
330 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
331 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
332
333 // We don't support sin/cos/fmod
334 setOperationAction(ISD::FSIN , MVT::f64, Expand);
335 setOperationAction(ISD::FCOS , MVT::f64, Expand);
336 setOperationAction(ISD::FREM , MVT::f64, Expand);
337 setOperationAction(ISD::FSIN , MVT::f32, Expand);
338 setOperationAction(ISD::FCOS , MVT::f32, Expand);
339 setOperationAction(ISD::FREM , MVT::f32, Expand);
340
341 // Expand FP immediates into loads from the stack, except for the special
342 // cases we handle.
343 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
344 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000345 addLegalFPImmediate(APFloat(+0.0)); // xorpd
346 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000347
348 // Conversions to long double (in X87) go through memory.
349 setConvertAction(MVT::f32, MVT::f80, Expand);
350 setConvertAction(MVT::f64, MVT::f80, Expand);
351
352 // Conversions from long double (in X87) go through memory.
353 setConvertAction(MVT::f80, MVT::f32, Expand);
354 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000355 } else if (X86ScalarSSEf32) {
356 // Use SSE for f32, x87 for f64.
357 // Set up the FP register classes.
358 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
359 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
360
361 // Use ANDPS to simulate FABS.
362 setOperationAction(ISD::FABS , MVT::f32, Custom);
363
364 // Use XORP to simulate FNEG.
365 setOperationAction(ISD::FNEG , MVT::f32, Custom);
366
367 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
368
369 // Use ANDPS and ORPS to simulate FCOPYSIGN.
370 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
371 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
372
373 // We don't support sin/cos/fmod
374 setOperationAction(ISD::FSIN , MVT::f32, Expand);
375 setOperationAction(ISD::FCOS , MVT::f32, Expand);
376 setOperationAction(ISD::FREM , MVT::f32, Expand);
377
378 // Expand FP immediates into loads from the stack, except for the special
379 // cases we handle.
380 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
381 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
382 addLegalFPImmediate(APFloat(+0.0f)); // xorps
383 addLegalFPImmediate(APFloat(+0.0)); // FLD0
384 addLegalFPImmediate(APFloat(+1.0)); // FLD1
385 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
386 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
387
388 // SSE->x87 conversions go through memory.
389 setConvertAction(MVT::f32, MVT::f64, Expand);
390 setConvertAction(MVT::f32, MVT::f80, Expand);
391
392 // x87->SSE truncations need to go through memory.
393 setConvertAction(MVT::f80, MVT::f32, Expand);
394 setConvertAction(MVT::f64, MVT::f32, Expand);
395 // And x87->x87 truncations also.
396 setConvertAction(MVT::f80, MVT::f64, Expand);
397
398 if (!UnsafeFPMath) {
399 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
400 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
401 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000403 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 // Set up the FP register classes.
405 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
406 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
407
408 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
409 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
410 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
411 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000412
413 // Floating truncations need to go through memory.
414 setConvertAction(MVT::f80, MVT::f32, Expand);
415 setConvertAction(MVT::f64, MVT::f32, Expand);
416 setConvertAction(MVT::f80, MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417
418 if (!UnsafeFPMath) {
419 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
420 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
421 }
422
423 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
424 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000425 addLegalFPImmediate(APFloat(+0.0)); // FLD0
426 addLegalFPImmediate(APFloat(+1.0)); // FLD1
427 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
428 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000429 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
430 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
431 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
432 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 }
434
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000435 // Long double always uses X87.
436 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000437 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
438 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
439 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000440 if (!UnsafeFPMath) {
441 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
442 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
443 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000444
Dan Gohman2f7b1982007-10-11 23:21:31 +0000445 // Always use a library call for pow.
446 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
447 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
448 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
449
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 // First set operation action for all vector types to expand. Then we
451 // will selectively turn on ones that can be effectively codegen'd.
452 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
453 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
454 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
455 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
456 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
457 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
458 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
459 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
460 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
461 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
464 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
465 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
466 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
467 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
468 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
469 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
470 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
471 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
475 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
476 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000477 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
478 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
479 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
480 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000481 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000482 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000485 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
486 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 }
492
493 if (Subtarget->hasMMX()) {
494 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
495 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
496 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
497 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
498
499 // FIXME: add MMX packed arithmetics
500
501 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
502 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
503 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
504 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
505
506 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
507 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
508 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000509 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510
511 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
512 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
513
514 setOperationAction(ISD::AND, MVT::v8i8, Promote);
515 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
516 setOperationAction(ISD::AND, MVT::v4i16, Promote);
517 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
518 setOperationAction(ISD::AND, MVT::v2i32, Promote);
519 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
520 setOperationAction(ISD::AND, MVT::v1i64, Legal);
521
522 setOperationAction(ISD::OR, MVT::v8i8, Promote);
523 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
524 setOperationAction(ISD::OR, MVT::v4i16, Promote);
525 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
526 setOperationAction(ISD::OR, MVT::v2i32, Promote);
527 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
528 setOperationAction(ISD::OR, MVT::v1i64, Legal);
529
530 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
531 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
532 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
533 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
534 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
535 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
536 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
537
538 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
539 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
540 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
541 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
542 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
543 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
544 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
545
546 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
547 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
548 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
549 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
550
551 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
552 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
553 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
554 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
555
556 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
557 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
558 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
559 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
560 }
561
562 if (Subtarget->hasSSE1()) {
563 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
564
565 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
566 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
567 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
568 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
569 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
570 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
572 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
573 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
574 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
575 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
576 }
577
578 if (Subtarget->hasSSE2()) {
579 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
580 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
581 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
582 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
583 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
584
585 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
586 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
587 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
588 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
589 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
590 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
591 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
592 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
593 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
594 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
595 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
596 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
597 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
598 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
599 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600
601 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
602 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
603 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
604 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
605 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
606 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
607
608 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
609 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000610 // Do not attempt to custom lower non-power-of-2 vectors
611 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
612 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
614 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
615 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
616 }
617 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
618 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
619 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
621 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000622 if (Subtarget->is64Bit())
623 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624
625 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
626 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
627 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
628 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
629 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
630 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
631 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
632 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
633 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
634 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
635 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
636 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
637 }
638
639 // Custom lower v2i64 and v2f64 selects.
640 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
641 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
642 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
643 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
644 }
645
646 // We want to custom lower some of our intrinsics.
647 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
648
649 // We have target-specific dag combine patterns for the following nodes:
650 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
651 setTargetDAGCombine(ISD::SELECT);
652
653 computeRegisterProperties();
654
655 // FIXME: These should be based on subtarget info. Plus, the values should
656 // be smaller when we are in optimizing for size mode.
657 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
658 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
659 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
660 allowUnalignedMemoryAccesses = true; // x86 supports it!
661}
662
663
Evan Cheng6fb06762007-11-09 01:32:10 +0000664/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
665/// jumptable.
666SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
667 SelectionDAG &DAG) const {
668 if (usesGlobalOffsetTable())
669 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
670 if (!Subtarget->isPICStyleRIPRel())
671 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
672 return Table;
673}
674
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675//===----------------------------------------------------------------------===//
676// Return Value Calling Convention Implementation
677//===----------------------------------------------------------------------===//
678
679#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000680
681/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
682/// exists skip possible ISD:TokenFactor.
683static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
684 if (Chain.getOpcode()==X86ISD::TAILCALL) {
685 return Chain;
686 } else if (Chain.getOpcode()==ISD::TokenFactor) {
687 if (Chain.getNumOperands() &&
688 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
689 return Chain.getOperand(0);
690 }
691 return Chain;
692}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693
694/// LowerRET - Lower an ISD::RET node.
695SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
696 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
697
698 SmallVector<CCValAssign, 16> RVLocs;
699 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
700 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
701 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
702 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000703
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 // If this is the first return lowered for this function, add the regs to the
705 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000706 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 for (unsigned i = 0; i != RVLocs.size(); ++i)
708 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000709 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000713 // Handle tail call return.
714 Chain = GetPossiblePreceedingTailCall(Chain);
715 if (Chain.getOpcode() == X86ISD::TAILCALL) {
716 SDOperand TailCall = Chain;
717 SDOperand TargetAddress = TailCall.getOperand(1);
718 SDOperand StackAdjustment = TailCall.getOperand(2);
719 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
720 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
721 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
722 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
723 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
724 "Expecting an global address, external symbol, or register");
725 assert( StackAdjustment.getOpcode() == ISD::Constant &&
726 "Expecting a const value");
727
728 SmallVector<SDOperand,8> Operands;
729 Operands.push_back(Chain.getOperand(0));
730 Operands.push_back(TargetAddress);
731 Operands.push_back(StackAdjustment);
732 // Copy registers used by the call. Last operand is a flag so it is not
733 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000734 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000735 Operands.push_back(Chain.getOperand(i));
736 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000737 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
738 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000739 }
740
741 // Regular return.
742 SDOperand Flag;
743
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 // Copy the result values into the output registers.
745 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
746 RVLocs[0].getLocReg() != X86::ST0) {
747 for (unsigned i = 0; i != RVLocs.size(); ++i) {
748 CCValAssign &VA = RVLocs[i];
749 assert(VA.isRegLoc() && "Can only return in registers!");
750 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
751 Flag);
752 Flag = Chain.getValue(1);
753 }
754 } else {
755 // We need to handle a destination of ST0 specially, because it isn't really
756 // a register.
757 SDOperand Value = Op.getOperand(1);
758
759 // If this is an FP return with ScalarSSE, we need to move the value from
760 // an XMM register onto the fp-stack.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000761 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
762 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 SDOperand MemLoc;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000764
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 // If this is a load into a scalarsse value, don't store the loaded value
766 // back to the stack, only to reload it: just replace the scalar-sse load.
767 if (ISD::isNON_EXTLoad(Value.Val) &&
768 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
769 Chain = Value.getOperand(0);
770 MemLoc = Value.getOperand(1);
771 } else {
772 // Spill the value to memory and reload it into top of stack.
773 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
774 MachineFunction &MF = DAG.getMachineFunction();
775 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
776 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
777 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
778 }
779 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
780 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
781 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
782 Chain = Value.getValue(1);
783 }
784
785 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
786 SDOperand Ops[] = { Chain, Value };
787 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
788 Flag = Chain.getValue(1);
789 }
790
791 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
792 if (Flag.Val)
793 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
794 else
795 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
796}
797
798
799/// LowerCallResult - Lower the result values of an ISD::CALL into the
800/// appropriate copies out of appropriate physical registers. This assumes that
801/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
802/// being lowered. The returns a SDNode with the same number of values as the
803/// ISD::CALL.
804SDNode *X86TargetLowering::
805LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
806 unsigned CallingConv, SelectionDAG &DAG) {
807
808 // Assign locations to each value returned by this call.
809 SmallVector<CCValAssign, 16> RVLocs;
810 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
811 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
812 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
813
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 SmallVector<SDOperand, 8> ResultVals;
815
816 // Copy all of the result registers out of their specified physreg.
817 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
818 for (unsigned i = 0; i != RVLocs.size(); ++i) {
819 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
820 RVLocs[i].getValVT(), InFlag).getValue(1);
821 InFlag = Chain.getValue(2);
822 ResultVals.push_back(Chain.getValue(0));
823 }
824 } else {
825 // Copies from the FP stack are special, as ST0 isn't a valid register
826 // before the fp stackifier runs.
827
828 // Copy ST0 into an RFP register with FP_GET_RESULT.
829 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
830 SDOperand GROps[] = { Chain, InFlag };
831 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
832 Chain = RetVal.getValue(1);
833 InFlag = RetVal.getValue(2);
834
835 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
836 // an XMM register.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000837 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
838 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Chris Lattner40758732007-12-29 06:41:28 +0000839 SDOperand StoreLoc;
840 const Value *SrcVal = 0;
841 int SrcValOffset = 0;
Chris Lattner8b815c22007-12-29 06:57:38 +0000842 MVT::ValueType RetStoreVT = RVLocs[0].getValVT();
Chris Lattner40758732007-12-29 06:41:28 +0000843
844 // Determine where to store the value. If the call result is directly
845 // used by a store, see if we can store directly into the location. In
846 // this case, we'll end up producing a fst + movss[load] + movss[store] to
847 // the same location, and the two movss's will be nuked as dead. This
848 // optimizes common things like "*D = atof(..)" to not need an
849 // intermediate stack slot.
850 if (SDOperand(TheCall, 0).hasOneUse() &&
851 SDOperand(TheCall, 1).hasOneUse()) {
Chris Lattner8b815c22007-12-29 06:57:38 +0000852 // In addition to direct uses, we also support a FP_ROUND that uses the
853 // value, if it is directly stored somewhere.
854 SDNode *User = *TheCall->use_begin();
855 if (User->getOpcode() == ISD::FP_ROUND && User->hasOneUse())
856 User = *User->use_begin();
857
Chris Lattner40758732007-12-29 06:41:28 +0000858 // Ok, we have one use of the value and one use of the chain. See if
859 // they are the same node: a store.
Chris Lattner8b815c22007-12-29 06:57:38 +0000860 if (StoreSDNode *N = dyn_cast<StoreSDNode>(User)) {
861 // Verify that the value being stored is either the call or a
862 // truncation of the call.
863 SDNode *StoreVal = N->getValue().Val;
864 if (StoreVal == TheCall)
865 ; // ok.
866 else if (StoreVal->getOpcode() == ISD::FP_ROUND &&
867 StoreVal->hasOneUse() &&
868 StoreVal->getOperand(0).Val == TheCall)
869 ; // ok.
870 else
871 N = 0; // not ok.
872
873 if (N && N->getChain().Val == TheCall &&
Chris Lattner40758732007-12-29 06:41:28 +0000874 !N->isVolatile() && !N->isTruncatingStore() &&
875 N->getAddressingMode() == ISD::UNINDEXED) {
876 StoreLoc = N->getBasePtr();
877 SrcVal = N->getSrcValue();
878 SrcValOffset = N->getSrcValueOffset();
Chris Lattner8b815c22007-12-29 06:57:38 +0000879 RetStoreVT = N->getValue().getValueType();
Chris Lattner40758732007-12-29 06:41:28 +0000880 }
881 }
882 }
883
884 // If we weren't able to optimize the result, just create a temporary
885 // stack slot.
886 if (StoreLoc.Val == 0) {
887 MachineFunction &MF = DAG.getMachineFunction();
888 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
889 StoreLoc = DAG.getFrameIndex(SSFI, getPointerTy());
890 }
891
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
893 // shouldn't be necessary except that RFP cannot be live across
Chris Lattner40758732007-12-29 06:41:28 +0000894 // multiple blocks (which could happen if a select gets lowered into
895 // multiple blocks and scheduled in between them). When stackifier is
896 // fixed, they can be uncoupled.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 SDOperand Ops[] = {
Chris Lattner8b815c22007-12-29 06:57:38 +0000898 Chain, RetVal, StoreLoc, DAG.getValueType(RetStoreVT), InFlag
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 };
900 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner8b815c22007-12-29 06:57:38 +0000901 RetVal = DAG.getLoad(RetStoreVT, Chain,
Chris Lattner40758732007-12-29 06:41:28 +0000902 StoreLoc, SrcVal, SrcValOffset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 Chain = RetVal.getValue(1);
Chris Lattner8b815c22007-12-29 06:57:38 +0000904
905 // If we optimized a truncate, then extend the result back to its desired
906 // type.
907 if (RVLocs[0].getValVT() != RetStoreVT)
908 RetVal = DAG.getNode(ISD::FP_EXTEND, RVLocs[0].getValVT(), RetVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 }
910 ResultVals.push_back(RetVal);
911 }
912
913 // Merge everything together with a MERGE_VALUES node.
914 ResultVals.push_back(Chain);
915 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
916 &ResultVals[0], ResultVals.size()).Val;
917}
918
919
920//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000921// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922//===----------------------------------------------------------------------===//
923// StdCall calling convention seems to be standard for many Windows' API
924// routines and around. It differs from C calling convention just a little:
925// callee should clean up the stack, not caller. Symbols should be also
926// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000927// For info on fast calling convention see Fast Calling Convention (tail call)
928// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929
930/// AddLiveIn - This helper function adds the specified physical register to the
931/// MachineFunction as a live in value. It also creates a corresponding virtual
932/// register for it.
933static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
934 const TargetRegisterClass *RC) {
935 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000936 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
937 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 return VReg;
939}
940
Gordon Henriksen18ace102008-01-05 16:56:59 +0000941// Determines whether a CALL node uses struct return semantics.
942static bool CallIsStructReturn(SDOperand Op) {
943 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
944 if (!NumOps)
945 return false;
946
947 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
948 return Flags->getValue() & ISD::ParamFlags::StructReturn;
949}
950
951// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
952static bool ArgsAreStructReturn(SDOperand Op) {
953 unsigned NumArgs = Op.Val->getNumValues() - 1;
954 if (!NumArgs)
955 return false;
956
957 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
958 return Flags->getValue() & ISD::ParamFlags::StructReturn;
959}
960
961// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
962// its own arguments. Callee pop is necessary to support tail calls.
963bool X86TargetLowering::IsCalleePop(SDOperand Op) {
964 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
965 if (IsVarArg)
966 return false;
967
968 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
969 default:
970 return false;
971 case CallingConv::X86_StdCall:
972 return !Subtarget->is64Bit();
973 case CallingConv::X86_FastCall:
974 return !Subtarget->is64Bit();
975 case CallingConv::Fast:
976 return PerformTailCallOpt;
977 }
978}
979
980// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
981CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
982 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
983
984 if (Subtarget->is64Bit())
985 if (CC == CallingConv::Fast && PerformTailCallOpt)
986 return CC_X86_64_TailCall;
987 else
988 return CC_X86_64_C;
989
990 if (CC == CallingConv::X86_FastCall)
991 return CC_X86_32_FastCall;
992 else if (CC == CallingConv::Fast && PerformTailCallOpt)
993 return CC_X86_32_TailCall;
994 else
995 return CC_X86_32_C;
996}
997
998// Selects the appropriate decoration to apply to a MachineFunction containing a
999// given FORMAL_ARGUMENTS node.
1000NameDecorationStyle
1001X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1002 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1003 if (CC == CallingConv::X86_FastCall)
1004 return FastCall;
1005 else if (CC == CallingConv::X86_StdCall)
1006 return StdCall;
1007 return None;
1008}
1009
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001010SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1011 const CCValAssign &VA,
1012 MachineFrameInfo *MFI,
1013 SDOperand Root, unsigned i) {
1014 // Create the nodes corresponding to a load from this parameter slot.
1015 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1016 VA.getLocMemOffset());
1017 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1018
1019 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1020
1021 if (Flags & ISD::ParamFlags::ByVal)
1022 return FIN;
1023 else
1024 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
1025}
1026
Gordon Henriksen18ace102008-01-05 16:56:59 +00001027SDOperand
1028X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001030 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1031
1032 const Function* Fn = MF.getFunction();
1033 if (Fn->hasExternalLinkage() &&
1034 Subtarget->isTargetCygMing() &&
1035 Fn->getName() == "main")
1036 FuncInfo->setForceFramePointer(true);
1037
1038 // Decorate the function name.
1039 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1040
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 MachineFrameInfo *MFI = MF.getFrameInfo();
1042 SDOperand Root = Op.getOperand(0);
1043 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001044 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001045 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001046
1047 assert(!(isVarArg && CC == CallingConv::Fast) &&
1048 "Var args not supported with calling convention fastcc");
1049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 // Assign locations to all of the incoming arguments.
1051 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001052 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001053 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 SmallVector<SDOperand, 8> ArgValues;
1056 unsigned LastVal = ~0U;
1057 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1058 CCValAssign &VA = ArgLocs[i];
1059 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1060 // places.
1061 assert(VA.getValNo() != LastVal &&
1062 "Don't support value assigned to multiple locs yet");
1063 LastVal = VA.getValNo();
1064
1065 if (VA.isRegLoc()) {
1066 MVT::ValueType RegVT = VA.getLocVT();
1067 TargetRegisterClass *RC;
1068 if (RegVT == MVT::i32)
1069 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001070 else if (Is64Bit && RegVT == MVT::i64)
1071 RC = X86::GR64RegisterClass;
1072 else if (Is64Bit && RegVT == MVT::f32)
1073 RC = X86::FR32RegisterClass;
1074 else if (Is64Bit && RegVT == MVT::f64)
1075 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 else {
1077 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001078 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1079 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1080 RegVT = MVT::i64;
1081 } else
1082 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001084
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1086 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1087
1088 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1089 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1090 // right size.
1091 if (VA.getLocInfo() == CCValAssign::SExt)
1092 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1093 DAG.getValueType(VA.getValVT()));
1094 else if (VA.getLocInfo() == CCValAssign::ZExt)
1095 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1096 DAG.getValueType(VA.getValVT()));
1097
1098 if (VA.getLocInfo() != CCValAssign::Full)
1099 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1100
Gordon Henriksen18ace102008-01-05 16:56:59 +00001101 // Handle MMX values passed in GPRs.
1102 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1103 MVT::getSizeInBits(RegVT) == 64)
1104 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 ArgValues.push_back(ArgValue);
1107 } else {
1108 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001109 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110 }
1111 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001112
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001114 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001115 if (CC == CallingConv::Fast)
1116 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117
1118 // If the function takes variable number of arguments, make a frame index for
1119 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001120 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001121 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1122 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1123 }
1124 if (Is64Bit) {
1125 static const unsigned GPR64ArgRegs[] = {
1126 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1127 };
1128 static const unsigned XMMArgRegs[] = {
1129 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1130 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1131 };
1132
1133 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1134 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1135
1136 // For X86-64, if there are vararg parameters that are passed via
1137 // registers, then we must store them to their spots on the stack so they
1138 // may be loaded by deferencing the result of va_next.
1139 VarArgsGPOffset = NumIntRegs * 8;
1140 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1141 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1142
1143 // Store the integer parameter registers.
1144 SmallVector<SDOperand, 8> MemOps;
1145 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1146 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1147 DAG.getConstant(VarArgsGPOffset,
1148 getPointerTy()));
1149 for (; NumIntRegs != 6; ++NumIntRegs) {
1150 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1151 X86::GR64RegisterClass);
1152 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1153 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1154 MemOps.push_back(Store);
1155 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1156 DAG.getConstant(8, getPointerTy()));
1157 }
1158
1159 // Now store the XMM (fp + vector) parameter registers.
1160 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1161 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1162 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1163 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1164 X86::VR128RegisterClass);
1165 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1166 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1167 MemOps.push_back(Store);
1168 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1169 DAG.getConstant(16, getPointerTy()));
1170 }
1171 if (!MemOps.empty())
1172 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1173 &MemOps[0], MemOps.size());
1174 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001175 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001176
1177 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1178 // arguments and the arguments after the retaddr has been pushed are
1179 // aligned.
1180 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1181 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1182 (StackSize & 7) == 0)
1183 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001185 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001186
Gordon Henriksen18ace102008-01-05 16:56:59 +00001187 // Some CCs need callee pop.
1188 if (IsCalleePop(Op)) {
1189 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001190 BytesCallerReserves = 0;
1191 } else {
1192 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001194 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 BytesCallerReserves = StackSize;
1197 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001198
Gordon Henriksen18ace102008-01-05 16:56:59 +00001199 if (!Is64Bit) {
1200 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1201 if (CC == CallingConv::X86_FastCall)
1202 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1203 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204
Anton Korobeynikove844e472007-08-15 17:12:32 +00001205 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206
1207 // Return the new list of results.
1208 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1209 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1210}
1211
Gordon Henriksen18ace102008-01-05 16:56:59 +00001212SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1213 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001215 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001217 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1218 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001221
1222 assert(!(isVarArg && CC == CallingConv::Fast) &&
1223 "Var args not supported with calling convention fastcc");
1224
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225 // Analyze operands of the call, assigning locations to each operand.
1226 SmallVector<CCValAssign, 16> ArgLocs;
1227 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001228 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229
1230 // Get a count of how many bytes are to be pushed on the stack.
1231 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001232 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001233 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234
Gordon Henriksen18ace102008-01-05 16:56:59 +00001235 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1236 // arguments and the arguments after the retaddr has been pushed are aligned.
1237 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1238 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1239 (NumBytes & 7) == 0)
1240 NumBytes += 4;
1241
1242 int FPDiff = 0;
1243 if (IsTailCall) {
1244 // Lower arguments at fp - stackoffset + fpdiff.
1245 unsigned NumBytesCallerPushed =
1246 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1247 FPDiff = NumBytesCallerPushed - NumBytes;
1248
1249 // Set the delta of movement of the returnaddr stackslot.
1250 // But only set if delta is greater than previous delta.
1251 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1252 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1253 }
1254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1256
Gordon Henriksen18ace102008-01-05 16:56:59 +00001257 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1258 if (IsTailCall) {
1259 // Adjust the Return address stack slot.
1260 if (FPDiff) {
1261 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1262 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1263 // Load the "old" Return address.
1264 RetAddrFrIdx =
1265 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1266 // Calculate the new stack slot for the return address.
1267 int SlotSize = Is64Bit ? 8 : 4;
1268 int NewReturnAddrFI =
1269 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1270 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1271 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1272 }
1273 }
1274
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1276 SmallVector<SDOperand, 8> MemOpChains;
1277
1278 SDOperand StackPtr;
1279
1280 // Walk the register/memloc assignments, inserting copies/loads.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001281 // For tail calls, lower arguments first to the stack slot where they would
1282 // normally - in case of a normal function call - be.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1284 CCValAssign &VA = ArgLocs[i];
1285 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1286
1287 // Promote the value if needed.
1288 switch (VA.getLocInfo()) {
1289 default: assert(0 && "Unknown loc info!");
1290 case CCValAssign::Full: break;
1291 case CCValAssign::SExt:
1292 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1293 break;
1294 case CCValAssign::ZExt:
1295 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1296 break;
1297 case CCValAssign::AExt:
1298 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1299 break;
1300 }
1301
1302 if (VA.isRegLoc()) {
1303 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1304 } else {
1305 assert(VA.isMemLoc());
1306 if (StackPtr.Val == 0)
1307 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola007b7142007-09-21 15:50:22 +00001308
1309 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1310 Arg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 }
1312 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313
1314 if (!MemOpChains.empty())
1315 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1316 &MemOpChains[0], MemOpChains.size());
1317
1318 // Build a sequence of copy-to-reg nodes chained together with token chain
1319 // and flag operands which copy the outgoing args into registers.
1320 SDOperand InFlag;
1321 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1322 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1323 InFlag);
1324 InFlag = Chain.getValue(1);
1325 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001326
1327 if (IsTailCall)
1328 InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output?
1329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1331 // GOT pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001332 // Does not work with tail call since ebx is not restored correctly by
1333 // tailcaller. TODO: at least for x86 - verify for x86-64
1334 if (!IsTailCall && !Is64Bit &&
1335 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 Subtarget->isPICStyleGOT()) {
1337 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1338 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1339 InFlag);
1340 InFlag = Chain.getValue(1);
1341 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001342
Gordon Henriksen18ace102008-01-05 16:56:59 +00001343 if (Is64Bit && isVarArg) {
1344 // From AMD64 ABI document:
1345 // For calls that may call functions that use varargs or stdargs
1346 // (prototype-less calls or calls to functions containing ellipsis (...) in
1347 // the declaration) %al is used as hidden argument to specify the number
1348 // of SSE registers used. The contents of %al do not need to match exactly
1349 // the number of registers, but must be an ubound on the number of SSE
1350 // registers used and is in the range 0 - 8 inclusive.
1351
1352 // Count the number of XMM registers allocated.
1353 static const unsigned XMMArgRegs[] = {
1354 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1355 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1356 };
1357 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1358
1359 Chain = DAG.getCopyToReg(Chain, X86::AL,
1360 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1361 InFlag = Chain.getValue(1);
1362 }
1363
1364 // Copy from stack slots to stack slot of a tail called function. This needs
1365 // to be done because if we would lower the arguments directly to their real
1366 // stack slot we might end up overwriting each other.
1367 // TODO: To make this more efficient (sometimes saving a store/load) we could
1368 // analyse the arguments and emit this store/load/store sequence only for
1369 // arguments which would be overwritten otherwise.
1370 if (IsTailCall) {
1371 SmallVector<SDOperand, 8> MemOpChains2;
1372 SDOperand PtrOff;
1373 SDOperand FIN;
1374 int FI = 0;
1375 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1376 CCValAssign &VA = ArgLocs[i];
1377 if (!VA.isRegLoc()) {
1378 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1379 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1380
1381 // Get source stack slot.
1382 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(),
1383 getPointerTy());
1384 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1385 // Create frame index.
1386 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1387 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1388 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1389 FIN = DAG.getFrameIndex(FI, MVT::i32);
1390 if (Flags & ISD::ParamFlags::ByVal) {
1391 // Copy relative to framepointer.
1392 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1393 ISD::ParamFlags::ByValAlignOffs);
1394
1395 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1396 ISD::ParamFlags::ByValSizeOffs;
1397
1398 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1399 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1400 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
1401
1402 MemOpChains2.push_back(DAG.getMemcpy(Chain, FIN, PtrOff, SizeNode,
1403 AlignNode,AlwaysInline));
1404 } else {
1405 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff,
1406 NULL, 0);
1407 // Store relative to framepointer.
1408 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1409 }
1410 }
1411 }
1412
1413 if (!MemOpChains2.empty())
1414 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1415 &MemOpChains2[0], MemOpChains.size());
1416
1417 // Store the return address to the appropriate stack slot.
1418 if (FPDiff)
1419 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1420 }
1421
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422 // If the callee is a GlobalAddress node (quite common, every direct call is)
1423 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1424 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1425 // We should use extra load for direct calls to dllimported functions in
1426 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001427 if ((IsTailCall || !Is64Bit ||
1428 getTargetMachine().getCodeModel() != CodeModel::Large)
1429 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1430 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001432 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001433 if (IsTailCall || !Is64Bit ||
1434 getTargetMachine().getCodeModel() != CodeModel::Large)
1435 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1436 } else if (IsTailCall) {
1437 assert(Callee.getOpcode() == ISD::LOAD &&
1438 "Function destination must be loaded into virtual register");
1439 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1440
1441 Chain = DAG.getCopyToReg(Chain,
1442 DAG.getRegister(Opc, getPointerTy()) ,
1443 Callee,InFlag);
1444 Callee = DAG.getRegister(Opc, getPointerTy());
1445 // Add register as live out.
1446 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001447 }
1448
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 // Returns a chain & a flag for retval copy to use.
1450 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1451 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001452
1453 if (IsTailCall) {
1454 Ops.push_back(Chain);
1455 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1456 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1457 if (InFlag.Val)
1458 Ops.push_back(InFlag);
1459 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1460 InFlag = Chain.getValue(1);
1461
1462 // Returns a chain & a flag for retval copy to use.
1463 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1464 Ops.clear();
1465 }
1466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001467 Ops.push_back(Chain);
1468 Ops.push_back(Callee);
1469
Gordon Henriksen18ace102008-01-05 16:56:59 +00001470 if (IsTailCall)
1471 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472
1473 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001474 if (!IsTailCall && !Is64Bit &&
1475 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001476 Subtarget->isPICStyleGOT())
1477 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001478
Gordon Henriksen18ace102008-01-05 16:56:59 +00001479 // Add argument registers to the end of the list so that they are known live
1480 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001481 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1482 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1483 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001484
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485 if (InFlag.Val)
1486 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001487
Gordon Henriksen18ace102008-01-05 16:56:59 +00001488 if (IsTailCall) {
1489 assert(InFlag.Val &&
1490 "Flag must be set. Depend on flag being set in LowerRET");
1491 Chain = DAG.getNode(X86ISD::TAILCALL,
1492 Op.Val->getVTList(), &Ops[0], Ops.size());
1493
1494 return SDOperand(Chain.Val, Op.ResNo);
1495 }
1496
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001497 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498 InFlag = Chain.getValue(1);
1499
1500 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001501 unsigned NumBytesForCalleeToPush;
1502 if (IsCalleePop(Op))
1503 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
1504 else if (!Is64Bit && CallIsStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 // If this is is a call to a struct-return function, the callee
1506 // pops the hidden struct pointer, so we have to push it back.
1507 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001508 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001509 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001510 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001511
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001512 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001513 Chain = DAG.getCALLSEQ_END(Chain,
1514 DAG.getConstant(NumBytes, getPointerTy()),
1515 DAG.getConstant(NumBytesForCalleeToPush,
1516 getPointerTy()),
1517 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 InFlag = Chain.getValue(1);
1519
1520 // Handle result values, copying them out of physregs into vregs that we
1521 // return.
1522 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1523}
1524
1525
1526//===----------------------------------------------------------------------===//
1527// FastCall Calling Convention implementation
1528//===----------------------------------------------------------------------===//
1529//
1530// The X86 'fastcall' calling convention passes up to two integer arguments in
1531// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1532// and requires that the callee pop its arguments off the stack (allowing proper
1533// tail calls), and has the same return value conventions as C calling convs.
1534//
1535// This calling convention always arranges for the callee pop value to be 8n+4
1536// bytes, which is needed for tail recursion elimination and stack alignment
1537// reasons.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001538
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001539SDOperand
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001540X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1541 const SDOperand &StackPtr,
1542 const CCValAssign &VA,
1543 SDOperand Chain,
1544 SDOperand Arg) {
1545 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1546 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1547 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1548 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1549 if (Flags & ISD::ParamFlags::ByVal) {
1550 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1551 ISD::ParamFlags::ByValAlignOffs);
1552
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001553 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1554 ISD::ParamFlags::ByValSizeOffs;
1555
1556 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1557 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00001558 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001559
Rafael Espindola80825902007-10-19 10:41:11 +00001560 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1561 AlwaysInline);
Rafael Espindoladdb88da2007-08-31 15:06:30 +00001562 } else {
1563 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1564 }
1565}
1566
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001567//===----------------------------------------------------------------------===//
1568// Fast Calling Convention (tail call) implementation
1569//===----------------------------------------------------------------------===//
1570
1571// Like std call, callee cleans arguments, convention except that ECX is
1572// reserved for storing the tail called function address. Only 2 registers are
1573// free for argument passing (inreg). Tail call optimization is performed
1574// provided:
1575// * tailcallopt is enabled
1576// * caller/callee are fastcc
1577// * elf/pic is disabled OR
1578// * elf/pic enabled + callee is in module + callee has
1579// visibility protected or hidden
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001580// To keep the stack aligned according to platform abi the function
1581// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1582// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001583// If a tail called function callee has more arguments than the caller the
1584// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001585// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001586// original REtADDR, but before the saved framepointer or the spilled registers
1587// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1588// stack layout:
1589// arg1
1590// arg2
1591// RETADDR
1592// [ new RETADDR
1593// move area ]
1594// (possible EBP)
1595// ESI
1596// EDI
1597// local1 ..
1598
1599/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1600/// for a 16 byte align requirement.
1601unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1602 SelectionDAG& DAG) {
1603 if (PerformTailCallOpt) {
1604 MachineFunction &MF = DAG.getMachineFunction();
1605 const TargetMachine &TM = MF.getTarget();
1606 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1607 unsigned StackAlignment = TFI.getStackAlignment();
1608 uint64_t AlignMask = StackAlignment - 1;
1609 int64_t Offset = StackSize;
1610 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1611 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1612 // Number smaller than 12 so just add the difference.
1613 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1614 } else {
1615 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1616 Offset = ((~AlignMask) & Offset) + StackAlignment +
1617 (StackAlignment-SlotSize);
1618 }
1619 StackSize = Offset;
1620 }
1621 return StackSize;
1622}
1623
1624/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001625/// following the call is a return. A function is eligible if caller/callee
1626/// calling conventions match, currently only fastcc supports tail calls, and
1627/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001628bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1629 SDOperand Ret,
1630 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001631 if (!PerformTailCallOpt)
1632 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001633
1634 // Check whether CALL node immediatly preceeds the RET node and whether the
1635 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001636 unsigned NumOps = Ret.getNumOperands();
1637 if ((NumOps == 1 &&
1638 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1639 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001640 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001641 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1642 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001643 MachineFunction &MF = DAG.getMachineFunction();
1644 unsigned CallerCC = MF.getFunction()->getCallingConv();
1645 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1646 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1647 SDOperand Callee = Call.getOperand(4);
1648 // On elf/pic %ebx needs to be livein.
Evan Chenge7a87392007-11-02 01:26:22 +00001649 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1650 !Subtarget->isPICStyleGOT())
1651 return true;
1652
1653 // Can only do local tail calls with PIC.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1655 return G->getGlobal()->hasHiddenVisibility()
1656 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001657 }
1658 }
Evan Chenge7a87392007-11-02 01:26:22 +00001659
1660 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001661}
1662
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663//===----------------------------------------------------------------------===//
1664// Other Lowering Hooks
1665//===----------------------------------------------------------------------===//
1666
1667
1668SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001669 MachineFunction &MF = DAG.getMachineFunction();
1670 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1671 int ReturnAddrIndex = FuncInfo->getRAIndex();
1672
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001673 if (ReturnAddrIndex == 0) {
1674 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 if (Subtarget->is64Bit())
1676 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1677 else
1678 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001679
1680 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 }
1682
1683 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1684}
1685
1686
1687
1688/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1689/// specific condition code. It returns a false if it cannot do a direct
1690/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1691/// needed.
1692static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1693 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1694 SelectionDAG &DAG) {
1695 X86CC = X86::COND_INVALID;
1696 if (!isFP) {
1697 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1698 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1699 // X > -1 -> X == 0, jump !sign.
1700 RHS = DAG.getConstant(0, RHS.getValueType());
1701 X86CC = X86::COND_NS;
1702 return true;
1703 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1704 // X < 0 -> X == 0, jump on sign.
1705 X86CC = X86::COND_S;
1706 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001707 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1708 // X < 1 -> X <= 0
1709 RHS = DAG.getConstant(0, RHS.getValueType());
1710 X86CC = X86::COND_LE;
1711 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001712 }
1713 }
1714
1715 switch (SetCCOpcode) {
1716 default: break;
1717 case ISD::SETEQ: X86CC = X86::COND_E; break;
1718 case ISD::SETGT: X86CC = X86::COND_G; break;
1719 case ISD::SETGE: X86CC = X86::COND_GE; break;
1720 case ISD::SETLT: X86CC = X86::COND_L; break;
1721 case ISD::SETLE: X86CC = X86::COND_LE; break;
1722 case ISD::SETNE: X86CC = X86::COND_NE; break;
1723 case ISD::SETULT: X86CC = X86::COND_B; break;
1724 case ISD::SETUGT: X86CC = X86::COND_A; break;
1725 case ISD::SETULE: X86CC = X86::COND_BE; break;
1726 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1727 }
1728 } else {
1729 // On a floating point condition, the flags are set as follows:
1730 // ZF PF CF op
1731 // 0 | 0 | 0 | X > Y
1732 // 0 | 0 | 1 | X < Y
1733 // 1 | 0 | 0 | X == Y
1734 // 1 | 1 | 1 | unordered
1735 bool Flip = false;
1736 switch (SetCCOpcode) {
1737 default: break;
1738 case ISD::SETUEQ:
1739 case ISD::SETEQ: X86CC = X86::COND_E; break;
1740 case ISD::SETOLT: Flip = true; // Fallthrough
1741 case ISD::SETOGT:
1742 case ISD::SETGT: X86CC = X86::COND_A; break;
1743 case ISD::SETOLE: Flip = true; // Fallthrough
1744 case ISD::SETOGE:
1745 case ISD::SETGE: X86CC = X86::COND_AE; break;
1746 case ISD::SETUGT: Flip = true; // Fallthrough
1747 case ISD::SETULT:
1748 case ISD::SETLT: X86CC = X86::COND_B; break;
1749 case ISD::SETUGE: Flip = true; // Fallthrough
1750 case ISD::SETULE:
1751 case ISD::SETLE: X86CC = X86::COND_BE; break;
1752 case ISD::SETONE:
1753 case ISD::SETNE: X86CC = X86::COND_NE; break;
1754 case ISD::SETUO: X86CC = X86::COND_P; break;
1755 case ISD::SETO: X86CC = X86::COND_NP; break;
1756 }
1757 if (Flip)
1758 std::swap(LHS, RHS);
1759 }
1760
1761 return X86CC != X86::COND_INVALID;
1762}
1763
1764/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1765/// code. Current x86 isa includes the following FP cmov instructions:
1766/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1767static bool hasFPCMov(unsigned X86CC) {
1768 switch (X86CC) {
1769 default:
1770 return false;
1771 case X86::COND_B:
1772 case X86::COND_BE:
1773 case X86::COND_E:
1774 case X86::COND_P:
1775 case X86::COND_A:
1776 case X86::COND_AE:
1777 case X86::COND_NE:
1778 case X86::COND_NP:
1779 return true;
1780 }
1781}
1782
1783/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1784/// true if Op is undef or if its value falls within the specified range (L, H].
1785static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1786 if (Op.getOpcode() == ISD::UNDEF)
1787 return true;
1788
1789 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1790 return (Val >= Low && Val < Hi);
1791}
1792
1793/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1794/// true if Op is undef or if its value equal to the specified value.
1795static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1796 if (Op.getOpcode() == ISD::UNDEF)
1797 return true;
1798 return cast<ConstantSDNode>(Op)->getValue() == Val;
1799}
1800
1801/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1802/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1803bool X86::isPSHUFDMask(SDNode *N) {
1804 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1805
Dan Gohman7dc19012007-08-02 21:17:01 +00001806 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001807 return false;
1808
1809 // Check if the value doesn't reference the second vector.
1810 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1811 SDOperand Arg = N->getOperand(i);
1812 if (Arg.getOpcode() == ISD::UNDEF) continue;
1813 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00001814 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001815 return false;
1816 }
1817
1818 return true;
1819}
1820
1821/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1822/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1823bool X86::isPSHUFHWMask(SDNode *N) {
1824 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1825
1826 if (N->getNumOperands() != 8)
1827 return false;
1828
1829 // Lower quadword copied in order.
1830 for (unsigned i = 0; i != 4; ++i) {
1831 SDOperand Arg = N->getOperand(i);
1832 if (Arg.getOpcode() == ISD::UNDEF) continue;
1833 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1834 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1835 return false;
1836 }
1837
1838 // Upper quadword shuffled.
1839 for (unsigned i = 4; i != 8; ++i) {
1840 SDOperand Arg = N->getOperand(i);
1841 if (Arg.getOpcode() == ISD::UNDEF) continue;
1842 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1843 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1844 if (Val < 4 || Val > 7)
1845 return false;
1846 }
1847
1848 return true;
1849}
1850
1851/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1852/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1853bool X86::isPSHUFLWMask(SDNode *N) {
1854 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1855
1856 if (N->getNumOperands() != 8)
1857 return false;
1858
1859 // Upper quadword copied in order.
1860 for (unsigned i = 4; i != 8; ++i)
1861 if (!isUndefOrEqual(N->getOperand(i), i))
1862 return false;
1863
1864 // Lower quadword shuffled.
1865 for (unsigned i = 0; i != 4; ++i)
1866 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1867 return false;
1868
1869 return true;
1870}
1871
1872/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1873/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1874static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1875 if (NumElems != 2 && NumElems != 4) return false;
1876
1877 unsigned Half = NumElems / 2;
1878 for (unsigned i = 0; i < Half; ++i)
1879 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1880 return false;
1881 for (unsigned i = Half; i < NumElems; ++i)
1882 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1883 return false;
1884
1885 return true;
1886}
1887
1888bool X86::isSHUFPMask(SDNode *N) {
1889 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1890 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1891}
1892
1893/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
1894/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1895/// half elements to come from vector 1 (which would equal the dest.) and
1896/// the upper half to come from vector 2.
1897static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1898 if (NumOps != 2 && NumOps != 4) return false;
1899
1900 unsigned Half = NumOps / 2;
1901 for (unsigned i = 0; i < Half; ++i)
1902 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1903 return false;
1904 for (unsigned i = Half; i < NumOps; ++i)
1905 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1906 return false;
1907 return true;
1908}
1909
1910static bool isCommutedSHUFP(SDNode *N) {
1911 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1912 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1913}
1914
1915/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1916/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1917bool X86::isMOVHLPSMask(SDNode *N) {
1918 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1919
1920 if (N->getNumOperands() != 4)
1921 return false;
1922
1923 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1924 return isUndefOrEqual(N->getOperand(0), 6) &&
1925 isUndefOrEqual(N->getOperand(1), 7) &&
1926 isUndefOrEqual(N->getOperand(2), 2) &&
1927 isUndefOrEqual(N->getOperand(3), 3);
1928}
1929
1930/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1931/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1932/// <2, 3, 2, 3>
1933bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1934 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1935
1936 if (N->getNumOperands() != 4)
1937 return false;
1938
1939 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1940 return isUndefOrEqual(N->getOperand(0), 2) &&
1941 isUndefOrEqual(N->getOperand(1), 3) &&
1942 isUndefOrEqual(N->getOperand(2), 2) &&
1943 isUndefOrEqual(N->getOperand(3), 3);
1944}
1945
1946/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1947/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1948bool X86::isMOVLPMask(SDNode *N) {
1949 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1950
1951 unsigned NumElems = N->getNumOperands();
1952 if (NumElems != 2 && NumElems != 4)
1953 return false;
1954
1955 for (unsigned i = 0; i < NumElems/2; ++i)
1956 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1957 return false;
1958
1959 for (unsigned i = NumElems/2; i < NumElems; ++i)
1960 if (!isUndefOrEqual(N->getOperand(i), i))
1961 return false;
1962
1963 return true;
1964}
1965
1966/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1967/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1968/// and MOVLHPS.
1969bool X86::isMOVHPMask(SDNode *N) {
1970 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1971
1972 unsigned NumElems = N->getNumOperands();
1973 if (NumElems != 2 && NumElems != 4)
1974 return false;
1975
1976 for (unsigned i = 0; i < NumElems/2; ++i)
1977 if (!isUndefOrEqual(N->getOperand(i), i))
1978 return false;
1979
1980 for (unsigned i = 0; i < NumElems/2; ++i) {
1981 SDOperand Arg = N->getOperand(i + NumElems/2);
1982 if (!isUndefOrEqual(Arg, i + NumElems))
1983 return false;
1984 }
1985
1986 return true;
1987}
1988
1989/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1990/// specifies a shuffle of elements that is suitable for input to UNPCKL.
1991bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1992 bool V2IsSplat = false) {
1993 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
1994 return false;
1995
1996 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1997 SDOperand BitI = Elts[i];
1998 SDOperand BitI1 = Elts[i+1];
1999 if (!isUndefOrEqual(BitI, j))
2000 return false;
2001 if (V2IsSplat) {
2002 if (isUndefOrEqual(BitI1, NumElts))
2003 return false;
2004 } else {
2005 if (!isUndefOrEqual(BitI1, j + NumElts))
2006 return false;
2007 }
2008 }
2009
2010 return true;
2011}
2012
2013bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2014 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2015 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2016}
2017
2018/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2019/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2020bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2021 bool V2IsSplat = false) {
2022 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2023 return false;
2024
2025 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2026 SDOperand BitI = Elts[i];
2027 SDOperand BitI1 = Elts[i+1];
2028 if (!isUndefOrEqual(BitI, j + NumElts/2))
2029 return false;
2030 if (V2IsSplat) {
2031 if (isUndefOrEqual(BitI1, NumElts))
2032 return false;
2033 } else {
2034 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2035 return false;
2036 }
2037 }
2038
2039 return true;
2040}
2041
2042bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2043 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2044 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2045}
2046
2047/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2048/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2049/// <0, 0, 1, 1>
2050bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2051 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2052
2053 unsigned NumElems = N->getNumOperands();
2054 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2055 return false;
2056
2057 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2058 SDOperand BitI = N->getOperand(i);
2059 SDOperand BitI1 = N->getOperand(i+1);
2060
2061 if (!isUndefOrEqual(BitI, j))
2062 return false;
2063 if (!isUndefOrEqual(BitI1, j))
2064 return false;
2065 }
2066
2067 return true;
2068}
2069
2070/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2071/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2072/// <2, 2, 3, 3>
2073bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2074 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2075
2076 unsigned NumElems = N->getNumOperands();
2077 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2078 return false;
2079
2080 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2081 SDOperand BitI = N->getOperand(i);
2082 SDOperand BitI1 = N->getOperand(i + 1);
2083
2084 if (!isUndefOrEqual(BitI, j))
2085 return false;
2086 if (!isUndefOrEqual(BitI1, j))
2087 return false;
2088 }
2089
2090 return true;
2091}
2092
2093/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2094/// specifies a shuffle of elements that is suitable for input to MOVSS,
2095/// MOVSD, and MOVD, i.e. setting the lowest element.
2096static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002097 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002098 return false;
2099
2100 if (!isUndefOrEqual(Elts[0], NumElts))
2101 return false;
2102
2103 for (unsigned i = 1; i < NumElts; ++i) {
2104 if (!isUndefOrEqual(Elts[i], i))
2105 return false;
2106 }
2107
2108 return true;
2109}
2110
2111bool X86::isMOVLMask(SDNode *N) {
2112 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2113 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2114}
2115
2116/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2117/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2118/// element of vector 2 and the other elements to come from vector 1 in order.
2119static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2120 bool V2IsSplat = false,
2121 bool V2IsUndef = false) {
2122 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2123 return false;
2124
2125 if (!isUndefOrEqual(Ops[0], 0))
2126 return false;
2127
2128 for (unsigned i = 1; i < NumOps; ++i) {
2129 SDOperand Arg = Ops[i];
2130 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2131 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2132 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2133 return false;
2134 }
2135
2136 return true;
2137}
2138
2139static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2140 bool V2IsUndef = false) {
2141 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2142 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2143 V2IsSplat, V2IsUndef);
2144}
2145
2146/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2147/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2148bool X86::isMOVSHDUPMask(SDNode *N) {
2149 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2150
2151 if (N->getNumOperands() != 4)
2152 return false;
2153
2154 // Expect 1, 1, 3, 3
2155 for (unsigned i = 0; i < 2; ++i) {
2156 SDOperand Arg = N->getOperand(i);
2157 if (Arg.getOpcode() == ISD::UNDEF) continue;
2158 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2159 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2160 if (Val != 1) return false;
2161 }
2162
2163 bool HasHi = false;
2164 for (unsigned i = 2; i < 4; ++i) {
2165 SDOperand Arg = N->getOperand(i);
2166 if (Arg.getOpcode() == ISD::UNDEF) continue;
2167 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2168 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2169 if (Val != 3) return false;
2170 HasHi = true;
2171 }
2172
2173 // Don't use movshdup if it can be done with a shufps.
2174 return HasHi;
2175}
2176
2177/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2178/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2179bool X86::isMOVSLDUPMask(SDNode *N) {
2180 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2181
2182 if (N->getNumOperands() != 4)
2183 return false;
2184
2185 // Expect 0, 0, 2, 2
2186 for (unsigned i = 0; i < 2; ++i) {
2187 SDOperand Arg = N->getOperand(i);
2188 if (Arg.getOpcode() == ISD::UNDEF) continue;
2189 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2190 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2191 if (Val != 0) return false;
2192 }
2193
2194 bool HasHi = false;
2195 for (unsigned i = 2; i < 4; ++i) {
2196 SDOperand Arg = N->getOperand(i);
2197 if (Arg.getOpcode() == ISD::UNDEF) continue;
2198 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2199 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2200 if (Val != 2) return false;
2201 HasHi = true;
2202 }
2203
2204 // Don't use movshdup if it can be done with a shufps.
2205 return HasHi;
2206}
2207
2208/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2209/// specifies a identity operation on the LHS or RHS.
2210static bool isIdentityMask(SDNode *N, bool RHS = false) {
2211 unsigned NumElems = N->getNumOperands();
2212 for (unsigned i = 0; i < NumElems; ++i)
2213 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2214 return false;
2215 return true;
2216}
2217
2218/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2219/// a splat of a single element.
2220static bool isSplatMask(SDNode *N) {
2221 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2222
2223 // This is a splat operation if each element of the permute is the same, and
2224 // if the value doesn't reference the second vector.
2225 unsigned NumElems = N->getNumOperands();
2226 SDOperand ElementBase;
2227 unsigned i = 0;
2228 for (; i != NumElems; ++i) {
2229 SDOperand Elt = N->getOperand(i);
2230 if (isa<ConstantSDNode>(Elt)) {
2231 ElementBase = Elt;
2232 break;
2233 }
2234 }
2235
2236 if (!ElementBase.Val)
2237 return false;
2238
2239 for (; i != NumElems; ++i) {
2240 SDOperand Arg = N->getOperand(i);
2241 if (Arg.getOpcode() == ISD::UNDEF) continue;
2242 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2243 if (Arg != ElementBase) return false;
2244 }
2245
2246 // Make sure it is a splat of the first vector operand.
2247 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2248}
2249
2250/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2251/// a splat of a single element and it's a 2 or 4 element mask.
2252bool X86::isSplatMask(SDNode *N) {
2253 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2254
2255 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2256 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2257 return false;
2258 return ::isSplatMask(N);
2259}
2260
2261/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2262/// specifies a splat of zero element.
2263bool X86::isSplatLoMask(SDNode *N) {
2264 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2265
2266 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2267 if (!isUndefOrEqual(N->getOperand(i), 0))
2268 return false;
2269 return true;
2270}
2271
2272/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2273/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2274/// instructions.
2275unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2276 unsigned NumOperands = N->getNumOperands();
2277 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2278 unsigned Mask = 0;
2279 for (unsigned i = 0; i < NumOperands; ++i) {
2280 unsigned Val = 0;
2281 SDOperand Arg = N->getOperand(NumOperands-i-1);
2282 if (Arg.getOpcode() != ISD::UNDEF)
2283 Val = cast<ConstantSDNode>(Arg)->getValue();
2284 if (Val >= NumOperands) Val -= NumOperands;
2285 Mask |= Val;
2286 if (i != NumOperands - 1)
2287 Mask <<= Shift;
2288 }
2289
2290 return Mask;
2291}
2292
2293/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2294/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2295/// instructions.
2296unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2297 unsigned Mask = 0;
2298 // 8 nodes, but we only care about the last 4.
2299 for (unsigned i = 7; i >= 4; --i) {
2300 unsigned Val = 0;
2301 SDOperand Arg = N->getOperand(i);
2302 if (Arg.getOpcode() != ISD::UNDEF)
2303 Val = cast<ConstantSDNode>(Arg)->getValue();
2304 Mask |= (Val - 4);
2305 if (i != 4)
2306 Mask <<= 2;
2307 }
2308
2309 return Mask;
2310}
2311
2312/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2313/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2314/// instructions.
2315unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2316 unsigned Mask = 0;
2317 // 8 nodes, but we only care about the first 4.
2318 for (int i = 3; i >= 0; --i) {
2319 unsigned Val = 0;
2320 SDOperand Arg = N->getOperand(i);
2321 if (Arg.getOpcode() != ISD::UNDEF)
2322 Val = cast<ConstantSDNode>(Arg)->getValue();
2323 Mask |= Val;
2324 if (i != 0)
2325 Mask <<= 2;
2326 }
2327
2328 return Mask;
2329}
2330
2331/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2332/// specifies a 8 element shuffle that can be broken into a pair of
2333/// PSHUFHW and PSHUFLW.
2334static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2335 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2336
2337 if (N->getNumOperands() != 8)
2338 return false;
2339
2340 // Lower quadword shuffled.
2341 for (unsigned i = 0; i != 4; ++i) {
2342 SDOperand Arg = N->getOperand(i);
2343 if (Arg.getOpcode() == ISD::UNDEF) continue;
2344 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2345 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002346 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 return false;
2348 }
2349
2350 // Upper quadword shuffled.
2351 for (unsigned i = 4; i != 8; ++i) {
2352 SDOperand Arg = N->getOperand(i);
2353 if (Arg.getOpcode() == ISD::UNDEF) continue;
2354 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2355 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2356 if (Val < 4 || Val > 7)
2357 return false;
2358 }
2359
2360 return true;
2361}
2362
Chris Lattnere6aa3862007-11-25 00:24:49 +00002363/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002364/// values in ther permute mask.
2365static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2366 SDOperand &V2, SDOperand &Mask,
2367 SelectionDAG &DAG) {
2368 MVT::ValueType VT = Op.getValueType();
2369 MVT::ValueType MaskVT = Mask.getValueType();
2370 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2371 unsigned NumElems = Mask.getNumOperands();
2372 SmallVector<SDOperand, 8> MaskVec;
2373
2374 for (unsigned i = 0; i != NumElems; ++i) {
2375 SDOperand Arg = Mask.getOperand(i);
2376 if (Arg.getOpcode() == ISD::UNDEF) {
2377 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2378 continue;
2379 }
2380 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2381 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2382 if (Val < NumElems)
2383 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2384 else
2385 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2386 }
2387
2388 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002389 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2391}
2392
Evan Chenga6769df2007-12-07 21:30:01 +00002393/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2394/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002395static
2396SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2397 MVT::ValueType MaskVT = Mask.getValueType();
2398 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2399 unsigned NumElems = Mask.getNumOperands();
2400 SmallVector<SDOperand, 8> MaskVec;
2401 for (unsigned i = 0; i != NumElems; ++i) {
2402 SDOperand Arg = Mask.getOperand(i);
2403 if (Arg.getOpcode() == ISD::UNDEF) {
2404 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2405 continue;
2406 }
2407 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2408 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2409 if (Val < NumElems)
2410 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2411 else
2412 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2413 }
2414 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2415}
2416
2417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002418/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2419/// match movhlps. The lower half elements should come from upper half of
2420/// V1 (and in order), and the upper half elements should come from the upper
2421/// half of V2 (and in order).
2422static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2423 unsigned NumElems = Mask->getNumOperands();
2424 if (NumElems != 4)
2425 return false;
2426 for (unsigned i = 0, e = 2; i != e; ++i)
2427 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2428 return false;
2429 for (unsigned i = 2; i != 4; ++i)
2430 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2431 return false;
2432 return true;
2433}
2434
2435/// isScalarLoadToVector - Returns true if the node is a scalar load that
2436/// is promoted to a vector.
2437static inline bool isScalarLoadToVector(SDNode *N) {
2438 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2439 N = N->getOperand(0).Val;
2440 return ISD::isNON_EXTLoad(N);
2441 }
2442 return false;
2443}
2444
2445/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2446/// match movlp{s|d}. The lower half elements should come from lower half of
2447/// V1 (and in order), and the upper half elements should come from the upper
2448/// half of V2 (and in order). And since V1 will become the source of the
2449/// MOVLP, it must be either a vector load or a scalar load to vector.
2450static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2451 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2452 return false;
2453 // Is V2 is a vector load, don't do this transformation. We will try to use
2454 // load folding shufps op.
2455 if (ISD::isNON_EXTLoad(V2))
2456 return false;
2457
2458 unsigned NumElems = Mask->getNumOperands();
2459 if (NumElems != 2 && NumElems != 4)
2460 return false;
2461 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2462 if (!isUndefOrEqual(Mask->getOperand(i), i))
2463 return false;
2464 for (unsigned i = NumElems/2; i != NumElems; ++i)
2465 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2466 return false;
2467 return true;
2468}
2469
2470/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2471/// all the same.
2472static bool isSplatVector(SDNode *N) {
2473 if (N->getOpcode() != ISD::BUILD_VECTOR)
2474 return false;
2475
2476 SDOperand SplatValue = N->getOperand(0);
2477 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2478 if (N->getOperand(i) != SplatValue)
2479 return false;
2480 return true;
2481}
2482
2483/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2484/// to an undef.
2485static bool isUndefShuffle(SDNode *N) {
2486 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2487 return false;
2488
2489 SDOperand V1 = N->getOperand(0);
2490 SDOperand V2 = N->getOperand(1);
2491 SDOperand Mask = N->getOperand(2);
2492 unsigned NumElems = Mask.getNumOperands();
2493 for (unsigned i = 0; i != NumElems; ++i) {
2494 SDOperand Arg = Mask.getOperand(i);
2495 if (Arg.getOpcode() != ISD::UNDEF) {
2496 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2497 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2498 return false;
2499 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2500 return false;
2501 }
2502 }
2503 return true;
2504}
2505
2506/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2507/// constant +0.0.
2508static inline bool isZeroNode(SDOperand Elt) {
2509 return ((isa<ConstantSDNode>(Elt) &&
2510 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2511 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002512 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513}
2514
2515/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2516/// to an zero vector.
2517static bool isZeroShuffle(SDNode *N) {
2518 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2519 return false;
2520
2521 SDOperand V1 = N->getOperand(0);
2522 SDOperand V2 = N->getOperand(1);
2523 SDOperand Mask = N->getOperand(2);
2524 unsigned NumElems = Mask.getNumOperands();
2525 for (unsigned i = 0; i != NumElems; ++i) {
2526 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002527 if (Arg.getOpcode() == ISD::UNDEF)
2528 continue;
2529
2530 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2531 if (Idx < NumElems) {
2532 unsigned Opc = V1.Val->getOpcode();
2533 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2534 continue;
2535 if (Opc != ISD::BUILD_VECTOR ||
2536 !isZeroNode(V1.Val->getOperand(Idx)))
2537 return false;
2538 } else if (Idx >= NumElems) {
2539 unsigned Opc = V2.Val->getOpcode();
2540 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2541 continue;
2542 if (Opc != ISD::BUILD_VECTOR ||
2543 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2544 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002545 }
2546 }
2547 return true;
2548}
2549
2550/// getZeroVector - Returns a vector of specified type with all zero elements.
2551///
2552static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2553 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002554
2555 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2556 // type. This ensures they get CSE'd.
2557 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2558 SDOperand Vec;
2559 if (MVT::getSizeInBits(VT) == 64) // MMX
2560 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2561 else // SSE
2562 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2563 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564}
2565
Chris Lattnere6aa3862007-11-25 00:24:49 +00002566/// getOnesVector - Returns a vector of specified type with all bits set.
2567///
2568static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2569 assert(MVT::isVector(VT) && "Expected a vector type");
2570
2571 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2572 // type. This ensures they get CSE'd.
2573 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2574 SDOperand Vec;
2575 if (MVT::getSizeInBits(VT) == 64) // MMX
2576 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2577 else // SSE
2578 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2579 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2580}
2581
2582
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002583/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2584/// that point to V2 points to its first element.
2585static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2586 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2587
2588 bool Changed = false;
2589 SmallVector<SDOperand, 8> MaskVec;
2590 unsigned NumElems = Mask.getNumOperands();
2591 for (unsigned i = 0; i != NumElems; ++i) {
2592 SDOperand Arg = Mask.getOperand(i);
2593 if (Arg.getOpcode() != ISD::UNDEF) {
2594 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2595 if (Val > NumElems) {
2596 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2597 Changed = true;
2598 }
2599 }
2600 MaskVec.push_back(Arg);
2601 }
2602
2603 if (Changed)
2604 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2605 &MaskVec[0], MaskVec.size());
2606 return Mask;
2607}
2608
2609/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2610/// operation of specified width.
2611static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2612 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2613 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2614
2615 SmallVector<SDOperand, 8> MaskVec;
2616 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2617 for (unsigned i = 1; i != NumElems; ++i)
2618 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2619 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2620}
2621
2622/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2623/// of specified width.
2624static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2625 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2626 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2627 SmallVector<SDOperand, 8> MaskVec;
2628 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2629 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2630 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2631 }
2632 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2633}
2634
2635/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2636/// of specified width.
2637static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2638 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2639 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2640 unsigned Half = NumElems/2;
2641 SmallVector<SDOperand, 8> MaskVec;
2642 for (unsigned i = 0; i != Half; ++i) {
2643 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2644 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2645 }
2646 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2647}
2648
2649/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2650///
2651static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2652 SDOperand V1 = Op.getOperand(0);
2653 SDOperand Mask = Op.getOperand(2);
2654 MVT::ValueType VT = Op.getValueType();
2655 unsigned NumElems = Mask.getNumOperands();
2656 Mask = getUnpacklMask(NumElems, DAG);
2657 while (NumElems != 4) {
2658 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2659 NumElems >>= 1;
2660 }
2661 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2662
Chris Lattnere6aa3862007-11-25 00:24:49 +00002663 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002664 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2665 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2666 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2667}
2668
2669/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002670/// vector of zero or undef vector. This produces a shuffle where the low
2671/// element of V2 is swizzled into the zero/undef vector, landing at element
2672/// 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 +00002673static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2674 unsigned NumElems, unsigned Idx,
2675 bool isZero, SelectionDAG &DAG) {
2676 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2677 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2678 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002679 SmallVector<SDOperand, 16> MaskVec;
2680 for (unsigned i = 0; i != NumElems; ++i)
2681 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2682 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2683 else
2684 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002685 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2686 &MaskVec[0], MaskVec.size());
2687 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2688}
2689
2690/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2691///
2692static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2693 unsigned NumNonZero, unsigned NumZero,
2694 SelectionDAG &DAG, TargetLowering &TLI) {
2695 if (NumNonZero > 8)
2696 return SDOperand();
2697
2698 SDOperand V(0, 0);
2699 bool First = true;
2700 for (unsigned i = 0; i < 16; ++i) {
2701 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2702 if (ThisIsNonZero && First) {
2703 if (NumZero)
2704 V = getZeroVector(MVT::v8i16, DAG);
2705 else
2706 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2707 First = false;
2708 }
2709
2710 if ((i & 1) != 0) {
2711 SDOperand ThisElt(0, 0), LastElt(0, 0);
2712 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2713 if (LastIsNonZero) {
2714 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2715 }
2716 if (ThisIsNonZero) {
2717 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2718 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2719 ThisElt, DAG.getConstant(8, MVT::i8));
2720 if (LastIsNonZero)
2721 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2722 } else
2723 ThisElt = LastElt;
2724
2725 if (ThisElt.Val)
2726 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2727 DAG.getConstant(i/2, TLI.getPointerTy()));
2728 }
2729 }
2730
2731 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2732}
2733
2734/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2735///
2736static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2737 unsigned NumNonZero, unsigned NumZero,
2738 SelectionDAG &DAG, TargetLowering &TLI) {
2739 if (NumNonZero > 4)
2740 return SDOperand();
2741
2742 SDOperand V(0, 0);
2743 bool First = true;
2744 for (unsigned i = 0; i < 8; ++i) {
2745 bool isNonZero = (NonZeros & (1 << i)) != 0;
2746 if (isNonZero) {
2747 if (First) {
2748 if (NumZero)
2749 V = getZeroVector(MVT::v8i16, DAG);
2750 else
2751 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2752 First = false;
2753 }
2754 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2755 DAG.getConstant(i, TLI.getPointerTy()));
2756 }
2757 }
2758
2759 return V;
2760}
2761
2762SDOperand
2763X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002764 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2765 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2766 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2767 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2768 // eliminated on x86-32 hosts.
2769 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2770 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002771
Chris Lattnere6aa3862007-11-25 00:24:49 +00002772 if (ISD::isBuildVectorAllOnes(Op.Val))
2773 return getOnesVector(Op.getValueType(), DAG);
2774 return getZeroVector(Op.getValueType(), DAG);
2775 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776
2777 MVT::ValueType VT = Op.getValueType();
2778 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2779 unsigned EVTBits = MVT::getSizeInBits(EVT);
2780
2781 unsigned NumElems = Op.getNumOperands();
2782 unsigned NumZero = 0;
2783 unsigned NumNonZero = 0;
2784 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00002785 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00002786 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002787 for (unsigned i = 0; i < NumElems; ++i) {
2788 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00002789 if (Elt.getOpcode() == ISD::UNDEF)
2790 continue;
2791 Values.insert(Elt);
2792 if (Elt.getOpcode() != ISD::Constant &&
2793 Elt.getOpcode() != ISD::ConstantFP)
2794 HasNonImms = true;
2795 if (isZeroNode(Elt))
2796 NumZero++;
2797 else {
2798 NonZeros |= (1 << i);
2799 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800 }
2801 }
2802
2803 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002804 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2805 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002806 }
2807
2808 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2809 if (Values.size() == 1)
2810 return SDOperand();
2811
2812 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00002813 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814 unsigned Idx = CountTrailingZeros_32(NonZeros);
2815 SDOperand Item = Op.getOperand(Idx);
2816 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2817 if (Idx == 0)
2818 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2819 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2820 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00002821 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
2822 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002823
2824 if (EVTBits == 32) {
2825 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2826 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2827 DAG);
2828 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2829 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2830 SmallVector<SDOperand, 8> MaskVec;
2831 for (unsigned i = 0; i < NumElems; i++)
2832 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2833 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2834 &MaskVec[0], MaskVec.size());
2835 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2836 DAG.getNode(ISD::UNDEF, VT), Mask);
2837 }
2838 }
2839
Dan Gohman21463242007-07-24 22:55:08 +00002840 // A vector full of immediates; various special cases are already
2841 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00002842 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00002843 return SDOperand();
2844
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 // Let legalizer expand 2-wide build_vectors.
2846 if (EVTBits == 64)
2847 return SDOperand();
2848
2849 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2850 if (EVTBits == 8 && NumElems == 16) {
2851 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2852 *this);
2853 if (V.Val) return V;
2854 }
2855
2856 if (EVTBits == 16 && NumElems == 8) {
2857 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2858 *this);
2859 if (V.Val) return V;
2860 }
2861
2862 // If element VT is == 32 bits, turn it into a number of shuffles.
2863 SmallVector<SDOperand, 8> V;
2864 V.resize(NumElems);
2865 if (NumElems == 4 && NumZero > 0) {
2866 for (unsigned i = 0; i < 4; ++i) {
2867 bool isZero = !(NonZeros & (1 << i));
2868 if (isZero)
2869 V[i] = getZeroVector(VT, DAG);
2870 else
2871 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2872 }
2873
2874 for (unsigned i = 0; i < 2; ++i) {
2875 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2876 default: break;
2877 case 0:
2878 V[i] = V[i*2]; // Must be a zero vector.
2879 break;
2880 case 1:
2881 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2882 getMOVLMask(NumElems, DAG));
2883 break;
2884 case 2:
2885 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2886 getMOVLMask(NumElems, DAG));
2887 break;
2888 case 3:
2889 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2890 getUnpacklMask(NumElems, DAG));
2891 break;
2892 }
2893 }
2894
2895 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2896 // clears the upper bits.
2897 // FIXME: we can do the same for v4f32 case when we know both parts of
2898 // the lower half come from scalar_to_vector (loadf32). We should do
2899 // that in post legalizer dag combiner with target specific hooks.
2900 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2901 return V[0];
2902 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2903 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2904 SmallVector<SDOperand, 8> MaskVec;
2905 bool Reverse = (NonZeros & 0x3) == 2;
2906 for (unsigned i = 0; i < 2; ++i)
2907 if (Reverse)
2908 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2909 else
2910 MaskVec.push_back(DAG.getConstant(i, EVT));
2911 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2912 for (unsigned i = 0; i < 2; ++i)
2913 if (Reverse)
2914 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2915 else
2916 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2917 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2918 &MaskVec[0], MaskVec.size());
2919 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2920 }
2921
2922 if (Values.size() > 2) {
2923 // Expand into a number of unpckl*.
2924 // e.g. for v4f32
2925 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2926 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2927 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2928 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2929 for (unsigned i = 0; i < NumElems; ++i)
2930 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2931 NumElems >>= 1;
2932 while (NumElems != 0) {
2933 for (unsigned i = 0; i < NumElems; ++i)
2934 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2935 UnpckMask);
2936 NumElems >>= 1;
2937 }
2938 return V[0];
2939 }
2940
2941 return SDOperand();
2942}
2943
Evan Chengfca29242007-12-07 08:07:39 +00002944static
2945SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
2946 SDOperand PermMask, SelectionDAG &DAG,
2947 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00002948 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00002949 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
2950 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00002951 MVT::ValueType PtrVT = TLI.getPointerTy();
2952 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
2953 PermMask.Val->op_end());
2954
2955 // First record which half of which vector the low elements come from.
2956 SmallVector<unsigned, 4> LowQuad(4);
2957 for (unsigned i = 0; i < 4; ++i) {
2958 SDOperand Elt = MaskElts[i];
2959 if (Elt.getOpcode() == ISD::UNDEF)
2960 continue;
2961 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
2962 int QuadIdx = EltIdx / 4;
2963 ++LowQuad[QuadIdx];
2964 }
2965 int BestLowQuad = -1;
2966 unsigned MaxQuad = 1;
2967 for (unsigned i = 0; i < 4; ++i) {
2968 if (LowQuad[i] > MaxQuad) {
2969 BestLowQuad = i;
2970 MaxQuad = LowQuad[i];
2971 }
Evan Chengfca29242007-12-07 08:07:39 +00002972 }
2973
Evan Cheng75184a92007-12-11 01:46:18 +00002974 // Record which half of which vector the high elements come from.
2975 SmallVector<unsigned, 4> HighQuad(4);
2976 for (unsigned i = 4; i < 8; ++i) {
2977 SDOperand Elt = MaskElts[i];
2978 if (Elt.getOpcode() == ISD::UNDEF)
2979 continue;
2980 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
2981 int QuadIdx = EltIdx / 4;
2982 ++HighQuad[QuadIdx];
2983 }
2984 int BestHighQuad = -1;
2985 MaxQuad = 1;
2986 for (unsigned i = 0; i < 4; ++i) {
2987 if (HighQuad[i] > MaxQuad) {
2988 BestHighQuad = i;
2989 MaxQuad = HighQuad[i];
2990 }
2991 }
2992
2993 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
2994 if (BestLowQuad != -1 || BestHighQuad != -1) {
2995 // First sort the 4 chunks in order using shufpd.
2996 SmallVector<SDOperand, 8> MaskVec;
2997 if (BestLowQuad != -1)
2998 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
2999 else
3000 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3001 if (BestHighQuad != -1)
3002 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3003 else
3004 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3005 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3006 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3007 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3008 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3009 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3010
3011 // Now sort high and low parts separately.
3012 BitVector InOrder(8);
3013 if (BestLowQuad != -1) {
3014 // Sort lower half in order using PSHUFLW.
3015 MaskVec.clear();
3016 bool AnyOutOrder = false;
3017 for (unsigned i = 0; i != 4; ++i) {
3018 SDOperand Elt = MaskElts[i];
3019 if (Elt.getOpcode() == ISD::UNDEF) {
3020 MaskVec.push_back(Elt);
3021 InOrder.set(i);
3022 } else {
3023 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3024 if (EltIdx != i)
3025 AnyOutOrder = true;
3026 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3027 // If this element is in the right place after this shuffle, then
3028 // remember it.
3029 if ((int)(EltIdx / 4) == BestLowQuad)
3030 InOrder.set(i);
3031 }
3032 }
3033 if (AnyOutOrder) {
3034 for (unsigned i = 4; i != 8; ++i)
3035 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3036 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3037 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3038 }
3039 }
3040
3041 if (BestHighQuad != -1) {
3042 // Sort high half in order using PSHUFHW if possible.
3043 MaskVec.clear();
3044 for (unsigned i = 0; i != 4; ++i)
3045 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3046 bool AnyOutOrder = false;
3047 for (unsigned i = 4; i != 8; ++i) {
3048 SDOperand Elt = MaskElts[i];
3049 if (Elt.getOpcode() == ISD::UNDEF) {
3050 MaskVec.push_back(Elt);
3051 InOrder.set(i);
3052 } else {
3053 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3054 if (EltIdx != i)
3055 AnyOutOrder = true;
3056 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3057 // If this element is in the right place after this shuffle, then
3058 // remember it.
3059 if ((int)(EltIdx / 4) == BestHighQuad)
3060 InOrder.set(i);
3061 }
3062 }
3063 if (AnyOutOrder) {
3064 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3065 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3066 }
3067 }
3068
3069 // The other elements are put in the right place using pextrw and pinsrw.
3070 for (unsigned i = 0; i != 8; ++i) {
3071 if (InOrder[i])
3072 continue;
3073 SDOperand Elt = MaskElts[i];
3074 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3075 if (EltIdx == i)
3076 continue;
3077 SDOperand ExtOp = (EltIdx < 8)
3078 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3079 DAG.getConstant(EltIdx, PtrVT))
3080 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3081 DAG.getConstant(EltIdx - 8, PtrVT));
3082 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3083 DAG.getConstant(i, PtrVT));
3084 }
3085 return NewV;
3086 }
3087
3088 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3089 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003090 // First, let's find out how many elements are already in the right order.
3091 unsigned V1InOrder = 0;
3092 unsigned V1FromV1 = 0;
3093 unsigned V2InOrder = 0;
3094 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003095 SmallVector<SDOperand, 8> V1Elts;
3096 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003097 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003098 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003099 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003100 V1Elts.push_back(Elt);
3101 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003102 ++V1InOrder;
3103 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003104 continue;
3105 }
3106 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3107 if (EltIdx == i) {
3108 V1Elts.push_back(Elt);
3109 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3110 ++V1InOrder;
3111 } else if (EltIdx == i+8) {
3112 V1Elts.push_back(Elt);
3113 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3114 ++V2InOrder;
3115 } else if (EltIdx < 8) {
3116 V1Elts.push_back(Elt);
3117 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003118 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003119 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3120 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003121 }
3122 }
3123
3124 if (V2InOrder > V1InOrder) {
3125 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3126 std::swap(V1, V2);
3127 std::swap(V1Elts, V2Elts);
3128 std::swap(V1FromV1, V2FromV2);
3129 }
3130
Evan Cheng75184a92007-12-11 01:46:18 +00003131 if ((V1FromV1 + V1InOrder) != 8) {
3132 // Some elements are from V2.
3133 if (V1FromV1) {
3134 // If there are elements that are from V1 but out of place,
3135 // then first sort them in place
3136 SmallVector<SDOperand, 8> MaskVec;
3137 for (unsigned i = 0; i < 8; ++i) {
3138 SDOperand Elt = V1Elts[i];
3139 if (Elt.getOpcode() == ISD::UNDEF) {
3140 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3141 continue;
3142 }
3143 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3144 if (EltIdx >= 8)
3145 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3146 else
3147 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3148 }
3149 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3150 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003151 }
Evan Cheng75184a92007-12-11 01:46:18 +00003152
3153 NewV = V1;
3154 for (unsigned i = 0; i < 8; ++i) {
3155 SDOperand Elt = V1Elts[i];
3156 if (Elt.getOpcode() == ISD::UNDEF)
3157 continue;
3158 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3159 if (EltIdx < 8)
3160 continue;
3161 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3162 DAG.getConstant(EltIdx - 8, PtrVT));
3163 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3164 DAG.getConstant(i, PtrVT));
3165 }
3166 return NewV;
3167 } else {
3168 // All elements are from V1.
3169 NewV = V1;
3170 for (unsigned i = 0; i < 8; ++i) {
3171 SDOperand Elt = V1Elts[i];
3172 if (Elt.getOpcode() == ISD::UNDEF)
3173 continue;
3174 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3175 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3176 DAG.getConstant(EltIdx, PtrVT));
3177 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3178 DAG.getConstant(i, PtrVT));
3179 }
3180 return NewV;
3181 }
3182}
3183
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003184/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3185/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3186/// done when every pair / quad of shuffle mask elements point to elements in
3187/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003188/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3189static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003190SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3191 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003192 SDOperand PermMask, SelectionDAG &DAG,
3193 TargetLowering &TLI) {
3194 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003195 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3196 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3197 MVT::ValueType NewVT = MaskVT;
3198 switch (VT) {
3199 case MVT::v4f32: NewVT = MVT::v2f64; break;
3200 case MVT::v4i32: NewVT = MVT::v2i64; break;
3201 case MVT::v8i16: NewVT = MVT::v4i32; break;
3202 case MVT::v16i8: NewVT = MVT::v4i32; break;
3203 default: assert(false && "Unexpected!");
3204 }
3205
3206 if (NewWidth == 2)
3207 if (MVT::isInteger(VT))
3208 NewVT = MVT::v2i64;
3209 else
3210 NewVT = MVT::v2f64;
3211 unsigned Scale = NumElems / NewWidth;
3212 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003213 for (unsigned i = 0; i < NumElems; i += Scale) {
3214 unsigned StartIdx = ~0U;
3215 for (unsigned j = 0; j < Scale; ++j) {
3216 SDOperand Elt = PermMask.getOperand(i+j);
3217 if (Elt.getOpcode() == ISD::UNDEF)
3218 continue;
3219 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3220 if (StartIdx == ~0U)
3221 StartIdx = EltIdx - (EltIdx % Scale);
3222 if (EltIdx != StartIdx + j)
3223 return SDOperand();
3224 }
3225 if (StartIdx == ~0U)
3226 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3227 else
3228 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003229 }
3230
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003231 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3232 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3233 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3234 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3235 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003236}
3237
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003238SDOperand
3239X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3240 SDOperand V1 = Op.getOperand(0);
3241 SDOperand V2 = Op.getOperand(1);
3242 SDOperand PermMask = Op.getOperand(2);
3243 MVT::ValueType VT = Op.getValueType();
3244 unsigned NumElems = PermMask.getNumOperands();
3245 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3246 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3247 bool V1IsSplat = false;
3248 bool V2IsSplat = false;
3249
3250 if (isUndefShuffle(Op.Val))
3251 return DAG.getNode(ISD::UNDEF, VT);
3252
3253 if (isZeroShuffle(Op.Val))
3254 return getZeroVector(VT, DAG);
3255
3256 if (isIdentityMask(PermMask.Val))
3257 return V1;
3258 else if (isIdentityMask(PermMask.Val, true))
3259 return V2;
3260
3261 if (isSplatMask(PermMask.Val)) {
3262 if (NumElems <= 4) return Op;
3263 // Promote it to a v4i32 splat.
3264 return PromoteSplat(Op, DAG);
3265 }
3266
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003267 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3268 // do it!
3269 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3270 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3271 if (NewOp.Val)
3272 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3273 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3274 // FIXME: Figure out a cleaner way to do this.
3275 // Try to make use of movq to zero out the top part.
3276 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3277 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3278 if (NewOp.Val) {
3279 SDOperand NewV1 = NewOp.getOperand(0);
3280 SDOperand NewV2 = NewOp.getOperand(1);
3281 SDOperand NewMask = NewOp.getOperand(2);
3282 if (isCommutedMOVL(NewMask.Val, true, false)) {
3283 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3284 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3285 NewV1, NewV2, getMOVLMask(2, DAG));
3286 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3287 }
3288 }
3289 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3290 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3291 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3292 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3293 }
3294 }
3295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 if (X86::isMOVLMask(PermMask.Val))
3297 return (V1IsUndef) ? V2 : Op;
3298
3299 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3300 X86::isMOVSLDUPMask(PermMask.Val) ||
3301 X86::isMOVHLPSMask(PermMask.Val) ||
3302 X86::isMOVHPMask(PermMask.Val) ||
3303 X86::isMOVLPMask(PermMask.Val))
3304 return Op;
3305
3306 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3307 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3308 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3309
3310 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003311 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3312 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003313 V1IsSplat = isSplatVector(V1.Val);
3314 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003315
3316 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003317 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3318 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3319 std::swap(V1IsSplat, V2IsSplat);
3320 std::swap(V1IsUndef, V2IsUndef);
3321 Commuted = true;
3322 }
3323
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003324 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003325 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3326 if (V2IsUndef) return V1;
3327 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3328 if (V2IsSplat) {
3329 // V2 is a splat, so the mask may be malformed. That is, it may point
3330 // to any V2 element. The instruction selectior won't like this. Get
3331 // a corrected mask and commute to form a proper MOVS{S|D}.
3332 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3333 if (NewMask.Val != PermMask.Val)
3334 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3335 }
3336 return Op;
3337 }
3338
3339 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3340 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3341 X86::isUNPCKLMask(PermMask.Val) ||
3342 X86::isUNPCKHMask(PermMask.Val))
3343 return Op;
3344
3345 if (V2IsSplat) {
3346 // Normalize mask so all entries that point to V2 points to its first
3347 // element then try to match unpck{h|l} again. If match, return a
3348 // new vector_shuffle with the corrected mask.
3349 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3350 if (NewMask.Val != PermMask.Val) {
3351 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3352 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3353 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3354 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3355 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3356 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3357 }
3358 }
3359 }
3360
3361 // Normalize the node to match x86 shuffle ops if needed
3362 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3363 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3364
3365 if (Commuted) {
3366 // Commute is back and try unpck* again.
3367 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3368 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3369 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3370 X86::isUNPCKLMask(PermMask.Val) ||
3371 X86::isUNPCKHMask(PermMask.Val))
3372 return Op;
3373 }
3374
3375 // If VT is integer, try PSHUF* first, then SHUFP*.
3376 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003377 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3378 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3379 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3380 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381 X86::isPSHUFHWMask(PermMask.Val) ||
3382 X86::isPSHUFLWMask(PermMask.Val)) {
3383 if (V2.getOpcode() != ISD::UNDEF)
3384 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3385 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3386 return Op;
3387 }
3388
3389 if (X86::isSHUFPMask(PermMask.Val) &&
3390 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3391 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003392 } else {
3393 // Floating point cases in the other order.
3394 if (X86::isSHUFPMask(PermMask.Val))
3395 return Op;
3396 if (X86::isPSHUFDMask(PermMask.Val) ||
3397 X86::isPSHUFHWMask(PermMask.Val) ||
3398 X86::isPSHUFLWMask(PermMask.Val)) {
3399 if (V2.getOpcode() != ISD::UNDEF)
3400 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3401 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3402 return Op;
3403 }
3404 }
3405
Evan Cheng75184a92007-12-11 01:46:18 +00003406 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3407 if (VT == MVT::v8i16) {
3408 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3409 if (NewOp.Val)
3410 return NewOp;
3411 }
3412
3413 // Handle all 4 wide cases with a number of shuffles.
3414 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003415 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003416 MVT::ValueType MaskVT = PermMask.getValueType();
3417 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3418 SmallVector<std::pair<int, int>, 8> Locs;
3419 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003420 SmallVector<SDOperand, 8> Mask1(NumElems,
3421 DAG.getNode(ISD::UNDEF, MaskEVT));
3422 SmallVector<SDOperand, 8> Mask2(NumElems,
3423 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424 unsigned NumHi = 0;
3425 unsigned NumLo = 0;
3426 // If no more than two elements come from either vector. This can be
3427 // implemented with two shuffles. First shuffle gather the elements.
3428 // The second shuffle, which takes the first shuffle as both of its
3429 // vector operands, put the elements into the right order.
3430 for (unsigned i = 0; i != NumElems; ++i) {
3431 SDOperand Elt = PermMask.getOperand(i);
3432 if (Elt.getOpcode() == ISD::UNDEF) {
3433 Locs[i] = std::make_pair(-1, -1);
3434 } else {
3435 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3436 if (Val < NumElems) {
3437 Locs[i] = std::make_pair(0, NumLo);
3438 Mask1[NumLo] = Elt;
3439 NumLo++;
3440 } else {
3441 Locs[i] = std::make_pair(1, NumHi);
3442 if (2+NumHi < NumElems)
3443 Mask1[2+NumHi] = Elt;
3444 NumHi++;
3445 }
3446 }
3447 }
3448 if (NumLo <= 2 && NumHi <= 2) {
3449 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3450 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3451 &Mask1[0], Mask1.size()));
3452 for (unsigned i = 0; i != NumElems; ++i) {
3453 if (Locs[i].first == -1)
3454 continue;
3455 else {
3456 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3457 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3458 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3459 }
3460 }
3461
3462 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3463 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3464 &Mask2[0], Mask2.size()));
3465 }
3466
3467 // Break it into (shuffle shuffle_hi, shuffle_lo).
3468 Locs.clear();
3469 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3470 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3471 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3472 unsigned MaskIdx = 0;
3473 unsigned LoIdx = 0;
3474 unsigned HiIdx = NumElems/2;
3475 for (unsigned i = 0; i != NumElems; ++i) {
3476 if (i == NumElems/2) {
3477 MaskPtr = &HiMask;
3478 MaskIdx = 1;
3479 LoIdx = 0;
3480 HiIdx = NumElems/2;
3481 }
3482 SDOperand Elt = PermMask.getOperand(i);
3483 if (Elt.getOpcode() == ISD::UNDEF) {
3484 Locs[i] = std::make_pair(-1, -1);
3485 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3486 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3487 (*MaskPtr)[LoIdx] = Elt;
3488 LoIdx++;
3489 } else {
3490 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3491 (*MaskPtr)[HiIdx] = Elt;
3492 HiIdx++;
3493 }
3494 }
3495
3496 SDOperand LoShuffle =
3497 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3498 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3499 &LoMask[0], LoMask.size()));
3500 SDOperand HiShuffle =
3501 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3502 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3503 &HiMask[0], HiMask.size()));
3504 SmallVector<SDOperand, 8> MaskOps;
3505 for (unsigned i = 0; i != NumElems; ++i) {
3506 if (Locs[i].first == -1) {
3507 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3508 } else {
3509 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3510 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3511 }
3512 }
3513 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3514 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3515 &MaskOps[0], MaskOps.size()));
3516 }
3517
3518 return SDOperand();
3519}
3520
3521SDOperand
3522X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3523 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3524 return SDOperand();
3525
3526 MVT::ValueType VT = Op.getValueType();
3527 // TODO: handle v16i8.
3528 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003529 SDOperand Vec = Op.getOperand(0);
3530 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3531 if (Idx == 0)
3532 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3533 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3534 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3535 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003536 // Transform it so it match pextrw which produces a 32-bit result.
3537 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3538 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3539 Op.getOperand(0), Op.getOperand(1));
3540 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3541 DAG.getValueType(VT));
3542 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3543 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003544 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3545 if (Idx == 0)
3546 return Op;
3547 // SHUFPS the element to the lowest double word, then movss.
3548 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3549 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003550 IdxVec.
3551 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3552 IdxVec.
3553 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3554 IdxVec.
3555 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3556 IdxVec.
3557 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003558 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3559 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003560 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003561 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3562 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3563 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3564 DAG.getConstant(0, getPointerTy()));
3565 } else if (MVT::getSizeInBits(VT) == 64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003566 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3567 if (Idx == 0)
3568 return Op;
3569
3570 // UNPCKHPD the element to the lowest double word, then movsd.
3571 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3572 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3573 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3574 SmallVector<SDOperand, 8> IdxVec;
3575 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003576 IdxVec.
3577 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003578 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3579 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003580 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3582 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3583 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3584 DAG.getConstant(0, getPointerTy()));
3585 }
3586
3587 return SDOperand();
3588}
3589
3590SDOperand
3591X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003592 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003593 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3594 if (EVT == MVT::i8)
3595 return SDOperand();
3596
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597 SDOperand N0 = Op.getOperand(0);
3598 SDOperand N1 = Op.getOperand(1);
3599 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003600
3601 if (MVT::getSizeInBits(EVT) == 16) {
3602 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3603 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003604 if (N1.getValueType() != MVT::i32)
3605 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3606 if (N2.getValueType() != MVT::i32)
3607 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
3608 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003609 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003610 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003611}
3612
3613SDOperand
3614X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3615 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3616 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3617}
3618
3619// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3620// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3621// one of the above mentioned nodes. It has to be wrapped because otherwise
3622// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3623// be used to form addressing mode. These wrapped nodes will be selected
3624// into MOV32ri.
3625SDOperand
3626X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3627 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3628 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3629 getPointerTy(),
3630 CP->getAlignment());
3631 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3632 // With PIC, the address is actually $g + Offset.
3633 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3634 !Subtarget->isPICStyleRIPRel()) {
3635 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3636 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3637 Result);
3638 }
3639
3640 return Result;
3641}
3642
3643SDOperand
3644X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3645 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3646 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3647 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3648 // With PIC, the address is actually $g + Offset.
3649 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3650 !Subtarget->isPICStyleRIPRel()) {
3651 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3652 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3653 Result);
3654 }
3655
3656 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3657 // load the value at address GV, not the value of GV itself. This means that
3658 // the GlobalAddress must be in the base or index register of the address, not
3659 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3660 // The same applies for external symbols during PIC codegen
3661 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3662 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3663
3664 return Result;
3665}
3666
3667// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3668static SDOperand
3669LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3670 const MVT::ValueType PtrVT) {
3671 SDOperand InFlag;
3672 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3673 DAG.getNode(X86ISD::GlobalBaseReg,
3674 PtrVT), InFlag);
3675 InFlag = Chain.getValue(1);
3676
3677 // emit leal symbol@TLSGD(,%ebx,1), %eax
3678 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3679 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3680 GA->getValueType(0),
3681 GA->getOffset());
3682 SDOperand Ops[] = { Chain, TGA, InFlag };
3683 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3684 InFlag = Result.getValue(2);
3685 Chain = Result.getValue(1);
3686
3687 // call ___tls_get_addr. This function receives its argument in
3688 // the register EAX.
3689 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3690 InFlag = Chain.getValue(1);
3691
3692 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3693 SDOperand Ops1[] = { Chain,
3694 DAG.getTargetExternalSymbol("___tls_get_addr",
3695 PtrVT),
3696 DAG.getRegister(X86::EAX, PtrVT),
3697 DAG.getRegister(X86::EBX, PtrVT),
3698 InFlag };
3699 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3700 InFlag = Chain.getValue(1);
3701
3702 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3703}
3704
3705// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3706// "local exec" model.
3707static SDOperand
3708LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3709 const MVT::ValueType PtrVT) {
3710 // Get the Thread Pointer
3711 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3712 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3713 // exec)
3714 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3715 GA->getValueType(0),
3716 GA->getOffset());
3717 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3718
3719 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3720 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3721
3722 // The address of the thread local variable is the add of the thread
3723 // pointer with the offset of the variable.
3724 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3725}
3726
3727SDOperand
3728X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3729 // TODO: implement the "local dynamic" model
3730 // TODO: implement the "initial exec"model for pic executables
3731 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3732 "TLS not implemented for non-ELF and 64-bit targets");
3733 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3734 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3735 // otherwise use the "Local Exec"TLS Model
3736 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3737 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3738 else
3739 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3740}
3741
3742SDOperand
3743X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3744 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3745 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3746 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3747 // With PIC, the address is actually $g + Offset.
3748 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3749 !Subtarget->isPICStyleRIPRel()) {
3750 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3751 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3752 Result);
3753 }
3754
3755 return Result;
3756}
3757
3758SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3759 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3760 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3761 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3762 // With PIC, the address is actually $g + Offset.
3763 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3764 !Subtarget->isPICStyleRIPRel()) {
3765 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3766 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3767 Result);
3768 }
3769
3770 return Result;
3771}
3772
Chris Lattner62814a32007-10-17 06:02:13 +00003773/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
3774/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003775SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00003776 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3777 "Not an i64 shift!");
3778 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3779 SDOperand ShOpLo = Op.getOperand(0);
3780 SDOperand ShOpHi = Op.getOperand(1);
3781 SDOperand ShAmt = Op.getOperand(2);
3782 SDOperand Tmp1 = isSRA ?
3783 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3784 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785
Chris Lattner62814a32007-10-17 06:02:13 +00003786 SDOperand Tmp2, Tmp3;
3787 if (Op.getOpcode() == ISD::SHL_PARTS) {
3788 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3789 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3790 } else {
3791 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3792 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3793 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003794
Chris Lattner62814a32007-10-17 06:02:13 +00003795 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3796 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3797 DAG.getConstant(32, MVT::i8));
3798 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3799 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003800
Chris Lattner62814a32007-10-17 06:02:13 +00003801 SDOperand Hi, Lo;
3802 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3803 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3804 SmallVector<SDOperand, 4> Ops;
3805 if (Op.getOpcode() == ISD::SHL_PARTS) {
3806 Ops.push_back(Tmp2);
3807 Ops.push_back(Tmp3);
3808 Ops.push_back(CC);
3809 Ops.push_back(Cond);
3810 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003811
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003812 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00003813 Ops.push_back(Tmp3);
3814 Ops.push_back(Tmp1);
3815 Ops.push_back(CC);
3816 Ops.push_back(Cond);
3817 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3818 } else {
3819 Ops.push_back(Tmp2);
3820 Ops.push_back(Tmp3);
3821 Ops.push_back(CC);
3822 Ops.push_back(Cond);
3823 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3824
3825 Ops.clear();
3826 Ops.push_back(Tmp3);
3827 Ops.push_back(Tmp1);
3828 Ops.push_back(CC);
3829 Ops.push_back(Cond);
3830 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3831 }
3832
3833 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3834 Ops.clear();
3835 Ops.push_back(Lo);
3836 Ops.push_back(Hi);
3837 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003838}
3839
3840SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3841 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3842 Op.getOperand(0).getValueType() >= MVT::i16 &&
3843 "Unknown SINT_TO_FP to lower!");
3844
3845 SDOperand Result;
3846 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3847 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3848 MachineFunction &MF = DAG.getMachineFunction();
3849 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3850 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3851 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3852 StackSlot, NULL, 0);
3853
Dale Johannesen2fc20782007-09-14 22:26:36 +00003854 // These are really Legal; caller falls through into that case.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003855 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
3856 return Result;
3857 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen2fc20782007-09-14 22:26:36 +00003858 return Result;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003859 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
3860 Subtarget->is64Bit())
3861 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00003862
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003863 // Build the FILD
3864 SDVTList Tys;
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003865 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
3866 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen2fc20782007-09-14 22:26:36 +00003867 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003868 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3869 else
3870 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
3871 SmallVector<SDOperand, 8> Ops;
3872 Ops.push_back(Chain);
3873 Ops.push_back(StackSlot);
3874 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00003875 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003876 Tys, &Ops[0], Ops.size());
3877
Dale Johannesen2fc20782007-09-14 22:26:36 +00003878 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003879 Chain = Result.getValue(1);
3880 SDOperand InFlag = Result.getValue(2);
3881
3882 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3883 // shouldn't be necessary except that RFP cannot be live across
3884 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3885 MachineFunction &MF = DAG.getMachineFunction();
3886 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3887 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3888 Tys = DAG.getVTList(MVT::Other);
3889 SmallVector<SDOperand, 8> Ops;
3890 Ops.push_back(Chain);
3891 Ops.push_back(Result);
3892 Ops.push_back(StackSlot);
3893 Ops.push_back(DAG.getValueType(Op.getValueType()));
3894 Ops.push_back(InFlag);
3895 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3896 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3897 }
3898
3899 return Result;
3900}
3901
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003902std::pair<SDOperand,SDOperand> X86TargetLowering::
3903FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003904 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3905 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906
Dale Johannesen2fc20782007-09-14 22:26:36 +00003907 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003908 if (Op.getValueType() == MVT::i32 &&
3909 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003910 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003911 if (Op.getValueType() == MVT::i32 &&
3912 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003913 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00003914 if (Subtarget->is64Bit() &&
3915 Op.getValueType() == MVT::i64 &&
3916 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003917 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00003918
Evan Cheng05441e62007-10-15 20:11:21 +00003919 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3920 // stack slot.
3921 MachineFunction &MF = DAG.getMachineFunction();
3922 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3923 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3924 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003925 unsigned Opc;
3926 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003927 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3928 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3929 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3930 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003931 }
3932
3933 SDOperand Chain = DAG.getEntryNode();
3934 SDOperand Value = Op.getOperand(0);
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003935 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
3936 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003937 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3938 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3939 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
3940 SDOperand Ops[] = {
3941 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3942 };
3943 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3944 Chain = Value.getValue(1);
3945 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3946 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3947 }
3948
3949 // Build the FP_TO_INT*_IN_MEM
3950 SDOperand Ops[] = { Chain, Value, StackSlot };
3951 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
3952
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003953 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954}
3955
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003956SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003957 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
3958 SDOperand FIST = Vals.first, StackSlot = Vals.second;
3959 if (FIST.Val == 0) return SDOperand();
3960
3961 // Load the result.
3962 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3963}
3964
3965SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
3966 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
3967 SDOperand FIST = Vals.first, StackSlot = Vals.second;
3968 if (FIST.Val == 0) return 0;
3969
3970 // Return an i64 load from the stack slot.
3971 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
3972
3973 // Use a MERGE_VALUES node to drop the chain result value.
3974 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
3975}
3976
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003977SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3978 MVT::ValueType VT = Op.getValueType();
3979 MVT::ValueType EltVT = VT;
3980 if (MVT::isVector(VT))
3981 EltVT = MVT::getVectorElementType(VT);
3982 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
3983 std::vector<Constant*> CV;
3984 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00003985 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 CV.push_back(C);
3987 CV.push_back(C);
3988 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00003989 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003990 CV.push_back(C);
3991 CV.push_back(C);
3992 CV.push_back(C);
3993 CV.push_back(C);
3994 }
Dan Gohman11821702007-07-27 17:16:43 +00003995 Constant *C = ConstantVector::get(CV);
3996 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3997 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3998 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003999 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4000}
4001
4002SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4003 MVT::ValueType VT = Op.getValueType();
4004 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004005 unsigned EltNum = 1;
4006 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004007 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004008 EltNum = MVT::getVectorNumElements(VT);
4009 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4011 std::vector<Constant*> CV;
4012 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004013 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004014 CV.push_back(C);
4015 CV.push_back(C);
4016 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004017 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004018 CV.push_back(C);
4019 CV.push_back(C);
4020 CV.push_back(C);
4021 CV.push_back(C);
4022 }
Dan Gohman11821702007-07-27 17:16:43 +00004023 Constant *C = ConstantVector::get(CV);
4024 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4025 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4026 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004027 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004028 return DAG.getNode(ISD::BIT_CONVERT, VT,
4029 DAG.getNode(ISD::XOR, MVT::v2i64,
4030 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4031 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4032 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004033 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4034 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035}
4036
4037SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4038 SDOperand Op0 = Op.getOperand(0);
4039 SDOperand Op1 = Op.getOperand(1);
4040 MVT::ValueType VT = Op.getValueType();
4041 MVT::ValueType SrcVT = Op1.getValueType();
4042 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4043
4044 // If second operand is smaller, extend it first.
4045 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4046 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4047 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004048 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004050 // And if it is bigger, shrink it first.
4051 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4052 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1);
4053 SrcVT = VT;
4054 SrcTy = MVT::getTypeForValueType(SrcVT);
4055 }
4056
4057 // At this point the operands and the result should have the same
4058 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059
4060 // First get the sign bit of second operand.
4061 std::vector<Constant*> CV;
4062 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004063 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4064 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004066 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4067 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4068 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4069 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 }
Dan Gohman11821702007-07-27 17:16:43 +00004071 Constant *C = ConstantVector::get(CV);
4072 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4073 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4074 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4076
4077 // Shift sign bit right or left if the two operands have different types.
4078 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4079 // Op0 is MVT::f32, Op1 is MVT::f64.
4080 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4081 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4082 DAG.getConstant(32, MVT::i32));
4083 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4084 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4085 DAG.getConstant(0, getPointerTy()));
4086 }
4087
4088 // Clear first operand sign bit.
4089 CV.clear();
4090 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004091 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4092 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004094 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4095 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4096 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4097 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098 }
Dan Gohman11821702007-07-27 17:16:43 +00004099 C = ConstantVector::get(CV);
4100 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4101 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4102 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4104
4105 // Or the value with the sign bit.
4106 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4107}
4108
Evan Cheng621216e2007-09-29 00:00:36 +00004109SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004110 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004111 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004112 SDOperand Op0 = Op.getOperand(0);
4113 SDOperand Op1 = Op.getOperand(1);
4114 SDOperand CC = Op.getOperand(2);
4115 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4116 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4117 unsigned X86CC;
4118
Evan Cheng950aac02007-09-25 01:57:46 +00004119 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004120 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004121 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4122 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004123 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004124 }
Evan Cheng950aac02007-09-25 01:57:46 +00004125
4126 assert(isFP && "Illegal integer SetCC!");
4127
Evan Cheng621216e2007-09-29 00:00:36 +00004128 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004129 switch (SetCCOpcode) {
4130 default: assert(false && "Illegal floating point SetCC!");
4131 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004132 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004133 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004134 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004135 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4136 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4137 }
4138 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004139 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004140 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004141 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004142 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4143 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4144 }
4145 }
4146}
4147
4148
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4150 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 SDOperand Cond = Op.getOperand(0);
4152 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004153
4154 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004155 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004156
Evan Cheng50d37ab2007-10-08 22:16:29 +00004157 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4158 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004159 if (Cond.getOpcode() == X86ISD::SETCC) {
4160 CC = Cond.getOperand(0);
4161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 SDOperand Cmp = Cond.getOperand(1);
4163 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004164 MVT::ValueType VT = Op.getValueType();
4165 bool IllegalFPCMov = false;
4166 if (VT == MVT::f32 && !X86ScalarSSEf32)
4167 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4168 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4169 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Dale Johannesen3b955db2007-10-16 18:09:08 +00004170 else if (VT == MVT::f80)
4171 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng621216e2007-09-29 00:00:36 +00004172 if ((Opc == X86ISD::CMP ||
4173 Opc == X86ISD::COMI ||
4174 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004175 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004176 addTest = false;
4177 }
4178 }
4179
4180 if (addTest) {
4181 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004182 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004183 }
4184
4185 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4186 MVT::Flag);
4187 SmallVector<SDOperand, 4> Ops;
4188 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4189 // condition is true.
4190 Ops.push_back(Op.getOperand(2));
4191 Ops.push_back(Op.getOperand(1));
4192 Ops.push_back(CC);
4193 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004194 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004195}
4196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4198 bool addTest = true;
4199 SDOperand Chain = Op.getOperand(0);
4200 SDOperand Cond = Op.getOperand(1);
4201 SDOperand Dest = Op.getOperand(2);
4202 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203
4204 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004205 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206
Evan Cheng50d37ab2007-10-08 22:16:29 +00004207 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4208 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004209 if (Cond.getOpcode() == X86ISD::SETCC) {
4210 CC = Cond.getOperand(0);
4211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004212 SDOperand Cmp = Cond.getOperand(1);
4213 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004214 if (Opc == X86ISD::CMP ||
4215 Opc == X86ISD::COMI ||
4216 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004217 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004218 addTest = false;
4219 }
4220 }
4221
4222 if (addTest) {
4223 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004224 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004225 }
Evan Cheng621216e2007-09-29 00:00:36 +00004226 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004227 Chain, Op.getOperand(2), CC, Cond);
4228}
4229
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230
4231// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4232// Calls to _alloca is needed to probe the stack when allocating more than 4k
4233// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4234// that the guard pages used by the OS virtual memory manager are allocated in
4235// correct sequence.
4236SDOperand
4237X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4238 SelectionDAG &DAG) {
4239 assert(Subtarget->isTargetCygMing() &&
4240 "This should be used only on Cygwin/Mingw targets");
4241
4242 // Get the inputs.
4243 SDOperand Chain = Op.getOperand(0);
4244 SDOperand Size = Op.getOperand(1);
4245 // FIXME: Ensure alignment here
4246
4247 SDOperand Flag;
4248
4249 MVT::ValueType IntPtr = getPointerTy();
4250 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
4251
4252 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4253 Flag = Chain.getValue(1);
4254
4255 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4256 SDOperand Ops[] = { Chain,
4257 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4258 DAG.getRegister(X86::EAX, IntPtr),
4259 Flag };
4260 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4261 Flag = Chain.getValue(1);
4262
4263 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4264
4265 std::vector<MVT::ValueType> Tys;
4266 Tys.push_back(SPTy);
4267 Tys.push_back(MVT::Other);
4268 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4269 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4270}
4271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4273 SDOperand InFlag(0, 0);
4274 SDOperand Chain = Op.getOperand(0);
4275 unsigned Align =
4276 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4277 if (Align == 0) Align = 1;
4278
4279 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004280 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004281 // The libc version is likely to be faster for these cases. It can use the
4282 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004283 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004284 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004285 MVT::ValueType IntPtr = getPointerTy();
4286 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4287 TargetLowering::ArgListTy Args;
4288 TargetLowering::ArgListEntry Entry;
4289 Entry.Node = Op.getOperand(1);
4290 Entry.Ty = IntPtrTy;
4291 Args.push_back(Entry);
4292 // Extend the unsigned i8 argument to be an int value for the call.
4293 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4294 Entry.Ty = IntPtrTy;
4295 Args.push_back(Entry);
4296 Entry.Node = Op.getOperand(3);
4297 Args.push_back(Entry);
4298 std::pair<SDOperand,SDOperand> CallResult =
4299 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4300 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4301 return CallResult.second;
4302 }
4303
4304 MVT::ValueType AVT;
4305 SDOperand Count;
4306 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4307 unsigned BytesLeft = 0;
4308 bool TwoRepStos = false;
4309 if (ValC) {
4310 unsigned ValReg;
4311 uint64_t Val = ValC->getValue() & 255;
4312
4313 // If the value is a constant, then we can potentially use larger sets.
4314 switch (Align & 3) {
4315 case 2: // WORD aligned
4316 AVT = MVT::i16;
4317 ValReg = X86::AX;
4318 Val = (Val << 8) | Val;
4319 break;
4320 case 0: // DWORD aligned
4321 AVT = MVT::i32;
4322 ValReg = X86::EAX;
4323 Val = (Val << 8) | Val;
4324 Val = (Val << 16) | Val;
4325 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4326 AVT = MVT::i64;
4327 ValReg = X86::RAX;
4328 Val = (Val << 32) | Val;
4329 }
4330 break;
4331 default: // Byte aligned
4332 AVT = MVT::i8;
4333 ValReg = X86::AL;
4334 Count = Op.getOperand(3);
4335 break;
4336 }
4337
4338 if (AVT > MVT::i8) {
4339 if (I) {
4340 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4341 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4342 BytesLeft = I->getValue() % UBytes;
4343 } else {
4344 assert(AVT >= MVT::i32 &&
4345 "Do not use rep;stos if not at least DWORD aligned");
4346 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4347 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4348 TwoRepStos = true;
4349 }
4350 }
4351
4352 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4353 InFlag);
4354 InFlag = Chain.getValue(1);
4355 } else {
4356 AVT = MVT::i8;
4357 Count = Op.getOperand(3);
4358 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4359 InFlag = Chain.getValue(1);
4360 }
4361
4362 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4363 Count, InFlag);
4364 InFlag = Chain.getValue(1);
4365 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4366 Op.getOperand(1), InFlag);
4367 InFlag = Chain.getValue(1);
4368
4369 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4370 SmallVector<SDOperand, 8> Ops;
4371 Ops.push_back(Chain);
4372 Ops.push_back(DAG.getValueType(AVT));
4373 Ops.push_back(InFlag);
4374 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4375
4376 if (TwoRepStos) {
4377 InFlag = Chain.getValue(1);
4378 Count = Op.getOperand(3);
4379 MVT::ValueType CVT = Count.getValueType();
4380 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4381 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4382 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4383 Left, InFlag);
4384 InFlag = Chain.getValue(1);
4385 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4386 Ops.clear();
4387 Ops.push_back(Chain);
4388 Ops.push_back(DAG.getValueType(MVT::i8));
4389 Ops.push_back(InFlag);
4390 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4391 } else if (BytesLeft) {
4392 // Issue stores for the last 1 - 7 bytes.
4393 SDOperand Value;
4394 unsigned Val = ValC->getValue() & 255;
4395 unsigned Offset = I->getValue() - BytesLeft;
4396 SDOperand DstAddr = Op.getOperand(1);
4397 MVT::ValueType AddrVT = DstAddr.getValueType();
4398 if (BytesLeft >= 4) {
4399 Val = (Val << 8) | Val;
4400 Val = (Val << 16) | Val;
4401 Value = DAG.getConstant(Val, MVT::i32);
4402 Chain = DAG.getStore(Chain, Value,
4403 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4404 DAG.getConstant(Offset, AddrVT)),
4405 NULL, 0);
4406 BytesLeft -= 4;
4407 Offset += 4;
4408 }
4409 if (BytesLeft >= 2) {
4410 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4411 Chain = DAG.getStore(Chain, Value,
4412 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4413 DAG.getConstant(Offset, AddrVT)),
4414 NULL, 0);
4415 BytesLeft -= 2;
4416 Offset += 2;
4417 }
4418 if (BytesLeft == 1) {
4419 Value = DAG.getConstant(Val, MVT::i8);
4420 Chain = DAG.getStore(Chain, Value,
4421 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4422 DAG.getConstant(Offset, AddrVT)),
4423 NULL, 0);
4424 }
4425 }
4426
4427 return Chain;
4428}
4429
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004430SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4431 SDOperand Dest,
4432 SDOperand Source,
4433 unsigned Size,
4434 unsigned Align,
4435 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004438 switch (Align & 3) {
4439 case 2: // WORD aligned
4440 AVT = MVT::i16;
4441 break;
4442 case 0: // DWORD aligned
4443 AVT = MVT::i32;
4444 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4445 AVT = MVT::i64;
4446 break;
4447 default: // Byte aligned
4448 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449 break;
4450 }
4451
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004452 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4453 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4454 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455
4456 SDOperand InFlag(0, 0);
4457 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4458 Count, InFlag);
4459 InFlag = Chain.getValue(1);
4460 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004461 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 InFlag = Chain.getValue(1);
4463 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004464 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465 InFlag = Chain.getValue(1);
4466
4467 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4468 SmallVector<SDOperand, 8> Ops;
4469 Ops.push_back(Chain);
4470 Ops.push_back(DAG.getValueType(AVT));
4471 Ops.push_back(InFlag);
4472 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4473
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004474 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004476 unsigned Offset = Size - BytesLeft;
4477 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004478 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004479 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 MVT::ValueType SrcVT = SrcAddr.getValueType();
4481 SDOperand Value;
4482 if (BytesLeft >= 4) {
4483 Value = DAG.getLoad(MVT::i32, Chain,
4484 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4485 DAG.getConstant(Offset, SrcVT)),
4486 NULL, 0);
4487 Chain = Value.getValue(1);
4488 Chain = DAG.getStore(Chain, Value,
4489 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4490 DAG.getConstant(Offset, DstVT)),
4491 NULL, 0);
4492 BytesLeft -= 4;
4493 Offset += 4;
4494 }
4495 if (BytesLeft >= 2) {
4496 Value = DAG.getLoad(MVT::i16, Chain,
4497 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4498 DAG.getConstant(Offset, SrcVT)),
4499 NULL, 0);
4500 Chain = Value.getValue(1);
4501 Chain = DAG.getStore(Chain, Value,
4502 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4503 DAG.getConstant(Offset, DstVT)),
4504 NULL, 0);
4505 BytesLeft -= 2;
4506 Offset += 2;
4507 }
4508
4509 if (BytesLeft == 1) {
4510 Value = DAG.getLoad(MVT::i8, Chain,
4511 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4512 DAG.getConstant(Offset, SrcVT)),
4513 NULL, 0);
4514 Chain = Value.getValue(1);
4515 Chain = DAG.getStore(Chain, Value,
4516 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4517 DAG.getConstant(Offset, DstVT)),
4518 NULL, 0);
4519 }
4520 }
4521
4522 return Chain;
4523}
4524
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004525/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4526SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004528 SDOperand TheChain = N->getOperand(0);
4529 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004531 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4532 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4533 MVT::i64, rax.getValue(2));
4534 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004535 DAG.getConstant(32, MVT::i8));
4536 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004537 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538 };
4539
4540 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004541 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004542 }
4543
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004544 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4545 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4546 MVT::i32, eax.getValue(2));
4547 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4548 SDOperand Ops[] = { eax, edx };
4549 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4550
4551 // Use a MERGE_VALUES to return the value and chain.
4552 Ops[1] = edx.getValue(1);
4553 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4554 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004555}
4556
4557SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4558 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4559
4560 if (!Subtarget->is64Bit()) {
4561 // vastart just stores the address of the VarArgsFrameIndex slot into the
4562 // memory location argument.
4563 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4564 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4565 SV->getOffset());
4566 }
4567
4568 // __va_list_tag:
4569 // gp_offset (0 - 6 * 8)
4570 // fp_offset (48 - 48 + 8 * 16)
4571 // overflow_arg_area (point to parameters coming in memory).
4572 // reg_save_area
4573 SmallVector<SDOperand, 8> MemOps;
4574 SDOperand FIN = Op.getOperand(1);
4575 // Store gp_offset
4576 SDOperand Store = DAG.getStore(Op.getOperand(0),
4577 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4578 FIN, SV->getValue(), SV->getOffset());
4579 MemOps.push_back(Store);
4580
4581 // Store fp_offset
4582 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4583 DAG.getConstant(4, getPointerTy()));
4584 Store = DAG.getStore(Op.getOperand(0),
4585 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4586 FIN, SV->getValue(), SV->getOffset());
4587 MemOps.push_back(Store);
4588
4589 // Store ptr to overflow_arg_area
4590 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4591 DAG.getConstant(4, getPointerTy()));
4592 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4593 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4594 SV->getOffset());
4595 MemOps.push_back(Store);
4596
4597 // Store ptr to reg_save_area.
4598 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4599 DAG.getConstant(8, getPointerTy()));
4600 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4601 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4602 SV->getOffset());
4603 MemOps.push_back(Store);
4604 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4605}
4606
4607SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4608 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4609 SDOperand Chain = Op.getOperand(0);
4610 SDOperand DstPtr = Op.getOperand(1);
4611 SDOperand SrcPtr = Op.getOperand(2);
4612 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4613 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4614
4615 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4616 SrcSV->getValue(), SrcSV->getOffset());
4617 Chain = SrcPtr.getValue(1);
4618 for (unsigned i = 0; i < 3; ++i) {
4619 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4620 SrcSV->getValue(), SrcSV->getOffset());
4621 Chain = Val.getValue(1);
4622 Chain = DAG.getStore(Chain, Val, DstPtr,
4623 DstSV->getValue(), DstSV->getOffset());
4624 if (i == 2)
4625 break;
4626 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4627 DAG.getConstant(8, getPointerTy()));
4628 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4629 DAG.getConstant(8, getPointerTy()));
4630 }
4631 return Chain;
4632}
4633
4634SDOperand
4635X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4636 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4637 switch (IntNo) {
4638 default: return SDOperand(); // Don't custom lower most intrinsics.
4639 // Comparison intrinsics.
4640 case Intrinsic::x86_sse_comieq_ss:
4641 case Intrinsic::x86_sse_comilt_ss:
4642 case Intrinsic::x86_sse_comile_ss:
4643 case Intrinsic::x86_sse_comigt_ss:
4644 case Intrinsic::x86_sse_comige_ss:
4645 case Intrinsic::x86_sse_comineq_ss:
4646 case Intrinsic::x86_sse_ucomieq_ss:
4647 case Intrinsic::x86_sse_ucomilt_ss:
4648 case Intrinsic::x86_sse_ucomile_ss:
4649 case Intrinsic::x86_sse_ucomigt_ss:
4650 case Intrinsic::x86_sse_ucomige_ss:
4651 case Intrinsic::x86_sse_ucomineq_ss:
4652 case Intrinsic::x86_sse2_comieq_sd:
4653 case Intrinsic::x86_sse2_comilt_sd:
4654 case Intrinsic::x86_sse2_comile_sd:
4655 case Intrinsic::x86_sse2_comigt_sd:
4656 case Intrinsic::x86_sse2_comige_sd:
4657 case Intrinsic::x86_sse2_comineq_sd:
4658 case Intrinsic::x86_sse2_ucomieq_sd:
4659 case Intrinsic::x86_sse2_ucomilt_sd:
4660 case Intrinsic::x86_sse2_ucomile_sd:
4661 case Intrinsic::x86_sse2_ucomigt_sd:
4662 case Intrinsic::x86_sse2_ucomige_sd:
4663 case Intrinsic::x86_sse2_ucomineq_sd: {
4664 unsigned Opc = 0;
4665 ISD::CondCode CC = ISD::SETCC_INVALID;
4666 switch (IntNo) {
4667 default: break;
4668 case Intrinsic::x86_sse_comieq_ss:
4669 case Intrinsic::x86_sse2_comieq_sd:
4670 Opc = X86ISD::COMI;
4671 CC = ISD::SETEQ;
4672 break;
4673 case Intrinsic::x86_sse_comilt_ss:
4674 case Intrinsic::x86_sse2_comilt_sd:
4675 Opc = X86ISD::COMI;
4676 CC = ISD::SETLT;
4677 break;
4678 case Intrinsic::x86_sse_comile_ss:
4679 case Intrinsic::x86_sse2_comile_sd:
4680 Opc = X86ISD::COMI;
4681 CC = ISD::SETLE;
4682 break;
4683 case Intrinsic::x86_sse_comigt_ss:
4684 case Intrinsic::x86_sse2_comigt_sd:
4685 Opc = X86ISD::COMI;
4686 CC = ISD::SETGT;
4687 break;
4688 case Intrinsic::x86_sse_comige_ss:
4689 case Intrinsic::x86_sse2_comige_sd:
4690 Opc = X86ISD::COMI;
4691 CC = ISD::SETGE;
4692 break;
4693 case Intrinsic::x86_sse_comineq_ss:
4694 case Intrinsic::x86_sse2_comineq_sd:
4695 Opc = X86ISD::COMI;
4696 CC = ISD::SETNE;
4697 break;
4698 case Intrinsic::x86_sse_ucomieq_ss:
4699 case Intrinsic::x86_sse2_ucomieq_sd:
4700 Opc = X86ISD::UCOMI;
4701 CC = ISD::SETEQ;
4702 break;
4703 case Intrinsic::x86_sse_ucomilt_ss:
4704 case Intrinsic::x86_sse2_ucomilt_sd:
4705 Opc = X86ISD::UCOMI;
4706 CC = ISD::SETLT;
4707 break;
4708 case Intrinsic::x86_sse_ucomile_ss:
4709 case Intrinsic::x86_sse2_ucomile_sd:
4710 Opc = X86ISD::UCOMI;
4711 CC = ISD::SETLE;
4712 break;
4713 case Intrinsic::x86_sse_ucomigt_ss:
4714 case Intrinsic::x86_sse2_ucomigt_sd:
4715 Opc = X86ISD::UCOMI;
4716 CC = ISD::SETGT;
4717 break;
4718 case Intrinsic::x86_sse_ucomige_ss:
4719 case Intrinsic::x86_sse2_ucomige_sd:
4720 Opc = X86ISD::UCOMI;
4721 CC = ISD::SETGE;
4722 break;
4723 case Intrinsic::x86_sse_ucomineq_ss:
4724 case Intrinsic::x86_sse2_ucomineq_sd:
4725 Opc = X86ISD::UCOMI;
4726 CC = ISD::SETNE;
4727 break;
4728 }
4729
4730 unsigned X86CC;
4731 SDOperand LHS = Op.getOperand(1);
4732 SDOperand RHS = Op.getOperand(2);
4733 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4734
Evan Cheng621216e2007-09-29 00:00:36 +00004735 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4736 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4737 DAG.getConstant(X86CC, MVT::i8), Cond);
4738 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004739 }
4740 }
4741}
4742
4743SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4744 // Depths > 0 not supported yet!
4745 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4746 return SDOperand();
4747
4748 // Just load the return address
4749 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4750 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4751}
4752
4753SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4754 // Depths > 0 not supported yet!
4755 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4756 return SDOperand();
4757
4758 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4759 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4760 DAG.getConstant(4, getPointerTy()));
4761}
4762
4763SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4764 SelectionDAG &DAG) {
4765 // Is not yet supported on x86-64
4766 if (Subtarget->is64Bit())
4767 return SDOperand();
4768
4769 return DAG.getConstant(8, getPointerTy());
4770}
4771
4772SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4773{
4774 assert(!Subtarget->is64Bit() &&
4775 "Lowering of eh_return builtin is not supported yet on x86-64");
4776
4777 MachineFunction &MF = DAG.getMachineFunction();
4778 SDOperand Chain = Op.getOperand(0);
4779 SDOperand Offset = Op.getOperand(1);
4780 SDOperand Handler = Op.getOperand(2);
4781
4782 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4783 getPointerTy());
4784
4785 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4786 DAG.getConstant(-4UL, getPointerTy()));
4787 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4788 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4789 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00004790 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004791
4792 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4793 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4794}
4795
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004796SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4797 SelectionDAG &DAG) {
4798 SDOperand Root = Op.getOperand(0);
4799 SDOperand Trmp = Op.getOperand(1); // trampoline
4800 SDOperand FPtr = Op.getOperand(2); // nested function
4801 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4802
4803 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4804
4805 if (Subtarget->is64Bit()) {
4806 return SDOperand(); // not yet supported
4807 } else {
4808 Function *Func = (Function *)
4809 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4810 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00004811 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004812
4813 switch (CC) {
4814 default:
4815 assert(0 && "Unsupported calling convention");
4816 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004817 case CallingConv::X86_StdCall: {
4818 // Pass 'nest' parameter in ECX.
4819 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00004820 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004821
4822 // Check that ECX wasn't needed by an 'inreg' parameter.
4823 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00004824 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004825
4826 if (Attrs && !Func->isVarArg()) {
4827 unsigned InRegCount = 0;
4828 unsigned Idx = 1;
4829
4830 for (FunctionType::param_iterator I = FTy->param_begin(),
4831 E = FTy->param_end(); I != E; ++I, ++Idx)
4832 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4833 // FIXME: should only count parameters that are lowered to integers.
4834 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4835
4836 if (InRegCount > 2) {
4837 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4838 abort();
4839 }
4840 }
4841 break;
4842 }
4843 case CallingConv::X86_FastCall:
4844 // Pass 'nest' parameter in EAX.
4845 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00004846 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004847 break;
4848 }
4849
Duncan Sands466eadd2007-08-29 19:01:20 +00004850 const X86InstrInfo *TII =
4851 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4852
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004853 SDOperand OutChains[4];
4854 SDOperand Addr, Disp;
4855
4856 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4857 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4858
Duncan Sands466eadd2007-08-29 19:01:20 +00004859 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Chris Lattnerd8559ce2007-12-16 20:26:54 +00004860 unsigned char N86Reg = ((X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00004861 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004862 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4863
4864 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4865 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4866 TrmpSV->getOffset() + 1, false, 1);
4867
Duncan Sands466eadd2007-08-29 19:01:20 +00004868 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004869 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4870 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4871 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4872
4873 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4874 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4875 TrmpSV->getOffset() + 6, false, 1);
4876
Duncan Sands7407a9f2007-09-11 14:10:23 +00004877 SDOperand Ops[] =
4878 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4879 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004880 }
4881}
4882
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00004883SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
4884 /*
4885 The rounding mode is in bits 11:10 of FPSR, and has the following
4886 settings:
4887 00 Round to nearest
4888 01 Round to -inf
4889 10 Round to +inf
4890 11 Round to 0
4891
4892 FLT_ROUNDS, on the other hand, expects the following:
4893 -1 Undefined
4894 0 Round to 0
4895 1 Round to nearest
4896 2 Round to +inf
4897 3 Round to -inf
4898
4899 To perform the conversion, we do:
4900 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
4901 */
4902
4903 MachineFunction &MF = DAG.getMachineFunction();
4904 const TargetMachine &TM = MF.getTarget();
4905 const TargetFrameInfo &TFI = *TM.getFrameInfo();
4906 unsigned StackAlignment = TFI.getStackAlignment();
4907 MVT::ValueType VT = Op.getValueType();
4908
4909 // Save FP Control Word to stack slot
4910 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
4911 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4912
4913 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
4914 DAG.getEntryNode(), StackSlot);
4915
4916 // Load FP Control Word from stack slot
4917 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
4918
4919 // Transform as necessary
4920 SDOperand CWD1 =
4921 DAG.getNode(ISD::SRL, MVT::i16,
4922 DAG.getNode(ISD::AND, MVT::i16,
4923 CWD, DAG.getConstant(0x800, MVT::i16)),
4924 DAG.getConstant(11, MVT::i8));
4925 SDOperand CWD2 =
4926 DAG.getNode(ISD::SRL, MVT::i16,
4927 DAG.getNode(ISD::AND, MVT::i16,
4928 CWD, DAG.getConstant(0x400, MVT::i16)),
4929 DAG.getConstant(9, MVT::i8));
4930
4931 SDOperand RetVal =
4932 DAG.getNode(ISD::AND, MVT::i16,
4933 DAG.getNode(ISD::ADD, MVT::i16,
4934 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
4935 DAG.getConstant(1, MVT::i16)),
4936 DAG.getConstant(3, MVT::i16));
4937
4938
4939 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
4940 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
4941}
4942
Evan Cheng48679f42007-12-14 02:13:44 +00004943SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
4944 MVT::ValueType VT = Op.getValueType();
4945 MVT::ValueType OpVT = VT;
4946 unsigned NumBits = MVT::getSizeInBits(VT);
4947
4948 Op = Op.getOperand(0);
4949 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00004950 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00004951 OpVT = MVT::i32;
4952 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
4953 }
Evan Cheng48679f42007-12-14 02:13:44 +00004954
Evan Cheng7cfbfe32007-12-14 08:30:15 +00004955 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
4956 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
4957 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
4958
4959 // If src is zero (i.e. bsr sets ZF), returns NumBits.
4960 SmallVector<SDOperand, 4> Ops;
4961 Ops.push_back(Op);
4962 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
4963 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
4964 Ops.push_back(Op.getValue(1));
4965 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
4966
4967 // Finally xor with NumBits-1.
4968 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
4969
Evan Cheng48679f42007-12-14 02:13:44 +00004970 if (VT == MVT::i8)
4971 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
4972 return Op;
4973}
4974
4975SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
4976 MVT::ValueType VT = Op.getValueType();
4977 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00004978 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00004979
4980 Op = Op.getOperand(0);
4981 if (VT == MVT::i8) {
4982 OpVT = MVT::i32;
4983 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
4984 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00004985
4986 // Issue a bsf (scan bits forward) which also sets EFLAGS.
4987 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
4988 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
4989
4990 // If src is zero (i.e. bsf sets ZF), returns NumBits.
4991 SmallVector<SDOperand, 4> Ops;
4992 Ops.push_back(Op);
4993 Ops.push_back(DAG.getConstant(NumBits, OpVT));
4994 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
4995 Ops.push_back(Op.getValue(1));
4996 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
4997
Evan Cheng48679f42007-12-14 02:13:44 +00004998 if (VT == MVT::i8)
4999 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5000 return Op;
5001}
5002
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005003/// LowerOperation - Provide custom lowering hooks for some operations.
5004///
5005SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5006 switch (Op.getOpcode()) {
5007 default: assert(0 && "Should not custom lower this!");
5008 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5009 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5010 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5011 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5012 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5013 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5014 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5015 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5016 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5017 case ISD::SHL_PARTS:
5018 case ISD::SRA_PARTS:
5019 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5020 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5021 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5022 case ISD::FABS: return LowerFABS(Op, DAG);
5023 case ISD::FNEG: return LowerFNEG(Op, DAG);
5024 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005025 case ISD::SETCC: return LowerSETCC(Op, DAG);
5026 case ISD::SELECT: return LowerSELECT(Op, DAG);
5027 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5029 case ISD::CALL: return LowerCALL(Op, DAG);
5030 case ISD::RET: return LowerRET(Op, DAG);
5031 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5032 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5033 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034 case ISD::VASTART: return LowerVASTART(Op, DAG);
5035 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5036 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5037 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5038 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5039 case ISD::FRAME_TO_ARGS_OFFSET:
5040 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5041 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5042 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005043 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005044 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005045 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5046 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005047
5048 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5049 case ISD::READCYCLECOUNTER:
5050 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005051 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005052}
5053
5054/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5055SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5056 switch (N->getOpcode()) {
5057 default: assert(0 && "Should not custom lower this!");
5058 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5059 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5060 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061}
5062
5063const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5064 switch (Opcode) {
5065 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005066 case X86ISD::BSF: return "X86ISD::BSF";
5067 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068 case X86ISD::SHLD: return "X86ISD::SHLD";
5069 case X86ISD::SHRD: return "X86ISD::SHRD";
5070 case X86ISD::FAND: return "X86ISD::FAND";
5071 case X86ISD::FOR: return "X86ISD::FOR";
5072 case X86ISD::FXOR: return "X86ISD::FXOR";
5073 case X86ISD::FSRL: return "X86ISD::FSRL";
5074 case X86ISD::FILD: return "X86ISD::FILD";
5075 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5076 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5077 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5078 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5079 case X86ISD::FLD: return "X86ISD::FLD";
5080 case X86ISD::FST: return "X86ISD::FST";
5081 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
5082 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5083 case X86ISD::CALL: return "X86ISD::CALL";
5084 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5085 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5086 case X86ISD::CMP: return "X86ISD::CMP";
5087 case X86ISD::COMI: return "X86ISD::COMI";
5088 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5089 case X86ISD::SETCC: return "X86ISD::SETCC";
5090 case X86ISD::CMOV: return "X86ISD::CMOV";
5091 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5092 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5093 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5094 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005095 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5096 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5097 case X86ISD::S2VEC: return "X86ISD::S2VEC";
5098 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5099 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5100 case X86ISD::FMAX: return "X86ISD::FMAX";
5101 case X86ISD::FMIN: return "X86ISD::FMIN";
5102 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5103 case X86ISD::FRCP: return "X86ISD::FRCP";
5104 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5105 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5106 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005107 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005108 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005109 }
5110}
5111
5112// isLegalAddressingMode - Return true if the addressing mode represented
5113// by AM is legal for this target, for a load/store of the specified type.
5114bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5115 const Type *Ty) const {
5116 // X86 supports extremely general addressing modes.
5117
5118 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5119 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5120 return false;
5121
5122 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005123 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005124 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5125 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005126
5127 // X86-64 only supports addr of globals in small code model.
5128 if (Subtarget->is64Bit()) {
5129 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5130 return false;
5131 // If lower 4G is not available, then we must use rip-relative addressing.
5132 if (AM.BaseOffs || AM.Scale > 1)
5133 return false;
5134 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005135 }
5136
5137 switch (AM.Scale) {
5138 case 0:
5139 case 1:
5140 case 2:
5141 case 4:
5142 case 8:
5143 // These scales always work.
5144 break;
5145 case 3:
5146 case 5:
5147 case 9:
5148 // These scales are formed with basereg+scalereg. Only accept if there is
5149 // no basereg yet.
5150 if (AM.HasBaseReg)
5151 return false;
5152 break;
5153 default: // Other stuff never works.
5154 return false;
5155 }
5156
5157 return true;
5158}
5159
5160
Evan Cheng27a820a2007-10-26 01:56:11 +00005161bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5162 if (!Ty1->isInteger() || !Ty2->isInteger())
5163 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005164 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5165 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5166 if (NumBits1 <= NumBits2)
5167 return false;
5168 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005169}
5170
Evan Cheng9decb332007-10-29 19:58:20 +00005171bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5172 MVT::ValueType VT2) const {
5173 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5174 return false;
5175 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5176 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5177 if (NumBits1 <= NumBits2)
5178 return false;
5179 return Subtarget->is64Bit() || NumBits1 < 64;
5180}
Evan Cheng27a820a2007-10-26 01:56:11 +00005181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005182/// isShuffleMaskLegal - Targets can use this to indicate that they only
5183/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5184/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5185/// are assumed to be legal.
5186bool
5187X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5188 // Only do shuffles on 128-bit vector types for now.
5189 if (MVT::getSizeInBits(VT) == 64) return false;
5190 return (Mask.Val->getNumOperands() <= 4 ||
5191 isIdentityMask(Mask.Val) ||
5192 isIdentityMask(Mask.Val, true) ||
5193 isSplatMask(Mask.Val) ||
5194 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5195 X86::isUNPCKLMask(Mask.Val) ||
5196 X86::isUNPCKHMask(Mask.Val) ||
5197 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5198 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5199}
5200
5201bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5202 MVT::ValueType EVT,
5203 SelectionDAG &DAG) const {
5204 unsigned NumElts = BVOps.size();
5205 // Only do shuffles on 128-bit vector types for now.
5206 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5207 if (NumElts == 2) return true;
5208 if (NumElts == 4) {
5209 return (isMOVLMask(&BVOps[0], 4) ||
5210 isCommutedMOVL(&BVOps[0], 4, true) ||
5211 isSHUFPMask(&BVOps[0], 4) ||
5212 isCommutedSHUFP(&BVOps[0], 4));
5213 }
5214 return false;
5215}
5216
5217//===----------------------------------------------------------------------===//
5218// X86 Scheduler Hooks
5219//===----------------------------------------------------------------------===//
5220
5221MachineBasicBlock *
5222X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5223 MachineBasicBlock *BB) {
5224 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5225 switch (MI->getOpcode()) {
5226 default: assert(false && "Unexpected instr type to insert");
5227 case X86::CMOV_FR32:
5228 case X86::CMOV_FR64:
5229 case X86::CMOV_V4F32:
5230 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005231 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005232 // To "insert" a SELECT_CC instruction, we actually have to insert the
5233 // diamond control-flow pattern. The incoming instruction knows the
5234 // destination vreg to set, the condition code register to branch on, the
5235 // true/false values to select between, and a branch opcode to use.
5236 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5237 ilist<MachineBasicBlock>::iterator It = BB;
5238 ++It;
5239
5240 // thisMBB:
5241 // ...
5242 // TrueVal = ...
5243 // cmpTY ccX, r1, r2
5244 // bCC copy1MBB
5245 // fallthrough --> copy0MBB
5246 MachineBasicBlock *thisMBB = BB;
5247 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5248 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5249 unsigned Opc =
5250 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5251 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5252 MachineFunction *F = BB->getParent();
5253 F->getBasicBlockList().insert(It, copy0MBB);
5254 F->getBasicBlockList().insert(It, sinkMBB);
5255 // Update machine-CFG edges by first adding all successors of the current
5256 // block to the new block which will contain the Phi node for the select.
5257 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5258 e = BB->succ_end(); i != e; ++i)
5259 sinkMBB->addSuccessor(*i);
5260 // Next, remove all successors of the current block, and add the true
5261 // and fallthrough blocks as its successors.
5262 while(!BB->succ_empty())
5263 BB->removeSuccessor(BB->succ_begin());
5264 BB->addSuccessor(copy0MBB);
5265 BB->addSuccessor(sinkMBB);
5266
5267 // copy0MBB:
5268 // %FalseValue = ...
5269 // # fallthrough to sinkMBB
5270 BB = copy0MBB;
5271
5272 // Update machine-CFG edges
5273 BB->addSuccessor(sinkMBB);
5274
5275 // sinkMBB:
5276 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5277 // ...
5278 BB = sinkMBB;
5279 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5280 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5281 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5282
5283 delete MI; // The pseudo instruction is gone now.
5284 return BB;
5285 }
5286
5287 case X86::FP32_TO_INT16_IN_MEM:
5288 case X86::FP32_TO_INT32_IN_MEM:
5289 case X86::FP32_TO_INT64_IN_MEM:
5290 case X86::FP64_TO_INT16_IN_MEM:
5291 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005292 case X86::FP64_TO_INT64_IN_MEM:
5293 case X86::FP80_TO_INT16_IN_MEM:
5294 case X86::FP80_TO_INT32_IN_MEM:
5295 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 // Change the floating point control register to use "round towards zero"
5297 // mode when truncating to an integer value.
5298 MachineFunction *F = BB->getParent();
5299 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5300 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5301
5302 // Load the old value of the high byte of the control word...
5303 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005304 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005305 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5306
5307 // Set the high part to be round to zero...
5308 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5309 .addImm(0xC7F);
5310
5311 // Reload the modified control word now...
5312 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5313
5314 // Restore the memory image of control word to original value
5315 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5316 .addReg(OldCW);
5317
5318 // Get the X86 opcode to use.
5319 unsigned Opc;
5320 switch (MI->getOpcode()) {
5321 default: assert(0 && "illegal opcode!");
5322 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5323 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5324 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5325 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5326 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5327 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005328 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5329 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5330 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005331 }
5332
5333 X86AddressMode AM;
5334 MachineOperand &Op = MI->getOperand(0);
5335 if (Op.isRegister()) {
5336 AM.BaseType = X86AddressMode::RegBase;
5337 AM.Base.Reg = Op.getReg();
5338 } else {
5339 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005340 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005341 }
5342 Op = MI->getOperand(1);
5343 if (Op.isImmediate())
5344 AM.Scale = Op.getImm();
5345 Op = MI->getOperand(2);
5346 if (Op.isImmediate())
5347 AM.IndexReg = Op.getImm();
5348 Op = MI->getOperand(3);
5349 if (Op.isGlobalAddress()) {
5350 AM.GV = Op.getGlobal();
5351 } else {
5352 AM.Disp = Op.getImm();
5353 }
5354 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5355 .addReg(MI->getOperand(4).getReg());
5356
5357 // Reload the original control word now.
5358 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5359
5360 delete MI; // The pseudo instruction is gone now.
5361 return BB;
5362 }
5363 }
5364}
5365
5366//===----------------------------------------------------------------------===//
5367// X86 Optimization Hooks
5368//===----------------------------------------------------------------------===//
5369
5370void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5371 uint64_t Mask,
5372 uint64_t &KnownZero,
5373 uint64_t &KnownOne,
5374 const SelectionDAG &DAG,
5375 unsigned Depth) const {
5376 unsigned Opc = Op.getOpcode();
5377 assert((Opc >= ISD::BUILTIN_OP_END ||
5378 Opc == ISD::INTRINSIC_WO_CHAIN ||
5379 Opc == ISD::INTRINSIC_W_CHAIN ||
5380 Opc == ISD::INTRINSIC_VOID) &&
5381 "Should use MaskedValueIsZero if you don't know whether Op"
5382 " is a target node!");
5383
5384 KnownZero = KnownOne = 0; // Don't know anything.
5385 switch (Opc) {
5386 default: break;
5387 case X86ISD::SETCC:
5388 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5389 break;
5390 }
5391}
5392
5393/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5394/// element of the result of the vector shuffle.
5395static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5396 MVT::ValueType VT = N->getValueType(0);
5397 SDOperand PermMask = N->getOperand(2);
5398 unsigned NumElems = PermMask.getNumOperands();
5399 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5400 i %= NumElems;
5401 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5402 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005403 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005404 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5405 SDOperand Idx = PermMask.getOperand(i);
5406 if (Idx.getOpcode() == ISD::UNDEF)
5407 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5408 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5409 }
5410 return SDOperand();
5411}
5412
5413/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5414/// node is a GlobalAddress + an offset.
5415static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5416 unsigned Opc = N->getOpcode();
5417 if (Opc == X86ISD::Wrapper) {
5418 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5419 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5420 return true;
5421 }
5422 } else if (Opc == ISD::ADD) {
5423 SDOperand N1 = N->getOperand(0);
5424 SDOperand N2 = N->getOperand(1);
5425 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5426 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5427 if (V) {
5428 Offset += V->getSignExtended();
5429 return true;
5430 }
5431 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5432 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5433 if (V) {
5434 Offset += V->getSignExtended();
5435 return true;
5436 }
5437 }
5438 }
5439 return false;
5440}
5441
5442/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5443/// + Dist * Size.
5444static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5445 MachineFrameInfo *MFI) {
5446 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5447 return false;
5448
5449 SDOperand Loc = N->getOperand(1);
5450 SDOperand BaseLoc = Base->getOperand(1);
5451 if (Loc.getOpcode() == ISD::FrameIndex) {
5452 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5453 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005454 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5455 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005456 int FS = MFI->getObjectSize(FI);
5457 int BFS = MFI->getObjectSize(BFI);
5458 if (FS != BFS || FS != Size) return false;
5459 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5460 } else {
5461 GlobalValue *GV1 = NULL;
5462 GlobalValue *GV2 = NULL;
5463 int64_t Offset1 = 0;
5464 int64_t Offset2 = 0;
5465 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5466 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5467 if (isGA1 && isGA2 && GV1 == GV2)
5468 return Offset1 == (Offset2 + Dist*Size);
5469 }
5470
5471 return false;
5472}
5473
5474static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5475 const X86Subtarget *Subtarget) {
5476 GlobalValue *GV;
5477 int64_t Offset;
5478 if (isGAPlusOffset(Base, GV, Offset))
5479 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5480 else {
5481 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman53491e92007-07-23 20:24:29 +00005482 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005483 if (BFI < 0)
5484 // Fixed objects do not specify alignment, however the offsets are known.
5485 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5486 (MFI->getObjectOffset(BFI) % 16) == 0);
5487 else
5488 return MFI->getObjectAlignment(BFI) >= 16;
5489 }
5490 return false;
5491}
5492
5493
5494/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5495/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5496/// if the load addresses are consecutive, non-overlapping, and in the right
5497/// order.
5498static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5499 const X86Subtarget *Subtarget) {
5500 MachineFunction &MF = DAG.getMachineFunction();
5501 MachineFrameInfo *MFI = MF.getFrameInfo();
5502 MVT::ValueType VT = N->getValueType(0);
5503 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5504 SDOperand PermMask = N->getOperand(2);
5505 int NumElems = (int)PermMask.getNumOperands();
5506 SDNode *Base = NULL;
5507 for (int i = 0; i < NumElems; ++i) {
5508 SDOperand Idx = PermMask.getOperand(i);
5509 if (Idx.getOpcode() == ISD::UNDEF) {
5510 if (!Base) return SDOperand();
5511 } else {
5512 SDOperand Arg =
5513 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5514 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5515 return SDOperand();
5516 if (!Base)
5517 Base = Arg.Val;
5518 else if (!isConsecutiveLoad(Arg.Val, Base,
5519 i, MVT::getSizeInBits(EVT)/8,MFI))
5520 return SDOperand();
5521 }
5522 }
5523
5524 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005525 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005526 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005527 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005528 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005530 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5531 LD->getSrcValueOffset(), LD->isVolatile(),
5532 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005533 }
5534}
5535
5536/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5537static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5538 const X86Subtarget *Subtarget) {
5539 SDOperand Cond = N->getOperand(0);
5540
5541 // If we have SSE[12] support, try to form min/max nodes.
5542 if (Subtarget->hasSSE2() &&
5543 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5544 if (Cond.getOpcode() == ISD::SETCC) {
5545 // Get the LHS/RHS of the select.
5546 SDOperand LHS = N->getOperand(1);
5547 SDOperand RHS = N->getOperand(2);
5548 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5549
5550 unsigned Opcode = 0;
5551 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5552 switch (CC) {
5553 default: break;
5554 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5555 case ISD::SETULE:
5556 case ISD::SETLE:
5557 if (!UnsafeFPMath) break;
5558 // FALL THROUGH.
5559 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5560 case ISD::SETLT:
5561 Opcode = X86ISD::FMIN;
5562 break;
5563
5564 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5565 case ISD::SETUGT:
5566 case ISD::SETGT:
5567 if (!UnsafeFPMath) break;
5568 // FALL THROUGH.
5569 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5570 case ISD::SETGE:
5571 Opcode = X86ISD::FMAX;
5572 break;
5573 }
5574 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5575 switch (CC) {
5576 default: break;
5577 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5578 case ISD::SETUGT:
5579 case ISD::SETGT:
5580 if (!UnsafeFPMath) break;
5581 // FALL THROUGH.
5582 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5583 case ISD::SETGE:
5584 Opcode = X86ISD::FMIN;
5585 break;
5586
5587 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5588 case ISD::SETULE:
5589 case ISD::SETLE:
5590 if (!UnsafeFPMath) break;
5591 // FALL THROUGH.
5592 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5593 case ISD::SETLT:
5594 Opcode = X86ISD::FMAX;
5595 break;
5596 }
5597 }
5598
5599 if (Opcode)
5600 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5601 }
5602
5603 }
5604
5605 return SDOperand();
5606}
5607
5608
5609SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5610 DAGCombinerInfo &DCI) const {
5611 SelectionDAG &DAG = DCI.DAG;
5612 switch (N->getOpcode()) {
5613 default: break;
5614 case ISD::VECTOR_SHUFFLE:
5615 return PerformShuffleCombine(N, DAG, Subtarget);
5616 case ISD::SELECT:
5617 return PerformSELECTCombine(N, DAG, Subtarget);
5618 }
5619
5620 return SDOperand();
5621}
5622
5623//===----------------------------------------------------------------------===//
5624// X86 Inline Assembly Support
5625//===----------------------------------------------------------------------===//
5626
5627/// getConstraintType - Given a constraint letter, return the type of
5628/// constraint it is for this target.
5629X86TargetLowering::ConstraintType
5630X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5631 if (Constraint.size() == 1) {
5632 switch (Constraint[0]) {
5633 case 'A':
5634 case 'r':
5635 case 'R':
5636 case 'l':
5637 case 'q':
5638 case 'Q':
5639 case 'x':
5640 case 'Y':
5641 return C_RegisterClass;
5642 default:
5643 break;
5644 }
5645 }
5646 return TargetLowering::getConstraintType(Constraint);
5647}
5648
Chris Lattnera531abc2007-08-25 00:47:38 +00005649/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5650/// vector. If it is invalid, don't add anything to Ops.
5651void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5652 char Constraint,
5653 std::vector<SDOperand>&Ops,
5654 SelectionDAG &DAG) {
5655 SDOperand Result(0, 0);
5656
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005657 switch (Constraint) {
5658 default: break;
5659 case 'I':
5660 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005661 if (C->getValue() <= 31) {
5662 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5663 break;
5664 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005665 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005666 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005667 case 'N':
5668 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005669 if (C->getValue() <= 255) {
5670 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5671 break;
5672 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005673 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005674 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005675 case 'i': {
5676 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00005677 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5678 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5679 break;
5680 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005681
5682 // If we are in non-pic codegen mode, we allow the address of a global (with
5683 // an optional displacement) to be used with 'i'.
5684 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5685 int64_t Offset = 0;
5686
5687 // Match either (GA) or (GA+C)
5688 if (GA) {
5689 Offset = GA->getOffset();
5690 } else if (Op.getOpcode() == ISD::ADD) {
5691 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5692 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5693 if (C && GA) {
5694 Offset = GA->getOffset()+C->getValue();
5695 } else {
5696 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5697 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5698 if (C && GA)
5699 Offset = GA->getOffset()+C->getValue();
5700 else
5701 C = 0, GA = 0;
5702 }
5703 }
5704
5705 if (GA) {
5706 // If addressing this global requires a load (e.g. in PIC mode), we can't
5707 // match.
5708 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5709 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00005710 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005711
5712 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5713 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00005714 Result = Op;
5715 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005716 }
5717
5718 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00005719 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005720 }
5721 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005722
5723 if (Result.Val) {
5724 Ops.push_back(Result);
5725 return;
5726 }
5727 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005728}
5729
5730std::vector<unsigned> X86TargetLowering::
5731getRegClassForInlineAsmConstraint(const std::string &Constraint,
5732 MVT::ValueType VT) const {
5733 if (Constraint.size() == 1) {
5734 // FIXME: not handling fp-stack yet!
5735 switch (Constraint[0]) { // GCC X86 Constraint Letters
5736 default: break; // Unknown constraint letter
5737 case 'A': // EAX/EDX
5738 if (VT == MVT::i32 || VT == MVT::i64)
5739 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5740 break;
5741 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5742 case 'Q': // Q_REGS
5743 if (VT == MVT::i32)
5744 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5745 else if (VT == MVT::i16)
5746 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5747 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00005748 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00005749 else if (VT == MVT::i64)
5750 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
5751 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005752 }
5753 }
5754
5755 return std::vector<unsigned>();
5756}
5757
5758std::pair<unsigned, const TargetRegisterClass*>
5759X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5760 MVT::ValueType VT) const {
5761 // First, see if this is a constraint that directly corresponds to an LLVM
5762 // register class.
5763 if (Constraint.size() == 1) {
5764 // GCC Constraint Letters
5765 switch (Constraint[0]) {
5766 default: break;
5767 case 'r': // GENERAL_REGS
5768 case 'R': // LEGACY_REGS
5769 case 'l': // INDEX_REGS
5770 if (VT == MVT::i64 && Subtarget->is64Bit())
5771 return std::make_pair(0U, X86::GR64RegisterClass);
5772 if (VT == MVT::i32)
5773 return std::make_pair(0U, X86::GR32RegisterClass);
5774 else if (VT == MVT::i16)
5775 return std::make_pair(0U, X86::GR16RegisterClass);
5776 else if (VT == MVT::i8)
5777 return std::make_pair(0U, X86::GR8RegisterClass);
5778 break;
5779 case 'y': // MMX_REGS if MMX allowed.
5780 if (!Subtarget->hasMMX()) break;
5781 return std::make_pair(0U, X86::VR64RegisterClass);
5782 break;
5783 case 'Y': // SSE_REGS if SSE2 allowed
5784 if (!Subtarget->hasSSE2()) break;
5785 // FALL THROUGH.
5786 case 'x': // SSE_REGS if SSE1 allowed
5787 if (!Subtarget->hasSSE1()) break;
5788
5789 switch (VT) {
5790 default: break;
5791 // Scalar SSE types.
5792 case MVT::f32:
5793 case MVT::i32:
5794 return std::make_pair(0U, X86::FR32RegisterClass);
5795 case MVT::f64:
5796 case MVT::i64:
5797 return std::make_pair(0U, X86::FR64RegisterClass);
5798 // Vector types.
5799 case MVT::v16i8:
5800 case MVT::v8i16:
5801 case MVT::v4i32:
5802 case MVT::v2i64:
5803 case MVT::v4f32:
5804 case MVT::v2f64:
5805 return std::make_pair(0U, X86::VR128RegisterClass);
5806 }
5807 break;
5808 }
5809 }
5810
5811 // Use the default implementation in TargetLowering to convert the register
5812 // constraint into a member of a register class.
5813 std::pair<unsigned, const TargetRegisterClass*> Res;
5814 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5815
5816 // Not found as a standard register?
5817 if (Res.second == 0) {
5818 // GCC calls "st(0)" just plain "st".
5819 if (StringsEqualNoCase("{st}", Constraint)) {
5820 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00005821 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822 }
5823
5824 return Res;
5825 }
5826
5827 // Otherwise, check to see if this is a register class of the wrong value
5828 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5829 // turn into {ax},{dx}.
5830 if (Res.second->hasType(VT))
5831 return Res; // Correct type already, nothing to do.
5832
5833 // All of the single-register GCC register classes map their values onto
5834 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5835 // really want an 8-bit or 32-bit register, map to the appropriate register
5836 // class and return the appropriate register.
5837 if (Res.second != X86::GR16RegisterClass)
5838 return Res;
5839
5840 if (VT == MVT::i8) {
5841 unsigned DestReg = 0;
5842 switch (Res.first) {
5843 default: break;
5844 case X86::AX: DestReg = X86::AL; break;
5845 case X86::DX: DestReg = X86::DL; break;
5846 case X86::CX: DestReg = X86::CL; break;
5847 case X86::BX: DestReg = X86::BL; break;
5848 }
5849 if (DestReg) {
5850 Res.first = DestReg;
5851 Res.second = Res.second = X86::GR8RegisterClass;
5852 }
5853 } else if (VT == MVT::i32) {
5854 unsigned DestReg = 0;
5855 switch (Res.first) {
5856 default: break;
5857 case X86::AX: DestReg = X86::EAX; break;
5858 case X86::DX: DestReg = X86::EDX; break;
5859 case X86::CX: DestReg = X86::ECX; break;
5860 case X86::BX: DestReg = X86::EBX; break;
5861 case X86::SI: DestReg = X86::ESI; break;
5862 case X86::DI: DestReg = X86::EDI; break;
5863 case X86::BP: DestReg = X86::EBP; break;
5864 case X86::SP: DestReg = X86::ESP; break;
5865 }
5866 if (DestReg) {
5867 Res.first = DestReg;
5868 Res.second = Res.second = X86::GR32RegisterClass;
5869 }
5870 } else if (VT == MVT::i64) {
5871 unsigned DestReg = 0;
5872 switch (Res.first) {
5873 default: break;
5874 case X86::AX: DestReg = X86::RAX; break;
5875 case X86::DX: DestReg = X86::RDX; break;
5876 case X86::CX: DestReg = X86::RCX; break;
5877 case X86::BX: DestReg = X86::RBX; break;
5878 case X86::SI: DestReg = X86::RSI; break;
5879 case X86::DI: DestReg = X86::RDI; break;
5880 case X86::BP: DestReg = X86::RBP; break;
5881 case X86::SP: DestReg = X86::RSP; break;
5882 }
5883 if (DestReg) {
5884 Res.first = DestReg;
5885 Res.second = Res.second = X86::GR64RegisterClass;
5886 }
5887 }
5888
5889 return Res;
5890}