blob: 3e1f9428c912600a3ff493d7e86aeb91014ffd95 [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"
Evan Cheng2e28d622008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000040#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/StringExtras.h"
Duncan Sandsd8455ca2007-07-27 20:02:49 +000042#include "llvm/ParameterAttributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043using namespace llvm;
44
45X86TargetLowering::X86TargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
47 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000048 X86ScalarSSEf64 = Subtarget->hasSSE2();
49 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000051
Chris Lattnerdec9cb52008-01-24 08:07:48 +000052 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 RegInfo = TM.getRegisterInfo();
55
56 // Set up the TargetLowering object.
57
58 // X86 is weird, it always uses i8 for shift amounts and setcc results.
59 setShiftAmountType(MVT::i8);
60 setSetCCResultType(MVT::i8);
61 setSetCCResultContents(ZeroOrOneSetCCResult);
62 setSchedulingPreference(SchedulingForRegPressure);
63 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
64 setStackPointerRegisterToSaveRestore(X86StackPtr);
65
66 if (Subtarget->isTargetDarwin()) {
67 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
68 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
70 } else if (Subtarget->isTargetMingw()) {
71 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
74 } else {
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
77 }
78
79 // Set up the register classes.
80 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
83 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
85
Duncan Sands082524c2008-01-23 20:39:46 +000086 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Chris Lattner3bc08502008-01-17 19:59:44 +000088 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 // operation.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
101
102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
105 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000106 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 else
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
111 }
112
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
117 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000118 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
125 }
126
Dale Johannesen958b08b2007-09-19 23:55:34 +0000127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 // this operation.
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000137 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } else {
142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
144 }
145
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 // conversion.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151
152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
155 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 else
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 }
165
166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000167 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
170 }
171
Dan Gohman5a199552007-10-08 18:33:35 +0000172 // Scalar integer multiply, multiply-high, divide, and remainder are
173 // lowered to use operations that produce two results, to match the
174 // available instructions. This exposes the two-result form to trivial
175 // CSE, which is able to combine x/y and x%y into a single instruction,
176 // for example. The single-result multiply instructions are introduced
177 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
178 // is not needed.
179 setOperationAction(ISD::MUL , MVT::i8 , Expand);
180 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
181 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
182 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
183 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
184 setOperationAction(ISD::SREM , MVT::i8 , Expand);
185 setOperationAction(ISD::UREM , MVT::i8 , Expand);
186 setOperationAction(ISD::MUL , MVT::i16 , Expand);
187 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
188 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
189 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
190 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::SREM , MVT::i16 , Expand);
192 setOperationAction(ISD::UREM , MVT::i16 , Expand);
193 setOperationAction(ISD::MUL , MVT::i32 , Expand);
194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
200 setOperationAction(ISD::MUL , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
202 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
203 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
205 setOperationAction(ISD::SREM , MVT::i64 , Expand);
206 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
209 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
210 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
211 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
212 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
213 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
218 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
219 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000220 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000226 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
227 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000229 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
230 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 if (Subtarget->is64Bit()) {
232 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000233 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
234 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 }
236
237 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
238 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
239
240 // These should be promoted to a larger select which is supported.
241 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
242 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
243 // X86 wants to expand cmov itself.
244 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
245 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000248 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000254 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
257 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
258 }
259 // X86 ret instruction may pop stack.
260 setOperationAction(ISD::RET , MVT::Other, Custom);
261 if (!Subtarget->is64Bit())
262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
263
264 // Darwin ABI issue.
265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
269 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
270 if (Subtarget->is64Bit()) {
271 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
272 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
273 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
274 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
275 }
276 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
277 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
278 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
279 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
280 // X86 wants to expand memset / memcpy itself.
281 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
282 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
283
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000284 if (!Subtarget->hasSSE2())
285 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
286
287
Evan Cheng2e28d622008-02-02 04:07:54 +0000288 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 // FIXME - use subtarget debug flags
291 if (!Subtarget->isTargetDarwin() &&
292 !Subtarget->isTargetELF() &&
293 !Subtarget->isTargetCygMing())
294 setOperationAction(ISD::LABEL, MVT::Other, Expand);
295
296 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
297 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
298 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
299 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
300 if (Subtarget->is64Bit()) {
301 // FIXME: Verify
302 setExceptionPointerRegister(X86::RAX);
303 setExceptionSelectorRegister(X86::RDX);
304 } else {
305 setExceptionPointerRegister(X86::EAX);
306 setExceptionSelectorRegister(X86::EDX);
307 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000308 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309
Duncan Sands7407a9f2007-09-11 14:10:23 +0000310 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000311
Chris Lattner56b941f2008-01-15 21:58:22 +0000312 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000313
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
315 setOperationAction(ISD::VASTART , MVT::Other, Custom);
316 setOperationAction(ISD::VAARG , MVT::Other, Expand);
317 setOperationAction(ISD::VAEND , MVT::Other, Expand);
318 if (Subtarget->is64Bit())
319 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
320 else
321 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
322
323 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
324 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
325 if (Subtarget->is64Bit())
326 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
327 if (Subtarget->isTargetCygMing())
328 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
329 else
330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
331
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000332 if (X86ScalarSSEf64) {
333 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 // Set up the FP register classes.
335 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
336 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
337
338 // Use ANDPD to simulate FABS.
339 setOperationAction(ISD::FABS , MVT::f64, Custom);
340 setOperationAction(ISD::FABS , MVT::f32, Custom);
341
342 // Use XORP to simulate FNEG.
343 setOperationAction(ISD::FNEG , MVT::f64, Custom);
344 setOperationAction(ISD::FNEG , MVT::f32, Custom);
345
346 // Use ANDPD and ORPD to simulate FCOPYSIGN.
347 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
348 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
349
350 // We don't support sin/cos/fmod
351 setOperationAction(ISD::FSIN , MVT::f64, Expand);
352 setOperationAction(ISD::FCOS , MVT::f64, Expand);
353 setOperationAction(ISD::FREM , MVT::f64, Expand);
354 setOperationAction(ISD::FSIN , MVT::f32, Expand);
355 setOperationAction(ISD::FCOS , MVT::f32, Expand);
356 setOperationAction(ISD::FREM , MVT::f32, Expand);
357
358 // Expand FP immediates into loads from the stack, except for the special
359 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000360 addLegalFPImmediate(APFloat(+0.0)); // xorpd
361 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000362
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000363 // Floating truncations from f80 and extensions to f80 go through memory.
364 // If optimizing, we lie about this though and handle it in
365 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
366 if (Fast) {
367 setConvertAction(MVT::f32, MVT::f80, Expand);
368 setConvertAction(MVT::f64, MVT::f80, Expand);
369 setConvertAction(MVT::f80, MVT::f32, Expand);
370 setConvertAction(MVT::f80, MVT::f64, Expand);
371 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000372 } else if (X86ScalarSSEf32) {
373 // Use SSE for f32, x87 for f64.
374 // Set up the FP register classes.
375 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
376 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
377
378 // Use ANDPS to simulate FABS.
379 setOperationAction(ISD::FABS , MVT::f32, Custom);
380
381 // Use XORP to simulate FNEG.
382 setOperationAction(ISD::FNEG , MVT::f32, Custom);
383
384 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
385
386 // Use ANDPS and ORPS to simulate FCOPYSIGN.
387 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
388 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
389
390 // We don't support sin/cos/fmod
391 setOperationAction(ISD::FSIN , MVT::f32, Expand);
392 setOperationAction(ISD::FCOS , MVT::f32, Expand);
393 setOperationAction(ISD::FREM , MVT::f32, Expand);
394
Nate Begemane2ba64f2008-02-14 08:57:00 +0000395 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000396 addLegalFPImmediate(APFloat(+0.0f)); // xorps
397 addLegalFPImmediate(APFloat(+0.0)); // FLD0
398 addLegalFPImmediate(APFloat(+1.0)); // FLD1
399 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
400 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
401
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000402 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
403 // this though and handle it in InstructionSelectPreprocess so that
404 // dagcombine2 can hack on these.
405 if (Fast) {
406 setConvertAction(MVT::f32, MVT::f64, Expand);
407 setConvertAction(MVT::f32, MVT::f80, Expand);
408 setConvertAction(MVT::f80, MVT::f32, Expand);
409 setConvertAction(MVT::f64, MVT::f32, Expand);
410 // And x87->x87 truncations also.
411 setConvertAction(MVT::f80, MVT::f64, Expand);
412 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000413
414 if (!UnsafeFPMath) {
415 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
416 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
417 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000419 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 // Set up the FP register classes.
421 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
422 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
423
424 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
425 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
426 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
427 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000428
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000429 // Floating truncations go through memory. If optimizing, we lie about
430 // this though and handle it in InstructionSelectPreprocess so that
431 // dagcombine2 can hack on these.
432 if (Fast) {
433 setConvertAction(MVT::f80, MVT::f32, Expand);
434 setConvertAction(MVT::f64, MVT::f32, Expand);
435 setConvertAction(MVT::f80, MVT::f64, Expand);
436 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437
438 if (!UnsafeFPMath) {
439 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
440 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
441 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000442 addLegalFPImmediate(APFloat(+0.0)); // FLD0
443 addLegalFPImmediate(APFloat(+1.0)); // FLD1
444 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
445 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000446 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
447 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
448 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
449 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 }
451
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000452 // Long double always uses X87.
453 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000454 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
455 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000456 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000457 APFloat TmpFlt(+0.0);
458 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
459 addLegalFPImmediate(TmpFlt); // FLD0
460 TmpFlt.changeSign();
461 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
462 APFloat TmpFlt2(+1.0);
463 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
464 addLegalFPImmediate(TmpFlt2); // FLD1
465 TmpFlt2.changeSign();
466 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
467 }
468
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000469 if (!UnsafeFPMath) {
470 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
471 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
472 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000473
Dan Gohman2f7b1982007-10-11 23:21:31 +0000474 // Always use a library call for pow.
475 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
476 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
477 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
478
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 // First set operation action for all vector types to expand. Then we
480 // will selectively turn on ones that can be effectively codegen'd.
481 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
482 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
483 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
485 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
486 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000506 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000510 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000511 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000514 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 }
521
522 if (Subtarget->hasMMX()) {
523 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
524 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
525 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
526 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
527
528 // FIXME: add MMX packed arithmetics
529
530 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
531 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
532 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
533 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
534
535 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
536 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
537 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000538 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539
540 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
541 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
542
543 setOperationAction(ISD::AND, MVT::v8i8, Promote);
544 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
545 setOperationAction(ISD::AND, MVT::v4i16, Promote);
546 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
547 setOperationAction(ISD::AND, MVT::v2i32, Promote);
548 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
549 setOperationAction(ISD::AND, MVT::v1i64, Legal);
550
551 setOperationAction(ISD::OR, MVT::v8i8, Promote);
552 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
553 setOperationAction(ISD::OR, MVT::v4i16, Promote);
554 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
555 setOperationAction(ISD::OR, MVT::v2i32, Promote);
556 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
557 setOperationAction(ISD::OR, MVT::v1i64, Legal);
558
559 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
560 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
561 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
562 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
563 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
564 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
565 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
566
567 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
568 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
569 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
570 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
571 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
572 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
573 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
574
575 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
576 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
577 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
578 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
579
580 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
581 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
582 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
583 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
584
585 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
586 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
587 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
588 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
589 }
590
591 if (Subtarget->hasSSE1()) {
592 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
593
594 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
595 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
596 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
597 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
598 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
599 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
601 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
602 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
603 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
604 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
605 }
606
607 if (Subtarget->hasSSE2()) {
608 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
609 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
610 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
611 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
612 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
613
614 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
615 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
616 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
617 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
618 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
619 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
620 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
621 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
622 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
623 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
624 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
625 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
626 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
627 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
628 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629
630 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
631 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
632 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
633 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
635
636 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
637 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000638 // Do not attempt to custom lower non-power-of-2 vectors
639 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
640 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
642 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
643 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
644 }
645 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
646 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
647 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000649 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000651 if (Subtarget->is64Bit()) {
652 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000653 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000654 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655
656 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
657 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
658 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
659 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
660 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
661 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
662 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
663 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
664 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
665 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
666 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
667 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
668 }
669
Chris Lattner3bc08502008-01-17 19:59:44 +0000670 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000671
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 // Custom lower v2i64 and v2f64 selects.
673 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
674 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
675 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
676 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
677 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000678
679 if (Subtarget->hasSSE41()) {
680 // FIXME: Do we need to handle scalar-to-vector here?
681 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
682
683 // i8 and i16 vectors are custom , because the source register and source
684 // source memory operand types are not the same width. f32 vectors are
685 // custom since the immediate controlling the insert encodes additional
686 // information.
687 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
689 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
690 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
691
692 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
694 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
695 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
696
697 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000698 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000700 }
701 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702
703 // We want to custom lower some of our intrinsics.
704 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
705
706 // We have target-specific dag combine patterns for the following nodes:
707 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
708 setTargetDAGCombine(ISD::SELECT);
709
710 computeRegisterProperties();
711
712 // FIXME: These should be based on subtarget info. Plus, the values should
713 // be smaller when we are in optimizing for size mode.
714 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
715 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
716 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
717 allowUnalignedMemoryAccesses = true; // x86 supports it!
718}
719
Evan Cheng5a67b812008-01-23 23:17:41 +0000720/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
721/// the desired ByVal argument alignment.
722static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
723 if (MaxAlign == 16)
724 return;
725 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
726 if (VTy->getBitWidth() == 128)
727 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000728 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
729 unsigned EltAlign = 0;
730 getMaxByValAlign(ATy->getElementType(), EltAlign);
731 if (EltAlign > MaxAlign)
732 MaxAlign = EltAlign;
733 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
734 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
735 unsigned EltAlign = 0;
736 getMaxByValAlign(STy->getElementType(i), EltAlign);
737 if (EltAlign > MaxAlign)
738 MaxAlign = EltAlign;
739 if (MaxAlign == 16)
740 break;
741 }
742 }
743 return;
744}
745
746/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
747/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000748/// that contain SSE vectors are placed at 16-byte boundaries while the rest
749/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000750unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
751 if (Subtarget->is64Bit())
752 return getTargetData()->getABITypeAlignment(Ty);
753 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000754 if (Subtarget->hasSSE1())
755 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000756 return Align;
757}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758
Evan Cheng6fb06762007-11-09 01:32:10 +0000759/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
760/// jumptable.
761SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
762 SelectionDAG &DAG) const {
763 if (usesGlobalOffsetTable())
764 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
765 if (!Subtarget->isPICStyleRIPRel())
766 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
767 return Table;
768}
769
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770//===----------------------------------------------------------------------===//
771// Return Value Calling Convention Implementation
772//===----------------------------------------------------------------------===//
773
774#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000775
776/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
777/// exists skip possible ISD:TokenFactor.
778static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000779 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000780 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000781 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000782 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000783 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000784 return Chain.getOperand(0);
785 }
786 return Chain;
787}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000788
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789/// LowerRET - Lower an ISD::RET node.
790SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
791 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
792
793 SmallVector<CCValAssign, 16> RVLocs;
794 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
795 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
796 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
797 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000798
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 // If this is the first return lowered for this function, add the regs to the
800 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000801 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 for (unsigned i = 0; i != RVLocs.size(); ++i)
803 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000804 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000808 // Handle tail call return.
809 Chain = GetPossiblePreceedingTailCall(Chain);
810 if (Chain.getOpcode() == X86ISD::TAILCALL) {
811 SDOperand TailCall = Chain;
812 SDOperand TargetAddress = TailCall.getOperand(1);
813 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000814 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000815 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
816 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
817 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
818 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
819 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000820 assert(StackAdjustment.getOpcode() == ISD::Constant &&
821 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000822
823 SmallVector<SDOperand,8> Operands;
824 Operands.push_back(Chain.getOperand(0));
825 Operands.push_back(TargetAddress);
826 Operands.push_back(StackAdjustment);
827 // Copy registers used by the call. Last operand is a flag so it is not
828 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000829 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000830 Operands.push_back(Chain.getOperand(i));
831 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000832 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
833 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000834 }
835
836 // Regular return.
837 SDOperand Flag;
838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 // Copy the result values into the output registers.
840 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
841 RVLocs[0].getLocReg() != X86::ST0) {
842 for (unsigned i = 0; i != RVLocs.size(); ++i) {
843 CCValAssign &VA = RVLocs[i];
844 assert(VA.isRegLoc() && "Can only return in registers!");
845 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
846 Flag);
847 Flag = Chain.getValue(1);
848 }
849 } else {
850 // We need to handle a destination of ST0 specially, because it isn't really
851 // a register.
852 SDOperand Value = Op.getOperand(1);
853
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000854 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
855 // This will get legalized into a load/store if it can't get optimized away.
856 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
857 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858
859 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
860 SDOperand Ops[] = { Chain, Value };
861 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
862 Flag = Chain.getValue(1);
863 }
864
865 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
866 if (Flag.Val)
867 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
868 else
869 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
870}
871
872
873/// LowerCallResult - Lower the result values of an ISD::CALL into the
874/// appropriate copies out of appropriate physical registers. This assumes that
875/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
876/// being lowered. The returns a SDNode with the same number of values as the
877/// ISD::CALL.
878SDNode *X86TargetLowering::
879LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
880 unsigned CallingConv, SelectionDAG &DAG) {
881
882 // Assign locations to each value returned by this call.
883 SmallVector<CCValAssign, 16> RVLocs;
884 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
885 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
886 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
887
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 SmallVector<SDOperand, 8> ResultVals;
889
890 // Copy all of the result registers out of their specified physreg.
891 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
892 for (unsigned i = 0; i != RVLocs.size(); ++i) {
893 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
894 RVLocs[i].getValVT(), InFlag).getValue(1);
895 InFlag = Chain.getValue(2);
896 ResultVals.push_back(Chain.getValue(0));
897 }
898 } else {
899 // Copies from the FP stack are special, as ST0 isn't a valid register
900 // before the fp stackifier runs.
901
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000902 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
903 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
904 // the specified value type.
905 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
906 if (isScalarFPTypeInSSEReg(GetResultTy))
907 GetResultTy = MVT::f80;
908 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
909
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 SDOperand GROps[] = { Chain, InFlag };
911 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
912 Chain = RetVal.getValue(1);
913 InFlag = RetVal.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000914
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000915 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
916 // there.
917 if (GetResultTy != RVLocs[0].getValVT())
918 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
919 // This truncation won't change the value.
920 DAG.getIntPtrConstant(1));
921
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 ResultVals.push_back(RetVal);
923 }
924
925 // Merge everything together with a MERGE_VALUES node.
926 ResultVals.push_back(Chain);
927 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
928 &ResultVals[0], ResultVals.size()).Val;
929}
930
Evan Cheng931a8f42008-01-29 19:34:22 +0000931/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
932/// ISD::CALL where the results are known to be in two 64-bit registers,
933/// e.g. XMM0 and XMM1. This simplify store the two values back to the
934/// fixed stack slot allocated for StructRet.
935SDNode *X86TargetLowering::
936LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
937 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
938 MVT::ValueType VT, SelectionDAG &DAG) {
939 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
940 Chain = RetVal1.getValue(1);
941 InFlag = RetVal1.getValue(2);
942 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
943 Chain = RetVal2.getValue(1);
944 InFlag = RetVal2.getValue(2);
945 SDOperand FIN = TheCall->getOperand(5);
946 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
947 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
948 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
949 return Chain.Val;
950}
951
952/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
953/// where the results are known to be in ST0 and ST1.
954SDNode *X86TargetLowering::
955LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
956 SDNode *TheCall, SelectionDAG &DAG) {
957 SmallVector<SDOperand, 8> ResultVals;
958 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
959 SDVTList Tys = DAG.getVTList(VTs, 4);
960 SDOperand Ops[] = { Chain, InFlag };
961 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
962 Chain = RetVal.getValue(2);
963 SDOperand FIN = TheCall->getOperand(5);
964 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
965 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
966 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
967 return Chain.Val;
968}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969
970//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000971// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972//===----------------------------------------------------------------------===//
973// StdCall calling convention seems to be standard for many Windows' API
974// routines and around. It differs from C calling convention just a little:
975// callee should clean up the stack, not caller. Symbols should be also
976// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000977// For info on fast calling convention see Fast Calling Convention (tail call)
978// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979
980/// AddLiveIn - This helper function adds the specified physical register to the
981/// MachineFunction as a live in value. It also creates a corresponding virtual
982/// register for it.
983static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
984 const TargetRegisterClass *RC) {
985 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000986 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
987 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 return VReg;
989}
990
Gordon Henriksen18ace102008-01-05 16:56:59 +0000991// Determines whether a CALL node uses struct return semantics.
992static bool CallIsStructReturn(SDOperand Op) {
993 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
994 if (!NumOps)
995 return false;
996
997 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
998 return Flags->getValue() & ISD::ParamFlags::StructReturn;
999}
1000
1001// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
1002static bool ArgsAreStructReturn(SDOperand Op) {
1003 unsigned NumArgs = Op.Val->getNumValues() - 1;
1004 if (!NumArgs)
1005 return false;
1006
1007 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1008 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1009}
1010
1011// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
1012// its own arguments. Callee pop is necessary to support tail calls.
1013bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1014 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1015 if (IsVarArg)
1016 return false;
1017
1018 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1019 default:
1020 return false;
1021 case CallingConv::X86_StdCall:
1022 return !Subtarget->is64Bit();
1023 case CallingConv::X86_FastCall:
1024 return !Subtarget->is64Bit();
1025 case CallingConv::Fast:
1026 return PerformTailCallOpt;
1027 }
1028}
1029
1030// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
1031CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1032 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1033
1034 if (Subtarget->is64Bit())
1035 if (CC == CallingConv::Fast && PerformTailCallOpt)
1036 return CC_X86_64_TailCall;
1037 else
1038 return CC_X86_64_C;
1039
1040 if (CC == CallingConv::X86_FastCall)
1041 return CC_X86_32_FastCall;
1042 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1043 return CC_X86_32_TailCall;
1044 else
1045 return CC_X86_32_C;
1046}
1047
1048// Selects the appropriate decoration to apply to a MachineFunction containing a
1049// given FORMAL_ARGUMENTS node.
1050NameDecorationStyle
1051X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1052 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1053 if (CC == CallingConv::X86_FastCall)
1054 return FastCall;
1055 else if (CC == CallingConv::X86_StdCall)
1056 return StdCall;
1057 return None;
1058}
1059
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001060
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001061// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly
1062// be overwritten when lowering the outgoing arguments in a tail call. Currently
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001063// the implementation of this call is very conservative and assumes all
1064// arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001065// registers would be overwritten by direct lowering.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001066// Possible improvement:
1067// Check FORMAL_ARGUMENTS corresponding MERGE_VALUES for CopyFromReg nodes
1068// indicating inreg passed arguments which also need not be lowered to a safe
1069// stack slot.
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001070static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001071 RegisterSDNode * OpReg = NULL;
1072 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1073 (Op.getOpcode()== ISD::CopyFromReg &&
1074 (OpReg = cast<RegisterSDNode>(Op.getOperand(1))) &&
Dan Gohman1e57df32008-02-10 18:45:23 +00001075 OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001076 return true;
1077 return false;
1078}
1079
Evan Cheng5817a0e2008-01-12 01:08:07 +00001080// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1081// by "Src" to address "Dst" with size and alignment information specified by
1082// the specific parameter attribute. The copy will be passed as a byval function
1083// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001084static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001085CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1086 unsigned Flags, SelectionDAG &DAG) {
1087 unsigned Align = 1 <<
1088 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1089 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001090 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001091 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1092 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001093 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001094 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001095}
1096
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001097SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1098 const CCValAssign &VA,
1099 MachineFrameInfo *MFI,
1100 SDOperand Root, unsigned i) {
1101 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001102 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1103 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1104
1105 // FIXME: For now, all byval parameter objects are marked mutable. This
1106 // can be changed with more analysis.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001107 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Evan Cheng3e42a522008-01-10 02:24:25 +00001108 VA.getLocMemOffset(), !isByVal);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001109 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001110 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001111 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001112 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001113 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001114}
1115
Gordon Henriksen18ace102008-01-05 16:56:59 +00001116SDOperand
1117X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001119 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1120
1121 const Function* Fn = MF.getFunction();
1122 if (Fn->hasExternalLinkage() &&
1123 Subtarget->isTargetCygMing() &&
1124 Fn->getName() == "main")
1125 FuncInfo->setForceFramePointer(true);
1126
1127 // Decorate the function name.
1128 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1129
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 MachineFrameInfo *MFI = MF.getFrameInfo();
1131 SDOperand Root = Op.getOperand(0);
1132 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001133 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001134 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001135
1136 assert(!(isVarArg && CC == CallingConv::Fast) &&
1137 "Var args not supported with calling convention fastcc");
1138
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001139 // Assign locations to all of the incoming arguments.
1140 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001141 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001142 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001143
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144 SmallVector<SDOperand, 8> ArgValues;
1145 unsigned LastVal = ~0U;
1146 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1147 CCValAssign &VA = ArgLocs[i];
1148 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1149 // places.
1150 assert(VA.getValNo() != LastVal &&
1151 "Don't support value assigned to multiple locs yet");
1152 LastVal = VA.getValNo();
1153
1154 if (VA.isRegLoc()) {
1155 MVT::ValueType RegVT = VA.getLocVT();
1156 TargetRegisterClass *RC;
1157 if (RegVT == MVT::i32)
1158 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001159 else if (Is64Bit && RegVT == MVT::i64)
1160 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001161 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001162 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001163 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001164 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 else {
1166 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001167 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1168 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1169 RegVT = MVT::i64;
1170 } else
1171 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001173
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1175 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1176
1177 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1178 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1179 // right size.
1180 if (VA.getLocInfo() == CCValAssign::SExt)
1181 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1182 DAG.getValueType(VA.getValVT()));
1183 else if (VA.getLocInfo() == CCValAssign::ZExt)
1184 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1185 DAG.getValueType(VA.getValVT()));
1186
1187 if (VA.getLocInfo() != CCValAssign::Full)
1188 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1189
Gordon Henriksen18ace102008-01-05 16:56:59 +00001190 // Handle MMX values passed in GPRs.
1191 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1192 MVT::getSizeInBits(RegVT) == 64)
1193 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1194
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195 ArgValues.push_back(ArgValue);
1196 } else {
1197 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001198 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199 }
1200 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001203 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001204 if (CC == CallingConv::Fast)
1205 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206
1207 // If the function takes variable number of arguments, make a frame index for
1208 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001209 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001210 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1211 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1212 }
1213 if (Is64Bit) {
1214 static const unsigned GPR64ArgRegs[] = {
1215 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1216 };
1217 static const unsigned XMMArgRegs[] = {
1218 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1219 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1220 };
1221
1222 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1223 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1224
1225 // For X86-64, if there are vararg parameters that are passed via
1226 // registers, then we must store them to their spots on the stack so they
1227 // may be loaded by deferencing the result of va_next.
1228 VarArgsGPOffset = NumIntRegs * 8;
1229 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1230 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1231
1232 // Store the integer parameter registers.
1233 SmallVector<SDOperand, 8> MemOps;
1234 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1235 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001236 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001237 for (; NumIntRegs != 6; ++NumIntRegs) {
1238 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1239 X86::GR64RegisterClass);
1240 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001241 SDOperand Store =
1242 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001243 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001244 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001245 MemOps.push_back(Store);
1246 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001247 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001248 }
1249
1250 // Now store the XMM (fp + vector) parameter registers.
1251 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001252 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001253 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1254 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1255 X86::VR128RegisterClass);
1256 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001257 SDOperand Store =
1258 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001259 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001260 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001261 MemOps.push_back(Store);
1262 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001263 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001264 }
1265 if (!MemOps.empty())
1266 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1267 &MemOps[0], MemOps.size());
1268 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001269 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001270
1271 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1272 // arguments and the arguments after the retaddr has been pushed are
1273 // aligned.
1274 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1275 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1276 (StackSize & 7) == 0)
1277 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001279 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001280
Gordon Henriksen18ace102008-01-05 16:56:59 +00001281 // Some CCs need callee pop.
1282 if (IsCalleePop(Op)) {
1283 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001284 BytesCallerReserves = 0;
1285 } else {
1286 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001288 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001289 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 BytesCallerReserves = StackSize;
1291 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001292
Gordon Henriksen18ace102008-01-05 16:56:59 +00001293 if (!Is64Bit) {
1294 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1295 if (CC == CallingConv::X86_FastCall)
1296 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1297 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298
Anton Korobeynikove844e472007-08-15 17:12:32 +00001299 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300
1301 // Return the new list of results.
1302 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1303 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1304}
1305
Evan Chengbc077bf2008-01-10 00:09:10 +00001306SDOperand
1307X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1308 const SDOperand &StackPtr,
1309 const CCValAssign &VA,
1310 SDOperand Chain,
1311 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001312 unsigned LocMemOffset = VA.getLocMemOffset();
1313 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001314 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1315 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1316 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1317 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001318 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001319 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001320 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001321 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001322}
1323
Evan Cheng931a8f42008-01-29 19:34:22 +00001324/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1325/// struct return call to the specified function. X86-64 ABI specifies
1326/// some SRet calls are actually returned in registers. Since current
1327/// LLVM cannot represent multi-value calls, they are represent as
1328/// calls where the results are passed in a hidden struct provided by
1329/// the caller. This function examines the type of the struct to
1330/// determine the correct way to implement the call.
1331X86::X86_64SRet
1332X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1333 // FIXME: Disabled for now.
1334 return X86::InMemory;
1335
1336 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1337 const Type *RTy = PTy->getElementType();
1338 unsigned Size = getTargetData()->getABITypeSize(RTy);
1339 if (Size != 16 && Size != 32)
1340 return X86::InMemory;
1341
1342 if (Size == 32) {
1343 const StructType *STy = dyn_cast<StructType>(RTy);
1344 if (!STy) return X86::InMemory;
1345 if (STy->getNumElements() == 2 &&
1346 STy->getElementType(0) == Type::X86_FP80Ty &&
1347 STy->getElementType(1) == Type::X86_FP80Ty)
1348 return X86::InX87;
1349 }
1350
1351 bool AllFP = true;
1352 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1353 I != E; ++I) {
1354 const Type *STy = I->get();
1355 if (!STy->isFPOrFPVector()) {
1356 AllFP = false;
1357 break;
1358 }
1359 }
1360
1361 if (AllFP)
1362 return X86::InSSE;
1363 return X86::InGPR64;
1364}
1365
1366void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1367 CCAssignFn *Fn,
1368 CCState &CCInfo) {
1369 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1370 for (unsigned i = 1; i != NumOps; ++i) {
1371 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1372 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1373 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1374 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1375 cerr << "Call operand #" << i << " has unhandled type "
1376 << MVT::getValueTypeString(ArgVT) << "\n";
1377 abort();
1378 }
1379 }
1380}
1381
Gordon Henriksen18ace102008-01-05 16:56:59 +00001382SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1383 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001385 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001387 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1388 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001390 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001391 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001392
1393 assert(!(isVarArg && CC == CallingConv::Fast) &&
1394 "Var args not supported with calling convention fastcc");
1395
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001396 // Analyze operands of the call, assigning locations to each operand.
1397 SmallVector<CCValAssign, 16> ArgLocs;
1398 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001399 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1400
1401 X86::X86_64SRet SRetMethod = X86::InMemory;
1402 if (Is64Bit && IsStructRet)
1403 // FIXME: We can't figure out type of the sret structure for indirect
1404 // calls. We need to copy more information from CallSite to the ISD::CALL
1405 // node.
1406 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1407 SRetMethod =
1408 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1409
1410 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1411 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1412 // a sret call.
1413 if (SRetMethod != X86::InMemory)
1414 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1415 else
1416 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417
1418 // Get a count of how many bytes are to be pushed on the stack.
1419 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001420 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001421 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422
Gordon Henriksen18ace102008-01-05 16:56:59 +00001423 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1424 // arguments and the arguments after the retaddr has been pushed are aligned.
1425 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1426 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1427 (NumBytes & 7) == 0)
1428 NumBytes += 4;
1429
1430 int FPDiff = 0;
1431 if (IsTailCall) {
1432 // Lower arguments at fp - stackoffset + fpdiff.
1433 unsigned NumBytesCallerPushed =
1434 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1435 FPDiff = NumBytesCallerPushed - NumBytes;
1436
1437 // Set the delta of movement of the returnaddr stackslot.
1438 // But only set if delta is greater than previous delta.
1439 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1440 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1441 }
1442
Chris Lattner5872a362008-01-17 07:00:52 +00001443 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444
Gordon Henriksen18ace102008-01-05 16:56:59 +00001445 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1446 if (IsTailCall) {
1447 // Adjust the Return address stack slot.
1448 if (FPDiff) {
1449 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1450 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1451 // Load the "old" Return address.
1452 RetAddrFrIdx =
1453 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1454 // Calculate the new stack slot for the return address.
1455 int SlotSize = Is64Bit ? 8 : 4;
1456 int NewReturnAddrFI =
1457 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1458 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1459 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1460 }
1461 }
1462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1464 SmallVector<SDOperand, 8> MemOpChains;
1465
1466 SDOperand StackPtr;
1467
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001468 // Walk the register/memloc assignments, inserting copies/loads. For tail
1469 // calls, lower arguments which could otherwise be possibly overwritten to the
1470 // stack slot where they would go on normal function calls.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1472 CCValAssign &VA = ArgLocs[i];
1473 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1474
1475 // Promote the value if needed.
1476 switch (VA.getLocInfo()) {
1477 default: assert(0 && "Unknown loc info!");
1478 case CCValAssign::Full: break;
1479 case CCValAssign::SExt:
1480 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1481 break;
1482 case CCValAssign::ZExt:
1483 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1484 break;
1485 case CCValAssign::AExt:
1486 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1487 break;
1488 }
1489
1490 if (VA.isRegLoc()) {
1491 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1492 } else {
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001493 if (!IsTailCall || IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001494 assert(VA.isMemLoc());
1495 if (StackPtr.Val == 0)
1496 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1497
1498 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1499 Arg));
1500 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 }
1502 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503
1504 if (!MemOpChains.empty())
1505 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1506 &MemOpChains[0], MemOpChains.size());
1507
1508 // Build a sequence of copy-to-reg nodes chained together with token chain
1509 // and flag operands which copy the outgoing args into registers.
1510 SDOperand InFlag;
1511 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1512 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1513 InFlag);
1514 InFlag = Chain.getValue(1);
1515 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001516
1517 if (IsTailCall)
1518 InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output?
1519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1521 // GOT pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001522 // Does not work with tail call since ebx is not restored correctly by
1523 // tailcaller. TODO: at least for x86 - verify for x86-64
1524 if (!IsTailCall && !Is64Bit &&
1525 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001526 Subtarget->isPICStyleGOT()) {
1527 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1528 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1529 InFlag);
1530 InFlag = Chain.getValue(1);
1531 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001532
Gordon Henriksen18ace102008-01-05 16:56:59 +00001533 if (Is64Bit && isVarArg) {
1534 // From AMD64 ABI document:
1535 // For calls that may call functions that use varargs or stdargs
1536 // (prototype-less calls or calls to functions containing ellipsis (...) in
1537 // the declaration) %al is used as hidden argument to specify the number
1538 // of SSE registers used. The contents of %al do not need to match exactly
1539 // the number of registers, but must be an ubound on the number of SSE
1540 // registers used and is in the range 0 - 8 inclusive.
1541
1542 // Count the number of XMM registers allocated.
1543 static const unsigned XMMArgRegs[] = {
1544 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1545 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1546 };
1547 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1548
1549 Chain = DAG.getCopyToReg(Chain, X86::AL,
1550 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1551 InFlag = Chain.getValue(1);
1552 }
1553
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001554 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001555 if (IsTailCall) {
1556 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001557 SDOperand FIN;
1558 int FI = 0;
1559 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1560 CCValAssign &VA = ArgLocs[i];
1561 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001562 assert(VA.isMemLoc());
1563 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001564 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1565 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001566 // Create frame index.
1567 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1568 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1569 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1570 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001571 SDOperand Source = Arg;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001572 if (IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001573 // Copy from stack slots to stack slot of a tail called function. This
1574 // needs to be done because if we would lower the arguments directly
1575 // to their real stack slot we might end up overwriting each other.
1576 // Get source stack slot.
Chris Lattner5872a362008-01-17 07:00:52 +00001577 Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001578 if (StackPtr.Val == 0)
1579 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1580 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1581 if ((Flags & ISD::ParamFlags::ByVal)==0)
Duncan Sands22981632008-01-13 21:20:29 +00001582 Source = DAG.getLoad(VA.getValVT(), Chain, Source, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001583 }
1584
Gordon Henriksen18ace102008-01-05 16:56:59 +00001585 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001586 // Copy relative to framepointer.
1587 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
1588 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001589 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001590 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001591 MemOpChains2.push_back(
1592 DAG.getStore(Chain, Source, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001593 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001594 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001595 }
1596 }
1597
1598 if (!MemOpChains2.empty())
1599 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001600 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001601
1602 // Store the return address to the appropriate stack slot.
1603 if (FPDiff)
1604 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1605 }
1606
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001607 // If the callee is a GlobalAddress node (quite common, every direct call is)
1608 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1609 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1610 // We should use extra load for direct calls to dllimported functions in
1611 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001612 if ((IsTailCall || !Is64Bit ||
1613 getTargetMachine().getCodeModel() != CodeModel::Large)
1614 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1615 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001616 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001617 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001618 if (IsTailCall || !Is64Bit ||
1619 getTargetMachine().getCodeModel() != CodeModel::Large)
1620 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1621 } else if (IsTailCall) {
1622 assert(Callee.getOpcode() == ISD::LOAD &&
1623 "Function destination must be loaded into virtual register");
1624 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1625
1626 Chain = DAG.getCopyToReg(Chain,
1627 DAG.getRegister(Opc, getPointerTy()) ,
1628 Callee,InFlag);
1629 Callee = DAG.getRegister(Opc, getPointerTy());
1630 // Add register as live out.
1631 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001632 }
1633
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 // Returns a chain & a flag for retval copy to use.
1635 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1636 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637
1638 if (IsTailCall) {
1639 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001640 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1641 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642 if (InFlag.Val)
1643 Ops.push_back(InFlag);
1644 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1645 InFlag = Chain.getValue(1);
1646
1647 // Returns a chain & a flag for retval copy to use.
1648 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1649 Ops.clear();
1650 }
1651
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001652 Ops.push_back(Chain);
1653 Ops.push_back(Callee);
1654
Gordon Henriksen18ace102008-01-05 16:56:59 +00001655 if (IsTailCall)
1656 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001657
1658 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001659 if (!IsTailCall && !Is64Bit &&
1660 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 Subtarget->isPICStyleGOT())
1662 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001663
Gordon Henriksen18ace102008-01-05 16:56:59 +00001664 // Add argument registers to the end of the list so that they are known live
1665 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001666 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1667 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1668 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001669
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001670 if (InFlag.Val)
1671 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001672
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673 if (IsTailCall) {
1674 assert(InFlag.Val &&
1675 "Flag must be set. Depend on flag being set in LowerRET");
1676 Chain = DAG.getNode(X86ISD::TAILCALL,
1677 Op.Val->getVTList(), &Ops[0], Ops.size());
1678
1679 return SDOperand(Chain.Val, Op.ResNo);
1680 }
1681
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001682 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001683 InFlag = Chain.getValue(1);
1684
1685 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001686 unsigned NumBytesForCalleeToPush;
1687 if (IsCalleePop(Op))
1688 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001689 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 // If this is is a call to a struct-return function, the callee
1691 // pops the hidden struct pointer, so we have to push it back.
1692 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001693 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001694 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001695 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001697 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001698 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001699 DAG.getIntPtrConstant(NumBytes),
1700 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001701 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001702 InFlag = Chain.getValue(1);
1703
1704 // Handle result values, copying them out of physregs into vregs that we
1705 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001706 switch (SRetMethod) {
1707 default:
1708 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1709 case X86::InGPR64:
1710 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1711 X86::RAX, X86::RDX,
1712 MVT::i64, DAG), Op.ResNo);
1713 case X86::InSSE:
1714 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1715 X86::XMM0, X86::XMM1,
1716 MVT::f64, DAG), Op.ResNo);
1717 case X86::InX87:
1718 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1719 Op.ResNo);
1720 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001721}
1722
1723
1724//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001725// Fast Calling Convention (tail call) implementation
1726//===----------------------------------------------------------------------===//
1727
1728// Like std call, callee cleans arguments, convention except that ECX is
1729// reserved for storing the tail called function address. Only 2 registers are
1730// free for argument passing (inreg). Tail call optimization is performed
1731// provided:
1732// * tailcallopt is enabled
1733// * caller/callee are fastcc
1734// * elf/pic is disabled OR
1735// * elf/pic enabled + callee is in module + callee has
1736// visibility protected or hidden
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001737// To keep the stack aligned according to platform abi the function
1738// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1739// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001740// If a tail called function callee has more arguments than the caller the
1741// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001742// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001743// original REtADDR, but before the saved framepointer or the spilled registers
1744// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1745// stack layout:
1746// arg1
1747// arg2
1748// RETADDR
1749// [ new RETADDR
1750// move area ]
1751// (possible EBP)
1752// ESI
1753// EDI
1754// local1 ..
1755
1756/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1757/// for a 16 byte align requirement.
1758unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1759 SelectionDAG& DAG) {
1760 if (PerformTailCallOpt) {
1761 MachineFunction &MF = DAG.getMachineFunction();
1762 const TargetMachine &TM = MF.getTarget();
1763 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1764 unsigned StackAlignment = TFI.getStackAlignment();
1765 uint64_t AlignMask = StackAlignment - 1;
1766 int64_t Offset = StackSize;
1767 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1768 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1769 // Number smaller than 12 so just add the difference.
1770 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1771 } else {
1772 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1773 Offset = ((~AlignMask) & Offset) + StackAlignment +
1774 (StackAlignment-SlotSize);
1775 }
1776 StackSize = Offset;
1777 }
1778 return StackSize;
1779}
1780
1781/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001782/// following the call is a return. A function is eligible if caller/callee
1783/// calling conventions match, currently only fastcc supports tail calls, and
1784/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001785bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1786 SDOperand Ret,
1787 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001788 if (!PerformTailCallOpt)
1789 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001790
1791 // Check whether CALL node immediatly preceeds the RET node and whether the
1792 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001793 unsigned NumOps = Ret.getNumOperands();
1794 if ((NumOps == 1 &&
1795 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1796 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001797 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001798 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1799 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001800 MachineFunction &MF = DAG.getMachineFunction();
1801 unsigned CallerCC = MF.getFunction()->getCallingConv();
1802 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1803 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1804 SDOperand Callee = Call.getOperand(4);
1805 // On elf/pic %ebx needs to be livein.
Evan Chenge7a87392007-11-02 01:26:22 +00001806 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1807 !Subtarget->isPICStyleGOT())
1808 return true;
1809
1810 // Can only do local tail calls with PIC.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001811 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1812 return G->getGlobal()->hasHiddenVisibility()
1813 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001814 }
1815 }
Evan Chenge7a87392007-11-02 01:26:22 +00001816
1817 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001818}
1819
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001820//===----------------------------------------------------------------------===//
1821// Other Lowering Hooks
1822//===----------------------------------------------------------------------===//
1823
1824
1825SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001826 MachineFunction &MF = DAG.getMachineFunction();
1827 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1828 int ReturnAddrIndex = FuncInfo->getRAIndex();
1829
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001830 if (ReturnAddrIndex == 0) {
1831 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001832 if (Subtarget->is64Bit())
1833 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1834 else
1835 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001836
1837 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001838 }
1839
1840 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1841}
1842
1843
1844
1845/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1846/// specific condition code. It returns a false if it cannot do a direct
1847/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1848/// needed.
1849static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1850 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1851 SelectionDAG &DAG) {
1852 X86CC = X86::COND_INVALID;
1853 if (!isFP) {
1854 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1855 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1856 // X > -1 -> X == 0, jump !sign.
1857 RHS = DAG.getConstant(0, RHS.getValueType());
1858 X86CC = X86::COND_NS;
1859 return true;
1860 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1861 // X < 0 -> X == 0, jump on sign.
1862 X86CC = X86::COND_S;
1863 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001864 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1865 // X < 1 -> X <= 0
1866 RHS = DAG.getConstant(0, RHS.getValueType());
1867 X86CC = X86::COND_LE;
1868 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 }
1870 }
1871
1872 switch (SetCCOpcode) {
1873 default: break;
1874 case ISD::SETEQ: X86CC = X86::COND_E; break;
1875 case ISD::SETGT: X86CC = X86::COND_G; break;
1876 case ISD::SETGE: X86CC = X86::COND_GE; break;
1877 case ISD::SETLT: X86CC = X86::COND_L; break;
1878 case ISD::SETLE: X86CC = X86::COND_LE; break;
1879 case ISD::SETNE: X86CC = X86::COND_NE; break;
1880 case ISD::SETULT: X86CC = X86::COND_B; break;
1881 case ISD::SETUGT: X86CC = X86::COND_A; break;
1882 case ISD::SETULE: X86CC = X86::COND_BE; break;
1883 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1884 }
1885 } else {
1886 // On a floating point condition, the flags are set as follows:
1887 // ZF PF CF op
1888 // 0 | 0 | 0 | X > Y
1889 // 0 | 0 | 1 | X < Y
1890 // 1 | 0 | 0 | X == Y
1891 // 1 | 1 | 1 | unordered
1892 bool Flip = false;
1893 switch (SetCCOpcode) {
1894 default: break;
1895 case ISD::SETUEQ:
1896 case ISD::SETEQ: X86CC = X86::COND_E; break;
1897 case ISD::SETOLT: Flip = true; // Fallthrough
1898 case ISD::SETOGT:
1899 case ISD::SETGT: X86CC = X86::COND_A; break;
1900 case ISD::SETOLE: Flip = true; // Fallthrough
1901 case ISD::SETOGE:
1902 case ISD::SETGE: X86CC = X86::COND_AE; break;
1903 case ISD::SETUGT: Flip = true; // Fallthrough
1904 case ISD::SETULT:
1905 case ISD::SETLT: X86CC = X86::COND_B; break;
1906 case ISD::SETUGE: Flip = true; // Fallthrough
1907 case ISD::SETULE:
1908 case ISD::SETLE: X86CC = X86::COND_BE; break;
1909 case ISD::SETONE:
1910 case ISD::SETNE: X86CC = X86::COND_NE; break;
1911 case ISD::SETUO: X86CC = X86::COND_P; break;
1912 case ISD::SETO: X86CC = X86::COND_NP; break;
1913 }
1914 if (Flip)
1915 std::swap(LHS, RHS);
1916 }
1917
1918 return X86CC != X86::COND_INVALID;
1919}
1920
1921/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1922/// code. Current x86 isa includes the following FP cmov instructions:
1923/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1924static bool hasFPCMov(unsigned X86CC) {
1925 switch (X86CC) {
1926 default:
1927 return false;
1928 case X86::COND_B:
1929 case X86::COND_BE:
1930 case X86::COND_E:
1931 case X86::COND_P:
1932 case X86::COND_A:
1933 case X86::COND_AE:
1934 case X86::COND_NE:
1935 case X86::COND_NP:
1936 return true;
1937 }
1938}
1939
1940/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1941/// true if Op is undef or if its value falls within the specified range (L, H].
1942static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1943 if (Op.getOpcode() == ISD::UNDEF)
1944 return true;
1945
1946 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1947 return (Val >= Low && Val < Hi);
1948}
1949
1950/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1951/// true if Op is undef or if its value equal to the specified value.
1952static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1953 if (Op.getOpcode() == ISD::UNDEF)
1954 return true;
1955 return cast<ConstantSDNode>(Op)->getValue() == Val;
1956}
1957
1958/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1959/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1960bool X86::isPSHUFDMask(SDNode *N) {
1961 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1962
Dan Gohman7dc19012007-08-02 21:17:01 +00001963 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964 return false;
1965
1966 // Check if the value doesn't reference the second vector.
1967 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1968 SDOperand Arg = N->getOperand(i);
1969 if (Arg.getOpcode() == ISD::UNDEF) continue;
1970 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00001971 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001972 return false;
1973 }
1974
1975 return true;
1976}
1977
1978/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1979/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1980bool X86::isPSHUFHWMask(SDNode *N) {
1981 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1982
1983 if (N->getNumOperands() != 8)
1984 return false;
1985
1986 // Lower quadword copied in order.
1987 for (unsigned i = 0; i != 4; ++i) {
1988 SDOperand Arg = N->getOperand(i);
1989 if (Arg.getOpcode() == ISD::UNDEF) continue;
1990 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1991 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1992 return false;
1993 }
1994
1995 // Upper quadword shuffled.
1996 for (unsigned i = 4; i != 8; ++i) {
1997 SDOperand Arg = N->getOperand(i);
1998 if (Arg.getOpcode() == ISD::UNDEF) continue;
1999 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2000 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2001 if (Val < 4 || Val > 7)
2002 return false;
2003 }
2004
2005 return true;
2006}
2007
2008/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2009/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2010bool X86::isPSHUFLWMask(SDNode *N) {
2011 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2012
2013 if (N->getNumOperands() != 8)
2014 return false;
2015
2016 // Upper quadword copied in order.
2017 for (unsigned i = 4; i != 8; ++i)
2018 if (!isUndefOrEqual(N->getOperand(i), i))
2019 return false;
2020
2021 // Lower quadword shuffled.
2022 for (unsigned i = 0; i != 4; ++i)
2023 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2024 return false;
2025
2026 return true;
2027}
2028
2029/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2030/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2031static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2032 if (NumElems != 2 && NumElems != 4) return false;
2033
2034 unsigned Half = NumElems / 2;
2035 for (unsigned i = 0; i < Half; ++i)
2036 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2037 return false;
2038 for (unsigned i = Half; i < NumElems; ++i)
2039 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2040 return false;
2041
2042 return true;
2043}
2044
2045bool X86::isSHUFPMask(SDNode *N) {
2046 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2047 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2048}
2049
2050/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2051/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2052/// half elements to come from vector 1 (which would equal the dest.) and
2053/// the upper half to come from vector 2.
2054static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2055 if (NumOps != 2 && NumOps != 4) return false;
2056
2057 unsigned Half = NumOps / 2;
2058 for (unsigned i = 0; i < Half; ++i)
2059 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2060 return false;
2061 for (unsigned i = Half; i < NumOps; ++i)
2062 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2063 return false;
2064 return true;
2065}
2066
2067static bool isCommutedSHUFP(SDNode *N) {
2068 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2069 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2070}
2071
2072/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2073/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2074bool X86::isMOVHLPSMask(SDNode *N) {
2075 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2076
2077 if (N->getNumOperands() != 4)
2078 return false;
2079
2080 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2081 return isUndefOrEqual(N->getOperand(0), 6) &&
2082 isUndefOrEqual(N->getOperand(1), 7) &&
2083 isUndefOrEqual(N->getOperand(2), 2) &&
2084 isUndefOrEqual(N->getOperand(3), 3);
2085}
2086
2087/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2088/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2089/// <2, 3, 2, 3>
2090bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2091 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2092
2093 if (N->getNumOperands() != 4)
2094 return false;
2095
2096 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2097 return isUndefOrEqual(N->getOperand(0), 2) &&
2098 isUndefOrEqual(N->getOperand(1), 3) &&
2099 isUndefOrEqual(N->getOperand(2), 2) &&
2100 isUndefOrEqual(N->getOperand(3), 3);
2101}
2102
2103/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2104/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2105bool X86::isMOVLPMask(SDNode *N) {
2106 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2107
2108 unsigned NumElems = N->getNumOperands();
2109 if (NumElems != 2 && NumElems != 4)
2110 return false;
2111
2112 for (unsigned i = 0; i < NumElems/2; ++i)
2113 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2114 return false;
2115
2116 for (unsigned i = NumElems/2; i < NumElems; ++i)
2117 if (!isUndefOrEqual(N->getOperand(i), i))
2118 return false;
2119
2120 return true;
2121}
2122
2123/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2124/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2125/// and MOVLHPS.
2126bool X86::isMOVHPMask(SDNode *N) {
2127 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2128
2129 unsigned NumElems = N->getNumOperands();
2130 if (NumElems != 2 && NumElems != 4)
2131 return false;
2132
2133 for (unsigned i = 0; i < NumElems/2; ++i)
2134 if (!isUndefOrEqual(N->getOperand(i), i))
2135 return false;
2136
2137 for (unsigned i = 0; i < NumElems/2; ++i) {
2138 SDOperand Arg = N->getOperand(i + NumElems/2);
2139 if (!isUndefOrEqual(Arg, i + NumElems))
2140 return false;
2141 }
2142
2143 return true;
2144}
2145
2146/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2147/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2148bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2149 bool V2IsSplat = false) {
2150 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2151 return false;
2152
2153 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2154 SDOperand BitI = Elts[i];
2155 SDOperand BitI1 = Elts[i+1];
2156 if (!isUndefOrEqual(BitI, j))
2157 return false;
2158 if (V2IsSplat) {
2159 if (isUndefOrEqual(BitI1, NumElts))
2160 return false;
2161 } else {
2162 if (!isUndefOrEqual(BitI1, j + NumElts))
2163 return false;
2164 }
2165 }
2166
2167 return true;
2168}
2169
2170bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2171 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2172 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2173}
2174
2175/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2176/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2177bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2178 bool V2IsSplat = false) {
2179 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2180 return false;
2181
2182 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2183 SDOperand BitI = Elts[i];
2184 SDOperand BitI1 = Elts[i+1];
2185 if (!isUndefOrEqual(BitI, j + NumElts/2))
2186 return false;
2187 if (V2IsSplat) {
2188 if (isUndefOrEqual(BitI1, NumElts))
2189 return false;
2190 } else {
2191 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2192 return false;
2193 }
2194 }
2195
2196 return true;
2197}
2198
2199bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2200 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2201 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2202}
2203
2204/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2205/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2206/// <0, 0, 1, 1>
2207bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2208 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2209
2210 unsigned NumElems = N->getNumOperands();
2211 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2212 return false;
2213
2214 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2215 SDOperand BitI = N->getOperand(i);
2216 SDOperand BitI1 = N->getOperand(i+1);
2217
2218 if (!isUndefOrEqual(BitI, j))
2219 return false;
2220 if (!isUndefOrEqual(BitI1, j))
2221 return false;
2222 }
2223
2224 return true;
2225}
2226
2227/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2228/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2229/// <2, 2, 3, 3>
2230bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2231 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2232
2233 unsigned NumElems = N->getNumOperands();
2234 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2235 return false;
2236
2237 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2238 SDOperand BitI = N->getOperand(i);
2239 SDOperand BitI1 = N->getOperand(i + 1);
2240
2241 if (!isUndefOrEqual(BitI, j))
2242 return false;
2243 if (!isUndefOrEqual(BitI1, j))
2244 return false;
2245 }
2246
2247 return true;
2248}
2249
2250/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2251/// specifies a shuffle of elements that is suitable for input to MOVSS,
2252/// MOVSD, and MOVD, i.e. setting the lowest element.
2253static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002254 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002255 return false;
2256
2257 if (!isUndefOrEqual(Elts[0], NumElts))
2258 return false;
2259
2260 for (unsigned i = 1; i < NumElts; ++i) {
2261 if (!isUndefOrEqual(Elts[i], i))
2262 return false;
2263 }
2264
2265 return true;
2266}
2267
2268bool X86::isMOVLMask(SDNode *N) {
2269 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2270 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2271}
2272
2273/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2274/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2275/// element of vector 2 and the other elements to come from vector 1 in order.
2276static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2277 bool V2IsSplat = false,
2278 bool V2IsUndef = false) {
2279 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2280 return false;
2281
2282 if (!isUndefOrEqual(Ops[0], 0))
2283 return false;
2284
2285 for (unsigned i = 1; i < NumOps; ++i) {
2286 SDOperand Arg = Ops[i];
2287 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2288 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2289 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2290 return false;
2291 }
2292
2293 return true;
2294}
2295
2296static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2297 bool V2IsUndef = false) {
2298 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2299 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2300 V2IsSplat, V2IsUndef);
2301}
2302
2303/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2304/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2305bool X86::isMOVSHDUPMask(SDNode *N) {
2306 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2307
2308 if (N->getNumOperands() != 4)
2309 return false;
2310
2311 // Expect 1, 1, 3, 3
2312 for (unsigned i = 0; i < 2; ++i) {
2313 SDOperand Arg = N->getOperand(i);
2314 if (Arg.getOpcode() == ISD::UNDEF) continue;
2315 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2316 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2317 if (Val != 1) return false;
2318 }
2319
2320 bool HasHi = false;
2321 for (unsigned i = 2; i < 4; ++i) {
2322 SDOperand Arg = N->getOperand(i);
2323 if (Arg.getOpcode() == ISD::UNDEF) continue;
2324 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2325 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2326 if (Val != 3) return false;
2327 HasHi = true;
2328 }
2329
2330 // Don't use movshdup if it can be done with a shufps.
2331 return HasHi;
2332}
2333
2334/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2335/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2336bool X86::isMOVSLDUPMask(SDNode *N) {
2337 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2338
2339 if (N->getNumOperands() != 4)
2340 return false;
2341
2342 // Expect 0, 0, 2, 2
2343 for (unsigned i = 0; i < 2; ++i) {
2344 SDOperand Arg = N->getOperand(i);
2345 if (Arg.getOpcode() == ISD::UNDEF) continue;
2346 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2347 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2348 if (Val != 0) return false;
2349 }
2350
2351 bool HasHi = false;
2352 for (unsigned i = 2; i < 4; ++i) {
2353 SDOperand Arg = N->getOperand(i);
2354 if (Arg.getOpcode() == ISD::UNDEF) continue;
2355 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2356 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2357 if (Val != 2) return false;
2358 HasHi = true;
2359 }
2360
2361 // Don't use movshdup if it can be done with a shufps.
2362 return HasHi;
2363}
2364
2365/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2366/// specifies a identity operation on the LHS or RHS.
2367static bool isIdentityMask(SDNode *N, bool RHS = false) {
2368 unsigned NumElems = N->getNumOperands();
2369 for (unsigned i = 0; i < NumElems; ++i)
2370 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2371 return false;
2372 return true;
2373}
2374
2375/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2376/// a splat of a single element.
2377static bool isSplatMask(SDNode *N) {
2378 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2379
2380 // This is a splat operation if each element of the permute is the same, and
2381 // if the value doesn't reference the second vector.
2382 unsigned NumElems = N->getNumOperands();
2383 SDOperand ElementBase;
2384 unsigned i = 0;
2385 for (; i != NumElems; ++i) {
2386 SDOperand Elt = N->getOperand(i);
2387 if (isa<ConstantSDNode>(Elt)) {
2388 ElementBase = Elt;
2389 break;
2390 }
2391 }
2392
2393 if (!ElementBase.Val)
2394 return false;
2395
2396 for (; i != NumElems; ++i) {
2397 SDOperand Arg = N->getOperand(i);
2398 if (Arg.getOpcode() == ISD::UNDEF) continue;
2399 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2400 if (Arg != ElementBase) return false;
2401 }
2402
2403 // Make sure it is a splat of the first vector operand.
2404 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2405}
2406
2407/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2408/// a splat of a single element and it's a 2 or 4 element mask.
2409bool X86::isSplatMask(SDNode *N) {
2410 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2411
2412 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2413 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2414 return false;
2415 return ::isSplatMask(N);
2416}
2417
2418/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2419/// specifies a splat of zero element.
2420bool X86::isSplatLoMask(SDNode *N) {
2421 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2422
2423 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2424 if (!isUndefOrEqual(N->getOperand(i), 0))
2425 return false;
2426 return true;
2427}
2428
2429/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2430/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2431/// instructions.
2432unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2433 unsigned NumOperands = N->getNumOperands();
2434 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2435 unsigned Mask = 0;
2436 for (unsigned i = 0; i < NumOperands; ++i) {
2437 unsigned Val = 0;
2438 SDOperand Arg = N->getOperand(NumOperands-i-1);
2439 if (Arg.getOpcode() != ISD::UNDEF)
2440 Val = cast<ConstantSDNode>(Arg)->getValue();
2441 if (Val >= NumOperands) Val -= NumOperands;
2442 Mask |= Val;
2443 if (i != NumOperands - 1)
2444 Mask <<= Shift;
2445 }
2446
2447 return Mask;
2448}
2449
2450/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2451/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2452/// instructions.
2453unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2454 unsigned Mask = 0;
2455 // 8 nodes, but we only care about the last 4.
2456 for (unsigned i = 7; i >= 4; --i) {
2457 unsigned Val = 0;
2458 SDOperand Arg = N->getOperand(i);
2459 if (Arg.getOpcode() != ISD::UNDEF)
2460 Val = cast<ConstantSDNode>(Arg)->getValue();
2461 Mask |= (Val - 4);
2462 if (i != 4)
2463 Mask <<= 2;
2464 }
2465
2466 return Mask;
2467}
2468
2469/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2470/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2471/// instructions.
2472unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2473 unsigned Mask = 0;
2474 // 8 nodes, but we only care about the first 4.
2475 for (int i = 3; i >= 0; --i) {
2476 unsigned Val = 0;
2477 SDOperand Arg = N->getOperand(i);
2478 if (Arg.getOpcode() != ISD::UNDEF)
2479 Val = cast<ConstantSDNode>(Arg)->getValue();
2480 Mask |= Val;
2481 if (i != 0)
2482 Mask <<= 2;
2483 }
2484
2485 return Mask;
2486}
2487
2488/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2489/// specifies a 8 element shuffle that can be broken into a pair of
2490/// PSHUFHW and PSHUFLW.
2491static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2492 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2493
2494 if (N->getNumOperands() != 8)
2495 return false;
2496
2497 // Lower quadword shuffled.
2498 for (unsigned i = 0; i != 4; ++i) {
2499 SDOperand Arg = N->getOperand(i);
2500 if (Arg.getOpcode() == ISD::UNDEF) continue;
2501 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2502 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002503 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 return false;
2505 }
2506
2507 // Upper quadword shuffled.
2508 for (unsigned i = 4; i != 8; ++i) {
2509 SDOperand Arg = N->getOperand(i);
2510 if (Arg.getOpcode() == ISD::UNDEF) continue;
2511 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2512 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2513 if (Val < 4 || Val > 7)
2514 return false;
2515 }
2516
2517 return true;
2518}
2519
Chris Lattnere6aa3862007-11-25 00:24:49 +00002520/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521/// values in ther permute mask.
2522static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2523 SDOperand &V2, SDOperand &Mask,
2524 SelectionDAG &DAG) {
2525 MVT::ValueType VT = Op.getValueType();
2526 MVT::ValueType MaskVT = Mask.getValueType();
2527 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2528 unsigned NumElems = Mask.getNumOperands();
2529 SmallVector<SDOperand, 8> MaskVec;
2530
2531 for (unsigned i = 0; i != NumElems; ++i) {
2532 SDOperand Arg = Mask.getOperand(i);
2533 if (Arg.getOpcode() == ISD::UNDEF) {
2534 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2535 continue;
2536 }
2537 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2538 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2539 if (Val < NumElems)
2540 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2541 else
2542 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2543 }
2544
2545 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002546 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002547 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2548}
2549
Evan Chenga6769df2007-12-07 21:30:01 +00002550/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2551/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002552static
2553SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2554 MVT::ValueType MaskVT = Mask.getValueType();
2555 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2556 unsigned NumElems = Mask.getNumOperands();
2557 SmallVector<SDOperand, 8> MaskVec;
2558 for (unsigned i = 0; i != NumElems; ++i) {
2559 SDOperand Arg = Mask.getOperand(i);
2560 if (Arg.getOpcode() == ISD::UNDEF) {
2561 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2562 continue;
2563 }
2564 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2565 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2566 if (Val < NumElems)
2567 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2568 else
2569 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2570 }
2571 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2572}
2573
2574
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002575/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2576/// match movhlps. The lower half elements should come from upper half of
2577/// V1 (and in order), and the upper half elements should come from the upper
2578/// half of V2 (and in order).
2579static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2580 unsigned NumElems = Mask->getNumOperands();
2581 if (NumElems != 4)
2582 return false;
2583 for (unsigned i = 0, e = 2; i != e; ++i)
2584 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2585 return false;
2586 for (unsigned i = 2; i != 4; ++i)
2587 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2588 return false;
2589 return true;
2590}
2591
2592/// isScalarLoadToVector - Returns true if the node is a scalar load that
2593/// is promoted to a vector.
2594static inline bool isScalarLoadToVector(SDNode *N) {
2595 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2596 N = N->getOperand(0).Val;
2597 return ISD::isNON_EXTLoad(N);
2598 }
2599 return false;
2600}
2601
2602/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2603/// match movlp{s|d}. The lower half elements should come from lower half of
2604/// V1 (and in order), and the upper half elements should come from the upper
2605/// half of V2 (and in order). And since V1 will become the source of the
2606/// MOVLP, it must be either a vector load or a scalar load to vector.
2607static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2608 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2609 return false;
2610 // Is V2 is a vector load, don't do this transformation. We will try to use
2611 // load folding shufps op.
2612 if (ISD::isNON_EXTLoad(V2))
2613 return false;
2614
2615 unsigned NumElems = Mask->getNumOperands();
2616 if (NumElems != 2 && NumElems != 4)
2617 return false;
2618 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2619 if (!isUndefOrEqual(Mask->getOperand(i), i))
2620 return false;
2621 for (unsigned i = NumElems/2; i != NumElems; ++i)
2622 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2623 return false;
2624 return true;
2625}
2626
2627/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2628/// all the same.
2629static bool isSplatVector(SDNode *N) {
2630 if (N->getOpcode() != ISD::BUILD_VECTOR)
2631 return false;
2632
2633 SDOperand SplatValue = N->getOperand(0);
2634 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2635 if (N->getOperand(i) != SplatValue)
2636 return false;
2637 return true;
2638}
2639
2640/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2641/// to an undef.
2642static bool isUndefShuffle(SDNode *N) {
2643 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2644 return false;
2645
2646 SDOperand V1 = N->getOperand(0);
2647 SDOperand V2 = N->getOperand(1);
2648 SDOperand Mask = N->getOperand(2);
2649 unsigned NumElems = Mask.getNumOperands();
2650 for (unsigned i = 0; i != NumElems; ++i) {
2651 SDOperand Arg = Mask.getOperand(i);
2652 if (Arg.getOpcode() != ISD::UNDEF) {
2653 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2654 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2655 return false;
2656 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2657 return false;
2658 }
2659 }
2660 return true;
2661}
2662
2663/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2664/// constant +0.0.
2665static inline bool isZeroNode(SDOperand Elt) {
2666 return ((isa<ConstantSDNode>(Elt) &&
2667 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2668 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002669 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002670}
2671
2672/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2673/// to an zero vector.
2674static bool isZeroShuffle(SDNode *N) {
2675 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2676 return false;
2677
2678 SDOperand V1 = N->getOperand(0);
2679 SDOperand V2 = N->getOperand(1);
2680 SDOperand Mask = N->getOperand(2);
2681 unsigned NumElems = Mask.getNumOperands();
2682 for (unsigned i = 0; i != NumElems; ++i) {
2683 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002684 if (Arg.getOpcode() == ISD::UNDEF)
2685 continue;
2686
2687 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2688 if (Idx < NumElems) {
2689 unsigned Opc = V1.Val->getOpcode();
2690 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2691 continue;
2692 if (Opc != ISD::BUILD_VECTOR ||
2693 !isZeroNode(V1.Val->getOperand(Idx)))
2694 return false;
2695 } else if (Idx >= NumElems) {
2696 unsigned Opc = V2.Val->getOpcode();
2697 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2698 continue;
2699 if (Opc != ISD::BUILD_VECTOR ||
2700 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2701 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002702 }
2703 }
2704 return true;
2705}
2706
2707/// getZeroVector - Returns a vector of specified type with all zero elements.
2708///
2709static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2710 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002711
2712 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2713 // type. This ensures they get CSE'd.
2714 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2715 SDOperand Vec;
2716 if (MVT::getSizeInBits(VT) == 64) // MMX
2717 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2718 else // SSE
2719 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2720 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002721}
2722
Chris Lattnere6aa3862007-11-25 00:24:49 +00002723/// getOnesVector - Returns a vector of specified type with all bits set.
2724///
2725static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2726 assert(MVT::isVector(VT) && "Expected a vector type");
2727
2728 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2729 // type. This ensures they get CSE'd.
2730 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2731 SDOperand Vec;
2732 if (MVT::getSizeInBits(VT) == 64) // MMX
2733 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2734 else // SSE
2735 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2736 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2737}
2738
2739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002740/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2741/// that point to V2 points to its first element.
2742static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2743 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2744
2745 bool Changed = false;
2746 SmallVector<SDOperand, 8> MaskVec;
2747 unsigned NumElems = Mask.getNumOperands();
2748 for (unsigned i = 0; i != NumElems; ++i) {
2749 SDOperand Arg = Mask.getOperand(i);
2750 if (Arg.getOpcode() != ISD::UNDEF) {
2751 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2752 if (Val > NumElems) {
2753 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2754 Changed = true;
2755 }
2756 }
2757 MaskVec.push_back(Arg);
2758 }
2759
2760 if (Changed)
2761 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2762 &MaskVec[0], MaskVec.size());
2763 return Mask;
2764}
2765
2766/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2767/// operation of specified width.
2768static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2769 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2770 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2771
2772 SmallVector<SDOperand, 8> MaskVec;
2773 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2774 for (unsigned i = 1; i != NumElems; ++i)
2775 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2776 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2777}
2778
2779/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2780/// of specified width.
2781static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2782 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2783 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2784 SmallVector<SDOperand, 8> MaskVec;
2785 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2786 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2787 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2788 }
2789 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2790}
2791
2792/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2793/// of specified width.
2794static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2795 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2796 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2797 unsigned Half = NumElems/2;
2798 SmallVector<SDOperand, 8> MaskVec;
2799 for (unsigned i = 0; i != Half; ++i) {
2800 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2801 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2802 }
2803 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2804}
2805
2806/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2807///
2808static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2809 SDOperand V1 = Op.getOperand(0);
2810 SDOperand Mask = Op.getOperand(2);
2811 MVT::ValueType VT = Op.getValueType();
2812 unsigned NumElems = Mask.getNumOperands();
2813 Mask = getUnpacklMask(NumElems, DAG);
2814 while (NumElems != 4) {
2815 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2816 NumElems >>= 1;
2817 }
2818 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2819
Chris Lattnere6aa3862007-11-25 00:24:49 +00002820 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2822 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2823 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2824}
2825
2826/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002827/// vector of zero or undef vector. This produces a shuffle where the low
2828/// element of V2 is swizzled into the zero/undef vector, landing at element
2829/// 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 +00002830static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2831 unsigned NumElems, unsigned Idx,
2832 bool isZero, SelectionDAG &DAG) {
2833 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2834 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2835 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002836 SmallVector<SDOperand, 16> MaskVec;
2837 for (unsigned i = 0; i != NumElems; ++i)
2838 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2839 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2840 else
2841 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002842 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2843 &MaskVec[0], MaskVec.size());
2844 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2845}
2846
2847/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2848///
2849static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2850 unsigned NumNonZero, unsigned NumZero,
2851 SelectionDAG &DAG, TargetLowering &TLI) {
2852 if (NumNonZero > 8)
2853 return SDOperand();
2854
2855 SDOperand V(0, 0);
2856 bool First = true;
2857 for (unsigned i = 0; i < 16; ++i) {
2858 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2859 if (ThisIsNonZero && First) {
2860 if (NumZero)
2861 V = getZeroVector(MVT::v8i16, DAG);
2862 else
2863 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2864 First = false;
2865 }
2866
2867 if ((i & 1) != 0) {
2868 SDOperand ThisElt(0, 0), LastElt(0, 0);
2869 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2870 if (LastIsNonZero) {
2871 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2872 }
2873 if (ThisIsNonZero) {
2874 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2875 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2876 ThisElt, DAG.getConstant(8, MVT::i8));
2877 if (LastIsNonZero)
2878 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2879 } else
2880 ThisElt = LastElt;
2881
2882 if (ThisElt.Val)
2883 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002884 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 }
2886 }
2887
2888 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2889}
2890
2891/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2892///
2893static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2894 unsigned NumNonZero, unsigned NumZero,
2895 SelectionDAG &DAG, TargetLowering &TLI) {
2896 if (NumNonZero > 4)
2897 return SDOperand();
2898
2899 SDOperand V(0, 0);
2900 bool First = true;
2901 for (unsigned i = 0; i < 8; ++i) {
2902 bool isNonZero = (NonZeros & (1 << i)) != 0;
2903 if (isNonZero) {
2904 if (First) {
2905 if (NumZero)
2906 V = getZeroVector(MVT::v8i16, DAG);
2907 else
2908 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2909 First = false;
2910 }
2911 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002912 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 }
2914 }
2915
2916 return V;
2917}
2918
2919SDOperand
2920X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002921 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2922 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2923 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2924 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2925 // eliminated on x86-32 hosts.
2926 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2927 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928
Chris Lattnere6aa3862007-11-25 00:24:49 +00002929 if (ISD::isBuildVectorAllOnes(Op.Val))
2930 return getOnesVector(Op.getValueType(), DAG);
2931 return getZeroVector(Op.getValueType(), DAG);
2932 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002933
2934 MVT::ValueType VT = Op.getValueType();
2935 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2936 unsigned EVTBits = MVT::getSizeInBits(EVT);
2937
2938 unsigned NumElems = Op.getNumOperands();
2939 unsigned NumZero = 0;
2940 unsigned NumNonZero = 0;
2941 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00002942 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00002943 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002944 for (unsigned i = 0; i < NumElems; ++i) {
2945 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00002946 if (Elt.getOpcode() == ISD::UNDEF)
2947 continue;
2948 Values.insert(Elt);
2949 if (Elt.getOpcode() != ISD::Constant &&
2950 Elt.getOpcode() != ISD::ConstantFP)
2951 HasNonImms = true;
2952 if (isZeroNode(Elt))
2953 NumZero++;
2954 else {
2955 NonZeros |= (1 << i);
2956 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002957 }
2958 }
2959
2960 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002961 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2962 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 }
2964
2965 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2966 if (Values.size() == 1)
2967 return SDOperand();
2968
2969 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00002970 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 unsigned Idx = CountTrailingZeros_32(NonZeros);
2972 SDOperand Item = Op.getOperand(Idx);
2973 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2974 if (Idx == 0)
2975 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2976 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2977 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00002978 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
2979 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980
2981 if (EVTBits == 32) {
2982 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2983 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2984 DAG);
2985 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2986 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2987 SmallVector<SDOperand, 8> MaskVec;
2988 for (unsigned i = 0; i < NumElems; i++)
2989 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2990 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2991 &MaskVec[0], MaskVec.size());
2992 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2993 DAG.getNode(ISD::UNDEF, VT), Mask);
2994 }
2995 }
2996
Dan Gohman21463242007-07-24 22:55:08 +00002997 // A vector full of immediates; various special cases are already
2998 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00002999 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00003000 return SDOperand();
3001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 // Let legalizer expand 2-wide build_vectors.
3003 if (EVTBits == 64)
3004 return SDOperand();
3005
3006 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3007 if (EVTBits == 8 && NumElems == 16) {
3008 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3009 *this);
3010 if (V.Val) return V;
3011 }
3012
3013 if (EVTBits == 16 && NumElems == 8) {
3014 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3015 *this);
3016 if (V.Val) return V;
3017 }
3018
3019 // If element VT is == 32 bits, turn it into a number of shuffles.
3020 SmallVector<SDOperand, 8> V;
3021 V.resize(NumElems);
3022 if (NumElems == 4 && NumZero > 0) {
3023 for (unsigned i = 0; i < 4; ++i) {
3024 bool isZero = !(NonZeros & (1 << i));
3025 if (isZero)
3026 V[i] = getZeroVector(VT, DAG);
3027 else
3028 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3029 }
3030
3031 for (unsigned i = 0; i < 2; ++i) {
3032 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3033 default: break;
3034 case 0:
3035 V[i] = V[i*2]; // Must be a zero vector.
3036 break;
3037 case 1:
3038 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3039 getMOVLMask(NumElems, DAG));
3040 break;
3041 case 2:
3042 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3043 getMOVLMask(NumElems, DAG));
3044 break;
3045 case 3:
3046 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3047 getUnpacklMask(NumElems, DAG));
3048 break;
3049 }
3050 }
3051
3052 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3053 // clears the upper bits.
3054 // FIXME: we can do the same for v4f32 case when we know both parts of
3055 // the lower half come from scalar_to_vector (loadf32). We should do
3056 // that in post legalizer dag combiner with target specific hooks.
3057 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3058 return V[0];
3059 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3060 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3061 SmallVector<SDOperand, 8> MaskVec;
3062 bool Reverse = (NonZeros & 0x3) == 2;
3063 for (unsigned i = 0; i < 2; ++i)
3064 if (Reverse)
3065 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3066 else
3067 MaskVec.push_back(DAG.getConstant(i, EVT));
3068 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3069 for (unsigned i = 0; i < 2; ++i)
3070 if (Reverse)
3071 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3072 else
3073 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3074 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3075 &MaskVec[0], MaskVec.size());
3076 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3077 }
3078
3079 if (Values.size() > 2) {
3080 // Expand into a number of unpckl*.
3081 // e.g. for v4f32
3082 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3083 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3084 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3085 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3086 for (unsigned i = 0; i < NumElems; ++i)
3087 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3088 NumElems >>= 1;
3089 while (NumElems != 0) {
3090 for (unsigned i = 0; i < NumElems; ++i)
3091 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3092 UnpckMask);
3093 NumElems >>= 1;
3094 }
3095 return V[0];
3096 }
3097
3098 return SDOperand();
3099}
3100
Evan Chengfca29242007-12-07 08:07:39 +00003101static
3102SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3103 SDOperand PermMask, SelectionDAG &DAG,
3104 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003105 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003106 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3107 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003108 MVT::ValueType PtrVT = TLI.getPointerTy();
3109 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3110 PermMask.Val->op_end());
3111
3112 // First record which half of which vector the low elements come from.
3113 SmallVector<unsigned, 4> LowQuad(4);
3114 for (unsigned i = 0; i < 4; ++i) {
3115 SDOperand Elt = MaskElts[i];
3116 if (Elt.getOpcode() == ISD::UNDEF)
3117 continue;
3118 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3119 int QuadIdx = EltIdx / 4;
3120 ++LowQuad[QuadIdx];
3121 }
3122 int BestLowQuad = -1;
3123 unsigned MaxQuad = 1;
3124 for (unsigned i = 0; i < 4; ++i) {
3125 if (LowQuad[i] > MaxQuad) {
3126 BestLowQuad = i;
3127 MaxQuad = LowQuad[i];
3128 }
Evan Chengfca29242007-12-07 08:07:39 +00003129 }
3130
Evan Cheng75184a92007-12-11 01:46:18 +00003131 // Record which half of which vector the high elements come from.
3132 SmallVector<unsigned, 4> HighQuad(4);
3133 for (unsigned i = 4; i < 8; ++i) {
3134 SDOperand Elt = MaskElts[i];
3135 if (Elt.getOpcode() == ISD::UNDEF)
3136 continue;
3137 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3138 int QuadIdx = EltIdx / 4;
3139 ++HighQuad[QuadIdx];
3140 }
3141 int BestHighQuad = -1;
3142 MaxQuad = 1;
3143 for (unsigned i = 0; i < 4; ++i) {
3144 if (HighQuad[i] > MaxQuad) {
3145 BestHighQuad = i;
3146 MaxQuad = HighQuad[i];
3147 }
3148 }
3149
3150 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3151 if (BestLowQuad != -1 || BestHighQuad != -1) {
3152 // First sort the 4 chunks in order using shufpd.
3153 SmallVector<SDOperand, 8> MaskVec;
3154 if (BestLowQuad != -1)
3155 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3156 else
3157 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3158 if (BestHighQuad != -1)
3159 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3160 else
3161 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3162 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3163 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3164 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3165 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3166 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3167
3168 // Now sort high and low parts separately.
3169 BitVector InOrder(8);
3170 if (BestLowQuad != -1) {
3171 // Sort lower half in order using PSHUFLW.
3172 MaskVec.clear();
3173 bool AnyOutOrder = false;
3174 for (unsigned i = 0; i != 4; ++i) {
3175 SDOperand Elt = MaskElts[i];
3176 if (Elt.getOpcode() == ISD::UNDEF) {
3177 MaskVec.push_back(Elt);
3178 InOrder.set(i);
3179 } else {
3180 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3181 if (EltIdx != i)
3182 AnyOutOrder = true;
3183 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3184 // If this element is in the right place after this shuffle, then
3185 // remember it.
3186 if ((int)(EltIdx / 4) == BestLowQuad)
3187 InOrder.set(i);
3188 }
3189 }
3190 if (AnyOutOrder) {
3191 for (unsigned i = 4; i != 8; ++i)
3192 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3193 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3194 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3195 }
3196 }
3197
3198 if (BestHighQuad != -1) {
3199 // Sort high half in order using PSHUFHW if possible.
3200 MaskVec.clear();
3201 for (unsigned i = 0; i != 4; ++i)
3202 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3203 bool AnyOutOrder = false;
3204 for (unsigned i = 4; i != 8; ++i) {
3205 SDOperand Elt = MaskElts[i];
3206 if (Elt.getOpcode() == ISD::UNDEF) {
3207 MaskVec.push_back(Elt);
3208 InOrder.set(i);
3209 } else {
3210 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3211 if (EltIdx != i)
3212 AnyOutOrder = true;
3213 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3214 // If this element is in the right place after this shuffle, then
3215 // remember it.
3216 if ((int)(EltIdx / 4) == BestHighQuad)
3217 InOrder.set(i);
3218 }
3219 }
3220 if (AnyOutOrder) {
3221 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3222 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3223 }
3224 }
3225
3226 // The other elements are put in the right place using pextrw and pinsrw.
3227 for (unsigned i = 0; i != 8; ++i) {
3228 if (InOrder[i])
3229 continue;
3230 SDOperand Elt = MaskElts[i];
3231 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3232 if (EltIdx == i)
3233 continue;
3234 SDOperand ExtOp = (EltIdx < 8)
3235 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3236 DAG.getConstant(EltIdx, PtrVT))
3237 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3238 DAG.getConstant(EltIdx - 8, PtrVT));
3239 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3240 DAG.getConstant(i, PtrVT));
3241 }
3242 return NewV;
3243 }
3244
3245 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3246 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003247 // First, let's find out how many elements are already in the right order.
3248 unsigned V1InOrder = 0;
3249 unsigned V1FromV1 = 0;
3250 unsigned V2InOrder = 0;
3251 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003252 SmallVector<SDOperand, 8> V1Elts;
3253 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003254 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003255 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003256 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003257 V1Elts.push_back(Elt);
3258 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003259 ++V1InOrder;
3260 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003261 continue;
3262 }
3263 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3264 if (EltIdx == i) {
3265 V1Elts.push_back(Elt);
3266 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3267 ++V1InOrder;
3268 } else if (EltIdx == i+8) {
3269 V1Elts.push_back(Elt);
3270 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3271 ++V2InOrder;
3272 } else if (EltIdx < 8) {
3273 V1Elts.push_back(Elt);
3274 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003275 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003276 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3277 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003278 }
3279 }
3280
3281 if (V2InOrder > V1InOrder) {
3282 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3283 std::swap(V1, V2);
3284 std::swap(V1Elts, V2Elts);
3285 std::swap(V1FromV1, V2FromV2);
3286 }
3287
Evan Cheng75184a92007-12-11 01:46:18 +00003288 if ((V1FromV1 + V1InOrder) != 8) {
3289 // Some elements are from V2.
3290 if (V1FromV1) {
3291 // If there are elements that are from V1 but out of place,
3292 // then first sort them in place
3293 SmallVector<SDOperand, 8> MaskVec;
3294 for (unsigned i = 0; i < 8; ++i) {
3295 SDOperand Elt = V1Elts[i];
3296 if (Elt.getOpcode() == ISD::UNDEF) {
3297 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3298 continue;
3299 }
3300 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3301 if (EltIdx >= 8)
3302 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3303 else
3304 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3305 }
3306 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3307 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003308 }
Evan Cheng75184a92007-12-11 01:46:18 +00003309
3310 NewV = V1;
3311 for (unsigned i = 0; i < 8; ++i) {
3312 SDOperand Elt = V1Elts[i];
3313 if (Elt.getOpcode() == ISD::UNDEF)
3314 continue;
3315 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3316 if (EltIdx < 8)
3317 continue;
3318 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3319 DAG.getConstant(EltIdx - 8, PtrVT));
3320 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3321 DAG.getConstant(i, PtrVT));
3322 }
3323 return NewV;
3324 } else {
3325 // All elements are from V1.
3326 NewV = V1;
3327 for (unsigned i = 0; i < 8; ++i) {
3328 SDOperand Elt = V1Elts[i];
3329 if (Elt.getOpcode() == ISD::UNDEF)
3330 continue;
3331 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3332 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3333 DAG.getConstant(EltIdx, PtrVT));
3334 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3335 DAG.getConstant(i, PtrVT));
3336 }
3337 return NewV;
3338 }
3339}
3340
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003341/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3342/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3343/// done when every pair / quad of shuffle mask elements point to elements in
3344/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003345/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3346static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003347SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3348 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003349 SDOperand PermMask, SelectionDAG &DAG,
3350 TargetLowering &TLI) {
3351 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003352 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3353 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3354 MVT::ValueType NewVT = MaskVT;
3355 switch (VT) {
3356 case MVT::v4f32: NewVT = MVT::v2f64; break;
3357 case MVT::v4i32: NewVT = MVT::v2i64; break;
3358 case MVT::v8i16: NewVT = MVT::v4i32; break;
3359 case MVT::v16i8: NewVT = MVT::v4i32; break;
3360 default: assert(false && "Unexpected!");
3361 }
3362
3363 if (NewWidth == 2)
3364 if (MVT::isInteger(VT))
3365 NewVT = MVT::v2i64;
3366 else
3367 NewVT = MVT::v2f64;
3368 unsigned Scale = NumElems / NewWidth;
3369 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003370 for (unsigned i = 0; i < NumElems; i += Scale) {
3371 unsigned StartIdx = ~0U;
3372 for (unsigned j = 0; j < Scale; ++j) {
3373 SDOperand Elt = PermMask.getOperand(i+j);
3374 if (Elt.getOpcode() == ISD::UNDEF)
3375 continue;
3376 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3377 if (StartIdx == ~0U)
3378 StartIdx = EltIdx - (EltIdx % Scale);
3379 if (EltIdx != StartIdx + j)
3380 return SDOperand();
3381 }
3382 if (StartIdx == ~0U)
3383 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3384 else
3385 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003386 }
3387
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003388 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3389 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3390 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3391 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3392 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003393}
3394
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003395SDOperand
3396X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3397 SDOperand V1 = Op.getOperand(0);
3398 SDOperand V2 = Op.getOperand(1);
3399 SDOperand PermMask = Op.getOperand(2);
3400 MVT::ValueType VT = Op.getValueType();
3401 unsigned NumElems = PermMask.getNumOperands();
3402 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3403 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3404 bool V1IsSplat = false;
3405 bool V2IsSplat = false;
3406
3407 if (isUndefShuffle(Op.Val))
3408 return DAG.getNode(ISD::UNDEF, VT);
3409
3410 if (isZeroShuffle(Op.Val))
3411 return getZeroVector(VT, DAG);
3412
3413 if (isIdentityMask(PermMask.Val))
3414 return V1;
3415 else if (isIdentityMask(PermMask.Val, true))
3416 return V2;
3417
3418 if (isSplatMask(PermMask.Val)) {
3419 if (NumElems <= 4) return Op;
3420 // Promote it to a v4i32 splat.
3421 return PromoteSplat(Op, DAG);
3422 }
3423
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003424 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3425 // do it!
3426 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3427 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3428 if (NewOp.Val)
3429 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3430 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3431 // FIXME: Figure out a cleaner way to do this.
3432 // Try to make use of movq to zero out the top part.
3433 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3434 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3435 if (NewOp.Val) {
3436 SDOperand NewV1 = NewOp.getOperand(0);
3437 SDOperand NewV2 = NewOp.getOperand(1);
3438 SDOperand NewMask = NewOp.getOperand(2);
3439 if (isCommutedMOVL(NewMask.Val, true, false)) {
3440 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3441 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3442 NewV1, NewV2, getMOVLMask(2, DAG));
3443 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3444 }
3445 }
3446 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3447 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3448 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3449 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3450 }
3451 }
3452
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003453 if (X86::isMOVLMask(PermMask.Val))
3454 return (V1IsUndef) ? V2 : Op;
3455
3456 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3457 X86::isMOVSLDUPMask(PermMask.Val) ||
3458 X86::isMOVHLPSMask(PermMask.Val) ||
3459 X86::isMOVHPMask(PermMask.Val) ||
3460 X86::isMOVLPMask(PermMask.Val))
3461 return Op;
3462
3463 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3464 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3465 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3466
3467 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003468 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3469 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003470 V1IsSplat = isSplatVector(V1.Val);
3471 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003472
3473 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003474 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3475 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3476 std::swap(V1IsSplat, V2IsSplat);
3477 std::swap(V1IsUndef, V2IsUndef);
3478 Commuted = true;
3479 }
3480
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003481 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003482 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3483 if (V2IsUndef) return V1;
3484 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3485 if (V2IsSplat) {
3486 // V2 is a splat, so the mask may be malformed. That is, it may point
3487 // to any V2 element. The instruction selectior won't like this. Get
3488 // a corrected mask and commute to form a proper MOVS{S|D}.
3489 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3490 if (NewMask.Val != PermMask.Val)
3491 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3492 }
3493 return Op;
3494 }
3495
3496 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3497 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3498 X86::isUNPCKLMask(PermMask.Val) ||
3499 X86::isUNPCKHMask(PermMask.Val))
3500 return Op;
3501
3502 if (V2IsSplat) {
3503 // Normalize mask so all entries that point to V2 points to its first
3504 // element then try to match unpck{h|l} again. If match, return a
3505 // new vector_shuffle with the corrected mask.
3506 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3507 if (NewMask.Val != PermMask.Val) {
3508 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3509 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3510 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3511 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3512 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3513 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3514 }
3515 }
3516 }
3517
3518 // Normalize the node to match x86 shuffle ops if needed
3519 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3520 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3521
3522 if (Commuted) {
3523 // Commute is back and try unpck* again.
3524 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3525 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3526 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3527 X86::isUNPCKLMask(PermMask.Val) ||
3528 X86::isUNPCKHMask(PermMask.Val))
3529 return Op;
3530 }
3531
3532 // If VT is integer, try PSHUF* first, then SHUFP*.
3533 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003534 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3535 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3536 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3537 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003538 X86::isPSHUFHWMask(PermMask.Val) ||
3539 X86::isPSHUFLWMask(PermMask.Val)) {
3540 if (V2.getOpcode() != ISD::UNDEF)
3541 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3542 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3543 return Op;
3544 }
3545
3546 if (X86::isSHUFPMask(PermMask.Val) &&
3547 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3548 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003549 } else {
3550 // Floating point cases in the other order.
3551 if (X86::isSHUFPMask(PermMask.Val))
3552 return Op;
3553 if (X86::isPSHUFDMask(PermMask.Val) ||
3554 X86::isPSHUFHWMask(PermMask.Val) ||
3555 X86::isPSHUFLWMask(PermMask.Val)) {
3556 if (V2.getOpcode() != ISD::UNDEF)
3557 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3558 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3559 return Op;
3560 }
3561 }
3562
Evan Cheng75184a92007-12-11 01:46:18 +00003563 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3564 if (VT == MVT::v8i16) {
3565 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3566 if (NewOp.Val)
3567 return NewOp;
3568 }
3569
3570 // Handle all 4 wide cases with a number of shuffles.
3571 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003572 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003573 MVT::ValueType MaskVT = PermMask.getValueType();
3574 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3575 SmallVector<std::pair<int, int>, 8> Locs;
3576 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003577 SmallVector<SDOperand, 8> Mask1(NumElems,
3578 DAG.getNode(ISD::UNDEF, MaskEVT));
3579 SmallVector<SDOperand, 8> Mask2(NumElems,
3580 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 unsigned NumHi = 0;
3582 unsigned NumLo = 0;
3583 // If no more than two elements come from either vector. This can be
3584 // implemented with two shuffles. First shuffle gather the elements.
3585 // The second shuffle, which takes the first shuffle as both of its
3586 // vector operands, put the elements into the right order.
3587 for (unsigned i = 0; i != NumElems; ++i) {
3588 SDOperand Elt = PermMask.getOperand(i);
3589 if (Elt.getOpcode() == ISD::UNDEF) {
3590 Locs[i] = std::make_pair(-1, -1);
3591 } else {
3592 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3593 if (Val < NumElems) {
3594 Locs[i] = std::make_pair(0, NumLo);
3595 Mask1[NumLo] = Elt;
3596 NumLo++;
3597 } else {
3598 Locs[i] = std::make_pair(1, NumHi);
3599 if (2+NumHi < NumElems)
3600 Mask1[2+NumHi] = Elt;
3601 NumHi++;
3602 }
3603 }
3604 }
3605 if (NumLo <= 2 && NumHi <= 2) {
3606 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3607 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3608 &Mask1[0], Mask1.size()));
3609 for (unsigned i = 0; i != NumElems; ++i) {
3610 if (Locs[i].first == -1)
3611 continue;
3612 else {
3613 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3614 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3615 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3616 }
3617 }
3618
3619 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3620 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3621 &Mask2[0], Mask2.size()));
3622 }
3623
3624 // Break it into (shuffle shuffle_hi, shuffle_lo).
3625 Locs.clear();
3626 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3627 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3628 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3629 unsigned MaskIdx = 0;
3630 unsigned LoIdx = 0;
3631 unsigned HiIdx = NumElems/2;
3632 for (unsigned i = 0; i != NumElems; ++i) {
3633 if (i == NumElems/2) {
3634 MaskPtr = &HiMask;
3635 MaskIdx = 1;
3636 LoIdx = 0;
3637 HiIdx = NumElems/2;
3638 }
3639 SDOperand Elt = PermMask.getOperand(i);
3640 if (Elt.getOpcode() == ISD::UNDEF) {
3641 Locs[i] = std::make_pair(-1, -1);
3642 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3643 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3644 (*MaskPtr)[LoIdx] = Elt;
3645 LoIdx++;
3646 } else {
3647 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3648 (*MaskPtr)[HiIdx] = Elt;
3649 HiIdx++;
3650 }
3651 }
3652
3653 SDOperand LoShuffle =
3654 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3655 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3656 &LoMask[0], LoMask.size()));
3657 SDOperand HiShuffle =
3658 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3659 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3660 &HiMask[0], HiMask.size()));
3661 SmallVector<SDOperand, 8> MaskOps;
3662 for (unsigned i = 0; i != NumElems; ++i) {
3663 if (Locs[i].first == -1) {
3664 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3665 } else {
3666 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3667 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3668 }
3669 }
3670 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3671 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3672 &MaskOps[0], MaskOps.size()));
3673 }
3674
3675 return SDOperand();
3676}
3677
3678SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003679X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3680 SelectionDAG &DAG) {
3681 MVT::ValueType VT = Op.getValueType();
3682 if (MVT::getSizeInBits(VT) == 8) {
3683 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3684 Op.getOperand(0), Op.getOperand(1));
3685 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3686 DAG.getValueType(VT));
3687 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3688 } else if (MVT::getSizeInBits(VT) == 16) {
3689 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3690 Op.getOperand(0), Op.getOperand(1));
3691 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3692 DAG.getValueType(VT));
3693 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3694 }
3695 return SDOperand();
3696}
3697
3698
3699SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003700X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3701 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3702 return SDOperand();
3703
Nate Begemand77e59e2008-02-11 04:19:36 +00003704 if (Subtarget->hasSSE41())
3705 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3706
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003707 MVT::ValueType VT = Op.getValueType();
3708 // TODO: handle v16i8.
3709 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003710 SDOperand Vec = Op.getOperand(0);
3711 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3712 if (Idx == 0)
3713 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3714 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3715 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3716 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003717 // Transform it so it match pextrw which produces a 32-bit result.
3718 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3719 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3720 Op.getOperand(0), Op.getOperand(1));
3721 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3722 DAG.getValueType(VT));
3723 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3724 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003725 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3726 if (Idx == 0)
3727 return Op;
3728 // SHUFPS the element to the lowest double word, then movss.
3729 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3730 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003731 IdxVec.
3732 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3733 IdxVec.
3734 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3735 IdxVec.
3736 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3737 IdxVec.
3738 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003739 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3740 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003741 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003742 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3743 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3744 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003745 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003747 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3748 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3749 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3751 if (Idx == 0)
3752 return Op;
3753
3754 // UNPCKHPD the element to the lowest double word, then movsd.
3755 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3756 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3757 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3758 SmallVector<SDOperand, 8> IdxVec;
3759 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003760 IdxVec.
3761 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003762 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3763 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003764 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003765 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3766 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3767 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003768 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003769 }
3770
3771 return SDOperand();
3772}
3773
3774SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003775X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3776 MVT::ValueType VT = Op.getValueType();
3777 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3778
3779 SDOperand N0 = Op.getOperand(0);
3780 SDOperand N1 = Op.getOperand(1);
3781 SDOperand N2 = Op.getOperand(2);
3782
3783 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3784 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3785 : X86ISD::PINSRW;
3786 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3787 // argument.
3788 if (N1.getValueType() != MVT::i32)
3789 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3790 if (N2.getValueType() != MVT::i32)
3791 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3792 return DAG.getNode(Opc, VT, N0, N1, N2);
3793 } else if (EVT == MVT::f32) {
3794 // Bits [7:6] of the constant are the source select. This will always be
3795 // zero here. The DAG Combiner may combine an extract_elt index into these
3796 // bits. For example (insert (extract, 3), 2) could be matched by putting
3797 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3798 // Bits [5:4] of the constant are the destination select. This is the
3799 // value of the incoming immediate.
3800 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3801 // combine either bitwise AND or insert of float 0.0 to set these bits.
3802 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3803 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3804 }
3805 return SDOperand();
3806}
3807
3808SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003809X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003811 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003812
3813 if (Subtarget->hasSSE41())
3814 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3815
Evan Chenge12a7eb2007-12-12 07:55:34 +00003816 if (EVT == MVT::i8)
3817 return SDOperand();
3818
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003819 SDOperand N0 = Op.getOperand(0);
3820 SDOperand N1 = Op.getOperand(1);
3821 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003822
3823 if (MVT::getSizeInBits(EVT) == 16) {
3824 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3825 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003826 if (N1.getValueType() != MVT::i32)
3827 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3828 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003829 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003830 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003831 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003832 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003833}
3834
3835SDOperand
3836X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3837 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3838 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3839}
3840
3841// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3842// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3843// one of the above mentioned nodes. It has to be wrapped because otherwise
3844// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3845// be used to form addressing mode. These wrapped nodes will be selected
3846// into MOV32ri.
3847SDOperand
3848X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3849 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3850 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3851 getPointerTy(),
3852 CP->getAlignment());
3853 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3854 // With PIC, the address is actually $g + Offset.
3855 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3856 !Subtarget->isPICStyleRIPRel()) {
3857 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3858 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3859 Result);
3860 }
3861
3862 return Result;
3863}
3864
3865SDOperand
3866X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3867 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3868 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003869 // If it's a debug information descriptor, don't mess with it.
3870 if (DAG.isVerifiedDebugInfoDesc(Op))
3871 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003872 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3873 // With PIC, the address is actually $g + Offset.
3874 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3875 !Subtarget->isPICStyleRIPRel()) {
3876 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3877 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3878 Result);
3879 }
3880
3881 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3882 // load the value at address GV, not the value of GV itself. This means that
3883 // the GlobalAddress must be in the base or index register of the address, not
3884 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3885 // The same applies for external symbols during PIC codegen
3886 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003887 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003888 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889
3890 return Result;
3891}
3892
3893// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3894static SDOperand
3895LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3896 const MVT::ValueType PtrVT) {
3897 SDOperand InFlag;
3898 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3899 DAG.getNode(X86ISD::GlobalBaseReg,
3900 PtrVT), InFlag);
3901 InFlag = Chain.getValue(1);
3902
3903 // emit leal symbol@TLSGD(,%ebx,1), %eax
3904 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3905 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3906 GA->getValueType(0),
3907 GA->getOffset());
3908 SDOperand Ops[] = { Chain, TGA, InFlag };
3909 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3910 InFlag = Result.getValue(2);
3911 Chain = Result.getValue(1);
3912
3913 // call ___tls_get_addr. This function receives its argument in
3914 // the register EAX.
3915 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3916 InFlag = Chain.getValue(1);
3917
3918 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3919 SDOperand Ops1[] = { Chain,
3920 DAG.getTargetExternalSymbol("___tls_get_addr",
3921 PtrVT),
3922 DAG.getRegister(X86::EAX, PtrVT),
3923 DAG.getRegister(X86::EBX, PtrVT),
3924 InFlag };
3925 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3926 InFlag = Chain.getValue(1);
3927
3928 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3929}
3930
3931// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3932// "local exec" model.
3933static SDOperand
3934LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3935 const MVT::ValueType PtrVT) {
3936 // Get the Thread Pointer
3937 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3938 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3939 // exec)
3940 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3941 GA->getValueType(0),
3942 GA->getOffset());
3943 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3944
3945 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00003946 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003947 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948
3949 // The address of the thread local variable is the add of the thread
3950 // pointer with the offset of the variable.
3951 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3952}
3953
3954SDOperand
3955X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3956 // TODO: implement the "local dynamic" model
3957 // TODO: implement the "initial exec"model for pic executables
3958 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3959 "TLS not implemented for non-ELF and 64-bit targets");
3960 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3961 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3962 // otherwise use the "Local Exec"TLS Model
3963 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3964 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3965 else
3966 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3967}
3968
3969SDOperand
3970X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3971 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3972 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3973 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3974 // With PIC, the address is actually $g + Offset.
3975 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3976 !Subtarget->isPICStyleRIPRel()) {
3977 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3978 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3979 Result);
3980 }
3981
3982 return Result;
3983}
3984
3985SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3986 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3987 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3988 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3989 // With PIC, the address is actually $g + Offset.
3990 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3991 !Subtarget->isPICStyleRIPRel()) {
3992 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3993 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3994 Result);
3995 }
3996
3997 return Result;
3998}
3999
Chris Lattner62814a32007-10-17 06:02:13 +00004000/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4001/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00004003 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4004 "Not an i64 shift!");
4005 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4006 SDOperand ShOpLo = Op.getOperand(0);
4007 SDOperand ShOpHi = Op.getOperand(1);
4008 SDOperand ShAmt = Op.getOperand(2);
4009 SDOperand Tmp1 = isSRA ?
4010 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4011 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012
Chris Lattner62814a32007-10-17 06:02:13 +00004013 SDOperand Tmp2, Tmp3;
4014 if (Op.getOpcode() == ISD::SHL_PARTS) {
4015 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4016 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4017 } else {
4018 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4019 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4020 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004021
Chris Lattner62814a32007-10-17 06:02:13 +00004022 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4023 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4024 DAG.getConstant(32, MVT::i8));
4025 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4026 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027
Chris Lattner62814a32007-10-17 06:02:13 +00004028 SDOperand Hi, Lo;
4029 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4030 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4031 SmallVector<SDOperand, 4> Ops;
4032 if (Op.getOpcode() == ISD::SHL_PARTS) {
4033 Ops.push_back(Tmp2);
4034 Ops.push_back(Tmp3);
4035 Ops.push_back(CC);
4036 Ops.push_back(Cond);
4037 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004040 Ops.push_back(Tmp3);
4041 Ops.push_back(Tmp1);
4042 Ops.push_back(CC);
4043 Ops.push_back(Cond);
4044 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4045 } else {
4046 Ops.push_back(Tmp2);
4047 Ops.push_back(Tmp3);
4048 Ops.push_back(CC);
4049 Ops.push_back(Cond);
4050 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4051
4052 Ops.clear();
4053 Ops.push_back(Tmp3);
4054 Ops.push_back(Tmp1);
4055 Ops.push_back(CC);
4056 Ops.push_back(Cond);
4057 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4058 }
4059
4060 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4061 Ops.clear();
4062 Ops.push_back(Lo);
4063 Ops.push_back(Hi);
4064 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065}
4066
4067SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4068 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4069 Op.getOperand(0).getValueType() >= MVT::i16 &&
4070 "Unknown SINT_TO_FP to lower!");
4071
4072 SDOperand Result;
4073 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4074 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4075 MachineFunction &MF = DAG.getMachineFunction();
4076 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4077 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4078 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004079 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004080 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004081 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004082
Dale Johannesen2fc20782007-09-14 22:26:36 +00004083 // These are really Legal; caller falls through into that case.
Chris Lattnercf515b52008-01-16 06:24:21 +00004084 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004085 return Result;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004086 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen958b08b2007-09-19 23:55:34 +00004087 Subtarget->is64Bit())
4088 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00004089
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090 // Build the FILD
4091 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004092 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004093 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4095 else
4096 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4097 SmallVector<SDOperand, 8> Ops;
4098 Ops.push_back(Chain);
4099 Ops.push_back(StackSlot);
4100 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00004101 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004102 Tys, &Ops[0], Ops.size());
4103
Dale Johannesen2fc20782007-09-14 22:26:36 +00004104 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004105 Chain = Result.getValue(1);
4106 SDOperand InFlag = Result.getValue(2);
4107
4108 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4109 // shouldn't be necessary except that RFP cannot be live across
4110 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4111 MachineFunction &MF = DAG.getMachineFunction();
4112 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4113 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4114 Tys = DAG.getVTList(MVT::Other);
4115 SmallVector<SDOperand, 8> Ops;
4116 Ops.push_back(Chain);
4117 Ops.push_back(Result);
4118 Ops.push_back(StackSlot);
4119 Ops.push_back(DAG.getValueType(Op.getValueType()));
4120 Ops.push_back(InFlag);
4121 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004122 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004123 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124 }
4125
4126 return Result;
4127}
4128
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004129std::pair<SDOperand,SDOperand> X86TargetLowering::
4130FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4132 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133
Dale Johannesen2fc20782007-09-14 22:26:36 +00004134 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004135 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004136 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004137 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004138 if (Subtarget->is64Bit() &&
4139 Op.getValueType() == MVT::i64 &&
4140 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004141 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004142
Evan Cheng05441e62007-10-15 20:11:21 +00004143 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4144 // stack slot.
4145 MachineFunction &MF = DAG.getMachineFunction();
4146 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4147 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4148 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149 unsigned Opc;
4150 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004151 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4152 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4153 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4154 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 }
4156
4157 SDOperand Chain = DAG.getEntryNode();
4158 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004159 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004161 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004162 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004163 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4164 SDOperand Ops[] = {
4165 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4166 };
4167 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4168 Chain = Value.getValue(1);
4169 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4170 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4171 }
4172
4173 // Build the FP_TO_INT*_IN_MEM
4174 SDOperand Ops[] = { Chain, Value, StackSlot };
4175 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4176
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004177 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178}
4179
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004180SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004181 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4182 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4183 if (FIST.Val == 0) return SDOperand();
4184
4185 // Load the result.
4186 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4187}
4188
4189SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4190 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4191 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4192 if (FIST.Val == 0) return 0;
4193
4194 // Return an i64 load from the stack slot.
4195 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4196
4197 // Use a MERGE_VALUES node to drop the chain result value.
4198 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4199}
4200
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4202 MVT::ValueType VT = Op.getValueType();
4203 MVT::ValueType EltVT = VT;
4204 if (MVT::isVector(VT))
4205 EltVT = MVT::getVectorElementType(VT);
4206 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4207 std::vector<Constant*> CV;
4208 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004209 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210 CV.push_back(C);
4211 CV.push_back(C);
4212 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004213 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214 CV.push_back(C);
4215 CV.push_back(C);
4216 CV.push_back(C);
4217 CV.push_back(C);
4218 }
Dan Gohman11821702007-07-27 17:16:43 +00004219 Constant *C = ConstantVector::get(CV);
4220 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004221 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004222 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004223 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4225}
4226
4227SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4228 MVT::ValueType VT = Op.getValueType();
4229 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004230 unsigned EltNum = 1;
4231 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004232 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004233 EltNum = MVT::getVectorNumElements(VT);
4234 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4236 std::vector<Constant*> CV;
4237 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004238 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004239 CV.push_back(C);
4240 CV.push_back(C);
4241 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004242 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 CV.push_back(C);
4244 CV.push_back(C);
4245 CV.push_back(C);
4246 CV.push_back(C);
4247 }
Dan Gohman11821702007-07-27 17:16:43 +00004248 Constant *C = ConstantVector::get(CV);
4249 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004250 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004251 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004252 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004253 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004254 return DAG.getNode(ISD::BIT_CONVERT, VT,
4255 DAG.getNode(ISD::XOR, MVT::v2i64,
4256 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4257 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4258 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004259 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4260 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261}
4262
4263SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4264 SDOperand Op0 = Op.getOperand(0);
4265 SDOperand Op1 = Op.getOperand(1);
4266 MVT::ValueType VT = Op.getValueType();
4267 MVT::ValueType SrcVT = Op1.getValueType();
4268 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4269
4270 // If second operand is smaller, extend it first.
4271 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4272 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4273 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004274 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004275 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004276 // And if it is bigger, shrink it first.
4277 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004278 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004279 SrcVT = VT;
4280 SrcTy = MVT::getTypeForValueType(SrcVT);
4281 }
4282
4283 // At this point the operands and the result should have the same
4284 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004285
4286 // First get the sign bit of second operand.
4287 std::vector<Constant*> CV;
4288 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004289 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4290 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004291 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004292 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4293 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4294 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4295 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 }
Dan Gohman11821702007-07-27 17:16:43 +00004297 Constant *C = ConstantVector::get(CV);
4298 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004299 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004300 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004301 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004302 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4303
4304 // Shift sign bit right or left if the two operands have different types.
4305 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4306 // Op0 is MVT::f32, Op1 is MVT::f64.
4307 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4308 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4309 DAG.getConstant(32, MVT::i32));
4310 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4311 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004312 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313 }
4314
4315 // Clear first operand sign bit.
4316 CV.clear();
4317 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004318 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4319 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004321 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4322 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4323 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4324 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325 }
Dan Gohman11821702007-07-27 17:16:43 +00004326 C = ConstantVector::get(CV);
4327 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004328 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004329 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004330 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004331 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4332
4333 // Or the value with the sign bit.
4334 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4335}
4336
Evan Cheng621216e2007-09-29 00:00:36 +00004337SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004338 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004339 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004340 SDOperand Op0 = Op.getOperand(0);
4341 SDOperand Op1 = Op.getOperand(1);
4342 SDOperand CC = Op.getOperand(2);
4343 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4344 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4345 unsigned X86CC;
4346
Evan Cheng950aac02007-09-25 01:57:46 +00004347 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004348 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004349 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4350 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004351 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004352 }
Evan Cheng950aac02007-09-25 01:57:46 +00004353
4354 assert(isFP && "Illegal integer SetCC!");
4355
Evan Cheng621216e2007-09-29 00:00:36 +00004356 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004357 switch (SetCCOpcode) {
4358 default: assert(false && "Illegal floating point SetCC!");
4359 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004360 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004361 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004362 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004363 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4364 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4365 }
4366 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004367 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004368 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004369 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004370 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4371 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4372 }
4373 }
4374}
4375
4376
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4378 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004379 SDOperand Cond = Op.getOperand(0);
4380 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004381
4382 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004383 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384
Evan Cheng50d37ab2007-10-08 22:16:29 +00004385 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4386 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004387 if (Cond.getOpcode() == X86ISD::SETCC) {
4388 CC = Cond.getOperand(0);
4389
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390 SDOperand Cmp = Cond.getOperand(1);
4391 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004392 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004393
Evan Cheng50d37ab2007-10-08 22:16:29 +00004394 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004395 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004396 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004397 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004398
Evan Cheng621216e2007-09-29 00:00:36 +00004399 if ((Opc == X86ISD::CMP ||
4400 Opc == X86ISD::COMI ||
4401 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004402 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004403 addTest = false;
4404 }
4405 }
4406
4407 if (addTest) {
4408 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004409 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004410 }
4411
4412 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4413 MVT::Flag);
4414 SmallVector<SDOperand, 4> Ops;
4415 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4416 // condition is true.
4417 Ops.push_back(Op.getOperand(2));
4418 Ops.push_back(Op.getOperand(1));
4419 Ops.push_back(CC);
4420 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004421 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004422}
4423
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4425 bool addTest = true;
4426 SDOperand Chain = Op.getOperand(0);
4427 SDOperand Cond = Op.getOperand(1);
4428 SDOperand Dest = Op.getOperand(2);
4429 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430
4431 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004432 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433
Evan Cheng50d37ab2007-10-08 22:16:29 +00004434 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4435 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 if (Cond.getOpcode() == X86ISD::SETCC) {
4437 CC = Cond.getOperand(0);
4438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 SDOperand Cmp = Cond.getOperand(1);
4440 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004441 if (Opc == X86ISD::CMP ||
4442 Opc == X86ISD::COMI ||
4443 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004444 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004445 addTest = false;
4446 }
4447 }
4448
4449 if (addTest) {
4450 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004451 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004452 }
Evan Cheng621216e2007-09-29 00:00:36 +00004453 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004454 Chain, Op.getOperand(2), CC, Cond);
4455}
4456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457
4458// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4459// Calls to _alloca is needed to probe the stack when allocating more than 4k
4460// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4461// that the guard pages used by the OS virtual memory manager are allocated in
4462// correct sequence.
4463SDOperand
4464X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4465 SelectionDAG &DAG) {
4466 assert(Subtarget->isTargetCygMing() &&
4467 "This should be used only on Cygwin/Mingw targets");
4468
4469 // Get the inputs.
4470 SDOperand Chain = Op.getOperand(0);
4471 SDOperand Size = Op.getOperand(1);
4472 // FIXME: Ensure alignment here
4473
4474 SDOperand Flag;
4475
4476 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004477 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004478
4479 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4480 Flag = Chain.getValue(1);
4481
4482 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4483 SDOperand Ops[] = { Chain,
4484 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4485 DAG.getRegister(X86::EAX, IntPtr),
4486 Flag };
4487 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4488 Flag = Chain.getValue(1);
4489
4490 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4491
4492 std::vector<MVT::ValueType> Tys;
4493 Tys.push_back(SPTy);
4494 Tys.push_back(MVT::Other);
4495 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4496 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4497}
4498
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4500 SDOperand InFlag(0, 0);
4501 SDOperand Chain = Op.getOperand(0);
4502 unsigned Align =
4503 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4504 if (Align == 0) Align = 1;
4505
4506 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004507 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004508 // The libc version is likely to be faster for these cases. It can use the
4509 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004511 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512 MVT::ValueType IntPtr = getPointerTy();
4513 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4514 TargetLowering::ArgListTy Args;
4515 TargetLowering::ArgListEntry Entry;
4516 Entry.Node = Op.getOperand(1);
4517 Entry.Ty = IntPtrTy;
4518 Args.push_back(Entry);
4519 // Extend the unsigned i8 argument to be an int value for the call.
4520 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4521 Entry.Ty = IntPtrTy;
4522 Args.push_back(Entry);
4523 Entry.Node = Op.getOperand(3);
4524 Args.push_back(Entry);
4525 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004526 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4527 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 return CallResult.second;
4529 }
4530
4531 MVT::ValueType AVT;
4532 SDOperand Count;
4533 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4534 unsigned BytesLeft = 0;
4535 bool TwoRepStos = false;
4536 if (ValC) {
4537 unsigned ValReg;
4538 uint64_t Val = ValC->getValue() & 255;
4539
4540 // If the value is a constant, then we can potentially use larger sets.
4541 switch (Align & 3) {
4542 case 2: // WORD aligned
4543 AVT = MVT::i16;
4544 ValReg = X86::AX;
4545 Val = (Val << 8) | Val;
4546 break;
4547 case 0: // DWORD aligned
4548 AVT = MVT::i32;
4549 ValReg = X86::EAX;
4550 Val = (Val << 8) | Val;
4551 Val = (Val << 16) | Val;
4552 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4553 AVT = MVT::i64;
4554 ValReg = X86::RAX;
4555 Val = (Val << 32) | Val;
4556 }
4557 break;
4558 default: // Byte aligned
4559 AVT = MVT::i8;
4560 ValReg = X86::AL;
4561 Count = Op.getOperand(3);
4562 break;
4563 }
4564
4565 if (AVT > MVT::i8) {
4566 if (I) {
4567 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004568 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004569 BytesLeft = I->getValue() % UBytes;
4570 } else {
4571 assert(AVT >= MVT::i32 &&
4572 "Do not use rep;stos if not at least DWORD aligned");
4573 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4574 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4575 TwoRepStos = true;
4576 }
4577 }
4578
4579 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4580 InFlag);
4581 InFlag = Chain.getValue(1);
4582 } else {
4583 AVT = MVT::i8;
4584 Count = Op.getOperand(3);
4585 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4586 InFlag = Chain.getValue(1);
4587 }
4588
4589 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4590 Count, InFlag);
4591 InFlag = Chain.getValue(1);
4592 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4593 Op.getOperand(1), InFlag);
4594 InFlag = Chain.getValue(1);
4595
4596 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4597 SmallVector<SDOperand, 8> Ops;
4598 Ops.push_back(Chain);
4599 Ops.push_back(DAG.getValueType(AVT));
4600 Ops.push_back(InFlag);
4601 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4602
4603 if (TwoRepStos) {
4604 InFlag = Chain.getValue(1);
4605 Count = Op.getOperand(3);
4606 MVT::ValueType CVT = Count.getValueType();
4607 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4608 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4609 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4610 Left, InFlag);
4611 InFlag = Chain.getValue(1);
4612 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4613 Ops.clear();
4614 Ops.push_back(Chain);
4615 Ops.push_back(DAG.getValueType(MVT::i8));
4616 Ops.push_back(InFlag);
4617 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4618 } else if (BytesLeft) {
4619 // Issue stores for the last 1 - 7 bytes.
4620 SDOperand Value;
4621 unsigned Val = ValC->getValue() & 255;
4622 unsigned Offset = I->getValue() - BytesLeft;
4623 SDOperand DstAddr = Op.getOperand(1);
4624 MVT::ValueType AddrVT = DstAddr.getValueType();
4625 if (BytesLeft >= 4) {
4626 Val = (Val << 8) | Val;
4627 Val = (Val << 16) | Val;
4628 Value = DAG.getConstant(Val, MVT::i32);
4629 Chain = DAG.getStore(Chain, Value,
4630 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4631 DAG.getConstant(Offset, AddrVT)),
4632 NULL, 0);
4633 BytesLeft -= 4;
4634 Offset += 4;
4635 }
4636 if (BytesLeft >= 2) {
4637 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4638 Chain = DAG.getStore(Chain, Value,
4639 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4640 DAG.getConstant(Offset, AddrVT)),
4641 NULL, 0);
4642 BytesLeft -= 2;
4643 Offset += 2;
4644 }
4645 if (BytesLeft == 1) {
4646 Value = DAG.getConstant(Val, MVT::i8);
4647 Chain = DAG.getStore(Chain, Value,
4648 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4649 DAG.getConstant(Offset, AddrVT)),
4650 NULL, 0);
4651 }
4652 }
4653
4654 return Chain;
4655}
4656
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004657SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4658 SDOperand Dest,
4659 SDOperand Source,
4660 unsigned Size,
4661 unsigned Align,
4662 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004663 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004664 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665 switch (Align & 3) {
4666 case 2: // WORD aligned
4667 AVT = MVT::i16;
4668 break;
4669 case 0: // DWORD aligned
4670 AVT = MVT::i32;
4671 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4672 AVT = MVT::i64;
4673 break;
4674 default: // Byte aligned
4675 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676 break;
4677 }
4678
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004679 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004680 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004681 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004682
4683 SDOperand InFlag(0, 0);
4684 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4685 Count, InFlag);
4686 InFlag = Chain.getValue(1);
4687 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004688 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004689 InFlag = Chain.getValue(1);
4690 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004691 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692 InFlag = Chain.getValue(1);
4693
4694 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4695 SmallVector<SDOperand, 8> Ops;
4696 Ops.push_back(Chain);
4697 Ops.push_back(DAG.getValueType(AVT));
4698 Ops.push_back(InFlag);
4699 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4700
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004701 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004702 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004703 unsigned Offset = Size - BytesLeft;
4704 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004705 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004706 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004707 MVT::ValueType SrcVT = SrcAddr.getValueType();
4708 SDOperand Value;
4709 if (BytesLeft >= 4) {
4710 Value = DAG.getLoad(MVT::i32, Chain,
4711 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4712 DAG.getConstant(Offset, SrcVT)),
4713 NULL, 0);
4714 Chain = Value.getValue(1);
4715 Chain = DAG.getStore(Chain, Value,
4716 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4717 DAG.getConstant(Offset, DstVT)),
4718 NULL, 0);
4719 BytesLeft -= 4;
4720 Offset += 4;
4721 }
4722 if (BytesLeft >= 2) {
4723 Value = DAG.getLoad(MVT::i16, Chain,
4724 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4725 DAG.getConstant(Offset, SrcVT)),
4726 NULL, 0);
4727 Chain = Value.getValue(1);
4728 Chain = DAG.getStore(Chain, Value,
4729 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4730 DAG.getConstant(Offset, DstVT)),
4731 NULL, 0);
4732 BytesLeft -= 2;
4733 Offset += 2;
4734 }
4735
4736 if (BytesLeft == 1) {
4737 Value = DAG.getLoad(MVT::i8, Chain,
4738 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4739 DAG.getConstant(Offset, SrcVT)),
4740 NULL, 0);
4741 Chain = Value.getValue(1);
4742 Chain = DAG.getStore(Chain, Value,
4743 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4744 DAG.getConstant(Offset, DstVT)),
4745 NULL, 0);
4746 }
4747 }
4748
4749 return Chain;
4750}
4751
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004752/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4753SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004754 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004755 SDOperand TheChain = N->getOperand(0);
4756 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004758 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4759 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4760 MVT::i64, rax.getValue(2));
4761 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004762 DAG.getConstant(32, MVT::i8));
4763 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004764 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004765 };
4766
4767 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004768 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769 }
4770
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004771 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4772 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4773 MVT::i32, eax.getValue(2));
4774 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4775 SDOperand Ops[] = { eax, edx };
4776 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4777
4778 // Use a MERGE_VALUES to return the value and chain.
4779 Ops[1] = edx.getValue(1);
4780 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4781 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004782}
4783
4784SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004785 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004786
4787 if (!Subtarget->is64Bit()) {
4788 // vastart just stores the address of the VarArgsFrameIndex slot into the
4789 // memory location argument.
4790 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004791 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004792 }
4793
4794 // __va_list_tag:
4795 // gp_offset (0 - 6 * 8)
4796 // fp_offset (48 - 48 + 8 * 16)
4797 // overflow_arg_area (point to parameters coming in memory).
4798 // reg_save_area
4799 SmallVector<SDOperand, 8> MemOps;
4800 SDOperand FIN = Op.getOperand(1);
4801 // Store gp_offset
4802 SDOperand Store = DAG.getStore(Op.getOperand(0),
4803 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004804 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004805 MemOps.push_back(Store);
4806
4807 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004808 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004809 Store = DAG.getStore(Op.getOperand(0),
4810 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004811 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 MemOps.push_back(Store);
4813
4814 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004815 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004816 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004817 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818 MemOps.push_back(Store);
4819
4820 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004821 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004822 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004823 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004824 MemOps.push_back(Store);
4825 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4826}
4827
4828SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4829 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4830 SDOperand Chain = Op.getOperand(0);
4831 SDOperand DstPtr = Op.getOperand(1);
4832 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004833 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4834 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004835
Dan Gohman12a9c082008-02-06 22:27:42 +00004836 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004837 Chain = SrcPtr.getValue(1);
4838 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004839 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004841 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004842 if (i == 2)
4843 break;
4844 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004845 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004847 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004848 }
4849 return Chain;
4850}
4851
4852SDOperand
4853X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4854 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4855 switch (IntNo) {
4856 default: return SDOperand(); // Don't custom lower most intrinsics.
4857 // Comparison intrinsics.
4858 case Intrinsic::x86_sse_comieq_ss:
4859 case Intrinsic::x86_sse_comilt_ss:
4860 case Intrinsic::x86_sse_comile_ss:
4861 case Intrinsic::x86_sse_comigt_ss:
4862 case Intrinsic::x86_sse_comige_ss:
4863 case Intrinsic::x86_sse_comineq_ss:
4864 case Intrinsic::x86_sse_ucomieq_ss:
4865 case Intrinsic::x86_sse_ucomilt_ss:
4866 case Intrinsic::x86_sse_ucomile_ss:
4867 case Intrinsic::x86_sse_ucomigt_ss:
4868 case Intrinsic::x86_sse_ucomige_ss:
4869 case Intrinsic::x86_sse_ucomineq_ss:
4870 case Intrinsic::x86_sse2_comieq_sd:
4871 case Intrinsic::x86_sse2_comilt_sd:
4872 case Intrinsic::x86_sse2_comile_sd:
4873 case Intrinsic::x86_sse2_comigt_sd:
4874 case Intrinsic::x86_sse2_comige_sd:
4875 case Intrinsic::x86_sse2_comineq_sd:
4876 case Intrinsic::x86_sse2_ucomieq_sd:
4877 case Intrinsic::x86_sse2_ucomilt_sd:
4878 case Intrinsic::x86_sse2_ucomile_sd:
4879 case Intrinsic::x86_sse2_ucomigt_sd:
4880 case Intrinsic::x86_sse2_ucomige_sd:
4881 case Intrinsic::x86_sse2_ucomineq_sd: {
4882 unsigned Opc = 0;
4883 ISD::CondCode CC = ISD::SETCC_INVALID;
4884 switch (IntNo) {
4885 default: break;
4886 case Intrinsic::x86_sse_comieq_ss:
4887 case Intrinsic::x86_sse2_comieq_sd:
4888 Opc = X86ISD::COMI;
4889 CC = ISD::SETEQ;
4890 break;
4891 case Intrinsic::x86_sse_comilt_ss:
4892 case Intrinsic::x86_sse2_comilt_sd:
4893 Opc = X86ISD::COMI;
4894 CC = ISD::SETLT;
4895 break;
4896 case Intrinsic::x86_sse_comile_ss:
4897 case Intrinsic::x86_sse2_comile_sd:
4898 Opc = X86ISD::COMI;
4899 CC = ISD::SETLE;
4900 break;
4901 case Intrinsic::x86_sse_comigt_ss:
4902 case Intrinsic::x86_sse2_comigt_sd:
4903 Opc = X86ISD::COMI;
4904 CC = ISD::SETGT;
4905 break;
4906 case Intrinsic::x86_sse_comige_ss:
4907 case Intrinsic::x86_sse2_comige_sd:
4908 Opc = X86ISD::COMI;
4909 CC = ISD::SETGE;
4910 break;
4911 case Intrinsic::x86_sse_comineq_ss:
4912 case Intrinsic::x86_sse2_comineq_sd:
4913 Opc = X86ISD::COMI;
4914 CC = ISD::SETNE;
4915 break;
4916 case Intrinsic::x86_sse_ucomieq_ss:
4917 case Intrinsic::x86_sse2_ucomieq_sd:
4918 Opc = X86ISD::UCOMI;
4919 CC = ISD::SETEQ;
4920 break;
4921 case Intrinsic::x86_sse_ucomilt_ss:
4922 case Intrinsic::x86_sse2_ucomilt_sd:
4923 Opc = X86ISD::UCOMI;
4924 CC = ISD::SETLT;
4925 break;
4926 case Intrinsic::x86_sse_ucomile_ss:
4927 case Intrinsic::x86_sse2_ucomile_sd:
4928 Opc = X86ISD::UCOMI;
4929 CC = ISD::SETLE;
4930 break;
4931 case Intrinsic::x86_sse_ucomigt_ss:
4932 case Intrinsic::x86_sse2_ucomigt_sd:
4933 Opc = X86ISD::UCOMI;
4934 CC = ISD::SETGT;
4935 break;
4936 case Intrinsic::x86_sse_ucomige_ss:
4937 case Intrinsic::x86_sse2_ucomige_sd:
4938 Opc = X86ISD::UCOMI;
4939 CC = ISD::SETGE;
4940 break;
4941 case Intrinsic::x86_sse_ucomineq_ss:
4942 case Intrinsic::x86_sse2_ucomineq_sd:
4943 Opc = X86ISD::UCOMI;
4944 CC = ISD::SETNE;
4945 break;
4946 }
4947
4948 unsigned X86CC;
4949 SDOperand LHS = Op.getOperand(1);
4950 SDOperand RHS = Op.getOperand(2);
4951 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4952
Evan Cheng621216e2007-09-29 00:00:36 +00004953 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4954 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4955 DAG.getConstant(X86CC, MVT::i8), Cond);
4956 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 }
4958 }
4959}
4960
4961SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4962 // Depths > 0 not supported yet!
4963 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4964 return SDOperand();
4965
4966 // Just load the return address
4967 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4968 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4969}
4970
4971SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4972 // Depths > 0 not supported yet!
4973 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4974 return SDOperand();
4975
4976 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4977 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00004978 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004979}
4980
4981SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4982 SelectionDAG &DAG) {
4983 // Is not yet supported on x86-64
4984 if (Subtarget->is64Bit())
4985 return SDOperand();
4986
Chris Lattner5872a362008-01-17 07:00:52 +00004987 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004988}
4989
4990SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4991{
4992 assert(!Subtarget->is64Bit() &&
4993 "Lowering of eh_return builtin is not supported yet on x86-64");
4994
4995 MachineFunction &MF = DAG.getMachineFunction();
4996 SDOperand Chain = Op.getOperand(0);
4997 SDOperand Offset = Op.getOperand(1);
4998 SDOperand Handler = Op.getOperand(2);
4999
5000 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5001 getPointerTy());
5002
5003 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005004 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005005 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5006 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5007 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005008 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009
5010 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5011 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5012}
5013
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005014SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5015 SelectionDAG &DAG) {
5016 SDOperand Root = Op.getOperand(0);
5017 SDOperand Trmp = Op.getOperand(1); // trampoline
5018 SDOperand FPtr = Op.getOperand(2); // nested function
5019 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5020
Dan Gohman12a9c082008-02-06 22:27:42 +00005021 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005022
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005023 const X86InstrInfo *TII =
5024 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5025
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005026 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005027 SDOperand OutChains[6];
5028
5029 // Large code-model.
5030
5031 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5032 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5033
5034 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005035 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005036 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005037 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005038
5039 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5040
5041 // Load the pointer to the nested function into R11.
5042 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5043 SDOperand Addr = Trmp;
5044 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005045 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005046
5047 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005048 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005049
5050 // Load the 'nest' parameter value into R10.
5051 // R10 is specified in X86CallingConv.td
5052 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5053 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5054 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005055 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005056
5057 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005058 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005059
5060 // Jump to the nested function.
5061 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5062 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5063 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005064 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005065
5066 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5067 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5068 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005069 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005070
5071 SDOperand Ops[] =
5072 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5073 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005074 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005075 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005076 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5077 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005078 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005079
5080 switch (CC) {
5081 default:
5082 assert(0 && "Unsupported calling convention");
5083 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005084 case CallingConv::X86_StdCall: {
5085 // Pass 'nest' parameter in ECX.
5086 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005087 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005088
5089 // Check that ECX wasn't needed by an 'inreg' parameter.
5090 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005091 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005092
5093 if (Attrs && !Func->isVarArg()) {
5094 unsigned InRegCount = 0;
5095 unsigned Idx = 1;
5096
5097 for (FunctionType::param_iterator I = FTy->param_begin(),
5098 E = FTy->param_end(); I != E; ++I, ++Idx)
5099 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5100 // FIXME: should only count parameters that are lowered to integers.
5101 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5102
5103 if (InRegCount > 2) {
5104 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5105 abort();
5106 }
5107 }
5108 break;
5109 }
5110 case CallingConv::X86_FastCall:
5111 // Pass 'nest' parameter in EAX.
5112 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005113 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005114 break;
5115 }
5116
5117 SDOperand OutChains[4];
5118 SDOperand Addr, Disp;
5119
5120 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5121 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5122
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005123 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5124 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005125 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005126 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005127 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005128
5129 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005130 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005131
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005132 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005133 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5134 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005135 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005136
5137 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005138 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005139
Duncan Sands7407a9f2007-09-11 14:10:23 +00005140 SDOperand Ops[] =
5141 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5142 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005143 }
5144}
5145
Dan Gohman819574c2008-01-31 00:41:03 +00005146SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005147 /*
5148 The rounding mode is in bits 11:10 of FPSR, and has the following
5149 settings:
5150 00 Round to nearest
5151 01 Round to -inf
5152 10 Round to +inf
5153 11 Round to 0
5154
5155 FLT_ROUNDS, on the other hand, expects the following:
5156 -1 Undefined
5157 0 Round to 0
5158 1 Round to nearest
5159 2 Round to +inf
5160 3 Round to -inf
5161
5162 To perform the conversion, we do:
5163 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5164 */
5165
5166 MachineFunction &MF = DAG.getMachineFunction();
5167 const TargetMachine &TM = MF.getTarget();
5168 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5169 unsigned StackAlignment = TFI.getStackAlignment();
5170 MVT::ValueType VT = Op.getValueType();
5171
5172 // Save FP Control Word to stack slot
5173 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5174 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5175
5176 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5177 DAG.getEntryNode(), StackSlot);
5178
5179 // Load FP Control Word from stack slot
5180 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5181
5182 // Transform as necessary
5183 SDOperand CWD1 =
5184 DAG.getNode(ISD::SRL, MVT::i16,
5185 DAG.getNode(ISD::AND, MVT::i16,
5186 CWD, DAG.getConstant(0x800, MVT::i16)),
5187 DAG.getConstant(11, MVT::i8));
5188 SDOperand CWD2 =
5189 DAG.getNode(ISD::SRL, MVT::i16,
5190 DAG.getNode(ISD::AND, MVT::i16,
5191 CWD, DAG.getConstant(0x400, MVT::i16)),
5192 DAG.getConstant(9, MVT::i8));
5193
5194 SDOperand RetVal =
5195 DAG.getNode(ISD::AND, MVT::i16,
5196 DAG.getNode(ISD::ADD, MVT::i16,
5197 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5198 DAG.getConstant(1, MVT::i16)),
5199 DAG.getConstant(3, MVT::i16));
5200
5201
5202 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5203 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5204}
5205
Evan Cheng48679f42007-12-14 02:13:44 +00005206SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5207 MVT::ValueType VT = Op.getValueType();
5208 MVT::ValueType OpVT = VT;
5209 unsigned NumBits = MVT::getSizeInBits(VT);
5210
5211 Op = Op.getOperand(0);
5212 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005213 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005214 OpVT = MVT::i32;
5215 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5216 }
Evan Cheng48679f42007-12-14 02:13:44 +00005217
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005218 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5219 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5220 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5221
5222 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5223 SmallVector<SDOperand, 4> Ops;
5224 Ops.push_back(Op);
5225 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5226 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5227 Ops.push_back(Op.getValue(1));
5228 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5229
5230 // Finally xor with NumBits-1.
5231 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5232
Evan Cheng48679f42007-12-14 02:13:44 +00005233 if (VT == MVT::i8)
5234 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5235 return Op;
5236}
5237
5238SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5239 MVT::ValueType VT = Op.getValueType();
5240 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005241 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005242
5243 Op = Op.getOperand(0);
5244 if (VT == MVT::i8) {
5245 OpVT = MVT::i32;
5246 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5247 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005248
5249 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5250 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5251 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5252
5253 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5254 SmallVector<SDOperand, 4> Ops;
5255 Ops.push_back(Op);
5256 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5257 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5258 Ops.push_back(Op.getValue(1));
5259 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5260
Evan Cheng48679f42007-12-14 02:13:44 +00005261 if (VT == MVT::i8)
5262 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5263 return Op;
5264}
5265
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005266/// LowerOperation - Provide custom lowering hooks for some operations.
5267///
5268SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5269 switch (Op.getOpcode()) {
5270 default: assert(0 && "Should not custom lower this!");
5271 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5272 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5273 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5274 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5275 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5276 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5277 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5278 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5279 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5280 case ISD::SHL_PARTS:
5281 case ISD::SRA_PARTS:
5282 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5283 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5284 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5285 case ISD::FABS: return LowerFABS(Op, DAG);
5286 case ISD::FNEG: return LowerFNEG(Op, DAG);
5287 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005288 case ISD::SETCC: return LowerSETCC(Op, DAG);
5289 case ISD::SELECT: return LowerSELECT(Op, DAG);
5290 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005291 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5292 case ISD::CALL: return LowerCALL(Op, DAG);
5293 case ISD::RET: return LowerRET(Op, DAG);
5294 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5295 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5296 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005297 case ISD::VASTART: return LowerVASTART(Op, DAG);
5298 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5299 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5300 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5301 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5302 case ISD::FRAME_TO_ARGS_OFFSET:
5303 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5304 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5305 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005306 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005307 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005308 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5309 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005310
5311 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5312 case ISD::READCYCLECOUNTER:
5313 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005315}
5316
5317/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5318SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5319 switch (N->getOpcode()) {
5320 default: assert(0 && "Should not custom lower this!");
5321 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5322 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5323 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005324}
5325
5326const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5327 switch (Opcode) {
5328 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005329 case X86ISD::BSF: return "X86ISD::BSF";
5330 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005331 case X86ISD::SHLD: return "X86ISD::SHLD";
5332 case X86ISD::SHRD: return "X86ISD::SHRD";
5333 case X86ISD::FAND: return "X86ISD::FAND";
5334 case X86ISD::FOR: return "X86ISD::FOR";
5335 case X86ISD::FXOR: return "X86ISD::FXOR";
5336 case X86ISD::FSRL: return "X86ISD::FSRL";
5337 case X86ISD::FILD: return "X86ISD::FILD";
5338 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5339 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5340 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5341 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5342 case X86ISD::FLD: return "X86ISD::FLD";
5343 case X86ISD::FST: return "X86ISD::FST";
5344 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng931a8f42008-01-29 19:34:22 +00005345 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005346 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5347 case X86ISD::CALL: return "X86ISD::CALL";
5348 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5349 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5350 case X86ISD::CMP: return "X86ISD::CMP";
5351 case X86ISD::COMI: return "X86ISD::COMI";
5352 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5353 case X86ISD::SETCC: return "X86ISD::SETCC";
5354 case X86ISD::CMOV: return "X86ISD::CMOV";
5355 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5356 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5357 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5358 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005359 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5360 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5361 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Nate Begemand77e59e2008-02-11 04:19:36 +00005362 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005363 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005364 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5365 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005366 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5367 case X86ISD::FMAX: return "X86ISD::FMAX";
5368 case X86ISD::FMIN: return "X86ISD::FMIN";
5369 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5370 case X86ISD::FRCP: return "X86ISD::FRCP";
5371 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5372 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5373 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005374 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005375 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005376 }
5377}
5378
5379// isLegalAddressingMode - Return true if the addressing mode represented
5380// by AM is legal for this target, for a load/store of the specified type.
5381bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5382 const Type *Ty) const {
5383 // X86 supports extremely general addressing modes.
5384
5385 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5386 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5387 return false;
5388
5389 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005390 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005391 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5392 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005393
5394 // X86-64 only supports addr of globals in small code model.
5395 if (Subtarget->is64Bit()) {
5396 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5397 return false;
5398 // If lower 4G is not available, then we must use rip-relative addressing.
5399 if (AM.BaseOffs || AM.Scale > 1)
5400 return false;
5401 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005402 }
5403
5404 switch (AM.Scale) {
5405 case 0:
5406 case 1:
5407 case 2:
5408 case 4:
5409 case 8:
5410 // These scales always work.
5411 break;
5412 case 3:
5413 case 5:
5414 case 9:
5415 // These scales are formed with basereg+scalereg. Only accept if there is
5416 // no basereg yet.
5417 if (AM.HasBaseReg)
5418 return false;
5419 break;
5420 default: // Other stuff never works.
5421 return false;
5422 }
5423
5424 return true;
5425}
5426
5427
Evan Cheng27a820a2007-10-26 01:56:11 +00005428bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5429 if (!Ty1->isInteger() || !Ty2->isInteger())
5430 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005431 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5432 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5433 if (NumBits1 <= NumBits2)
5434 return false;
5435 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005436}
5437
Evan Cheng9decb332007-10-29 19:58:20 +00005438bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5439 MVT::ValueType VT2) const {
5440 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5441 return false;
5442 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5443 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5444 if (NumBits1 <= NumBits2)
5445 return false;
5446 return Subtarget->is64Bit() || NumBits1 < 64;
5447}
Evan Cheng27a820a2007-10-26 01:56:11 +00005448
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449/// isShuffleMaskLegal - Targets can use this to indicate that they only
5450/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5451/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5452/// are assumed to be legal.
5453bool
5454X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5455 // Only do shuffles on 128-bit vector types for now.
5456 if (MVT::getSizeInBits(VT) == 64) return false;
5457 return (Mask.Val->getNumOperands() <= 4 ||
5458 isIdentityMask(Mask.Val) ||
5459 isIdentityMask(Mask.Val, true) ||
5460 isSplatMask(Mask.Val) ||
5461 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5462 X86::isUNPCKLMask(Mask.Val) ||
5463 X86::isUNPCKHMask(Mask.Val) ||
5464 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5465 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5466}
5467
5468bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5469 MVT::ValueType EVT,
5470 SelectionDAG &DAG) const {
5471 unsigned NumElts = BVOps.size();
5472 // Only do shuffles on 128-bit vector types for now.
5473 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5474 if (NumElts == 2) return true;
5475 if (NumElts == 4) {
5476 return (isMOVLMask(&BVOps[0], 4) ||
5477 isCommutedMOVL(&BVOps[0], 4, true) ||
5478 isSHUFPMask(&BVOps[0], 4) ||
5479 isCommutedSHUFP(&BVOps[0], 4));
5480 }
5481 return false;
5482}
5483
5484//===----------------------------------------------------------------------===//
5485// X86 Scheduler Hooks
5486//===----------------------------------------------------------------------===//
5487
5488MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005489X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5490 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005491 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5492 switch (MI->getOpcode()) {
5493 default: assert(false && "Unexpected instr type to insert");
5494 case X86::CMOV_FR32:
5495 case X86::CMOV_FR64:
5496 case X86::CMOV_V4F32:
5497 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005498 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005499 // To "insert" a SELECT_CC instruction, we actually have to insert the
5500 // diamond control-flow pattern. The incoming instruction knows the
5501 // destination vreg to set, the condition code register to branch on, the
5502 // true/false values to select between, and a branch opcode to use.
5503 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5504 ilist<MachineBasicBlock>::iterator It = BB;
5505 ++It;
5506
5507 // thisMBB:
5508 // ...
5509 // TrueVal = ...
5510 // cmpTY ccX, r1, r2
5511 // bCC copy1MBB
5512 // fallthrough --> copy0MBB
5513 MachineBasicBlock *thisMBB = BB;
5514 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5515 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5516 unsigned Opc =
5517 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5518 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5519 MachineFunction *F = BB->getParent();
5520 F->getBasicBlockList().insert(It, copy0MBB);
5521 F->getBasicBlockList().insert(It, sinkMBB);
5522 // Update machine-CFG edges by first adding all successors of the current
5523 // block to the new block which will contain the Phi node for the select.
5524 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5525 e = BB->succ_end(); i != e; ++i)
5526 sinkMBB->addSuccessor(*i);
5527 // Next, remove all successors of the current block, and add the true
5528 // and fallthrough blocks as its successors.
5529 while(!BB->succ_empty())
5530 BB->removeSuccessor(BB->succ_begin());
5531 BB->addSuccessor(copy0MBB);
5532 BB->addSuccessor(sinkMBB);
5533
5534 // copy0MBB:
5535 // %FalseValue = ...
5536 // # fallthrough to sinkMBB
5537 BB = copy0MBB;
5538
5539 // Update machine-CFG edges
5540 BB->addSuccessor(sinkMBB);
5541
5542 // sinkMBB:
5543 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5544 // ...
5545 BB = sinkMBB;
5546 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5547 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5548 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5549
5550 delete MI; // The pseudo instruction is gone now.
5551 return BB;
5552 }
5553
5554 case X86::FP32_TO_INT16_IN_MEM:
5555 case X86::FP32_TO_INT32_IN_MEM:
5556 case X86::FP32_TO_INT64_IN_MEM:
5557 case X86::FP64_TO_INT16_IN_MEM:
5558 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005559 case X86::FP64_TO_INT64_IN_MEM:
5560 case X86::FP80_TO_INT16_IN_MEM:
5561 case X86::FP80_TO_INT32_IN_MEM:
5562 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563 // Change the floating point control register to use "round towards zero"
5564 // mode when truncating to an integer value.
5565 MachineFunction *F = BB->getParent();
5566 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5567 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5568
5569 // Load the old value of the high byte of the control word...
5570 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005571 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005572 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5573
5574 // Set the high part to be round to zero...
5575 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5576 .addImm(0xC7F);
5577
5578 // Reload the modified control word now...
5579 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5580
5581 // Restore the memory image of control word to original value
5582 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5583 .addReg(OldCW);
5584
5585 // Get the X86 opcode to use.
5586 unsigned Opc;
5587 switch (MI->getOpcode()) {
5588 default: assert(0 && "illegal opcode!");
5589 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5590 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5591 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5592 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5593 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5594 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005595 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5596 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5597 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598 }
5599
5600 X86AddressMode AM;
5601 MachineOperand &Op = MI->getOperand(0);
5602 if (Op.isRegister()) {
5603 AM.BaseType = X86AddressMode::RegBase;
5604 AM.Base.Reg = Op.getReg();
5605 } else {
5606 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005607 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608 }
5609 Op = MI->getOperand(1);
5610 if (Op.isImmediate())
5611 AM.Scale = Op.getImm();
5612 Op = MI->getOperand(2);
5613 if (Op.isImmediate())
5614 AM.IndexReg = Op.getImm();
5615 Op = MI->getOperand(3);
5616 if (Op.isGlobalAddress()) {
5617 AM.GV = Op.getGlobal();
5618 } else {
5619 AM.Disp = Op.getImm();
5620 }
5621 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5622 .addReg(MI->getOperand(4).getReg());
5623
5624 // Reload the original control word now.
5625 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5626
5627 delete MI; // The pseudo instruction is gone now.
5628 return BB;
5629 }
5630 }
5631}
5632
5633//===----------------------------------------------------------------------===//
5634// X86 Optimization Hooks
5635//===----------------------------------------------------------------------===//
5636
5637void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005638 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005639 APInt &KnownZero,
5640 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005641 const SelectionDAG &DAG,
5642 unsigned Depth) const {
5643 unsigned Opc = Op.getOpcode();
5644 assert((Opc >= ISD::BUILTIN_OP_END ||
5645 Opc == ISD::INTRINSIC_WO_CHAIN ||
5646 Opc == ISD::INTRINSIC_W_CHAIN ||
5647 Opc == ISD::INTRINSIC_VOID) &&
5648 "Should use MaskedValueIsZero if you don't know whether Op"
5649 " is a target node!");
5650
Dan Gohman1d79e432008-02-13 23:07:24 +00005651 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005652 switch (Opc) {
5653 default: break;
5654 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005655 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5656 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005657 break;
5658 }
5659}
5660
5661/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5662/// element of the result of the vector shuffle.
5663static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5664 MVT::ValueType VT = N->getValueType(0);
5665 SDOperand PermMask = N->getOperand(2);
5666 unsigned NumElems = PermMask.getNumOperands();
5667 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5668 i %= NumElems;
5669 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5670 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005671 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005672 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5673 SDOperand Idx = PermMask.getOperand(i);
5674 if (Idx.getOpcode() == ISD::UNDEF)
5675 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5676 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5677 }
5678 return SDOperand();
5679}
5680
5681/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5682/// node is a GlobalAddress + an offset.
5683static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5684 unsigned Opc = N->getOpcode();
5685 if (Opc == X86ISD::Wrapper) {
5686 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5687 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5688 return true;
5689 }
5690 } else if (Opc == ISD::ADD) {
5691 SDOperand N1 = N->getOperand(0);
5692 SDOperand N2 = N->getOperand(1);
5693 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5694 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5695 if (V) {
5696 Offset += V->getSignExtended();
5697 return true;
5698 }
5699 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5700 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5701 if (V) {
5702 Offset += V->getSignExtended();
5703 return true;
5704 }
5705 }
5706 }
5707 return false;
5708}
5709
5710/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5711/// + Dist * Size.
5712static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5713 MachineFrameInfo *MFI) {
5714 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5715 return false;
5716
5717 SDOperand Loc = N->getOperand(1);
5718 SDOperand BaseLoc = Base->getOperand(1);
5719 if (Loc.getOpcode() == ISD::FrameIndex) {
5720 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5721 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005722 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5723 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005724 int FS = MFI->getObjectSize(FI);
5725 int BFS = MFI->getObjectSize(BFI);
5726 if (FS != BFS || FS != Size) return false;
5727 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5728 } else {
5729 GlobalValue *GV1 = NULL;
5730 GlobalValue *GV2 = NULL;
5731 int64_t Offset1 = 0;
5732 int64_t Offset2 = 0;
5733 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5734 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5735 if (isGA1 && isGA2 && GV1 == GV2)
5736 return Offset1 == (Offset2 + Dist*Size);
5737 }
5738
5739 return false;
5740}
5741
5742static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5743 const X86Subtarget *Subtarget) {
5744 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005745 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005746 if (isGAPlusOffset(Base, GV, Offset))
5747 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005748 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005749 return false;
5750}
5751
5752
5753/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5754/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5755/// if the load addresses are consecutive, non-overlapping, and in the right
5756/// order.
5757static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5758 const X86Subtarget *Subtarget) {
5759 MachineFunction &MF = DAG.getMachineFunction();
5760 MachineFrameInfo *MFI = MF.getFrameInfo();
5761 MVT::ValueType VT = N->getValueType(0);
5762 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5763 SDOperand PermMask = N->getOperand(2);
5764 int NumElems = (int)PermMask.getNumOperands();
5765 SDNode *Base = NULL;
5766 for (int i = 0; i < NumElems; ++i) {
5767 SDOperand Idx = PermMask.getOperand(i);
5768 if (Idx.getOpcode() == ISD::UNDEF) {
5769 if (!Base) return SDOperand();
5770 } else {
5771 SDOperand Arg =
5772 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5773 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5774 return SDOperand();
5775 if (!Base)
5776 Base = Arg.Val;
5777 else if (!isConsecutiveLoad(Arg.Val, Base,
5778 i, MVT::getSizeInBits(EVT)/8,MFI))
5779 return SDOperand();
5780 }
5781 }
5782
5783 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005784 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005785 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005787 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005788 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005789 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5790 LD->getSrcValueOffset(), LD->isVolatile(),
5791 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005792 }
5793}
5794
5795/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5796static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5797 const X86Subtarget *Subtarget) {
5798 SDOperand Cond = N->getOperand(0);
5799
5800 // If we have SSE[12] support, try to form min/max nodes.
5801 if (Subtarget->hasSSE2() &&
5802 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5803 if (Cond.getOpcode() == ISD::SETCC) {
5804 // Get the LHS/RHS of the select.
5805 SDOperand LHS = N->getOperand(1);
5806 SDOperand RHS = N->getOperand(2);
5807 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5808
5809 unsigned Opcode = 0;
5810 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5811 switch (CC) {
5812 default: break;
5813 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5814 case ISD::SETULE:
5815 case ISD::SETLE:
5816 if (!UnsafeFPMath) break;
5817 // FALL THROUGH.
5818 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5819 case ISD::SETLT:
5820 Opcode = X86ISD::FMIN;
5821 break;
5822
5823 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5824 case ISD::SETUGT:
5825 case ISD::SETGT:
5826 if (!UnsafeFPMath) break;
5827 // FALL THROUGH.
5828 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5829 case ISD::SETGE:
5830 Opcode = X86ISD::FMAX;
5831 break;
5832 }
5833 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5834 switch (CC) {
5835 default: break;
5836 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5837 case ISD::SETUGT:
5838 case ISD::SETGT:
5839 if (!UnsafeFPMath) break;
5840 // FALL THROUGH.
5841 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5842 case ISD::SETGE:
5843 Opcode = X86ISD::FMIN;
5844 break;
5845
5846 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5847 case ISD::SETULE:
5848 case ISD::SETLE:
5849 if (!UnsafeFPMath) break;
5850 // FALL THROUGH.
5851 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5852 case ISD::SETLT:
5853 Opcode = X86ISD::FMAX;
5854 break;
5855 }
5856 }
5857
5858 if (Opcode)
5859 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5860 }
5861
5862 }
5863
5864 return SDOperand();
5865}
5866
Chris Lattner470d5dc2008-01-25 06:14:17 +00005867/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
5868/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00005869static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00005870 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
5871 // F[X]OR(0.0, x) -> x
5872 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00005873 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5874 if (C->getValueAPF().isPosZero())
5875 return N->getOperand(1);
5876 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5877 if (C->getValueAPF().isPosZero())
5878 return N->getOperand(0);
5879 return SDOperand();
5880}
5881
5882/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
5883static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
5884 // FAND(0.0, x) -> 0.0
5885 // FAND(x, 0.0) -> 0.0
5886 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5887 if (C->getValueAPF().isPosZero())
5888 return N->getOperand(0);
5889 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5890 if (C->getValueAPF().isPosZero())
5891 return N->getOperand(1);
5892 return SDOperand();
5893}
5894
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005895
5896SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5897 DAGCombinerInfo &DCI) const {
5898 SelectionDAG &DAG = DCI.DAG;
5899 switch (N->getOpcode()) {
5900 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00005901 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
5902 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00005903 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00005904 case X86ISD::FOR: return PerformFORCombine(N, DAG);
5905 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005906 }
5907
5908 return SDOperand();
5909}
5910
5911//===----------------------------------------------------------------------===//
5912// X86 Inline Assembly Support
5913//===----------------------------------------------------------------------===//
5914
5915/// getConstraintType - Given a constraint letter, return the type of
5916/// constraint it is for this target.
5917X86TargetLowering::ConstraintType
5918X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5919 if (Constraint.size() == 1) {
5920 switch (Constraint[0]) {
5921 case 'A':
5922 case 'r':
5923 case 'R':
5924 case 'l':
5925 case 'q':
5926 case 'Q':
5927 case 'x':
5928 case 'Y':
5929 return C_RegisterClass;
5930 default:
5931 break;
5932 }
5933 }
5934 return TargetLowering::getConstraintType(Constraint);
5935}
5936
Dale Johannesene99fc902008-01-29 02:21:21 +00005937/// LowerXConstraint - try to replace an X constraint, which matches anything,
5938/// with another that has more specific requirements based on the type of the
5939/// corresponding operand.
5940void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
5941 std::string& s) const {
5942 if (MVT::isFloatingPoint(ConstraintVT)) {
5943 if (Subtarget->hasSSE2())
5944 s = "Y";
5945 else if (Subtarget->hasSSE1())
5946 s = "x";
5947 else
5948 s = "f";
5949 } else
5950 return TargetLowering::lowerXConstraint(ConstraintVT, s);
5951}
5952
Chris Lattnera531abc2007-08-25 00:47:38 +00005953/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5954/// vector. If it is invalid, don't add anything to Ops.
5955void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5956 char Constraint,
5957 std::vector<SDOperand>&Ops,
5958 SelectionDAG &DAG) {
5959 SDOperand Result(0, 0);
5960
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961 switch (Constraint) {
5962 default: break;
5963 case 'I':
5964 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005965 if (C->getValue() <= 31) {
5966 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5967 break;
5968 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005969 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005970 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005971 case 'N':
5972 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005973 if (C->getValue() <= 255) {
5974 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5975 break;
5976 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005977 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005978 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005979 case 'i': {
5980 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00005981 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5982 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5983 break;
5984 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005985
5986 // If we are in non-pic codegen mode, we allow the address of a global (with
5987 // an optional displacement) to be used with 'i'.
5988 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5989 int64_t Offset = 0;
5990
5991 // Match either (GA) or (GA+C)
5992 if (GA) {
5993 Offset = GA->getOffset();
5994 } else if (Op.getOpcode() == ISD::ADD) {
5995 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5996 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5997 if (C && GA) {
5998 Offset = GA->getOffset()+C->getValue();
5999 } else {
6000 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6001 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6002 if (C && GA)
6003 Offset = GA->getOffset()+C->getValue();
6004 else
6005 C = 0, GA = 0;
6006 }
6007 }
6008
6009 if (GA) {
6010 // If addressing this global requires a load (e.g. in PIC mode), we can't
6011 // match.
6012 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6013 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006014 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006015
6016 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6017 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006018 Result = Op;
6019 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006020 }
6021
6022 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006023 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006024 }
6025 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006026
6027 if (Result.Val) {
6028 Ops.push_back(Result);
6029 return;
6030 }
6031 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006032}
6033
6034std::vector<unsigned> X86TargetLowering::
6035getRegClassForInlineAsmConstraint(const std::string &Constraint,
6036 MVT::ValueType VT) const {
6037 if (Constraint.size() == 1) {
6038 // FIXME: not handling fp-stack yet!
6039 switch (Constraint[0]) { // GCC X86 Constraint Letters
6040 default: break; // Unknown constraint letter
6041 case 'A': // EAX/EDX
6042 if (VT == MVT::i32 || VT == MVT::i64)
6043 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6044 break;
6045 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6046 case 'Q': // Q_REGS
6047 if (VT == MVT::i32)
6048 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6049 else if (VT == MVT::i16)
6050 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6051 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006052 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006053 else if (VT == MVT::i64)
6054 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6055 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006056 }
6057 }
6058
6059 return std::vector<unsigned>();
6060}
6061
6062std::pair<unsigned, const TargetRegisterClass*>
6063X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6064 MVT::ValueType VT) const {
6065 // First, see if this is a constraint that directly corresponds to an LLVM
6066 // register class.
6067 if (Constraint.size() == 1) {
6068 // GCC Constraint Letters
6069 switch (Constraint[0]) {
6070 default: break;
6071 case 'r': // GENERAL_REGS
6072 case 'R': // LEGACY_REGS
6073 case 'l': // INDEX_REGS
6074 if (VT == MVT::i64 && Subtarget->is64Bit())
6075 return std::make_pair(0U, X86::GR64RegisterClass);
6076 if (VT == MVT::i32)
6077 return std::make_pair(0U, X86::GR32RegisterClass);
6078 else if (VT == MVT::i16)
6079 return std::make_pair(0U, X86::GR16RegisterClass);
6080 else if (VT == MVT::i8)
6081 return std::make_pair(0U, X86::GR8RegisterClass);
6082 break;
6083 case 'y': // MMX_REGS if MMX allowed.
6084 if (!Subtarget->hasMMX()) break;
6085 return std::make_pair(0U, X86::VR64RegisterClass);
6086 break;
6087 case 'Y': // SSE_REGS if SSE2 allowed
6088 if (!Subtarget->hasSSE2()) break;
6089 // FALL THROUGH.
6090 case 'x': // SSE_REGS if SSE1 allowed
6091 if (!Subtarget->hasSSE1()) break;
6092
6093 switch (VT) {
6094 default: break;
6095 // Scalar SSE types.
6096 case MVT::f32:
6097 case MVT::i32:
6098 return std::make_pair(0U, X86::FR32RegisterClass);
6099 case MVT::f64:
6100 case MVT::i64:
6101 return std::make_pair(0U, X86::FR64RegisterClass);
6102 // Vector types.
6103 case MVT::v16i8:
6104 case MVT::v8i16:
6105 case MVT::v4i32:
6106 case MVT::v2i64:
6107 case MVT::v4f32:
6108 case MVT::v2f64:
6109 return std::make_pair(0U, X86::VR128RegisterClass);
6110 }
6111 break;
6112 }
6113 }
6114
6115 // Use the default implementation in TargetLowering to convert the register
6116 // constraint into a member of a register class.
6117 std::pair<unsigned, const TargetRegisterClass*> Res;
6118 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6119
6120 // Not found as a standard register?
6121 if (Res.second == 0) {
6122 // GCC calls "st(0)" just plain "st".
6123 if (StringsEqualNoCase("{st}", Constraint)) {
6124 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006125 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126 }
6127
6128 return Res;
6129 }
6130
6131 // Otherwise, check to see if this is a register class of the wrong value
6132 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6133 // turn into {ax},{dx}.
6134 if (Res.second->hasType(VT))
6135 return Res; // Correct type already, nothing to do.
6136
6137 // All of the single-register GCC register classes map their values onto
6138 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6139 // really want an 8-bit or 32-bit register, map to the appropriate register
6140 // class and return the appropriate register.
6141 if (Res.second != X86::GR16RegisterClass)
6142 return Res;
6143
6144 if (VT == MVT::i8) {
6145 unsigned DestReg = 0;
6146 switch (Res.first) {
6147 default: break;
6148 case X86::AX: DestReg = X86::AL; break;
6149 case X86::DX: DestReg = X86::DL; break;
6150 case X86::CX: DestReg = X86::CL; break;
6151 case X86::BX: DestReg = X86::BL; break;
6152 }
6153 if (DestReg) {
6154 Res.first = DestReg;
6155 Res.second = Res.second = X86::GR8RegisterClass;
6156 }
6157 } else if (VT == MVT::i32) {
6158 unsigned DestReg = 0;
6159 switch (Res.first) {
6160 default: break;
6161 case X86::AX: DestReg = X86::EAX; break;
6162 case X86::DX: DestReg = X86::EDX; break;
6163 case X86::CX: DestReg = X86::ECX; break;
6164 case X86::BX: DestReg = X86::EBX; break;
6165 case X86::SI: DestReg = X86::ESI; break;
6166 case X86::DI: DestReg = X86::EDI; break;
6167 case X86::BP: DestReg = X86::EBP; break;
6168 case X86::SP: DestReg = X86::ESP; break;
6169 }
6170 if (DestReg) {
6171 Res.first = DestReg;
6172 Res.second = Res.second = X86::GR32RegisterClass;
6173 }
6174 } else if (VT == MVT::i64) {
6175 unsigned DestReg = 0;
6176 switch (Res.first) {
6177 default: break;
6178 case X86::AX: DestReg = X86::RAX; break;
6179 case X86::DX: DestReg = X86::RDX; break;
6180 case X86::CX: DestReg = X86::RCX; break;
6181 case X86::BX: DestReg = X86::RBX; break;
6182 case X86::SI: DestReg = X86::RSI; break;
6183 case X86::DI: DestReg = X86::RDI; break;
6184 case X86::BP: DestReg = X86::RBP; break;
6185 case X86::SP: DestReg = X86::RSP; break;
6186 }
6187 if (DestReg) {
6188 Res.first = DestReg;
6189 Res.second = Res.second = X86::GR64RegisterClass;
6190 }
6191 }
6192
6193 return Res;
6194}