blob: 5bd9cf85c2ea0a9aeebc2286a52835af667fe317 [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"
Dale Johannesen98738822008-02-22 22:17:59 +000042#include "llvm/ParamAttrsList.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);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000707 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708
709 computeRegisterProperties();
710
711 // FIXME: These should be based on subtarget info. Plus, the values should
712 // be smaller when we are in optimizing for size mode.
713 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
714 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
715 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
716 allowUnalignedMemoryAccesses = true; // x86 supports it!
717}
718
Evan Cheng5a67b812008-01-23 23:17:41 +0000719/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
720/// the desired ByVal argument alignment.
721static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
722 if (MaxAlign == 16)
723 return;
724 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
725 if (VTy->getBitWidth() == 128)
726 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000727 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
728 unsigned EltAlign = 0;
729 getMaxByValAlign(ATy->getElementType(), EltAlign);
730 if (EltAlign > MaxAlign)
731 MaxAlign = EltAlign;
732 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
733 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
734 unsigned EltAlign = 0;
735 getMaxByValAlign(STy->getElementType(i), EltAlign);
736 if (EltAlign > MaxAlign)
737 MaxAlign = EltAlign;
738 if (MaxAlign == 16)
739 break;
740 }
741 }
742 return;
743}
744
745/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
746/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000747/// that contain SSE vectors are placed at 16-byte boundaries while the rest
748/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000749unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
750 if (Subtarget->is64Bit())
751 return getTargetData()->getABITypeAlignment(Ty);
752 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000753 if (Subtarget->hasSSE1())
754 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000755 return Align;
756}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757
Evan Cheng6fb06762007-11-09 01:32:10 +0000758/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
759/// jumptable.
760SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
761 SelectionDAG &DAG) const {
762 if (usesGlobalOffsetTable())
763 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
764 if (!Subtarget->isPICStyleRIPRel())
765 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
766 return Table;
767}
768
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769//===----------------------------------------------------------------------===//
770// Return Value Calling Convention Implementation
771//===----------------------------------------------------------------------===//
772
773#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000774
775/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
776/// exists skip possible ISD:TokenFactor.
777static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000778 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000779 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000780 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000781 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000782 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000783 return Chain.getOperand(0);
784 }
785 return Chain;
786}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000787
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788/// LowerRET - Lower an ISD::RET node.
789SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
790 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
791
792 SmallVector<CCValAssign, 16> RVLocs;
793 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
794 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
795 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
796 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000797
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 // If this is the first return lowered for this function, add the regs to the
799 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000800 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 for (unsigned i = 0; i != RVLocs.size(); ++i)
802 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000803 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000807 // Handle tail call return.
808 Chain = GetPossiblePreceedingTailCall(Chain);
809 if (Chain.getOpcode() == X86ISD::TAILCALL) {
810 SDOperand TailCall = Chain;
811 SDOperand TargetAddress = TailCall.getOperand(1);
812 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000813 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000814 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
815 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
816 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
817 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
818 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000819 assert(StackAdjustment.getOpcode() == ISD::Constant &&
820 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000821
822 SmallVector<SDOperand,8> Operands;
823 Operands.push_back(Chain.getOperand(0));
824 Operands.push_back(TargetAddress);
825 Operands.push_back(StackAdjustment);
826 // Copy registers used by the call. Last operand is a flag so it is not
827 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000828 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000829 Operands.push_back(Chain.getOperand(i));
830 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000831 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
832 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000833 }
834
835 // Regular return.
836 SDOperand Flag;
837
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 // Copy the result values into the output registers.
839 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
840 RVLocs[0].getLocReg() != X86::ST0) {
841 for (unsigned i = 0; i != RVLocs.size(); ++i) {
842 CCValAssign &VA = RVLocs[i];
843 assert(VA.isRegLoc() && "Can only return in registers!");
844 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
845 Flag);
846 Flag = Chain.getValue(1);
847 }
848 } else {
849 // We need to handle a destination of ST0 specially, because it isn't really
850 // a register.
851 SDOperand Value = Op.getOperand(1);
852
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000853 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
854 // This will get legalized into a load/store if it can't get optimized away.
855 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
856 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857
858 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
859 SDOperand Ops[] = { Chain, Value };
860 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
861 Flag = Chain.getValue(1);
862 }
863
864 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
865 if (Flag.Val)
866 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
867 else
868 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
869}
870
871
872/// LowerCallResult - Lower the result values of an ISD::CALL into the
873/// appropriate copies out of appropriate physical registers. This assumes that
874/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
875/// being lowered. The returns a SDNode with the same number of values as the
876/// ISD::CALL.
877SDNode *X86TargetLowering::
878LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
879 unsigned CallingConv, SelectionDAG &DAG) {
880
881 // Assign locations to each value returned by this call.
882 SmallVector<CCValAssign, 16> RVLocs;
883 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
884 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
885 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
886
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 SmallVector<SDOperand, 8> ResultVals;
888
889 // Copy all of the result registers out of their specified physreg.
890 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
891 for (unsigned i = 0; i != RVLocs.size(); ++i) {
892 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
893 RVLocs[i].getValVT(), InFlag).getValue(1);
894 InFlag = Chain.getValue(2);
895 ResultVals.push_back(Chain.getValue(0));
896 }
897 } else {
898 // Copies from the FP stack are special, as ST0 isn't a valid register
899 // before the fp stackifier runs.
900
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000901 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
902 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
903 // the specified value type.
904 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
905 if (isScalarFPTypeInSSEReg(GetResultTy))
906 GetResultTy = MVT::f80;
907 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
908
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 SDOperand GROps[] = { Chain, InFlag };
910 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
911 Chain = RetVal.getValue(1);
912 InFlag = RetVal.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000913
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000914 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
915 // there.
916 if (GetResultTy != RVLocs[0].getValVT())
917 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
918 // This truncation won't change the value.
919 DAG.getIntPtrConstant(1));
920
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 ResultVals.push_back(RetVal);
922 }
923
924 // Merge everything together with a MERGE_VALUES node.
925 ResultVals.push_back(Chain);
926 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
927 &ResultVals[0], ResultVals.size()).Val;
928}
929
Evan Cheng931a8f42008-01-29 19:34:22 +0000930/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
931/// ISD::CALL where the results are known to be in two 64-bit registers,
932/// e.g. XMM0 and XMM1. This simplify store the two values back to the
933/// fixed stack slot allocated for StructRet.
934SDNode *X86TargetLowering::
935LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
936 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
937 MVT::ValueType VT, SelectionDAG &DAG) {
938 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
939 Chain = RetVal1.getValue(1);
940 InFlag = RetVal1.getValue(2);
941 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
942 Chain = RetVal2.getValue(1);
943 InFlag = RetVal2.getValue(2);
944 SDOperand FIN = TheCall->getOperand(5);
945 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
946 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
947 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
948 return Chain.Val;
949}
950
951/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
952/// where the results are known to be in ST0 and ST1.
953SDNode *X86TargetLowering::
954LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
955 SDNode *TheCall, SelectionDAG &DAG) {
956 SmallVector<SDOperand, 8> ResultVals;
957 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
958 SDVTList Tys = DAG.getVTList(VTs, 4);
959 SDOperand Ops[] = { Chain, InFlag };
960 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
961 Chain = RetVal.getValue(2);
962 SDOperand FIN = TheCall->getOperand(5);
963 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
964 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
965 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
966 return Chain.Val;
967}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968
969//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000970// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971//===----------------------------------------------------------------------===//
972// StdCall calling convention seems to be standard for many Windows' API
973// routines and around. It differs from C calling convention just a little:
974// callee should clean up the stack, not caller. Symbols should be also
975// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000976// For info on fast calling convention see Fast Calling Convention (tail call)
977// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978
979/// AddLiveIn - This helper function adds the specified physical register to the
980/// MachineFunction as a live in value. It also creates a corresponding virtual
981/// register for it.
982static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
983 const TargetRegisterClass *RC) {
984 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000985 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
986 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 return VReg;
988}
989
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000990/// CallIsStructReturn - Determines whether a CALL node uses struct return
991/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000992static bool CallIsStructReturn(SDOperand Op) {
993 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
994 if (!NumOps)
995 return false;
996
997 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
998 return Flags->getValue() & ISD::ParamFlags::StructReturn;
999}
1000
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001001/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1002/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001003static bool ArgsAreStructReturn(SDOperand Op) {
1004 unsigned NumArgs = Op.Val->getNumValues() - 1;
1005 if (!NumArgs)
1006 return false;
1007
1008 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1009 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1010}
1011
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001012/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1013/// callee to pop its own arguments. Callee pop is necessary to support tail
1014/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001015bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1016 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1017 if (IsVarArg)
1018 return false;
1019
1020 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1021 default:
1022 return false;
1023 case CallingConv::X86_StdCall:
1024 return !Subtarget->is64Bit();
1025 case CallingConv::X86_FastCall:
1026 return !Subtarget->is64Bit();
1027 case CallingConv::Fast:
1028 return PerformTailCallOpt;
1029 }
1030}
1031
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001032/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1033/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001034CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1035 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1036
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001037 if (Subtarget->is64Bit()) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001038 if (CC == CallingConv::Fast && PerformTailCallOpt)
1039 return CC_X86_64_TailCall;
1040 else
1041 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001042 }
1043
Gordon Henriksen18ace102008-01-05 16:56:59 +00001044 if (CC == CallingConv::X86_FastCall)
1045 return CC_X86_32_FastCall;
1046 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1047 return CC_X86_32_TailCall;
1048 else
1049 return CC_X86_32_C;
1050}
1051
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001052/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1053/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001054NameDecorationStyle
1055X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1056 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1057 if (CC == CallingConv::X86_FastCall)
1058 return FastCall;
1059 else if (CC == CallingConv::X86_StdCall)
1060 return StdCall;
1061 return None;
1062}
1063
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001064/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1065/// possibly be overwritten when lowering the outgoing arguments in a tail
1066/// call. Currently the implementation of this call is very conservative and
1067/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1068/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001069static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1070 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001071 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001072 FrameIndexSDNode * FrameIdxNode = NULL;
1073 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001074 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1075 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001076 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1077 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1078 (Op.getOpcode() == ISD::LOAD &&
1079 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1080 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1081 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001082 return true;
1083 return false;
1084}
1085
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001086/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1087/// arguments to force loading and guarantee that arguments sourcing from
1088/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001089static SDOperand
1090CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1091 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1092 SelectionDAG &DAG,
1093 MachineFunction &MF,
1094 const TargetLowering * TL) {
1095
1096 SDOperand InFlag;
1097 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1098 SDOperand Arg = TailCallClobberedVRegs[i].second;
1099 unsigned Idx = TailCallClobberedVRegs[i].first;
1100 unsigned VReg =
1101 MF.getRegInfo().
1102 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1103 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1104 InFlag = Chain.getValue(1);
1105 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1106 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1107 Chain = Arg.getValue(1);
1108 InFlag = Arg.getValue(2);
1109 }
1110 return Chain;
1111}
1112
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001113/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1114/// by "Src" to address "Dst" with size and alignment information specified by
1115/// the specific parameter attribute. The copy will be passed as a byval function
1116/// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001117static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001118CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1119 unsigned Flags, SelectionDAG &DAG) {
1120 unsigned Align = 1 <<
1121 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1122 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001123 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001124 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1125 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001126 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001127 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001128}
1129
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001130SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1131 const CCValAssign &VA,
1132 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001133 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001134 SDOperand Root, unsigned i) {
1135 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001136 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001137 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Evan Cheng3e42a522008-01-10 02:24:25 +00001138 bool isByVal = Flags & ISD::ParamFlags::ByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001139 bool isImmutable = !AlwaysUseMutable && !isByVal;
Evan Cheng3e42a522008-01-10 02:24:25 +00001140
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001141 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1142 // changed with more analysis.
1143 // In case of tail call optimization mark all arguments mutable. Since they
1144 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001145 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001146 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001147 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001148 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001149 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001150 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001151 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001152}
1153
Gordon Henriksen18ace102008-01-05 16:56:59 +00001154SDOperand
1155X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001157 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1158
1159 const Function* Fn = MF.getFunction();
1160 if (Fn->hasExternalLinkage() &&
1161 Subtarget->isTargetCygMing() &&
1162 Fn->getName() == "main")
1163 FuncInfo->setForceFramePointer(true);
1164
1165 // Decorate the function name.
1166 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1167
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168 MachineFrameInfo *MFI = MF.getFrameInfo();
1169 SDOperand Root = Op.getOperand(0);
1170 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001171 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001172 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001173
1174 assert(!(isVarArg && CC == CallingConv::Fast) &&
1175 "Var args not supported with calling convention fastcc");
1176
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 // Assign locations to all of the incoming arguments.
1178 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001179 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001180 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001182 SmallVector<SDOperand, 8> ArgValues;
1183 unsigned LastVal = ~0U;
1184 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1185 CCValAssign &VA = ArgLocs[i];
1186 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1187 // places.
1188 assert(VA.getValNo() != LastVal &&
1189 "Don't support value assigned to multiple locs yet");
1190 LastVal = VA.getValNo();
1191
1192 if (VA.isRegLoc()) {
1193 MVT::ValueType RegVT = VA.getLocVT();
1194 TargetRegisterClass *RC;
1195 if (RegVT == MVT::i32)
1196 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001197 else if (Is64Bit && RegVT == MVT::i64)
1198 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001199 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001200 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001201 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001202 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 else {
1204 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001205 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1206 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1207 RegVT = MVT::i64;
1208 } else
1209 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001210 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1213 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1214
1215 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1216 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1217 // right size.
1218 if (VA.getLocInfo() == CCValAssign::SExt)
1219 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1220 DAG.getValueType(VA.getValVT()));
1221 else if (VA.getLocInfo() == CCValAssign::ZExt)
1222 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1223 DAG.getValueType(VA.getValVT()));
1224
1225 if (VA.getLocInfo() != CCValAssign::Full)
1226 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1227
Gordon Henriksen18ace102008-01-05 16:56:59 +00001228 // Handle MMX values passed in GPRs.
1229 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1230 MVT::getSizeInBits(RegVT) == 64)
1231 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 ArgValues.push_back(ArgValue);
1234 } else {
1235 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001236 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 }
1238 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001241 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001242 if (CC == CallingConv::Fast)
1243 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244
1245 // If the function takes variable number of arguments, make a frame index for
1246 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001247 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001248 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1249 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1250 }
1251 if (Is64Bit) {
1252 static const unsigned GPR64ArgRegs[] = {
1253 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1254 };
1255 static const unsigned XMMArgRegs[] = {
1256 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1257 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1258 };
1259
1260 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1261 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1262
1263 // For X86-64, if there are vararg parameters that are passed via
1264 // registers, then we must store them to their spots on the stack so they
1265 // may be loaded by deferencing the result of va_next.
1266 VarArgsGPOffset = NumIntRegs * 8;
1267 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1268 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1269
1270 // Store the integer parameter registers.
1271 SmallVector<SDOperand, 8> MemOps;
1272 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1273 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001274 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001275 for (; NumIntRegs != 6; ++NumIntRegs) {
1276 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1277 X86::GR64RegisterClass);
1278 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001279 SDOperand Store =
1280 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001281 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001282 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001283 MemOps.push_back(Store);
1284 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001285 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001286 }
1287
1288 // Now store the XMM (fp + vector) parameter registers.
1289 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001290 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001291 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1292 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1293 X86::VR128RegisterClass);
1294 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001295 SDOperand Store =
1296 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001297 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001298 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001299 MemOps.push_back(Store);
1300 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001301 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001302 }
1303 if (!MemOps.empty())
1304 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1305 &MemOps[0], MemOps.size());
1306 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001307 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001308
1309 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1310 // arguments and the arguments after the retaddr has been pushed are
1311 // aligned.
1312 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1313 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1314 (StackSize & 7) == 0)
1315 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001317 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001318
Gordon Henriksen18ace102008-01-05 16:56:59 +00001319 // Some CCs need callee pop.
1320 if (IsCalleePop(Op)) {
1321 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 BytesCallerReserves = 0;
1323 } else {
1324 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001326 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 BytesCallerReserves = StackSize;
1329 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001330
Gordon Henriksen18ace102008-01-05 16:56:59 +00001331 if (!Is64Bit) {
1332 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1333 if (CC == CallingConv::X86_FastCall)
1334 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1335 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336
Anton Korobeynikove844e472007-08-15 17:12:32 +00001337 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001338
1339 // Return the new list of results.
1340 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1341 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1342}
1343
Evan Chengbc077bf2008-01-10 00:09:10 +00001344SDOperand
1345X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1346 const SDOperand &StackPtr,
1347 const CCValAssign &VA,
1348 SDOperand Chain,
1349 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001350 unsigned LocMemOffset = VA.getLocMemOffset();
1351 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001352 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1353 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1354 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1355 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001356 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001357 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001358 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001359 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001360}
1361
Evan Cheng931a8f42008-01-29 19:34:22 +00001362/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1363/// struct return call to the specified function. X86-64 ABI specifies
1364/// some SRet calls are actually returned in registers. Since current
1365/// LLVM cannot represent multi-value calls, they are represent as
1366/// calls where the results are passed in a hidden struct provided by
1367/// the caller. This function examines the type of the struct to
1368/// determine the correct way to implement the call.
1369X86::X86_64SRet
1370X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1371 // FIXME: Disabled for now.
1372 return X86::InMemory;
1373
1374 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1375 const Type *RTy = PTy->getElementType();
1376 unsigned Size = getTargetData()->getABITypeSize(RTy);
1377 if (Size != 16 && Size != 32)
1378 return X86::InMemory;
1379
1380 if (Size == 32) {
1381 const StructType *STy = dyn_cast<StructType>(RTy);
1382 if (!STy) return X86::InMemory;
1383 if (STy->getNumElements() == 2 &&
1384 STy->getElementType(0) == Type::X86_FP80Ty &&
1385 STy->getElementType(1) == Type::X86_FP80Ty)
1386 return X86::InX87;
1387 }
1388
1389 bool AllFP = true;
1390 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1391 I != E; ++I) {
1392 const Type *STy = I->get();
1393 if (!STy->isFPOrFPVector()) {
1394 AllFP = false;
1395 break;
1396 }
1397 }
1398
1399 if (AllFP)
1400 return X86::InSSE;
1401 return X86::InGPR64;
1402}
1403
1404void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1405 CCAssignFn *Fn,
1406 CCState &CCInfo) {
1407 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1408 for (unsigned i = 1; i != NumOps; ++i) {
1409 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1410 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1411 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1412 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1413 cerr << "Call operand #" << i << " has unhandled type "
1414 << MVT::getValueTypeString(ArgVT) << "\n";
1415 abort();
1416 }
1417 }
1418}
1419
Gordon Henriksen18ace102008-01-05 16:56:59 +00001420SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1421 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001422 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001424 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001426 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1427 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001429 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001430 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001431
1432 assert(!(isVarArg && CC == CallingConv::Fast) &&
1433 "Var args not supported with calling convention fastcc");
1434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 // Analyze operands of the call, assigning locations to each operand.
1436 SmallVector<CCValAssign, 16> ArgLocs;
1437 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001438 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1439
1440 X86::X86_64SRet SRetMethod = X86::InMemory;
1441 if (Is64Bit && IsStructRet)
1442 // FIXME: We can't figure out type of the sret structure for indirect
1443 // calls. We need to copy more information from CallSite to the ISD::CALL
1444 // node.
1445 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1446 SRetMethod =
1447 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1448
1449 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1450 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1451 // a sret call.
1452 if (SRetMethod != X86::InMemory)
1453 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1454 else
1455 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456
1457 // Get a count of how many bytes are to be pushed on the stack.
1458 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001459 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001460 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461
Gordon Henriksen18ace102008-01-05 16:56:59 +00001462 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1463 // arguments and the arguments after the retaddr has been pushed are aligned.
1464 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1465 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1466 (NumBytes & 7) == 0)
1467 NumBytes += 4;
1468
1469 int FPDiff = 0;
1470 if (IsTailCall) {
1471 // Lower arguments at fp - stackoffset + fpdiff.
1472 unsigned NumBytesCallerPushed =
1473 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1474 FPDiff = NumBytesCallerPushed - NumBytes;
1475
1476 // Set the delta of movement of the returnaddr stackslot.
1477 // But only set if delta is greater than previous delta.
1478 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1479 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1480 }
1481
Chris Lattner5872a362008-01-17 07:00:52 +00001482 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001484 SDOperand RetAddrFrIdx;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001485 if (IsTailCall) {
1486 // Adjust the Return address stack slot.
1487 if (FPDiff) {
1488 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1489 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1490 // Load the "old" Return address.
1491 RetAddrFrIdx =
1492 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001493 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1494 }
1495 }
1496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001498 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499 SmallVector<SDOperand, 8> MemOpChains;
1500
1501 SDOperand StackPtr;
1502
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001503 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001504 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1506 CCValAssign &VA = ArgLocs[i];
1507 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1508
1509 // Promote the value if needed.
1510 switch (VA.getLocInfo()) {
1511 default: assert(0 && "Unknown loc info!");
1512 case CCValAssign::Full: break;
1513 case CCValAssign::SExt:
1514 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1515 break;
1516 case CCValAssign::ZExt:
1517 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1518 break;
1519 case CCValAssign::AExt:
1520 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1521 break;
1522 }
1523
1524 if (VA.isRegLoc()) {
1525 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1526 } else {
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001527 if (!IsTailCall) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001528 assert(VA.isMemLoc());
1529 if (StackPtr.Val == 0)
1530 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1531
1532 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1533 Arg));
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001534 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1535 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001536 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001537 }
1538 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001539
1540 if (!MemOpChains.empty())
1541 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1542 &MemOpChains[0], MemOpChains.size());
1543
1544 // Build a sequence of copy-to-reg nodes chained together with token chain
1545 // and flag operands which copy the outgoing args into registers.
1546 SDOperand InFlag;
1547 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1548 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1549 InFlag);
1550 InFlag = Chain.getValue(1);
1551 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001552
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001554 // GOT pointer.
1555 // If we are tail calling and generating PIC/GOT style code load the address
1556 // of the callee into ecx. The value in ecx is used as target of the tail
1557 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1558 // calls on PIC/GOT architectures. Normally we would just put the address of
1559 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1560 // restored (since ebx is callee saved) before jumping to the target@PLT.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001561 if (!IsTailCall && !Is64Bit &&
1562 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 Subtarget->isPICStyleGOT()) {
1564 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1565 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1566 InFlag);
1567 InFlag = Chain.getValue(1);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001568 } else if (!Is64Bit && IsTailCall &&
1569 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1570 Subtarget->isPICStyleGOT() ) {
1571 // Note: The actual moving to ecx is done further down.
1572 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1573 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1574 !G->getGlobal()->hasProtectedVisibility())
1575 Callee = LowerGlobalAddress(Callee, DAG);
1576 else if (isa<ExternalSymbolSDNode>(Callee))
1577 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001578 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001579
Gordon Henriksen18ace102008-01-05 16:56:59 +00001580 if (Is64Bit && isVarArg) {
1581 // From AMD64 ABI document:
1582 // For calls that may call functions that use varargs or stdargs
1583 // (prototype-less calls or calls to functions containing ellipsis (...) in
1584 // the declaration) %al is used as hidden argument to specify the number
1585 // of SSE registers used. The contents of %al do not need to match exactly
1586 // the number of registers, but must be an ubound on the number of SSE
1587 // registers used and is in the range 0 - 8 inclusive.
1588
1589 // Count the number of XMM registers allocated.
1590 static const unsigned XMMArgRegs[] = {
1591 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1592 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1593 };
1594 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1595
1596 Chain = DAG.getCopyToReg(Chain, X86::AL,
1597 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1598 InFlag = Chain.getValue(1);
1599 }
1600
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001601
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001602 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001603 if (IsTailCall) {
1604 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001605 SDOperand FIN;
1606 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001607 // Do not flag preceeding copytoreg stuff together with the following stuff.
1608 InFlag = SDOperand();
1609
1610 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1611 DAG, MF, this);
1612
Gordon Henriksen18ace102008-01-05 16:56:59 +00001613 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1614 CCValAssign &VA = ArgLocs[i];
1615 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001616 assert(VA.isMemLoc());
1617 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001618 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1619 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001620 // Create frame index.
1621 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1622 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1623 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1624 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001625
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001626 // Find virtual register for this argument.
1627 bool Found=false;
1628 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1629 if (TailCallClobberedVRegs[idx].first==i) {
1630 Arg = TailCallClobberedVRegs[idx].second;
1631 Found=true;
1632 break;
1633 }
1634 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1635 (Found==true && "No corresponding Argument was found"));
1636
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001638 // Copy relative to framepointer.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001639 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001640 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001641 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001642 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001643 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001644 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001645 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001646 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001647 }
1648 }
1649
1650 if (!MemOpChains2.empty())
1651 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001652 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001653
1654 // Store the return address to the appropriate stack slot.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001655 if (FPDiff) {
1656 // Calculate the new stack slot for the return address.
1657 int SlotSize = Is64Bit ? 8 : 4;
1658 int NewReturnAddrFI =
1659 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1660 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1661 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1662 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1663 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1664 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001665 }
1666
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001667 // If the callee is a GlobalAddress node (quite common, every direct call is)
1668 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1669 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1670 // We should use extra load for direct calls to dllimported functions in
1671 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001672 if ((IsTailCall || !Is64Bit ||
1673 getTargetMachine().getCodeModel() != CodeModel::Large)
1674 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1675 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001677 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001678 if (IsTailCall || !Is64Bit ||
1679 getTargetMachine().getCodeModel() != CodeModel::Large)
1680 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1681 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001682 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1683
1684 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001685 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001686 Callee,InFlag);
1687 Callee = DAG.getRegister(Opc, getPointerTy());
1688 // Add register as live out.
1689 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001690 }
1691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001692 // Returns a chain & a flag for retval copy to use.
1693 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1694 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695
1696 if (IsTailCall) {
1697 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001698 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1699 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001700 if (InFlag.Val)
1701 Ops.push_back(InFlag);
1702 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1703 InFlag = Chain.getValue(1);
1704
1705 // Returns a chain & a flag for retval copy to use.
1706 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1707 Ops.clear();
1708 }
1709
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001710 Ops.push_back(Chain);
1711 Ops.push_back(Callee);
1712
Gordon Henriksen18ace102008-01-05 16:56:59 +00001713 if (IsTailCall)
1714 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715
1716 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001717 if (!IsTailCall && !Is64Bit &&
1718 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 Subtarget->isPICStyleGOT())
1720 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001721
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722 // Add argument registers to the end of the list so that they are known live
1723 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001724 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1725 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1726 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001727
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001728 if (InFlag.Val)
1729 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001730
Gordon Henriksen18ace102008-01-05 16:56:59 +00001731 if (IsTailCall) {
1732 assert(InFlag.Val &&
1733 "Flag must be set. Depend on flag being set in LowerRET");
1734 Chain = DAG.getNode(X86ISD::TAILCALL,
1735 Op.Val->getVTList(), &Ops[0], Ops.size());
1736
1737 return SDOperand(Chain.Val, Op.ResNo);
1738 }
1739
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001740 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001741 InFlag = Chain.getValue(1);
1742
1743 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001744 unsigned NumBytesForCalleeToPush;
1745 if (IsCalleePop(Op))
1746 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001747 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001748 // If this is is a call to a struct-return function, the callee
1749 // pops the hidden struct pointer, so we have to push it back.
1750 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001751 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001752 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001753 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001754
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001755 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001756 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001757 DAG.getIntPtrConstant(NumBytes),
1758 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001759 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001760 InFlag = Chain.getValue(1);
1761
1762 // Handle result values, copying them out of physregs into vregs that we
1763 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001764 switch (SRetMethod) {
1765 default:
1766 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1767 case X86::InGPR64:
1768 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1769 X86::RAX, X86::RDX,
1770 MVT::i64, DAG), Op.ResNo);
1771 case X86::InSSE:
1772 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1773 X86::XMM0, X86::XMM1,
1774 MVT::f64, DAG), Op.ResNo);
1775 case X86::InX87:
1776 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1777 Op.ResNo);
1778 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779}
1780
1781
1782//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001783// Fast Calling Convention (tail call) implementation
1784//===----------------------------------------------------------------------===//
1785
1786// Like std call, callee cleans arguments, convention except that ECX is
1787// reserved for storing the tail called function address. Only 2 registers are
1788// free for argument passing (inreg). Tail call optimization is performed
1789// provided:
1790// * tailcallopt is enabled
1791// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001792// On X86_64 architecture with GOT-style position independent code only local
1793// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001794// To keep the stack aligned according to platform abi the function
1795// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1796// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001797// If a tail called function callee has more arguments than the caller the
1798// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001799// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001800// original REtADDR, but before the saved framepointer or the spilled registers
1801// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1802// stack layout:
1803// arg1
1804// arg2
1805// RETADDR
1806// [ new RETADDR
1807// move area ]
1808// (possible EBP)
1809// ESI
1810// EDI
1811// local1 ..
1812
1813/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1814/// for a 16 byte align requirement.
1815unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1816 SelectionDAG& DAG) {
1817 if (PerformTailCallOpt) {
1818 MachineFunction &MF = DAG.getMachineFunction();
1819 const TargetMachine &TM = MF.getTarget();
1820 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1821 unsigned StackAlignment = TFI.getStackAlignment();
1822 uint64_t AlignMask = StackAlignment - 1;
1823 int64_t Offset = StackSize;
1824 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1825 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1826 // Number smaller than 12 so just add the difference.
1827 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1828 } else {
1829 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1830 Offset = ((~AlignMask) & Offset) + StackAlignment +
1831 (StackAlignment-SlotSize);
1832 }
1833 StackSize = Offset;
1834 }
1835 return StackSize;
1836}
1837
1838/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001839/// following the call is a return. A function is eligible if caller/callee
1840/// calling conventions match, currently only fastcc supports tail calls, and
1841/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001842bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1843 SDOperand Ret,
1844 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001845 if (!PerformTailCallOpt)
1846 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001847
1848 // Check whether CALL node immediatly preceeds the RET node and whether the
1849 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001850 unsigned NumOps = Ret.getNumOperands();
1851 if ((NumOps == 1 &&
1852 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1853 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001854 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001855 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1856 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001857 MachineFunction &MF = DAG.getMachineFunction();
1858 unsigned CallerCC = MF.getFunction()->getCallingConv();
1859 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1860 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1861 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001862 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001863 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001864 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001865 return true;
1866
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001867 // Can only do local tail calls (in same module, hidden or protected) on
1868 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001869 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1870 return G->getGlobal()->hasHiddenVisibility()
1871 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001872 }
1873 }
Evan Chenge7a87392007-11-02 01:26:22 +00001874
1875 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001876}
1877
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001878//===----------------------------------------------------------------------===//
1879// Other Lowering Hooks
1880//===----------------------------------------------------------------------===//
1881
1882
1883SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001884 MachineFunction &MF = DAG.getMachineFunction();
1885 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1886 int ReturnAddrIndex = FuncInfo->getRAIndex();
1887
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001888 if (ReturnAddrIndex == 0) {
1889 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 if (Subtarget->is64Bit())
1891 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1892 else
1893 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001894
1895 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001896 }
1897
1898 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1899}
1900
1901
1902
1903/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1904/// specific condition code. It returns a false if it cannot do a direct
1905/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1906/// needed.
1907static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1908 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1909 SelectionDAG &DAG) {
1910 X86CC = X86::COND_INVALID;
1911 if (!isFP) {
1912 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1913 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1914 // X > -1 -> X == 0, jump !sign.
1915 RHS = DAG.getConstant(0, RHS.getValueType());
1916 X86CC = X86::COND_NS;
1917 return true;
1918 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1919 // X < 0 -> X == 0, jump on sign.
1920 X86CC = X86::COND_S;
1921 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001922 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1923 // X < 1 -> X <= 0
1924 RHS = DAG.getConstant(0, RHS.getValueType());
1925 X86CC = X86::COND_LE;
1926 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927 }
1928 }
1929
1930 switch (SetCCOpcode) {
1931 default: break;
1932 case ISD::SETEQ: X86CC = X86::COND_E; break;
1933 case ISD::SETGT: X86CC = X86::COND_G; break;
1934 case ISD::SETGE: X86CC = X86::COND_GE; break;
1935 case ISD::SETLT: X86CC = X86::COND_L; break;
1936 case ISD::SETLE: X86CC = X86::COND_LE; break;
1937 case ISD::SETNE: X86CC = X86::COND_NE; break;
1938 case ISD::SETULT: X86CC = X86::COND_B; break;
1939 case ISD::SETUGT: X86CC = X86::COND_A; break;
1940 case ISD::SETULE: X86CC = X86::COND_BE; break;
1941 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1942 }
1943 } else {
1944 // On a floating point condition, the flags are set as follows:
1945 // ZF PF CF op
1946 // 0 | 0 | 0 | X > Y
1947 // 0 | 0 | 1 | X < Y
1948 // 1 | 0 | 0 | X == Y
1949 // 1 | 1 | 1 | unordered
1950 bool Flip = false;
1951 switch (SetCCOpcode) {
1952 default: break;
1953 case ISD::SETUEQ:
1954 case ISD::SETEQ: X86CC = X86::COND_E; break;
1955 case ISD::SETOLT: Flip = true; // Fallthrough
1956 case ISD::SETOGT:
1957 case ISD::SETGT: X86CC = X86::COND_A; break;
1958 case ISD::SETOLE: Flip = true; // Fallthrough
1959 case ISD::SETOGE:
1960 case ISD::SETGE: X86CC = X86::COND_AE; break;
1961 case ISD::SETUGT: Flip = true; // Fallthrough
1962 case ISD::SETULT:
1963 case ISD::SETLT: X86CC = X86::COND_B; break;
1964 case ISD::SETUGE: Flip = true; // Fallthrough
1965 case ISD::SETULE:
1966 case ISD::SETLE: X86CC = X86::COND_BE; break;
1967 case ISD::SETONE:
1968 case ISD::SETNE: X86CC = X86::COND_NE; break;
1969 case ISD::SETUO: X86CC = X86::COND_P; break;
1970 case ISD::SETO: X86CC = X86::COND_NP; break;
1971 }
1972 if (Flip)
1973 std::swap(LHS, RHS);
1974 }
1975
1976 return X86CC != X86::COND_INVALID;
1977}
1978
1979/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1980/// code. Current x86 isa includes the following FP cmov instructions:
1981/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1982static bool hasFPCMov(unsigned X86CC) {
1983 switch (X86CC) {
1984 default:
1985 return false;
1986 case X86::COND_B:
1987 case X86::COND_BE:
1988 case X86::COND_E:
1989 case X86::COND_P:
1990 case X86::COND_A:
1991 case X86::COND_AE:
1992 case X86::COND_NE:
1993 case X86::COND_NP:
1994 return true;
1995 }
1996}
1997
1998/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1999/// true if Op is undef or if its value falls within the specified range (L, H].
2000static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2001 if (Op.getOpcode() == ISD::UNDEF)
2002 return true;
2003
2004 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2005 return (Val >= Low && Val < Hi);
2006}
2007
2008/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2009/// true if Op is undef or if its value equal to the specified value.
2010static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2011 if (Op.getOpcode() == ISD::UNDEF)
2012 return true;
2013 return cast<ConstantSDNode>(Op)->getValue() == Val;
2014}
2015
2016/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2017/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2018bool X86::isPSHUFDMask(SDNode *N) {
2019 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2020
Dan Gohman7dc19012007-08-02 21:17:01 +00002021 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002022 return false;
2023
2024 // Check if the value doesn't reference the second vector.
2025 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2026 SDOperand Arg = N->getOperand(i);
2027 if (Arg.getOpcode() == ISD::UNDEF) continue;
2028 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002029 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 return false;
2031 }
2032
2033 return true;
2034}
2035
2036/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2037/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2038bool X86::isPSHUFHWMask(SDNode *N) {
2039 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2040
2041 if (N->getNumOperands() != 8)
2042 return false;
2043
2044 // Lower quadword copied in order.
2045 for (unsigned i = 0; i != 4; ++i) {
2046 SDOperand Arg = N->getOperand(i);
2047 if (Arg.getOpcode() == ISD::UNDEF) continue;
2048 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2049 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2050 return false;
2051 }
2052
2053 // Upper quadword shuffled.
2054 for (unsigned i = 4; i != 8; ++i) {
2055 SDOperand Arg = N->getOperand(i);
2056 if (Arg.getOpcode() == ISD::UNDEF) continue;
2057 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2058 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2059 if (Val < 4 || Val > 7)
2060 return false;
2061 }
2062
2063 return true;
2064}
2065
2066/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2067/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2068bool X86::isPSHUFLWMask(SDNode *N) {
2069 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2070
2071 if (N->getNumOperands() != 8)
2072 return false;
2073
2074 // Upper quadword copied in order.
2075 for (unsigned i = 4; i != 8; ++i)
2076 if (!isUndefOrEqual(N->getOperand(i), i))
2077 return false;
2078
2079 // Lower quadword shuffled.
2080 for (unsigned i = 0; i != 4; ++i)
2081 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2082 return false;
2083
2084 return true;
2085}
2086
2087/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2088/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2089static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2090 if (NumElems != 2 && NumElems != 4) return false;
2091
2092 unsigned Half = NumElems / 2;
2093 for (unsigned i = 0; i < Half; ++i)
2094 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2095 return false;
2096 for (unsigned i = Half; i < NumElems; ++i)
2097 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2098 return false;
2099
2100 return true;
2101}
2102
2103bool X86::isSHUFPMask(SDNode *N) {
2104 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2105 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2106}
2107
2108/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2109/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2110/// half elements to come from vector 1 (which would equal the dest.) and
2111/// the upper half to come from vector 2.
2112static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2113 if (NumOps != 2 && NumOps != 4) return false;
2114
2115 unsigned Half = NumOps / 2;
2116 for (unsigned i = 0; i < Half; ++i)
2117 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2118 return false;
2119 for (unsigned i = Half; i < NumOps; ++i)
2120 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2121 return false;
2122 return true;
2123}
2124
2125static bool isCommutedSHUFP(SDNode *N) {
2126 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2127 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2128}
2129
2130/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2131/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2132bool X86::isMOVHLPSMask(SDNode *N) {
2133 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2134
2135 if (N->getNumOperands() != 4)
2136 return false;
2137
2138 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2139 return isUndefOrEqual(N->getOperand(0), 6) &&
2140 isUndefOrEqual(N->getOperand(1), 7) &&
2141 isUndefOrEqual(N->getOperand(2), 2) &&
2142 isUndefOrEqual(N->getOperand(3), 3);
2143}
2144
2145/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2146/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2147/// <2, 3, 2, 3>
2148bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2149 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2150
2151 if (N->getNumOperands() != 4)
2152 return false;
2153
2154 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2155 return isUndefOrEqual(N->getOperand(0), 2) &&
2156 isUndefOrEqual(N->getOperand(1), 3) &&
2157 isUndefOrEqual(N->getOperand(2), 2) &&
2158 isUndefOrEqual(N->getOperand(3), 3);
2159}
2160
2161/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2162/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2163bool X86::isMOVLPMask(SDNode *N) {
2164 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2165
2166 unsigned NumElems = N->getNumOperands();
2167 if (NumElems != 2 && NumElems != 4)
2168 return false;
2169
2170 for (unsigned i = 0; i < NumElems/2; ++i)
2171 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2172 return false;
2173
2174 for (unsigned i = NumElems/2; i < NumElems; ++i)
2175 if (!isUndefOrEqual(N->getOperand(i), i))
2176 return false;
2177
2178 return true;
2179}
2180
2181/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2182/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2183/// and MOVLHPS.
2184bool X86::isMOVHPMask(SDNode *N) {
2185 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2186
2187 unsigned NumElems = N->getNumOperands();
2188 if (NumElems != 2 && NumElems != 4)
2189 return false;
2190
2191 for (unsigned i = 0; i < NumElems/2; ++i)
2192 if (!isUndefOrEqual(N->getOperand(i), i))
2193 return false;
2194
2195 for (unsigned i = 0; i < NumElems/2; ++i) {
2196 SDOperand Arg = N->getOperand(i + NumElems/2);
2197 if (!isUndefOrEqual(Arg, i + NumElems))
2198 return false;
2199 }
2200
2201 return true;
2202}
2203
2204/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2205/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2206bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2207 bool V2IsSplat = false) {
2208 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2209 return false;
2210
2211 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2212 SDOperand BitI = Elts[i];
2213 SDOperand BitI1 = Elts[i+1];
2214 if (!isUndefOrEqual(BitI, j))
2215 return false;
2216 if (V2IsSplat) {
2217 if (isUndefOrEqual(BitI1, NumElts))
2218 return false;
2219 } else {
2220 if (!isUndefOrEqual(BitI1, j + NumElts))
2221 return false;
2222 }
2223 }
2224
2225 return true;
2226}
2227
2228bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2229 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2230 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2231}
2232
2233/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2234/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2235bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2236 bool V2IsSplat = false) {
2237 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2238 return false;
2239
2240 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2241 SDOperand BitI = Elts[i];
2242 SDOperand BitI1 = Elts[i+1];
2243 if (!isUndefOrEqual(BitI, j + NumElts/2))
2244 return false;
2245 if (V2IsSplat) {
2246 if (isUndefOrEqual(BitI1, NumElts))
2247 return false;
2248 } else {
2249 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2250 return false;
2251 }
2252 }
2253
2254 return true;
2255}
2256
2257bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2258 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2259 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2260}
2261
2262/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2263/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2264/// <0, 0, 1, 1>
2265bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2266 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2267
2268 unsigned NumElems = N->getNumOperands();
2269 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2270 return false;
2271
2272 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2273 SDOperand BitI = N->getOperand(i);
2274 SDOperand BitI1 = N->getOperand(i+1);
2275
2276 if (!isUndefOrEqual(BitI, j))
2277 return false;
2278 if (!isUndefOrEqual(BitI1, j))
2279 return false;
2280 }
2281
2282 return true;
2283}
2284
2285/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2286/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2287/// <2, 2, 3, 3>
2288bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2289 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2290
2291 unsigned NumElems = N->getNumOperands();
2292 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2293 return false;
2294
2295 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2296 SDOperand BitI = N->getOperand(i);
2297 SDOperand BitI1 = N->getOperand(i + 1);
2298
2299 if (!isUndefOrEqual(BitI, j))
2300 return false;
2301 if (!isUndefOrEqual(BitI1, j))
2302 return false;
2303 }
2304
2305 return true;
2306}
2307
2308/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2309/// specifies a shuffle of elements that is suitable for input to MOVSS,
2310/// MOVSD, and MOVD, i.e. setting the lowest element.
2311static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002312 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002313 return false;
2314
2315 if (!isUndefOrEqual(Elts[0], NumElts))
2316 return false;
2317
2318 for (unsigned i = 1; i < NumElts; ++i) {
2319 if (!isUndefOrEqual(Elts[i], i))
2320 return false;
2321 }
2322
2323 return true;
2324}
2325
2326bool X86::isMOVLMask(SDNode *N) {
2327 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2328 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2329}
2330
2331/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2332/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2333/// element of vector 2 and the other elements to come from vector 1 in order.
2334static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2335 bool V2IsSplat = false,
2336 bool V2IsUndef = false) {
2337 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2338 return false;
2339
2340 if (!isUndefOrEqual(Ops[0], 0))
2341 return false;
2342
2343 for (unsigned i = 1; i < NumOps; ++i) {
2344 SDOperand Arg = Ops[i];
2345 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2346 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2347 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2348 return false;
2349 }
2350
2351 return true;
2352}
2353
2354static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2355 bool V2IsUndef = false) {
2356 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2357 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2358 V2IsSplat, V2IsUndef);
2359}
2360
2361/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2362/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2363bool X86::isMOVSHDUPMask(SDNode *N) {
2364 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2365
2366 if (N->getNumOperands() != 4)
2367 return false;
2368
2369 // Expect 1, 1, 3, 3
2370 for (unsigned i = 0; i < 2; ++i) {
2371 SDOperand Arg = N->getOperand(i);
2372 if (Arg.getOpcode() == ISD::UNDEF) continue;
2373 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2374 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2375 if (Val != 1) return false;
2376 }
2377
2378 bool HasHi = false;
2379 for (unsigned i = 2; i < 4; ++i) {
2380 SDOperand Arg = N->getOperand(i);
2381 if (Arg.getOpcode() == ISD::UNDEF) continue;
2382 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2383 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2384 if (Val != 3) return false;
2385 HasHi = true;
2386 }
2387
2388 // Don't use movshdup if it can be done with a shufps.
2389 return HasHi;
2390}
2391
2392/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2393/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2394bool X86::isMOVSLDUPMask(SDNode *N) {
2395 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2396
2397 if (N->getNumOperands() != 4)
2398 return false;
2399
2400 // Expect 0, 0, 2, 2
2401 for (unsigned i = 0; i < 2; ++i) {
2402 SDOperand Arg = N->getOperand(i);
2403 if (Arg.getOpcode() == ISD::UNDEF) continue;
2404 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2405 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2406 if (Val != 0) return false;
2407 }
2408
2409 bool HasHi = false;
2410 for (unsigned i = 2; i < 4; ++i) {
2411 SDOperand Arg = N->getOperand(i);
2412 if (Arg.getOpcode() == ISD::UNDEF) continue;
2413 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2414 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2415 if (Val != 2) return false;
2416 HasHi = true;
2417 }
2418
2419 // Don't use movshdup if it can be done with a shufps.
2420 return HasHi;
2421}
2422
2423/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2424/// specifies a identity operation on the LHS or RHS.
2425static bool isIdentityMask(SDNode *N, bool RHS = false) {
2426 unsigned NumElems = N->getNumOperands();
2427 for (unsigned i = 0; i < NumElems; ++i)
2428 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2429 return false;
2430 return true;
2431}
2432
2433/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2434/// a splat of a single element.
2435static bool isSplatMask(SDNode *N) {
2436 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2437
2438 // This is a splat operation if each element of the permute is the same, and
2439 // if the value doesn't reference the second vector.
2440 unsigned NumElems = N->getNumOperands();
2441 SDOperand ElementBase;
2442 unsigned i = 0;
2443 for (; i != NumElems; ++i) {
2444 SDOperand Elt = N->getOperand(i);
2445 if (isa<ConstantSDNode>(Elt)) {
2446 ElementBase = Elt;
2447 break;
2448 }
2449 }
2450
2451 if (!ElementBase.Val)
2452 return false;
2453
2454 for (; i != NumElems; ++i) {
2455 SDOperand Arg = N->getOperand(i);
2456 if (Arg.getOpcode() == ISD::UNDEF) continue;
2457 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2458 if (Arg != ElementBase) return false;
2459 }
2460
2461 // Make sure it is a splat of the first vector operand.
2462 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2463}
2464
2465/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2466/// a splat of a single element and it's a 2 or 4 element mask.
2467bool X86::isSplatMask(SDNode *N) {
2468 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2469
2470 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2471 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2472 return false;
2473 return ::isSplatMask(N);
2474}
2475
2476/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2477/// specifies a splat of zero element.
2478bool X86::isSplatLoMask(SDNode *N) {
2479 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2480
2481 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2482 if (!isUndefOrEqual(N->getOperand(i), 0))
2483 return false;
2484 return true;
2485}
2486
2487/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2488/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2489/// instructions.
2490unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2491 unsigned NumOperands = N->getNumOperands();
2492 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2493 unsigned Mask = 0;
2494 for (unsigned i = 0; i < NumOperands; ++i) {
2495 unsigned Val = 0;
2496 SDOperand Arg = N->getOperand(NumOperands-i-1);
2497 if (Arg.getOpcode() != ISD::UNDEF)
2498 Val = cast<ConstantSDNode>(Arg)->getValue();
2499 if (Val >= NumOperands) Val -= NumOperands;
2500 Mask |= Val;
2501 if (i != NumOperands - 1)
2502 Mask <<= Shift;
2503 }
2504
2505 return Mask;
2506}
2507
2508/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2509/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2510/// instructions.
2511unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2512 unsigned Mask = 0;
2513 // 8 nodes, but we only care about the last 4.
2514 for (unsigned i = 7; i >= 4; --i) {
2515 unsigned Val = 0;
2516 SDOperand Arg = N->getOperand(i);
2517 if (Arg.getOpcode() != ISD::UNDEF)
2518 Val = cast<ConstantSDNode>(Arg)->getValue();
2519 Mask |= (Val - 4);
2520 if (i != 4)
2521 Mask <<= 2;
2522 }
2523
2524 return Mask;
2525}
2526
2527/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2528/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2529/// instructions.
2530unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2531 unsigned Mask = 0;
2532 // 8 nodes, but we only care about the first 4.
2533 for (int i = 3; i >= 0; --i) {
2534 unsigned Val = 0;
2535 SDOperand Arg = N->getOperand(i);
2536 if (Arg.getOpcode() != ISD::UNDEF)
2537 Val = cast<ConstantSDNode>(Arg)->getValue();
2538 Mask |= Val;
2539 if (i != 0)
2540 Mask <<= 2;
2541 }
2542
2543 return Mask;
2544}
2545
2546/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2547/// specifies a 8 element shuffle that can be broken into a pair of
2548/// PSHUFHW and PSHUFLW.
2549static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2550 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2551
2552 if (N->getNumOperands() != 8)
2553 return false;
2554
2555 // Lower quadword shuffled.
2556 for (unsigned i = 0; i != 4; ++i) {
2557 SDOperand Arg = N->getOperand(i);
2558 if (Arg.getOpcode() == ISD::UNDEF) continue;
2559 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2560 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002561 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 return false;
2563 }
2564
2565 // Upper quadword shuffled.
2566 for (unsigned i = 4; i != 8; ++i) {
2567 SDOperand Arg = N->getOperand(i);
2568 if (Arg.getOpcode() == ISD::UNDEF) continue;
2569 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2570 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2571 if (Val < 4 || Val > 7)
2572 return false;
2573 }
2574
2575 return true;
2576}
2577
Chris Lattnere6aa3862007-11-25 00:24:49 +00002578/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579/// values in ther permute mask.
2580static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2581 SDOperand &V2, SDOperand &Mask,
2582 SelectionDAG &DAG) {
2583 MVT::ValueType VT = Op.getValueType();
2584 MVT::ValueType MaskVT = Mask.getValueType();
2585 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2586 unsigned NumElems = Mask.getNumOperands();
2587 SmallVector<SDOperand, 8> MaskVec;
2588
2589 for (unsigned i = 0; i != NumElems; ++i) {
2590 SDOperand Arg = Mask.getOperand(i);
2591 if (Arg.getOpcode() == ISD::UNDEF) {
2592 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2593 continue;
2594 }
2595 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2596 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2597 if (Val < NumElems)
2598 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2599 else
2600 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2601 }
2602
2603 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002604 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2606}
2607
Evan Chenga6769df2007-12-07 21:30:01 +00002608/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2609/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002610static
2611SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2612 MVT::ValueType MaskVT = Mask.getValueType();
2613 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2614 unsigned NumElems = Mask.getNumOperands();
2615 SmallVector<SDOperand, 8> MaskVec;
2616 for (unsigned i = 0; i != NumElems; ++i) {
2617 SDOperand Arg = Mask.getOperand(i);
2618 if (Arg.getOpcode() == ISD::UNDEF) {
2619 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2620 continue;
2621 }
2622 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2623 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2624 if (Val < NumElems)
2625 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2626 else
2627 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2628 }
2629 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2630}
2631
2632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2634/// match movhlps. The lower half elements should come from upper half of
2635/// V1 (and in order), and the upper half elements should come from the upper
2636/// half of V2 (and in order).
2637static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2638 unsigned NumElems = Mask->getNumOperands();
2639 if (NumElems != 4)
2640 return false;
2641 for (unsigned i = 0, e = 2; i != e; ++i)
2642 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2643 return false;
2644 for (unsigned i = 2; i != 4; ++i)
2645 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2646 return false;
2647 return true;
2648}
2649
2650/// isScalarLoadToVector - Returns true if the node is a scalar load that
2651/// is promoted to a vector.
2652static inline bool isScalarLoadToVector(SDNode *N) {
2653 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2654 N = N->getOperand(0).Val;
2655 return ISD::isNON_EXTLoad(N);
2656 }
2657 return false;
2658}
2659
2660/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2661/// match movlp{s|d}. The lower half elements should come from lower half of
2662/// V1 (and in order), and the upper half elements should come from the upper
2663/// half of V2 (and in order). And since V1 will become the source of the
2664/// MOVLP, it must be either a vector load or a scalar load to vector.
2665static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2666 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2667 return false;
2668 // Is V2 is a vector load, don't do this transformation. We will try to use
2669 // load folding shufps op.
2670 if (ISD::isNON_EXTLoad(V2))
2671 return false;
2672
2673 unsigned NumElems = Mask->getNumOperands();
2674 if (NumElems != 2 && NumElems != 4)
2675 return false;
2676 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2677 if (!isUndefOrEqual(Mask->getOperand(i), i))
2678 return false;
2679 for (unsigned i = NumElems/2; i != NumElems; ++i)
2680 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2681 return false;
2682 return true;
2683}
2684
2685/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2686/// all the same.
2687static bool isSplatVector(SDNode *N) {
2688 if (N->getOpcode() != ISD::BUILD_VECTOR)
2689 return false;
2690
2691 SDOperand SplatValue = N->getOperand(0);
2692 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2693 if (N->getOperand(i) != SplatValue)
2694 return false;
2695 return true;
2696}
2697
2698/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2699/// to an undef.
2700static bool isUndefShuffle(SDNode *N) {
2701 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2702 return false;
2703
2704 SDOperand V1 = N->getOperand(0);
2705 SDOperand V2 = N->getOperand(1);
2706 SDOperand Mask = N->getOperand(2);
2707 unsigned NumElems = Mask.getNumOperands();
2708 for (unsigned i = 0; i != NumElems; ++i) {
2709 SDOperand Arg = Mask.getOperand(i);
2710 if (Arg.getOpcode() != ISD::UNDEF) {
2711 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2712 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2713 return false;
2714 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2715 return false;
2716 }
2717 }
2718 return true;
2719}
2720
2721/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2722/// constant +0.0.
2723static inline bool isZeroNode(SDOperand Elt) {
2724 return ((isa<ConstantSDNode>(Elt) &&
2725 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2726 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002727 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002728}
2729
2730/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2731/// to an zero vector.
2732static bool isZeroShuffle(SDNode *N) {
2733 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2734 return false;
2735
2736 SDOperand V1 = N->getOperand(0);
2737 SDOperand V2 = N->getOperand(1);
2738 SDOperand Mask = N->getOperand(2);
2739 unsigned NumElems = Mask.getNumOperands();
2740 for (unsigned i = 0; i != NumElems; ++i) {
2741 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002742 if (Arg.getOpcode() == ISD::UNDEF)
2743 continue;
2744
2745 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2746 if (Idx < NumElems) {
2747 unsigned Opc = V1.Val->getOpcode();
2748 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2749 continue;
2750 if (Opc != ISD::BUILD_VECTOR ||
2751 !isZeroNode(V1.Val->getOperand(Idx)))
2752 return false;
2753 } else if (Idx >= NumElems) {
2754 unsigned Opc = V2.Val->getOpcode();
2755 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2756 continue;
2757 if (Opc != ISD::BUILD_VECTOR ||
2758 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2759 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002760 }
2761 }
2762 return true;
2763}
2764
2765/// getZeroVector - Returns a vector of specified type with all zero elements.
2766///
2767static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2768 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002769
2770 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2771 // type. This ensures they get CSE'd.
2772 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2773 SDOperand Vec;
2774 if (MVT::getSizeInBits(VT) == 64) // MMX
2775 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2776 else // SSE
2777 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2778 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779}
2780
Chris Lattnere6aa3862007-11-25 00:24:49 +00002781/// getOnesVector - Returns a vector of specified type with all bits set.
2782///
2783static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2784 assert(MVT::isVector(VT) && "Expected a vector type");
2785
2786 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2787 // type. This ensures they get CSE'd.
2788 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2789 SDOperand Vec;
2790 if (MVT::getSizeInBits(VT) == 64) // MMX
2791 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2792 else // SSE
2793 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2794 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2795}
2796
2797
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2799/// that point to V2 points to its first element.
2800static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2801 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2802
2803 bool Changed = false;
2804 SmallVector<SDOperand, 8> MaskVec;
2805 unsigned NumElems = Mask.getNumOperands();
2806 for (unsigned i = 0; i != NumElems; ++i) {
2807 SDOperand Arg = Mask.getOperand(i);
2808 if (Arg.getOpcode() != ISD::UNDEF) {
2809 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2810 if (Val > NumElems) {
2811 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2812 Changed = true;
2813 }
2814 }
2815 MaskVec.push_back(Arg);
2816 }
2817
2818 if (Changed)
2819 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2820 &MaskVec[0], MaskVec.size());
2821 return Mask;
2822}
2823
2824/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2825/// operation of specified width.
2826static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2827 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2828 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2829
2830 SmallVector<SDOperand, 8> MaskVec;
2831 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2832 for (unsigned i = 1; i != NumElems; ++i)
2833 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2834 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2835}
2836
2837/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2838/// of specified width.
2839static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2840 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2841 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2842 SmallVector<SDOperand, 8> MaskVec;
2843 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2844 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2845 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2846 }
2847 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2848}
2849
2850/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2851/// of specified width.
2852static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2853 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2854 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2855 unsigned Half = NumElems/2;
2856 SmallVector<SDOperand, 8> MaskVec;
2857 for (unsigned i = 0; i != Half; ++i) {
2858 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2859 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2860 }
2861 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2862}
2863
2864/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2865///
2866static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2867 SDOperand V1 = Op.getOperand(0);
2868 SDOperand Mask = Op.getOperand(2);
2869 MVT::ValueType VT = Op.getValueType();
2870 unsigned NumElems = Mask.getNumOperands();
2871 Mask = getUnpacklMask(NumElems, DAG);
2872 while (NumElems != 4) {
2873 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2874 NumElems >>= 1;
2875 }
2876 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2877
Chris Lattnere6aa3862007-11-25 00:24:49 +00002878 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002879 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2880 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2881 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2882}
2883
2884/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002885/// vector of zero or undef vector. This produces a shuffle where the low
2886/// element of V2 is swizzled into the zero/undef vector, landing at element
2887/// 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 +00002888static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2889 unsigned NumElems, unsigned Idx,
2890 bool isZero, SelectionDAG &DAG) {
2891 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2892 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2893 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002894 SmallVector<SDOperand, 16> MaskVec;
2895 for (unsigned i = 0; i != NumElems; ++i)
2896 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2897 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2898 else
2899 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002900 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2901 &MaskVec[0], MaskVec.size());
2902 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2903}
2904
2905/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2906///
2907static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2908 unsigned NumNonZero, unsigned NumZero,
2909 SelectionDAG &DAG, TargetLowering &TLI) {
2910 if (NumNonZero > 8)
2911 return SDOperand();
2912
2913 SDOperand V(0, 0);
2914 bool First = true;
2915 for (unsigned i = 0; i < 16; ++i) {
2916 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2917 if (ThisIsNonZero && First) {
2918 if (NumZero)
2919 V = getZeroVector(MVT::v8i16, DAG);
2920 else
2921 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2922 First = false;
2923 }
2924
2925 if ((i & 1) != 0) {
2926 SDOperand ThisElt(0, 0), LastElt(0, 0);
2927 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2928 if (LastIsNonZero) {
2929 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2930 }
2931 if (ThisIsNonZero) {
2932 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2933 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2934 ThisElt, DAG.getConstant(8, MVT::i8));
2935 if (LastIsNonZero)
2936 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2937 } else
2938 ThisElt = LastElt;
2939
2940 if (ThisElt.Val)
2941 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002942 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 }
2944 }
2945
2946 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2947}
2948
2949/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2950///
2951static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2952 unsigned NumNonZero, unsigned NumZero,
2953 SelectionDAG &DAG, TargetLowering &TLI) {
2954 if (NumNonZero > 4)
2955 return SDOperand();
2956
2957 SDOperand V(0, 0);
2958 bool First = true;
2959 for (unsigned i = 0; i < 8; ++i) {
2960 bool isNonZero = (NonZeros & (1 << i)) != 0;
2961 if (isNonZero) {
2962 if (First) {
2963 if (NumZero)
2964 V = getZeroVector(MVT::v8i16, DAG);
2965 else
2966 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2967 First = false;
2968 }
2969 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002970 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 }
2972 }
2973
2974 return V;
2975}
2976
2977SDOperand
2978X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002979 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2980 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2981 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2982 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2983 // eliminated on x86-32 hosts.
2984 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2985 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986
Chris Lattnere6aa3862007-11-25 00:24:49 +00002987 if (ISD::isBuildVectorAllOnes(Op.Val))
2988 return getOnesVector(Op.getValueType(), DAG);
2989 return getZeroVector(Op.getValueType(), DAG);
2990 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991
2992 MVT::ValueType VT = Op.getValueType();
2993 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2994 unsigned EVTBits = MVT::getSizeInBits(EVT);
2995
2996 unsigned NumElems = Op.getNumOperands();
2997 unsigned NumZero = 0;
2998 unsigned NumNonZero = 0;
2999 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00003000 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00003001 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 for (unsigned i = 0; i < NumElems; ++i) {
3003 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003004 if (Elt.getOpcode() == ISD::UNDEF)
3005 continue;
3006 Values.insert(Elt);
3007 if (Elt.getOpcode() != ISD::Constant &&
3008 Elt.getOpcode() != ISD::ConstantFP)
3009 HasNonImms = true;
3010 if (isZeroNode(Elt))
3011 NumZero++;
3012 else {
3013 NonZeros |= (1 << i);
3014 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015 }
3016 }
3017
3018 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003019 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3020 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021 }
3022
3023 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3024 if (Values.size() == 1)
3025 return SDOperand();
3026
3027 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00003028 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 unsigned Idx = CountTrailingZeros_32(NonZeros);
3030 SDOperand Item = Op.getOperand(Idx);
3031 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3032 if (Idx == 0)
3033 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3034 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3035 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00003036 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
3037 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003038
3039 if (EVTBits == 32) {
3040 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3041 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3042 DAG);
3043 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3044 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3045 SmallVector<SDOperand, 8> MaskVec;
3046 for (unsigned i = 0; i < NumElems; i++)
3047 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3048 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3049 &MaskVec[0], MaskVec.size());
3050 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3051 DAG.getNode(ISD::UNDEF, VT), Mask);
3052 }
3053 }
3054
Dan Gohman21463242007-07-24 22:55:08 +00003055 // A vector full of immediates; various special cases are already
3056 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00003057 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00003058 return SDOperand();
3059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003060 // Let legalizer expand 2-wide build_vectors.
3061 if (EVTBits == 64)
3062 return SDOperand();
3063
3064 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3065 if (EVTBits == 8 && NumElems == 16) {
3066 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3067 *this);
3068 if (V.Val) return V;
3069 }
3070
3071 if (EVTBits == 16 && NumElems == 8) {
3072 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3073 *this);
3074 if (V.Val) return V;
3075 }
3076
3077 // If element VT is == 32 bits, turn it into a number of shuffles.
3078 SmallVector<SDOperand, 8> V;
3079 V.resize(NumElems);
3080 if (NumElems == 4 && NumZero > 0) {
3081 for (unsigned i = 0; i < 4; ++i) {
3082 bool isZero = !(NonZeros & (1 << i));
3083 if (isZero)
3084 V[i] = getZeroVector(VT, DAG);
3085 else
3086 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3087 }
3088
3089 for (unsigned i = 0; i < 2; ++i) {
3090 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3091 default: break;
3092 case 0:
3093 V[i] = V[i*2]; // Must be a zero vector.
3094 break;
3095 case 1:
3096 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3097 getMOVLMask(NumElems, DAG));
3098 break;
3099 case 2:
3100 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3101 getMOVLMask(NumElems, DAG));
3102 break;
3103 case 3:
3104 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3105 getUnpacklMask(NumElems, DAG));
3106 break;
3107 }
3108 }
3109
3110 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3111 // clears the upper bits.
3112 // FIXME: we can do the same for v4f32 case when we know both parts of
3113 // the lower half come from scalar_to_vector (loadf32). We should do
3114 // that in post legalizer dag combiner with target specific hooks.
3115 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3116 return V[0];
3117 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3118 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3119 SmallVector<SDOperand, 8> MaskVec;
3120 bool Reverse = (NonZeros & 0x3) == 2;
3121 for (unsigned i = 0; i < 2; ++i)
3122 if (Reverse)
3123 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3124 else
3125 MaskVec.push_back(DAG.getConstant(i, EVT));
3126 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3127 for (unsigned i = 0; i < 2; ++i)
3128 if (Reverse)
3129 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3130 else
3131 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3132 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3133 &MaskVec[0], MaskVec.size());
3134 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3135 }
3136
3137 if (Values.size() > 2) {
3138 // Expand into a number of unpckl*.
3139 // e.g. for v4f32
3140 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3141 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3142 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3143 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3144 for (unsigned i = 0; i < NumElems; ++i)
3145 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3146 NumElems >>= 1;
3147 while (NumElems != 0) {
3148 for (unsigned i = 0; i < NumElems; ++i)
3149 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3150 UnpckMask);
3151 NumElems >>= 1;
3152 }
3153 return V[0];
3154 }
3155
3156 return SDOperand();
3157}
3158
Evan Chengfca29242007-12-07 08:07:39 +00003159static
3160SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3161 SDOperand PermMask, SelectionDAG &DAG,
3162 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003163 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003164 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3165 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003166 MVT::ValueType PtrVT = TLI.getPointerTy();
3167 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3168 PermMask.Val->op_end());
3169
3170 // First record which half of which vector the low elements come from.
3171 SmallVector<unsigned, 4> LowQuad(4);
3172 for (unsigned i = 0; i < 4; ++i) {
3173 SDOperand Elt = MaskElts[i];
3174 if (Elt.getOpcode() == ISD::UNDEF)
3175 continue;
3176 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3177 int QuadIdx = EltIdx / 4;
3178 ++LowQuad[QuadIdx];
3179 }
3180 int BestLowQuad = -1;
3181 unsigned MaxQuad = 1;
3182 for (unsigned i = 0; i < 4; ++i) {
3183 if (LowQuad[i] > MaxQuad) {
3184 BestLowQuad = i;
3185 MaxQuad = LowQuad[i];
3186 }
Evan Chengfca29242007-12-07 08:07:39 +00003187 }
3188
Evan Cheng75184a92007-12-11 01:46:18 +00003189 // Record which half of which vector the high elements come from.
3190 SmallVector<unsigned, 4> HighQuad(4);
3191 for (unsigned i = 4; i < 8; ++i) {
3192 SDOperand Elt = MaskElts[i];
3193 if (Elt.getOpcode() == ISD::UNDEF)
3194 continue;
3195 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3196 int QuadIdx = EltIdx / 4;
3197 ++HighQuad[QuadIdx];
3198 }
3199 int BestHighQuad = -1;
3200 MaxQuad = 1;
3201 for (unsigned i = 0; i < 4; ++i) {
3202 if (HighQuad[i] > MaxQuad) {
3203 BestHighQuad = i;
3204 MaxQuad = HighQuad[i];
3205 }
3206 }
3207
3208 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3209 if (BestLowQuad != -1 || BestHighQuad != -1) {
3210 // First sort the 4 chunks in order using shufpd.
3211 SmallVector<SDOperand, 8> MaskVec;
3212 if (BestLowQuad != -1)
3213 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3214 else
3215 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3216 if (BestHighQuad != -1)
3217 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3218 else
3219 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3220 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3221 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3222 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3223 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3224 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3225
3226 // Now sort high and low parts separately.
3227 BitVector InOrder(8);
3228 if (BestLowQuad != -1) {
3229 // Sort lower half in order using PSHUFLW.
3230 MaskVec.clear();
3231 bool AnyOutOrder = false;
3232 for (unsigned i = 0; i != 4; ++i) {
3233 SDOperand Elt = MaskElts[i];
3234 if (Elt.getOpcode() == ISD::UNDEF) {
3235 MaskVec.push_back(Elt);
3236 InOrder.set(i);
3237 } else {
3238 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3239 if (EltIdx != i)
3240 AnyOutOrder = true;
3241 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3242 // If this element is in the right place after this shuffle, then
3243 // remember it.
3244 if ((int)(EltIdx / 4) == BestLowQuad)
3245 InOrder.set(i);
3246 }
3247 }
3248 if (AnyOutOrder) {
3249 for (unsigned i = 4; i != 8; ++i)
3250 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3251 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3252 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3253 }
3254 }
3255
3256 if (BestHighQuad != -1) {
3257 // Sort high half in order using PSHUFHW if possible.
3258 MaskVec.clear();
3259 for (unsigned i = 0; i != 4; ++i)
3260 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3261 bool AnyOutOrder = false;
3262 for (unsigned i = 4; i != 8; ++i) {
3263 SDOperand Elt = MaskElts[i];
3264 if (Elt.getOpcode() == ISD::UNDEF) {
3265 MaskVec.push_back(Elt);
3266 InOrder.set(i);
3267 } else {
3268 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3269 if (EltIdx != i)
3270 AnyOutOrder = true;
3271 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3272 // If this element is in the right place after this shuffle, then
3273 // remember it.
3274 if ((int)(EltIdx / 4) == BestHighQuad)
3275 InOrder.set(i);
3276 }
3277 }
3278 if (AnyOutOrder) {
3279 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3280 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3281 }
3282 }
3283
3284 // The other elements are put in the right place using pextrw and pinsrw.
3285 for (unsigned i = 0; i != 8; ++i) {
3286 if (InOrder[i])
3287 continue;
3288 SDOperand Elt = MaskElts[i];
3289 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3290 if (EltIdx == i)
3291 continue;
3292 SDOperand ExtOp = (EltIdx < 8)
3293 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3294 DAG.getConstant(EltIdx, PtrVT))
3295 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3296 DAG.getConstant(EltIdx - 8, PtrVT));
3297 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3298 DAG.getConstant(i, PtrVT));
3299 }
3300 return NewV;
3301 }
3302
3303 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3304 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003305 // First, let's find out how many elements are already in the right order.
3306 unsigned V1InOrder = 0;
3307 unsigned V1FromV1 = 0;
3308 unsigned V2InOrder = 0;
3309 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003310 SmallVector<SDOperand, 8> V1Elts;
3311 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003312 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003313 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003314 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003315 V1Elts.push_back(Elt);
3316 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003317 ++V1InOrder;
3318 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003319 continue;
3320 }
3321 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3322 if (EltIdx == i) {
3323 V1Elts.push_back(Elt);
3324 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3325 ++V1InOrder;
3326 } else if (EltIdx == i+8) {
3327 V1Elts.push_back(Elt);
3328 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3329 ++V2InOrder;
3330 } else if (EltIdx < 8) {
3331 V1Elts.push_back(Elt);
3332 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003333 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003334 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3335 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003336 }
3337 }
3338
3339 if (V2InOrder > V1InOrder) {
3340 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3341 std::swap(V1, V2);
3342 std::swap(V1Elts, V2Elts);
3343 std::swap(V1FromV1, V2FromV2);
3344 }
3345
Evan Cheng75184a92007-12-11 01:46:18 +00003346 if ((V1FromV1 + V1InOrder) != 8) {
3347 // Some elements are from V2.
3348 if (V1FromV1) {
3349 // If there are elements that are from V1 but out of place,
3350 // then first sort them in place
3351 SmallVector<SDOperand, 8> MaskVec;
3352 for (unsigned i = 0; i < 8; ++i) {
3353 SDOperand Elt = V1Elts[i];
3354 if (Elt.getOpcode() == ISD::UNDEF) {
3355 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3356 continue;
3357 }
3358 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3359 if (EltIdx >= 8)
3360 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3361 else
3362 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3363 }
3364 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3365 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003366 }
Evan Cheng75184a92007-12-11 01:46:18 +00003367
3368 NewV = V1;
3369 for (unsigned i = 0; i < 8; ++i) {
3370 SDOperand Elt = V1Elts[i];
3371 if (Elt.getOpcode() == ISD::UNDEF)
3372 continue;
3373 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3374 if (EltIdx < 8)
3375 continue;
3376 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3377 DAG.getConstant(EltIdx - 8, PtrVT));
3378 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3379 DAG.getConstant(i, PtrVT));
3380 }
3381 return NewV;
3382 } else {
3383 // All elements are from V1.
3384 NewV = V1;
3385 for (unsigned i = 0; i < 8; ++i) {
3386 SDOperand Elt = V1Elts[i];
3387 if (Elt.getOpcode() == ISD::UNDEF)
3388 continue;
3389 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3390 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3391 DAG.getConstant(EltIdx, PtrVT));
3392 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3393 DAG.getConstant(i, PtrVT));
3394 }
3395 return NewV;
3396 }
3397}
3398
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003399/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3400/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3401/// done when every pair / quad of shuffle mask elements point to elements in
3402/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003403/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3404static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003405SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3406 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003407 SDOperand PermMask, SelectionDAG &DAG,
3408 TargetLowering &TLI) {
3409 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003410 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3411 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3412 MVT::ValueType NewVT = MaskVT;
3413 switch (VT) {
3414 case MVT::v4f32: NewVT = MVT::v2f64; break;
3415 case MVT::v4i32: NewVT = MVT::v2i64; break;
3416 case MVT::v8i16: NewVT = MVT::v4i32; break;
3417 case MVT::v16i8: NewVT = MVT::v4i32; break;
3418 default: assert(false && "Unexpected!");
3419 }
3420
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003421 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003422 if (MVT::isInteger(VT))
3423 NewVT = MVT::v2i64;
3424 else
3425 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003426 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003427 unsigned Scale = NumElems / NewWidth;
3428 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003429 for (unsigned i = 0; i < NumElems; i += Scale) {
3430 unsigned StartIdx = ~0U;
3431 for (unsigned j = 0; j < Scale; ++j) {
3432 SDOperand Elt = PermMask.getOperand(i+j);
3433 if (Elt.getOpcode() == ISD::UNDEF)
3434 continue;
3435 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3436 if (StartIdx == ~0U)
3437 StartIdx = EltIdx - (EltIdx % Scale);
3438 if (EltIdx != StartIdx + j)
3439 return SDOperand();
3440 }
3441 if (StartIdx == ~0U)
3442 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3443 else
3444 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003445 }
3446
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003447 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3448 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3449 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3450 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3451 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003452}
3453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454SDOperand
3455X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3456 SDOperand V1 = Op.getOperand(0);
3457 SDOperand V2 = Op.getOperand(1);
3458 SDOperand PermMask = Op.getOperand(2);
3459 MVT::ValueType VT = Op.getValueType();
3460 unsigned NumElems = PermMask.getNumOperands();
3461 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3462 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3463 bool V1IsSplat = false;
3464 bool V2IsSplat = false;
3465
3466 if (isUndefShuffle(Op.Val))
3467 return DAG.getNode(ISD::UNDEF, VT);
3468
3469 if (isZeroShuffle(Op.Val))
3470 return getZeroVector(VT, DAG);
3471
3472 if (isIdentityMask(PermMask.Val))
3473 return V1;
3474 else if (isIdentityMask(PermMask.Val, true))
3475 return V2;
3476
3477 if (isSplatMask(PermMask.Val)) {
3478 if (NumElems <= 4) return Op;
3479 // Promote it to a v4i32 splat.
3480 return PromoteSplat(Op, DAG);
3481 }
3482
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003483 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3484 // do it!
3485 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3486 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3487 if (NewOp.Val)
3488 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3489 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3490 // FIXME: Figure out a cleaner way to do this.
3491 // Try to make use of movq to zero out the top part.
3492 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3493 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3494 if (NewOp.Val) {
3495 SDOperand NewV1 = NewOp.getOperand(0);
3496 SDOperand NewV2 = NewOp.getOperand(1);
3497 SDOperand NewMask = NewOp.getOperand(2);
3498 if (isCommutedMOVL(NewMask.Val, true, false)) {
3499 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3500 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3501 NewV1, NewV2, getMOVLMask(2, DAG));
3502 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3503 }
3504 }
3505 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3506 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3507 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3508 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3509 }
3510 }
3511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003512 if (X86::isMOVLMask(PermMask.Val))
3513 return (V1IsUndef) ? V2 : Op;
3514
3515 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3516 X86::isMOVSLDUPMask(PermMask.Val) ||
3517 X86::isMOVHLPSMask(PermMask.Val) ||
3518 X86::isMOVHPMask(PermMask.Val) ||
3519 X86::isMOVLPMask(PermMask.Val))
3520 return Op;
3521
3522 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3523 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3524 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3525
3526 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003527 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3528 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 V1IsSplat = isSplatVector(V1.Val);
3530 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003531
3532 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003533 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3534 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3535 std::swap(V1IsSplat, V2IsSplat);
3536 std::swap(V1IsUndef, V2IsUndef);
3537 Commuted = true;
3538 }
3539
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003540 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003541 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3542 if (V2IsUndef) return V1;
3543 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3544 if (V2IsSplat) {
3545 // V2 is a splat, so the mask may be malformed. That is, it may point
3546 // to any V2 element. The instruction selectior won't like this. Get
3547 // a corrected mask and commute to form a proper MOVS{S|D}.
3548 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3549 if (NewMask.Val != PermMask.Val)
3550 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3551 }
3552 return Op;
3553 }
3554
3555 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3556 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3557 X86::isUNPCKLMask(PermMask.Val) ||
3558 X86::isUNPCKHMask(PermMask.Val))
3559 return Op;
3560
3561 if (V2IsSplat) {
3562 // Normalize mask so all entries that point to V2 points to its first
3563 // element then try to match unpck{h|l} again. If match, return a
3564 // new vector_shuffle with the corrected mask.
3565 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3566 if (NewMask.Val != PermMask.Val) {
3567 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3568 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3569 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3570 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3571 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3572 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3573 }
3574 }
3575 }
3576
3577 // Normalize the node to match x86 shuffle ops if needed
3578 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3579 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3580
3581 if (Commuted) {
3582 // Commute is back and try unpck* again.
3583 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3584 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3585 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3586 X86::isUNPCKLMask(PermMask.Val) ||
3587 X86::isUNPCKHMask(PermMask.Val))
3588 return Op;
3589 }
3590
3591 // If VT is integer, try PSHUF* first, then SHUFP*.
3592 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003593 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3594 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3595 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3596 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597 X86::isPSHUFHWMask(PermMask.Val) ||
3598 X86::isPSHUFLWMask(PermMask.Val)) {
3599 if (V2.getOpcode() != ISD::UNDEF)
3600 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3601 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3602 return Op;
3603 }
3604
3605 if (X86::isSHUFPMask(PermMask.Val) &&
3606 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3607 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003608 } else {
3609 // Floating point cases in the other order.
3610 if (X86::isSHUFPMask(PermMask.Val))
3611 return Op;
3612 if (X86::isPSHUFDMask(PermMask.Val) ||
3613 X86::isPSHUFHWMask(PermMask.Val) ||
3614 X86::isPSHUFLWMask(PermMask.Val)) {
3615 if (V2.getOpcode() != ISD::UNDEF)
3616 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3617 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3618 return Op;
3619 }
3620 }
3621
Evan Cheng75184a92007-12-11 01:46:18 +00003622 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3623 if (VT == MVT::v8i16) {
3624 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3625 if (NewOp.Val)
3626 return NewOp;
3627 }
3628
3629 // Handle all 4 wide cases with a number of shuffles.
3630 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003631 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003632 MVT::ValueType MaskVT = PermMask.getValueType();
3633 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3634 SmallVector<std::pair<int, int>, 8> Locs;
3635 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003636 SmallVector<SDOperand, 8> Mask1(NumElems,
3637 DAG.getNode(ISD::UNDEF, MaskEVT));
3638 SmallVector<SDOperand, 8> Mask2(NumElems,
3639 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003640 unsigned NumHi = 0;
3641 unsigned NumLo = 0;
3642 // If no more than two elements come from either vector. This can be
3643 // implemented with two shuffles. First shuffle gather the elements.
3644 // The second shuffle, which takes the first shuffle as both of its
3645 // vector operands, put the elements into the right order.
3646 for (unsigned i = 0; i != NumElems; ++i) {
3647 SDOperand Elt = PermMask.getOperand(i);
3648 if (Elt.getOpcode() == ISD::UNDEF) {
3649 Locs[i] = std::make_pair(-1, -1);
3650 } else {
3651 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3652 if (Val < NumElems) {
3653 Locs[i] = std::make_pair(0, NumLo);
3654 Mask1[NumLo] = Elt;
3655 NumLo++;
3656 } else {
3657 Locs[i] = std::make_pair(1, NumHi);
3658 if (2+NumHi < NumElems)
3659 Mask1[2+NumHi] = Elt;
3660 NumHi++;
3661 }
3662 }
3663 }
3664 if (NumLo <= 2 && NumHi <= 2) {
3665 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3666 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3667 &Mask1[0], Mask1.size()));
3668 for (unsigned i = 0; i != NumElems; ++i) {
3669 if (Locs[i].first == -1)
3670 continue;
3671 else {
3672 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3673 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3674 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3675 }
3676 }
3677
3678 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3679 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3680 &Mask2[0], Mask2.size()));
3681 }
3682
3683 // Break it into (shuffle shuffle_hi, shuffle_lo).
3684 Locs.clear();
3685 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3686 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3687 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3688 unsigned MaskIdx = 0;
3689 unsigned LoIdx = 0;
3690 unsigned HiIdx = NumElems/2;
3691 for (unsigned i = 0; i != NumElems; ++i) {
3692 if (i == NumElems/2) {
3693 MaskPtr = &HiMask;
3694 MaskIdx = 1;
3695 LoIdx = 0;
3696 HiIdx = NumElems/2;
3697 }
3698 SDOperand Elt = PermMask.getOperand(i);
3699 if (Elt.getOpcode() == ISD::UNDEF) {
3700 Locs[i] = std::make_pair(-1, -1);
3701 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3702 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3703 (*MaskPtr)[LoIdx] = Elt;
3704 LoIdx++;
3705 } else {
3706 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3707 (*MaskPtr)[HiIdx] = Elt;
3708 HiIdx++;
3709 }
3710 }
3711
3712 SDOperand LoShuffle =
3713 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3714 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3715 &LoMask[0], LoMask.size()));
3716 SDOperand HiShuffle =
3717 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3718 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3719 &HiMask[0], HiMask.size()));
3720 SmallVector<SDOperand, 8> MaskOps;
3721 for (unsigned i = 0; i != NumElems; ++i) {
3722 if (Locs[i].first == -1) {
3723 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3724 } else {
3725 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3726 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3727 }
3728 }
3729 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3730 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3731 &MaskOps[0], MaskOps.size()));
3732 }
3733
3734 return SDOperand();
3735}
3736
3737SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003738X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3739 SelectionDAG &DAG) {
3740 MVT::ValueType VT = Op.getValueType();
3741 if (MVT::getSizeInBits(VT) == 8) {
3742 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3743 Op.getOperand(0), Op.getOperand(1));
3744 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3745 DAG.getValueType(VT));
3746 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3747 } else if (MVT::getSizeInBits(VT) == 16) {
3748 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3749 Op.getOperand(0), Op.getOperand(1));
3750 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3751 DAG.getValueType(VT));
3752 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3753 }
3754 return SDOperand();
3755}
3756
3757
3758SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003759X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3760 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3761 return SDOperand();
3762
Nate Begemand77e59e2008-02-11 04:19:36 +00003763 if (Subtarget->hasSSE41())
3764 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3765
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003766 MVT::ValueType VT = Op.getValueType();
3767 // TODO: handle v16i8.
3768 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003769 SDOperand Vec = Op.getOperand(0);
3770 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3771 if (Idx == 0)
3772 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3773 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3774 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3775 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003776 // Transform it so it match pextrw which produces a 32-bit result.
3777 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3778 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3779 Op.getOperand(0), Op.getOperand(1));
3780 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3781 DAG.getValueType(VT));
3782 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3783 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003784 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3785 if (Idx == 0)
3786 return Op;
3787 // SHUFPS the element to the lowest double word, then movss.
3788 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3789 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003790 IdxVec.
3791 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3792 IdxVec.
3793 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3794 IdxVec.
3795 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3796 IdxVec.
3797 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003798 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3799 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003800 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003801 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3802 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3803 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003804 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003805 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003806 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3807 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3808 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003809 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3810 if (Idx == 0)
3811 return Op;
3812
3813 // UNPCKHPD the element to the lowest double word, then movsd.
3814 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3815 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3816 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3817 SmallVector<SDOperand, 8> IdxVec;
3818 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003819 IdxVec.
3820 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3822 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003823 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003824 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3825 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3826 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003827 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003828 }
3829
3830 return SDOperand();
3831}
3832
3833SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003834X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3835 MVT::ValueType VT = Op.getValueType();
3836 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3837
3838 SDOperand N0 = Op.getOperand(0);
3839 SDOperand N1 = Op.getOperand(1);
3840 SDOperand N2 = Op.getOperand(2);
3841
3842 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3843 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3844 : X86ISD::PINSRW;
3845 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3846 // argument.
3847 if (N1.getValueType() != MVT::i32)
3848 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3849 if (N2.getValueType() != MVT::i32)
3850 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3851 return DAG.getNode(Opc, VT, N0, N1, N2);
3852 } else if (EVT == MVT::f32) {
3853 // Bits [7:6] of the constant are the source select. This will always be
3854 // zero here. The DAG Combiner may combine an extract_elt index into these
3855 // bits. For example (insert (extract, 3), 2) could be matched by putting
3856 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3857 // Bits [5:4] of the constant are the destination select. This is the
3858 // value of the incoming immediate.
3859 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3860 // combine either bitwise AND or insert of float 0.0 to set these bits.
3861 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3862 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3863 }
3864 return SDOperand();
3865}
3866
3867SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003868X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003869 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003870 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003871
3872 if (Subtarget->hasSSE41())
3873 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3874
Evan Chenge12a7eb2007-12-12 07:55:34 +00003875 if (EVT == MVT::i8)
3876 return SDOperand();
3877
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003878 SDOperand N0 = Op.getOperand(0);
3879 SDOperand N1 = Op.getOperand(1);
3880 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003881
3882 if (MVT::getSizeInBits(EVT) == 16) {
3883 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3884 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885 if (N1.getValueType() != MVT::i32)
3886 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3887 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003888 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003890 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003891 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003892}
3893
3894SDOperand
3895X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3896 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003897 MVT::ValueType VT = MVT::v2i32;
3898 switch (Op.getValueType()) {
3899 default: break;
3900 case MVT::v16i8:
3901 case MVT::v8i16:
3902 VT = MVT::v4i32;
3903 break;
3904 }
3905 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3906 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003907}
3908
3909// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3910// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3911// one of the above mentioned nodes. It has to be wrapped because otherwise
3912// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3913// be used to form addressing mode. These wrapped nodes will be selected
3914// into MOV32ri.
3915SDOperand
3916X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3917 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3918 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3919 getPointerTy(),
3920 CP->getAlignment());
3921 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3922 // With PIC, the address is actually $g + Offset.
3923 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3924 !Subtarget->isPICStyleRIPRel()) {
3925 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3926 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3927 Result);
3928 }
3929
3930 return Result;
3931}
3932
3933SDOperand
3934X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3935 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3936 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003937 // If it's a debug information descriptor, don't mess with it.
3938 if (DAG.isVerifiedDebugInfoDesc(Op))
3939 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003940 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3941 // With PIC, the address is actually $g + Offset.
3942 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3943 !Subtarget->isPICStyleRIPRel()) {
3944 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3945 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3946 Result);
3947 }
3948
3949 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3950 // load the value at address GV, not the value of GV itself. This means that
3951 // the GlobalAddress must be in the base or index register of the address, not
3952 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3953 // The same applies for external symbols during PIC codegen
3954 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003955 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003956 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957
3958 return Result;
3959}
3960
3961// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3962static SDOperand
3963LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3964 const MVT::ValueType PtrVT) {
3965 SDOperand InFlag;
3966 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3967 DAG.getNode(X86ISD::GlobalBaseReg,
3968 PtrVT), InFlag);
3969 InFlag = Chain.getValue(1);
3970
3971 // emit leal symbol@TLSGD(,%ebx,1), %eax
3972 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3973 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3974 GA->getValueType(0),
3975 GA->getOffset());
3976 SDOperand Ops[] = { Chain, TGA, InFlag };
3977 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3978 InFlag = Result.getValue(2);
3979 Chain = Result.getValue(1);
3980
3981 // call ___tls_get_addr. This function receives its argument in
3982 // the register EAX.
3983 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3984 InFlag = Chain.getValue(1);
3985
3986 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3987 SDOperand Ops1[] = { Chain,
3988 DAG.getTargetExternalSymbol("___tls_get_addr",
3989 PtrVT),
3990 DAG.getRegister(X86::EAX, PtrVT),
3991 DAG.getRegister(X86::EBX, PtrVT),
3992 InFlag };
3993 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3994 InFlag = Chain.getValue(1);
3995
3996 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3997}
3998
3999// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4000// "local exec" model.
4001static SDOperand
4002LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4003 const MVT::ValueType PtrVT) {
4004 // Get the Thread Pointer
4005 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4006 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4007 // exec)
4008 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4009 GA->getValueType(0),
4010 GA->getOffset());
4011 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4012
4013 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004014 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004015 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016
4017 // The address of the thread local variable is the add of the thread
4018 // pointer with the offset of the variable.
4019 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4020}
4021
4022SDOperand
4023X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4024 // TODO: implement the "local dynamic" model
4025 // TODO: implement the "initial exec"model for pic executables
4026 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4027 "TLS not implemented for non-ELF and 64-bit targets");
4028 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4029 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4030 // otherwise use the "Local Exec"TLS Model
4031 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4032 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4033 else
4034 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4035}
4036
4037SDOperand
4038X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4039 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4040 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4041 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4042 // With PIC, the address is actually $g + Offset.
4043 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4044 !Subtarget->isPICStyleRIPRel()) {
4045 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4046 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4047 Result);
4048 }
4049
4050 return Result;
4051}
4052
4053SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4054 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4055 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4056 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4057 // With PIC, the address is actually $g + Offset.
4058 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4059 !Subtarget->isPICStyleRIPRel()) {
4060 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4061 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4062 Result);
4063 }
4064
4065 return Result;
4066}
4067
Chris Lattner62814a32007-10-17 06:02:13 +00004068/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4069/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00004071 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4072 "Not an i64 shift!");
4073 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4074 SDOperand ShOpLo = Op.getOperand(0);
4075 SDOperand ShOpHi = Op.getOperand(1);
4076 SDOperand ShAmt = Op.getOperand(2);
4077 SDOperand Tmp1 = isSRA ?
4078 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4079 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080
Chris Lattner62814a32007-10-17 06:02:13 +00004081 SDOperand Tmp2, Tmp3;
4082 if (Op.getOpcode() == ISD::SHL_PARTS) {
4083 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4084 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4085 } else {
4086 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4087 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4088 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004089
Chris Lattner62814a32007-10-17 06:02:13 +00004090 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4091 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4092 DAG.getConstant(32, MVT::i8));
4093 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4094 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004095
Chris Lattner62814a32007-10-17 06:02:13 +00004096 SDOperand Hi, Lo;
4097 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4098 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4099 SmallVector<SDOperand, 4> Ops;
4100 if (Op.getOpcode() == ISD::SHL_PARTS) {
4101 Ops.push_back(Tmp2);
4102 Ops.push_back(Tmp3);
4103 Ops.push_back(CC);
4104 Ops.push_back(Cond);
4105 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004107 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004108 Ops.push_back(Tmp3);
4109 Ops.push_back(Tmp1);
4110 Ops.push_back(CC);
4111 Ops.push_back(Cond);
4112 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4113 } else {
4114 Ops.push_back(Tmp2);
4115 Ops.push_back(Tmp3);
4116 Ops.push_back(CC);
4117 Ops.push_back(Cond);
4118 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4119
4120 Ops.clear();
4121 Ops.push_back(Tmp3);
4122 Ops.push_back(Tmp1);
4123 Ops.push_back(CC);
4124 Ops.push_back(Cond);
4125 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4126 }
4127
4128 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4129 Ops.clear();
4130 Ops.push_back(Lo);
4131 Ops.push_back(Hi);
4132 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133}
4134
4135SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4136 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4137 Op.getOperand(0).getValueType() >= MVT::i16 &&
4138 "Unknown SINT_TO_FP to lower!");
4139
4140 SDOperand Result;
4141 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4142 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4143 MachineFunction &MF = DAG.getMachineFunction();
4144 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4145 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4146 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004147 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004148 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004149 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150
Dale Johannesen2fc20782007-09-14 22:26:36 +00004151 // These are really Legal; caller falls through into that case.
Chris Lattnercf515b52008-01-16 06:24:21 +00004152 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004153 return Result;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004154 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen958b08b2007-09-19 23:55:34 +00004155 Subtarget->is64Bit())
4156 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00004157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158 // Build the FILD
4159 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004160 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004161 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4163 else
4164 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4165 SmallVector<SDOperand, 8> Ops;
4166 Ops.push_back(Chain);
4167 Ops.push_back(StackSlot);
4168 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00004169 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004170 Tys, &Ops[0], Ops.size());
4171
Dale Johannesen2fc20782007-09-14 22:26:36 +00004172 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004173 Chain = Result.getValue(1);
4174 SDOperand InFlag = Result.getValue(2);
4175
4176 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4177 // shouldn't be necessary except that RFP cannot be live across
4178 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4179 MachineFunction &MF = DAG.getMachineFunction();
4180 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4181 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4182 Tys = DAG.getVTList(MVT::Other);
4183 SmallVector<SDOperand, 8> Ops;
4184 Ops.push_back(Chain);
4185 Ops.push_back(Result);
4186 Ops.push_back(StackSlot);
4187 Ops.push_back(DAG.getValueType(Op.getValueType()));
4188 Ops.push_back(InFlag);
4189 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004190 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004191 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192 }
4193
4194 return Result;
4195}
4196
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004197std::pair<SDOperand,SDOperand> X86TargetLowering::
4198FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004199 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4200 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201
Dale Johannesen2fc20782007-09-14 22:26:36 +00004202 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004203 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004204 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004205 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004206 if (Subtarget->is64Bit() &&
4207 Op.getValueType() == MVT::i64 &&
4208 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004209 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004210
Evan Cheng05441e62007-10-15 20:11:21 +00004211 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4212 // stack slot.
4213 MachineFunction &MF = DAG.getMachineFunction();
4214 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4215 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4216 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004217 unsigned Opc;
4218 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004219 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4220 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4221 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4222 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004223 }
4224
4225 SDOperand Chain = DAG.getEntryNode();
4226 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004227 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004229 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004230 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004231 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4232 SDOperand Ops[] = {
4233 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4234 };
4235 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4236 Chain = Value.getValue(1);
4237 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4238 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4239 }
4240
4241 // Build the FP_TO_INT*_IN_MEM
4242 SDOperand Ops[] = { Chain, Value, StackSlot };
4243 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4244
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004245 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246}
4247
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004248SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004249 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4250 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4251 if (FIST.Val == 0) return SDOperand();
4252
4253 // Load the result.
4254 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4255}
4256
4257SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4258 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4259 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4260 if (FIST.Val == 0) return 0;
4261
4262 // Return an i64 load from the stack slot.
4263 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4264
4265 // Use a MERGE_VALUES node to drop the chain result value.
4266 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4267}
4268
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004269SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4270 MVT::ValueType VT = Op.getValueType();
4271 MVT::ValueType EltVT = VT;
4272 if (MVT::isVector(VT))
4273 EltVT = MVT::getVectorElementType(VT);
4274 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4275 std::vector<Constant*> CV;
4276 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004277 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278 CV.push_back(C);
4279 CV.push_back(C);
4280 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004281 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282 CV.push_back(C);
4283 CV.push_back(C);
4284 CV.push_back(C);
4285 CV.push_back(C);
4286 }
Dan Gohman11821702007-07-27 17:16:43 +00004287 Constant *C = ConstantVector::get(CV);
4288 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004289 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004290 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004291 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4293}
4294
4295SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4296 MVT::ValueType VT = Op.getValueType();
4297 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004298 unsigned EltNum = 1;
4299 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004300 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004301 EltNum = MVT::getVectorNumElements(VT);
4302 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4304 std::vector<Constant*> CV;
4305 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004306 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307 CV.push_back(C);
4308 CV.push_back(C);
4309 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004310 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004311 CV.push_back(C);
4312 CV.push_back(C);
4313 CV.push_back(C);
4314 CV.push_back(C);
4315 }
Dan Gohman11821702007-07-27 17:16:43 +00004316 Constant *C = ConstantVector::get(CV);
4317 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004318 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004319 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004320 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004321 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004322 return DAG.getNode(ISD::BIT_CONVERT, VT,
4323 DAG.getNode(ISD::XOR, MVT::v2i64,
4324 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4325 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4326 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004327 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4328 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329}
4330
4331SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4332 SDOperand Op0 = Op.getOperand(0);
4333 SDOperand Op1 = Op.getOperand(1);
4334 MVT::ValueType VT = Op.getValueType();
4335 MVT::ValueType SrcVT = Op1.getValueType();
4336 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4337
4338 // If second operand is smaller, extend it first.
4339 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4340 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4341 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004342 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004344 // And if it is bigger, shrink it first.
4345 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004346 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004347 SrcVT = VT;
4348 SrcTy = MVT::getTypeForValueType(SrcVT);
4349 }
4350
4351 // At this point the operands and the result should have the same
4352 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004353
4354 // First get the sign bit of second operand.
4355 std::vector<Constant*> CV;
4356 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004357 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4358 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004360 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4361 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4362 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4363 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364 }
Dan Gohman11821702007-07-27 17:16:43 +00004365 Constant *C = ConstantVector::get(CV);
4366 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004367 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004368 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004369 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004370 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4371
4372 // Shift sign bit right or left if the two operands have different types.
4373 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4374 // Op0 is MVT::f32, Op1 is MVT::f64.
4375 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4376 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4377 DAG.getConstant(32, MVT::i32));
4378 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4379 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004380 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004381 }
4382
4383 // Clear first operand sign bit.
4384 CV.clear();
4385 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004386 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4387 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004389 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4390 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4391 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4392 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004393 }
Dan Gohman11821702007-07-27 17:16:43 +00004394 C = ConstantVector::get(CV);
4395 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004396 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004397 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004398 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4400
4401 // Or the value with the sign bit.
4402 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4403}
4404
Evan Cheng621216e2007-09-29 00:00:36 +00004405SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004406 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004407 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004408 SDOperand Op0 = Op.getOperand(0);
4409 SDOperand Op1 = Op.getOperand(1);
4410 SDOperand CC = Op.getOperand(2);
4411 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4412 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4413 unsigned X86CC;
4414
Evan Cheng950aac02007-09-25 01:57:46 +00004415 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004416 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004417 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4418 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004419 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004420 }
Evan Cheng950aac02007-09-25 01:57:46 +00004421
4422 assert(isFP && "Illegal integer SetCC!");
4423
Evan Cheng621216e2007-09-29 00:00:36 +00004424 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004425 switch (SetCCOpcode) {
4426 default: assert(false && "Illegal floating point SetCC!");
4427 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004428 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004429 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004430 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004431 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4432 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4433 }
4434 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004435 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004436 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004437 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004438 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4439 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4440 }
4441 }
4442}
4443
4444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4446 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447 SDOperand Cond = Op.getOperand(0);
4448 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449
4450 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004451 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452
Evan Cheng50d37ab2007-10-08 22:16:29 +00004453 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4454 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 if (Cond.getOpcode() == X86ISD::SETCC) {
4456 CC = Cond.getOperand(0);
4457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458 SDOperand Cmp = Cond.getOperand(1);
4459 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004460 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004461
Evan Cheng50d37ab2007-10-08 22:16:29 +00004462 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004463 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004464 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004465 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004466
Evan Cheng621216e2007-09-29 00:00:36 +00004467 if ((Opc == X86ISD::CMP ||
4468 Opc == X86ISD::COMI ||
4469 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004470 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004471 addTest = false;
4472 }
4473 }
4474
4475 if (addTest) {
4476 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004477 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004478 }
4479
4480 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4481 MVT::Flag);
4482 SmallVector<SDOperand, 4> Ops;
4483 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4484 // condition is true.
4485 Ops.push_back(Op.getOperand(2));
4486 Ops.push_back(Op.getOperand(1));
4487 Ops.push_back(CC);
4488 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004489 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004490}
4491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4493 bool addTest = true;
4494 SDOperand Chain = Op.getOperand(0);
4495 SDOperand Cond = Op.getOperand(1);
4496 SDOperand Dest = Op.getOperand(2);
4497 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498
4499 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004500 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004501
Evan Cheng50d37ab2007-10-08 22:16:29 +00004502 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4503 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004504 if (Cond.getOpcode() == X86ISD::SETCC) {
4505 CC = Cond.getOperand(0);
4506
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507 SDOperand Cmp = Cond.getOperand(1);
4508 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004509 if (Opc == X86ISD::CMP ||
4510 Opc == X86ISD::COMI ||
4511 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004512 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004513 addTest = false;
4514 }
4515 }
4516
4517 if (addTest) {
4518 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004519 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004520 }
Evan Cheng621216e2007-09-29 00:00:36 +00004521 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004522 Chain, Op.getOperand(2), CC, Cond);
4523}
4524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004525
4526// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4527// Calls to _alloca is needed to probe the stack when allocating more than 4k
4528// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4529// that the guard pages used by the OS virtual memory manager are allocated in
4530// correct sequence.
4531SDOperand
4532X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4533 SelectionDAG &DAG) {
4534 assert(Subtarget->isTargetCygMing() &&
4535 "This should be used only on Cygwin/Mingw targets");
4536
4537 // Get the inputs.
4538 SDOperand Chain = Op.getOperand(0);
4539 SDOperand Size = Op.getOperand(1);
4540 // FIXME: Ensure alignment here
4541
4542 SDOperand Flag;
4543
4544 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004545 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546
4547 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4548 Flag = Chain.getValue(1);
4549
4550 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4551 SDOperand Ops[] = { Chain,
4552 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4553 DAG.getRegister(X86::EAX, IntPtr),
4554 Flag };
4555 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4556 Flag = Chain.getValue(1);
4557
4558 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4559
4560 std::vector<MVT::ValueType> Tys;
4561 Tys.push_back(SPTy);
4562 Tys.push_back(MVT::Other);
4563 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4564 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4565}
4566
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004567SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4568 SDOperand InFlag(0, 0);
4569 SDOperand Chain = Op.getOperand(0);
4570 unsigned Align =
4571 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4572 if (Align == 0) Align = 1;
4573
4574 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004575 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004576 // The libc version is likely to be faster for these cases. It can use the
4577 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004579 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 MVT::ValueType IntPtr = getPointerTy();
4581 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4582 TargetLowering::ArgListTy Args;
4583 TargetLowering::ArgListEntry Entry;
4584 Entry.Node = Op.getOperand(1);
4585 Entry.Ty = IntPtrTy;
4586 Args.push_back(Entry);
4587 // Extend the unsigned i8 argument to be an int value for the call.
4588 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4589 Entry.Ty = IntPtrTy;
4590 Args.push_back(Entry);
4591 Entry.Node = Op.getOperand(3);
4592 Args.push_back(Entry);
4593 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004594 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4595 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004596 return CallResult.second;
4597 }
4598
4599 MVT::ValueType AVT;
4600 SDOperand Count;
4601 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4602 unsigned BytesLeft = 0;
4603 bool TwoRepStos = false;
4604 if (ValC) {
4605 unsigned ValReg;
4606 uint64_t Val = ValC->getValue() & 255;
4607
4608 // If the value is a constant, then we can potentially use larger sets.
4609 switch (Align & 3) {
4610 case 2: // WORD aligned
4611 AVT = MVT::i16;
4612 ValReg = X86::AX;
4613 Val = (Val << 8) | Val;
4614 break;
4615 case 0: // DWORD aligned
4616 AVT = MVT::i32;
4617 ValReg = X86::EAX;
4618 Val = (Val << 8) | Val;
4619 Val = (Val << 16) | Val;
4620 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4621 AVT = MVT::i64;
4622 ValReg = X86::RAX;
4623 Val = (Val << 32) | Val;
4624 }
4625 break;
4626 default: // Byte aligned
4627 AVT = MVT::i8;
4628 ValReg = X86::AL;
4629 Count = Op.getOperand(3);
4630 break;
4631 }
4632
4633 if (AVT > MVT::i8) {
4634 if (I) {
4635 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004636 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637 BytesLeft = I->getValue() % UBytes;
4638 } else {
4639 assert(AVT >= MVT::i32 &&
4640 "Do not use rep;stos if not at least DWORD aligned");
4641 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4642 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4643 TwoRepStos = true;
4644 }
4645 }
4646
4647 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4648 InFlag);
4649 InFlag = Chain.getValue(1);
4650 } else {
4651 AVT = MVT::i8;
4652 Count = Op.getOperand(3);
4653 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4654 InFlag = Chain.getValue(1);
4655 }
4656
4657 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4658 Count, InFlag);
4659 InFlag = Chain.getValue(1);
4660 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4661 Op.getOperand(1), InFlag);
4662 InFlag = Chain.getValue(1);
4663
4664 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4665 SmallVector<SDOperand, 8> Ops;
4666 Ops.push_back(Chain);
4667 Ops.push_back(DAG.getValueType(AVT));
4668 Ops.push_back(InFlag);
4669 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4670
4671 if (TwoRepStos) {
4672 InFlag = Chain.getValue(1);
4673 Count = Op.getOperand(3);
4674 MVT::ValueType CVT = Count.getValueType();
4675 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4676 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4677 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4678 Left, InFlag);
4679 InFlag = Chain.getValue(1);
4680 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4681 Ops.clear();
4682 Ops.push_back(Chain);
4683 Ops.push_back(DAG.getValueType(MVT::i8));
4684 Ops.push_back(InFlag);
4685 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4686 } else if (BytesLeft) {
4687 // Issue stores for the last 1 - 7 bytes.
4688 SDOperand Value;
4689 unsigned Val = ValC->getValue() & 255;
4690 unsigned Offset = I->getValue() - BytesLeft;
4691 SDOperand DstAddr = Op.getOperand(1);
4692 MVT::ValueType AddrVT = DstAddr.getValueType();
4693 if (BytesLeft >= 4) {
4694 Val = (Val << 8) | Val;
4695 Val = (Val << 16) | Val;
4696 Value = DAG.getConstant(Val, MVT::i32);
4697 Chain = DAG.getStore(Chain, Value,
4698 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4699 DAG.getConstant(Offset, AddrVT)),
4700 NULL, 0);
4701 BytesLeft -= 4;
4702 Offset += 4;
4703 }
4704 if (BytesLeft >= 2) {
4705 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4706 Chain = DAG.getStore(Chain, Value,
4707 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4708 DAG.getConstant(Offset, AddrVT)),
4709 NULL, 0);
4710 BytesLeft -= 2;
4711 Offset += 2;
4712 }
4713 if (BytesLeft == 1) {
4714 Value = DAG.getConstant(Val, MVT::i8);
4715 Chain = DAG.getStore(Chain, Value,
4716 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4717 DAG.getConstant(Offset, AddrVT)),
4718 NULL, 0);
4719 }
4720 }
4721
4722 return Chain;
4723}
4724
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004725SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4726 SDOperand Dest,
4727 SDOperand Source,
4728 unsigned Size,
4729 unsigned Align,
4730 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004731 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004732 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004733 switch (Align & 3) {
4734 case 2: // WORD aligned
4735 AVT = MVT::i16;
4736 break;
4737 case 0: // DWORD aligned
4738 AVT = MVT::i32;
4739 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4740 AVT = MVT::i64;
4741 break;
4742 default: // Byte aligned
4743 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004744 break;
4745 }
4746
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004747 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004748 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004749 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004750
4751 SDOperand InFlag(0, 0);
4752 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4753 Count, InFlag);
4754 InFlag = Chain.getValue(1);
4755 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004756 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 InFlag = Chain.getValue(1);
4758 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004759 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004760 InFlag = Chain.getValue(1);
4761
4762 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4763 SmallVector<SDOperand, 8> Ops;
4764 Ops.push_back(Chain);
4765 Ops.push_back(DAG.getValueType(AVT));
4766 Ops.push_back(InFlag);
4767 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4768
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004769 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004771 unsigned Offset = Size - BytesLeft;
4772 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004773 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004774 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004775 MVT::ValueType SrcVT = SrcAddr.getValueType();
4776 SDOperand Value;
4777 if (BytesLeft >= 4) {
4778 Value = DAG.getLoad(MVT::i32, Chain,
4779 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4780 DAG.getConstant(Offset, SrcVT)),
4781 NULL, 0);
4782 Chain = Value.getValue(1);
4783 Chain = DAG.getStore(Chain, Value,
4784 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4785 DAG.getConstant(Offset, DstVT)),
4786 NULL, 0);
4787 BytesLeft -= 4;
4788 Offset += 4;
4789 }
4790 if (BytesLeft >= 2) {
4791 Value = DAG.getLoad(MVT::i16, Chain,
4792 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4793 DAG.getConstant(Offset, SrcVT)),
4794 NULL, 0);
4795 Chain = Value.getValue(1);
4796 Chain = DAG.getStore(Chain, Value,
4797 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4798 DAG.getConstant(Offset, DstVT)),
4799 NULL, 0);
4800 BytesLeft -= 2;
4801 Offset += 2;
4802 }
4803
4804 if (BytesLeft == 1) {
4805 Value = DAG.getLoad(MVT::i8, Chain,
4806 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4807 DAG.getConstant(Offset, SrcVT)),
4808 NULL, 0);
4809 Chain = Value.getValue(1);
4810 Chain = DAG.getStore(Chain, Value,
4811 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4812 DAG.getConstant(Offset, DstVT)),
4813 NULL, 0);
4814 }
4815 }
4816
4817 return Chain;
4818}
4819
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004820/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4821SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004822 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004823 SDOperand TheChain = N->getOperand(0);
4824 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004826 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4827 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4828 MVT::i64, rax.getValue(2));
4829 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 DAG.getConstant(32, MVT::i8));
4831 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004832 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004833 };
4834
4835 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004836 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004837 }
4838
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004839 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4840 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4841 MVT::i32, eax.getValue(2));
4842 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4843 SDOperand Ops[] = { eax, edx };
4844 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4845
4846 // Use a MERGE_VALUES to return the value and chain.
4847 Ops[1] = edx.getValue(1);
4848 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4849 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004850}
4851
4852SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004853 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004854
4855 if (!Subtarget->is64Bit()) {
4856 // vastart just stores the address of the VarArgsFrameIndex slot into the
4857 // memory location argument.
4858 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004859 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004860 }
4861
4862 // __va_list_tag:
4863 // gp_offset (0 - 6 * 8)
4864 // fp_offset (48 - 48 + 8 * 16)
4865 // overflow_arg_area (point to parameters coming in memory).
4866 // reg_save_area
4867 SmallVector<SDOperand, 8> MemOps;
4868 SDOperand FIN = Op.getOperand(1);
4869 // Store gp_offset
4870 SDOperand Store = DAG.getStore(Op.getOperand(0),
4871 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004872 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 MemOps.push_back(Store);
4874
4875 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004876 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004877 Store = DAG.getStore(Op.getOperand(0),
4878 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004879 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 MemOps.push_back(Store);
4881
4882 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004883 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004885 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886 MemOps.push_back(Store);
4887
4888 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004889 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004890 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004891 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004892 MemOps.push_back(Store);
4893 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4894}
4895
4896SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4897 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4898 SDOperand Chain = Op.getOperand(0);
4899 SDOperand DstPtr = Op.getOperand(1);
4900 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004901 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4902 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903
Dan Gohman12a9c082008-02-06 22:27:42 +00004904 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004905 Chain = SrcPtr.getValue(1);
4906 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004907 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004908 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004909 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004910 if (i == 2)
4911 break;
4912 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004913 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004914 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004915 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916 }
4917 return Chain;
4918}
4919
4920SDOperand
4921X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4922 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4923 switch (IntNo) {
4924 default: return SDOperand(); // Don't custom lower most intrinsics.
4925 // Comparison intrinsics.
4926 case Intrinsic::x86_sse_comieq_ss:
4927 case Intrinsic::x86_sse_comilt_ss:
4928 case Intrinsic::x86_sse_comile_ss:
4929 case Intrinsic::x86_sse_comigt_ss:
4930 case Intrinsic::x86_sse_comige_ss:
4931 case Intrinsic::x86_sse_comineq_ss:
4932 case Intrinsic::x86_sse_ucomieq_ss:
4933 case Intrinsic::x86_sse_ucomilt_ss:
4934 case Intrinsic::x86_sse_ucomile_ss:
4935 case Intrinsic::x86_sse_ucomigt_ss:
4936 case Intrinsic::x86_sse_ucomige_ss:
4937 case Intrinsic::x86_sse_ucomineq_ss:
4938 case Intrinsic::x86_sse2_comieq_sd:
4939 case Intrinsic::x86_sse2_comilt_sd:
4940 case Intrinsic::x86_sse2_comile_sd:
4941 case Intrinsic::x86_sse2_comigt_sd:
4942 case Intrinsic::x86_sse2_comige_sd:
4943 case Intrinsic::x86_sse2_comineq_sd:
4944 case Intrinsic::x86_sse2_ucomieq_sd:
4945 case Intrinsic::x86_sse2_ucomilt_sd:
4946 case Intrinsic::x86_sse2_ucomile_sd:
4947 case Intrinsic::x86_sse2_ucomigt_sd:
4948 case Intrinsic::x86_sse2_ucomige_sd:
4949 case Intrinsic::x86_sse2_ucomineq_sd: {
4950 unsigned Opc = 0;
4951 ISD::CondCode CC = ISD::SETCC_INVALID;
4952 switch (IntNo) {
4953 default: break;
4954 case Intrinsic::x86_sse_comieq_ss:
4955 case Intrinsic::x86_sse2_comieq_sd:
4956 Opc = X86ISD::COMI;
4957 CC = ISD::SETEQ;
4958 break;
4959 case Intrinsic::x86_sse_comilt_ss:
4960 case Intrinsic::x86_sse2_comilt_sd:
4961 Opc = X86ISD::COMI;
4962 CC = ISD::SETLT;
4963 break;
4964 case Intrinsic::x86_sse_comile_ss:
4965 case Intrinsic::x86_sse2_comile_sd:
4966 Opc = X86ISD::COMI;
4967 CC = ISD::SETLE;
4968 break;
4969 case Intrinsic::x86_sse_comigt_ss:
4970 case Intrinsic::x86_sse2_comigt_sd:
4971 Opc = X86ISD::COMI;
4972 CC = ISD::SETGT;
4973 break;
4974 case Intrinsic::x86_sse_comige_ss:
4975 case Intrinsic::x86_sse2_comige_sd:
4976 Opc = X86ISD::COMI;
4977 CC = ISD::SETGE;
4978 break;
4979 case Intrinsic::x86_sse_comineq_ss:
4980 case Intrinsic::x86_sse2_comineq_sd:
4981 Opc = X86ISD::COMI;
4982 CC = ISD::SETNE;
4983 break;
4984 case Intrinsic::x86_sse_ucomieq_ss:
4985 case Intrinsic::x86_sse2_ucomieq_sd:
4986 Opc = X86ISD::UCOMI;
4987 CC = ISD::SETEQ;
4988 break;
4989 case Intrinsic::x86_sse_ucomilt_ss:
4990 case Intrinsic::x86_sse2_ucomilt_sd:
4991 Opc = X86ISD::UCOMI;
4992 CC = ISD::SETLT;
4993 break;
4994 case Intrinsic::x86_sse_ucomile_ss:
4995 case Intrinsic::x86_sse2_ucomile_sd:
4996 Opc = X86ISD::UCOMI;
4997 CC = ISD::SETLE;
4998 break;
4999 case Intrinsic::x86_sse_ucomigt_ss:
5000 case Intrinsic::x86_sse2_ucomigt_sd:
5001 Opc = X86ISD::UCOMI;
5002 CC = ISD::SETGT;
5003 break;
5004 case Intrinsic::x86_sse_ucomige_ss:
5005 case Intrinsic::x86_sse2_ucomige_sd:
5006 Opc = X86ISD::UCOMI;
5007 CC = ISD::SETGE;
5008 break;
5009 case Intrinsic::x86_sse_ucomineq_ss:
5010 case Intrinsic::x86_sse2_ucomineq_sd:
5011 Opc = X86ISD::UCOMI;
5012 CC = ISD::SETNE;
5013 break;
5014 }
5015
5016 unsigned X86CC;
5017 SDOperand LHS = Op.getOperand(1);
5018 SDOperand RHS = Op.getOperand(2);
5019 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5020
Evan Cheng621216e2007-09-29 00:00:36 +00005021 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5022 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5023 DAG.getConstant(X86CC, MVT::i8), Cond);
5024 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005025 }
5026 }
5027}
5028
5029SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5030 // Depths > 0 not supported yet!
5031 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5032 return SDOperand();
5033
5034 // Just load the return address
5035 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5036 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5037}
5038
5039SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5040 // Depths > 0 not supported yet!
5041 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5042 return SDOperand();
5043
5044 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5045 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005046 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005047}
5048
5049SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5050 SelectionDAG &DAG) {
5051 // Is not yet supported on x86-64
5052 if (Subtarget->is64Bit())
5053 return SDOperand();
5054
Chris Lattner5872a362008-01-17 07:00:52 +00005055 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005056}
5057
5058SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5059{
5060 assert(!Subtarget->is64Bit() &&
5061 "Lowering of eh_return builtin is not supported yet on x86-64");
5062
5063 MachineFunction &MF = DAG.getMachineFunction();
5064 SDOperand Chain = Op.getOperand(0);
5065 SDOperand Offset = Op.getOperand(1);
5066 SDOperand Handler = Op.getOperand(2);
5067
5068 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5069 getPointerTy());
5070
5071 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005072 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005073 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5074 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5075 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005076 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005077
5078 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5079 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5080}
5081
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005082SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5083 SelectionDAG &DAG) {
5084 SDOperand Root = Op.getOperand(0);
5085 SDOperand Trmp = Op.getOperand(1); // trampoline
5086 SDOperand FPtr = Op.getOperand(2); // nested function
5087 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5088
Dan Gohman12a9c082008-02-06 22:27:42 +00005089 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005090
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005091 const X86InstrInfo *TII =
5092 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5093
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005094 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005095 SDOperand OutChains[6];
5096
5097 // Large code-model.
5098
5099 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5100 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5101
5102 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005103 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005104 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005105 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005106
5107 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5108
5109 // Load the pointer to the nested function into R11.
5110 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5111 SDOperand Addr = Trmp;
5112 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005113 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005114
5115 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005116 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005117
5118 // Load the 'nest' parameter value into R10.
5119 // R10 is specified in X86CallingConv.td
5120 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5121 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5122 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005123 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005124
5125 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005126 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005127
5128 // Jump to the nested function.
5129 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5130 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5131 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005132 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005133
5134 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5135 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5136 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005137 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005138
5139 SDOperand Ops[] =
5140 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5141 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005142 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005143 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005144 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5145 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005146 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005147
5148 switch (CC) {
5149 default:
5150 assert(0 && "Unsupported calling convention");
5151 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005152 case CallingConv::X86_StdCall: {
5153 // Pass 'nest' parameter in ECX.
5154 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005155 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005156
5157 // Check that ECX wasn't needed by an 'inreg' parameter.
5158 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005159 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005160
5161 if (Attrs && !Func->isVarArg()) {
5162 unsigned InRegCount = 0;
5163 unsigned Idx = 1;
5164
5165 for (FunctionType::param_iterator I = FTy->param_begin(),
5166 E = FTy->param_end(); I != E; ++I, ++Idx)
5167 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5168 // FIXME: should only count parameters that are lowered to integers.
5169 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5170
5171 if (InRegCount > 2) {
5172 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5173 abort();
5174 }
5175 }
5176 break;
5177 }
5178 case CallingConv::X86_FastCall:
5179 // Pass 'nest' parameter in EAX.
5180 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005181 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005182 break;
5183 }
5184
5185 SDOperand OutChains[4];
5186 SDOperand Addr, Disp;
5187
5188 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5189 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5190
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005191 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5192 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005193 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005194 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005195 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005196
5197 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005198 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005199
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005200 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005201 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5202 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005203 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005204
5205 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005206 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005207
Duncan Sands7407a9f2007-09-11 14:10:23 +00005208 SDOperand Ops[] =
5209 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5210 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005211 }
5212}
5213
Dan Gohman819574c2008-01-31 00:41:03 +00005214SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005215 /*
5216 The rounding mode is in bits 11:10 of FPSR, and has the following
5217 settings:
5218 00 Round to nearest
5219 01 Round to -inf
5220 10 Round to +inf
5221 11 Round to 0
5222
5223 FLT_ROUNDS, on the other hand, expects the following:
5224 -1 Undefined
5225 0 Round to 0
5226 1 Round to nearest
5227 2 Round to +inf
5228 3 Round to -inf
5229
5230 To perform the conversion, we do:
5231 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5232 */
5233
5234 MachineFunction &MF = DAG.getMachineFunction();
5235 const TargetMachine &TM = MF.getTarget();
5236 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5237 unsigned StackAlignment = TFI.getStackAlignment();
5238 MVT::ValueType VT = Op.getValueType();
5239
5240 // Save FP Control Word to stack slot
5241 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5242 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5243
5244 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5245 DAG.getEntryNode(), StackSlot);
5246
5247 // Load FP Control Word from stack slot
5248 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5249
5250 // Transform as necessary
5251 SDOperand CWD1 =
5252 DAG.getNode(ISD::SRL, MVT::i16,
5253 DAG.getNode(ISD::AND, MVT::i16,
5254 CWD, DAG.getConstant(0x800, MVT::i16)),
5255 DAG.getConstant(11, MVT::i8));
5256 SDOperand CWD2 =
5257 DAG.getNode(ISD::SRL, MVT::i16,
5258 DAG.getNode(ISD::AND, MVT::i16,
5259 CWD, DAG.getConstant(0x400, MVT::i16)),
5260 DAG.getConstant(9, MVT::i8));
5261
5262 SDOperand RetVal =
5263 DAG.getNode(ISD::AND, MVT::i16,
5264 DAG.getNode(ISD::ADD, MVT::i16,
5265 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5266 DAG.getConstant(1, MVT::i16)),
5267 DAG.getConstant(3, MVT::i16));
5268
5269
5270 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5271 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5272}
5273
Evan Cheng48679f42007-12-14 02:13:44 +00005274SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5275 MVT::ValueType VT = Op.getValueType();
5276 MVT::ValueType OpVT = VT;
5277 unsigned NumBits = MVT::getSizeInBits(VT);
5278
5279 Op = Op.getOperand(0);
5280 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005281 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005282 OpVT = MVT::i32;
5283 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5284 }
Evan Cheng48679f42007-12-14 02:13:44 +00005285
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005286 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5287 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5288 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5289
5290 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5291 SmallVector<SDOperand, 4> Ops;
5292 Ops.push_back(Op);
5293 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5294 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5295 Ops.push_back(Op.getValue(1));
5296 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5297
5298 // Finally xor with NumBits-1.
5299 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5300
Evan Cheng48679f42007-12-14 02:13:44 +00005301 if (VT == MVT::i8)
5302 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5303 return Op;
5304}
5305
5306SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5307 MVT::ValueType VT = Op.getValueType();
5308 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005309 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005310
5311 Op = Op.getOperand(0);
5312 if (VT == MVT::i8) {
5313 OpVT = MVT::i32;
5314 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5315 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005316
5317 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5318 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5319 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5320
5321 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5322 SmallVector<SDOperand, 4> Ops;
5323 Ops.push_back(Op);
5324 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5325 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5326 Ops.push_back(Op.getValue(1));
5327 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5328
Evan Cheng48679f42007-12-14 02:13:44 +00005329 if (VT == MVT::i8)
5330 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5331 return Op;
5332}
5333
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334/// LowerOperation - Provide custom lowering hooks for some operations.
5335///
5336SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5337 switch (Op.getOpcode()) {
5338 default: assert(0 && "Should not custom lower this!");
5339 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5340 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5341 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5342 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5343 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5344 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5345 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5346 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5347 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5348 case ISD::SHL_PARTS:
5349 case ISD::SRA_PARTS:
5350 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5351 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5352 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5353 case ISD::FABS: return LowerFABS(Op, DAG);
5354 case ISD::FNEG: return LowerFNEG(Op, DAG);
5355 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005356 case ISD::SETCC: return LowerSETCC(Op, DAG);
5357 case ISD::SELECT: return LowerSELECT(Op, DAG);
5358 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005359 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5360 case ISD::CALL: return LowerCALL(Op, DAG);
5361 case ISD::RET: return LowerRET(Op, DAG);
5362 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5363 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5364 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005365 case ISD::VASTART: return LowerVASTART(Op, DAG);
5366 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5367 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5368 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5369 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5370 case ISD::FRAME_TO_ARGS_OFFSET:
5371 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5372 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5373 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005374 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005375 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005376 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5377 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005378
5379 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5380 case ISD::READCYCLECOUNTER:
5381 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005382 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005383}
5384
5385/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5386SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5387 switch (N->getOpcode()) {
5388 default: assert(0 && "Should not custom lower this!");
5389 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5390 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5391 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005392}
5393
5394const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5395 switch (Opcode) {
5396 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005397 case X86ISD::BSF: return "X86ISD::BSF";
5398 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005399 case X86ISD::SHLD: return "X86ISD::SHLD";
5400 case X86ISD::SHRD: return "X86ISD::SHRD";
5401 case X86ISD::FAND: return "X86ISD::FAND";
5402 case X86ISD::FOR: return "X86ISD::FOR";
5403 case X86ISD::FXOR: return "X86ISD::FXOR";
5404 case X86ISD::FSRL: return "X86ISD::FSRL";
5405 case X86ISD::FILD: return "X86ISD::FILD";
5406 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5407 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5408 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5409 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5410 case X86ISD::FLD: return "X86ISD::FLD";
5411 case X86ISD::FST: return "X86ISD::FST";
5412 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng931a8f42008-01-29 19:34:22 +00005413 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005414 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5415 case X86ISD::CALL: return "X86ISD::CALL";
5416 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5417 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5418 case X86ISD::CMP: return "X86ISD::CMP";
5419 case X86ISD::COMI: return "X86ISD::COMI";
5420 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5421 case X86ISD::SETCC: return "X86ISD::SETCC";
5422 case X86ISD::CMOV: return "X86ISD::CMOV";
5423 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5424 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5425 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5426 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5428 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005429 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005430 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005431 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5432 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005433 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5434 case X86ISD::FMAX: return "X86ISD::FMAX";
5435 case X86ISD::FMIN: return "X86ISD::FMIN";
5436 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5437 case X86ISD::FRCP: return "X86ISD::FRCP";
5438 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5439 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5440 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005441 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005442 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005443 }
5444}
5445
5446// isLegalAddressingMode - Return true if the addressing mode represented
5447// by AM is legal for this target, for a load/store of the specified type.
5448bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5449 const Type *Ty) const {
5450 // X86 supports extremely general addressing modes.
5451
5452 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5453 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5454 return false;
5455
5456 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005457 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5459 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005460
5461 // X86-64 only supports addr of globals in small code model.
5462 if (Subtarget->is64Bit()) {
5463 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5464 return false;
5465 // If lower 4G is not available, then we must use rip-relative addressing.
5466 if (AM.BaseOffs || AM.Scale > 1)
5467 return false;
5468 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005469 }
5470
5471 switch (AM.Scale) {
5472 case 0:
5473 case 1:
5474 case 2:
5475 case 4:
5476 case 8:
5477 // These scales always work.
5478 break;
5479 case 3:
5480 case 5:
5481 case 9:
5482 // These scales are formed with basereg+scalereg. Only accept if there is
5483 // no basereg yet.
5484 if (AM.HasBaseReg)
5485 return false;
5486 break;
5487 default: // Other stuff never works.
5488 return false;
5489 }
5490
5491 return true;
5492}
5493
5494
Evan Cheng27a820a2007-10-26 01:56:11 +00005495bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5496 if (!Ty1->isInteger() || !Ty2->isInteger())
5497 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005498 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5499 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5500 if (NumBits1 <= NumBits2)
5501 return false;
5502 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005503}
5504
Evan Cheng9decb332007-10-29 19:58:20 +00005505bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5506 MVT::ValueType VT2) const {
5507 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5508 return false;
5509 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5510 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5511 if (NumBits1 <= NumBits2)
5512 return false;
5513 return Subtarget->is64Bit() || NumBits1 < 64;
5514}
Evan Cheng27a820a2007-10-26 01:56:11 +00005515
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005516/// isShuffleMaskLegal - Targets can use this to indicate that they only
5517/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5518/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5519/// are assumed to be legal.
5520bool
5521X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5522 // Only do shuffles on 128-bit vector types for now.
5523 if (MVT::getSizeInBits(VT) == 64) return false;
5524 return (Mask.Val->getNumOperands() <= 4 ||
5525 isIdentityMask(Mask.Val) ||
5526 isIdentityMask(Mask.Val, true) ||
5527 isSplatMask(Mask.Val) ||
5528 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5529 X86::isUNPCKLMask(Mask.Val) ||
5530 X86::isUNPCKHMask(Mask.Val) ||
5531 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5532 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5533}
5534
5535bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5536 MVT::ValueType EVT,
5537 SelectionDAG &DAG) const {
5538 unsigned NumElts = BVOps.size();
5539 // Only do shuffles on 128-bit vector types for now.
5540 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5541 if (NumElts == 2) return true;
5542 if (NumElts == 4) {
5543 return (isMOVLMask(&BVOps[0], 4) ||
5544 isCommutedMOVL(&BVOps[0], 4, true) ||
5545 isSHUFPMask(&BVOps[0], 4) ||
5546 isCommutedSHUFP(&BVOps[0], 4));
5547 }
5548 return false;
5549}
5550
5551//===----------------------------------------------------------------------===//
5552// X86 Scheduler Hooks
5553//===----------------------------------------------------------------------===//
5554
5555MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005556X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5557 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005558 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5559 switch (MI->getOpcode()) {
5560 default: assert(false && "Unexpected instr type to insert");
5561 case X86::CMOV_FR32:
5562 case X86::CMOV_FR64:
5563 case X86::CMOV_V4F32:
5564 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005565 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 // To "insert" a SELECT_CC instruction, we actually have to insert the
5567 // diamond control-flow pattern. The incoming instruction knows the
5568 // destination vreg to set, the condition code register to branch on, the
5569 // true/false values to select between, and a branch opcode to use.
5570 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5571 ilist<MachineBasicBlock>::iterator It = BB;
5572 ++It;
5573
5574 // thisMBB:
5575 // ...
5576 // TrueVal = ...
5577 // cmpTY ccX, r1, r2
5578 // bCC copy1MBB
5579 // fallthrough --> copy0MBB
5580 MachineBasicBlock *thisMBB = BB;
5581 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5582 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5583 unsigned Opc =
5584 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5585 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5586 MachineFunction *F = BB->getParent();
5587 F->getBasicBlockList().insert(It, copy0MBB);
5588 F->getBasicBlockList().insert(It, sinkMBB);
5589 // Update machine-CFG edges by first adding all successors of the current
5590 // block to the new block which will contain the Phi node for the select.
5591 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5592 e = BB->succ_end(); i != e; ++i)
5593 sinkMBB->addSuccessor(*i);
5594 // Next, remove all successors of the current block, and add the true
5595 // and fallthrough blocks as its successors.
5596 while(!BB->succ_empty())
5597 BB->removeSuccessor(BB->succ_begin());
5598 BB->addSuccessor(copy0MBB);
5599 BB->addSuccessor(sinkMBB);
5600
5601 // copy0MBB:
5602 // %FalseValue = ...
5603 // # fallthrough to sinkMBB
5604 BB = copy0MBB;
5605
5606 // Update machine-CFG edges
5607 BB->addSuccessor(sinkMBB);
5608
5609 // sinkMBB:
5610 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5611 // ...
5612 BB = sinkMBB;
5613 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5614 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5615 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5616
5617 delete MI; // The pseudo instruction is gone now.
5618 return BB;
5619 }
5620
5621 case X86::FP32_TO_INT16_IN_MEM:
5622 case X86::FP32_TO_INT32_IN_MEM:
5623 case X86::FP32_TO_INT64_IN_MEM:
5624 case X86::FP64_TO_INT16_IN_MEM:
5625 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005626 case X86::FP64_TO_INT64_IN_MEM:
5627 case X86::FP80_TO_INT16_IN_MEM:
5628 case X86::FP80_TO_INT32_IN_MEM:
5629 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005630 // Change the floating point control register to use "round towards zero"
5631 // mode when truncating to an integer value.
5632 MachineFunction *F = BB->getParent();
5633 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5634 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5635
5636 // Load the old value of the high byte of the control word...
5637 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005638 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005639 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5640
5641 // Set the high part to be round to zero...
5642 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5643 .addImm(0xC7F);
5644
5645 // Reload the modified control word now...
5646 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5647
5648 // Restore the memory image of control word to original value
5649 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5650 .addReg(OldCW);
5651
5652 // Get the X86 opcode to use.
5653 unsigned Opc;
5654 switch (MI->getOpcode()) {
5655 default: assert(0 && "illegal opcode!");
5656 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5657 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5658 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5659 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5660 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5661 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005662 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5663 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5664 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005665 }
5666
5667 X86AddressMode AM;
5668 MachineOperand &Op = MI->getOperand(0);
5669 if (Op.isRegister()) {
5670 AM.BaseType = X86AddressMode::RegBase;
5671 AM.Base.Reg = Op.getReg();
5672 } else {
5673 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005674 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005675 }
5676 Op = MI->getOperand(1);
5677 if (Op.isImmediate())
5678 AM.Scale = Op.getImm();
5679 Op = MI->getOperand(2);
5680 if (Op.isImmediate())
5681 AM.IndexReg = Op.getImm();
5682 Op = MI->getOperand(3);
5683 if (Op.isGlobalAddress()) {
5684 AM.GV = Op.getGlobal();
5685 } else {
5686 AM.Disp = Op.getImm();
5687 }
5688 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5689 .addReg(MI->getOperand(4).getReg());
5690
5691 // Reload the original control word now.
5692 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5693
5694 delete MI; // The pseudo instruction is gone now.
5695 return BB;
5696 }
5697 }
5698}
5699
5700//===----------------------------------------------------------------------===//
5701// X86 Optimization Hooks
5702//===----------------------------------------------------------------------===//
5703
5704void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005705 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005706 APInt &KnownZero,
5707 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005708 const SelectionDAG &DAG,
5709 unsigned Depth) const {
5710 unsigned Opc = Op.getOpcode();
5711 assert((Opc >= ISD::BUILTIN_OP_END ||
5712 Opc == ISD::INTRINSIC_WO_CHAIN ||
5713 Opc == ISD::INTRINSIC_W_CHAIN ||
5714 Opc == ISD::INTRINSIC_VOID) &&
5715 "Should use MaskedValueIsZero if you don't know whether Op"
5716 " is a target node!");
5717
Dan Gohman1d79e432008-02-13 23:07:24 +00005718 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005719 switch (Opc) {
5720 default: break;
5721 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005722 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5723 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005724 break;
5725 }
5726}
5727
5728/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5729/// element of the result of the vector shuffle.
5730static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5731 MVT::ValueType VT = N->getValueType(0);
5732 SDOperand PermMask = N->getOperand(2);
5733 unsigned NumElems = PermMask.getNumOperands();
5734 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5735 i %= NumElems;
5736 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5737 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005738 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005739 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5740 SDOperand Idx = PermMask.getOperand(i);
5741 if (Idx.getOpcode() == ISD::UNDEF)
5742 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5743 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5744 }
5745 return SDOperand();
5746}
5747
5748/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5749/// node is a GlobalAddress + an offset.
5750static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5751 unsigned Opc = N->getOpcode();
5752 if (Opc == X86ISD::Wrapper) {
5753 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5754 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5755 return true;
5756 }
5757 } else if (Opc == ISD::ADD) {
5758 SDOperand N1 = N->getOperand(0);
5759 SDOperand N2 = N->getOperand(1);
5760 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5761 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5762 if (V) {
5763 Offset += V->getSignExtended();
5764 return true;
5765 }
5766 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5767 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5768 if (V) {
5769 Offset += V->getSignExtended();
5770 return true;
5771 }
5772 }
5773 }
5774 return false;
5775}
5776
5777/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5778/// + Dist * Size.
5779static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5780 MachineFrameInfo *MFI) {
5781 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5782 return false;
5783
5784 SDOperand Loc = N->getOperand(1);
5785 SDOperand BaseLoc = Base->getOperand(1);
5786 if (Loc.getOpcode() == ISD::FrameIndex) {
5787 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5788 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005789 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5790 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005791 int FS = MFI->getObjectSize(FI);
5792 int BFS = MFI->getObjectSize(BFI);
5793 if (FS != BFS || FS != Size) return false;
5794 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5795 } else {
5796 GlobalValue *GV1 = NULL;
5797 GlobalValue *GV2 = NULL;
5798 int64_t Offset1 = 0;
5799 int64_t Offset2 = 0;
5800 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5801 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5802 if (isGA1 && isGA2 && GV1 == GV2)
5803 return Offset1 == (Offset2 + Dist*Size);
5804 }
5805
5806 return false;
5807}
5808
5809static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5810 const X86Subtarget *Subtarget) {
5811 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005812 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005813 if (isGAPlusOffset(Base, GV, Offset))
5814 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005815 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005816 return false;
5817}
5818
5819
5820/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5821/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5822/// if the load addresses are consecutive, non-overlapping, and in the right
5823/// order.
5824static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5825 const X86Subtarget *Subtarget) {
5826 MachineFunction &MF = DAG.getMachineFunction();
5827 MachineFrameInfo *MFI = MF.getFrameInfo();
5828 MVT::ValueType VT = N->getValueType(0);
5829 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5830 SDOperand PermMask = N->getOperand(2);
5831 int NumElems = (int)PermMask.getNumOperands();
5832 SDNode *Base = NULL;
5833 for (int i = 0; i < NumElems; ++i) {
5834 SDOperand Idx = PermMask.getOperand(i);
5835 if (Idx.getOpcode() == ISD::UNDEF) {
5836 if (!Base) return SDOperand();
5837 } else {
5838 SDOperand Arg =
5839 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5840 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5841 return SDOperand();
5842 if (!Base)
5843 Base = Arg.Val;
5844 else if (!isConsecutiveLoad(Arg.Val, Base,
5845 i, MVT::getSizeInBits(EVT)/8,MFI))
5846 return SDOperand();
5847 }
5848 }
5849
5850 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005851 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005852 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005853 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005854 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005855 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005856 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5857 LD->getSrcValueOffset(), LD->isVolatile(),
5858 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859 }
5860}
5861
5862/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5863static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5864 const X86Subtarget *Subtarget) {
5865 SDOperand Cond = N->getOperand(0);
5866
5867 // If we have SSE[12] support, try to form min/max nodes.
5868 if (Subtarget->hasSSE2() &&
5869 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5870 if (Cond.getOpcode() == ISD::SETCC) {
5871 // Get the LHS/RHS of the select.
5872 SDOperand LHS = N->getOperand(1);
5873 SDOperand RHS = N->getOperand(2);
5874 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5875
5876 unsigned Opcode = 0;
5877 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5878 switch (CC) {
5879 default: break;
5880 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5881 case ISD::SETULE:
5882 case ISD::SETLE:
5883 if (!UnsafeFPMath) break;
5884 // FALL THROUGH.
5885 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5886 case ISD::SETLT:
5887 Opcode = X86ISD::FMIN;
5888 break;
5889
5890 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5891 case ISD::SETUGT:
5892 case ISD::SETGT:
5893 if (!UnsafeFPMath) break;
5894 // FALL THROUGH.
5895 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5896 case ISD::SETGE:
5897 Opcode = X86ISD::FMAX;
5898 break;
5899 }
5900 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5901 switch (CC) {
5902 default: break;
5903 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5904 case ISD::SETUGT:
5905 case ISD::SETGT:
5906 if (!UnsafeFPMath) break;
5907 // FALL THROUGH.
5908 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5909 case ISD::SETGE:
5910 Opcode = X86ISD::FMIN;
5911 break;
5912
5913 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5914 case ISD::SETULE:
5915 case ISD::SETLE:
5916 if (!UnsafeFPMath) break;
5917 // FALL THROUGH.
5918 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5919 case ISD::SETLT:
5920 Opcode = X86ISD::FMAX;
5921 break;
5922 }
5923 }
5924
5925 if (Opcode)
5926 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5927 }
5928
5929 }
5930
5931 return SDOperand();
5932}
5933
Chris Lattnerce84ae42008-02-22 02:09:43 +00005934/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
5935static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
5936 const X86Subtarget *Subtarget) {
5937 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
5938 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00005939 // A preferable solution to the general problem is to figure out the right
5940 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00005941 if (MVT::isVector(St->getValue().getValueType()) &&
5942 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00005943 isa<LoadSDNode>(St->getValue()) &&
5944 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
5945 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005946 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00005947 LoadSDNode *Ld = 0;
5948 int TokenFactorIndex = -1;
5949 SmallVector<SDOperand, 8> Ops;
5950 SDNode* ChainVal = St->getChain().Val;
5951 // Must be a store of a load. We currently handle two cases: the load
5952 // is a direct child, and it's under an intervening TokenFactor. It is
5953 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00005954 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00005955 Ld = cast<LoadSDNode>(St->getChain());
5956 else if (St->getValue().hasOneUse() &&
5957 ChainVal->getOpcode() == ISD::TokenFactor) {
5958 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005959 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00005960 TokenFactorIndex = i;
5961 Ld = cast<LoadSDNode>(St->getValue());
5962 } else
5963 Ops.push_back(ChainVal->getOperand(i));
5964 }
5965 }
5966 if (Ld) {
5967 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
5968 if (Subtarget->is64Bit()) {
5969 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
5970 Ld->getBasePtr(), Ld->getSrcValue(),
5971 Ld->getSrcValueOffset(), Ld->isVolatile(),
5972 Ld->getAlignment());
5973 SDOperand NewChain = NewLd.getValue(1);
5974 if (TokenFactorIndex != -1) {
5975 Ops.push_back(NewLd);
5976 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
5977 Ops.size());
5978 }
5979 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
5980 St->getSrcValue(), St->getSrcValueOffset(),
5981 St->isVolatile(), St->getAlignment());
5982 }
5983
5984 // Otherwise, lower to two 32-bit copies.
5985 SDOperand LoAddr = Ld->getBasePtr();
5986 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
5987 DAG.getConstant(MVT::i32, 4));
5988
5989 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
5990 Ld->getSrcValue(), Ld->getSrcValueOffset(),
5991 Ld->isVolatile(), Ld->getAlignment());
5992 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
5993 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
5994 Ld->isVolatile(),
5995 MinAlign(Ld->getAlignment(), 4));
5996
5997 SDOperand NewChain = LoLd.getValue(1);
5998 if (TokenFactorIndex != -1) {
5999 Ops.push_back(LoLd);
6000 Ops.push_back(HiLd);
6001 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6002 Ops.size());
6003 }
6004
6005 LoAddr = St->getBasePtr();
6006 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6007 DAG.getConstant(MVT::i32, 4));
6008
6009 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006010 St->getSrcValue(), St->getSrcValueOffset(),
6011 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006012 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6013 St->getSrcValue(), St->getSrcValueOffset()+4,
6014 St->isVolatile(),
6015 MinAlign(St->getAlignment(), 4));
6016 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006017 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006018 }
6019 return SDOperand();
6020}
6021
Chris Lattner470d5dc2008-01-25 06:14:17 +00006022/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6023/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006024static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006025 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6026 // F[X]OR(0.0, x) -> x
6027 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006028 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6029 if (C->getValueAPF().isPosZero())
6030 return N->getOperand(1);
6031 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6032 if (C->getValueAPF().isPosZero())
6033 return N->getOperand(0);
6034 return SDOperand();
6035}
6036
6037/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6038static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6039 // FAND(0.0, x) -> 0.0
6040 // FAND(x, 0.0) -> 0.0
6041 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6042 if (C->getValueAPF().isPosZero())
6043 return N->getOperand(0);
6044 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6045 if (C->getValueAPF().isPosZero())
6046 return N->getOperand(1);
6047 return SDOperand();
6048}
6049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006050
6051SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6052 DAGCombinerInfo &DCI) const {
6053 SelectionDAG &DAG = DCI.DAG;
6054 switch (N->getOpcode()) {
6055 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006056 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6057 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006058 case ISD::STORE:
6059 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006060 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006061 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6062 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006063 }
6064
6065 return SDOperand();
6066}
6067
6068//===----------------------------------------------------------------------===//
6069// X86 Inline Assembly Support
6070//===----------------------------------------------------------------------===//
6071
6072/// getConstraintType - Given a constraint letter, return the type of
6073/// constraint it is for this target.
6074X86TargetLowering::ConstraintType
6075X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6076 if (Constraint.size() == 1) {
6077 switch (Constraint[0]) {
6078 case 'A':
6079 case 'r':
6080 case 'R':
6081 case 'l':
6082 case 'q':
6083 case 'Q':
6084 case 'x':
6085 case 'Y':
6086 return C_RegisterClass;
6087 default:
6088 break;
6089 }
6090 }
6091 return TargetLowering::getConstraintType(Constraint);
6092}
6093
Dale Johannesene99fc902008-01-29 02:21:21 +00006094/// LowerXConstraint - try to replace an X constraint, which matches anything,
6095/// with another that has more specific requirements based on the type of the
6096/// corresponding operand.
6097void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6098 std::string& s) const {
6099 if (MVT::isFloatingPoint(ConstraintVT)) {
6100 if (Subtarget->hasSSE2())
6101 s = "Y";
6102 else if (Subtarget->hasSSE1())
6103 s = "x";
6104 else
6105 s = "f";
6106 } else
6107 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6108}
6109
Chris Lattnera531abc2007-08-25 00:47:38 +00006110/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6111/// vector. If it is invalid, don't add anything to Ops.
6112void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6113 char Constraint,
6114 std::vector<SDOperand>&Ops,
6115 SelectionDAG &DAG) {
6116 SDOperand Result(0, 0);
6117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006118 switch (Constraint) {
6119 default: break;
6120 case 'I':
6121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006122 if (C->getValue() <= 31) {
6123 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6124 break;
6125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006127 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006128 case 'N':
6129 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006130 if (C->getValue() <= 255) {
6131 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6132 break;
6133 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006134 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006135 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006136 case 'i': {
6137 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006138 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6139 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6140 break;
6141 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006142
6143 // If we are in non-pic codegen mode, we allow the address of a global (with
6144 // an optional displacement) to be used with 'i'.
6145 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6146 int64_t Offset = 0;
6147
6148 // Match either (GA) or (GA+C)
6149 if (GA) {
6150 Offset = GA->getOffset();
6151 } else if (Op.getOpcode() == ISD::ADD) {
6152 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6153 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6154 if (C && GA) {
6155 Offset = GA->getOffset()+C->getValue();
6156 } else {
6157 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6158 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6159 if (C && GA)
6160 Offset = GA->getOffset()+C->getValue();
6161 else
6162 C = 0, GA = 0;
6163 }
6164 }
6165
6166 if (GA) {
6167 // If addressing this global requires a load (e.g. in PIC mode), we can't
6168 // match.
6169 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6170 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006171 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006172
6173 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6174 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006175 Result = Op;
6176 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006177 }
6178
6179 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006180 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006181 }
6182 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006183
6184 if (Result.Val) {
6185 Ops.push_back(Result);
6186 return;
6187 }
6188 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006189}
6190
6191std::vector<unsigned> X86TargetLowering::
6192getRegClassForInlineAsmConstraint(const std::string &Constraint,
6193 MVT::ValueType VT) const {
6194 if (Constraint.size() == 1) {
6195 // FIXME: not handling fp-stack yet!
6196 switch (Constraint[0]) { // GCC X86 Constraint Letters
6197 default: break; // Unknown constraint letter
6198 case 'A': // EAX/EDX
6199 if (VT == MVT::i32 || VT == MVT::i64)
6200 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6201 break;
6202 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6203 case 'Q': // Q_REGS
6204 if (VT == MVT::i32)
6205 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6206 else if (VT == MVT::i16)
6207 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6208 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006209 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006210 else if (VT == MVT::i64)
6211 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6212 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006213 }
6214 }
6215
6216 return std::vector<unsigned>();
6217}
6218
6219std::pair<unsigned, const TargetRegisterClass*>
6220X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6221 MVT::ValueType VT) const {
6222 // First, see if this is a constraint that directly corresponds to an LLVM
6223 // register class.
6224 if (Constraint.size() == 1) {
6225 // GCC Constraint Letters
6226 switch (Constraint[0]) {
6227 default: break;
6228 case 'r': // GENERAL_REGS
6229 case 'R': // LEGACY_REGS
6230 case 'l': // INDEX_REGS
6231 if (VT == MVT::i64 && Subtarget->is64Bit())
6232 return std::make_pair(0U, X86::GR64RegisterClass);
6233 if (VT == MVT::i32)
6234 return std::make_pair(0U, X86::GR32RegisterClass);
6235 else if (VT == MVT::i16)
6236 return std::make_pair(0U, X86::GR16RegisterClass);
6237 else if (VT == MVT::i8)
6238 return std::make_pair(0U, X86::GR8RegisterClass);
6239 break;
6240 case 'y': // MMX_REGS if MMX allowed.
6241 if (!Subtarget->hasMMX()) break;
6242 return std::make_pair(0U, X86::VR64RegisterClass);
6243 break;
6244 case 'Y': // SSE_REGS if SSE2 allowed
6245 if (!Subtarget->hasSSE2()) break;
6246 // FALL THROUGH.
6247 case 'x': // SSE_REGS if SSE1 allowed
6248 if (!Subtarget->hasSSE1()) break;
6249
6250 switch (VT) {
6251 default: break;
6252 // Scalar SSE types.
6253 case MVT::f32:
6254 case MVT::i32:
6255 return std::make_pair(0U, X86::FR32RegisterClass);
6256 case MVT::f64:
6257 case MVT::i64:
6258 return std::make_pair(0U, X86::FR64RegisterClass);
6259 // Vector types.
6260 case MVT::v16i8:
6261 case MVT::v8i16:
6262 case MVT::v4i32:
6263 case MVT::v2i64:
6264 case MVT::v4f32:
6265 case MVT::v2f64:
6266 return std::make_pair(0U, X86::VR128RegisterClass);
6267 }
6268 break;
6269 }
6270 }
6271
6272 // Use the default implementation in TargetLowering to convert the register
6273 // constraint into a member of a register class.
6274 std::pair<unsigned, const TargetRegisterClass*> Res;
6275 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6276
6277 // Not found as a standard register?
6278 if (Res.second == 0) {
6279 // GCC calls "st(0)" just plain "st".
6280 if (StringsEqualNoCase("{st}", Constraint)) {
6281 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006282 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006283 }
6284
6285 return Res;
6286 }
6287
6288 // Otherwise, check to see if this is a register class of the wrong value
6289 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6290 // turn into {ax},{dx}.
6291 if (Res.second->hasType(VT))
6292 return Res; // Correct type already, nothing to do.
6293
6294 // All of the single-register GCC register classes map their values onto
6295 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6296 // really want an 8-bit or 32-bit register, map to the appropriate register
6297 // class and return the appropriate register.
6298 if (Res.second != X86::GR16RegisterClass)
6299 return Res;
6300
6301 if (VT == MVT::i8) {
6302 unsigned DestReg = 0;
6303 switch (Res.first) {
6304 default: break;
6305 case X86::AX: DestReg = X86::AL; break;
6306 case X86::DX: DestReg = X86::DL; break;
6307 case X86::CX: DestReg = X86::CL; break;
6308 case X86::BX: DestReg = X86::BL; break;
6309 }
6310 if (DestReg) {
6311 Res.first = DestReg;
6312 Res.second = Res.second = X86::GR8RegisterClass;
6313 }
6314 } else if (VT == MVT::i32) {
6315 unsigned DestReg = 0;
6316 switch (Res.first) {
6317 default: break;
6318 case X86::AX: DestReg = X86::EAX; break;
6319 case X86::DX: DestReg = X86::EDX; break;
6320 case X86::CX: DestReg = X86::ECX; break;
6321 case X86::BX: DestReg = X86::EBX; break;
6322 case X86::SI: DestReg = X86::ESI; break;
6323 case X86::DI: DestReg = X86::EDI; break;
6324 case X86::BP: DestReg = X86::EBP; break;
6325 case X86::SP: DestReg = X86::ESP; break;
6326 }
6327 if (DestReg) {
6328 Res.first = DestReg;
6329 Res.second = Res.second = X86::GR32RegisterClass;
6330 }
6331 } else if (VT == MVT::i64) {
6332 unsigned DestReg = 0;
6333 switch (Res.first) {
6334 default: break;
6335 case X86::AX: DestReg = X86::RAX; break;
6336 case X86::DX: DestReg = X86::RDX; break;
6337 case X86::CX: DestReg = X86::RCX; break;
6338 case X86::BX: DestReg = X86::RBX; break;
6339 case X86::SI: DestReg = X86::RSI; break;
6340 case X86::DI: DestReg = X86::RDI; break;
6341 case X86::BP: DestReg = X86::RBP; break;
6342 case X86::SP: DestReg = X86::RSP; break;
6343 }
6344 if (DestReg) {
6345 Res.first = DestReg;
6346 Res.second = Res.second = X86::GR64RegisterClass;
6347 }
6348 }
6349
6350 return Res;
6351}