blob: b5e91ce71449493715d5d7caf5233989439d379d [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 Gohman8450d862008-02-18 19:34:53 +0000172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
176 //
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000194 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);
Dan Gohman5a199552007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
211 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
212 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000219 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000222 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
223 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000225 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
226 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000228 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
229 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 if (Subtarget->is64Bit()) {
231 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000232 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
233 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 }
235
236 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
237 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
238
239 // These should be promoted to a larger select which is supported.
240 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
241 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
242 // X86 wants to expand cmov itself.
243 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
244 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
245 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000247 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
251 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000253 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 if (Subtarget->is64Bit()) {
255 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
256 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
257 }
258 // X86 ret instruction may pop stack.
259 setOperationAction(ISD::RET , MVT::Other, Custom);
260 if (!Subtarget->is64Bit())
261 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
262
263 // Darwin ABI issue.
264 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
265 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
266 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
268 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
269 if (Subtarget->is64Bit()) {
270 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
271 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
272 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
273 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
274 }
275 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
276 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
277 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
278 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
279 // X86 wants to expand memset / memcpy itself.
280 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
281 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
282
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000283 if (!Subtarget->hasSSE2())
284 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
285
286
Evan Cheng2e28d622008-02-02 04:07:54 +0000287 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 // FIXME - use subtarget debug flags
290 if (!Subtarget->isTargetDarwin() &&
291 !Subtarget->isTargetELF() &&
292 !Subtarget->isTargetCygMing())
293 setOperationAction(ISD::LABEL, MVT::Other, Expand);
294
295 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
296 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
297 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
298 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
299 if (Subtarget->is64Bit()) {
300 // FIXME: Verify
301 setExceptionPointerRegister(X86::RAX);
302 setExceptionSelectorRegister(X86::RDX);
303 } else {
304 setExceptionPointerRegister(X86::EAX);
305 setExceptionSelectorRegister(X86::EDX);
306 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000307 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308
Duncan Sands7407a9f2007-09-11 14:10:23 +0000309 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000310
Chris Lattner56b941f2008-01-15 21:58:22 +0000311 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000312
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
314 setOperationAction(ISD::VASTART , MVT::Other, Custom);
315 setOperationAction(ISD::VAARG , MVT::Other, Expand);
316 setOperationAction(ISD::VAEND , MVT::Other, Expand);
317 if (Subtarget->is64Bit())
318 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
319 else
320 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
321
322 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
323 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
324 if (Subtarget->is64Bit())
325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
326 if (Subtarget->isTargetCygMing())
327 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
328 else
329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
330
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000331 if (X86ScalarSSEf64) {
332 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 // Set up the FP register classes.
334 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
335 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
336
337 // Use ANDPD to simulate FABS.
338 setOperationAction(ISD::FABS , MVT::f64, Custom);
339 setOperationAction(ISD::FABS , MVT::f32, Custom);
340
341 // Use XORP to simulate FNEG.
342 setOperationAction(ISD::FNEG , MVT::f64, Custom);
343 setOperationAction(ISD::FNEG , MVT::f32, Custom);
344
345 // Use ANDPD and ORPD to simulate FCOPYSIGN.
346 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
347 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
348
349 // We don't support sin/cos/fmod
350 setOperationAction(ISD::FSIN , MVT::f64, Expand);
351 setOperationAction(ISD::FCOS , MVT::f64, Expand);
352 setOperationAction(ISD::FREM , MVT::f64, Expand);
353 setOperationAction(ISD::FSIN , MVT::f32, Expand);
354 setOperationAction(ISD::FCOS , MVT::f32, Expand);
355 setOperationAction(ISD::FREM , MVT::f32, Expand);
356
357 // Expand FP immediates into loads from the stack, except for the special
358 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000359 addLegalFPImmediate(APFloat(+0.0)); // xorpd
360 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000361
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000362 // Floating truncations from f80 and extensions to f80 go through memory.
363 // If optimizing, we lie about this though and handle it in
364 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
365 if (Fast) {
366 setConvertAction(MVT::f32, MVT::f80, Expand);
367 setConvertAction(MVT::f64, MVT::f80, Expand);
368 setConvertAction(MVT::f80, MVT::f32, Expand);
369 setConvertAction(MVT::f80, MVT::f64, Expand);
370 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000371 } else if (X86ScalarSSEf32) {
372 // Use SSE for f32, x87 for f64.
373 // Set up the FP register classes.
374 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
375 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
376
377 // Use ANDPS to simulate FABS.
378 setOperationAction(ISD::FABS , MVT::f32, Custom);
379
380 // Use XORP to simulate FNEG.
381 setOperationAction(ISD::FNEG , MVT::f32, Custom);
382
383 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
384
385 // Use ANDPS and ORPS to simulate FCOPYSIGN.
386 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
387 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
388
389 // We don't support sin/cos/fmod
390 setOperationAction(ISD::FSIN , MVT::f32, Expand);
391 setOperationAction(ISD::FCOS , MVT::f32, Expand);
392 setOperationAction(ISD::FREM , MVT::f32, Expand);
393
Nate Begemane2ba64f2008-02-14 08:57:00 +0000394 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000395 addLegalFPImmediate(APFloat(+0.0f)); // xorps
396 addLegalFPImmediate(APFloat(+0.0)); // FLD0
397 addLegalFPImmediate(APFloat(+1.0)); // FLD1
398 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
399 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
400
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000401 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
402 // this though and handle it in InstructionSelectPreprocess so that
403 // dagcombine2 can hack on these.
404 if (Fast) {
405 setConvertAction(MVT::f32, MVT::f64, Expand);
406 setConvertAction(MVT::f32, MVT::f80, Expand);
407 setConvertAction(MVT::f80, MVT::f32, Expand);
408 setConvertAction(MVT::f64, MVT::f32, Expand);
409 // And x87->x87 truncations also.
410 setConvertAction(MVT::f80, MVT::f64, Expand);
411 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000412
413 if (!UnsafeFPMath) {
414 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
415 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
416 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000418 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 // Set up the FP register classes.
420 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
421 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
422
423 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
424 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
425 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
426 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000427
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000428 // Floating truncations go through memory. If optimizing, we lie about
429 // this though and handle it in InstructionSelectPreprocess so that
430 // dagcombine2 can hack on these.
431 if (Fast) {
432 setConvertAction(MVT::f80, MVT::f32, Expand);
433 setConvertAction(MVT::f64, MVT::f32, Expand);
434 setConvertAction(MVT::f80, MVT::f64, Expand);
435 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436
437 if (!UnsafeFPMath) {
438 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
439 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
440 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000441 addLegalFPImmediate(APFloat(+0.0)); // FLD0
442 addLegalFPImmediate(APFloat(+1.0)); // FLD1
443 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
444 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000445 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
446 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
447 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
448 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 }
450
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000451 // Long double always uses X87.
452 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000453 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
454 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000455 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000456 APFloat TmpFlt(+0.0);
457 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
458 addLegalFPImmediate(TmpFlt); // FLD0
459 TmpFlt.changeSign();
460 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
461 APFloat TmpFlt2(+1.0);
462 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
463 addLegalFPImmediate(TmpFlt2); // FLD1
464 TmpFlt2.changeSign();
465 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
466 }
467
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000468 if (!UnsafeFPMath) {
469 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
470 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
471 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000472
Dan Gohman2f7b1982007-10-11 23:21:31 +0000473 // Always use a library call for pow.
474 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
475 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
476 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
477
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 // First set operation action for all vector types to expand. Then we
479 // will selectively turn on ones that can be effectively codegen'd.
480 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
481 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
482 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
485 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
486 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000505 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000509 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000510 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000513 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 }
520
521 if (Subtarget->hasMMX()) {
522 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
523 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
524 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
525 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
526
527 // FIXME: add MMX packed arithmetics
528
529 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
530 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
531 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
532 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
533
534 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
535 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
536 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000537 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538
539 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
540 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
541
542 setOperationAction(ISD::AND, MVT::v8i8, Promote);
543 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
544 setOperationAction(ISD::AND, MVT::v4i16, Promote);
545 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
546 setOperationAction(ISD::AND, MVT::v2i32, Promote);
547 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
548 setOperationAction(ISD::AND, MVT::v1i64, Legal);
549
550 setOperationAction(ISD::OR, MVT::v8i8, Promote);
551 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
552 setOperationAction(ISD::OR, MVT::v4i16, Promote);
553 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
554 setOperationAction(ISD::OR, MVT::v2i32, Promote);
555 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
556 setOperationAction(ISD::OR, MVT::v1i64, Legal);
557
558 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
559 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
560 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
561 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
562 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
563 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
564 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
565
566 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
567 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
568 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
569 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
570 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
571 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
572 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
573
574 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
575 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
576 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
577 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
578
579 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
580 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
581 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
582 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
583
584 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
585 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
587 }
588
589 if (Subtarget->hasSSE1()) {
590 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
591
592 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
593 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
594 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
595 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
596 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
597 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
599 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
600 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
601 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
602 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
603 }
604
605 if (Subtarget->hasSSE2()) {
606 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
607 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
608 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
609 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
610 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
611
612 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
613 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
614 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
615 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
616 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
617 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
618 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
619 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
620 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
621 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
622 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
623 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
624 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
625 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
626 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627
628 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
630 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
631 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
633
634 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
635 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000636 // Do not attempt to custom lower non-power-of-2 vectors
637 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
638 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
640 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
641 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
642 }
643 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
644 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
646 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000647 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000649 if (Subtarget->is64Bit()) {
650 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000651 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000652 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653
654 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
655 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
656 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
657 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
658 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
659 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
660 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
661 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
662 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
663 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
664 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
665 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
666 }
667
Chris Lattner3bc08502008-01-17 19:59:44 +0000668 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000669
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 // Custom lower v2i64 and v2f64 selects.
671 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
672 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
673 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
674 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
675 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000676
677 if (Subtarget->hasSSE41()) {
678 // FIXME: Do we need to handle scalar-to-vector here?
679 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
680
681 // i8 and i16 vectors are custom , because the source register and source
682 // source memory operand types are not the same width. f32 vectors are
683 // custom since the immediate controlling the insert encodes additional
684 // information.
685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
686 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
687 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
689
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
692 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
694
695 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
697 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000698 }
699 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700
701 // We want to custom lower some of our intrinsics.
702 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
703
704 // We have target-specific dag combine patterns for the following nodes:
705 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
706 setTargetDAGCombine(ISD::SELECT);
707
708 computeRegisterProperties();
709
710 // FIXME: These should be based on subtarget info. Plus, the values should
711 // be smaller when we are in optimizing for size mode.
712 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
713 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
714 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
715 allowUnalignedMemoryAccesses = true; // x86 supports it!
716}
717
Evan Cheng5a67b812008-01-23 23:17:41 +0000718/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
719/// the desired ByVal argument alignment.
720static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
721 if (MaxAlign == 16)
722 return;
723 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
724 if (VTy->getBitWidth() == 128)
725 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000726 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
727 unsigned EltAlign = 0;
728 getMaxByValAlign(ATy->getElementType(), EltAlign);
729 if (EltAlign > MaxAlign)
730 MaxAlign = EltAlign;
731 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
732 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
733 unsigned EltAlign = 0;
734 getMaxByValAlign(STy->getElementType(i), EltAlign);
735 if (EltAlign > MaxAlign)
736 MaxAlign = EltAlign;
737 if (MaxAlign == 16)
738 break;
739 }
740 }
741 return;
742}
743
744/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
745/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000746/// that contain SSE vectors are placed at 16-byte boundaries while the rest
747/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000748unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
749 if (Subtarget->is64Bit())
750 return getTargetData()->getABITypeAlignment(Ty);
751 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000752 if (Subtarget->hasSSE1())
753 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000754 return Align;
755}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756
Evan Cheng6fb06762007-11-09 01:32:10 +0000757/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
758/// jumptable.
759SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
760 SelectionDAG &DAG) const {
761 if (usesGlobalOffsetTable())
762 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
763 if (!Subtarget->isPICStyleRIPRel())
764 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
765 return Table;
766}
767
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768//===----------------------------------------------------------------------===//
769// Return Value Calling Convention Implementation
770//===----------------------------------------------------------------------===//
771
772#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000773
774/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
775/// exists skip possible ISD:TokenFactor.
776static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000777 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000778 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000779 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000780 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000781 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000782 return Chain.getOperand(0);
783 }
784 return Chain;
785}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000786
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787/// LowerRET - Lower an ISD::RET node.
788SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
789 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
790
791 SmallVector<CCValAssign, 16> RVLocs;
792 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
793 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
794 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
795 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000796
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 // If this is the first return lowered for this function, add the regs to the
798 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000799 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 for (unsigned i = 0; i != RVLocs.size(); ++i)
801 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000802 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000806 // Handle tail call return.
807 Chain = GetPossiblePreceedingTailCall(Chain);
808 if (Chain.getOpcode() == X86ISD::TAILCALL) {
809 SDOperand TailCall = Chain;
810 SDOperand TargetAddress = TailCall.getOperand(1);
811 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000812 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000813 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
814 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
815 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
816 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
817 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000818 assert(StackAdjustment.getOpcode() == ISD::Constant &&
819 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000820
821 SmallVector<SDOperand,8> Operands;
822 Operands.push_back(Chain.getOperand(0));
823 Operands.push_back(TargetAddress);
824 Operands.push_back(StackAdjustment);
825 // Copy registers used by the call. Last operand is a flag so it is not
826 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000827 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000828 Operands.push_back(Chain.getOperand(i));
829 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000830 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
831 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000832 }
833
834 // Regular return.
835 SDOperand Flag;
836
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 // Copy the result values into the output registers.
838 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
839 RVLocs[0].getLocReg() != X86::ST0) {
840 for (unsigned i = 0; i != RVLocs.size(); ++i) {
841 CCValAssign &VA = RVLocs[i];
842 assert(VA.isRegLoc() && "Can only return in registers!");
843 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
844 Flag);
845 Flag = Chain.getValue(1);
846 }
847 } else {
848 // We need to handle a destination of ST0 specially, because it isn't really
849 // a register.
850 SDOperand Value = Op.getOperand(1);
851
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000852 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
853 // This will get legalized into a load/store if it can't get optimized away.
854 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
855 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856
857 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
858 SDOperand Ops[] = { Chain, Value };
859 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
860 Flag = Chain.getValue(1);
861 }
862
863 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
864 if (Flag.Val)
865 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
866 else
867 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
868}
869
870
871/// LowerCallResult - Lower the result values of an ISD::CALL into the
872/// appropriate copies out of appropriate physical registers. This assumes that
873/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
874/// being lowered. The returns a SDNode with the same number of values as the
875/// ISD::CALL.
876SDNode *X86TargetLowering::
877LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
878 unsigned CallingConv, SelectionDAG &DAG) {
879
880 // Assign locations to each value returned by this call.
881 SmallVector<CCValAssign, 16> RVLocs;
882 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
883 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
884 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
885
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 SmallVector<SDOperand, 8> ResultVals;
887
888 // Copy all of the result registers out of their specified physreg.
889 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
890 for (unsigned i = 0; i != RVLocs.size(); ++i) {
891 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
892 RVLocs[i].getValVT(), InFlag).getValue(1);
893 InFlag = Chain.getValue(2);
894 ResultVals.push_back(Chain.getValue(0));
895 }
896 } else {
897 // Copies from the FP stack are special, as ST0 isn't a valid register
898 // before the fp stackifier runs.
899
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000900 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
901 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
902 // the specified value type.
903 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
904 if (isScalarFPTypeInSSEReg(GetResultTy))
905 GetResultTy = MVT::f80;
906 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
907
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 SDOperand GROps[] = { Chain, InFlag };
909 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
910 Chain = RetVal.getValue(1);
911 InFlag = RetVal.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000912
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000913 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
914 // there.
915 if (GetResultTy != RVLocs[0].getValVT())
916 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
917 // This truncation won't change the value.
918 DAG.getIntPtrConstant(1));
919
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 ResultVals.push_back(RetVal);
921 }
922
923 // Merge everything together with a MERGE_VALUES node.
924 ResultVals.push_back(Chain);
925 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
926 &ResultVals[0], ResultVals.size()).Val;
927}
928
Evan Cheng931a8f42008-01-29 19:34:22 +0000929/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
930/// ISD::CALL where the results are known to be in two 64-bit registers,
931/// e.g. XMM0 and XMM1. This simplify store the two values back to the
932/// fixed stack slot allocated for StructRet.
933SDNode *X86TargetLowering::
934LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
935 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
936 MVT::ValueType VT, SelectionDAG &DAG) {
937 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
938 Chain = RetVal1.getValue(1);
939 InFlag = RetVal1.getValue(2);
940 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
941 Chain = RetVal2.getValue(1);
942 InFlag = RetVal2.getValue(2);
943 SDOperand FIN = TheCall->getOperand(5);
944 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
945 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
946 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
947 return Chain.Val;
948}
949
950/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
951/// where the results are known to be in ST0 and ST1.
952SDNode *X86TargetLowering::
953LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
954 SDNode *TheCall, SelectionDAG &DAG) {
955 SmallVector<SDOperand, 8> ResultVals;
956 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
957 SDVTList Tys = DAG.getVTList(VTs, 4);
958 SDOperand Ops[] = { Chain, InFlag };
959 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
960 Chain = RetVal.getValue(2);
961 SDOperand FIN = TheCall->getOperand(5);
962 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
963 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
964 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
965 return Chain.Val;
966}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967
968//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000969// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970//===----------------------------------------------------------------------===//
971// StdCall calling convention seems to be standard for many Windows' API
972// routines and around. It differs from C calling convention just a little:
973// callee should clean up the stack, not caller. Symbols should be also
974// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000975// For info on fast calling convention see Fast Calling Convention (tail call)
976// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977
978/// AddLiveIn - This helper function adds the specified physical register to the
979/// MachineFunction as a live in value. It also creates a corresponding virtual
980/// register for it.
981static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
982 const TargetRegisterClass *RC) {
983 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000984 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
985 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 return VReg;
987}
988
Gordon Henriksen18ace102008-01-05 16:56:59 +0000989// Determines whether a CALL node uses struct return semantics.
990static bool CallIsStructReturn(SDOperand Op) {
991 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
992 if (!NumOps)
993 return false;
994
995 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
996 return Flags->getValue() & ISD::ParamFlags::StructReturn;
997}
998
999// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
1000static bool ArgsAreStructReturn(SDOperand Op) {
1001 unsigned NumArgs = Op.Val->getNumValues() - 1;
1002 if (!NumArgs)
1003 return false;
1004
1005 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1006 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1007}
1008
1009// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
1010// its own arguments. Callee pop is necessary to support tail calls.
1011bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1012 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1013 if (IsVarArg)
1014 return false;
1015
1016 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1017 default:
1018 return false;
1019 case CallingConv::X86_StdCall:
1020 return !Subtarget->is64Bit();
1021 case CallingConv::X86_FastCall:
1022 return !Subtarget->is64Bit();
1023 case CallingConv::Fast:
1024 return PerformTailCallOpt;
1025 }
1026}
1027
1028// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
1029CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1030 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1031
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001032 if (Subtarget->is64Bit()) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001033 if (CC == CallingConv::Fast && PerformTailCallOpt)
1034 return CC_X86_64_TailCall;
1035 else
1036 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001037 }
1038
Gordon Henriksen18ace102008-01-05 16:56:59 +00001039 if (CC == CallingConv::X86_FastCall)
1040 return CC_X86_32_FastCall;
1041 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1042 return CC_X86_32_TailCall;
1043 else
1044 return CC_X86_32_C;
1045}
1046
1047// Selects the appropriate decoration to apply to a MachineFunction containing a
1048// given FORMAL_ARGUMENTS node.
1049NameDecorationStyle
1050X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1051 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1052 if (CC == CallingConv::X86_FastCall)
1053 return FastCall;
1054 else if (CC == CallingConv::X86_StdCall)
1055 return StdCall;
1056 return None;
1057}
1058
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001059
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001060// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly
1061// be overwritten when lowering the outgoing arguments in a tail call. Currently
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001062// the implementation of this call is very conservative and assumes all
1063// arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001064// registers would be overwritten by direct lowering.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001065// Possible improvement:
1066// Check FORMAL_ARGUMENTS corresponding MERGE_VALUES for CopyFromReg nodes
1067// indicating inreg passed arguments which also need not be lowered to a safe
1068// stack slot.
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001069static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001070 RegisterSDNode * OpReg = NULL;
1071 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1072 (Op.getOpcode()== ISD::CopyFromReg &&
1073 (OpReg = cast<RegisterSDNode>(Op.getOperand(1))) &&
Dan Gohman1e57df32008-02-10 18:45:23 +00001074 OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001075 return true;
1076 return false;
1077}
1078
Evan Cheng5817a0e2008-01-12 01:08:07 +00001079// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1080// by "Src" to address "Dst" with size and alignment information specified by
1081// the specific parameter attribute. The copy will be passed as a byval function
1082// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001083static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001084CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1085 unsigned Flags, SelectionDAG &DAG) {
1086 unsigned Align = 1 <<
1087 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1088 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001089 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001090 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1091 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001092 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001093 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001094}
1095
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001096SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1097 const CCValAssign &VA,
1098 MachineFrameInfo *MFI,
1099 SDOperand Root, unsigned i) {
1100 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001101 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1102 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1103
1104 // FIXME: For now, all byval parameter objects are marked mutable. This
1105 // can be changed with more analysis.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001106 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Evan Cheng3e42a522008-01-10 02:24:25 +00001107 VA.getLocMemOffset(), !isByVal);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001108 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001109 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001110 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001111 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001112 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001113}
1114
Gordon Henriksen18ace102008-01-05 16:56:59 +00001115SDOperand
1116X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001118 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1119
1120 const Function* Fn = MF.getFunction();
1121 if (Fn->hasExternalLinkage() &&
1122 Subtarget->isTargetCygMing() &&
1123 Fn->getName() == "main")
1124 FuncInfo->setForceFramePointer(true);
1125
1126 // Decorate the function name.
1127 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1128
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129 MachineFrameInfo *MFI = MF.getFrameInfo();
1130 SDOperand Root = Op.getOperand(0);
1131 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001132 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001133 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001134
1135 assert(!(isVarArg && CC == CallingConv::Fast) &&
1136 "Var args not supported with calling convention fastcc");
1137
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 // Assign locations to all of the incoming arguments.
1139 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001140 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001141 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001142
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 SmallVector<SDOperand, 8> ArgValues;
1144 unsigned LastVal = ~0U;
1145 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1146 CCValAssign &VA = ArgLocs[i];
1147 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1148 // places.
1149 assert(VA.getValNo() != LastVal &&
1150 "Don't support value assigned to multiple locs yet");
1151 LastVal = VA.getValNo();
1152
1153 if (VA.isRegLoc()) {
1154 MVT::ValueType RegVT = VA.getLocVT();
1155 TargetRegisterClass *RC;
1156 if (RegVT == MVT::i32)
1157 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001158 else if (Is64Bit && RegVT == MVT::i64)
1159 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001160 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001161 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001162 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001163 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 else {
1165 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001166 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1167 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1168 RegVT = MVT::i64;
1169 } else
1170 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001171 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1174 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1175
1176 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1177 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1178 // right size.
1179 if (VA.getLocInfo() == CCValAssign::SExt)
1180 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1181 DAG.getValueType(VA.getValVT()));
1182 else if (VA.getLocInfo() == CCValAssign::ZExt)
1183 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1184 DAG.getValueType(VA.getValVT()));
1185
1186 if (VA.getLocInfo() != CCValAssign::Full)
1187 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1188
Gordon Henriksen18ace102008-01-05 16:56:59 +00001189 // Handle MMX values passed in GPRs.
1190 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1191 MVT::getSizeInBits(RegVT) == 64)
1192 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001194 ArgValues.push_back(ArgValue);
1195 } else {
1196 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001197 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 }
1199 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001200
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001201 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001202 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001203 if (CC == CallingConv::Fast)
1204 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001205
1206 // If the function takes variable number of arguments, make a frame index for
1207 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001208 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001209 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1210 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1211 }
1212 if (Is64Bit) {
1213 static const unsigned GPR64ArgRegs[] = {
1214 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1215 };
1216 static const unsigned XMMArgRegs[] = {
1217 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1218 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1219 };
1220
1221 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1222 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1223
1224 // For X86-64, if there are vararg parameters that are passed via
1225 // registers, then we must store them to their spots on the stack so they
1226 // may be loaded by deferencing the result of va_next.
1227 VarArgsGPOffset = NumIntRegs * 8;
1228 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1229 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1230
1231 // Store the integer parameter registers.
1232 SmallVector<SDOperand, 8> MemOps;
1233 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1234 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001235 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001236 for (; NumIntRegs != 6; ++NumIntRegs) {
1237 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1238 X86::GR64RegisterClass);
1239 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001240 SDOperand Store =
1241 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001242 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001243 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001244 MemOps.push_back(Store);
1245 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001246 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001247 }
1248
1249 // Now store the XMM (fp + vector) parameter registers.
1250 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001251 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001252 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1253 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1254 X86::VR128RegisterClass);
1255 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001256 SDOperand Store =
1257 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001258 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001259 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001260 MemOps.push_back(Store);
1261 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001262 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001263 }
1264 if (!MemOps.empty())
1265 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1266 &MemOps[0], MemOps.size());
1267 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001268 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001269
1270 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1271 // arguments and the arguments after the retaddr has been pushed are
1272 // aligned.
1273 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1274 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1275 (StackSize & 7) == 0)
1276 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001278 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001279
Gordon Henriksen18ace102008-01-05 16:56:59 +00001280 // Some CCs need callee pop.
1281 if (IsCalleePop(Op)) {
1282 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 BytesCallerReserves = 0;
1284 } else {
1285 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001287 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001289 BytesCallerReserves = StackSize;
1290 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001291
Gordon Henriksen18ace102008-01-05 16:56:59 +00001292 if (!Is64Bit) {
1293 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1294 if (CC == CallingConv::X86_FastCall)
1295 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1296 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001297
Anton Korobeynikove844e472007-08-15 17:12:32 +00001298 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299
1300 // Return the new list of results.
1301 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1302 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1303}
1304
Evan Chengbc077bf2008-01-10 00:09:10 +00001305SDOperand
1306X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1307 const SDOperand &StackPtr,
1308 const CCValAssign &VA,
1309 SDOperand Chain,
1310 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001311 unsigned LocMemOffset = VA.getLocMemOffset();
1312 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001313 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1314 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1315 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1316 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001317 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001318 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001319 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001320 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001321}
1322
Evan Cheng931a8f42008-01-29 19:34:22 +00001323/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1324/// struct return call to the specified function. X86-64 ABI specifies
1325/// some SRet calls are actually returned in registers. Since current
1326/// LLVM cannot represent multi-value calls, they are represent as
1327/// calls where the results are passed in a hidden struct provided by
1328/// the caller. This function examines the type of the struct to
1329/// determine the correct way to implement the call.
1330X86::X86_64SRet
1331X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1332 // FIXME: Disabled for now.
1333 return X86::InMemory;
1334
1335 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1336 const Type *RTy = PTy->getElementType();
1337 unsigned Size = getTargetData()->getABITypeSize(RTy);
1338 if (Size != 16 && Size != 32)
1339 return X86::InMemory;
1340
1341 if (Size == 32) {
1342 const StructType *STy = dyn_cast<StructType>(RTy);
1343 if (!STy) return X86::InMemory;
1344 if (STy->getNumElements() == 2 &&
1345 STy->getElementType(0) == Type::X86_FP80Ty &&
1346 STy->getElementType(1) == Type::X86_FP80Ty)
1347 return X86::InX87;
1348 }
1349
1350 bool AllFP = true;
1351 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1352 I != E; ++I) {
1353 const Type *STy = I->get();
1354 if (!STy->isFPOrFPVector()) {
1355 AllFP = false;
1356 break;
1357 }
1358 }
1359
1360 if (AllFP)
1361 return X86::InSSE;
1362 return X86::InGPR64;
1363}
1364
1365void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1366 CCAssignFn *Fn,
1367 CCState &CCInfo) {
1368 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1369 for (unsigned i = 1; i != NumOps; ++i) {
1370 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1371 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1372 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1373 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1374 cerr << "Call operand #" << i << " has unhandled type "
1375 << MVT::getValueTypeString(ArgVT) << "\n";
1376 abort();
1377 }
1378 }
1379}
1380
Gordon Henriksen18ace102008-01-05 16:56:59 +00001381SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1382 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001384 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1387 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001390 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001391
1392 assert(!(isVarArg && CC == CallingConv::Fast) &&
1393 "Var args not supported with calling convention fastcc");
1394
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 // Analyze operands of the call, assigning locations to each operand.
1396 SmallVector<CCValAssign, 16> ArgLocs;
1397 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001398 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1399
1400 X86::X86_64SRet SRetMethod = X86::InMemory;
1401 if (Is64Bit && IsStructRet)
1402 // FIXME: We can't figure out type of the sret structure for indirect
1403 // calls. We need to copy more information from CallSite to the ISD::CALL
1404 // node.
1405 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1406 SRetMethod =
1407 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1408
1409 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1410 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1411 // a sret call.
1412 if (SRetMethod != X86::InMemory)
1413 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1414 else
1415 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416
1417 // Get a count of how many bytes are to be pushed on the stack.
1418 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001419 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001420 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001421
Gordon Henriksen18ace102008-01-05 16:56:59 +00001422 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1423 // arguments and the arguments after the retaddr has been pushed are aligned.
1424 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1425 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1426 (NumBytes & 7) == 0)
1427 NumBytes += 4;
1428
1429 int FPDiff = 0;
1430 if (IsTailCall) {
1431 // Lower arguments at fp - stackoffset + fpdiff.
1432 unsigned NumBytesCallerPushed =
1433 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1434 FPDiff = NumBytesCallerPushed - NumBytes;
1435
1436 // Set the delta of movement of the returnaddr stackslot.
1437 // But only set if delta is greater than previous delta.
1438 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1439 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1440 }
1441
Chris Lattner5872a362008-01-17 07:00:52 +00001442 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443
Gordon Henriksen18ace102008-01-05 16:56:59 +00001444 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1445 if (IsTailCall) {
1446 // Adjust the Return address stack slot.
1447 if (FPDiff) {
1448 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1449 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1450 // Load the "old" Return address.
1451 RetAddrFrIdx =
1452 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1453 // Calculate the new stack slot for the return address.
1454 int SlotSize = Is64Bit ? 8 : 4;
1455 int NewReturnAddrFI =
1456 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1457 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1458 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1459 }
1460 }
1461
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1463 SmallVector<SDOperand, 8> MemOpChains;
1464
1465 SDOperand StackPtr;
1466
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001467 // Walk the register/memloc assignments, inserting copies/loads. For tail
1468 // calls, lower arguments which could otherwise be possibly overwritten to the
1469 // stack slot where they would go on normal function calls.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1471 CCValAssign &VA = ArgLocs[i];
1472 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1473
1474 // Promote the value if needed.
1475 switch (VA.getLocInfo()) {
1476 default: assert(0 && "Unknown loc info!");
1477 case CCValAssign::Full: break;
1478 case CCValAssign::SExt:
1479 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1480 break;
1481 case CCValAssign::ZExt:
1482 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1483 break;
1484 case CCValAssign::AExt:
1485 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1486 break;
1487 }
1488
1489 if (VA.isRegLoc()) {
1490 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1491 } else {
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001492 if (!IsTailCall || IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001493 assert(VA.isMemLoc());
1494 if (StackPtr.Val == 0)
1495 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1496
1497 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1498 Arg));
1499 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500 }
1501 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502
1503 if (!MemOpChains.empty())
1504 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1505 &MemOpChains[0], MemOpChains.size());
1506
1507 // Build a sequence of copy-to-reg nodes chained together with token chain
1508 // and flag operands which copy the outgoing args into registers.
1509 SDOperand InFlag;
1510 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1511 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1512 InFlag);
1513 InFlag = Chain.getValue(1);
1514 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001515
1516 if (IsTailCall)
1517 InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output?
1518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1520 // GOT pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001521 // Does not work with tail call since ebx is not restored correctly by
1522 // tailcaller. TODO: at least for x86 - verify for x86-64
1523 if (!IsTailCall && !Is64Bit &&
1524 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525 Subtarget->isPICStyleGOT()) {
1526 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1527 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1528 InFlag);
1529 InFlag = Chain.getValue(1);
1530 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001531
Gordon Henriksen18ace102008-01-05 16:56:59 +00001532 if (Is64Bit && isVarArg) {
1533 // From AMD64 ABI document:
1534 // For calls that may call functions that use varargs or stdargs
1535 // (prototype-less calls or calls to functions containing ellipsis (...) in
1536 // the declaration) %al is used as hidden argument to specify the number
1537 // of SSE registers used. The contents of %al do not need to match exactly
1538 // the number of registers, but must be an ubound on the number of SSE
1539 // registers used and is in the range 0 - 8 inclusive.
1540
1541 // Count the number of XMM registers allocated.
1542 static const unsigned XMMArgRegs[] = {
1543 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1544 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1545 };
1546 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1547
1548 Chain = DAG.getCopyToReg(Chain, X86::AL,
1549 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1550 InFlag = Chain.getValue(1);
1551 }
1552
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001553 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001554 if (IsTailCall) {
1555 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001556 SDOperand FIN;
1557 int FI = 0;
1558 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1559 CCValAssign &VA = ArgLocs[i];
1560 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001561 assert(VA.isMemLoc());
1562 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001563 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1564 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001565 // Create frame index.
1566 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1567 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1568 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1569 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001570 SDOperand Source = Arg;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001571 if (IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001572 // Copy from stack slots to stack slot of a tail called function. This
1573 // needs to be done because if we would lower the arguments directly
1574 // to their real stack slot we might end up overwriting each other.
1575 // Get source stack slot.
Chris Lattner5872a362008-01-17 07:00:52 +00001576 Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001577 if (StackPtr.Val == 0)
1578 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1579 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1580 if ((Flags & ISD::ParamFlags::ByVal)==0)
Duncan Sands22981632008-01-13 21:20:29 +00001581 Source = DAG.getLoad(VA.getValVT(), Chain, Source, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001582 }
1583
Gordon Henriksen18ace102008-01-05 16:56:59 +00001584 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001585 // Copy relative to framepointer.
1586 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
1587 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001588 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001589 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001590 MemOpChains2.push_back(
1591 DAG.getStore(Chain, Source, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001592 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001593 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001594 }
1595 }
1596
1597 if (!MemOpChains2.empty())
1598 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001599 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001600
1601 // Store the return address to the appropriate stack slot.
1602 if (FPDiff)
1603 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1604 }
1605
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001606 // If the callee is a GlobalAddress node (quite common, every direct call is)
1607 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1608 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1609 // We should use extra load for direct calls to dllimported functions in
1610 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001611 if ((IsTailCall || !Is64Bit ||
1612 getTargetMachine().getCodeModel() != CodeModel::Large)
1613 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1614 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001616 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001617 if (IsTailCall || !Is64Bit ||
1618 getTargetMachine().getCodeModel() != CodeModel::Large)
1619 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1620 } else if (IsTailCall) {
1621 assert(Callee.getOpcode() == ISD::LOAD &&
1622 "Function destination must be loaded into virtual register");
1623 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1624
1625 Chain = DAG.getCopyToReg(Chain,
1626 DAG.getRegister(Opc, getPointerTy()) ,
1627 Callee,InFlag);
1628 Callee = DAG.getRegister(Opc, getPointerTy());
1629 // Add register as live out.
1630 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001631 }
1632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633 // Returns a chain & a flag for retval copy to use.
1634 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1635 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001636
1637 if (IsTailCall) {
1638 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001639 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1640 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001641 if (InFlag.Val)
1642 Ops.push_back(InFlag);
1643 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1644 InFlag = Chain.getValue(1);
1645
1646 // Returns a chain & a flag for retval copy to use.
1647 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1648 Ops.clear();
1649 }
1650
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001651 Ops.push_back(Chain);
1652 Ops.push_back(Callee);
1653
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 if (IsTailCall)
1655 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001656
1657 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001658 if (!IsTailCall && !Is64Bit &&
1659 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001660 Subtarget->isPICStyleGOT())
1661 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001662
Gordon Henriksen18ace102008-01-05 16:56:59 +00001663 // Add argument registers to the end of the list so that they are known live
1664 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001665 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1666 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1667 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001669 if (InFlag.Val)
1670 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001671
Gordon Henriksen18ace102008-01-05 16:56:59 +00001672 if (IsTailCall) {
1673 assert(InFlag.Val &&
1674 "Flag must be set. Depend on flag being set in LowerRET");
1675 Chain = DAG.getNode(X86ISD::TAILCALL,
1676 Op.Val->getVTList(), &Ops[0], Ops.size());
1677
1678 return SDOperand(Chain.Val, Op.ResNo);
1679 }
1680
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001681 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 InFlag = Chain.getValue(1);
1683
1684 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001685 unsigned NumBytesForCalleeToPush;
1686 if (IsCalleePop(Op))
1687 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001688 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001689 // If this is is a call to a struct-return function, the callee
1690 // pops the hidden struct pointer, so we have to push it back.
1691 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001692 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001694 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001696 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001697 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001698 DAG.getIntPtrConstant(NumBytes),
1699 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001700 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001701 InFlag = Chain.getValue(1);
1702
1703 // Handle result values, copying them out of physregs into vregs that we
1704 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001705 switch (SRetMethod) {
1706 default:
1707 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1708 case X86::InGPR64:
1709 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1710 X86::RAX, X86::RDX,
1711 MVT::i64, DAG), Op.ResNo);
1712 case X86::InSSE:
1713 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1714 X86::XMM0, X86::XMM1,
1715 MVT::f64, DAG), Op.ResNo);
1716 case X86::InX87:
1717 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1718 Op.ResNo);
1719 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720}
1721
1722
1723//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001724// Fast Calling Convention (tail call) implementation
1725//===----------------------------------------------------------------------===//
1726
1727// Like std call, callee cleans arguments, convention except that ECX is
1728// reserved for storing the tail called function address. Only 2 registers are
1729// free for argument passing (inreg). Tail call optimization is performed
1730// provided:
1731// * tailcallopt is enabled
1732// * caller/callee are fastcc
1733// * elf/pic is disabled OR
1734// * elf/pic enabled + callee is in module + callee has
1735// visibility protected or hidden
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001736// To keep the stack aligned according to platform abi the function
1737// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1738// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001739// If a tail called function callee has more arguments than the caller the
1740// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001741// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001742// original REtADDR, but before the saved framepointer or the spilled registers
1743// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1744// stack layout:
1745// arg1
1746// arg2
1747// RETADDR
1748// [ new RETADDR
1749// move area ]
1750// (possible EBP)
1751// ESI
1752// EDI
1753// local1 ..
1754
1755/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1756/// for a 16 byte align requirement.
1757unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1758 SelectionDAG& DAG) {
1759 if (PerformTailCallOpt) {
1760 MachineFunction &MF = DAG.getMachineFunction();
1761 const TargetMachine &TM = MF.getTarget();
1762 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1763 unsigned StackAlignment = TFI.getStackAlignment();
1764 uint64_t AlignMask = StackAlignment - 1;
1765 int64_t Offset = StackSize;
1766 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1767 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1768 // Number smaller than 12 so just add the difference.
1769 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1770 } else {
1771 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1772 Offset = ((~AlignMask) & Offset) + StackAlignment +
1773 (StackAlignment-SlotSize);
1774 }
1775 StackSize = Offset;
1776 }
1777 return StackSize;
1778}
1779
1780/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001781/// following the call is a return. A function is eligible if caller/callee
1782/// calling conventions match, currently only fastcc supports tail calls, and
1783/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001784bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1785 SDOperand Ret,
1786 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001787 if (!PerformTailCallOpt)
1788 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001789
1790 // Check whether CALL node immediatly preceeds the RET node and whether the
1791 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001792 unsigned NumOps = Ret.getNumOperands();
1793 if ((NumOps == 1 &&
1794 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1795 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001796 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001797 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1798 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001799 MachineFunction &MF = DAG.getMachineFunction();
1800 unsigned CallerCC = MF.getFunction()->getCallingConv();
1801 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1802 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1803 SDOperand Callee = Call.getOperand(4);
1804 // On elf/pic %ebx needs to be livein.
Evan Chenge7a87392007-11-02 01:26:22 +00001805 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1806 !Subtarget->isPICStyleGOT())
1807 return true;
1808
1809 // Can only do local tail calls with PIC.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001810 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1811 return G->getGlobal()->hasHiddenVisibility()
1812 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001813 }
1814 }
Evan Chenge7a87392007-11-02 01:26:22 +00001815
1816 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001817}
1818
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001819//===----------------------------------------------------------------------===//
1820// Other Lowering Hooks
1821//===----------------------------------------------------------------------===//
1822
1823
1824SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001825 MachineFunction &MF = DAG.getMachineFunction();
1826 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1827 int ReturnAddrIndex = FuncInfo->getRAIndex();
1828
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829 if (ReturnAddrIndex == 0) {
1830 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 if (Subtarget->is64Bit())
1832 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1833 else
1834 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001835
1836 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001837 }
1838
1839 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1840}
1841
1842
1843
1844/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1845/// specific condition code. It returns a false if it cannot do a direct
1846/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1847/// needed.
1848static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1849 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1850 SelectionDAG &DAG) {
1851 X86CC = X86::COND_INVALID;
1852 if (!isFP) {
1853 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1854 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1855 // X > -1 -> X == 0, jump !sign.
1856 RHS = DAG.getConstant(0, RHS.getValueType());
1857 X86CC = X86::COND_NS;
1858 return true;
1859 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1860 // X < 0 -> X == 0, jump on sign.
1861 X86CC = X86::COND_S;
1862 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001863 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1864 // X < 1 -> X <= 0
1865 RHS = DAG.getConstant(0, RHS.getValueType());
1866 X86CC = X86::COND_LE;
1867 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001868 }
1869 }
1870
1871 switch (SetCCOpcode) {
1872 default: break;
1873 case ISD::SETEQ: X86CC = X86::COND_E; break;
1874 case ISD::SETGT: X86CC = X86::COND_G; break;
1875 case ISD::SETGE: X86CC = X86::COND_GE; break;
1876 case ISD::SETLT: X86CC = X86::COND_L; break;
1877 case ISD::SETLE: X86CC = X86::COND_LE; break;
1878 case ISD::SETNE: X86CC = X86::COND_NE; break;
1879 case ISD::SETULT: X86CC = X86::COND_B; break;
1880 case ISD::SETUGT: X86CC = X86::COND_A; break;
1881 case ISD::SETULE: X86CC = X86::COND_BE; break;
1882 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1883 }
1884 } else {
1885 // On a floating point condition, the flags are set as follows:
1886 // ZF PF CF op
1887 // 0 | 0 | 0 | X > Y
1888 // 0 | 0 | 1 | X < Y
1889 // 1 | 0 | 0 | X == Y
1890 // 1 | 1 | 1 | unordered
1891 bool Flip = false;
1892 switch (SetCCOpcode) {
1893 default: break;
1894 case ISD::SETUEQ:
1895 case ISD::SETEQ: X86CC = X86::COND_E; break;
1896 case ISD::SETOLT: Flip = true; // Fallthrough
1897 case ISD::SETOGT:
1898 case ISD::SETGT: X86CC = X86::COND_A; break;
1899 case ISD::SETOLE: Flip = true; // Fallthrough
1900 case ISD::SETOGE:
1901 case ISD::SETGE: X86CC = X86::COND_AE; break;
1902 case ISD::SETUGT: Flip = true; // Fallthrough
1903 case ISD::SETULT:
1904 case ISD::SETLT: X86CC = X86::COND_B; break;
1905 case ISD::SETUGE: Flip = true; // Fallthrough
1906 case ISD::SETULE:
1907 case ISD::SETLE: X86CC = X86::COND_BE; break;
1908 case ISD::SETONE:
1909 case ISD::SETNE: X86CC = X86::COND_NE; break;
1910 case ISD::SETUO: X86CC = X86::COND_P; break;
1911 case ISD::SETO: X86CC = X86::COND_NP; break;
1912 }
1913 if (Flip)
1914 std::swap(LHS, RHS);
1915 }
1916
1917 return X86CC != X86::COND_INVALID;
1918}
1919
1920/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1921/// code. Current x86 isa includes the following FP cmov instructions:
1922/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1923static bool hasFPCMov(unsigned X86CC) {
1924 switch (X86CC) {
1925 default:
1926 return false;
1927 case X86::COND_B:
1928 case X86::COND_BE:
1929 case X86::COND_E:
1930 case X86::COND_P:
1931 case X86::COND_A:
1932 case X86::COND_AE:
1933 case X86::COND_NE:
1934 case X86::COND_NP:
1935 return true;
1936 }
1937}
1938
1939/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1940/// true if Op is undef or if its value falls within the specified range (L, H].
1941static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1942 if (Op.getOpcode() == ISD::UNDEF)
1943 return true;
1944
1945 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1946 return (Val >= Low && Val < Hi);
1947}
1948
1949/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1950/// true if Op is undef or if its value equal to the specified value.
1951static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1952 if (Op.getOpcode() == ISD::UNDEF)
1953 return true;
1954 return cast<ConstantSDNode>(Op)->getValue() == Val;
1955}
1956
1957/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1958/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1959bool X86::isPSHUFDMask(SDNode *N) {
1960 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1961
Dan Gohman7dc19012007-08-02 21:17:01 +00001962 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001963 return false;
1964
1965 // Check if the value doesn't reference the second vector.
1966 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1967 SDOperand Arg = N->getOperand(i);
1968 if (Arg.getOpcode() == ISD::UNDEF) continue;
1969 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00001970 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001971 return false;
1972 }
1973
1974 return true;
1975}
1976
1977/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1978/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1979bool X86::isPSHUFHWMask(SDNode *N) {
1980 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1981
1982 if (N->getNumOperands() != 8)
1983 return false;
1984
1985 // Lower quadword copied in order.
1986 for (unsigned i = 0; i != 4; ++i) {
1987 SDOperand Arg = N->getOperand(i);
1988 if (Arg.getOpcode() == ISD::UNDEF) continue;
1989 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1990 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1991 return false;
1992 }
1993
1994 // Upper quadword shuffled.
1995 for (unsigned i = 4; i != 8; ++i) {
1996 SDOperand Arg = N->getOperand(i);
1997 if (Arg.getOpcode() == ISD::UNDEF) continue;
1998 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1999 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2000 if (Val < 4 || Val > 7)
2001 return false;
2002 }
2003
2004 return true;
2005}
2006
2007/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2008/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2009bool X86::isPSHUFLWMask(SDNode *N) {
2010 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2011
2012 if (N->getNumOperands() != 8)
2013 return false;
2014
2015 // Upper quadword copied in order.
2016 for (unsigned i = 4; i != 8; ++i)
2017 if (!isUndefOrEqual(N->getOperand(i), i))
2018 return false;
2019
2020 // Lower quadword shuffled.
2021 for (unsigned i = 0; i != 4; ++i)
2022 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2023 return false;
2024
2025 return true;
2026}
2027
2028/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2029/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2030static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2031 if (NumElems != 2 && NumElems != 4) return false;
2032
2033 unsigned Half = NumElems / 2;
2034 for (unsigned i = 0; i < Half; ++i)
2035 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2036 return false;
2037 for (unsigned i = Half; i < NumElems; ++i)
2038 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2039 return false;
2040
2041 return true;
2042}
2043
2044bool X86::isSHUFPMask(SDNode *N) {
2045 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2046 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2047}
2048
2049/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2050/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2051/// half elements to come from vector 1 (which would equal the dest.) and
2052/// the upper half to come from vector 2.
2053static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2054 if (NumOps != 2 && NumOps != 4) return false;
2055
2056 unsigned Half = NumOps / 2;
2057 for (unsigned i = 0; i < Half; ++i)
2058 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2059 return false;
2060 for (unsigned i = Half; i < NumOps; ++i)
2061 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2062 return false;
2063 return true;
2064}
2065
2066static bool isCommutedSHUFP(SDNode *N) {
2067 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2068 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2069}
2070
2071/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2072/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2073bool X86::isMOVHLPSMask(SDNode *N) {
2074 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2075
2076 if (N->getNumOperands() != 4)
2077 return false;
2078
2079 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2080 return isUndefOrEqual(N->getOperand(0), 6) &&
2081 isUndefOrEqual(N->getOperand(1), 7) &&
2082 isUndefOrEqual(N->getOperand(2), 2) &&
2083 isUndefOrEqual(N->getOperand(3), 3);
2084}
2085
2086/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2087/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2088/// <2, 3, 2, 3>
2089bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2090 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2091
2092 if (N->getNumOperands() != 4)
2093 return false;
2094
2095 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2096 return isUndefOrEqual(N->getOperand(0), 2) &&
2097 isUndefOrEqual(N->getOperand(1), 3) &&
2098 isUndefOrEqual(N->getOperand(2), 2) &&
2099 isUndefOrEqual(N->getOperand(3), 3);
2100}
2101
2102/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2103/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2104bool X86::isMOVLPMask(SDNode *N) {
2105 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2106
2107 unsigned NumElems = N->getNumOperands();
2108 if (NumElems != 2 && NumElems != 4)
2109 return false;
2110
2111 for (unsigned i = 0; i < NumElems/2; ++i)
2112 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2113 return false;
2114
2115 for (unsigned i = NumElems/2; i < NumElems; ++i)
2116 if (!isUndefOrEqual(N->getOperand(i), i))
2117 return false;
2118
2119 return true;
2120}
2121
2122/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2123/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2124/// and MOVLHPS.
2125bool X86::isMOVHPMask(SDNode *N) {
2126 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2127
2128 unsigned NumElems = N->getNumOperands();
2129 if (NumElems != 2 && NumElems != 4)
2130 return false;
2131
2132 for (unsigned i = 0; i < NumElems/2; ++i)
2133 if (!isUndefOrEqual(N->getOperand(i), i))
2134 return false;
2135
2136 for (unsigned i = 0; i < NumElems/2; ++i) {
2137 SDOperand Arg = N->getOperand(i + NumElems/2);
2138 if (!isUndefOrEqual(Arg, i + NumElems))
2139 return false;
2140 }
2141
2142 return true;
2143}
2144
2145/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2146/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2147bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2148 bool V2IsSplat = false) {
2149 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2150 return false;
2151
2152 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2153 SDOperand BitI = Elts[i];
2154 SDOperand BitI1 = Elts[i+1];
2155 if (!isUndefOrEqual(BitI, j))
2156 return false;
2157 if (V2IsSplat) {
2158 if (isUndefOrEqual(BitI1, NumElts))
2159 return false;
2160 } else {
2161 if (!isUndefOrEqual(BitI1, j + NumElts))
2162 return false;
2163 }
2164 }
2165
2166 return true;
2167}
2168
2169bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2170 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2171 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2172}
2173
2174/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2175/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2176bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2177 bool V2IsSplat = false) {
2178 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2179 return false;
2180
2181 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2182 SDOperand BitI = Elts[i];
2183 SDOperand BitI1 = Elts[i+1];
2184 if (!isUndefOrEqual(BitI, j + NumElts/2))
2185 return false;
2186 if (V2IsSplat) {
2187 if (isUndefOrEqual(BitI1, NumElts))
2188 return false;
2189 } else {
2190 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2191 return false;
2192 }
2193 }
2194
2195 return true;
2196}
2197
2198bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2199 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2200 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2201}
2202
2203/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2204/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2205/// <0, 0, 1, 1>
2206bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2207 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2208
2209 unsigned NumElems = N->getNumOperands();
2210 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2211 return false;
2212
2213 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2214 SDOperand BitI = N->getOperand(i);
2215 SDOperand BitI1 = N->getOperand(i+1);
2216
2217 if (!isUndefOrEqual(BitI, j))
2218 return false;
2219 if (!isUndefOrEqual(BitI1, j))
2220 return false;
2221 }
2222
2223 return true;
2224}
2225
2226/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2227/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2228/// <2, 2, 3, 3>
2229bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2230 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2231
2232 unsigned NumElems = N->getNumOperands();
2233 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2234 return false;
2235
2236 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2237 SDOperand BitI = N->getOperand(i);
2238 SDOperand BitI1 = N->getOperand(i + 1);
2239
2240 if (!isUndefOrEqual(BitI, j))
2241 return false;
2242 if (!isUndefOrEqual(BitI1, j))
2243 return false;
2244 }
2245
2246 return true;
2247}
2248
2249/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2250/// specifies a shuffle of elements that is suitable for input to MOVSS,
2251/// MOVSD, and MOVD, i.e. setting the lowest element.
2252static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002253 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 return false;
2255
2256 if (!isUndefOrEqual(Elts[0], NumElts))
2257 return false;
2258
2259 for (unsigned i = 1; i < NumElts; ++i) {
2260 if (!isUndefOrEqual(Elts[i], i))
2261 return false;
2262 }
2263
2264 return true;
2265}
2266
2267bool X86::isMOVLMask(SDNode *N) {
2268 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2269 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2270}
2271
2272/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2273/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2274/// element of vector 2 and the other elements to come from vector 1 in order.
2275static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2276 bool V2IsSplat = false,
2277 bool V2IsUndef = false) {
2278 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2279 return false;
2280
2281 if (!isUndefOrEqual(Ops[0], 0))
2282 return false;
2283
2284 for (unsigned i = 1; i < NumOps; ++i) {
2285 SDOperand Arg = Ops[i];
2286 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2287 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2288 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2289 return false;
2290 }
2291
2292 return true;
2293}
2294
2295static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2296 bool V2IsUndef = false) {
2297 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2298 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2299 V2IsSplat, V2IsUndef);
2300}
2301
2302/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2303/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2304bool X86::isMOVSHDUPMask(SDNode *N) {
2305 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2306
2307 if (N->getNumOperands() != 4)
2308 return false;
2309
2310 // Expect 1, 1, 3, 3
2311 for (unsigned i = 0; i < 2; ++i) {
2312 SDOperand Arg = N->getOperand(i);
2313 if (Arg.getOpcode() == ISD::UNDEF) continue;
2314 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2315 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2316 if (Val != 1) return false;
2317 }
2318
2319 bool HasHi = false;
2320 for (unsigned i = 2; i < 4; ++i) {
2321 SDOperand Arg = N->getOperand(i);
2322 if (Arg.getOpcode() == ISD::UNDEF) continue;
2323 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2324 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2325 if (Val != 3) return false;
2326 HasHi = true;
2327 }
2328
2329 // Don't use movshdup if it can be done with a shufps.
2330 return HasHi;
2331}
2332
2333/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2334/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2335bool X86::isMOVSLDUPMask(SDNode *N) {
2336 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2337
2338 if (N->getNumOperands() != 4)
2339 return false;
2340
2341 // Expect 0, 0, 2, 2
2342 for (unsigned i = 0; i < 2; ++i) {
2343 SDOperand Arg = N->getOperand(i);
2344 if (Arg.getOpcode() == ISD::UNDEF) continue;
2345 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2346 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2347 if (Val != 0) return false;
2348 }
2349
2350 bool HasHi = false;
2351 for (unsigned i = 2; i < 4; ++i) {
2352 SDOperand Arg = N->getOperand(i);
2353 if (Arg.getOpcode() == ISD::UNDEF) continue;
2354 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2355 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2356 if (Val != 2) return false;
2357 HasHi = true;
2358 }
2359
2360 // Don't use movshdup if it can be done with a shufps.
2361 return HasHi;
2362}
2363
2364/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2365/// specifies a identity operation on the LHS or RHS.
2366static bool isIdentityMask(SDNode *N, bool RHS = false) {
2367 unsigned NumElems = N->getNumOperands();
2368 for (unsigned i = 0; i < NumElems; ++i)
2369 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2370 return false;
2371 return true;
2372}
2373
2374/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2375/// a splat of a single element.
2376static bool isSplatMask(SDNode *N) {
2377 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2378
2379 // This is a splat operation if each element of the permute is the same, and
2380 // if the value doesn't reference the second vector.
2381 unsigned NumElems = N->getNumOperands();
2382 SDOperand ElementBase;
2383 unsigned i = 0;
2384 for (; i != NumElems; ++i) {
2385 SDOperand Elt = N->getOperand(i);
2386 if (isa<ConstantSDNode>(Elt)) {
2387 ElementBase = Elt;
2388 break;
2389 }
2390 }
2391
2392 if (!ElementBase.Val)
2393 return false;
2394
2395 for (; i != NumElems; ++i) {
2396 SDOperand Arg = N->getOperand(i);
2397 if (Arg.getOpcode() == ISD::UNDEF) continue;
2398 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2399 if (Arg != ElementBase) return false;
2400 }
2401
2402 // Make sure it is a splat of the first vector operand.
2403 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2404}
2405
2406/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2407/// a splat of a single element and it's a 2 or 4 element mask.
2408bool X86::isSplatMask(SDNode *N) {
2409 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2410
2411 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2412 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2413 return false;
2414 return ::isSplatMask(N);
2415}
2416
2417/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2418/// specifies a splat of zero element.
2419bool X86::isSplatLoMask(SDNode *N) {
2420 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2421
2422 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2423 if (!isUndefOrEqual(N->getOperand(i), 0))
2424 return false;
2425 return true;
2426}
2427
2428/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2429/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2430/// instructions.
2431unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2432 unsigned NumOperands = N->getNumOperands();
2433 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2434 unsigned Mask = 0;
2435 for (unsigned i = 0; i < NumOperands; ++i) {
2436 unsigned Val = 0;
2437 SDOperand Arg = N->getOperand(NumOperands-i-1);
2438 if (Arg.getOpcode() != ISD::UNDEF)
2439 Val = cast<ConstantSDNode>(Arg)->getValue();
2440 if (Val >= NumOperands) Val -= NumOperands;
2441 Mask |= Val;
2442 if (i != NumOperands - 1)
2443 Mask <<= Shift;
2444 }
2445
2446 return Mask;
2447}
2448
2449/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2450/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2451/// instructions.
2452unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2453 unsigned Mask = 0;
2454 // 8 nodes, but we only care about the last 4.
2455 for (unsigned i = 7; i >= 4; --i) {
2456 unsigned Val = 0;
2457 SDOperand Arg = N->getOperand(i);
2458 if (Arg.getOpcode() != ISD::UNDEF)
2459 Val = cast<ConstantSDNode>(Arg)->getValue();
2460 Mask |= (Val - 4);
2461 if (i != 4)
2462 Mask <<= 2;
2463 }
2464
2465 return Mask;
2466}
2467
2468/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2469/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2470/// instructions.
2471unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2472 unsigned Mask = 0;
2473 // 8 nodes, but we only care about the first 4.
2474 for (int i = 3; i >= 0; --i) {
2475 unsigned Val = 0;
2476 SDOperand Arg = N->getOperand(i);
2477 if (Arg.getOpcode() != ISD::UNDEF)
2478 Val = cast<ConstantSDNode>(Arg)->getValue();
2479 Mask |= Val;
2480 if (i != 0)
2481 Mask <<= 2;
2482 }
2483
2484 return Mask;
2485}
2486
2487/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2488/// specifies a 8 element shuffle that can be broken into a pair of
2489/// PSHUFHW and PSHUFLW.
2490static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2491 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2492
2493 if (N->getNumOperands() != 8)
2494 return false;
2495
2496 // Lower quadword shuffled.
2497 for (unsigned i = 0; i != 4; ++i) {
2498 SDOperand Arg = N->getOperand(i);
2499 if (Arg.getOpcode() == ISD::UNDEF) continue;
2500 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2501 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002502 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 return false;
2504 }
2505
2506 // Upper quadword shuffled.
2507 for (unsigned i = 4; i != 8; ++i) {
2508 SDOperand Arg = N->getOperand(i);
2509 if (Arg.getOpcode() == ISD::UNDEF) continue;
2510 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2511 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2512 if (Val < 4 || Val > 7)
2513 return false;
2514 }
2515
2516 return true;
2517}
2518
Chris Lattnere6aa3862007-11-25 00:24:49 +00002519/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520/// values in ther permute mask.
2521static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2522 SDOperand &V2, SDOperand &Mask,
2523 SelectionDAG &DAG) {
2524 MVT::ValueType VT = Op.getValueType();
2525 MVT::ValueType MaskVT = Mask.getValueType();
2526 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2527 unsigned NumElems = Mask.getNumOperands();
2528 SmallVector<SDOperand, 8> MaskVec;
2529
2530 for (unsigned i = 0; i != NumElems; ++i) {
2531 SDOperand Arg = Mask.getOperand(i);
2532 if (Arg.getOpcode() == ISD::UNDEF) {
2533 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2534 continue;
2535 }
2536 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2537 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2538 if (Val < NumElems)
2539 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2540 else
2541 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2542 }
2543
2544 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002545 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2547}
2548
Evan Chenga6769df2007-12-07 21:30:01 +00002549/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2550/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002551static
2552SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2553 MVT::ValueType MaskVT = Mask.getValueType();
2554 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2555 unsigned NumElems = Mask.getNumOperands();
2556 SmallVector<SDOperand, 8> MaskVec;
2557 for (unsigned i = 0; i != NumElems; ++i) {
2558 SDOperand Arg = Mask.getOperand(i);
2559 if (Arg.getOpcode() == ISD::UNDEF) {
2560 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2561 continue;
2562 }
2563 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2564 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2565 if (Val < NumElems)
2566 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2567 else
2568 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2569 }
2570 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2571}
2572
2573
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002574/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2575/// match movhlps. The lower half elements should come from upper half of
2576/// V1 (and in order), and the upper half elements should come from the upper
2577/// half of V2 (and in order).
2578static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2579 unsigned NumElems = Mask->getNumOperands();
2580 if (NumElems != 4)
2581 return false;
2582 for (unsigned i = 0, e = 2; i != e; ++i)
2583 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2584 return false;
2585 for (unsigned i = 2; i != 4; ++i)
2586 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2587 return false;
2588 return true;
2589}
2590
2591/// isScalarLoadToVector - Returns true if the node is a scalar load that
2592/// is promoted to a vector.
2593static inline bool isScalarLoadToVector(SDNode *N) {
2594 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2595 N = N->getOperand(0).Val;
2596 return ISD::isNON_EXTLoad(N);
2597 }
2598 return false;
2599}
2600
2601/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2602/// match movlp{s|d}. The lower half elements should come from lower half of
2603/// V1 (and in order), and the upper half elements should come from the upper
2604/// half of V2 (and in order). And since V1 will become the source of the
2605/// MOVLP, it must be either a vector load or a scalar load to vector.
2606static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2607 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2608 return false;
2609 // Is V2 is a vector load, don't do this transformation. We will try to use
2610 // load folding shufps op.
2611 if (ISD::isNON_EXTLoad(V2))
2612 return false;
2613
2614 unsigned NumElems = Mask->getNumOperands();
2615 if (NumElems != 2 && NumElems != 4)
2616 return false;
2617 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2618 if (!isUndefOrEqual(Mask->getOperand(i), i))
2619 return false;
2620 for (unsigned i = NumElems/2; i != NumElems; ++i)
2621 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2622 return false;
2623 return true;
2624}
2625
2626/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2627/// all the same.
2628static bool isSplatVector(SDNode *N) {
2629 if (N->getOpcode() != ISD::BUILD_VECTOR)
2630 return false;
2631
2632 SDOperand SplatValue = N->getOperand(0);
2633 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2634 if (N->getOperand(i) != SplatValue)
2635 return false;
2636 return true;
2637}
2638
2639/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2640/// to an undef.
2641static bool isUndefShuffle(SDNode *N) {
2642 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2643 return false;
2644
2645 SDOperand V1 = N->getOperand(0);
2646 SDOperand V2 = N->getOperand(1);
2647 SDOperand Mask = N->getOperand(2);
2648 unsigned NumElems = Mask.getNumOperands();
2649 for (unsigned i = 0; i != NumElems; ++i) {
2650 SDOperand Arg = Mask.getOperand(i);
2651 if (Arg.getOpcode() != ISD::UNDEF) {
2652 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2653 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2654 return false;
2655 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2656 return false;
2657 }
2658 }
2659 return true;
2660}
2661
2662/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2663/// constant +0.0.
2664static inline bool isZeroNode(SDOperand Elt) {
2665 return ((isa<ConstantSDNode>(Elt) &&
2666 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2667 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002668 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669}
2670
2671/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2672/// to an zero vector.
2673static bool isZeroShuffle(SDNode *N) {
2674 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2675 return false;
2676
2677 SDOperand V1 = N->getOperand(0);
2678 SDOperand V2 = N->getOperand(1);
2679 SDOperand Mask = N->getOperand(2);
2680 unsigned NumElems = Mask.getNumOperands();
2681 for (unsigned i = 0; i != NumElems; ++i) {
2682 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002683 if (Arg.getOpcode() == ISD::UNDEF)
2684 continue;
2685
2686 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2687 if (Idx < NumElems) {
2688 unsigned Opc = V1.Val->getOpcode();
2689 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2690 continue;
2691 if (Opc != ISD::BUILD_VECTOR ||
2692 !isZeroNode(V1.Val->getOperand(Idx)))
2693 return false;
2694 } else if (Idx >= NumElems) {
2695 unsigned Opc = V2.Val->getOpcode();
2696 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2697 continue;
2698 if (Opc != ISD::BUILD_VECTOR ||
2699 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2700 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 }
2702 }
2703 return true;
2704}
2705
2706/// getZeroVector - Returns a vector of specified type with all zero elements.
2707///
2708static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2709 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002710
2711 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2712 // type. This ensures they get CSE'd.
2713 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2714 SDOperand Vec;
2715 if (MVT::getSizeInBits(VT) == 64) // MMX
2716 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2717 else // SSE
2718 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2719 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720}
2721
Chris Lattnere6aa3862007-11-25 00:24:49 +00002722/// getOnesVector - Returns a vector of specified type with all bits set.
2723///
2724static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2725 assert(MVT::isVector(VT) && "Expected a vector type");
2726
2727 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2728 // type. This ensures they get CSE'd.
2729 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2730 SDOperand Vec;
2731 if (MVT::getSizeInBits(VT) == 64) // MMX
2732 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2733 else // SSE
2734 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2735 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2736}
2737
2738
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002739/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2740/// that point to V2 points to its first element.
2741static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2742 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2743
2744 bool Changed = false;
2745 SmallVector<SDOperand, 8> MaskVec;
2746 unsigned NumElems = Mask.getNumOperands();
2747 for (unsigned i = 0; i != NumElems; ++i) {
2748 SDOperand Arg = Mask.getOperand(i);
2749 if (Arg.getOpcode() != ISD::UNDEF) {
2750 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2751 if (Val > NumElems) {
2752 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2753 Changed = true;
2754 }
2755 }
2756 MaskVec.push_back(Arg);
2757 }
2758
2759 if (Changed)
2760 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2761 &MaskVec[0], MaskVec.size());
2762 return Mask;
2763}
2764
2765/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2766/// operation of specified width.
2767static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2768 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2769 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2770
2771 SmallVector<SDOperand, 8> MaskVec;
2772 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2773 for (unsigned i = 1; i != NumElems; ++i)
2774 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2775 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2776}
2777
2778/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2779/// of specified width.
2780static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2781 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2782 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2783 SmallVector<SDOperand, 8> MaskVec;
2784 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2785 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2786 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2787 }
2788 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2789}
2790
2791/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2792/// of specified width.
2793static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2794 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2795 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2796 unsigned Half = NumElems/2;
2797 SmallVector<SDOperand, 8> MaskVec;
2798 for (unsigned i = 0; i != Half; ++i) {
2799 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2800 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2801 }
2802 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2803}
2804
2805/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2806///
2807static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2808 SDOperand V1 = Op.getOperand(0);
2809 SDOperand Mask = Op.getOperand(2);
2810 MVT::ValueType VT = Op.getValueType();
2811 unsigned NumElems = Mask.getNumOperands();
2812 Mask = getUnpacklMask(NumElems, DAG);
2813 while (NumElems != 4) {
2814 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2815 NumElems >>= 1;
2816 }
2817 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2818
Chris Lattnere6aa3862007-11-25 00:24:49 +00002819 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2821 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2822 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2823}
2824
2825/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002826/// vector of zero or undef vector. This produces a shuffle where the low
2827/// element of V2 is swizzled into the zero/undef vector, landing at element
2828/// 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 +00002829static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2830 unsigned NumElems, unsigned Idx,
2831 bool isZero, SelectionDAG &DAG) {
2832 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2833 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2834 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002835 SmallVector<SDOperand, 16> MaskVec;
2836 for (unsigned i = 0; i != NumElems; ++i)
2837 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2838 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2839 else
2840 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2842 &MaskVec[0], MaskVec.size());
2843 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2844}
2845
2846/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2847///
2848static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2849 unsigned NumNonZero, unsigned NumZero,
2850 SelectionDAG &DAG, TargetLowering &TLI) {
2851 if (NumNonZero > 8)
2852 return SDOperand();
2853
2854 SDOperand V(0, 0);
2855 bool First = true;
2856 for (unsigned i = 0; i < 16; ++i) {
2857 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2858 if (ThisIsNonZero && First) {
2859 if (NumZero)
2860 V = getZeroVector(MVT::v8i16, DAG);
2861 else
2862 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2863 First = false;
2864 }
2865
2866 if ((i & 1) != 0) {
2867 SDOperand ThisElt(0, 0), LastElt(0, 0);
2868 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2869 if (LastIsNonZero) {
2870 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2871 }
2872 if (ThisIsNonZero) {
2873 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2874 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2875 ThisElt, DAG.getConstant(8, MVT::i8));
2876 if (LastIsNonZero)
2877 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2878 } else
2879 ThisElt = LastElt;
2880
2881 if (ThisElt.Val)
2882 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002883 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002884 }
2885 }
2886
2887 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2888}
2889
2890/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2891///
2892static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2893 unsigned NumNonZero, unsigned NumZero,
2894 SelectionDAG &DAG, TargetLowering &TLI) {
2895 if (NumNonZero > 4)
2896 return SDOperand();
2897
2898 SDOperand V(0, 0);
2899 bool First = true;
2900 for (unsigned i = 0; i < 8; ++i) {
2901 bool isNonZero = (NonZeros & (1 << i)) != 0;
2902 if (isNonZero) {
2903 if (First) {
2904 if (NumZero)
2905 V = getZeroVector(MVT::v8i16, DAG);
2906 else
2907 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2908 First = false;
2909 }
2910 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002911 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002912 }
2913 }
2914
2915 return V;
2916}
2917
2918SDOperand
2919X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002920 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2921 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2922 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2923 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2924 // eliminated on x86-32 hosts.
2925 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2926 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002927
Chris Lattnere6aa3862007-11-25 00:24:49 +00002928 if (ISD::isBuildVectorAllOnes(Op.Val))
2929 return getOnesVector(Op.getValueType(), DAG);
2930 return getZeroVector(Op.getValueType(), DAG);
2931 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002932
2933 MVT::ValueType VT = Op.getValueType();
2934 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2935 unsigned EVTBits = MVT::getSizeInBits(EVT);
2936
2937 unsigned NumElems = Op.getNumOperands();
2938 unsigned NumZero = 0;
2939 unsigned NumNonZero = 0;
2940 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00002941 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00002942 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 for (unsigned i = 0; i < NumElems; ++i) {
2944 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00002945 if (Elt.getOpcode() == ISD::UNDEF)
2946 continue;
2947 Values.insert(Elt);
2948 if (Elt.getOpcode() != ISD::Constant &&
2949 Elt.getOpcode() != ISD::ConstantFP)
2950 HasNonImms = true;
2951 if (isZeroNode(Elt))
2952 NumZero++;
2953 else {
2954 NonZeros |= (1 << i);
2955 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002956 }
2957 }
2958
2959 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002960 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2961 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002962 }
2963
2964 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2965 if (Values.size() == 1)
2966 return SDOperand();
2967
2968 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00002969 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 unsigned Idx = CountTrailingZeros_32(NonZeros);
2971 SDOperand Item = Op.getOperand(Idx);
2972 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2973 if (Idx == 0)
2974 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2975 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2976 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00002977 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
2978 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002979
2980 if (EVTBits == 32) {
2981 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2982 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2983 DAG);
2984 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2985 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2986 SmallVector<SDOperand, 8> MaskVec;
2987 for (unsigned i = 0; i < NumElems; i++)
2988 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2989 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2990 &MaskVec[0], MaskVec.size());
2991 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2992 DAG.getNode(ISD::UNDEF, VT), Mask);
2993 }
2994 }
2995
Dan Gohman21463242007-07-24 22:55:08 +00002996 // A vector full of immediates; various special cases are already
2997 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00002998 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00002999 return SDOperand();
3000
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001 // Let legalizer expand 2-wide build_vectors.
3002 if (EVTBits == 64)
3003 return SDOperand();
3004
3005 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3006 if (EVTBits == 8 && NumElems == 16) {
3007 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3008 *this);
3009 if (V.Val) return V;
3010 }
3011
3012 if (EVTBits == 16 && NumElems == 8) {
3013 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3014 *this);
3015 if (V.Val) return V;
3016 }
3017
3018 // If element VT is == 32 bits, turn it into a number of shuffles.
3019 SmallVector<SDOperand, 8> V;
3020 V.resize(NumElems);
3021 if (NumElems == 4 && NumZero > 0) {
3022 for (unsigned i = 0; i < 4; ++i) {
3023 bool isZero = !(NonZeros & (1 << i));
3024 if (isZero)
3025 V[i] = getZeroVector(VT, DAG);
3026 else
3027 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3028 }
3029
3030 for (unsigned i = 0; i < 2; ++i) {
3031 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3032 default: break;
3033 case 0:
3034 V[i] = V[i*2]; // Must be a zero vector.
3035 break;
3036 case 1:
3037 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3038 getMOVLMask(NumElems, DAG));
3039 break;
3040 case 2:
3041 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3042 getMOVLMask(NumElems, DAG));
3043 break;
3044 case 3:
3045 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3046 getUnpacklMask(NumElems, DAG));
3047 break;
3048 }
3049 }
3050
3051 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3052 // clears the upper bits.
3053 // FIXME: we can do the same for v4f32 case when we know both parts of
3054 // the lower half come from scalar_to_vector (loadf32). We should do
3055 // that in post legalizer dag combiner with target specific hooks.
3056 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3057 return V[0];
3058 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3059 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3060 SmallVector<SDOperand, 8> MaskVec;
3061 bool Reverse = (NonZeros & 0x3) == 2;
3062 for (unsigned i = 0; i < 2; ++i)
3063 if (Reverse)
3064 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3065 else
3066 MaskVec.push_back(DAG.getConstant(i, EVT));
3067 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3068 for (unsigned i = 0; i < 2; ++i)
3069 if (Reverse)
3070 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3071 else
3072 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3073 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3074 &MaskVec[0], MaskVec.size());
3075 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3076 }
3077
3078 if (Values.size() > 2) {
3079 // Expand into a number of unpckl*.
3080 // e.g. for v4f32
3081 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3082 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3083 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3084 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3085 for (unsigned i = 0; i < NumElems; ++i)
3086 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3087 NumElems >>= 1;
3088 while (NumElems != 0) {
3089 for (unsigned i = 0; i < NumElems; ++i)
3090 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3091 UnpckMask);
3092 NumElems >>= 1;
3093 }
3094 return V[0];
3095 }
3096
3097 return SDOperand();
3098}
3099
Evan Chengfca29242007-12-07 08:07:39 +00003100static
3101SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3102 SDOperand PermMask, SelectionDAG &DAG,
3103 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003104 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003105 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3106 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003107 MVT::ValueType PtrVT = TLI.getPointerTy();
3108 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3109 PermMask.Val->op_end());
3110
3111 // First record which half of which vector the low elements come from.
3112 SmallVector<unsigned, 4> LowQuad(4);
3113 for (unsigned i = 0; i < 4; ++i) {
3114 SDOperand Elt = MaskElts[i];
3115 if (Elt.getOpcode() == ISD::UNDEF)
3116 continue;
3117 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3118 int QuadIdx = EltIdx / 4;
3119 ++LowQuad[QuadIdx];
3120 }
3121 int BestLowQuad = -1;
3122 unsigned MaxQuad = 1;
3123 for (unsigned i = 0; i < 4; ++i) {
3124 if (LowQuad[i] > MaxQuad) {
3125 BestLowQuad = i;
3126 MaxQuad = LowQuad[i];
3127 }
Evan Chengfca29242007-12-07 08:07:39 +00003128 }
3129
Evan Cheng75184a92007-12-11 01:46:18 +00003130 // Record which half of which vector the high elements come from.
3131 SmallVector<unsigned, 4> HighQuad(4);
3132 for (unsigned i = 4; i < 8; ++i) {
3133 SDOperand Elt = MaskElts[i];
3134 if (Elt.getOpcode() == ISD::UNDEF)
3135 continue;
3136 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3137 int QuadIdx = EltIdx / 4;
3138 ++HighQuad[QuadIdx];
3139 }
3140 int BestHighQuad = -1;
3141 MaxQuad = 1;
3142 for (unsigned i = 0; i < 4; ++i) {
3143 if (HighQuad[i] > MaxQuad) {
3144 BestHighQuad = i;
3145 MaxQuad = HighQuad[i];
3146 }
3147 }
3148
3149 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3150 if (BestLowQuad != -1 || BestHighQuad != -1) {
3151 // First sort the 4 chunks in order using shufpd.
3152 SmallVector<SDOperand, 8> MaskVec;
3153 if (BestLowQuad != -1)
3154 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3155 else
3156 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3157 if (BestHighQuad != -1)
3158 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3159 else
3160 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3161 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3162 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3163 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3164 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3165 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3166
3167 // Now sort high and low parts separately.
3168 BitVector InOrder(8);
3169 if (BestLowQuad != -1) {
3170 // Sort lower half in order using PSHUFLW.
3171 MaskVec.clear();
3172 bool AnyOutOrder = false;
3173 for (unsigned i = 0; i != 4; ++i) {
3174 SDOperand Elt = MaskElts[i];
3175 if (Elt.getOpcode() == ISD::UNDEF) {
3176 MaskVec.push_back(Elt);
3177 InOrder.set(i);
3178 } else {
3179 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3180 if (EltIdx != i)
3181 AnyOutOrder = true;
3182 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3183 // If this element is in the right place after this shuffle, then
3184 // remember it.
3185 if ((int)(EltIdx / 4) == BestLowQuad)
3186 InOrder.set(i);
3187 }
3188 }
3189 if (AnyOutOrder) {
3190 for (unsigned i = 4; i != 8; ++i)
3191 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3192 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3193 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3194 }
3195 }
3196
3197 if (BestHighQuad != -1) {
3198 // Sort high half in order using PSHUFHW if possible.
3199 MaskVec.clear();
3200 for (unsigned i = 0; i != 4; ++i)
3201 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3202 bool AnyOutOrder = false;
3203 for (unsigned i = 4; i != 8; ++i) {
3204 SDOperand Elt = MaskElts[i];
3205 if (Elt.getOpcode() == ISD::UNDEF) {
3206 MaskVec.push_back(Elt);
3207 InOrder.set(i);
3208 } else {
3209 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3210 if (EltIdx != i)
3211 AnyOutOrder = true;
3212 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3213 // If this element is in the right place after this shuffle, then
3214 // remember it.
3215 if ((int)(EltIdx / 4) == BestHighQuad)
3216 InOrder.set(i);
3217 }
3218 }
3219 if (AnyOutOrder) {
3220 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3221 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3222 }
3223 }
3224
3225 // The other elements are put in the right place using pextrw and pinsrw.
3226 for (unsigned i = 0; i != 8; ++i) {
3227 if (InOrder[i])
3228 continue;
3229 SDOperand Elt = MaskElts[i];
3230 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3231 if (EltIdx == i)
3232 continue;
3233 SDOperand ExtOp = (EltIdx < 8)
3234 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3235 DAG.getConstant(EltIdx, PtrVT))
3236 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3237 DAG.getConstant(EltIdx - 8, PtrVT));
3238 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3239 DAG.getConstant(i, PtrVT));
3240 }
3241 return NewV;
3242 }
3243
3244 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3245 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003246 // First, let's find out how many elements are already in the right order.
3247 unsigned V1InOrder = 0;
3248 unsigned V1FromV1 = 0;
3249 unsigned V2InOrder = 0;
3250 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003251 SmallVector<SDOperand, 8> V1Elts;
3252 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003253 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003254 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003255 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003256 V1Elts.push_back(Elt);
3257 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003258 ++V1InOrder;
3259 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003260 continue;
3261 }
3262 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3263 if (EltIdx == i) {
3264 V1Elts.push_back(Elt);
3265 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3266 ++V1InOrder;
3267 } else if (EltIdx == i+8) {
3268 V1Elts.push_back(Elt);
3269 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3270 ++V2InOrder;
3271 } else if (EltIdx < 8) {
3272 V1Elts.push_back(Elt);
3273 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003274 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003275 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3276 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003277 }
3278 }
3279
3280 if (V2InOrder > V1InOrder) {
3281 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3282 std::swap(V1, V2);
3283 std::swap(V1Elts, V2Elts);
3284 std::swap(V1FromV1, V2FromV2);
3285 }
3286
Evan Cheng75184a92007-12-11 01:46:18 +00003287 if ((V1FromV1 + V1InOrder) != 8) {
3288 // Some elements are from V2.
3289 if (V1FromV1) {
3290 // If there are elements that are from V1 but out of place,
3291 // then first sort them in place
3292 SmallVector<SDOperand, 8> MaskVec;
3293 for (unsigned i = 0; i < 8; ++i) {
3294 SDOperand Elt = V1Elts[i];
3295 if (Elt.getOpcode() == ISD::UNDEF) {
3296 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3297 continue;
3298 }
3299 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3300 if (EltIdx >= 8)
3301 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3302 else
3303 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3304 }
3305 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3306 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003307 }
Evan Cheng75184a92007-12-11 01:46:18 +00003308
3309 NewV = V1;
3310 for (unsigned i = 0; i < 8; ++i) {
3311 SDOperand Elt = V1Elts[i];
3312 if (Elt.getOpcode() == ISD::UNDEF)
3313 continue;
3314 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3315 if (EltIdx < 8)
3316 continue;
3317 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3318 DAG.getConstant(EltIdx - 8, PtrVT));
3319 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3320 DAG.getConstant(i, PtrVT));
3321 }
3322 return NewV;
3323 } else {
3324 // All elements are from V1.
3325 NewV = V1;
3326 for (unsigned i = 0; i < 8; ++i) {
3327 SDOperand Elt = V1Elts[i];
3328 if (Elt.getOpcode() == ISD::UNDEF)
3329 continue;
3330 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3331 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3332 DAG.getConstant(EltIdx, PtrVT));
3333 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3334 DAG.getConstant(i, PtrVT));
3335 }
3336 return NewV;
3337 }
3338}
3339
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003340/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3341/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3342/// done when every pair / quad of shuffle mask elements point to elements in
3343/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003344/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3345static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003346SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3347 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003348 SDOperand PermMask, SelectionDAG &DAG,
3349 TargetLowering &TLI) {
3350 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003351 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3352 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3353 MVT::ValueType NewVT = MaskVT;
3354 switch (VT) {
3355 case MVT::v4f32: NewVT = MVT::v2f64; break;
3356 case MVT::v4i32: NewVT = MVT::v2i64; break;
3357 case MVT::v8i16: NewVT = MVT::v4i32; break;
3358 case MVT::v16i8: NewVT = MVT::v4i32; break;
3359 default: assert(false && "Unexpected!");
3360 }
3361
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003362 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003363 if (MVT::isInteger(VT))
3364 NewVT = MVT::v2i64;
3365 else
3366 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003367 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003368 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));
Evan Chengd1045a62008-02-18 23:04:32 +00003838 MVT::ValueType VT = MVT::v2i32;
3839 switch (Op.getValueType()) {
3840 default: break;
3841 case MVT::v16i8:
3842 case MVT::v8i16:
3843 VT = MVT::v4i32;
3844 break;
3845 }
3846 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3847 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003848}
3849
3850// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3851// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3852// one of the above mentioned nodes. It has to be wrapped because otherwise
3853// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3854// be used to form addressing mode. These wrapped nodes will be selected
3855// into MOV32ri.
3856SDOperand
3857X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3858 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3859 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3860 getPointerTy(),
3861 CP->getAlignment());
3862 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3863 // With PIC, the address is actually $g + Offset.
3864 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3865 !Subtarget->isPICStyleRIPRel()) {
3866 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3867 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3868 Result);
3869 }
3870
3871 return Result;
3872}
3873
3874SDOperand
3875X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3876 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3877 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003878 // If it's a debug information descriptor, don't mess with it.
3879 if (DAG.isVerifiedDebugInfoDesc(Op))
3880 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003881 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3882 // With PIC, the address is actually $g + Offset.
3883 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3884 !Subtarget->isPICStyleRIPRel()) {
3885 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3886 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3887 Result);
3888 }
3889
3890 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3891 // load the value at address GV, not the value of GV itself. This means that
3892 // the GlobalAddress must be in the base or index register of the address, not
3893 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3894 // The same applies for external symbols during PIC codegen
3895 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003896 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003897 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003898
3899 return Result;
3900}
3901
3902// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3903static SDOperand
3904LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3905 const MVT::ValueType PtrVT) {
3906 SDOperand InFlag;
3907 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3908 DAG.getNode(X86ISD::GlobalBaseReg,
3909 PtrVT), InFlag);
3910 InFlag = Chain.getValue(1);
3911
3912 // emit leal symbol@TLSGD(,%ebx,1), %eax
3913 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3914 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3915 GA->getValueType(0),
3916 GA->getOffset());
3917 SDOperand Ops[] = { Chain, TGA, InFlag };
3918 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3919 InFlag = Result.getValue(2);
3920 Chain = Result.getValue(1);
3921
3922 // call ___tls_get_addr. This function receives its argument in
3923 // the register EAX.
3924 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3925 InFlag = Chain.getValue(1);
3926
3927 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3928 SDOperand Ops1[] = { Chain,
3929 DAG.getTargetExternalSymbol("___tls_get_addr",
3930 PtrVT),
3931 DAG.getRegister(X86::EAX, PtrVT),
3932 DAG.getRegister(X86::EBX, PtrVT),
3933 InFlag };
3934 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3935 InFlag = Chain.getValue(1);
3936
3937 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3938}
3939
3940// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3941// "local exec" model.
3942static SDOperand
3943LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3944 const MVT::ValueType PtrVT) {
3945 // Get the Thread Pointer
3946 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3947 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3948 // exec)
3949 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3950 GA->getValueType(0),
3951 GA->getOffset());
3952 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3953
3954 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00003955 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003956 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957
3958 // The address of the thread local variable is the add of the thread
3959 // pointer with the offset of the variable.
3960 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3961}
3962
3963SDOperand
3964X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3965 // TODO: implement the "local dynamic" model
3966 // TODO: implement the "initial exec"model for pic executables
3967 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3968 "TLS not implemented for non-ELF and 64-bit targets");
3969 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3970 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3971 // otherwise use the "Local Exec"TLS Model
3972 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3973 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3974 else
3975 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3976}
3977
3978SDOperand
3979X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3980 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3981 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3982 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3983 // With PIC, the address is actually $g + Offset.
3984 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3985 !Subtarget->isPICStyleRIPRel()) {
3986 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3987 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3988 Result);
3989 }
3990
3991 return Result;
3992}
3993
3994SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3995 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3996 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3997 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3998 // With PIC, the address is actually $g + Offset.
3999 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4000 !Subtarget->isPICStyleRIPRel()) {
4001 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4002 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4003 Result);
4004 }
4005
4006 return Result;
4007}
4008
Chris Lattner62814a32007-10-17 06:02:13 +00004009/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4010/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004011SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00004012 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4013 "Not an i64 shift!");
4014 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4015 SDOperand ShOpLo = Op.getOperand(0);
4016 SDOperand ShOpHi = Op.getOperand(1);
4017 SDOperand ShAmt = Op.getOperand(2);
4018 SDOperand Tmp1 = isSRA ?
4019 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4020 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004021
Chris Lattner62814a32007-10-17 06:02:13 +00004022 SDOperand Tmp2, Tmp3;
4023 if (Op.getOpcode() == ISD::SHL_PARTS) {
4024 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4025 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4026 } else {
4027 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4028 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4029 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030
Chris Lattner62814a32007-10-17 06:02:13 +00004031 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4032 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4033 DAG.getConstant(32, MVT::i8));
4034 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4035 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036
Chris Lattner62814a32007-10-17 06:02:13 +00004037 SDOperand Hi, Lo;
4038 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4039 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4040 SmallVector<SDOperand, 4> Ops;
4041 if (Op.getOpcode() == ISD::SHL_PARTS) {
4042 Ops.push_back(Tmp2);
4043 Ops.push_back(Tmp3);
4044 Ops.push_back(CC);
4045 Ops.push_back(Cond);
4046 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004049 Ops.push_back(Tmp3);
4050 Ops.push_back(Tmp1);
4051 Ops.push_back(CC);
4052 Ops.push_back(Cond);
4053 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4054 } else {
4055 Ops.push_back(Tmp2);
4056 Ops.push_back(Tmp3);
4057 Ops.push_back(CC);
4058 Ops.push_back(Cond);
4059 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4060
4061 Ops.clear();
4062 Ops.push_back(Tmp3);
4063 Ops.push_back(Tmp1);
4064 Ops.push_back(CC);
4065 Ops.push_back(Cond);
4066 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4067 }
4068
4069 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4070 Ops.clear();
4071 Ops.push_back(Lo);
4072 Ops.push_back(Hi);
4073 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004074}
4075
4076SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4077 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4078 Op.getOperand(0).getValueType() >= MVT::i16 &&
4079 "Unknown SINT_TO_FP to lower!");
4080
4081 SDOperand Result;
4082 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4083 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4084 MachineFunction &MF = DAG.getMachineFunction();
4085 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4086 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4087 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004088 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004089 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004090 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091
Dale Johannesen2fc20782007-09-14 22:26:36 +00004092 // These are really Legal; caller falls through into that case.
Chris Lattnercf515b52008-01-16 06:24:21 +00004093 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004094 return Result;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004095 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen958b08b2007-09-19 23:55:34 +00004096 Subtarget->is64Bit())
4097 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00004098
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004099 // Build the FILD
4100 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004101 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004102 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4104 else
4105 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4106 SmallVector<SDOperand, 8> Ops;
4107 Ops.push_back(Chain);
4108 Ops.push_back(StackSlot);
4109 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00004110 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111 Tys, &Ops[0], Ops.size());
4112
Dale Johannesen2fc20782007-09-14 22:26:36 +00004113 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114 Chain = Result.getValue(1);
4115 SDOperand InFlag = Result.getValue(2);
4116
4117 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4118 // shouldn't be necessary except that RFP cannot be live across
4119 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4120 MachineFunction &MF = DAG.getMachineFunction();
4121 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4122 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4123 Tys = DAG.getVTList(MVT::Other);
4124 SmallVector<SDOperand, 8> Ops;
4125 Ops.push_back(Chain);
4126 Ops.push_back(Result);
4127 Ops.push_back(StackSlot);
4128 Ops.push_back(DAG.getValueType(Op.getValueType()));
4129 Ops.push_back(InFlag);
4130 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004131 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004132 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 }
4134
4135 return Result;
4136}
4137
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004138std::pair<SDOperand,SDOperand> X86TargetLowering::
4139FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4141 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004142
Dale Johannesen2fc20782007-09-14 22:26:36 +00004143 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004144 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004145 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004146 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004147 if (Subtarget->is64Bit() &&
4148 Op.getValueType() == MVT::i64 &&
4149 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004150 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004151
Evan Cheng05441e62007-10-15 20:11:21 +00004152 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4153 // stack slot.
4154 MachineFunction &MF = DAG.getMachineFunction();
4155 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4156 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4157 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158 unsigned Opc;
4159 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004160 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4161 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4162 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4163 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164 }
4165
4166 SDOperand Chain = DAG.getEntryNode();
4167 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004168 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004170 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004171 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4173 SDOperand Ops[] = {
4174 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4175 };
4176 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4177 Chain = Value.getValue(1);
4178 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4179 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4180 }
4181
4182 // Build the FP_TO_INT*_IN_MEM
4183 SDOperand Ops[] = { Chain, Value, StackSlot };
4184 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4185
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004186 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004187}
4188
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004189SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004190 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4191 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4192 if (FIST.Val == 0) return SDOperand();
4193
4194 // Load the result.
4195 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4196}
4197
4198SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4199 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4200 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4201 if (FIST.Val == 0) return 0;
4202
4203 // Return an i64 load from the stack slot.
4204 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4205
4206 // Use a MERGE_VALUES node to drop the chain result value.
4207 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4208}
4209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4211 MVT::ValueType VT = Op.getValueType();
4212 MVT::ValueType EltVT = VT;
4213 if (MVT::isVector(VT))
4214 EltVT = MVT::getVectorElementType(VT);
4215 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4216 std::vector<Constant*> CV;
4217 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004218 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 CV.push_back(C);
4220 CV.push_back(C);
4221 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004222 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004223 CV.push_back(C);
4224 CV.push_back(C);
4225 CV.push_back(C);
4226 CV.push_back(C);
4227 }
Dan Gohman11821702007-07-27 17:16:43 +00004228 Constant *C = ConstantVector::get(CV);
4229 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004230 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004231 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004232 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4234}
4235
4236SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4237 MVT::ValueType VT = Op.getValueType();
4238 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004239 unsigned EltNum = 1;
4240 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004242 EltNum = MVT::getVectorNumElements(VT);
4243 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4245 std::vector<Constant*> CV;
4246 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004247 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004248 CV.push_back(C);
4249 CV.push_back(C);
4250 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004251 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252 CV.push_back(C);
4253 CV.push_back(C);
4254 CV.push_back(C);
4255 CV.push_back(C);
4256 }
Dan Gohman11821702007-07-27 17:16:43 +00004257 Constant *C = ConstantVector::get(CV);
4258 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004259 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004260 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004261 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004262 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004263 return DAG.getNode(ISD::BIT_CONVERT, VT,
4264 DAG.getNode(ISD::XOR, MVT::v2i64,
4265 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4266 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4267 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004268 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4269 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270}
4271
4272SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4273 SDOperand Op0 = Op.getOperand(0);
4274 SDOperand Op1 = Op.getOperand(1);
4275 MVT::ValueType VT = Op.getValueType();
4276 MVT::ValueType SrcVT = Op1.getValueType();
4277 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4278
4279 // If second operand is smaller, extend it first.
4280 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4281 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4282 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004283 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004285 // And if it is bigger, shrink it first.
4286 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004287 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004288 SrcVT = VT;
4289 SrcTy = MVT::getTypeForValueType(SrcVT);
4290 }
4291
4292 // At this point the operands and the result should have the same
4293 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004294
4295 // First get the sign bit of second operand.
4296 std::vector<Constant*> CV;
4297 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004298 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4299 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004300 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004301 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4302 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4303 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4304 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305 }
Dan Gohman11821702007-07-27 17:16:43 +00004306 Constant *C = ConstantVector::get(CV);
4307 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004308 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004309 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004310 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004311 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4312
4313 // Shift sign bit right or left if the two operands have different types.
4314 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4315 // Op0 is MVT::f32, Op1 is MVT::f64.
4316 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4317 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4318 DAG.getConstant(32, MVT::i32));
4319 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4320 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004321 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004322 }
4323
4324 // Clear first operand sign bit.
4325 CV.clear();
4326 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004327 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4328 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004330 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4331 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4332 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4333 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 }
Dan Gohman11821702007-07-27 17:16:43 +00004335 C = ConstantVector::get(CV);
4336 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004337 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004338 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004339 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004340 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4341
4342 // Or the value with the sign bit.
4343 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4344}
4345
Evan Cheng621216e2007-09-29 00:00:36 +00004346SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004347 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004348 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004349 SDOperand Op0 = Op.getOperand(0);
4350 SDOperand Op1 = Op.getOperand(1);
4351 SDOperand CC = Op.getOperand(2);
4352 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4353 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4354 unsigned X86CC;
4355
Evan Cheng950aac02007-09-25 01:57:46 +00004356 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004357 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004358 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4359 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004360 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004361 }
Evan Cheng950aac02007-09-25 01:57:46 +00004362
4363 assert(isFP && "Illegal integer SetCC!");
4364
Evan Cheng621216e2007-09-29 00:00:36 +00004365 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004366 switch (SetCCOpcode) {
4367 default: assert(false && "Illegal floating point SetCC!");
4368 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004369 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004370 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004371 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004372 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4373 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4374 }
4375 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004376 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004377 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004378 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004379 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4380 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4381 }
4382 }
4383}
4384
4385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004386SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4387 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 SDOperand Cond = Op.getOperand(0);
4389 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390
4391 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004392 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004393
Evan Cheng50d37ab2007-10-08 22:16:29 +00004394 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4395 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004396 if (Cond.getOpcode() == X86ISD::SETCC) {
4397 CC = Cond.getOperand(0);
4398
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399 SDOperand Cmp = Cond.getOperand(1);
4400 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004401 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004402
Evan Cheng50d37ab2007-10-08 22:16:29 +00004403 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004404 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004405 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004406 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004407
Evan Cheng621216e2007-09-29 00:00:36 +00004408 if ((Opc == X86ISD::CMP ||
4409 Opc == X86ISD::COMI ||
4410 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004411 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004412 addTest = false;
4413 }
4414 }
4415
4416 if (addTest) {
4417 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004418 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004419 }
4420
4421 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4422 MVT::Flag);
4423 SmallVector<SDOperand, 4> Ops;
4424 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4425 // condition is true.
4426 Ops.push_back(Op.getOperand(2));
4427 Ops.push_back(Op.getOperand(1));
4428 Ops.push_back(CC);
4429 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004430 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004431}
4432
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4434 bool addTest = true;
4435 SDOperand Chain = Op.getOperand(0);
4436 SDOperand Cond = Op.getOperand(1);
4437 SDOperand Dest = Op.getOperand(2);
4438 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439
4440 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004441 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442
Evan Cheng50d37ab2007-10-08 22:16:29 +00004443 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4444 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445 if (Cond.getOpcode() == X86ISD::SETCC) {
4446 CC = Cond.getOperand(0);
4447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 SDOperand Cmp = Cond.getOperand(1);
4449 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004450 if (Opc == X86ISD::CMP ||
4451 Opc == X86ISD::COMI ||
4452 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004453 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004454 addTest = false;
4455 }
4456 }
4457
4458 if (addTest) {
4459 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004460 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004461 }
Evan Cheng621216e2007-09-29 00:00:36 +00004462 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004463 Chain, Op.getOperand(2), CC, Cond);
4464}
4465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466
4467// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4468// Calls to _alloca is needed to probe the stack when allocating more than 4k
4469// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4470// that the guard pages used by the OS virtual memory manager are allocated in
4471// correct sequence.
4472SDOperand
4473X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4474 SelectionDAG &DAG) {
4475 assert(Subtarget->isTargetCygMing() &&
4476 "This should be used only on Cygwin/Mingw targets");
4477
4478 // Get the inputs.
4479 SDOperand Chain = Op.getOperand(0);
4480 SDOperand Size = Op.getOperand(1);
4481 // FIXME: Ensure alignment here
4482
4483 SDOperand Flag;
4484
4485 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004486 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004487
4488 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4489 Flag = Chain.getValue(1);
4490
4491 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4492 SDOperand Ops[] = { Chain,
4493 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4494 DAG.getRegister(X86::EAX, IntPtr),
4495 Flag };
4496 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4497 Flag = Chain.getValue(1);
4498
4499 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4500
4501 std::vector<MVT::ValueType> Tys;
4502 Tys.push_back(SPTy);
4503 Tys.push_back(MVT::Other);
4504 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4505 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4506}
4507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4509 SDOperand InFlag(0, 0);
4510 SDOperand Chain = Op.getOperand(0);
4511 unsigned Align =
4512 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4513 if (Align == 0) Align = 1;
4514
4515 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004516 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004517 // The libc version is likely to be faster for these cases. It can use the
4518 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004520 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004521 MVT::ValueType IntPtr = getPointerTy();
4522 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4523 TargetLowering::ArgListTy Args;
4524 TargetLowering::ArgListEntry Entry;
4525 Entry.Node = Op.getOperand(1);
4526 Entry.Ty = IntPtrTy;
4527 Args.push_back(Entry);
4528 // Extend the unsigned i8 argument to be an int value for the call.
4529 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4530 Entry.Ty = IntPtrTy;
4531 Args.push_back(Entry);
4532 Entry.Node = Op.getOperand(3);
4533 Args.push_back(Entry);
4534 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004535 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4536 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537 return CallResult.second;
4538 }
4539
4540 MVT::ValueType AVT;
4541 SDOperand Count;
4542 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4543 unsigned BytesLeft = 0;
4544 bool TwoRepStos = false;
4545 if (ValC) {
4546 unsigned ValReg;
4547 uint64_t Val = ValC->getValue() & 255;
4548
4549 // If the value is a constant, then we can potentially use larger sets.
4550 switch (Align & 3) {
4551 case 2: // WORD aligned
4552 AVT = MVT::i16;
4553 ValReg = X86::AX;
4554 Val = (Val << 8) | Val;
4555 break;
4556 case 0: // DWORD aligned
4557 AVT = MVT::i32;
4558 ValReg = X86::EAX;
4559 Val = (Val << 8) | Val;
4560 Val = (Val << 16) | Val;
4561 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4562 AVT = MVT::i64;
4563 ValReg = X86::RAX;
4564 Val = (Val << 32) | Val;
4565 }
4566 break;
4567 default: // Byte aligned
4568 AVT = MVT::i8;
4569 ValReg = X86::AL;
4570 Count = Op.getOperand(3);
4571 break;
4572 }
4573
4574 if (AVT > MVT::i8) {
4575 if (I) {
4576 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004577 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578 BytesLeft = I->getValue() % UBytes;
4579 } else {
4580 assert(AVT >= MVT::i32 &&
4581 "Do not use rep;stos if not at least DWORD aligned");
4582 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4583 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4584 TwoRepStos = true;
4585 }
4586 }
4587
4588 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4589 InFlag);
4590 InFlag = Chain.getValue(1);
4591 } else {
4592 AVT = MVT::i8;
4593 Count = Op.getOperand(3);
4594 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4595 InFlag = Chain.getValue(1);
4596 }
4597
4598 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4599 Count, InFlag);
4600 InFlag = Chain.getValue(1);
4601 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4602 Op.getOperand(1), InFlag);
4603 InFlag = Chain.getValue(1);
4604
4605 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4606 SmallVector<SDOperand, 8> Ops;
4607 Ops.push_back(Chain);
4608 Ops.push_back(DAG.getValueType(AVT));
4609 Ops.push_back(InFlag);
4610 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4611
4612 if (TwoRepStos) {
4613 InFlag = Chain.getValue(1);
4614 Count = Op.getOperand(3);
4615 MVT::ValueType CVT = Count.getValueType();
4616 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4617 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4618 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4619 Left, InFlag);
4620 InFlag = Chain.getValue(1);
4621 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4622 Ops.clear();
4623 Ops.push_back(Chain);
4624 Ops.push_back(DAG.getValueType(MVT::i8));
4625 Ops.push_back(InFlag);
4626 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4627 } else if (BytesLeft) {
4628 // Issue stores for the last 1 - 7 bytes.
4629 SDOperand Value;
4630 unsigned Val = ValC->getValue() & 255;
4631 unsigned Offset = I->getValue() - BytesLeft;
4632 SDOperand DstAddr = Op.getOperand(1);
4633 MVT::ValueType AddrVT = DstAddr.getValueType();
4634 if (BytesLeft >= 4) {
4635 Val = (Val << 8) | Val;
4636 Val = (Val << 16) | Val;
4637 Value = DAG.getConstant(Val, MVT::i32);
4638 Chain = DAG.getStore(Chain, Value,
4639 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4640 DAG.getConstant(Offset, AddrVT)),
4641 NULL, 0);
4642 BytesLeft -= 4;
4643 Offset += 4;
4644 }
4645 if (BytesLeft >= 2) {
4646 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4647 Chain = DAG.getStore(Chain, Value,
4648 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4649 DAG.getConstant(Offset, AddrVT)),
4650 NULL, 0);
4651 BytesLeft -= 2;
4652 Offset += 2;
4653 }
4654 if (BytesLeft == 1) {
4655 Value = DAG.getConstant(Val, MVT::i8);
4656 Chain = DAG.getStore(Chain, Value,
4657 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4658 DAG.getConstant(Offset, AddrVT)),
4659 NULL, 0);
4660 }
4661 }
4662
4663 return Chain;
4664}
4665
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004666SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4667 SDOperand Dest,
4668 SDOperand Source,
4669 unsigned Size,
4670 unsigned Align,
4671 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004672 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 switch (Align & 3) {
4675 case 2: // WORD aligned
4676 AVT = MVT::i16;
4677 break;
4678 case 0: // DWORD aligned
4679 AVT = MVT::i32;
4680 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4681 AVT = MVT::i64;
4682 break;
4683 default: // Byte aligned
4684 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685 break;
4686 }
4687
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004688 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004689 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004690 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004691
4692 SDOperand InFlag(0, 0);
4693 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4694 Count, InFlag);
4695 InFlag = Chain.getValue(1);
4696 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004697 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698 InFlag = Chain.getValue(1);
4699 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004700 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 InFlag = Chain.getValue(1);
4702
4703 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4704 SmallVector<SDOperand, 8> Ops;
4705 Ops.push_back(Chain);
4706 Ops.push_back(DAG.getValueType(AVT));
4707 Ops.push_back(InFlag);
4708 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4709
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004710 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004711 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004712 unsigned Offset = Size - BytesLeft;
4713 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004714 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004715 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716 MVT::ValueType SrcVT = SrcAddr.getValueType();
4717 SDOperand Value;
4718 if (BytesLeft >= 4) {
4719 Value = DAG.getLoad(MVT::i32, Chain,
4720 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4721 DAG.getConstant(Offset, SrcVT)),
4722 NULL, 0);
4723 Chain = Value.getValue(1);
4724 Chain = DAG.getStore(Chain, Value,
4725 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4726 DAG.getConstant(Offset, DstVT)),
4727 NULL, 0);
4728 BytesLeft -= 4;
4729 Offset += 4;
4730 }
4731 if (BytesLeft >= 2) {
4732 Value = DAG.getLoad(MVT::i16, Chain,
4733 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4734 DAG.getConstant(Offset, SrcVT)),
4735 NULL, 0);
4736 Chain = Value.getValue(1);
4737 Chain = DAG.getStore(Chain, Value,
4738 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4739 DAG.getConstant(Offset, DstVT)),
4740 NULL, 0);
4741 BytesLeft -= 2;
4742 Offset += 2;
4743 }
4744
4745 if (BytesLeft == 1) {
4746 Value = DAG.getLoad(MVT::i8, Chain,
4747 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4748 DAG.getConstant(Offset, SrcVT)),
4749 NULL, 0);
4750 Chain = Value.getValue(1);
4751 Chain = DAG.getStore(Chain, Value,
4752 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4753 DAG.getConstant(Offset, DstVT)),
4754 NULL, 0);
4755 }
4756 }
4757
4758 return Chain;
4759}
4760
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004761/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4762SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004764 SDOperand TheChain = N->getOperand(0);
4765 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004766 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004767 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4768 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4769 MVT::i64, rax.getValue(2));
4770 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771 DAG.getConstant(32, MVT::i8));
4772 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004773 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774 };
4775
4776 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004777 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004778 }
4779
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004780 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4781 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4782 MVT::i32, eax.getValue(2));
4783 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4784 SDOperand Ops[] = { eax, edx };
4785 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4786
4787 // Use a MERGE_VALUES to return the value and chain.
4788 Ops[1] = edx.getValue(1);
4789 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4790 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004791}
4792
4793SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004794 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795
4796 if (!Subtarget->is64Bit()) {
4797 // vastart just stores the address of the VarArgsFrameIndex slot into the
4798 // memory location argument.
4799 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004800 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004801 }
4802
4803 // __va_list_tag:
4804 // gp_offset (0 - 6 * 8)
4805 // fp_offset (48 - 48 + 8 * 16)
4806 // overflow_arg_area (point to parameters coming in memory).
4807 // reg_save_area
4808 SmallVector<SDOperand, 8> MemOps;
4809 SDOperand FIN = Op.getOperand(1);
4810 // Store gp_offset
4811 SDOperand Store = DAG.getStore(Op.getOperand(0),
4812 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004813 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004814 MemOps.push_back(Store);
4815
4816 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004817 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818 Store = DAG.getStore(Op.getOperand(0),
4819 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004820 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004821 MemOps.push_back(Store);
4822
4823 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004824 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004826 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827 MemOps.push_back(Store);
4828
4829 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004830 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004831 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004832 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004833 MemOps.push_back(Store);
4834 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4835}
4836
4837SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4838 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4839 SDOperand Chain = Op.getOperand(0);
4840 SDOperand DstPtr = Op.getOperand(1);
4841 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004842 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4843 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004844
Dan Gohman12a9c082008-02-06 22:27:42 +00004845 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 Chain = SrcPtr.getValue(1);
4847 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004848 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004849 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004850 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851 if (i == 2)
4852 break;
4853 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004854 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004855 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004856 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004857 }
4858 return Chain;
4859}
4860
4861SDOperand
4862X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4863 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4864 switch (IntNo) {
4865 default: return SDOperand(); // Don't custom lower most intrinsics.
4866 // Comparison intrinsics.
4867 case Intrinsic::x86_sse_comieq_ss:
4868 case Intrinsic::x86_sse_comilt_ss:
4869 case Intrinsic::x86_sse_comile_ss:
4870 case Intrinsic::x86_sse_comigt_ss:
4871 case Intrinsic::x86_sse_comige_ss:
4872 case Intrinsic::x86_sse_comineq_ss:
4873 case Intrinsic::x86_sse_ucomieq_ss:
4874 case Intrinsic::x86_sse_ucomilt_ss:
4875 case Intrinsic::x86_sse_ucomile_ss:
4876 case Intrinsic::x86_sse_ucomigt_ss:
4877 case Intrinsic::x86_sse_ucomige_ss:
4878 case Intrinsic::x86_sse_ucomineq_ss:
4879 case Intrinsic::x86_sse2_comieq_sd:
4880 case Intrinsic::x86_sse2_comilt_sd:
4881 case Intrinsic::x86_sse2_comile_sd:
4882 case Intrinsic::x86_sse2_comigt_sd:
4883 case Intrinsic::x86_sse2_comige_sd:
4884 case Intrinsic::x86_sse2_comineq_sd:
4885 case Intrinsic::x86_sse2_ucomieq_sd:
4886 case Intrinsic::x86_sse2_ucomilt_sd:
4887 case Intrinsic::x86_sse2_ucomile_sd:
4888 case Intrinsic::x86_sse2_ucomigt_sd:
4889 case Intrinsic::x86_sse2_ucomige_sd:
4890 case Intrinsic::x86_sse2_ucomineq_sd: {
4891 unsigned Opc = 0;
4892 ISD::CondCode CC = ISD::SETCC_INVALID;
4893 switch (IntNo) {
4894 default: break;
4895 case Intrinsic::x86_sse_comieq_ss:
4896 case Intrinsic::x86_sse2_comieq_sd:
4897 Opc = X86ISD::COMI;
4898 CC = ISD::SETEQ;
4899 break;
4900 case Intrinsic::x86_sse_comilt_ss:
4901 case Intrinsic::x86_sse2_comilt_sd:
4902 Opc = X86ISD::COMI;
4903 CC = ISD::SETLT;
4904 break;
4905 case Intrinsic::x86_sse_comile_ss:
4906 case Intrinsic::x86_sse2_comile_sd:
4907 Opc = X86ISD::COMI;
4908 CC = ISD::SETLE;
4909 break;
4910 case Intrinsic::x86_sse_comigt_ss:
4911 case Intrinsic::x86_sse2_comigt_sd:
4912 Opc = X86ISD::COMI;
4913 CC = ISD::SETGT;
4914 break;
4915 case Intrinsic::x86_sse_comige_ss:
4916 case Intrinsic::x86_sse2_comige_sd:
4917 Opc = X86ISD::COMI;
4918 CC = ISD::SETGE;
4919 break;
4920 case Intrinsic::x86_sse_comineq_ss:
4921 case Intrinsic::x86_sse2_comineq_sd:
4922 Opc = X86ISD::COMI;
4923 CC = ISD::SETNE;
4924 break;
4925 case Intrinsic::x86_sse_ucomieq_ss:
4926 case Intrinsic::x86_sse2_ucomieq_sd:
4927 Opc = X86ISD::UCOMI;
4928 CC = ISD::SETEQ;
4929 break;
4930 case Intrinsic::x86_sse_ucomilt_ss:
4931 case Intrinsic::x86_sse2_ucomilt_sd:
4932 Opc = X86ISD::UCOMI;
4933 CC = ISD::SETLT;
4934 break;
4935 case Intrinsic::x86_sse_ucomile_ss:
4936 case Intrinsic::x86_sse2_ucomile_sd:
4937 Opc = X86ISD::UCOMI;
4938 CC = ISD::SETLE;
4939 break;
4940 case Intrinsic::x86_sse_ucomigt_ss:
4941 case Intrinsic::x86_sse2_ucomigt_sd:
4942 Opc = X86ISD::UCOMI;
4943 CC = ISD::SETGT;
4944 break;
4945 case Intrinsic::x86_sse_ucomige_ss:
4946 case Intrinsic::x86_sse2_ucomige_sd:
4947 Opc = X86ISD::UCOMI;
4948 CC = ISD::SETGE;
4949 break;
4950 case Intrinsic::x86_sse_ucomineq_ss:
4951 case Intrinsic::x86_sse2_ucomineq_sd:
4952 Opc = X86ISD::UCOMI;
4953 CC = ISD::SETNE;
4954 break;
4955 }
4956
4957 unsigned X86CC;
4958 SDOperand LHS = Op.getOperand(1);
4959 SDOperand RHS = Op.getOperand(2);
4960 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4961
Evan Cheng621216e2007-09-29 00:00:36 +00004962 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4963 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4964 DAG.getConstant(X86CC, MVT::i8), Cond);
4965 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004966 }
4967 }
4968}
4969
4970SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4971 // Depths > 0 not supported yet!
4972 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4973 return SDOperand();
4974
4975 // Just load the return address
4976 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4977 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4978}
4979
4980SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4981 // Depths > 0 not supported yet!
4982 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4983 return SDOperand();
4984
4985 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4986 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00004987 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004988}
4989
4990SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4991 SelectionDAG &DAG) {
4992 // Is not yet supported on x86-64
4993 if (Subtarget->is64Bit())
4994 return SDOperand();
4995
Chris Lattner5872a362008-01-17 07:00:52 +00004996 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004997}
4998
4999SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5000{
5001 assert(!Subtarget->is64Bit() &&
5002 "Lowering of eh_return builtin is not supported yet on x86-64");
5003
5004 MachineFunction &MF = DAG.getMachineFunction();
5005 SDOperand Chain = Op.getOperand(0);
5006 SDOperand Offset = Op.getOperand(1);
5007 SDOperand Handler = Op.getOperand(2);
5008
5009 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5010 getPointerTy());
5011
5012 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005013 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005014 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5015 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5016 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005017 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005018
5019 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5020 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5021}
5022
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005023SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5024 SelectionDAG &DAG) {
5025 SDOperand Root = Op.getOperand(0);
5026 SDOperand Trmp = Op.getOperand(1); // trampoline
5027 SDOperand FPtr = Op.getOperand(2); // nested function
5028 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5029
Dan Gohman12a9c082008-02-06 22:27:42 +00005030 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005031
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005032 const X86InstrInfo *TII =
5033 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5034
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005035 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005036 SDOperand OutChains[6];
5037
5038 // Large code-model.
5039
5040 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5041 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5042
5043 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005044 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005045 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005046 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005047
5048 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5049
5050 // Load the pointer to the nested function into R11.
5051 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5052 SDOperand Addr = Trmp;
5053 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005054 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005055
5056 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005057 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005058
5059 // Load the 'nest' parameter value into R10.
5060 // R10 is specified in X86CallingConv.td
5061 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5062 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5063 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005064 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005065
5066 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005067 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005068
5069 // Jump to the nested function.
5070 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5071 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5072 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005073 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005074
5075 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5076 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5077 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005078 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005079
5080 SDOperand Ops[] =
5081 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5082 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005083 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005084 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005085 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5086 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005087 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005088
5089 switch (CC) {
5090 default:
5091 assert(0 && "Unsupported calling convention");
5092 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005093 case CallingConv::X86_StdCall: {
5094 // Pass 'nest' parameter in ECX.
5095 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005096 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005097
5098 // Check that ECX wasn't needed by an 'inreg' parameter.
5099 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005100 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005101
5102 if (Attrs && !Func->isVarArg()) {
5103 unsigned InRegCount = 0;
5104 unsigned Idx = 1;
5105
5106 for (FunctionType::param_iterator I = FTy->param_begin(),
5107 E = FTy->param_end(); I != E; ++I, ++Idx)
5108 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5109 // FIXME: should only count parameters that are lowered to integers.
5110 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5111
5112 if (InRegCount > 2) {
5113 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5114 abort();
5115 }
5116 }
5117 break;
5118 }
5119 case CallingConv::X86_FastCall:
5120 // Pass 'nest' parameter in EAX.
5121 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005122 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005123 break;
5124 }
5125
5126 SDOperand OutChains[4];
5127 SDOperand Addr, Disp;
5128
5129 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5130 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5131
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005132 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5133 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005134 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005135 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005136 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005137
5138 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005139 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005140
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005141 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005142 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5143 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005144 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005145
5146 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005147 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005148
Duncan Sands7407a9f2007-09-11 14:10:23 +00005149 SDOperand Ops[] =
5150 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5151 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005152 }
5153}
5154
Dan Gohman819574c2008-01-31 00:41:03 +00005155SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005156 /*
5157 The rounding mode is in bits 11:10 of FPSR, and has the following
5158 settings:
5159 00 Round to nearest
5160 01 Round to -inf
5161 10 Round to +inf
5162 11 Round to 0
5163
5164 FLT_ROUNDS, on the other hand, expects the following:
5165 -1 Undefined
5166 0 Round to 0
5167 1 Round to nearest
5168 2 Round to +inf
5169 3 Round to -inf
5170
5171 To perform the conversion, we do:
5172 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5173 */
5174
5175 MachineFunction &MF = DAG.getMachineFunction();
5176 const TargetMachine &TM = MF.getTarget();
5177 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5178 unsigned StackAlignment = TFI.getStackAlignment();
5179 MVT::ValueType VT = Op.getValueType();
5180
5181 // Save FP Control Word to stack slot
5182 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5183 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5184
5185 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5186 DAG.getEntryNode(), StackSlot);
5187
5188 // Load FP Control Word from stack slot
5189 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5190
5191 // Transform as necessary
5192 SDOperand CWD1 =
5193 DAG.getNode(ISD::SRL, MVT::i16,
5194 DAG.getNode(ISD::AND, MVT::i16,
5195 CWD, DAG.getConstant(0x800, MVT::i16)),
5196 DAG.getConstant(11, MVT::i8));
5197 SDOperand CWD2 =
5198 DAG.getNode(ISD::SRL, MVT::i16,
5199 DAG.getNode(ISD::AND, MVT::i16,
5200 CWD, DAG.getConstant(0x400, MVT::i16)),
5201 DAG.getConstant(9, MVT::i8));
5202
5203 SDOperand RetVal =
5204 DAG.getNode(ISD::AND, MVT::i16,
5205 DAG.getNode(ISD::ADD, MVT::i16,
5206 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5207 DAG.getConstant(1, MVT::i16)),
5208 DAG.getConstant(3, MVT::i16));
5209
5210
5211 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5212 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5213}
5214
Evan Cheng48679f42007-12-14 02:13:44 +00005215SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5216 MVT::ValueType VT = Op.getValueType();
5217 MVT::ValueType OpVT = VT;
5218 unsigned NumBits = MVT::getSizeInBits(VT);
5219
5220 Op = Op.getOperand(0);
5221 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005222 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005223 OpVT = MVT::i32;
5224 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5225 }
Evan Cheng48679f42007-12-14 02:13:44 +00005226
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005227 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5228 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5229 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5230
5231 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5232 SmallVector<SDOperand, 4> Ops;
5233 Ops.push_back(Op);
5234 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5235 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5236 Ops.push_back(Op.getValue(1));
5237 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5238
5239 // Finally xor with NumBits-1.
5240 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5241
Evan Cheng48679f42007-12-14 02:13:44 +00005242 if (VT == MVT::i8)
5243 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5244 return Op;
5245}
5246
5247SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5248 MVT::ValueType VT = Op.getValueType();
5249 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005250 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005251
5252 Op = Op.getOperand(0);
5253 if (VT == MVT::i8) {
5254 OpVT = MVT::i32;
5255 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5256 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005257
5258 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5259 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5260 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5261
5262 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5263 SmallVector<SDOperand, 4> Ops;
5264 Ops.push_back(Op);
5265 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5266 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5267 Ops.push_back(Op.getValue(1));
5268 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5269
Evan Cheng48679f42007-12-14 02:13:44 +00005270 if (VT == MVT::i8)
5271 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5272 return Op;
5273}
5274
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005275/// LowerOperation - Provide custom lowering hooks for some operations.
5276///
5277SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5278 switch (Op.getOpcode()) {
5279 default: assert(0 && "Should not custom lower this!");
5280 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5281 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5282 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5283 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5284 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5285 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5286 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5287 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5288 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5289 case ISD::SHL_PARTS:
5290 case ISD::SRA_PARTS:
5291 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5292 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5293 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5294 case ISD::FABS: return LowerFABS(Op, DAG);
5295 case ISD::FNEG: return LowerFNEG(Op, DAG);
5296 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005297 case ISD::SETCC: return LowerSETCC(Op, DAG);
5298 case ISD::SELECT: return LowerSELECT(Op, DAG);
5299 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005300 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5301 case ISD::CALL: return LowerCALL(Op, DAG);
5302 case ISD::RET: return LowerRET(Op, DAG);
5303 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5304 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5305 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306 case ISD::VASTART: return LowerVASTART(Op, DAG);
5307 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5308 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5309 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5310 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5311 case ISD::FRAME_TO_ARGS_OFFSET:
5312 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5313 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5314 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005315 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005316 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005317 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5318 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005319
5320 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5321 case ISD::READCYCLECOUNTER:
5322 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005323 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005324}
5325
5326/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5327SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5328 switch (N->getOpcode()) {
5329 default: assert(0 && "Should not custom lower this!");
5330 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5331 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5332 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005333}
5334
5335const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5336 switch (Opcode) {
5337 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005338 case X86ISD::BSF: return "X86ISD::BSF";
5339 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005340 case X86ISD::SHLD: return "X86ISD::SHLD";
5341 case X86ISD::SHRD: return "X86ISD::SHRD";
5342 case X86ISD::FAND: return "X86ISD::FAND";
5343 case X86ISD::FOR: return "X86ISD::FOR";
5344 case X86ISD::FXOR: return "X86ISD::FXOR";
5345 case X86ISD::FSRL: return "X86ISD::FSRL";
5346 case X86ISD::FILD: return "X86ISD::FILD";
5347 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5348 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5349 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5350 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5351 case X86ISD::FLD: return "X86ISD::FLD";
5352 case X86ISD::FST: return "X86ISD::FST";
5353 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng931a8f42008-01-29 19:34:22 +00005354 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005355 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5356 case X86ISD::CALL: return "X86ISD::CALL";
5357 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5358 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5359 case X86ISD::CMP: return "X86ISD::CMP";
5360 case X86ISD::COMI: return "X86ISD::COMI";
5361 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5362 case X86ISD::SETCC: return "X86ISD::SETCC";
5363 case X86ISD::CMOV: return "X86ISD::CMOV";
5364 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5365 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5366 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5367 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005368 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5369 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005370 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005371 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005372 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5373 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005374 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5375 case X86ISD::FMAX: return "X86ISD::FMAX";
5376 case X86ISD::FMIN: return "X86ISD::FMIN";
5377 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5378 case X86ISD::FRCP: return "X86ISD::FRCP";
5379 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5380 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5381 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005382 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005383 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384 }
5385}
5386
5387// isLegalAddressingMode - Return true if the addressing mode represented
5388// by AM is legal for this target, for a load/store of the specified type.
5389bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5390 const Type *Ty) const {
5391 // X86 supports extremely general addressing modes.
5392
5393 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5394 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5395 return false;
5396
5397 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005398 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005399 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5400 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005401
5402 // X86-64 only supports addr of globals in small code model.
5403 if (Subtarget->is64Bit()) {
5404 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5405 return false;
5406 // If lower 4G is not available, then we must use rip-relative addressing.
5407 if (AM.BaseOffs || AM.Scale > 1)
5408 return false;
5409 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005410 }
5411
5412 switch (AM.Scale) {
5413 case 0:
5414 case 1:
5415 case 2:
5416 case 4:
5417 case 8:
5418 // These scales always work.
5419 break;
5420 case 3:
5421 case 5:
5422 case 9:
5423 // These scales are formed with basereg+scalereg. Only accept if there is
5424 // no basereg yet.
5425 if (AM.HasBaseReg)
5426 return false;
5427 break;
5428 default: // Other stuff never works.
5429 return false;
5430 }
5431
5432 return true;
5433}
5434
5435
Evan Cheng27a820a2007-10-26 01:56:11 +00005436bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5437 if (!Ty1->isInteger() || !Ty2->isInteger())
5438 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005439 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5440 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5441 if (NumBits1 <= NumBits2)
5442 return false;
5443 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005444}
5445
Evan Cheng9decb332007-10-29 19:58:20 +00005446bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5447 MVT::ValueType VT2) const {
5448 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5449 return false;
5450 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5451 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5452 if (NumBits1 <= NumBits2)
5453 return false;
5454 return Subtarget->is64Bit() || NumBits1 < 64;
5455}
Evan Cheng27a820a2007-10-26 01:56:11 +00005456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005457/// isShuffleMaskLegal - Targets can use this to indicate that they only
5458/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5459/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5460/// are assumed to be legal.
5461bool
5462X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5463 // Only do shuffles on 128-bit vector types for now.
5464 if (MVT::getSizeInBits(VT) == 64) return false;
5465 return (Mask.Val->getNumOperands() <= 4 ||
5466 isIdentityMask(Mask.Val) ||
5467 isIdentityMask(Mask.Val, true) ||
5468 isSplatMask(Mask.Val) ||
5469 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5470 X86::isUNPCKLMask(Mask.Val) ||
5471 X86::isUNPCKHMask(Mask.Val) ||
5472 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5473 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5474}
5475
5476bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5477 MVT::ValueType EVT,
5478 SelectionDAG &DAG) const {
5479 unsigned NumElts = BVOps.size();
5480 // Only do shuffles on 128-bit vector types for now.
5481 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5482 if (NumElts == 2) return true;
5483 if (NumElts == 4) {
5484 return (isMOVLMask(&BVOps[0], 4) ||
5485 isCommutedMOVL(&BVOps[0], 4, true) ||
5486 isSHUFPMask(&BVOps[0], 4) ||
5487 isCommutedSHUFP(&BVOps[0], 4));
5488 }
5489 return false;
5490}
5491
5492//===----------------------------------------------------------------------===//
5493// X86 Scheduler Hooks
5494//===----------------------------------------------------------------------===//
5495
5496MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005497X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5498 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005499 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5500 switch (MI->getOpcode()) {
5501 default: assert(false && "Unexpected instr type to insert");
5502 case X86::CMOV_FR32:
5503 case X86::CMOV_FR64:
5504 case X86::CMOV_V4F32:
5505 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005506 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507 // To "insert" a SELECT_CC instruction, we actually have to insert the
5508 // diamond control-flow pattern. The incoming instruction knows the
5509 // destination vreg to set, the condition code register to branch on, the
5510 // true/false values to select between, and a branch opcode to use.
5511 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5512 ilist<MachineBasicBlock>::iterator It = BB;
5513 ++It;
5514
5515 // thisMBB:
5516 // ...
5517 // TrueVal = ...
5518 // cmpTY ccX, r1, r2
5519 // bCC copy1MBB
5520 // fallthrough --> copy0MBB
5521 MachineBasicBlock *thisMBB = BB;
5522 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5523 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5524 unsigned Opc =
5525 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5526 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5527 MachineFunction *F = BB->getParent();
5528 F->getBasicBlockList().insert(It, copy0MBB);
5529 F->getBasicBlockList().insert(It, sinkMBB);
5530 // Update machine-CFG edges by first adding all successors of the current
5531 // block to the new block which will contain the Phi node for the select.
5532 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5533 e = BB->succ_end(); i != e; ++i)
5534 sinkMBB->addSuccessor(*i);
5535 // Next, remove all successors of the current block, and add the true
5536 // and fallthrough blocks as its successors.
5537 while(!BB->succ_empty())
5538 BB->removeSuccessor(BB->succ_begin());
5539 BB->addSuccessor(copy0MBB);
5540 BB->addSuccessor(sinkMBB);
5541
5542 // copy0MBB:
5543 // %FalseValue = ...
5544 // # fallthrough to sinkMBB
5545 BB = copy0MBB;
5546
5547 // Update machine-CFG edges
5548 BB->addSuccessor(sinkMBB);
5549
5550 // sinkMBB:
5551 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5552 // ...
5553 BB = sinkMBB;
5554 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5555 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5556 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5557
5558 delete MI; // The pseudo instruction is gone now.
5559 return BB;
5560 }
5561
5562 case X86::FP32_TO_INT16_IN_MEM:
5563 case X86::FP32_TO_INT32_IN_MEM:
5564 case X86::FP32_TO_INT64_IN_MEM:
5565 case X86::FP64_TO_INT16_IN_MEM:
5566 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005567 case X86::FP64_TO_INT64_IN_MEM:
5568 case X86::FP80_TO_INT16_IN_MEM:
5569 case X86::FP80_TO_INT32_IN_MEM:
5570 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 // Change the floating point control register to use "round towards zero"
5572 // mode when truncating to an integer value.
5573 MachineFunction *F = BB->getParent();
5574 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5575 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5576
5577 // Load the old value of the high byte of the control word...
5578 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005579 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005580 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5581
5582 // Set the high part to be round to zero...
5583 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5584 .addImm(0xC7F);
5585
5586 // Reload the modified control word now...
5587 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5588
5589 // Restore the memory image of control word to original value
5590 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5591 .addReg(OldCW);
5592
5593 // Get the X86 opcode to use.
5594 unsigned Opc;
5595 switch (MI->getOpcode()) {
5596 default: assert(0 && "illegal opcode!");
5597 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5598 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5599 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5600 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5601 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5602 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005603 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5604 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5605 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005606 }
5607
5608 X86AddressMode AM;
5609 MachineOperand &Op = MI->getOperand(0);
5610 if (Op.isRegister()) {
5611 AM.BaseType = X86AddressMode::RegBase;
5612 AM.Base.Reg = Op.getReg();
5613 } else {
5614 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005615 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005616 }
5617 Op = MI->getOperand(1);
5618 if (Op.isImmediate())
5619 AM.Scale = Op.getImm();
5620 Op = MI->getOperand(2);
5621 if (Op.isImmediate())
5622 AM.IndexReg = Op.getImm();
5623 Op = MI->getOperand(3);
5624 if (Op.isGlobalAddress()) {
5625 AM.GV = Op.getGlobal();
5626 } else {
5627 AM.Disp = Op.getImm();
5628 }
5629 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5630 .addReg(MI->getOperand(4).getReg());
5631
5632 // Reload the original control word now.
5633 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5634
5635 delete MI; // The pseudo instruction is gone now.
5636 return BB;
5637 }
5638 }
5639}
5640
5641//===----------------------------------------------------------------------===//
5642// X86 Optimization Hooks
5643//===----------------------------------------------------------------------===//
5644
5645void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005646 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005647 APInt &KnownZero,
5648 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005649 const SelectionDAG &DAG,
5650 unsigned Depth) const {
5651 unsigned Opc = Op.getOpcode();
5652 assert((Opc >= ISD::BUILTIN_OP_END ||
5653 Opc == ISD::INTRINSIC_WO_CHAIN ||
5654 Opc == ISD::INTRINSIC_W_CHAIN ||
5655 Opc == ISD::INTRINSIC_VOID) &&
5656 "Should use MaskedValueIsZero if you don't know whether Op"
5657 " is a target node!");
5658
Dan Gohman1d79e432008-02-13 23:07:24 +00005659 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005660 switch (Opc) {
5661 default: break;
5662 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005663 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5664 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005665 break;
5666 }
5667}
5668
5669/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5670/// element of the result of the vector shuffle.
5671static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5672 MVT::ValueType VT = N->getValueType(0);
5673 SDOperand PermMask = N->getOperand(2);
5674 unsigned NumElems = PermMask.getNumOperands();
5675 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5676 i %= NumElems;
5677 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5678 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005679 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005680 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5681 SDOperand Idx = PermMask.getOperand(i);
5682 if (Idx.getOpcode() == ISD::UNDEF)
5683 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5684 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5685 }
5686 return SDOperand();
5687}
5688
5689/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5690/// node is a GlobalAddress + an offset.
5691static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5692 unsigned Opc = N->getOpcode();
5693 if (Opc == X86ISD::Wrapper) {
5694 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5695 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5696 return true;
5697 }
5698 } else if (Opc == ISD::ADD) {
5699 SDOperand N1 = N->getOperand(0);
5700 SDOperand N2 = N->getOperand(1);
5701 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5702 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5703 if (V) {
5704 Offset += V->getSignExtended();
5705 return true;
5706 }
5707 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5708 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5709 if (V) {
5710 Offset += V->getSignExtended();
5711 return true;
5712 }
5713 }
5714 }
5715 return false;
5716}
5717
5718/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5719/// + Dist * Size.
5720static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5721 MachineFrameInfo *MFI) {
5722 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5723 return false;
5724
5725 SDOperand Loc = N->getOperand(1);
5726 SDOperand BaseLoc = Base->getOperand(1);
5727 if (Loc.getOpcode() == ISD::FrameIndex) {
5728 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5729 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005730 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5731 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 int FS = MFI->getObjectSize(FI);
5733 int BFS = MFI->getObjectSize(BFI);
5734 if (FS != BFS || FS != Size) return false;
5735 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5736 } else {
5737 GlobalValue *GV1 = NULL;
5738 GlobalValue *GV2 = NULL;
5739 int64_t Offset1 = 0;
5740 int64_t Offset2 = 0;
5741 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5742 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5743 if (isGA1 && isGA2 && GV1 == GV2)
5744 return Offset1 == (Offset2 + Dist*Size);
5745 }
5746
5747 return false;
5748}
5749
5750static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5751 const X86Subtarget *Subtarget) {
5752 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005753 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005754 if (isGAPlusOffset(Base, GV, Offset))
5755 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005756 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005757 return false;
5758}
5759
5760
5761/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5762/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5763/// if the load addresses are consecutive, non-overlapping, and in the right
5764/// order.
5765static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5766 const X86Subtarget *Subtarget) {
5767 MachineFunction &MF = DAG.getMachineFunction();
5768 MachineFrameInfo *MFI = MF.getFrameInfo();
5769 MVT::ValueType VT = N->getValueType(0);
5770 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5771 SDOperand PermMask = N->getOperand(2);
5772 int NumElems = (int)PermMask.getNumOperands();
5773 SDNode *Base = NULL;
5774 for (int i = 0; i < NumElems; ++i) {
5775 SDOperand Idx = PermMask.getOperand(i);
5776 if (Idx.getOpcode() == ISD::UNDEF) {
5777 if (!Base) return SDOperand();
5778 } else {
5779 SDOperand Arg =
5780 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5781 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5782 return SDOperand();
5783 if (!Base)
5784 Base = Arg.Val;
5785 else if (!isConsecutiveLoad(Arg.Val, Base,
5786 i, MVT::getSizeInBits(EVT)/8,MFI))
5787 return SDOperand();
5788 }
5789 }
5790
5791 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005792 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005793 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005794 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005795 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005796 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005797 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5798 LD->getSrcValueOffset(), LD->isVolatile(),
5799 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800 }
5801}
5802
5803/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5804static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5805 const X86Subtarget *Subtarget) {
5806 SDOperand Cond = N->getOperand(0);
5807
5808 // If we have SSE[12] support, try to form min/max nodes.
5809 if (Subtarget->hasSSE2() &&
5810 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5811 if (Cond.getOpcode() == ISD::SETCC) {
5812 // Get the LHS/RHS of the select.
5813 SDOperand LHS = N->getOperand(1);
5814 SDOperand RHS = N->getOperand(2);
5815 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5816
5817 unsigned Opcode = 0;
5818 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5819 switch (CC) {
5820 default: break;
5821 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5822 case ISD::SETULE:
5823 case ISD::SETLE:
5824 if (!UnsafeFPMath) break;
5825 // FALL THROUGH.
5826 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5827 case ISD::SETLT:
5828 Opcode = X86ISD::FMIN;
5829 break;
5830
5831 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5832 case ISD::SETUGT:
5833 case ISD::SETGT:
5834 if (!UnsafeFPMath) break;
5835 // FALL THROUGH.
5836 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5837 case ISD::SETGE:
5838 Opcode = X86ISD::FMAX;
5839 break;
5840 }
5841 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5842 switch (CC) {
5843 default: break;
5844 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5845 case ISD::SETUGT:
5846 case ISD::SETGT:
5847 if (!UnsafeFPMath) break;
5848 // FALL THROUGH.
5849 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5850 case ISD::SETGE:
5851 Opcode = X86ISD::FMIN;
5852 break;
5853
5854 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5855 case ISD::SETULE:
5856 case ISD::SETLE:
5857 if (!UnsafeFPMath) break;
5858 // FALL THROUGH.
5859 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5860 case ISD::SETLT:
5861 Opcode = X86ISD::FMAX;
5862 break;
5863 }
5864 }
5865
5866 if (Opcode)
5867 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5868 }
5869
5870 }
5871
5872 return SDOperand();
5873}
5874
Chris Lattner470d5dc2008-01-25 06:14:17 +00005875/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
5876/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00005877static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00005878 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
5879 // F[X]OR(0.0, x) -> x
5880 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00005881 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5882 if (C->getValueAPF().isPosZero())
5883 return N->getOperand(1);
5884 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5885 if (C->getValueAPF().isPosZero())
5886 return N->getOperand(0);
5887 return SDOperand();
5888}
5889
5890/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
5891static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
5892 // FAND(0.0, x) -> 0.0
5893 // FAND(x, 0.0) -> 0.0
5894 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5895 if (C->getValueAPF().isPosZero())
5896 return N->getOperand(0);
5897 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5898 if (C->getValueAPF().isPosZero())
5899 return N->getOperand(1);
5900 return SDOperand();
5901}
5902
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005903
5904SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5905 DAGCombinerInfo &DCI) const {
5906 SelectionDAG &DAG = DCI.DAG;
5907 switch (N->getOpcode()) {
5908 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00005909 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
5910 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00005911 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00005912 case X86ISD::FOR: return PerformFORCombine(N, DAG);
5913 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005914 }
5915
5916 return SDOperand();
5917}
5918
5919//===----------------------------------------------------------------------===//
5920// X86 Inline Assembly Support
5921//===----------------------------------------------------------------------===//
5922
5923/// getConstraintType - Given a constraint letter, return the type of
5924/// constraint it is for this target.
5925X86TargetLowering::ConstraintType
5926X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5927 if (Constraint.size() == 1) {
5928 switch (Constraint[0]) {
5929 case 'A':
5930 case 'r':
5931 case 'R':
5932 case 'l':
5933 case 'q':
5934 case 'Q':
5935 case 'x':
5936 case 'Y':
5937 return C_RegisterClass;
5938 default:
5939 break;
5940 }
5941 }
5942 return TargetLowering::getConstraintType(Constraint);
5943}
5944
Dale Johannesene99fc902008-01-29 02:21:21 +00005945/// LowerXConstraint - try to replace an X constraint, which matches anything,
5946/// with another that has more specific requirements based on the type of the
5947/// corresponding operand.
5948void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
5949 std::string& s) const {
5950 if (MVT::isFloatingPoint(ConstraintVT)) {
5951 if (Subtarget->hasSSE2())
5952 s = "Y";
5953 else if (Subtarget->hasSSE1())
5954 s = "x";
5955 else
5956 s = "f";
5957 } else
5958 return TargetLowering::lowerXConstraint(ConstraintVT, s);
5959}
5960
Chris Lattnera531abc2007-08-25 00:47:38 +00005961/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5962/// vector. If it is invalid, don't add anything to Ops.
5963void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5964 char Constraint,
5965 std::vector<SDOperand>&Ops,
5966 SelectionDAG &DAG) {
5967 SDOperand Result(0, 0);
5968
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005969 switch (Constraint) {
5970 default: break;
5971 case 'I':
5972 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005973 if (C->getValue() <= 31) {
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 'N':
5980 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005981 if (C->getValue() <= 255) {
5982 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5983 break;
5984 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005985 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005986 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005987 case 'i': {
5988 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00005989 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5990 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5991 break;
5992 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005993
5994 // If we are in non-pic codegen mode, we allow the address of a global (with
5995 // an optional displacement) to be used with 'i'.
5996 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5997 int64_t Offset = 0;
5998
5999 // Match either (GA) or (GA+C)
6000 if (GA) {
6001 Offset = GA->getOffset();
6002 } else if (Op.getOpcode() == ISD::ADD) {
6003 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6004 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6005 if (C && GA) {
6006 Offset = GA->getOffset()+C->getValue();
6007 } else {
6008 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6009 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6010 if (C && GA)
6011 Offset = GA->getOffset()+C->getValue();
6012 else
6013 C = 0, GA = 0;
6014 }
6015 }
6016
6017 if (GA) {
6018 // If addressing this global requires a load (e.g. in PIC mode), we can't
6019 // match.
6020 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6021 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006022 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006023
6024 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6025 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006026 Result = Op;
6027 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006028 }
6029
6030 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006031 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006032 }
6033 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006034
6035 if (Result.Val) {
6036 Ops.push_back(Result);
6037 return;
6038 }
6039 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006040}
6041
6042std::vector<unsigned> X86TargetLowering::
6043getRegClassForInlineAsmConstraint(const std::string &Constraint,
6044 MVT::ValueType VT) const {
6045 if (Constraint.size() == 1) {
6046 // FIXME: not handling fp-stack yet!
6047 switch (Constraint[0]) { // GCC X86 Constraint Letters
6048 default: break; // Unknown constraint letter
6049 case 'A': // EAX/EDX
6050 if (VT == MVT::i32 || VT == MVT::i64)
6051 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6052 break;
6053 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6054 case 'Q': // Q_REGS
6055 if (VT == MVT::i32)
6056 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6057 else if (VT == MVT::i16)
6058 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6059 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006060 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006061 else if (VT == MVT::i64)
6062 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6063 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006064 }
6065 }
6066
6067 return std::vector<unsigned>();
6068}
6069
6070std::pair<unsigned, const TargetRegisterClass*>
6071X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6072 MVT::ValueType VT) const {
6073 // First, see if this is a constraint that directly corresponds to an LLVM
6074 // register class.
6075 if (Constraint.size() == 1) {
6076 // GCC Constraint Letters
6077 switch (Constraint[0]) {
6078 default: break;
6079 case 'r': // GENERAL_REGS
6080 case 'R': // LEGACY_REGS
6081 case 'l': // INDEX_REGS
6082 if (VT == MVT::i64 && Subtarget->is64Bit())
6083 return std::make_pair(0U, X86::GR64RegisterClass);
6084 if (VT == MVT::i32)
6085 return std::make_pair(0U, X86::GR32RegisterClass);
6086 else if (VT == MVT::i16)
6087 return std::make_pair(0U, X86::GR16RegisterClass);
6088 else if (VT == MVT::i8)
6089 return std::make_pair(0U, X86::GR8RegisterClass);
6090 break;
6091 case 'y': // MMX_REGS if MMX allowed.
6092 if (!Subtarget->hasMMX()) break;
6093 return std::make_pair(0U, X86::VR64RegisterClass);
6094 break;
6095 case 'Y': // SSE_REGS if SSE2 allowed
6096 if (!Subtarget->hasSSE2()) break;
6097 // FALL THROUGH.
6098 case 'x': // SSE_REGS if SSE1 allowed
6099 if (!Subtarget->hasSSE1()) break;
6100
6101 switch (VT) {
6102 default: break;
6103 // Scalar SSE types.
6104 case MVT::f32:
6105 case MVT::i32:
6106 return std::make_pair(0U, X86::FR32RegisterClass);
6107 case MVT::f64:
6108 case MVT::i64:
6109 return std::make_pair(0U, X86::FR64RegisterClass);
6110 // Vector types.
6111 case MVT::v16i8:
6112 case MVT::v8i16:
6113 case MVT::v4i32:
6114 case MVT::v2i64:
6115 case MVT::v4f32:
6116 case MVT::v2f64:
6117 return std::make_pair(0U, X86::VR128RegisterClass);
6118 }
6119 break;
6120 }
6121 }
6122
6123 // Use the default implementation in TargetLowering to convert the register
6124 // constraint into a member of a register class.
6125 std::pair<unsigned, const TargetRegisterClass*> Res;
6126 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6127
6128 // Not found as a standard register?
6129 if (Res.second == 0) {
6130 // GCC calls "st(0)" just plain "st".
6131 if (StringsEqualNoCase("{st}", Constraint)) {
6132 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006133 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006134 }
6135
6136 return Res;
6137 }
6138
6139 // Otherwise, check to see if this is a register class of the wrong value
6140 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6141 // turn into {ax},{dx}.
6142 if (Res.second->hasType(VT))
6143 return Res; // Correct type already, nothing to do.
6144
6145 // All of the single-register GCC register classes map their values onto
6146 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6147 // really want an 8-bit or 32-bit register, map to the appropriate register
6148 // class and return the appropriate register.
6149 if (Res.second != X86::GR16RegisterClass)
6150 return Res;
6151
6152 if (VT == MVT::i8) {
6153 unsigned DestReg = 0;
6154 switch (Res.first) {
6155 default: break;
6156 case X86::AX: DestReg = X86::AL; break;
6157 case X86::DX: DestReg = X86::DL; break;
6158 case X86::CX: DestReg = X86::CL; break;
6159 case X86::BX: DestReg = X86::BL; break;
6160 }
6161 if (DestReg) {
6162 Res.first = DestReg;
6163 Res.second = Res.second = X86::GR8RegisterClass;
6164 }
6165 } else if (VT == MVT::i32) {
6166 unsigned DestReg = 0;
6167 switch (Res.first) {
6168 default: break;
6169 case X86::AX: DestReg = X86::EAX; break;
6170 case X86::DX: DestReg = X86::EDX; break;
6171 case X86::CX: DestReg = X86::ECX; break;
6172 case X86::BX: DestReg = X86::EBX; break;
6173 case X86::SI: DestReg = X86::ESI; break;
6174 case X86::DI: DestReg = X86::EDI; break;
6175 case X86::BP: DestReg = X86::EBP; break;
6176 case X86::SP: DestReg = X86::ESP; break;
6177 }
6178 if (DestReg) {
6179 Res.first = DestReg;
6180 Res.second = Res.second = X86::GR32RegisterClass;
6181 }
6182 } else if (VT == MVT::i64) {
6183 unsigned DestReg = 0;
6184 switch (Res.first) {
6185 default: break;
6186 case X86::AX: DestReg = X86::RAX; break;
6187 case X86::DX: DestReg = X86::RDX; break;
6188 case X86::CX: DestReg = X86::RCX; break;
6189 case X86::BX: DestReg = X86::RBX; break;
6190 case X86::SI: DestReg = X86::RSI; break;
6191 case X86::DI: DestReg = X86::RDI; break;
6192 case X86::BP: DestReg = X86::RBP; break;
6193 case X86::SP: DestReg = X86::RSP; break;
6194 }
6195 if (DestReg) {
6196 Res.first = DestReg;
6197 Res.second = Res.second = X86::GR64RegisterClass;
6198 }
6199 }
6200
6201 return Res;
6202}