blob: 8acf779676edd02a495c83cffa59317cbe781f77 [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!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000717 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718}
719
Evan Cheng5a67b812008-01-23 23:17:41 +0000720/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
721/// the desired ByVal argument alignment.
722static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
723 if (MaxAlign == 16)
724 return;
725 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
726 if (VTy->getBitWidth() == 128)
727 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000728 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
729 unsigned EltAlign = 0;
730 getMaxByValAlign(ATy->getElementType(), EltAlign);
731 if (EltAlign > MaxAlign)
732 MaxAlign = EltAlign;
733 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
734 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
735 unsigned EltAlign = 0;
736 getMaxByValAlign(STy->getElementType(i), EltAlign);
737 if (EltAlign > MaxAlign)
738 MaxAlign = EltAlign;
739 if (MaxAlign == 16)
740 break;
741 }
742 }
743 return;
744}
745
746/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
747/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000748/// that contain SSE vectors are placed at 16-byte boundaries while the rest
749/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000750unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
751 if (Subtarget->is64Bit())
752 return getTargetData()->getABITypeAlignment(Ty);
753 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000754 if (Subtarget->hasSSE1())
755 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000756 return Align;
757}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758
Evan Cheng6fb06762007-11-09 01:32:10 +0000759/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
760/// jumptable.
761SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
762 SelectionDAG &DAG) const {
763 if (usesGlobalOffsetTable())
764 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
765 if (!Subtarget->isPICStyleRIPRel())
766 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
767 return Table;
768}
769
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770//===----------------------------------------------------------------------===//
771// Return Value Calling Convention Implementation
772//===----------------------------------------------------------------------===//
773
774#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000775
776/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
777/// exists skip possible ISD:TokenFactor.
778static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000779 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000780 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000781 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000782 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000783 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000784 return Chain.getOperand(0);
785 }
786 return Chain;
787}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000788
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789/// LowerRET - Lower an ISD::RET node.
790SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
791 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
792
793 SmallVector<CCValAssign, 16> RVLocs;
794 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
795 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
796 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
797 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000798
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 // If this is the first return lowered for this function, add the regs to the
800 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000801 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 for (unsigned i = 0; i != RVLocs.size(); ++i)
803 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000804 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000808 // Handle tail call return.
809 Chain = GetPossiblePreceedingTailCall(Chain);
810 if (Chain.getOpcode() == X86ISD::TAILCALL) {
811 SDOperand TailCall = Chain;
812 SDOperand TargetAddress = TailCall.getOperand(1);
813 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000814 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000815 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
816 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
817 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
818 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
819 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000820 assert(StackAdjustment.getOpcode() == ISD::Constant &&
821 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000822
823 SmallVector<SDOperand,8> Operands;
824 Operands.push_back(Chain.getOperand(0));
825 Operands.push_back(TargetAddress);
826 Operands.push_back(StackAdjustment);
827 // Copy registers used by the call. Last operand is a flag so it is not
828 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000829 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000830 Operands.push_back(Chain.getOperand(i));
831 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000832 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
833 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000834 }
835
836 // Regular return.
837 SDOperand Flag;
838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 // Copy the result values into the output registers.
840 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
841 RVLocs[0].getLocReg() != X86::ST0) {
842 for (unsigned i = 0; i != RVLocs.size(); ++i) {
843 CCValAssign &VA = RVLocs[i];
844 assert(VA.isRegLoc() && "Can only return in registers!");
845 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
846 Flag);
847 Flag = Chain.getValue(1);
848 }
849 } else {
850 // We need to handle a destination of ST0 specially, because it isn't really
851 // a register.
852 SDOperand Value = Op.getOperand(1);
853
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000854 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
855 // This will get legalized into a load/store if it can't get optimized away.
856 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
857 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858
859 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
860 SDOperand Ops[] = { Chain, Value };
861 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
862 Flag = Chain.getValue(1);
863 }
864
865 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
866 if (Flag.Val)
867 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
868 else
869 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
870}
871
872
873/// LowerCallResult - Lower the result values of an ISD::CALL into the
874/// appropriate copies out of appropriate physical registers. This assumes that
875/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
876/// being lowered. The returns a SDNode with the same number of values as the
877/// ISD::CALL.
878SDNode *X86TargetLowering::
879LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
880 unsigned CallingConv, SelectionDAG &DAG) {
881
882 // Assign locations to each value returned by this call.
883 SmallVector<CCValAssign, 16> RVLocs;
884 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
885 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
886 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
887
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 SmallVector<SDOperand, 8> ResultVals;
889
890 // Copy all of the result registers out of their specified physreg.
891 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
892 for (unsigned i = 0; i != RVLocs.size(); ++i) {
893 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
894 RVLocs[i].getValVT(), InFlag).getValue(1);
895 InFlag = Chain.getValue(2);
896 ResultVals.push_back(Chain.getValue(0));
897 }
898 } else {
899 // Copies from the FP stack are special, as ST0 isn't a valid register
900 // before the fp stackifier runs.
901
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000902 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
903 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
904 // the specified value type.
905 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
906 if (isScalarFPTypeInSSEReg(GetResultTy))
907 GetResultTy = MVT::f80;
908 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
909
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 SDOperand GROps[] = { Chain, InFlag };
911 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
912 Chain = RetVal.getValue(1);
913 InFlag = RetVal.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000914
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000915 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
916 // there.
917 if (GetResultTy != RVLocs[0].getValVT())
918 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
919 // This truncation won't change the value.
920 DAG.getIntPtrConstant(1));
921
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 ResultVals.push_back(RetVal);
923 }
924
925 // Merge everything together with a MERGE_VALUES node.
926 ResultVals.push_back(Chain);
927 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
928 &ResultVals[0], ResultVals.size()).Val;
929}
930
Evan Cheng931a8f42008-01-29 19:34:22 +0000931/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
932/// ISD::CALL where the results are known to be in two 64-bit registers,
933/// e.g. XMM0 and XMM1. This simplify store the two values back to the
934/// fixed stack slot allocated for StructRet.
935SDNode *X86TargetLowering::
936LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
937 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
938 MVT::ValueType VT, SelectionDAG &DAG) {
939 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
940 Chain = RetVal1.getValue(1);
941 InFlag = RetVal1.getValue(2);
942 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
943 Chain = RetVal2.getValue(1);
944 InFlag = RetVal2.getValue(2);
945 SDOperand FIN = TheCall->getOperand(5);
946 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
947 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
948 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
949 return Chain.Val;
950}
951
952/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
953/// where the results are known to be in ST0 and ST1.
954SDNode *X86TargetLowering::
955LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
956 SDNode *TheCall, SelectionDAG &DAG) {
957 SmallVector<SDOperand, 8> ResultVals;
958 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
959 SDVTList Tys = DAG.getVTList(VTs, 4);
960 SDOperand Ops[] = { Chain, InFlag };
961 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
962 Chain = RetVal.getValue(2);
963 SDOperand FIN = TheCall->getOperand(5);
964 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
965 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
966 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
967 return Chain.Val;
968}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969
970//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000971// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972//===----------------------------------------------------------------------===//
973// StdCall calling convention seems to be standard for many Windows' API
974// routines and around. It differs from C calling convention just a little:
975// callee should clean up the stack, not caller. Symbols should be also
976// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000977// For info on fast calling convention see Fast Calling Convention (tail call)
978// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979
980/// AddLiveIn - This helper function adds the specified physical register to the
981/// MachineFunction as a live in value. It also creates a corresponding virtual
982/// register for it.
983static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
984 const TargetRegisterClass *RC) {
985 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000986 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
987 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 return VReg;
989}
990
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000991/// CallIsStructReturn - Determines whether a CALL node uses struct return
992/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000993static bool CallIsStructReturn(SDOperand Op) {
994 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
995 if (!NumOps)
996 return false;
997
998 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
999 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1000}
1001
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001002/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1003/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001004static bool ArgsAreStructReturn(SDOperand Op) {
1005 unsigned NumArgs = Op.Val->getNumValues() - 1;
1006 if (!NumArgs)
1007 return false;
1008
1009 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1010 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1011}
1012
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001013/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1014/// callee to pop its own arguments. Callee pop is necessary to support tail
1015/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001016bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1017 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1018 if (IsVarArg)
1019 return false;
1020
1021 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1022 default:
1023 return false;
1024 case CallingConv::X86_StdCall:
1025 return !Subtarget->is64Bit();
1026 case CallingConv::X86_FastCall:
1027 return !Subtarget->is64Bit();
1028 case CallingConv::Fast:
1029 return PerformTailCallOpt;
1030 }
1031}
1032
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001033/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1034/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001035CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1036 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1037
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001038 if (Subtarget->is64Bit()) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001039 if (CC == CallingConv::Fast && PerformTailCallOpt)
1040 return CC_X86_64_TailCall;
1041 else
1042 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001043 }
1044
Gordon Henriksen18ace102008-01-05 16:56:59 +00001045 if (CC == CallingConv::X86_FastCall)
1046 return CC_X86_32_FastCall;
1047 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1048 return CC_X86_32_TailCall;
1049 else
1050 return CC_X86_32_C;
1051}
1052
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001053/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1054/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001055NameDecorationStyle
1056X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1057 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1058 if (CC == CallingConv::X86_FastCall)
1059 return FastCall;
1060 else if (CC == CallingConv::X86_StdCall)
1061 return StdCall;
1062 return None;
1063}
1064
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001065/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1066/// possibly be overwritten when lowering the outgoing arguments in a tail
1067/// call. Currently the implementation of this call is very conservative and
1068/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1069/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001070static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1071 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001072 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001073 FrameIndexSDNode * FrameIdxNode = NULL;
1074 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001075 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1076 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001077 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1078 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1079 (Op.getOpcode() == ISD::LOAD &&
1080 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1081 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1082 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001083 return true;
1084 return false;
1085}
1086
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001087/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1088/// in a register before calling.
1089bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1090 return !IsTailCall && !Is64Bit &&
1091 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1092 Subtarget->isPICStyleGOT();
1093}
1094
1095
1096/// CallRequiresFnAddressInReg - Check whether the call requires the function
1097/// address to be loaded in a register.
1098bool
1099X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1100 return !Is64Bit && IsTailCall &&
1101 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1102 Subtarget->isPICStyleGOT();
1103}
1104
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001105/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1106/// arguments to force loading and guarantee that arguments sourcing from
1107/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001108static SDOperand
1109CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1110 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1111 SelectionDAG &DAG,
1112 MachineFunction &MF,
1113 const TargetLowering * TL) {
1114
1115 SDOperand InFlag;
1116 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1117 SDOperand Arg = TailCallClobberedVRegs[i].second;
1118 unsigned Idx = TailCallClobberedVRegs[i].first;
1119 unsigned VReg =
1120 MF.getRegInfo().
1121 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1122 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1123 InFlag = Chain.getValue(1);
1124 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1125 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1126 Chain = Arg.getValue(1);
1127 InFlag = Arg.getValue(2);
1128 }
1129 return Chain;
1130}
1131
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001132/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1133/// by "Src" to address "Dst" with size and alignment information specified by
1134/// the specific parameter attribute. The copy will be passed as a byval function
1135/// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001136static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001137CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1138 unsigned Flags, SelectionDAG &DAG) {
1139 unsigned Align = 1 <<
1140 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1141 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001142 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001143 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1144 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001145 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001146 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001147}
1148
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001149SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1150 const CCValAssign &VA,
1151 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001152 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001153 SDOperand Root, unsigned i) {
1154 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001155 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001156 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Evan Cheng3e42a522008-01-10 02:24:25 +00001157 bool isByVal = Flags & ISD::ParamFlags::ByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001158 bool isImmutable = !AlwaysUseMutable && !isByVal;
Evan Cheng3e42a522008-01-10 02:24:25 +00001159
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001160 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1161 // changed with more analysis.
1162 // In case of tail call optimization mark all arguments mutable. Since they
1163 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001164 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001165 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001166 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001167 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001168 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001169 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001170 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001171}
1172
Gordon Henriksen18ace102008-01-05 16:56:59 +00001173SDOperand
1174X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001176 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1177
1178 const Function* Fn = MF.getFunction();
1179 if (Fn->hasExternalLinkage() &&
1180 Subtarget->isTargetCygMing() &&
1181 Fn->getName() == "main")
1182 FuncInfo->setForceFramePointer(true);
1183
1184 // Decorate the function name.
1185 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 MachineFrameInfo *MFI = MF.getFrameInfo();
1188 SDOperand Root = Op.getOperand(0);
1189 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001190 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001191 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001192
1193 assert(!(isVarArg && CC == CallingConv::Fast) &&
1194 "Var args not supported with calling convention fastcc");
1195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 // Assign locations to all of the incoming arguments.
1197 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001198 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001199 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001200
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001201 SmallVector<SDOperand, 8> ArgValues;
1202 unsigned LastVal = ~0U;
1203 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1204 CCValAssign &VA = ArgLocs[i];
1205 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1206 // places.
1207 assert(VA.getValNo() != LastVal &&
1208 "Don't support value assigned to multiple locs yet");
1209 LastVal = VA.getValNo();
1210
1211 if (VA.isRegLoc()) {
1212 MVT::ValueType RegVT = VA.getLocVT();
1213 TargetRegisterClass *RC;
1214 if (RegVT == MVT::i32)
1215 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001216 else if (Is64Bit && RegVT == MVT::i64)
1217 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001218 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001219 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001220 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001221 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001222 else {
1223 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001224 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1225 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1226 RegVT = MVT::i64;
1227 } else
1228 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001230
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1232 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1233
1234 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1235 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1236 // right size.
1237 if (VA.getLocInfo() == CCValAssign::SExt)
1238 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1239 DAG.getValueType(VA.getValVT()));
1240 else if (VA.getLocInfo() == CCValAssign::ZExt)
1241 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1242 DAG.getValueType(VA.getValVT()));
1243
1244 if (VA.getLocInfo() != CCValAssign::Full)
1245 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1246
Gordon Henriksen18ace102008-01-05 16:56:59 +00001247 // Handle MMX values passed in GPRs.
1248 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1249 MVT::getSizeInBits(RegVT) == 64)
1250 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252 ArgValues.push_back(ArgValue);
1253 } else {
1254 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001255 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001256 }
1257 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001260 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001261 if (CC == CallingConv::Fast)
1262 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001263
1264 // If the function takes variable number of arguments, make a frame index for
1265 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001266 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001267 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1268 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1269 }
1270 if (Is64Bit) {
1271 static const unsigned GPR64ArgRegs[] = {
1272 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1273 };
1274 static const unsigned XMMArgRegs[] = {
1275 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1276 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1277 };
1278
1279 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1280 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1281
1282 // For X86-64, if there are vararg parameters that are passed via
1283 // registers, then we must store them to their spots on the stack so they
1284 // may be loaded by deferencing the result of va_next.
1285 VarArgsGPOffset = NumIntRegs * 8;
1286 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1287 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1288
1289 // Store the integer parameter registers.
1290 SmallVector<SDOperand, 8> MemOps;
1291 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1292 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001293 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001294 for (; NumIntRegs != 6; ++NumIntRegs) {
1295 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1296 X86::GR64RegisterClass);
1297 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001298 SDOperand Store =
1299 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001300 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001301 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001302 MemOps.push_back(Store);
1303 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001304 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001305 }
1306
1307 // Now store the XMM (fp + vector) parameter registers.
1308 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001309 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001310 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1311 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1312 X86::VR128RegisterClass);
1313 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001314 SDOperand Store =
1315 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001316 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001317 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001318 MemOps.push_back(Store);
1319 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001320 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001321 }
1322 if (!MemOps.empty())
1323 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1324 &MemOps[0], MemOps.size());
1325 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001326 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001327
1328 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1329 // arguments and the arguments after the retaddr has been pushed are
1330 // aligned.
1331 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1332 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1333 (StackSize & 7) == 0)
1334 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001336 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001337
Gordon Henriksen18ace102008-01-05 16:56:59 +00001338 // Some CCs need callee pop.
1339 if (IsCalleePop(Op)) {
1340 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 BytesCallerReserves = 0;
1342 } else {
1343 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 BytesCallerReserves = StackSize;
1348 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001349
Gordon Henriksen18ace102008-01-05 16:56:59 +00001350 if (!Is64Bit) {
1351 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1352 if (CC == CallingConv::X86_FastCall)
1353 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1354 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355
Anton Korobeynikove844e472007-08-15 17:12:32 +00001356 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357
1358 // Return the new list of results.
1359 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1360 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1361}
1362
Evan Chengbc077bf2008-01-10 00:09:10 +00001363SDOperand
1364X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1365 const SDOperand &StackPtr,
1366 const CCValAssign &VA,
1367 SDOperand Chain,
1368 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001369 unsigned LocMemOffset = VA.getLocMemOffset();
1370 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001371 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1372 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1373 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1374 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001375 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001376 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001377 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001378 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001379}
1380
Evan Cheng931a8f42008-01-29 19:34:22 +00001381/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1382/// struct return call to the specified function. X86-64 ABI specifies
1383/// some SRet calls are actually returned in registers. Since current
1384/// LLVM cannot represent multi-value calls, they are represent as
1385/// calls where the results are passed in a hidden struct provided by
1386/// the caller. This function examines the type of the struct to
1387/// determine the correct way to implement the call.
1388X86::X86_64SRet
1389X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1390 // FIXME: Disabled for now.
1391 return X86::InMemory;
1392
1393 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1394 const Type *RTy = PTy->getElementType();
1395 unsigned Size = getTargetData()->getABITypeSize(RTy);
1396 if (Size != 16 && Size != 32)
1397 return X86::InMemory;
1398
1399 if (Size == 32) {
1400 const StructType *STy = dyn_cast<StructType>(RTy);
1401 if (!STy) return X86::InMemory;
1402 if (STy->getNumElements() == 2 &&
1403 STy->getElementType(0) == Type::X86_FP80Ty &&
1404 STy->getElementType(1) == Type::X86_FP80Ty)
1405 return X86::InX87;
1406 }
1407
1408 bool AllFP = true;
1409 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1410 I != E; ++I) {
1411 const Type *STy = I->get();
1412 if (!STy->isFPOrFPVector()) {
1413 AllFP = false;
1414 break;
1415 }
1416 }
1417
1418 if (AllFP)
1419 return X86::InSSE;
1420 return X86::InGPR64;
1421}
1422
1423void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1424 CCAssignFn *Fn,
1425 CCState &CCInfo) {
1426 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1427 for (unsigned i = 1; i != NumOps; ++i) {
1428 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1429 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1430 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1431 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1432 cerr << "Call operand #" << i << " has unhandled type "
1433 << MVT::getValueTypeString(ArgVT) << "\n";
1434 abort();
1435 }
1436 }
1437}
1438
Gordon Henriksen18ace102008-01-05 16:56:59 +00001439SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1440 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001441 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001443 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001445 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1446 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001448 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001449 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001450
1451 assert(!(isVarArg && CC == CallingConv::Fast) &&
1452 "Var args not supported with calling convention fastcc");
1453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 // Analyze operands of the call, assigning locations to each operand.
1455 SmallVector<CCValAssign, 16> ArgLocs;
1456 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001457 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1458
1459 X86::X86_64SRet SRetMethod = X86::InMemory;
1460 if (Is64Bit && IsStructRet)
1461 // FIXME: We can't figure out type of the sret structure for indirect
1462 // calls. We need to copy more information from CallSite to the ISD::CALL
1463 // node.
1464 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1465 SRetMethod =
1466 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1467
1468 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1469 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1470 // a sret call.
1471 if (SRetMethod != X86::InMemory)
1472 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1473 else
1474 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475
1476 // Get a count of how many bytes are to be pushed on the stack.
1477 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001478 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001479 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001480
Gordon Henriksen18ace102008-01-05 16:56:59 +00001481 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1482 // arguments and the arguments after the retaddr has been pushed are aligned.
1483 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1484 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1485 (NumBytes & 7) == 0)
1486 NumBytes += 4;
1487
1488 int FPDiff = 0;
1489 if (IsTailCall) {
1490 // Lower arguments at fp - stackoffset + fpdiff.
1491 unsigned NumBytesCallerPushed =
1492 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1493 FPDiff = NumBytesCallerPushed - NumBytes;
1494
1495 // Set the delta of movement of the returnaddr stackslot.
1496 // But only set if delta is greater than previous delta.
1497 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1498 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1499 }
1500
Chris Lattner5872a362008-01-17 07:00:52 +00001501 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001503 SDOperand RetAddrFrIdx;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001504 if (IsTailCall) {
1505 // Adjust the Return address stack slot.
1506 if (FPDiff) {
1507 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1508 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1509 // Load the "old" Return address.
1510 RetAddrFrIdx =
1511 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001512 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1513 }
1514 }
1515
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001517 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 SmallVector<SDOperand, 8> MemOpChains;
1519
1520 SDOperand StackPtr;
1521
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001522 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001523 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001524 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1525 CCValAssign &VA = ArgLocs[i];
1526 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1527
1528 // Promote the value if needed.
1529 switch (VA.getLocInfo()) {
1530 default: assert(0 && "Unknown loc info!");
1531 case CCValAssign::Full: break;
1532 case CCValAssign::SExt:
1533 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1534 break;
1535 case CCValAssign::ZExt:
1536 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1537 break;
1538 case CCValAssign::AExt:
1539 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1540 break;
1541 }
1542
1543 if (VA.isRegLoc()) {
1544 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1545 } else {
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001546 if (!IsTailCall) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001547 assert(VA.isMemLoc());
1548 if (StackPtr.Val == 0)
1549 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1550
1551 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1552 Arg));
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001553 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1554 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001555 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 }
1557 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558
1559 if (!MemOpChains.empty())
1560 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1561 &MemOpChains[0], MemOpChains.size());
1562
1563 // Build a sequence of copy-to-reg nodes chained together with token chain
1564 // and flag operands which copy the outgoing args into registers.
1565 SDOperand InFlag;
1566 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1567 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1568 InFlag);
1569 InFlag = Chain.getValue(1);
1570 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001571
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001573 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001574 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1575 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1576 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1577 InFlag);
1578 InFlag = Chain.getValue(1);
1579 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001580 // If we are tail calling and generating PIC/GOT style code load the address
1581 // of the callee into ecx. The value in ecx is used as target of the tail
1582 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1583 // calls on PIC/GOT architectures. Normally we would just put the address of
1584 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1585 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001586 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001587 // Note: The actual moving to ecx is done further down.
1588 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1589 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1590 !G->getGlobal()->hasProtectedVisibility())
1591 Callee = LowerGlobalAddress(Callee, DAG);
1592 else if (isa<ExternalSymbolSDNode>(Callee))
1593 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001594 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001595
Gordon Henriksen18ace102008-01-05 16:56:59 +00001596 if (Is64Bit && isVarArg) {
1597 // From AMD64 ABI document:
1598 // For calls that may call functions that use varargs or stdargs
1599 // (prototype-less calls or calls to functions containing ellipsis (...) in
1600 // the declaration) %al is used as hidden argument to specify the number
1601 // of SSE registers used. The contents of %al do not need to match exactly
1602 // the number of registers, but must be an ubound on the number of SSE
1603 // registers used and is in the range 0 - 8 inclusive.
1604
1605 // Count the number of XMM registers allocated.
1606 static const unsigned XMMArgRegs[] = {
1607 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1608 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1609 };
1610 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1611
1612 Chain = DAG.getCopyToReg(Chain, X86::AL,
1613 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1614 InFlag = Chain.getValue(1);
1615 }
1616
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001617
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001618 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001619 if (IsTailCall) {
1620 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001621 SDOperand FIN;
1622 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001623 // Do not flag preceeding copytoreg stuff together with the following stuff.
1624 InFlag = SDOperand();
1625
1626 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1627 DAG, MF, this);
1628
Gordon Henriksen18ace102008-01-05 16:56:59 +00001629 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1630 CCValAssign &VA = ArgLocs[i];
1631 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001632 assert(VA.isMemLoc());
1633 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001634 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1635 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001636 // Create frame index.
1637 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1638 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1639 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1640 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001641
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001642 // Find virtual register for this argument.
1643 bool Found=false;
1644 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1645 if (TailCallClobberedVRegs[idx].first==i) {
1646 Arg = TailCallClobberedVRegs[idx].second;
1647 Found=true;
1648 break;
1649 }
1650 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1651 (Found==true && "No corresponding Argument was found"));
1652
Gordon Henriksen18ace102008-01-05 16:56:59 +00001653 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001654 // Copy relative to framepointer.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001655 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001656 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001658 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001659 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001660 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001661 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001662 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001663 }
1664 }
1665
1666 if (!MemOpChains2.empty())
1667 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001668 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001669
1670 // Store the return address to the appropriate stack slot.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001671 if (FPDiff) {
1672 // Calculate the new stack slot for the return address.
1673 int SlotSize = Is64Bit ? 8 : 4;
1674 int NewReturnAddrFI =
1675 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1676 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1677 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1678 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1679 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1680 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001681 }
1682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001683 // If the callee is a GlobalAddress node (quite common, every direct call is)
1684 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1685 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1686 // We should use extra load for direct calls to dllimported functions in
1687 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001688 if ((IsTailCall || !Is64Bit ||
1689 getTargetMachine().getCodeModel() != CodeModel::Large)
1690 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1691 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001692 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001693 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001694 if (IsTailCall || !Is64Bit ||
1695 getTargetMachine().getCodeModel() != CodeModel::Large)
1696 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1697 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001698 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1699
1700 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001701 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 Callee,InFlag);
1703 Callee = DAG.getRegister(Opc, getPointerTy());
1704 // Add register as live out.
1705 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001706 }
1707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 // Returns a chain & a flag for retval copy to use.
1709 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1710 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001711
1712 if (IsTailCall) {
1713 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001714 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1715 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001716 if (InFlag.Val)
1717 Ops.push_back(InFlag);
1718 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1719 InFlag = Chain.getValue(1);
1720
1721 // Returns a chain & a flag for retval copy to use.
1722 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1723 Ops.clear();
1724 }
1725
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001726 Ops.push_back(Chain);
1727 Ops.push_back(Callee);
1728
Gordon Henriksen18ace102008-01-05 16:56:59 +00001729 if (IsTailCall)
1730 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731
1732 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001733 if (!IsTailCall && !Is64Bit &&
1734 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735 Subtarget->isPICStyleGOT())
1736 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001737
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738 // Add argument registers to the end of the list so that they are known live
1739 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001740 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1741 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1742 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001743
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001744 if (InFlag.Val)
1745 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001746
Gordon Henriksen18ace102008-01-05 16:56:59 +00001747 if (IsTailCall) {
1748 assert(InFlag.Val &&
1749 "Flag must be set. Depend on flag being set in LowerRET");
1750 Chain = DAG.getNode(X86ISD::TAILCALL,
1751 Op.Val->getVTList(), &Ops[0], Ops.size());
1752
1753 return SDOperand(Chain.Val, Op.ResNo);
1754 }
1755
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001756 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757 InFlag = Chain.getValue(1);
1758
1759 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001760 unsigned NumBytesForCalleeToPush;
1761 if (IsCalleePop(Op))
1762 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001763 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 // If this is is a call to a struct-return function, the callee
1765 // pops the hidden struct pointer, so we have to push it back.
1766 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001767 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001769 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001770
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001771 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001772 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001773 DAG.getIntPtrConstant(NumBytes),
1774 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001775 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 InFlag = Chain.getValue(1);
1777
1778 // Handle result values, copying them out of physregs into vregs that we
1779 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001780 switch (SRetMethod) {
1781 default:
1782 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1783 case X86::InGPR64:
1784 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1785 X86::RAX, X86::RDX,
1786 MVT::i64, DAG), Op.ResNo);
1787 case X86::InSSE:
1788 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1789 X86::XMM0, X86::XMM1,
1790 MVT::f64, DAG), Op.ResNo);
1791 case X86::InX87:
1792 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1793 Op.ResNo);
1794 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001795}
1796
1797
1798//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001799// Fast Calling Convention (tail call) implementation
1800//===----------------------------------------------------------------------===//
1801
1802// Like std call, callee cleans arguments, convention except that ECX is
1803// reserved for storing the tail called function address. Only 2 registers are
1804// free for argument passing (inreg). Tail call optimization is performed
1805// provided:
1806// * tailcallopt is enabled
1807// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001808// On X86_64 architecture with GOT-style position independent code only local
1809// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001810// To keep the stack aligned according to platform abi the function
1811// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1812// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001813// If a tail called function callee has more arguments than the caller the
1814// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001815// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001816// original REtADDR, but before the saved framepointer or the spilled registers
1817// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1818// stack layout:
1819// arg1
1820// arg2
1821// RETADDR
1822// [ new RETADDR
1823// move area ]
1824// (possible EBP)
1825// ESI
1826// EDI
1827// local1 ..
1828
1829/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1830/// for a 16 byte align requirement.
1831unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1832 SelectionDAG& DAG) {
1833 if (PerformTailCallOpt) {
1834 MachineFunction &MF = DAG.getMachineFunction();
1835 const TargetMachine &TM = MF.getTarget();
1836 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1837 unsigned StackAlignment = TFI.getStackAlignment();
1838 uint64_t AlignMask = StackAlignment - 1;
1839 int64_t Offset = StackSize;
1840 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1841 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1842 // Number smaller than 12 so just add the difference.
1843 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1844 } else {
1845 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1846 Offset = ((~AlignMask) & Offset) + StackAlignment +
1847 (StackAlignment-SlotSize);
1848 }
1849 StackSize = Offset;
1850 }
1851 return StackSize;
1852}
1853
1854/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001855/// following the call is a return. A function is eligible if caller/callee
1856/// calling conventions match, currently only fastcc supports tail calls, and
1857/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001858bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1859 SDOperand Ret,
1860 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001861 if (!PerformTailCallOpt)
1862 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001863
1864 // Check whether CALL node immediatly preceeds the RET node and whether the
1865 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001866 unsigned NumOps = Ret.getNumOperands();
1867 if ((NumOps == 1 &&
1868 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1869 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001870 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001871 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1872 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001873 MachineFunction &MF = DAG.getMachineFunction();
1874 unsigned CallerCC = MF.getFunction()->getCallingConv();
1875 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1876 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1877 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001878 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001879 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001880 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001881 return true;
1882
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001883 // Can only do local tail calls (in same module, hidden or protected) on
1884 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001885 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1886 return G->getGlobal()->hasHiddenVisibility()
1887 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001888 }
1889 }
Evan Chenge7a87392007-11-02 01:26:22 +00001890
1891 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001892}
1893
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894//===----------------------------------------------------------------------===//
1895// Other Lowering Hooks
1896//===----------------------------------------------------------------------===//
1897
1898
1899SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001900 MachineFunction &MF = DAG.getMachineFunction();
1901 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1902 int ReturnAddrIndex = FuncInfo->getRAIndex();
1903
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001904 if (ReturnAddrIndex == 0) {
1905 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001906 if (Subtarget->is64Bit())
1907 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1908 else
1909 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001910
1911 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 }
1913
1914 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1915}
1916
1917
1918
1919/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1920/// specific condition code. It returns a false if it cannot do a direct
1921/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1922/// needed.
1923static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1924 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1925 SelectionDAG &DAG) {
1926 X86CC = X86::COND_INVALID;
1927 if (!isFP) {
1928 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1929 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1930 // X > -1 -> X == 0, jump !sign.
1931 RHS = DAG.getConstant(0, RHS.getValueType());
1932 X86CC = X86::COND_NS;
1933 return true;
1934 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1935 // X < 0 -> X == 0, jump on sign.
1936 X86CC = X86::COND_S;
1937 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001938 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1939 // X < 1 -> X <= 0
1940 RHS = DAG.getConstant(0, RHS.getValueType());
1941 X86CC = X86::COND_LE;
1942 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001943 }
1944 }
1945
1946 switch (SetCCOpcode) {
1947 default: break;
1948 case ISD::SETEQ: X86CC = X86::COND_E; break;
1949 case ISD::SETGT: X86CC = X86::COND_G; break;
1950 case ISD::SETGE: X86CC = X86::COND_GE; break;
1951 case ISD::SETLT: X86CC = X86::COND_L; break;
1952 case ISD::SETLE: X86CC = X86::COND_LE; break;
1953 case ISD::SETNE: X86CC = X86::COND_NE; break;
1954 case ISD::SETULT: X86CC = X86::COND_B; break;
1955 case ISD::SETUGT: X86CC = X86::COND_A; break;
1956 case ISD::SETULE: X86CC = X86::COND_BE; break;
1957 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1958 }
1959 } else {
1960 // On a floating point condition, the flags are set as follows:
1961 // ZF PF CF op
1962 // 0 | 0 | 0 | X > Y
1963 // 0 | 0 | 1 | X < Y
1964 // 1 | 0 | 0 | X == Y
1965 // 1 | 1 | 1 | unordered
1966 bool Flip = false;
1967 switch (SetCCOpcode) {
1968 default: break;
1969 case ISD::SETUEQ:
1970 case ISD::SETEQ: X86CC = X86::COND_E; break;
1971 case ISD::SETOLT: Flip = true; // Fallthrough
1972 case ISD::SETOGT:
1973 case ISD::SETGT: X86CC = X86::COND_A; break;
1974 case ISD::SETOLE: Flip = true; // Fallthrough
1975 case ISD::SETOGE:
1976 case ISD::SETGE: X86CC = X86::COND_AE; break;
1977 case ISD::SETUGT: Flip = true; // Fallthrough
1978 case ISD::SETULT:
1979 case ISD::SETLT: X86CC = X86::COND_B; break;
1980 case ISD::SETUGE: Flip = true; // Fallthrough
1981 case ISD::SETULE:
1982 case ISD::SETLE: X86CC = X86::COND_BE; break;
1983 case ISD::SETONE:
1984 case ISD::SETNE: X86CC = X86::COND_NE; break;
1985 case ISD::SETUO: X86CC = X86::COND_P; break;
1986 case ISD::SETO: X86CC = X86::COND_NP; break;
1987 }
1988 if (Flip)
1989 std::swap(LHS, RHS);
1990 }
1991
1992 return X86CC != X86::COND_INVALID;
1993}
1994
1995/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1996/// code. Current x86 isa includes the following FP cmov instructions:
1997/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1998static bool hasFPCMov(unsigned X86CC) {
1999 switch (X86CC) {
2000 default:
2001 return false;
2002 case X86::COND_B:
2003 case X86::COND_BE:
2004 case X86::COND_E:
2005 case X86::COND_P:
2006 case X86::COND_A:
2007 case X86::COND_AE:
2008 case X86::COND_NE:
2009 case X86::COND_NP:
2010 return true;
2011 }
2012}
2013
2014/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2015/// true if Op is undef or if its value falls within the specified range (L, H].
2016static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2017 if (Op.getOpcode() == ISD::UNDEF)
2018 return true;
2019
2020 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2021 return (Val >= Low && Val < Hi);
2022}
2023
2024/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2025/// true if Op is undef or if its value equal to the specified value.
2026static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2027 if (Op.getOpcode() == ISD::UNDEF)
2028 return true;
2029 return cast<ConstantSDNode>(Op)->getValue() == Val;
2030}
2031
2032/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2033/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2034bool X86::isPSHUFDMask(SDNode *N) {
2035 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2036
Dan Gohman7dc19012007-08-02 21:17:01 +00002037 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002038 return false;
2039
2040 // Check if the value doesn't reference the second vector.
2041 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2042 SDOperand Arg = N->getOperand(i);
2043 if (Arg.getOpcode() == ISD::UNDEF) continue;
2044 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002045 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046 return false;
2047 }
2048
2049 return true;
2050}
2051
2052/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2053/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2054bool X86::isPSHUFHWMask(SDNode *N) {
2055 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2056
2057 if (N->getNumOperands() != 8)
2058 return false;
2059
2060 // Lower quadword copied in order.
2061 for (unsigned i = 0; i != 4; ++i) {
2062 SDOperand Arg = N->getOperand(i);
2063 if (Arg.getOpcode() == ISD::UNDEF) continue;
2064 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2065 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2066 return false;
2067 }
2068
2069 // Upper quadword shuffled.
2070 for (unsigned i = 4; i != 8; ++i) {
2071 SDOperand Arg = N->getOperand(i);
2072 if (Arg.getOpcode() == ISD::UNDEF) continue;
2073 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2074 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2075 if (Val < 4 || Val > 7)
2076 return false;
2077 }
2078
2079 return true;
2080}
2081
2082/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2083/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2084bool X86::isPSHUFLWMask(SDNode *N) {
2085 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2086
2087 if (N->getNumOperands() != 8)
2088 return false;
2089
2090 // Upper quadword copied in order.
2091 for (unsigned i = 4; i != 8; ++i)
2092 if (!isUndefOrEqual(N->getOperand(i), i))
2093 return false;
2094
2095 // Lower quadword shuffled.
2096 for (unsigned i = 0; i != 4; ++i)
2097 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2098 return false;
2099
2100 return true;
2101}
2102
2103/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2104/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2105static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2106 if (NumElems != 2 && NumElems != 4) return false;
2107
2108 unsigned Half = NumElems / 2;
2109 for (unsigned i = 0; i < Half; ++i)
2110 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2111 return false;
2112 for (unsigned i = Half; i < NumElems; ++i)
2113 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2114 return false;
2115
2116 return true;
2117}
2118
2119bool X86::isSHUFPMask(SDNode *N) {
2120 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2121 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2122}
2123
2124/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2125/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2126/// half elements to come from vector 1 (which would equal the dest.) and
2127/// the upper half to come from vector 2.
2128static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2129 if (NumOps != 2 && NumOps != 4) return false;
2130
2131 unsigned Half = NumOps / 2;
2132 for (unsigned i = 0; i < Half; ++i)
2133 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2134 return false;
2135 for (unsigned i = Half; i < NumOps; ++i)
2136 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2137 return false;
2138 return true;
2139}
2140
2141static bool isCommutedSHUFP(SDNode *N) {
2142 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2143 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2144}
2145
2146/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2147/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2148bool X86::isMOVHLPSMask(SDNode *N) {
2149 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2150
2151 if (N->getNumOperands() != 4)
2152 return false;
2153
2154 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2155 return isUndefOrEqual(N->getOperand(0), 6) &&
2156 isUndefOrEqual(N->getOperand(1), 7) &&
2157 isUndefOrEqual(N->getOperand(2), 2) &&
2158 isUndefOrEqual(N->getOperand(3), 3);
2159}
2160
2161/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2162/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2163/// <2, 3, 2, 3>
2164bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2165 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2166
2167 if (N->getNumOperands() != 4)
2168 return false;
2169
2170 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2171 return isUndefOrEqual(N->getOperand(0), 2) &&
2172 isUndefOrEqual(N->getOperand(1), 3) &&
2173 isUndefOrEqual(N->getOperand(2), 2) &&
2174 isUndefOrEqual(N->getOperand(3), 3);
2175}
2176
2177/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2178/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2179bool X86::isMOVLPMask(SDNode *N) {
2180 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2181
2182 unsigned NumElems = N->getNumOperands();
2183 if (NumElems != 2 && NumElems != 4)
2184 return false;
2185
2186 for (unsigned i = 0; i < NumElems/2; ++i)
2187 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2188 return false;
2189
2190 for (unsigned i = NumElems/2; i < NumElems; ++i)
2191 if (!isUndefOrEqual(N->getOperand(i), i))
2192 return false;
2193
2194 return true;
2195}
2196
2197/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2198/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2199/// and MOVLHPS.
2200bool X86::isMOVHPMask(SDNode *N) {
2201 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2202
2203 unsigned NumElems = N->getNumOperands();
2204 if (NumElems != 2 && NumElems != 4)
2205 return false;
2206
2207 for (unsigned i = 0; i < NumElems/2; ++i)
2208 if (!isUndefOrEqual(N->getOperand(i), i))
2209 return false;
2210
2211 for (unsigned i = 0; i < NumElems/2; ++i) {
2212 SDOperand Arg = N->getOperand(i + NumElems/2);
2213 if (!isUndefOrEqual(Arg, i + NumElems))
2214 return false;
2215 }
2216
2217 return true;
2218}
2219
2220/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2221/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2222bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2223 bool V2IsSplat = false) {
2224 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2225 return false;
2226
2227 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2228 SDOperand BitI = Elts[i];
2229 SDOperand BitI1 = Elts[i+1];
2230 if (!isUndefOrEqual(BitI, j))
2231 return false;
2232 if (V2IsSplat) {
2233 if (isUndefOrEqual(BitI1, NumElts))
2234 return false;
2235 } else {
2236 if (!isUndefOrEqual(BitI1, j + NumElts))
2237 return false;
2238 }
2239 }
2240
2241 return true;
2242}
2243
2244bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2245 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2246 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2247}
2248
2249/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2250/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2251bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2252 bool V2IsSplat = false) {
2253 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2254 return false;
2255
2256 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2257 SDOperand BitI = Elts[i];
2258 SDOperand BitI1 = Elts[i+1];
2259 if (!isUndefOrEqual(BitI, j + NumElts/2))
2260 return false;
2261 if (V2IsSplat) {
2262 if (isUndefOrEqual(BitI1, NumElts))
2263 return false;
2264 } else {
2265 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2266 return false;
2267 }
2268 }
2269
2270 return true;
2271}
2272
2273bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2274 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2275 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2276}
2277
2278/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2279/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2280/// <0, 0, 1, 1>
2281bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2282 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2283
2284 unsigned NumElems = N->getNumOperands();
2285 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2286 return false;
2287
2288 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2289 SDOperand BitI = N->getOperand(i);
2290 SDOperand BitI1 = N->getOperand(i+1);
2291
2292 if (!isUndefOrEqual(BitI, j))
2293 return false;
2294 if (!isUndefOrEqual(BitI1, j))
2295 return false;
2296 }
2297
2298 return true;
2299}
2300
2301/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2302/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2303/// <2, 2, 3, 3>
2304bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2305 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2306
2307 unsigned NumElems = N->getNumOperands();
2308 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2309 return false;
2310
2311 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2312 SDOperand BitI = N->getOperand(i);
2313 SDOperand BitI1 = N->getOperand(i + 1);
2314
2315 if (!isUndefOrEqual(BitI, j))
2316 return false;
2317 if (!isUndefOrEqual(BitI1, j))
2318 return false;
2319 }
2320
2321 return true;
2322}
2323
2324/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2325/// specifies a shuffle of elements that is suitable for input to MOVSS,
2326/// MOVSD, and MOVD, i.e. setting the lowest element.
2327static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002328 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002329 return false;
2330
2331 if (!isUndefOrEqual(Elts[0], NumElts))
2332 return false;
2333
2334 for (unsigned i = 1; i < NumElts; ++i) {
2335 if (!isUndefOrEqual(Elts[i], i))
2336 return false;
2337 }
2338
2339 return true;
2340}
2341
2342bool X86::isMOVLMask(SDNode *N) {
2343 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2344 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2345}
2346
2347/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2348/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2349/// element of vector 2 and the other elements to come from vector 1 in order.
2350static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2351 bool V2IsSplat = false,
2352 bool V2IsUndef = false) {
2353 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2354 return false;
2355
2356 if (!isUndefOrEqual(Ops[0], 0))
2357 return false;
2358
2359 for (unsigned i = 1; i < NumOps; ++i) {
2360 SDOperand Arg = Ops[i];
2361 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2362 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2363 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2364 return false;
2365 }
2366
2367 return true;
2368}
2369
2370static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2371 bool V2IsUndef = false) {
2372 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2373 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2374 V2IsSplat, V2IsUndef);
2375}
2376
2377/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2378/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2379bool X86::isMOVSHDUPMask(SDNode *N) {
2380 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2381
2382 if (N->getNumOperands() != 4)
2383 return false;
2384
2385 // Expect 1, 1, 3, 3
2386 for (unsigned i = 0; i < 2; ++i) {
2387 SDOperand Arg = N->getOperand(i);
2388 if (Arg.getOpcode() == ISD::UNDEF) continue;
2389 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2390 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2391 if (Val != 1) return false;
2392 }
2393
2394 bool HasHi = false;
2395 for (unsigned i = 2; i < 4; ++i) {
2396 SDOperand Arg = N->getOperand(i);
2397 if (Arg.getOpcode() == ISD::UNDEF) continue;
2398 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2399 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2400 if (Val != 3) return false;
2401 HasHi = true;
2402 }
2403
2404 // Don't use movshdup if it can be done with a shufps.
2405 return HasHi;
2406}
2407
2408/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2409/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2410bool X86::isMOVSLDUPMask(SDNode *N) {
2411 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2412
2413 if (N->getNumOperands() != 4)
2414 return false;
2415
2416 // Expect 0, 0, 2, 2
2417 for (unsigned i = 0; i < 2; ++i) {
2418 SDOperand Arg = N->getOperand(i);
2419 if (Arg.getOpcode() == ISD::UNDEF) continue;
2420 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2421 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2422 if (Val != 0) return false;
2423 }
2424
2425 bool HasHi = false;
2426 for (unsigned i = 2; i < 4; ++i) {
2427 SDOperand Arg = N->getOperand(i);
2428 if (Arg.getOpcode() == ISD::UNDEF) continue;
2429 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2430 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2431 if (Val != 2) return false;
2432 HasHi = true;
2433 }
2434
2435 // Don't use movshdup if it can be done with a shufps.
2436 return HasHi;
2437}
2438
2439/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2440/// specifies a identity operation on the LHS or RHS.
2441static bool isIdentityMask(SDNode *N, bool RHS = false) {
2442 unsigned NumElems = N->getNumOperands();
2443 for (unsigned i = 0; i < NumElems; ++i)
2444 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2445 return false;
2446 return true;
2447}
2448
2449/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2450/// a splat of a single element.
2451static bool isSplatMask(SDNode *N) {
2452 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2453
2454 // This is a splat operation if each element of the permute is the same, and
2455 // if the value doesn't reference the second vector.
2456 unsigned NumElems = N->getNumOperands();
2457 SDOperand ElementBase;
2458 unsigned i = 0;
2459 for (; i != NumElems; ++i) {
2460 SDOperand Elt = N->getOperand(i);
2461 if (isa<ConstantSDNode>(Elt)) {
2462 ElementBase = Elt;
2463 break;
2464 }
2465 }
2466
2467 if (!ElementBase.Val)
2468 return false;
2469
2470 for (; i != NumElems; ++i) {
2471 SDOperand Arg = N->getOperand(i);
2472 if (Arg.getOpcode() == ISD::UNDEF) continue;
2473 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2474 if (Arg != ElementBase) return false;
2475 }
2476
2477 // Make sure it is a splat of the first vector operand.
2478 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2479}
2480
2481/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2482/// a splat of a single element and it's a 2 or 4 element mask.
2483bool X86::isSplatMask(SDNode *N) {
2484 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2485
2486 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2487 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2488 return false;
2489 return ::isSplatMask(N);
2490}
2491
2492/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2493/// specifies a splat of zero element.
2494bool X86::isSplatLoMask(SDNode *N) {
2495 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2496
2497 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2498 if (!isUndefOrEqual(N->getOperand(i), 0))
2499 return false;
2500 return true;
2501}
2502
2503/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2504/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2505/// instructions.
2506unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2507 unsigned NumOperands = N->getNumOperands();
2508 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2509 unsigned Mask = 0;
2510 for (unsigned i = 0; i < NumOperands; ++i) {
2511 unsigned Val = 0;
2512 SDOperand Arg = N->getOperand(NumOperands-i-1);
2513 if (Arg.getOpcode() != ISD::UNDEF)
2514 Val = cast<ConstantSDNode>(Arg)->getValue();
2515 if (Val >= NumOperands) Val -= NumOperands;
2516 Mask |= Val;
2517 if (i != NumOperands - 1)
2518 Mask <<= Shift;
2519 }
2520
2521 return Mask;
2522}
2523
2524/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2525/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2526/// instructions.
2527unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2528 unsigned Mask = 0;
2529 // 8 nodes, but we only care about the last 4.
2530 for (unsigned i = 7; i >= 4; --i) {
2531 unsigned Val = 0;
2532 SDOperand Arg = N->getOperand(i);
2533 if (Arg.getOpcode() != ISD::UNDEF)
2534 Val = cast<ConstantSDNode>(Arg)->getValue();
2535 Mask |= (Val - 4);
2536 if (i != 4)
2537 Mask <<= 2;
2538 }
2539
2540 return Mask;
2541}
2542
2543/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2544/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2545/// instructions.
2546unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2547 unsigned Mask = 0;
2548 // 8 nodes, but we only care about the first 4.
2549 for (int i = 3; i >= 0; --i) {
2550 unsigned Val = 0;
2551 SDOperand Arg = N->getOperand(i);
2552 if (Arg.getOpcode() != ISD::UNDEF)
2553 Val = cast<ConstantSDNode>(Arg)->getValue();
2554 Mask |= Val;
2555 if (i != 0)
2556 Mask <<= 2;
2557 }
2558
2559 return Mask;
2560}
2561
2562/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2563/// specifies a 8 element shuffle that can be broken into a pair of
2564/// PSHUFHW and PSHUFLW.
2565static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2566 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2567
2568 if (N->getNumOperands() != 8)
2569 return false;
2570
2571 // Lower quadword shuffled.
2572 for (unsigned i = 0; i != 4; ++i) {
2573 SDOperand Arg = N->getOperand(i);
2574 if (Arg.getOpcode() == ISD::UNDEF) continue;
2575 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2576 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002577 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002578 return false;
2579 }
2580
2581 // Upper quadword shuffled.
2582 for (unsigned i = 4; i != 8; ++i) {
2583 SDOperand Arg = N->getOperand(i);
2584 if (Arg.getOpcode() == ISD::UNDEF) continue;
2585 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2586 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2587 if (Val < 4 || Val > 7)
2588 return false;
2589 }
2590
2591 return true;
2592}
2593
Chris Lattnere6aa3862007-11-25 00:24:49 +00002594/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002595/// values in ther permute mask.
2596static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2597 SDOperand &V2, SDOperand &Mask,
2598 SelectionDAG &DAG) {
2599 MVT::ValueType VT = Op.getValueType();
2600 MVT::ValueType MaskVT = Mask.getValueType();
2601 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2602 unsigned NumElems = Mask.getNumOperands();
2603 SmallVector<SDOperand, 8> MaskVec;
2604
2605 for (unsigned i = 0; i != NumElems; ++i) {
2606 SDOperand Arg = Mask.getOperand(i);
2607 if (Arg.getOpcode() == ISD::UNDEF) {
2608 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2609 continue;
2610 }
2611 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2612 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2613 if (Val < NumElems)
2614 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2615 else
2616 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2617 }
2618
2619 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002620 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002621 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2622}
2623
Evan Chenga6769df2007-12-07 21:30:01 +00002624/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2625/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002626static
2627SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2628 MVT::ValueType MaskVT = Mask.getValueType();
2629 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2630 unsigned NumElems = Mask.getNumOperands();
2631 SmallVector<SDOperand, 8> MaskVec;
2632 for (unsigned i = 0; i != NumElems; ++i) {
2633 SDOperand Arg = Mask.getOperand(i);
2634 if (Arg.getOpcode() == ISD::UNDEF) {
2635 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2636 continue;
2637 }
2638 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2639 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2640 if (Val < NumElems)
2641 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2642 else
2643 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2644 }
2645 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2646}
2647
2648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2650/// match movhlps. The lower half elements should come from upper half of
2651/// V1 (and in order), and the upper half elements should come from the upper
2652/// half of V2 (and in order).
2653static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2654 unsigned NumElems = Mask->getNumOperands();
2655 if (NumElems != 4)
2656 return false;
2657 for (unsigned i = 0, e = 2; i != e; ++i)
2658 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2659 return false;
2660 for (unsigned i = 2; i != 4; ++i)
2661 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2662 return false;
2663 return true;
2664}
2665
2666/// isScalarLoadToVector - Returns true if the node is a scalar load that
2667/// is promoted to a vector.
2668static inline bool isScalarLoadToVector(SDNode *N) {
2669 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2670 N = N->getOperand(0).Val;
2671 return ISD::isNON_EXTLoad(N);
2672 }
2673 return false;
2674}
2675
2676/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2677/// match movlp{s|d}. The lower half elements should come from lower half of
2678/// V1 (and in order), and the upper half elements should come from the upper
2679/// half of V2 (and in order). And since V1 will become the source of the
2680/// MOVLP, it must be either a vector load or a scalar load to vector.
2681static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2682 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2683 return false;
2684 // Is V2 is a vector load, don't do this transformation. We will try to use
2685 // load folding shufps op.
2686 if (ISD::isNON_EXTLoad(V2))
2687 return false;
2688
2689 unsigned NumElems = Mask->getNumOperands();
2690 if (NumElems != 2 && NumElems != 4)
2691 return false;
2692 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2693 if (!isUndefOrEqual(Mask->getOperand(i), i))
2694 return false;
2695 for (unsigned i = NumElems/2; i != NumElems; ++i)
2696 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2697 return false;
2698 return true;
2699}
2700
2701/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2702/// all the same.
2703static bool isSplatVector(SDNode *N) {
2704 if (N->getOpcode() != ISD::BUILD_VECTOR)
2705 return false;
2706
2707 SDOperand SplatValue = N->getOperand(0);
2708 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2709 if (N->getOperand(i) != SplatValue)
2710 return false;
2711 return true;
2712}
2713
2714/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2715/// to an undef.
2716static bool isUndefShuffle(SDNode *N) {
2717 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2718 return false;
2719
2720 SDOperand V1 = N->getOperand(0);
2721 SDOperand V2 = N->getOperand(1);
2722 SDOperand Mask = N->getOperand(2);
2723 unsigned NumElems = Mask.getNumOperands();
2724 for (unsigned i = 0; i != NumElems; ++i) {
2725 SDOperand Arg = Mask.getOperand(i);
2726 if (Arg.getOpcode() != ISD::UNDEF) {
2727 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2728 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2729 return false;
2730 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2731 return false;
2732 }
2733 }
2734 return true;
2735}
2736
2737/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2738/// constant +0.0.
2739static inline bool isZeroNode(SDOperand Elt) {
2740 return ((isa<ConstantSDNode>(Elt) &&
2741 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2742 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002743 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744}
2745
2746/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2747/// to an zero vector.
2748static bool isZeroShuffle(SDNode *N) {
2749 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2750 return false;
2751
2752 SDOperand V1 = N->getOperand(0);
2753 SDOperand V2 = N->getOperand(1);
2754 SDOperand Mask = N->getOperand(2);
2755 unsigned NumElems = Mask.getNumOperands();
2756 for (unsigned i = 0; i != NumElems; ++i) {
2757 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002758 if (Arg.getOpcode() == ISD::UNDEF)
2759 continue;
2760
2761 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2762 if (Idx < NumElems) {
2763 unsigned Opc = V1.Val->getOpcode();
2764 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2765 continue;
2766 if (Opc != ISD::BUILD_VECTOR ||
2767 !isZeroNode(V1.Val->getOperand(Idx)))
2768 return false;
2769 } else if (Idx >= NumElems) {
2770 unsigned Opc = V2.Val->getOpcode();
2771 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2772 continue;
2773 if (Opc != ISD::BUILD_VECTOR ||
2774 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2775 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776 }
2777 }
2778 return true;
2779}
2780
2781/// getZeroVector - Returns a vector of specified type with all zero elements.
2782///
2783static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2784 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002785
2786 // Always build zero 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(0, 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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002795}
2796
Chris Lattnere6aa3862007-11-25 00:24:49 +00002797/// getOnesVector - Returns a vector of specified type with all bits set.
2798///
2799static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2800 assert(MVT::isVector(VT) && "Expected a vector type");
2801
2802 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2803 // type. This ensures they get CSE'd.
2804 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2805 SDOperand Vec;
2806 if (MVT::getSizeInBits(VT) == 64) // MMX
2807 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2808 else // SSE
2809 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2810 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2811}
2812
2813
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2815/// that point to V2 points to its first element.
2816static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2817 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2818
2819 bool Changed = false;
2820 SmallVector<SDOperand, 8> MaskVec;
2821 unsigned NumElems = Mask.getNumOperands();
2822 for (unsigned i = 0; i != NumElems; ++i) {
2823 SDOperand Arg = Mask.getOperand(i);
2824 if (Arg.getOpcode() != ISD::UNDEF) {
2825 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2826 if (Val > NumElems) {
2827 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2828 Changed = true;
2829 }
2830 }
2831 MaskVec.push_back(Arg);
2832 }
2833
2834 if (Changed)
2835 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2836 &MaskVec[0], MaskVec.size());
2837 return Mask;
2838}
2839
2840/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2841/// operation of specified width.
2842static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2843 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2844 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2845
2846 SmallVector<SDOperand, 8> MaskVec;
2847 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2848 for (unsigned i = 1; i != NumElems; ++i)
2849 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2850 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2851}
2852
2853/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2854/// of specified width.
2855static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2856 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2857 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2858 SmallVector<SDOperand, 8> MaskVec;
2859 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2860 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2861 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2862 }
2863 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2864}
2865
2866/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2867/// of specified width.
2868static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2869 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2870 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2871 unsigned Half = NumElems/2;
2872 SmallVector<SDOperand, 8> MaskVec;
2873 for (unsigned i = 0; i != Half; ++i) {
2874 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2875 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2876 }
2877 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2878}
2879
2880/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2881///
2882static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2883 SDOperand V1 = Op.getOperand(0);
2884 SDOperand Mask = Op.getOperand(2);
2885 MVT::ValueType VT = Op.getValueType();
2886 unsigned NumElems = Mask.getNumOperands();
2887 Mask = getUnpacklMask(NumElems, DAG);
2888 while (NumElems != 4) {
2889 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2890 NumElems >>= 1;
2891 }
2892 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2893
Chris Lattnere6aa3862007-11-25 00:24:49 +00002894 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2896 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2897 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2898}
2899
2900/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002901/// vector of zero or undef vector. This produces a shuffle where the low
2902/// element of V2 is swizzled into the zero/undef vector, landing at element
2903/// 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 +00002904static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2905 unsigned NumElems, unsigned Idx,
2906 bool isZero, SelectionDAG &DAG) {
2907 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2908 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2909 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002910 SmallVector<SDOperand, 16> MaskVec;
2911 for (unsigned i = 0; i != NumElems; ++i)
2912 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2913 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2914 else
2915 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002916 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2917 &MaskVec[0], MaskVec.size());
2918 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2919}
2920
2921/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2922///
2923static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2924 unsigned NumNonZero, unsigned NumZero,
2925 SelectionDAG &DAG, TargetLowering &TLI) {
2926 if (NumNonZero > 8)
2927 return SDOperand();
2928
2929 SDOperand V(0, 0);
2930 bool First = true;
2931 for (unsigned i = 0; i < 16; ++i) {
2932 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2933 if (ThisIsNonZero && First) {
2934 if (NumZero)
2935 V = getZeroVector(MVT::v8i16, DAG);
2936 else
2937 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2938 First = false;
2939 }
2940
2941 if ((i & 1) != 0) {
2942 SDOperand ThisElt(0, 0), LastElt(0, 0);
2943 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2944 if (LastIsNonZero) {
2945 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2946 }
2947 if (ThisIsNonZero) {
2948 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2949 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2950 ThisElt, DAG.getConstant(8, MVT::i8));
2951 if (LastIsNonZero)
2952 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2953 } else
2954 ThisElt = LastElt;
2955
2956 if (ThisElt.Val)
2957 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002958 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959 }
2960 }
2961
2962 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2963}
2964
2965/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2966///
2967static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2968 unsigned NumNonZero, unsigned NumZero,
2969 SelectionDAG &DAG, TargetLowering &TLI) {
2970 if (NumNonZero > 4)
2971 return SDOperand();
2972
2973 SDOperand V(0, 0);
2974 bool First = true;
2975 for (unsigned i = 0; i < 8; ++i) {
2976 bool isNonZero = (NonZeros & (1 << i)) != 0;
2977 if (isNonZero) {
2978 if (First) {
2979 if (NumZero)
2980 V = getZeroVector(MVT::v8i16, DAG);
2981 else
2982 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2983 First = false;
2984 }
2985 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002986 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002987 }
2988 }
2989
2990 return V;
2991}
2992
2993SDOperand
2994X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002995 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2996 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2997 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2998 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2999 // eliminated on x86-32 hosts.
3000 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3001 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002
Chris Lattnere6aa3862007-11-25 00:24:49 +00003003 if (ISD::isBuildVectorAllOnes(Op.Val))
3004 return getOnesVector(Op.getValueType(), DAG);
3005 return getZeroVector(Op.getValueType(), DAG);
3006 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007
3008 MVT::ValueType VT = Op.getValueType();
3009 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3010 unsigned EVTBits = MVT::getSizeInBits(EVT);
3011
3012 unsigned NumElems = Op.getNumOperands();
3013 unsigned NumZero = 0;
3014 unsigned NumNonZero = 0;
3015 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00003016 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00003017 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 for (unsigned i = 0; i < NumElems; ++i) {
3019 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003020 if (Elt.getOpcode() == ISD::UNDEF)
3021 continue;
3022 Values.insert(Elt);
3023 if (Elt.getOpcode() != ISD::Constant &&
3024 Elt.getOpcode() != ISD::ConstantFP)
3025 HasNonImms = true;
3026 if (isZeroNode(Elt))
3027 NumZero++;
3028 else {
3029 NonZeros |= (1 << i);
3030 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 }
3032 }
3033
3034 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003035 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3036 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003037 }
3038
3039 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3040 if (Values.size() == 1)
3041 return SDOperand();
3042
3043 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00003044 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 unsigned Idx = CountTrailingZeros_32(NonZeros);
3046 SDOperand Item = Op.getOperand(Idx);
3047 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3048 if (Idx == 0)
3049 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3050 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3051 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00003052 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
3053 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054
3055 if (EVTBits == 32) {
3056 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3057 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3058 DAG);
3059 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3060 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3061 SmallVector<SDOperand, 8> MaskVec;
3062 for (unsigned i = 0; i < NumElems; i++)
3063 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3064 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3065 &MaskVec[0], MaskVec.size());
3066 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3067 DAG.getNode(ISD::UNDEF, VT), Mask);
3068 }
3069 }
3070
Dan Gohman21463242007-07-24 22:55:08 +00003071 // A vector full of immediates; various special cases are already
3072 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00003073 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00003074 return SDOperand();
3075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003076 // Let legalizer expand 2-wide build_vectors.
3077 if (EVTBits == 64)
3078 return SDOperand();
3079
3080 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3081 if (EVTBits == 8 && NumElems == 16) {
3082 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3083 *this);
3084 if (V.Val) return V;
3085 }
3086
3087 if (EVTBits == 16 && NumElems == 8) {
3088 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3089 *this);
3090 if (V.Val) return V;
3091 }
3092
3093 // If element VT is == 32 bits, turn it into a number of shuffles.
3094 SmallVector<SDOperand, 8> V;
3095 V.resize(NumElems);
3096 if (NumElems == 4 && NumZero > 0) {
3097 for (unsigned i = 0; i < 4; ++i) {
3098 bool isZero = !(NonZeros & (1 << i));
3099 if (isZero)
3100 V[i] = getZeroVector(VT, DAG);
3101 else
3102 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3103 }
3104
3105 for (unsigned i = 0; i < 2; ++i) {
3106 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3107 default: break;
3108 case 0:
3109 V[i] = V[i*2]; // Must be a zero vector.
3110 break;
3111 case 1:
3112 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3113 getMOVLMask(NumElems, DAG));
3114 break;
3115 case 2:
3116 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3117 getMOVLMask(NumElems, DAG));
3118 break;
3119 case 3:
3120 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3121 getUnpacklMask(NumElems, DAG));
3122 break;
3123 }
3124 }
3125
3126 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3127 // clears the upper bits.
3128 // FIXME: we can do the same for v4f32 case when we know both parts of
3129 // the lower half come from scalar_to_vector (loadf32). We should do
3130 // that in post legalizer dag combiner with target specific hooks.
3131 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3132 return V[0];
3133 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3134 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3135 SmallVector<SDOperand, 8> MaskVec;
3136 bool Reverse = (NonZeros & 0x3) == 2;
3137 for (unsigned i = 0; i < 2; ++i)
3138 if (Reverse)
3139 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3140 else
3141 MaskVec.push_back(DAG.getConstant(i, EVT));
3142 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3143 for (unsigned i = 0; i < 2; ++i)
3144 if (Reverse)
3145 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3146 else
3147 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3148 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3149 &MaskVec[0], MaskVec.size());
3150 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3151 }
3152
3153 if (Values.size() > 2) {
3154 // Expand into a number of unpckl*.
3155 // e.g. for v4f32
3156 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3157 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3158 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3159 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3160 for (unsigned i = 0; i < NumElems; ++i)
3161 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3162 NumElems >>= 1;
3163 while (NumElems != 0) {
3164 for (unsigned i = 0; i < NumElems; ++i)
3165 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3166 UnpckMask);
3167 NumElems >>= 1;
3168 }
3169 return V[0];
3170 }
3171
3172 return SDOperand();
3173}
3174
Evan Chengfca29242007-12-07 08:07:39 +00003175static
3176SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3177 SDOperand PermMask, SelectionDAG &DAG,
3178 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003179 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003180 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3181 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003182 MVT::ValueType PtrVT = TLI.getPointerTy();
3183 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3184 PermMask.Val->op_end());
3185
3186 // First record which half of which vector the low elements come from.
3187 SmallVector<unsigned, 4> LowQuad(4);
3188 for (unsigned i = 0; i < 4; ++i) {
3189 SDOperand Elt = MaskElts[i];
3190 if (Elt.getOpcode() == ISD::UNDEF)
3191 continue;
3192 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3193 int QuadIdx = EltIdx / 4;
3194 ++LowQuad[QuadIdx];
3195 }
3196 int BestLowQuad = -1;
3197 unsigned MaxQuad = 1;
3198 for (unsigned i = 0; i < 4; ++i) {
3199 if (LowQuad[i] > MaxQuad) {
3200 BestLowQuad = i;
3201 MaxQuad = LowQuad[i];
3202 }
Evan Chengfca29242007-12-07 08:07:39 +00003203 }
3204
Evan Cheng75184a92007-12-11 01:46:18 +00003205 // Record which half of which vector the high elements come from.
3206 SmallVector<unsigned, 4> HighQuad(4);
3207 for (unsigned i = 4; i < 8; ++i) {
3208 SDOperand Elt = MaskElts[i];
3209 if (Elt.getOpcode() == ISD::UNDEF)
3210 continue;
3211 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3212 int QuadIdx = EltIdx / 4;
3213 ++HighQuad[QuadIdx];
3214 }
3215 int BestHighQuad = -1;
3216 MaxQuad = 1;
3217 for (unsigned i = 0; i < 4; ++i) {
3218 if (HighQuad[i] > MaxQuad) {
3219 BestHighQuad = i;
3220 MaxQuad = HighQuad[i];
3221 }
3222 }
3223
3224 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3225 if (BestLowQuad != -1 || BestHighQuad != -1) {
3226 // First sort the 4 chunks in order using shufpd.
3227 SmallVector<SDOperand, 8> MaskVec;
3228 if (BestLowQuad != -1)
3229 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3230 else
3231 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3232 if (BestHighQuad != -1)
3233 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3234 else
3235 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3236 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3237 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3238 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3239 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3240 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3241
3242 // Now sort high and low parts separately.
3243 BitVector InOrder(8);
3244 if (BestLowQuad != -1) {
3245 // Sort lower half in order using PSHUFLW.
3246 MaskVec.clear();
3247 bool AnyOutOrder = false;
3248 for (unsigned i = 0; i != 4; ++i) {
3249 SDOperand Elt = MaskElts[i];
3250 if (Elt.getOpcode() == ISD::UNDEF) {
3251 MaskVec.push_back(Elt);
3252 InOrder.set(i);
3253 } else {
3254 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3255 if (EltIdx != i)
3256 AnyOutOrder = true;
3257 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3258 // If this element is in the right place after this shuffle, then
3259 // remember it.
3260 if ((int)(EltIdx / 4) == BestLowQuad)
3261 InOrder.set(i);
3262 }
3263 }
3264 if (AnyOutOrder) {
3265 for (unsigned i = 4; i != 8; ++i)
3266 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3267 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3268 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3269 }
3270 }
3271
3272 if (BestHighQuad != -1) {
3273 // Sort high half in order using PSHUFHW if possible.
3274 MaskVec.clear();
3275 for (unsigned i = 0; i != 4; ++i)
3276 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3277 bool AnyOutOrder = false;
3278 for (unsigned i = 4; i != 8; ++i) {
3279 SDOperand Elt = MaskElts[i];
3280 if (Elt.getOpcode() == ISD::UNDEF) {
3281 MaskVec.push_back(Elt);
3282 InOrder.set(i);
3283 } else {
3284 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3285 if (EltIdx != i)
3286 AnyOutOrder = true;
3287 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3288 // If this element is in the right place after this shuffle, then
3289 // remember it.
3290 if ((int)(EltIdx / 4) == BestHighQuad)
3291 InOrder.set(i);
3292 }
3293 }
3294 if (AnyOutOrder) {
3295 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3296 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3297 }
3298 }
3299
3300 // The other elements are put in the right place using pextrw and pinsrw.
3301 for (unsigned i = 0; i != 8; ++i) {
3302 if (InOrder[i])
3303 continue;
3304 SDOperand Elt = MaskElts[i];
3305 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3306 if (EltIdx == i)
3307 continue;
3308 SDOperand ExtOp = (EltIdx < 8)
3309 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3310 DAG.getConstant(EltIdx, PtrVT))
3311 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3312 DAG.getConstant(EltIdx - 8, PtrVT));
3313 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3314 DAG.getConstant(i, PtrVT));
3315 }
3316 return NewV;
3317 }
3318
3319 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3320 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003321 // First, let's find out how many elements are already in the right order.
3322 unsigned V1InOrder = 0;
3323 unsigned V1FromV1 = 0;
3324 unsigned V2InOrder = 0;
3325 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003326 SmallVector<SDOperand, 8> V1Elts;
3327 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003328 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003329 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003330 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003331 V1Elts.push_back(Elt);
3332 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003333 ++V1InOrder;
3334 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003335 continue;
3336 }
3337 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3338 if (EltIdx == i) {
3339 V1Elts.push_back(Elt);
3340 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3341 ++V1InOrder;
3342 } else if (EltIdx == i+8) {
3343 V1Elts.push_back(Elt);
3344 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3345 ++V2InOrder;
3346 } else if (EltIdx < 8) {
3347 V1Elts.push_back(Elt);
3348 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003349 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003350 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3351 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003352 }
3353 }
3354
3355 if (V2InOrder > V1InOrder) {
3356 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3357 std::swap(V1, V2);
3358 std::swap(V1Elts, V2Elts);
3359 std::swap(V1FromV1, V2FromV2);
3360 }
3361
Evan Cheng75184a92007-12-11 01:46:18 +00003362 if ((V1FromV1 + V1InOrder) != 8) {
3363 // Some elements are from V2.
3364 if (V1FromV1) {
3365 // If there are elements that are from V1 but out of place,
3366 // then first sort them in place
3367 SmallVector<SDOperand, 8> MaskVec;
3368 for (unsigned i = 0; i < 8; ++i) {
3369 SDOperand Elt = V1Elts[i];
3370 if (Elt.getOpcode() == ISD::UNDEF) {
3371 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3372 continue;
3373 }
3374 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3375 if (EltIdx >= 8)
3376 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3377 else
3378 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3379 }
3380 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3381 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003382 }
Evan Cheng75184a92007-12-11 01:46:18 +00003383
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 if (EltIdx < 8)
3391 continue;
3392 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3393 DAG.getConstant(EltIdx - 8, PtrVT));
3394 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3395 DAG.getConstant(i, PtrVT));
3396 }
3397 return NewV;
3398 } else {
3399 // All elements are from V1.
3400 NewV = V1;
3401 for (unsigned i = 0; i < 8; ++i) {
3402 SDOperand Elt = V1Elts[i];
3403 if (Elt.getOpcode() == ISD::UNDEF)
3404 continue;
3405 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3406 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3407 DAG.getConstant(EltIdx, PtrVT));
3408 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3409 DAG.getConstant(i, PtrVT));
3410 }
3411 return NewV;
3412 }
3413}
3414
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003415/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3416/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3417/// done when every pair / quad of shuffle mask elements point to elements in
3418/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003419/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3420static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003421SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3422 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003423 SDOperand PermMask, SelectionDAG &DAG,
3424 TargetLowering &TLI) {
3425 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003426 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3427 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3428 MVT::ValueType NewVT = MaskVT;
3429 switch (VT) {
3430 case MVT::v4f32: NewVT = MVT::v2f64; break;
3431 case MVT::v4i32: NewVT = MVT::v2i64; break;
3432 case MVT::v8i16: NewVT = MVT::v4i32; break;
3433 case MVT::v16i8: NewVT = MVT::v4i32; break;
3434 default: assert(false && "Unexpected!");
3435 }
3436
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003437 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003438 if (MVT::isInteger(VT))
3439 NewVT = MVT::v2i64;
3440 else
3441 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003442 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003443 unsigned Scale = NumElems / NewWidth;
3444 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003445 for (unsigned i = 0; i < NumElems; i += Scale) {
3446 unsigned StartIdx = ~0U;
3447 for (unsigned j = 0; j < Scale; ++j) {
3448 SDOperand Elt = PermMask.getOperand(i+j);
3449 if (Elt.getOpcode() == ISD::UNDEF)
3450 continue;
3451 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3452 if (StartIdx == ~0U)
3453 StartIdx = EltIdx - (EltIdx % Scale);
3454 if (EltIdx != StartIdx + j)
3455 return SDOperand();
3456 }
3457 if (StartIdx == ~0U)
3458 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3459 else
3460 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003461 }
3462
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003463 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3464 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3465 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3466 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3467 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003468}
3469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003470SDOperand
3471X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3472 SDOperand V1 = Op.getOperand(0);
3473 SDOperand V2 = Op.getOperand(1);
3474 SDOperand PermMask = Op.getOperand(2);
3475 MVT::ValueType VT = Op.getValueType();
3476 unsigned NumElems = PermMask.getNumOperands();
3477 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3478 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3479 bool V1IsSplat = false;
3480 bool V2IsSplat = false;
3481
3482 if (isUndefShuffle(Op.Val))
3483 return DAG.getNode(ISD::UNDEF, VT);
3484
3485 if (isZeroShuffle(Op.Val))
3486 return getZeroVector(VT, DAG);
3487
3488 if (isIdentityMask(PermMask.Val))
3489 return V1;
3490 else if (isIdentityMask(PermMask.Val, true))
3491 return V2;
3492
3493 if (isSplatMask(PermMask.Val)) {
3494 if (NumElems <= 4) return Op;
3495 // Promote it to a v4i32 splat.
3496 return PromoteSplat(Op, DAG);
3497 }
3498
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003499 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3500 // do it!
3501 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3502 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3503 if (NewOp.Val)
3504 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3505 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3506 // FIXME: Figure out a cleaner way to do this.
3507 // Try to make use of movq to zero out the top part.
3508 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3509 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3510 if (NewOp.Val) {
3511 SDOperand NewV1 = NewOp.getOperand(0);
3512 SDOperand NewV2 = NewOp.getOperand(1);
3513 SDOperand NewMask = NewOp.getOperand(2);
3514 if (isCommutedMOVL(NewMask.Val, true, false)) {
3515 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3516 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3517 NewV1, NewV2, getMOVLMask(2, DAG));
3518 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3519 }
3520 }
3521 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3522 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3523 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3524 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3525 }
3526 }
3527
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528 if (X86::isMOVLMask(PermMask.Val))
3529 return (V1IsUndef) ? V2 : Op;
3530
3531 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3532 X86::isMOVSLDUPMask(PermMask.Val) ||
3533 X86::isMOVHLPSMask(PermMask.Val) ||
3534 X86::isMOVHPMask(PermMask.Val) ||
3535 X86::isMOVLPMask(PermMask.Val))
3536 return Op;
3537
3538 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3539 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3540 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3541
3542 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003543 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3544 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003545 V1IsSplat = isSplatVector(V1.Val);
3546 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003547
3548 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003549 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3550 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3551 std::swap(V1IsSplat, V2IsSplat);
3552 std::swap(V1IsUndef, V2IsUndef);
3553 Commuted = true;
3554 }
3555
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003556 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003557 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3558 if (V2IsUndef) return V1;
3559 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3560 if (V2IsSplat) {
3561 // V2 is a splat, so the mask may be malformed. That is, it may point
3562 // to any V2 element. The instruction selectior won't like this. Get
3563 // a corrected mask and commute to form a proper MOVS{S|D}.
3564 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3565 if (NewMask.Val != PermMask.Val)
3566 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3567 }
3568 return Op;
3569 }
3570
3571 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3572 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3573 X86::isUNPCKLMask(PermMask.Val) ||
3574 X86::isUNPCKHMask(PermMask.Val))
3575 return Op;
3576
3577 if (V2IsSplat) {
3578 // Normalize mask so all entries that point to V2 points to its first
3579 // element then try to match unpck{h|l} again. If match, return a
3580 // new vector_shuffle with the corrected mask.
3581 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3582 if (NewMask.Val != PermMask.Val) {
3583 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3584 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3585 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3586 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3587 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3588 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3589 }
3590 }
3591 }
3592
3593 // Normalize the node to match x86 shuffle ops if needed
3594 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3595 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3596
3597 if (Commuted) {
3598 // Commute is back and try unpck* again.
3599 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3600 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3601 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3602 X86::isUNPCKLMask(PermMask.Val) ||
3603 X86::isUNPCKHMask(PermMask.Val))
3604 return Op;
3605 }
3606
3607 // If VT is integer, try PSHUF* first, then SHUFP*.
3608 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003609 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3610 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3611 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3612 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003613 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 if (X86::isSHUFPMask(PermMask.Val) &&
3622 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3623 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003624 } else {
3625 // Floating point cases in the other order.
3626 if (X86::isSHUFPMask(PermMask.Val))
3627 return Op;
3628 if (X86::isPSHUFDMask(PermMask.Val) ||
3629 X86::isPSHUFHWMask(PermMask.Val) ||
3630 X86::isPSHUFLWMask(PermMask.Val)) {
3631 if (V2.getOpcode() != ISD::UNDEF)
3632 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3633 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3634 return Op;
3635 }
3636 }
3637
Evan Cheng75184a92007-12-11 01:46:18 +00003638 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3639 if (VT == MVT::v8i16) {
3640 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3641 if (NewOp.Val)
3642 return NewOp;
3643 }
3644
3645 // Handle all 4 wide cases with a number of shuffles.
3646 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003647 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003648 MVT::ValueType MaskVT = PermMask.getValueType();
3649 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3650 SmallVector<std::pair<int, int>, 8> Locs;
3651 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003652 SmallVector<SDOperand, 8> Mask1(NumElems,
3653 DAG.getNode(ISD::UNDEF, MaskEVT));
3654 SmallVector<SDOperand, 8> Mask2(NumElems,
3655 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003656 unsigned NumHi = 0;
3657 unsigned NumLo = 0;
3658 // If no more than two elements come from either vector. This can be
3659 // implemented with two shuffles. First shuffle gather the elements.
3660 // The second shuffle, which takes the first shuffle as both of its
3661 // vector operands, put the elements into the right order.
3662 for (unsigned i = 0; i != NumElems; ++i) {
3663 SDOperand Elt = PermMask.getOperand(i);
3664 if (Elt.getOpcode() == ISD::UNDEF) {
3665 Locs[i] = std::make_pair(-1, -1);
3666 } else {
3667 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3668 if (Val < NumElems) {
3669 Locs[i] = std::make_pair(0, NumLo);
3670 Mask1[NumLo] = Elt;
3671 NumLo++;
3672 } else {
3673 Locs[i] = std::make_pair(1, NumHi);
3674 if (2+NumHi < NumElems)
3675 Mask1[2+NumHi] = Elt;
3676 NumHi++;
3677 }
3678 }
3679 }
3680 if (NumLo <= 2 && NumHi <= 2) {
3681 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3682 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3683 &Mask1[0], Mask1.size()));
3684 for (unsigned i = 0; i != NumElems; ++i) {
3685 if (Locs[i].first == -1)
3686 continue;
3687 else {
3688 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3689 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3690 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3691 }
3692 }
3693
3694 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3695 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3696 &Mask2[0], Mask2.size()));
3697 }
3698
3699 // Break it into (shuffle shuffle_hi, shuffle_lo).
3700 Locs.clear();
3701 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3702 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3703 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3704 unsigned MaskIdx = 0;
3705 unsigned LoIdx = 0;
3706 unsigned HiIdx = NumElems/2;
3707 for (unsigned i = 0; i != NumElems; ++i) {
3708 if (i == NumElems/2) {
3709 MaskPtr = &HiMask;
3710 MaskIdx = 1;
3711 LoIdx = 0;
3712 HiIdx = NumElems/2;
3713 }
3714 SDOperand Elt = PermMask.getOperand(i);
3715 if (Elt.getOpcode() == ISD::UNDEF) {
3716 Locs[i] = std::make_pair(-1, -1);
3717 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3718 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3719 (*MaskPtr)[LoIdx] = Elt;
3720 LoIdx++;
3721 } else {
3722 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3723 (*MaskPtr)[HiIdx] = Elt;
3724 HiIdx++;
3725 }
3726 }
3727
3728 SDOperand LoShuffle =
3729 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3730 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3731 &LoMask[0], LoMask.size()));
3732 SDOperand HiShuffle =
3733 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3734 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3735 &HiMask[0], HiMask.size()));
3736 SmallVector<SDOperand, 8> MaskOps;
3737 for (unsigned i = 0; i != NumElems; ++i) {
3738 if (Locs[i].first == -1) {
3739 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3740 } else {
3741 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3742 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3743 }
3744 }
3745 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3746 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3747 &MaskOps[0], MaskOps.size()));
3748 }
3749
3750 return SDOperand();
3751}
3752
3753SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003754X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3755 SelectionDAG &DAG) {
3756 MVT::ValueType VT = Op.getValueType();
3757 if (MVT::getSizeInBits(VT) == 8) {
3758 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3759 Op.getOperand(0), Op.getOperand(1));
3760 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3761 DAG.getValueType(VT));
3762 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3763 } else if (MVT::getSizeInBits(VT) == 16) {
3764 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3765 Op.getOperand(0), Op.getOperand(1));
3766 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3767 DAG.getValueType(VT));
3768 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3769 }
3770 return SDOperand();
3771}
3772
3773
3774SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003775X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3776 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3777 return SDOperand();
3778
Nate Begemand77e59e2008-02-11 04:19:36 +00003779 if (Subtarget->hasSSE41())
3780 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3781
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782 MVT::ValueType VT = Op.getValueType();
3783 // TODO: handle v16i8.
3784 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003785 SDOperand Vec = Op.getOperand(0);
3786 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3787 if (Idx == 0)
3788 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3789 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3790 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3791 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003792 // Transform it so it match pextrw which produces a 32-bit result.
3793 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3794 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3795 Op.getOperand(0), Op.getOperand(1));
3796 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3797 DAG.getValueType(VT));
3798 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3799 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003800 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3801 if (Idx == 0)
3802 return Op;
3803 // SHUFPS the element to the lowest double word, then movss.
3804 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3805 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003806 IdxVec.
3807 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3808 IdxVec.
3809 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3810 IdxVec.
3811 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3812 IdxVec.
3813 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003814 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3815 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003816 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003817 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3818 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3819 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003820 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003822 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3823 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3824 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3826 if (Idx == 0)
3827 return Op;
3828
3829 // UNPCKHPD the element to the lowest double word, then movsd.
3830 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3831 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3832 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3833 SmallVector<SDOperand, 8> IdxVec;
3834 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003835 IdxVec.
3836 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003837 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3838 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003839 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003840 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3841 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3842 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003843 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 }
3845
3846 return SDOperand();
3847}
3848
3849SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003850X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3851 MVT::ValueType VT = Op.getValueType();
3852 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3853
3854 SDOperand N0 = Op.getOperand(0);
3855 SDOperand N1 = Op.getOperand(1);
3856 SDOperand N2 = Op.getOperand(2);
3857
3858 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3859 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3860 : X86ISD::PINSRW;
3861 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3862 // argument.
3863 if (N1.getValueType() != MVT::i32)
3864 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3865 if (N2.getValueType() != MVT::i32)
3866 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3867 return DAG.getNode(Opc, VT, N0, N1, N2);
3868 } else if (EVT == MVT::f32) {
3869 // Bits [7:6] of the constant are the source select. This will always be
3870 // zero here. The DAG Combiner may combine an extract_elt index into these
3871 // bits. For example (insert (extract, 3), 2) could be matched by putting
3872 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3873 // Bits [5:4] of the constant are the destination select. This is the
3874 // value of the incoming immediate.
3875 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3876 // combine either bitwise AND or insert of float 0.0 to set these bits.
3877 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3878 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3879 }
3880 return SDOperand();
3881}
3882
3883SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003884X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003886 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003887
3888 if (Subtarget->hasSSE41())
3889 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3890
Evan Chenge12a7eb2007-12-12 07:55:34 +00003891 if (EVT == MVT::i8)
3892 return SDOperand();
3893
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003894 SDOperand N0 = Op.getOperand(0);
3895 SDOperand N1 = Op.getOperand(1);
3896 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003897
3898 if (MVT::getSizeInBits(EVT) == 16) {
3899 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3900 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901 if (N1.getValueType() != MVT::i32)
3902 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3903 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003904 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003907 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908}
3909
3910SDOperand
3911X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3912 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003913 MVT::ValueType VT = MVT::v2i32;
3914 switch (Op.getValueType()) {
3915 default: break;
3916 case MVT::v16i8:
3917 case MVT::v8i16:
3918 VT = MVT::v4i32;
3919 break;
3920 }
3921 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3922 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923}
3924
3925// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3926// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3927// one of the above mentioned nodes. It has to be wrapped because otherwise
3928// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3929// be used to form addressing mode. These wrapped nodes will be selected
3930// into MOV32ri.
3931SDOperand
3932X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3933 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3934 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3935 getPointerTy(),
3936 CP->getAlignment());
3937 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3938 // With PIC, the address is actually $g + Offset.
3939 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3940 !Subtarget->isPICStyleRIPRel()) {
3941 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3942 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3943 Result);
3944 }
3945
3946 return Result;
3947}
3948
3949SDOperand
3950X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3951 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3952 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003953 // If it's a debug information descriptor, don't mess with it.
3954 if (DAG.isVerifiedDebugInfoDesc(Op))
3955 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003956 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3957 // With PIC, the address is actually $g + Offset.
3958 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3959 !Subtarget->isPICStyleRIPRel()) {
3960 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3961 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3962 Result);
3963 }
3964
3965 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3966 // load the value at address GV, not the value of GV itself. This means that
3967 // the GlobalAddress must be in the base or index register of the address, not
3968 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3969 // The same applies for external symbols during PIC codegen
3970 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003971 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003972 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973
3974 return Result;
3975}
3976
3977// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3978static SDOperand
3979LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3980 const MVT::ValueType PtrVT) {
3981 SDOperand InFlag;
3982 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3983 DAG.getNode(X86ISD::GlobalBaseReg,
3984 PtrVT), InFlag);
3985 InFlag = Chain.getValue(1);
3986
3987 // emit leal symbol@TLSGD(,%ebx,1), %eax
3988 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3989 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3990 GA->getValueType(0),
3991 GA->getOffset());
3992 SDOperand Ops[] = { Chain, TGA, InFlag };
3993 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3994 InFlag = Result.getValue(2);
3995 Chain = Result.getValue(1);
3996
3997 // call ___tls_get_addr. This function receives its argument in
3998 // the register EAX.
3999 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4000 InFlag = Chain.getValue(1);
4001
4002 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4003 SDOperand Ops1[] = { Chain,
4004 DAG.getTargetExternalSymbol("___tls_get_addr",
4005 PtrVT),
4006 DAG.getRegister(X86::EAX, PtrVT),
4007 DAG.getRegister(X86::EBX, PtrVT),
4008 InFlag };
4009 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4010 InFlag = Chain.getValue(1);
4011
4012 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4013}
4014
4015// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4016// "local exec" model.
4017static SDOperand
4018LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4019 const MVT::ValueType PtrVT) {
4020 // Get the Thread Pointer
4021 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4022 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4023 // exec)
4024 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4025 GA->getValueType(0),
4026 GA->getOffset());
4027 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4028
4029 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004030 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004031 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004032
4033 // The address of the thread local variable is the add of the thread
4034 // pointer with the offset of the variable.
4035 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4036}
4037
4038SDOperand
4039X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4040 // TODO: implement the "local dynamic" model
4041 // TODO: implement the "initial exec"model for pic executables
4042 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4043 "TLS not implemented for non-ELF and 64-bit targets");
4044 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4045 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4046 // otherwise use the "Local Exec"TLS Model
4047 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4048 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4049 else
4050 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4051}
4052
4053SDOperand
4054X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4055 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4056 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4057 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4058 // With PIC, the address is actually $g + Offset.
4059 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4060 !Subtarget->isPICStyleRIPRel()) {
4061 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4062 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4063 Result);
4064 }
4065
4066 return Result;
4067}
4068
4069SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4070 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4071 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4072 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4073 // With PIC, the address is actually $g + Offset.
4074 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4075 !Subtarget->isPICStyleRIPRel()) {
4076 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4077 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4078 Result);
4079 }
4080
4081 return Result;
4082}
4083
Chris Lattner62814a32007-10-17 06:02:13 +00004084/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4085/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004086SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00004087 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4088 "Not an i64 shift!");
4089 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4090 SDOperand ShOpLo = Op.getOperand(0);
4091 SDOperand ShOpHi = Op.getOperand(1);
4092 SDOperand ShAmt = Op.getOperand(2);
4093 SDOperand Tmp1 = isSRA ?
4094 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4095 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004096
Chris Lattner62814a32007-10-17 06:02:13 +00004097 SDOperand Tmp2, Tmp3;
4098 if (Op.getOpcode() == ISD::SHL_PARTS) {
4099 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4100 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4101 } else {
4102 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4103 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4104 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004105
Chris Lattner62814a32007-10-17 06:02:13 +00004106 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4107 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4108 DAG.getConstant(32, MVT::i8));
4109 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4110 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111
Chris Lattner62814a32007-10-17 06:02:13 +00004112 SDOperand Hi, Lo;
4113 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4114 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4115 SmallVector<SDOperand, 4> Ops;
4116 if (Op.getOpcode() == ISD::SHL_PARTS) {
4117 Ops.push_back(Tmp2);
4118 Ops.push_back(Tmp3);
4119 Ops.push_back(CC);
4120 Ops.push_back(Cond);
4121 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004122
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004123 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004124 Ops.push_back(Tmp3);
4125 Ops.push_back(Tmp1);
4126 Ops.push_back(CC);
4127 Ops.push_back(Cond);
4128 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4129 } else {
4130 Ops.push_back(Tmp2);
4131 Ops.push_back(Tmp3);
4132 Ops.push_back(CC);
4133 Ops.push_back(Cond);
4134 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4135
4136 Ops.clear();
4137 Ops.push_back(Tmp3);
4138 Ops.push_back(Tmp1);
4139 Ops.push_back(CC);
4140 Ops.push_back(Cond);
4141 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4142 }
4143
4144 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4145 Ops.clear();
4146 Ops.push_back(Lo);
4147 Ops.push_back(Hi);
4148 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149}
4150
4151SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004153 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4154 "Unknown SINT_TO_FP to lower!");
4155
4156 // These are really Legal; caller falls through into that case.
4157 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4158 return SDOperand();
4159 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4160 Subtarget->is64Bit())
4161 return SDOperand();
4162
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004163 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4164 MachineFunction &MF = DAG.getMachineFunction();
4165 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4166 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4167 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004168 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004169 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004170 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171
4172 // Build the FILD
4173 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004174 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004175 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4177 else
4178 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4179 SmallVector<SDOperand, 8> Ops;
4180 Ops.push_back(Chain);
4181 Ops.push_back(StackSlot);
4182 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004183 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4184 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185
Dale Johannesen2fc20782007-09-14 22:26:36 +00004186 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004187 Chain = Result.getValue(1);
4188 SDOperand InFlag = Result.getValue(2);
4189
4190 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4191 // shouldn't be necessary except that RFP cannot be live across
4192 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4193 MachineFunction &MF = DAG.getMachineFunction();
4194 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4195 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4196 Tys = DAG.getVTList(MVT::Other);
4197 SmallVector<SDOperand, 8> Ops;
4198 Ops.push_back(Chain);
4199 Ops.push_back(Result);
4200 Ops.push_back(StackSlot);
4201 Ops.push_back(DAG.getValueType(Op.getValueType()));
4202 Ops.push_back(InFlag);
4203 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004204 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004205 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206 }
4207
4208 return Result;
4209}
4210
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004211std::pair<SDOperand,SDOperand> X86TargetLowering::
4212FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4214 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215
Dale Johannesen2fc20782007-09-14 22:26:36 +00004216 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004217 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004218 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004219 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004220 if (Subtarget->is64Bit() &&
4221 Op.getValueType() == MVT::i64 &&
4222 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004223 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004224
Evan Cheng05441e62007-10-15 20:11:21 +00004225 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4226 // stack slot.
4227 MachineFunction &MF = DAG.getMachineFunction();
4228 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4229 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4230 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004231 unsigned Opc;
4232 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004233 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4234 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4235 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4236 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 }
4238
4239 SDOperand Chain = DAG.getEntryNode();
4240 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004241 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004243 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004244 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4246 SDOperand Ops[] = {
4247 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4248 };
4249 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4250 Chain = Value.getValue(1);
4251 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4252 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4253 }
4254
4255 // Build the FP_TO_INT*_IN_MEM
4256 SDOperand Ops[] = { Chain, Value, StackSlot };
4257 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4258
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004259 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260}
4261
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004262SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004263 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4264 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4265 if (FIST.Val == 0) return SDOperand();
4266
4267 // Load the result.
4268 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4269}
4270
4271SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4272 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4273 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4274 if (FIST.Val == 0) return 0;
4275
4276 // Return an i64 load from the stack slot.
4277 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4278
4279 // Use a MERGE_VALUES node to drop the chain result value.
4280 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4281}
4282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004283SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4284 MVT::ValueType VT = Op.getValueType();
4285 MVT::ValueType EltVT = VT;
4286 if (MVT::isVector(VT))
4287 EltVT = MVT::getVectorElementType(VT);
4288 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4289 std::vector<Constant*> CV;
4290 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004291 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 CV.push_back(C);
4293 CV.push_back(C);
4294 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004295 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 CV.push_back(C);
4297 CV.push_back(C);
4298 CV.push_back(C);
4299 CV.push_back(C);
4300 }
Dan Gohman11821702007-07-27 17:16:43 +00004301 Constant *C = ConstantVector::get(CV);
4302 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004303 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004304 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004305 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004306 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4307}
4308
4309SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4310 MVT::ValueType VT = Op.getValueType();
4311 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004312 unsigned EltNum = 1;
4313 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004315 EltNum = MVT::getVectorNumElements(VT);
4316 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004317 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4318 std::vector<Constant*> CV;
4319 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004320 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004321 CV.push_back(C);
4322 CV.push_back(C);
4323 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004324 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325 CV.push_back(C);
4326 CV.push_back(C);
4327 CV.push_back(C);
4328 CV.push_back(C);
4329 }
Dan Gohman11821702007-07-27 17:16:43 +00004330 Constant *C = ConstantVector::get(CV);
4331 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004332 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004333 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004334 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004335 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004336 return DAG.getNode(ISD::BIT_CONVERT, VT,
4337 DAG.getNode(ISD::XOR, MVT::v2i64,
4338 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4339 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4340 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004341 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4342 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343}
4344
4345SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4346 SDOperand Op0 = Op.getOperand(0);
4347 SDOperand Op1 = Op.getOperand(1);
4348 MVT::ValueType VT = Op.getValueType();
4349 MVT::ValueType SrcVT = Op1.getValueType();
4350 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4351
4352 // If second operand is smaller, extend it first.
4353 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4354 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4355 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004356 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004358 // And if it is bigger, shrink it first.
4359 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004360 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004361 SrcVT = VT;
4362 SrcTy = MVT::getTypeForValueType(SrcVT);
4363 }
4364
4365 // At this point the operands and the result should have the same
4366 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004367
4368 // First get the sign bit of second operand.
4369 std::vector<Constant*> CV;
4370 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004371 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4372 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004374 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4375 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4376 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4377 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378 }
Dan Gohman11821702007-07-27 17:16:43 +00004379 Constant *C = ConstantVector::get(CV);
4380 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004381 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004382 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004383 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4385
4386 // Shift sign bit right or left if the two operands have different types.
4387 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4388 // Op0 is MVT::f32, Op1 is MVT::f64.
4389 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4390 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4391 DAG.getConstant(32, MVT::i32));
4392 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4393 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004394 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004395 }
4396
4397 // Clear first operand sign bit.
4398 CV.clear();
4399 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004400 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4401 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004403 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4404 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4405 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4406 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004407 }
Dan Gohman11821702007-07-27 17:16:43 +00004408 C = ConstantVector::get(CV);
4409 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004410 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004411 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004412 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4414
4415 // Or the value with the sign bit.
4416 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4417}
4418
Evan Cheng621216e2007-09-29 00:00:36 +00004419SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004420 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004421 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004422 SDOperand Op0 = Op.getOperand(0);
4423 SDOperand Op1 = Op.getOperand(1);
4424 SDOperand CC = Op.getOperand(2);
4425 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4426 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4427 unsigned X86CC;
4428
Evan Cheng950aac02007-09-25 01:57:46 +00004429 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004430 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004431 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4432 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004433 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004434 }
Evan Cheng950aac02007-09-25 01:57:46 +00004435
4436 assert(isFP && "Illegal integer SetCC!");
4437
Evan Cheng621216e2007-09-29 00:00:36 +00004438 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004439 switch (SetCCOpcode) {
4440 default: assert(false && "Illegal floating point SetCC!");
4441 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004442 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004443 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004444 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004445 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4446 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4447 }
4448 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004449 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004450 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004451 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004452 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4453 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4454 }
4455 }
4456}
4457
4458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004459SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4460 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004461 SDOperand Cond = Op.getOperand(0);
4462 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463
4464 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004465 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466
Evan Cheng50d37ab2007-10-08 22:16:29 +00004467 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4468 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004469 if (Cond.getOpcode() == X86ISD::SETCC) {
4470 CC = Cond.getOperand(0);
4471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004472 SDOperand Cmp = Cond.getOperand(1);
4473 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004474 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004475
Evan Cheng50d37ab2007-10-08 22:16:29 +00004476 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004477 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004478 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004479 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004480
Evan Cheng621216e2007-09-29 00:00:36 +00004481 if ((Opc == X86ISD::CMP ||
4482 Opc == X86ISD::COMI ||
4483 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004484 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004485 addTest = false;
4486 }
4487 }
4488
4489 if (addTest) {
4490 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004491 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004492 }
4493
4494 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4495 MVT::Flag);
4496 SmallVector<SDOperand, 4> Ops;
4497 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4498 // condition is true.
4499 Ops.push_back(Op.getOperand(2));
4500 Ops.push_back(Op.getOperand(1));
4501 Ops.push_back(CC);
4502 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004503 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004504}
4505
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004506SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4507 bool addTest = true;
4508 SDOperand Chain = Op.getOperand(0);
4509 SDOperand Cond = Op.getOperand(1);
4510 SDOperand Dest = Op.getOperand(2);
4511 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512
4513 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004514 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004515
Evan Cheng50d37ab2007-10-08 22:16:29 +00004516 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4517 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004518 if (Cond.getOpcode() == X86ISD::SETCC) {
4519 CC = Cond.getOperand(0);
4520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004521 SDOperand Cmp = Cond.getOperand(1);
4522 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004523 if (Opc == X86ISD::CMP ||
4524 Opc == X86ISD::COMI ||
4525 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004526 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004527 addTest = false;
4528 }
4529 }
4530
4531 if (addTest) {
4532 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004533 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004534 }
Evan Cheng621216e2007-09-29 00:00:36 +00004535 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004536 Chain, Op.getOperand(2), CC, Cond);
4537}
4538
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539
4540// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4541// Calls to _alloca is needed to probe the stack when allocating more than 4k
4542// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4543// that the guard pages used by the OS virtual memory manager are allocated in
4544// correct sequence.
4545SDOperand
4546X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4547 SelectionDAG &DAG) {
4548 assert(Subtarget->isTargetCygMing() &&
4549 "This should be used only on Cygwin/Mingw targets");
4550
4551 // Get the inputs.
4552 SDOperand Chain = Op.getOperand(0);
4553 SDOperand Size = Op.getOperand(1);
4554 // FIXME: Ensure alignment here
4555
4556 SDOperand Flag;
4557
4558 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004559 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560
4561 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4562 Flag = Chain.getValue(1);
4563
4564 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4565 SDOperand Ops[] = { Chain,
4566 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4567 DAG.getRegister(X86::EAX, IntPtr),
4568 Flag };
4569 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4570 Flag = Chain.getValue(1);
4571
4572 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4573
4574 std::vector<MVT::ValueType> Tys;
4575 Tys.push_back(SPTy);
4576 Tys.push_back(MVT::Other);
4577 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4578 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4579}
4580
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004581SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4582 SDOperand InFlag(0, 0);
4583 SDOperand Chain = Op.getOperand(0);
4584 unsigned Align =
4585 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4586 if (Align == 0) Align = 1;
4587
4588 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004589 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004590 // The libc version is likely to be faster for these cases. It can use the
4591 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004593 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004594 MVT::ValueType IntPtr = getPointerTy();
4595 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4596 TargetLowering::ArgListTy Args;
4597 TargetLowering::ArgListEntry Entry;
4598 Entry.Node = Op.getOperand(1);
4599 Entry.Ty = IntPtrTy;
4600 Args.push_back(Entry);
4601 // Extend the unsigned i8 argument to be an int value for the call.
4602 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4603 Entry.Ty = IntPtrTy;
4604 Args.push_back(Entry);
4605 Entry.Node = Op.getOperand(3);
4606 Args.push_back(Entry);
4607 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004608 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4609 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610 return CallResult.second;
4611 }
4612
4613 MVT::ValueType AVT;
4614 SDOperand Count;
4615 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4616 unsigned BytesLeft = 0;
4617 bool TwoRepStos = false;
4618 if (ValC) {
4619 unsigned ValReg;
4620 uint64_t Val = ValC->getValue() & 255;
4621
4622 // If the value is a constant, then we can potentially use larger sets.
4623 switch (Align & 3) {
4624 case 2: // WORD aligned
4625 AVT = MVT::i16;
4626 ValReg = X86::AX;
4627 Val = (Val << 8) | Val;
4628 break;
4629 case 0: // DWORD aligned
4630 AVT = MVT::i32;
4631 ValReg = X86::EAX;
4632 Val = (Val << 8) | Val;
4633 Val = (Val << 16) | Val;
4634 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4635 AVT = MVT::i64;
4636 ValReg = X86::RAX;
4637 Val = (Val << 32) | Val;
4638 }
4639 break;
4640 default: // Byte aligned
4641 AVT = MVT::i8;
4642 ValReg = X86::AL;
4643 Count = Op.getOperand(3);
4644 break;
4645 }
4646
4647 if (AVT > MVT::i8) {
4648 if (I) {
4649 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004650 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004651 BytesLeft = I->getValue() % UBytes;
4652 } else {
4653 assert(AVT >= MVT::i32 &&
4654 "Do not use rep;stos if not at least DWORD aligned");
4655 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4656 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4657 TwoRepStos = true;
4658 }
4659 }
4660
4661 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4662 InFlag);
4663 InFlag = Chain.getValue(1);
4664 } else {
4665 AVT = MVT::i8;
4666 Count = Op.getOperand(3);
4667 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4668 InFlag = Chain.getValue(1);
4669 }
4670
4671 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4672 Count, InFlag);
4673 InFlag = Chain.getValue(1);
4674 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4675 Op.getOperand(1), InFlag);
4676 InFlag = Chain.getValue(1);
4677
4678 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4679 SmallVector<SDOperand, 8> Ops;
4680 Ops.push_back(Chain);
4681 Ops.push_back(DAG.getValueType(AVT));
4682 Ops.push_back(InFlag);
4683 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4684
4685 if (TwoRepStos) {
4686 InFlag = Chain.getValue(1);
4687 Count = Op.getOperand(3);
4688 MVT::ValueType CVT = Count.getValueType();
4689 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4690 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4691 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4692 Left, InFlag);
4693 InFlag = Chain.getValue(1);
4694 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4695 Ops.clear();
4696 Ops.push_back(Chain);
4697 Ops.push_back(DAG.getValueType(MVT::i8));
4698 Ops.push_back(InFlag);
4699 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4700 } else if (BytesLeft) {
4701 // Issue stores for the last 1 - 7 bytes.
4702 SDOperand Value;
4703 unsigned Val = ValC->getValue() & 255;
4704 unsigned Offset = I->getValue() - BytesLeft;
4705 SDOperand DstAddr = Op.getOperand(1);
4706 MVT::ValueType AddrVT = DstAddr.getValueType();
4707 if (BytesLeft >= 4) {
4708 Val = (Val << 8) | Val;
4709 Val = (Val << 16) | Val;
4710 Value = DAG.getConstant(Val, MVT::i32);
4711 Chain = DAG.getStore(Chain, Value,
4712 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4713 DAG.getConstant(Offset, AddrVT)),
4714 NULL, 0);
4715 BytesLeft -= 4;
4716 Offset += 4;
4717 }
4718 if (BytesLeft >= 2) {
4719 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4720 Chain = DAG.getStore(Chain, Value,
4721 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4722 DAG.getConstant(Offset, AddrVT)),
4723 NULL, 0);
4724 BytesLeft -= 2;
4725 Offset += 2;
4726 }
4727 if (BytesLeft == 1) {
4728 Value = DAG.getConstant(Val, MVT::i8);
4729 Chain = DAG.getStore(Chain, Value,
4730 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4731 DAG.getConstant(Offset, AddrVT)),
4732 NULL, 0);
4733 }
4734 }
4735
4736 return Chain;
4737}
4738
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004739SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4740 SDOperand Dest,
4741 SDOperand Source,
4742 unsigned Size,
4743 unsigned Align,
4744 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004745 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004746 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747 switch (Align & 3) {
4748 case 2: // WORD aligned
4749 AVT = MVT::i16;
4750 break;
4751 case 0: // DWORD aligned
4752 AVT = MVT::i32;
4753 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4754 AVT = MVT::i64;
4755 break;
4756 default: // Byte aligned
4757 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004758 break;
4759 }
4760
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004761 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004762 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004763 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004764
4765 SDOperand InFlag(0, 0);
4766 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4767 Count, InFlag);
4768 InFlag = Chain.getValue(1);
4769 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004770 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771 InFlag = Chain.getValue(1);
4772 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004773 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774 InFlag = Chain.getValue(1);
4775
4776 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4777 SmallVector<SDOperand, 8> Ops;
4778 Ops.push_back(Chain);
4779 Ops.push_back(DAG.getValueType(AVT));
4780 Ops.push_back(InFlag);
4781 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4782
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004783 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004785 unsigned Offset = Size - BytesLeft;
4786 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004788 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004789 MVT::ValueType SrcVT = SrcAddr.getValueType();
4790 SDOperand Value;
4791 if (BytesLeft >= 4) {
4792 Value = DAG.getLoad(MVT::i32, Chain,
4793 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4794 DAG.getConstant(Offset, SrcVT)),
4795 NULL, 0);
4796 Chain = Value.getValue(1);
4797 Chain = DAG.getStore(Chain, Value,
4798 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4799 DAG.getConstant(Offset, DstVT)),
4800 NULL, 0);
4801 BytesLeft -= 4;
4802 Offset += 4;
4803 }
4804 if (BytesLeft >= 2) {
4805 Value = DAG.getLoad(MVT::i16, 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 BytesLeft -= 2;
4815 Offset += 2;
4816 }
4817
4818 if (BytesLeft == 1) {
4819 Value = DAG.getLoad(MVT::i8, Chain,
4820 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4821 DAG.getConstant(Offset, SrcVT)),
4822 NULL, 0);
4823 Chain = Value.getValue(1);
4824 Chain = DAG.getStore(Chain, Value,
4825 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4826 DAG.getConstant(Offset, DstVT)),
4827 NULL, 0);
4828 }
4829 }
4830
4831 return Chain;
4832}
4833
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004834/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4835SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004837 SDOperand TheChain = N->getOperand(0);
4838 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004839 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004840 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4841 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4842 MVT::i64, rax.getValue(2));
4843 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004844 DAG.getConstant(32, MVT::i8));
4845 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004846 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847 };
4848
4849 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004850 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851 }
4852
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004853 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4854 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4855 MVT::i32, eax.getValue(2));
4856 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4857 SDOperand Ops[] = { eax, edx };
4858 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4859
4860 // Use a MERGE_VALUES to return the value and chain.
4861 Ops[1] = edx.getValue(1);
4862 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4863 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864}
4865
4866SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004867 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868
4869 if (!Subtarget->is64Bit()) {
4870 // vastart just stores the address of the VarArgsFrameIndex slot into the
4871 // memory location argument.
4872 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004873 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004874 }
4875
4876 // __va_list_tag:
4877 // gp_offset (0 - 6 * 8)
4878 // fp_offset (48 - 48 + 8 * 16)
4879 // overflow_arg_area (point to parameters coming in memory).
4880 // reg_save_area
4881 SmallVector<SDOperand, 8> MemOps;
4882 SDOperand FIN = Op.getOperand(1);
4883 // Store gp_offset
4884 SDOperand Store = DAG.getStore(Op.getOperand(0),
4885 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004886 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004887 MemOps.push_back(Store);
4888
4889 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004890 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004891 Store = DAG.getStore(Op.getOperand(0),
4892 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004893 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894 MemOps.push_back(Store);
4895
4896 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004897 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004898 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004899 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004900 MemOps.push_back(Store);
4901
4902 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004903 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004904 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004905 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004906 MemOps.push_back(Store);
4907 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4908}
4909
4910SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4911 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4912 SDOperand Chain = Op.getOperand(0);
4913 SDOperand DstPtr = Op.getOperand(1);
4914 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004915 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4916 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004917
Dan Gohman12a9c082008-02-06 22:27:42 +00004918 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004919 Chain = SrcPtr.getValue(1);
4920 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004921 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004923 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004924 if (i == 2)
4925 break;
4926 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004927 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004928 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004929 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004930 }
4931 return Chain;
4932}
4933
4934SDOperand
4935X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4936 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4937 switch (IntNo) {
4938 default: return SDOperand(); // Don't custom lower most intrinsics.
4939 // Comparison intrinsics.
4940 case Intrinsic::x86_sse_comieq_ss:
4941 case Intrinsic::x86_sse_comilt_ss:
4942 case Intrinsic::x86_sse_comile_ss:
4943 case Intrinsic::x86_sse_comigt_ss:
4944 case Intrinsic::x86_sse_comige_ss:
4945 case Intrinsic::x86_sse_comineq_ss:
4946 case Intrinsic::x86_sse_ucomieq_ss:
4947 case Intrinsic::x86_sse_ucomilt_ss:
4948 case Intrinsic::x86_sse_ucomile_ss:
4949 case Intrinsic::x86_sse_ucomigt_ss:
4950 case Intrinsic::x86_sse_ucomige_ss:
4951 case Intrinsic::x86_sse_ucomineq_ss:
4952 case Intrinsic::x86_sse2_comieq_sd:
4953 case Intrinsic::x86_sse2_comilt_sd:
4954 case Intrinsic::x86_sse2_comile_sd:
4955 case Intrinsic::x86_sse2_comigt_sd:
4956 case Intrinsic::x86_sse2_comige_sd:
4957 case Intrinsic::x86_sse2_comineq_sd:
4958 case Intrinsic::x86_sse2_ucomieq_sd:
4959 case Intrinsic::x86_sse2_ucomilt_sd:
4960 case Intrinsic::x86_sse2_ucomile_sd:
4961 case Intrinsic::x86_sse2_ucomigt_sd:
4962 case Intrinsic::x86_sse2_ucomige_sd:
4963 case Intrinsic::x86_sse2_ucomineq_sd: {
4964 unsigned Opc = 0;
4965 ISD::CondCode CC = ISD::SETCC_INVALID;
4966 switch (IntNo) {
4967 default: break;
4968 case Intrinsic::x86_sse_comieq_ss:
4969 case Intrinsic::x86_sse2_comieq_sd:
4970 Opc = X86ISD::COMI;
4971 CC = ISD::SETEQ;
4972 break;
4973 case Intrinsic::x86_sse_comilt_ss:
4974 case Intrinsic::x86_sse2_comilt_sd:
4975 Opc = X86ISD::COMI;
4976 CC = ISD::SETLT;
4977 break;
4978 case Intrinsic::x86_sse_comile_ss:
4979 case Intrinsic::x86_sse2_comile_sd:
4980 Opc = X86ISD::COMI;
4981 CC = ISD::SETLE;
4982 break;
4983 case Intrinsic::x86_sse_comigt_ss:
4984 case Intrinsic::x86_sse2_comigt_sd:
4985 Opc = X86ISD::COMI;
4986 CC = ISD::SETGT;
4987 break;
4988 case Intrinsic::x86_sse_comige_ss:
4989 case Intrinsic::x86_sse2_comige_sd:
4990 Opc = X86ISD::COMI;
4991 CC = ISD::SETGE;
4992 break;
4993 case Intrinsic::x86_sse_comineq_ss:
4994 case Intrinsic::x86_sse2_comineq_sd:
4995 Opc = X86ISD::COMI;
4996 CC = ISD::SETNE;
4997 break;
4998 case Intrinsic::x86_sse_ucomieq_ss:
4999 case Intrinsic::x86_sse2_ucomieq_sd:
5000 Opc = X86ISD::UCOMI;
5001 CC = ISD::SETEQ;
5002 break;
5003 case Intrinsic::x86_sse_ucomilt_ss:
5004 case Intrinsic::x86_sse2_ucomilt_sd:
5005 Opc = X86ISD::UCOMI;
5006 CC = ISD::SETLT;
5007 break;
5008 case Intrinsic::x86_sse_ucomile_ss:
5009 case Intrinsic::x86_sse2_ucomile_sd:
5010 Opc = X86ISD::UCOMI;
5011 CC = ISD::SETLE;
5012 break;
5013 case Intrinsic::x86_sse_ucomigt_ss:
5014 case Intrinsic::x86_sse2_ucomigt_sd:
5015 Opc = X86ISD::UCOMI;
5016 CC = ISD::SETGT;
5017 break;
5018 case Intrinsic::x86_sse_ucomige_ss:
5019 case Intrinsic::x86_sse2_ucomige_sd:
5020 Opc = X86ISD::UCOMI;
5021 CC = ISD::SETGE;
5022 break;
5023 case Intrinsic::x86_sse_ucomineq_ss:
5024 case Intrinsic::x86_sse2_ucomineq_sd:
5025 Opc = X86ISD::UCOMI;
5026 CC = ISD::SETNE;
5027 break;
5028 }
5029
5030 unsigned X86CC;
5031 SDOperand LHS = Op.getOperand(1);
5032 SDOperand RHS = Op.getOperand(2);
5033 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5034
Evan Cheng621216e2007-09-29 00:00:36 +00005035 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5036 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5037 DAG.getConstant(X86CC, MVT::i8), Cond);
5038 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005039 }
5040 }
5041}
5042
5043SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5044 // Depths > 0 not supported yet!
5045 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5046 return SDOperand();
5047
5048 // Just load the return address
5049 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5050 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5051}
5052
5053SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5054 // Depths > 0 not supported yet!
5055 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5056 return SDOperand();
5057
5058 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5059 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005060 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061}
5062
5063SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5064 SelectionDAG &DAG) {
5065 // Is not yet supported on x86-64
5066 if (Subtarget->is64Bit())
5067 return SDOperand();
5068
Chris Lattner5872a362008-01-17 07:00:52 +00005069 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005070}
5071
5072SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5073{
5074 assert(!Subtarget->is64Bit() &&
5075 "Lowering of eh_return builtin is not supported yet on x86-64");
5076
5077 MachineFunction &MF = DAG.getMachineFunction();
5078 SDOperand Chain = Op.getOperand(0);
5079 SDOperand Offset = Op.getOperand(1);
5080 SDOperand Handler = Op.getOperand(2);
5081
5082 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5083 getPointerTy());
5084
5085 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005086 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5088 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5089 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005090 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005091
5092 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5093 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5094}
5095
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005096SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5097 SelectionDAG &DAG) {
5098 SDOperand Root = Op.getOperand(0);
5099 SDOperand Trmp = Op.getOperand(1); // trampoline
5100 SDOperand FPtr = Op.getOperand(2); // nested function
5101 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5102
Dan Gohman12a9c082008-02-06 22:27:42 +00005103 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005104
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005105 const X86InstrInfo *TII =
5106 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5107
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005108 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005109 SDOperand OutChains[6];
5110
5111 // Large code-model.
5112
5113 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5114 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5115
5116 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005117 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005118 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005119 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005120
5121 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5122
5123 // Load the pointer to the nested function into R11.
5124 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5125 SDOperand Addr = Trmp;
5126 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005127 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005128
5129 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005130 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005131
5132 // Load the 'nest' parameter value into R10.
5133 // R10 is specified in X86CallingConv.td
5134 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5135 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5136 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005137 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005138
5139 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005140 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005141
5142 // Jump to the nested function.
5143 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5144 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5145 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005146 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005147
5148 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5149 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5150 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005151 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005152
5153 SDOperand Ops[] =
5154 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5155 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005156 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005157 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005158 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5159 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005160 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005161
5162 switch (CC) {
5163 default:
5164 assert(0 && "Unsupported calling convention");
5165 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005166 case CallingConv::X86_StdCall: {
5167 // Pass 'nest' parameter in ECX.
5168 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005169 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005170
5171 // Check that ECX wasn't needed by an 'inreg' parameter.
5172 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005173 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005174
5175 if (Attrs && !Func->isVarArg()) {
5176 unsigned InRegCount = 0;
5177 unsigned Idx = 1;
5178
5179 for (FunctionType::param_iterator I = FTy->param_begin(),
5180 E = FTy->param_end(); I != E; ++I, ++Idx)
5181 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5182 // FIXME: should only count parameters that are lowered to integers.
5183 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5184
5185 if (InRegCount > 2) {
5186 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5187 abort();
5188 }
5189 }
5190 break;
5191 }
5192 case CallingConv::X86_FastCall:
5193 // Pass 'nest' parameter in EAX.
5194 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005195 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005196 break;
5197 }
5198
5199 SDOperand OutChains[4];
5200 SDOperand Addr, Disp;
5201
5202 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5203 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5204
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005205 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5206 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005207 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005208 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005209 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005210
5211 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005212 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005213
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005214 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005215 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5216 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005217 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005218
5219 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005220 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005221
Duncan Sands7407a9f2007-09-11 14:10:23 +00005222 SDOperand Ops[] =
5223 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5224 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005225 }
5226}
5227
Dan Gohman819574c2008-01-31 00:41:03 +00005228SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005229 /*
5230 The rounding mode is in bits 11:10 of FPSR, and has the following
5231 settings:
5232 00 Round to nearest
5233 01 Round to -inf
5234 10 Round to +inf
5235 11 Round to 0
5236
5237 FLT_ROUNDS, on the other hand, expects the following:
5238 -1 Undefined
5239 0 Round to 0
5240 1 Round to nearest
5241 2 Round to +inf
5242 3 Round to -inf
5243
5244 To perform the conversion, we do:
5245 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5246 */
5247
5248 MachineFunction &MF = DAG.getMachineFunction();
5249 const TargetMachine &TM = MF.getTarget();
5250 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5251 unsigned StackAlignment = TFI.getStackAlignment();
5252 MVT::ValueType VT = Op.getValueType();
5253
5254 // Save FP Control Word to stack slot
5255 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5256 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5257
5258 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5259 DAG.getEntryNode(), StackSlot);
5260
5261 // Load FP Control Word from stack slot
5262 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5263
5264 // Transform as necessary
5265 SDOperand CWD1 =
5266 DAG.getNode(ISD::SRL, MVT::i16,
5267 DAG.getNode(ISD::AND, MVT::i16,
5268 CWD, DAG.getConstant(0x800, MVT::i16)),
5269 DAG.getConstant(11, MVT::i8));
5270 SDOperand CWD2 =
5271 DAG.getNode(ISD::SRL, MVT::i16,
5272 DAG.getNode(ISD::AND, MVT::i16,
5273 CWD, DAG.getConstant(0x400, MVT::i16)),
5274 DAG.getConstant(9, MVT::i8));
5275
5276 SDOperand RetVal =
5277 DAG.getNode(ISD::AND, MVT::i16,
5278 DAG.getNode(ISD::ADD, MVT::i16,
5279 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5280 DAG.getConstant(1, MVT::i16)),
5281 DAG.getConstant(3, MVT::i16));
5282
5283
5284 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5285 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5286}
5287
Evan Cheng48679f42007-12-14 02:13:44 +00005288SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5289 MVT::ValueType VT = Op.getValueType();
5290 MVT::ValueType OpVT = VT;
5291 unsigned NumBits = MVT::getSizeInBits(VT);
5292
5293 Op = Op.getOperand(0);
5294 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005295 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005296 OpVT = MVT::i32;
5297 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5298 }
Evan Cheng48679f42007-12-14 02:13:44 +00005299
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005300 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5301 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5302 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5303
5304 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5305 SmallVector<SDOperand, 4> Ops;
5306 Ops.push_back(Op);
5307 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5308 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5309 Ops.push_back(Op.getValue(1));
5310 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5311
5312 // Finally xor with NumBits-1.
5313 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5314
Evan Cheng48679f42007-12-14 02:13:44 +00005315 if (VT == MVT::i8)
5316 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5317 return Op;
5318}
5319
5320SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5321 MVT::ValueType VT = Op.getValueType();
5322 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005323 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005324
5325 Op = Op.getOperand(0);
5326 if (VT == MVT::i8) {
5327 OpVT = MVT::i32;
5328 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5329 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005330
5331 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5332 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5333 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5334
5335 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5336 SmallVector<SDOperand, 4> Ops;
5337 Ops.push_back(Op);
5338 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5339 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5340 Ops.push_back(Op.getValue(1));
5341 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5342
Evan Cheng48679f42007-12-14 02:13:44 +00005343 if (VT == MVT::i8)
5344 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5345 return Op;
5346}
5347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005348/// LowerOperation - Provide custom lowering hooks for some operations.
5349///
5350SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5351 switch (Op.getOpcode()) {
5352 default: assert(0 && "Should not custom lower this!");
5353 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5354 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5355 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5356 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5357 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5358 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5359 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5360 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5361 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5362 case ISD::SHL_PARTS:
5363 case ISD::SRA_PARTS:
5364 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5365 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5366 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5367 case ISD::FABS: return LowerFABS(Op, DAG);
5368 case ISD::FNEG: return LowerFNEG(Op, DAG);
5369 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005370 case ISD::SETCC: return LowerSETCC(Op, DAG);
5371 case ISD::SELECT: return LowerSELECT(Op, DAG);
5372 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005373 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5374 case ISD::CALL: return LowerCALL(Op, DAG);
5375 case ISD::RET: return LowerRET(Op, DAG);
5376 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5377 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5378 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379 case ISD::VASTART: return LowerVASTART(Op, DAG);
5380 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5381 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5382 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5383 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5384 case ISD::FRAME_TO_ARGS_OFFSET:
5385 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5386 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5387 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005388 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005389 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005390 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5391 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005392
5393 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5394 case ISD::READCYCLECOUNTER:
5395 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005396 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005397}
5398
5399/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5400SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5401 switch (N->getOpcode()) {
5402 default: assert(0 && "Should not custom lower this!");
5403 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5404 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5405 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005406}
5407
5408const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5409 switch (Opcode) {
5410 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005411 case X86ISD::BSF: return "X86ISD::BSF";
5412 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005413 case X86ISD::SHLD: return "X86ISD::SHLD";
5414 case X86ISD::SHRD: return "X86ISD::SHRD";
5415 case X86ISD::FAND: return "X86ISD::FAND";
5416 case X86ISD::FOR: return "X86ISD::FOR";
5417 case X86ISD::FXOR: return "X86ISD::FXOR";
5418 case X86ISD::FSRL: return "X86ISD::FSRL";
5419 case X86ISD::FILD: return "X86ISD::FILD";
5420 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5421 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5422 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5423 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5424 case X86ISD::FLD: return "X86ISD::FLD";
5425 case X86ISD::FST: return "X86ISD::FST";
5426 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng931a8f42008-01-29 19:34:22 +00005427 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005428 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5429 case X86ISD::CALL: return "X86ISD::CALL";
5430 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5431 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5432 case X86ISD::CMP: return "X86ISD::CMP";
5433 case X86ISD::COMI: return "X86ISD::COMI";
5434 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5435 case X86ISD::SETCC: return "X86ISD::SETCC";
5436 case X86ISD::CMOV: return "X86ISD::CMOV";
5437 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5438 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5439 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5440 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005441 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5442 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005443 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005444 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005445 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5446 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005447 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5448 case X86ISD::FMAX: return "X86ISD::FMAX";
5449 case X86ISD::FMIN: return "X86ISD::FMIN";
5450 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5451 case X86ISD::FRCP: return "X86ISD::FRCP";
5452 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5453 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5454 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005455 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005456 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005457 }
5458}
5459
5460// isLegalAddressingMode - Return true if the addressing mode represented
5461// by AM is legal for this target, for a load/store of the specified type.
5462bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5463 const Type *Ty) const {
5464 // X86 supports extremely general addressing modes.
5465
5466 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5467 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5468 return false;
5469
5470 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005471 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005472 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5473 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005474
5475 // X86-64 only supports addr of globals in small code model.
5476 if (Subtarget->is64Bit()) {
5477 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5478 return false;
5479 // If lower 4G is not available, then we must use rip-relative addressing.
5480 if (AM.BaseOffs || AM.Scale > 1)
5481 return false;
5482 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005483 }
5484
5485 switch (AM.Scale) {
5486 case 0:
5487 case 1:
5488 case 2:
5489 case 4:
5490 case 8:
5491 // These scales always work.
5492 break;
5493 case 3:
5494 case 5:
5495 case 9:
5496 // These scales are formed with basereg+scalereg. Only accept if there is
5497 // no basereg yet.
5498 if (AM.HasBaseReg)
5499 return false;
5500 break;
5501 default: // Other stuff never works.
5502 return false;
5503 }
5504
5505 return true;
5506}
5507
5508
Evan Cheng27a820a2007-10-26 01:56:11 +00005509bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5510 if (!Ty1->isInteger() || !Ty2->isInteger())
5511 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005512 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5513 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5514 if (NumBits1 <= NumBits2)
5515 return false;
5516 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005517}
5518
Evan Cheng9decb332007-10-29 19:58:20 +00005519bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5520 MVT::ValueType VT2) const {
5521 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5522 return false;
5523 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5524 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5525 if (NumBits1 <= NumBits2)
5526 return false;
5527 return Subtarget->is64Bit() || NumBits1 < 64;
5528}
Evan Cheng27a820a2007-10-26 01:56:11 +00005529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005530/// isShuffleMaskLegal - Targets can use this to indicate that they only
5531/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5532/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5533/// are assumed to be legal.
5534bool
5535X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5536 // Only do shuffles on 128-bit vector types for now.
5537 if (MVT::getSizeInBits(VT) == 64) return false;
5538 return (Mask.Val->getNumOperands() <= 4 ||
5539 isIdentityMask(Mask.Val) ||
5540 isIdentityMask(Mask.Val, true) ||
5541 isSplatMask(Mask.Val) ||
5542 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5543 X86::isUNPCKLMask(Mask.Val) ||
5544 X86::isUNPCKHMask(Mask.Val) ||
5545 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5546 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5547}
5548
5549bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5550 MVT::ValueType EVT,
5551 SelectionDAG &DAG) const {
5552 unsigned NumElts = BVOps.size();
5553 // Only do shuffles on 128-bit vector types for now.
5554 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5555 if (NumElts == 2) return true;
5556 if (NumElts == 4) {
5557 return (isMOVLMask(&BVOps[0], 4) ||
5558 isCommutedMOVL(&BVOps[0], 4, true) ||
5559 isSHUFPMask(&BVOps[0], 4) ||
5560 isCommutedSHUFP(&BVOps[0], 4));
5561 }
5562 return false;
5563}
5564
5565//===----------------------------------------------------------------------===//
5566// X86 Scheduler Hooks
5567//===----------------------------------------------------------------------===//
5568
5569MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005570X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5571 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005572 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5573 switch (MI->getOpcode()) {
5574 default: assert(false && "Unexpected instr type to insert");
5575 case X86::CMOV_FR32:
5576 case X86::CMOV_FR64:
5577 case X86::CMOV_V4F32:
5578 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005579 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005580 // To "insert" a SELECT_CC instruction, we actually have to insert the
5581 // diamond control-flow pattern. The incoming instruction knows the
5582 // destination vreg to set, the condition code register to branch on, the
5583 // true/false values to select between, and a branch opcode to use.
5584 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5585 ilist<MachineBasicBlock>::iterator It = BB;
5586 ++It;
5587
5588 // thisMBB:
5589 // ...
5590 // TrueVal = ...
5591 // cmpTY ccX, r1, r2
5592 // bCC copy1MBB
5593 // fallthrough --> copy0MBB
5594 MachineBasicBlock *thisMBB = BB;
5595 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5596 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5597 unsigned Opc =
5598 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5599 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5600 MachineFunction *F = BB->getParent();
5601 F->getBasicBlockList().insert(It, copy0MBB);
5602 F->getBasicBlockList().insert(It, sinkMBB);
5603 // Update machine-CFG edges by first adding all successors of the current
5604 // block to the new block which will contain the Phi node for the select.
5605 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5606 e = BB->succ_end(); i != e; ++i)
5607 sinkMBB->addSuccessor(*i);
5608 // Next, remove all successors of the current block, and add the true
5609 // and fallthrough blocks as its successors.
5610 while(!BB->succ_empty())
5611 BB->removeSuccessor(BB->succ_begin());
5612 BB->addSuccessor(copy0MBB);
5613 BB->addSuccessor(sinkMBB);
5614
5615 // copy0MBB:
5616 // %FalseValue = ...
5617 // # fallthrough to sinkMBB
5618 BB = copy0MBB;
5619
5620 // Update machine-CFG edges
5621 BB->addSuccessor(sinkMBB);
5622
5623 // sinkMBB:
5624 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5625 // ...
5626 BB = sinkMBB;
5627 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5628 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5629 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5630
5631 delete MI; // The pseudo instruction is gone now.
5632 return BB;
5633 }
5634
5635 case X86::FP32_TO_INT16_IN_MEM:
5636 case X86::FP32_TO_INT32_IN_MEM:
5637 case X86::FP32_TO_INT64_IN_MEM:
5638 case X86::FP64_TO_INT16_IN_MEM:
5639 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005640 case X86::FP64_TO_INT64_IN_MEM:
5641 case X86::FP80_TO_INT16_IN_MEM:
5642 case X86::FP80_TO_INT32_IN_MEM:
5643 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 // Change the floating point control register to use "round towards zero"
5645 // mode when truncating to an integer value.
5646 MachineFunction *F = BB->getParent();
5647 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5648 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5649
5650 // Load the old value of the high byte of the control word...
5651 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005652 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005653 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5654
5655 // Set the high part to be round to zero...
5656 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5657 .addImm(0xC7F);
5658
5659 // Reload the modified control word now...
5660 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5661
5662 // Restore the memory image of control word to original value
5663 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5664 .addReg(OldCW);
5665
5666 // Get the X86 opcode to use.
5667 unsigned Opc;
5668 switch (MI->getOpcode()) {
5669 default: assert(0 && "illegal opcode!");
5670 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5671 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5672 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5673 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5674 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5675 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005676 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5677 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5678 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005679 }
5680
5681 X86AddressMode AM;
5682 MachineOperand &Op = MI->getOperand(0);
5683 if (Op.isRegister()) {
5684 AM.BaseType = X86AddressMode::RegBase;
5685 AM.Base.Reg = Op.getReg();
5686 } else {
5687 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005688 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005689 }
5690 Op = MI->getOperand(1);
5691 if (Op.isImmediate())
5692 AM.Scale = Op.getImm();
5693 Op = MI->getOperand(2);
5694 if (Op.isImmediate())
5695 AM.IndexReg = Op.getImm();
5696 Op = MI->getOperand(3);
5697 if (Op.isGlobalAddress()) {
5698 AM.GV = Op.getGlobal();
5699 } else {
5700 AM.Disp = Op.getImm();
5701 }
5702 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5703 .addReg(MI->getOperand(4).getReg());
5704
5705 // Reload the original control word now.
5706 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5707
5708 delete MI; // The pseudo instruction is gone now.
5709 return BB;
5710 }
5711 }
5712}
5713
5714//===----------------------------------------------------------------------===//
5715// X86 Optimization Hooks
5716//===----------------------------------------------------------------------===//
5717
5718void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005719 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005720 APInt &KnownZero,
5721 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722 const SelectionDAG &DAG,
5723 unsigned Depth) const {
5724 unsigned Opc = Op.getOpcode();
5725 assert((Opc >= ISD::BUILTIN_OP_END ||
5726 Opc == ISD::INTRINSIC_WO_CHAIN ||
5727 Opc == ISD::INTRINSIC_W_CHAIN ||
5728 Opc == ISD::INTRINSIC_VOID) &&
5729 "Should use MaskedValueIsZero if you don't know whether Op"
5730 " is a target node!");
5731
Dan Gohman1d79e432008-02-13 23:07:24 +00005732 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005733 switch (Opc) {
5734 default: break;
5735 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005736 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5737 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005738 break;
5739 }
5740}
5741
5742/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5743/// element of the result of the vector shuffle.
5744static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5745 MVT::ValueType VT = N->getValueType(0);
5746 SDOperand PermMask = N->getOperand(2);
5747 unsigned NumElems = PermMask.getNumOperands();
5748 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5749 i %= NumElems;
5750 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5751 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005752 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005753 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5754 SDOperand Idx = PermMask.getOperand(i);
5755 if (Idx.getOpcode() == ISD::UNDEF)
5756 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5757 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5758 }
5759 return SDOperand();
5760}
5761
5762/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5763/// node is a GlobalAddress + an offset.
5764static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5765 unsigned Opc = N->getOpcode();
5766 if (Opc == X86ISD::Wrapper) {
5767 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5768 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5769 return true;
5770 }
5771 } else if (Opc == ISD::ADD) {
5772 SDOperand N1 = N->getOperand(0);
5773 SDOperand N2 = N->getOperand(1);
5774 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5775 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5776 if (V) {
5777 Offset += V->getSignExtended();
5778 return true;
5779 }
5780 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5781 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5782 if (V) {
5783 Offset += V->getSignExtended();
5784 return true;
5785 }
5786 }
5787 }
5788 return false;
5789}
5790
5791/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5792/// + Dist * Size.
5793static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5794 MachineFrameInfo *MFI) {
5795 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5796 return false;
5797
5798 SDOperand Loc = N->getOperand(1);
5799 SDOperand BaseLoc = Base->getOperand(1);
5800 if (Loc.getOpcode() == ISD::FrameIndex) {
5801 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5802 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005803 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5804 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005805 int FS = MFI->getObjectSize(FI);
5806 int BFS = MFI->getObjectSize(BFI);
5807 if (FS != BFS || FS != Size) return false;
5808 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5809 } else {
5810 GlobalValue *GV1 = NULL;
5811 GlobalValue *GV2 = NULL;
5812 int64_t Offset1 = 0;
5813 int64_t Offset2 = 0;
5814 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5815 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5816 if (isGA1 && isGA2 && GV1 == GV2)
5817 return Offset1 == (Offset2 + Dist*Size);
5818 }
5819
5820 return false;
5821}
5822
5823static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5824 const X86Subtarget *Subtarget) {
5825 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005826 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005827 if (isGAPlusOffset(Base, GV, Offset))
5828 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005829 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005830 return false;
5831}
5832
5833
5834/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5835/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5836/// if the load addresses are consecutive, non-overlapping, and in the right
5837/// order.
5838static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5839 const X86Subtarget *Subtarget) {
5840 MachineFunction &MF = DAG.getMachineFunction();
5841 MachineFrameInfo *MFI = MF.getFrameInfo();
5842 MVT::ValueType VT = N->getValueType(0);
5843 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5844 SDOperand PermMask = N->getOperand(2);
5845 int NumElems = (int)PermMask.getNumOperands();
5846 SDNode *Base = NULL;
5847 for (int i = 0; i < NumElems; ++i) {
5848 SDOperand Idx = PermMask.getOperand(i);
5849 if (Idx.getOpcode() == ISD::UNDEF) {
5850 if (!Base) return SDOperand();
5851 } else {
5852 SDOperand Arg =
5853 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5854 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5855 return SDOperand();
5856 if (!Base)
5857 Base = Arg.Val;
5858 else if (!isConsecutiveLoad(Arg.Val, Base,
5859 i, MVT::getSizeInBits(EVT)/8,MFI))
5860 return SDOperand();
5861 }
5862 }
5863
5864 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005865 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005866 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005867 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005868 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005869 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005870 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5871 LD->getSrcValueOffset(), LD->isVolatile(),
5872 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005873 }
5874}
5875
5876/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5877static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5878 const X86Subtarget *Subtarget) {
5879 SDOperand Cond = N->getOperand(0);
5880
5881 // If we have SSE[12] support, try to form min/max nodes.
5882 if (Subtarget->hasSSE2() &&
5883 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5884 if (Cond.getOpcode() == ISD::SETCC) {
5885 // Get the LHS/RHS of the select.
5886 SDOperand LHS = N->getOperand(1);
5887 SDOperand RHS = N->getOperand(2);
5888 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5889
5890 unsigned Opcode = 0;
5891 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5892 switch (CC) {
5893 default: break;
5894 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5895 case ISD::SETULE:
5896 case ISD::SETLE:
5897 if (!UnsafeFPMath) break;
5898 // FALL THROUGH.
5899 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5900 case ISD::SETLT:
5901 Opcode = X86ISD::FMIN;
5902 break;
5903
5904 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5905 case ISD::SETUGT:
5906 case ISD::SETGT:
5907 if (!UnsafeFPMath) break;
5908 // FALL THROUGH.
5909 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5910 case ISD::SETGE:
5911 Opcode = X86ISD::FMAX;
5912 break;
5913 }
5914 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5915 switch (CC) {
5916 default: break;
5917 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5918 case ISD::SETUGT:
5919 case ISD::SETGT:
5920 if (!UnsafeFPMath) break;
5921 // FALL THROUGH.
5922 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5923 case ISD::SETGE:
5924 Opcode = X86ISD::FMIN;
5925 break;
5926
5927 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5928 case ISD::SETULE:
5929 case ISD::SETLE:
5930 if (!UnsafeFPMath) break;
5931 // FALL THROUGH.
5932 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5933 case ISD::SETLT:
5934 Opcode = X86ISD::FMAX;
5935 break;
5936 }
5937 }
5938
5939 if (Opcode)
5940 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5941 }
5942
5943 }
5944
5945 return SDOperand();
5946}
5947
Chris Lattnerce84ae42008-02-22 02:09:43 +00005948/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
5949static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
5950 const X86Subtarget *Subtarget) {
5951 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
5952 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00005953 // A preferable solution to the general problem is to figure out the right
5954 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00005955 if (MVT::isVector(St->getValue().getValueType()) &&
5956 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00005957 isa<LoadSDNode>(St->getValue()) &&
5958 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
5959 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005960 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00005961 LoadSDNode *Ld = 0;
5962 int TokenFactorIndex = -1;
5963 SmallVector<SDOperand, 8> Ops;
5964 SDNode* ChainVal = St->getChain().Val;
5965 // Must be a store of a load. We currently handle two cases: the load
5966 // is a direct child, and it's under an intervening TokenFactor. It is
5967 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00005968 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00005969 Ld = cast<LoadSDNode>(St->getChain());
5970 else if (St->getValue().hasOneUse() &&
5971 ChainVal->getOpcode() == ISD::TokenFactor) {
5972 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005973 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00005974 TokenFactorIndex = i;
5975 Ld = cast<LoadSDNode>(St->getValue());
5976 } else
5977 Ops.push_back(ChainVal->getOperand(i));
5978 }
5979 }
5980 if (Ld) {
5981 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
5982 if (Subtarget->is64Bit()) {
5983 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
5984 Ld->getBasePtr(), Ld->getSrcValue(),
5985 Ld->getSrcValueOffset(), Ld->isVolatile(),
5986 Ld->getAlignment());
5987 SDOperand NewChain = NewLd.getValue(1);
5988 if (TokenFactorIndex != -1) {
5989 Ops.push_back(NewLd);
5990 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
5991 Ops.size());
5992 }
5993 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
5994 St->getSrcValue(), St->getSrcValueOffset(),
5995 St->isVolatile(), St->getAlignment());
5996 }
5997
5998 // Otherwise, lower to two 32-bit copies.
5999 SDOperand LoAddr = Ld->getBasePtr();
6000 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6001 DAG.getConstant(MVT::i32, 4));
6002
6003 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6004 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6005 Ld->isVolatile(), Ld->getAlignment());
6006 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6007 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6008 Ld->isVolatile(),
6009 MinAlign(Ld->getAlignment(), 4));
6010
6011 SDOperand NewChain = LoLd.getValue(1);
6012 if (TokenFactorIndex != -1) {
6013 Ops.push_back(LoLd);
6014 Ops.push_back(HiLd);
6015 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6016 Ops.size());
6017 }
6018
6019 LoAddr = St->getBasePtr();
6020 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6021 DAG.getConstant(MVT::i32, 4));
6022
6023 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006024 St->getSrcValue(), St->getSrcValueOffset(),
6025 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006026 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6027 St->getSrcValue(), St->getSrcValueOffset()+4,
6028 St->isVolatile(),
6029 MinAlign(St->getAlignment(), 4));
6030 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006031 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006032 }
6033 return SDOperand();
6034}
6035
Chris Lattner470d5dc2008-01-25 06:14:17 +00006036/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6037/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006038static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006039 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6040 // F[X]OR(0.0, x) -> x
6041 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006042 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6043 if (C->getValueAPF().isPosZero())
6044 return N->getOperand(1);
6045 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6046 if (C->getValueAPF().isPosZero())
6047 return N->getOperand(0);
6048 return SDOperand();
6049}
6050
6051/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6052static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6053 // FAND(0.0, x) -> 0.0
6054 // FAND(x, 0.0) -> 0.0
6055 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6056 if (C->getValueAPF().isPosZero())
6057 return N->getOperand(0);
6058 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6059 if (C->getValueAPF().isPosZero())
6060 return N->getOperand(1);
6061 return SDOperand();
6062}
6063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006064
6065SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6066 DAGCombinerInfo &DCI) const {
6067 SelectionDAG &DAG = DCI.DAG;
6068 switch (N->getOpcode()) {
6069 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006070 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6071 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006072 case ISD::STORE:
6073 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006074 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006075 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6076 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006077 }
6078
6079 return SDOperand();
6080}
6081
6082//===----------------------------------------------------------------------===//
6083// X86 Inline Assembly Support
6084//===----------------------------------------------------------------------===//
6085
6086/// getConstraintType - Given a constraint letter, return the type of
6087/// constraint it is for this target.
6088X86TargetLowering::ConstraintType
6089X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6090 if (Constraint.size() == 1) {
6091 switch (Constraint[0]) {
6092 case 'A':
6093 case 'r':
6094 case 'R':
6095 case 'l':
6096 case 'q':
6097 case 'Q':
6098 case 'x':
6099 case 'Y':
6100 return C_RegisterClass;
6101 default:
6102 break;
6103 }
6104 }
6105 return TargetLowering::getConstraintType(Constraint);
6106}
6107
Dale Johannesene99fc902008-01-29 02:21:21 +00006108/// LowerXConstraint - try to replace an X constraint, which matches anything,
6109/// with another that has more specific requirements based on the type of the
6110/// corresponding operand.
6111void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6112 std::string& s) const {
6113 if (MVT::isFloatingPoint(ConstraintVT)) {
6114 if (Subtarget->hasSSE2())
6115 s = "Y";
6116 else if (Subtarget->hasSSE1())
6117 s = "x";
6118 else
6119 s = "f";
6120 } else
6121 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6122}
6123
Chris Lattnera531abc2007-08-25 00:47:38 +00006124/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6125/// vector. If it is invalid, don't add anything to Ops.
6126void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6127 char Constraint,
6128 std::vector<SDOperand>&Ops,
6129 SelectionDAG &DAG) {
6130 SDOperand Result(0, 0);
6131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006132 switch (Constraint) {
6133 default: break;
6134 case 'I':
6135 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006136 if (C->getValue() <= 31) {
6137 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6138 break;
6139 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006140 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006141 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006142 case 'N':
6143 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006144 if (C->getValue() <= 255) {
6145 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6146 break;
6147 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006148 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006149 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006150 case 'i': {
6151 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006152 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6153 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6154 break;
6155 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006156
6157 // If we are in non-pic codegen mode, we allow the address of a global (with
6158 // an optional displacement) to be used with 'i'.
6159 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6160 int64_t Offset = 0;
6161
6162 // Match either (GA) or (GA+C)
6163 if (GA) {
6164 Offset = GA->getOffset();
6165 } else if (Op.getOpcode() == ISD::ADD) {
6166 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6167 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6168 if (C && GA) {
6169 Offset = GA->getOffset()+C->getValue();
6170 } else {
6171 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6172 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6173 if (C && GA)
6174 Offset = GA->getOffset()+C->getValue();
6175 else
6176 C = 0, GA = 0;
6177 }
6178 }
6179
6180 if (GA) {
6181 // If addressing this global requires a load (e.g. in PIC mode), we can't
6182 // match.
6183 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6184 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006185 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006186
6187 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6188 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006189 Result = Op;
6190 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006191 }
6192
6193 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006194 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006195 }
6196 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006197
6198 if (Result.Val) {
6199 Ops.push_back(Result);
6200 return;
6201 }
6202 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006203}
6204
6205std::vector<unsigned> X86TargetLowering::
6206getRegClassForInlineAsmConstraint(const std::string &Constraint,
6207 MVT::ValueType VT) const {
6208 if (Constraint.size() == 1) {
6209 // FIXME: not handling fp-stack yet!
6210 switch (Constraint[0]) { // GCC X86 Constraint Letters
6211 default: break; // Unknown constraint letter
6212 case 'A': // EAX/EDX
6213 if (VT == MVT::i32 || VT == MVT::i64)
6214 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6215 break;
6216 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6217 case 'Q': // Q_REGS
6218 if (VT == MVT::i32)
6219 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6220 else if (VT == MVT::i16)
6221 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6222 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006223 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006224 else if (VT == MVT::i64)
6225 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6226 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006227 }
6228 }
6229
6230 return std::vector<unsigned>();
6231}
6232
6233std::pair<unsigned, const TargetRegisterClass*>
6234X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6235 MVT::ValueType VT) const {
6236 // First, see if this is a constraint that directly corresponds to an LLVM
6237 // register class.
6238 if (Constraint.size() == 1) {
6239 // GCC Constraint Letters
6240 switch (Constraint[0]) {
6241 default: break;
6242 case 'r': // GENERAL_REGS
6243 case 'R': // LEGACY_REGS
6244 case 'l': // INDEX_REGS
6245 if (VT == MVT::i64 && Subtarget->is64Bit())
6246 return std::make_pair(0U, X86::GR64RegisterClass);
6247 if (VT == MVT::i32)
6248 return std::make_pair(0U, X86::GR32RegisterClass);
6249 else if (VT == MVT::i16)
6250 return std::make_pair(0U, X86::GR16RegisterClass);
6251 else if (VT == MVT::i8)
6252 return std::make_pair(0U, X86::GR8RegisterClass);
6253 break;
6254 case 'y': // MMX_REGS if MMX allowed.
6255 if (!Subtarget->hasMMX()) break;
6256 return std::make_pair(0U, X86::VR64RegisterClass);
6257 break;
6258 case 'Y': // SSE_REGS if SSE2 allowed
6259 if (!Subtarget->hasSSE2()) break;
6260 // FALL THROUGH.
6261 case 'x': // SSE_REGS if SSE1 allowed
6262 if (!Subtarget->hasSSE1()) break;
6263
6264 switch (VT) {
6265 default: break;
6266 // Scalar SSE types.
6267 case MVT::f32:
6268 case MVT::i32:
6269 return std::make_pair(0U, X86::FR32RegisterClass);
6270 case MVT::f64:
6271 case MVT::i64:
6272 return std::make_pair(0U, X86::FR64RegisterClass);
6273 // Vector types.
6274 case MVT::v16i8:
6275 case MVT::v8i16:
6276 case MVT::v4i32:
6277 case MVT::v2i64:
6278 case MVT::v4f32:
6279 case MVT::v2f64:
6280 return std::make_pair(0U, X86::VR128RegisterClass);
6281 }
6282 break;
6283 }
6284 }
6285
6286 // Use the default implementation in TargetLowering to convert the register
6287 // constraint into a member of a register class.
6288 std::pair<unsigned, const TargetRegisterClass*> Res;
6289 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6290
6291 // Not found as a standard register?
6292 if (Res.second == 0) {
6293 // GCC calls "st(0)" just plain "st".
6294 if (StringsEqualNoCase("{st}", Constraint)) {
6295 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006296 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006297 }
6298
6299 return Res;
6300 }
6301
6302 // Otherwise, check to see if this is a register class of the wrong value
6303 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6304 // turn into {ax},{dx}.
6305 if (Res.second->hasType(VT))
6306 return Res; // Correct type already, nothing to do.
6307
6308 // All of the single-register GCC register classes map their values onto
6309 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6310 // really want an 8-bit or 32-bit register, map to the appropriate register
6311 // class and return the appropriate register.
6312 if (Res.second != X86::GR16RegisterClass)
6313 return Res;
6314
6315 if (VT == MVT::i8) {
6316 unsigned DestReg = 0;
6317 switch (Res.first) {
6318 default: break;
6319 case X86::AX: DestReg = X86::AL; break;
6320 case X86::DX: DestReg = X86::DL; break;
6321 case X86::CX: DestReg = X86::CL; break;
6322 case X86::BX: DestReg = X86::BL; break;
6323 }
6324 if (DestReg) {
6325 Res.first = DestReg;
6326 Res.second = Res.second = X86::GR8RegisterClass;
6327 }
6328 } else if (VT == MVT::i32) {
6329 unsigned DestReg = 0;
6330 switch (Res.first) {
6331 default: break;
6332 case X86::AX: DestReg = X86::EAX; break;
6333 case X86::DX: DestReg = X86::EDX; break;
6334 case X86::CX: DestReg = X86::ECX; break;
6335 case X86::BX: DestReg = X86::EBX; break;
6336 case X86::SI: DestReg = X86::ESI; break;
6337 case X86::DI: DestReg = X86::EDI; break;
6338 case X86::BP: DestReg = X86::EBP; break;
6339 case X86::SP: DestReg = X86::ESP; break;
6340 }
6341 if (DestReg) {
6342 Res.first = DestReg;
6343 Res.second = Res.second = X86::GR32RegisterClass;
6344 }
6345 } else if (VT == MVT::i64) {
6346 unsigned DestReg = 0;
6347 switch (Res.first) {
6348 default: break;
6349 case X86::AX: DestReg = X86::RAX; break;
6350 case X86::DX: DestReg = X86::RDX; break;
6351 case X86::CX: DestReg = X86::RCX; break;
6352 case X86::BX: DestReg = X86::RBX; break;
6353 case X86::SI: DestReg = X86::RSI; break;
6354 case X86::DI: DestReg = X86::RDI; break;
6355 case X86::BP: DestReg = X86::RBP; break;
6356 case X86::SP: DestReg = X86::RSP; break;
6357 }
6358 if (DestReg) {
6359 Res.first = DestReg;
6360 Res.second = Res.second = X86::GR64RegisterClass;
6361 }
6362 }
6363
6364 return Res;
6365}