blob: 50014b31824a1a4c4b9e5c28bbc769d4e6fdea3b [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
29#include "llvm/CodeGen/CallingConvLower.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000040#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/StringExtras.h"
Dale Johannesen98738822008-02-22 22:17:59 +000042#include "llvm/ParamAttrsList.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043using namespace llvm;
44
45X86TargetLowering::X86TargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
47 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000048 X86ScalarSSEf64 = Subtarget->hasSSE2();
49 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000051
Chris Lattnerdec9cb52008-01-24 08:07:48 +000052 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 RegInfo = TM.getRegisterInfo();
55
56 // Set up the TargetLowering object.
57
58 // X86 is weird, it always uses i8 for shift amounts and setcc results.
59 setShiftAmountType(MVT::i8);
60 setSetCCResultType(MVT::i8);
61 setSetCCResultContents(ZeroOrOneSetCCResult);
62 setSchedulingPreference(SchedulingForRegPressure);
63 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
64 setStackPointerRegisterToSaveRestore(X86StackPtr);
65
66 if (Subtarget->isTargetDarwin()) {
67 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
68 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
70 } else if (Subtarget->isTargetMingw()) {
71 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
74 } else {
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
77 }
78
79 // Set up the register classes.
80 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
83 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
85
Duncan Sands082524c2008-01-23 20:39:46 +000086 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Chris Lattner3bc08502008-01-17 19:59:44 +000088 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 // operation.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
101
102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
105 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000106 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 else
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
111 }
112
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
117 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000118 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
125 }
126
Dale Johannesen958b08b2007-09-19 23:55:34 +0000127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 // this operation.
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000137 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } else {
142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
144 }
145
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 // conversion.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151
152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
155 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 else
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 }
165
166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000167 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
170 }
171
Dan Gohman8450d862008-02-18 19:34:53 +0000172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
176 //
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
211 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
212 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000219 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000222 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
223 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000225 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
226 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000228 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
229 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 if (Subtarget->is64Bit()) {
231 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000232 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
233 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 }
235
236 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
237 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
238
239 // These should be promoted to a larger select which is supported.
240 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
241 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
242 // X86 wants to expand cmov itself.
243 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
244 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
245 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000247 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
251 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000253 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 if (Subtarget->is64Bit()) {
255 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
256 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
257 }
258 // X86 ret instruction may pop stack.
259 setOperationAction(ISD::RET , MVT::Other, Custom);
260 if (!Subtarget->is64Bit())
261 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
262
263 // Darwin ABI issue.
264 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
265 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
266 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
268 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
269 if (Subtarget->is64Bit()) {
270 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
271 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
272 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
273 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
274 }
275 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
276 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
277 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
278 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
279 // X86 wants to expand memset / memcpy itself.
280 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
281 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
282
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000283 if (!Subtarget->hasSSE2())
284 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
285
286
Evan Cheng2e28d622008-02-02 04:07:54 +0000287 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 // FIXME - use subtarget debug flags
290 if (!Subtarget->isTargetDarwin() &&
291 !Subtarget->isTargetELF() &&
292 !Subtarget->isTargetCygMing())
293 setOperationAction(ISD::LABEL, MVT::Other, Expand);
294
295 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
296 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
297 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
298 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
299 if (Subtarget->is64Bit()) {
300 // FIXME: Verify
301 setExceptionPointerRegister(X86::RAX);
302 setExceptionSelectorRegister(X86::RDX);
303 } else {
304 setExceptionPointerRegister(X86::EAX);
305 setExceptionSelectorRegister(X86::EDX);
306 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000307 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308
Duncan Sands7407a9f2007-09-11 14:10:23 +0000309 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000310
Chris Lattner56b941f2008-01-15 21:58:22 +0000311 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000312
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
314 setOperationAction(ISD::VASTART , MVT::Other, Custom);
315 setOperationAction(ISD::VAARG , MVT::Other, Expand);
316 setOperationAction(ISD::VAEND , MVT::Other, Expand);
317 if (Subtarget->is64Bit())
318 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
319 else
320 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
321
322 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
323 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
324 if (Subtarget->is64Bit())
325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
326 if (Subtarget->isTargetCygMing())
327 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
328 else
329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
330
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000331 if (X86ScalarSSEf64) {
332 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 // Set up the FP register classes.
334 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
335 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
336
337 // Use ANDPD to simulate FABS.
338 setOperationAction(ISD::FABS , MVT::f64, Custom);
339 setOperationAction(ISD::FABS , MVT::f32, Custom);
340
341 // Use XORP to simulate FNEG.
342 setOperationAction(ISD::FNEG , MVT::f64, Custom);
343 setOperationAction(ISD::FNEG , MVT::f32, Custom);
344
345 // Use ANDPD and ORPD to simulate FCOPYSIGN.
346 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
347 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
348
349 // We don't support sin/cos/fmod
350 setOperationAction(ISD::FSIN , MVT::f64, Expand);
351 setOperationAction(ISD::FCOS , MVT::f64, Expand);
352 setOperationAction(ISD::FREM , MVT::f64, Expand);
353 setOperationAction(ISD::FSIN , MVT::f32, Expand);
354 setOperationAction(ISD::FCOS , MVT::f32, Expand);
355 setOperationAction(ISD::FREM , MVT::f32, Expand);
356
357 // Expand FP immediates into loads from the stack, except for the special
358 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000359 addLegalFPImmediate(APFloat(+0.0)); // xorpd
360 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000361
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000362 // Floating truncations from f80 and extensions to f80 go through memory.
363 // If optimizing, we lie about this though and handle it in
364 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
365 if (Fast) {
366 setConvertAction(MVT::f32, MVT::f80, Expand);
367 setConvertAction(MVT::f64, MVT::f80, Expand);
368 setConvertAction(MVT::f80, MVT::f32, Expand);
369 setConvertAction(MVT::f80, MVT::f64, Expand);
370 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000371 } else if (X86ScalarSSEf32) {
372 // Use SSE for f32, x87 for f64.
373 // Set up the FP register classes.
374 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
375 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
376
377 // Use ANDPS to simulate FABS.
378 setOperationAction(ISD::FABS , MVT::f32, Custom);
379
380 // Use XORP to simulate FNEG.
381 setOperationAction(ISD::FNEG , MVT::f32, Custom);
382
383 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
384
385 // Use ANDPS and ORPS to simulate FCOPYSIGN.
386 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
387 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
388
389 // We don't support sin/cos/fmod
390 setOperationAction(ISD::FSIN , MVT::f32, Expand);
391 setOperationAction(ISD::FCOS , MVT::f32, Expand);
392 setOperationAction(ISD::FREM , MVT::f32, Expand);
393
Nate Begemane2ba64f2008-02-14 08:57:00 +0000394 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000395 addLegalFPImmediate(APFloat(+0.0f)); // xorps
396 addLegalFPImmediate(APFloat(+0.0)); // FLD0
397 addLegalFPImmediate(APFloat(+1.0)); // FLD1
398 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
399 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
400
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000401 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
402 // this though and handle it in InstructionSelectPreprocess so that
403 // dagcombine2 can hack on these.
404 if (Fast) {
405 setConvertAction(MVT::f32, MVT::f64, Expand);
406 setConvertAction(MVT::f32, MVT::f80, Expand);
407 setConvertAction(MVT::f80, MVT::f32, Expand);
408 setConvertAction(MVT::f64, MVT::f32, Expand);
409 // And x87->x87 truncations also.
410 setConvertAction(MVT::f80, MVT::f64, Expand);
411 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000412
413 if (!UnsafeFPMath) {
414 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
415 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
416 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000418 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 // Set up the FP register classes.
420 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
421 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
422
423 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
424 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
425 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
426 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000427
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000428 // Floating truncations go through memory. If optimizing, we lie about
429 // this though and handle it in InstructionSelectPreprocess so that
430 // dagcombine2 can hack on these.
431 if (Fast) {
432 setConvertAction(MVT::f80, MVT::f32, Expand);
433 setConvertAction(MVT::f64, MVT::f32, Expand);
434 setConvertAction(MVT::f80, MVT::f64, Expand);
435 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436
437 if (!UnsafeFPMath) {
438 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
439 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
440 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000441 addLegalFPImmediate(APFloat(+0.0)); // FLD0
442 addLegalFPImmediate(APFloat(+1.0)); // FLD1
443 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
444 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000445 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
446 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
447 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
448 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 }
450
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000451 // Long double always uses X87.
452 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000453 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
454 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000455 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000456 APFloat TmpFlt(+0.0);
457 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
458 addLegalFPImmediate(TmpFlt); // FLD0
459 TmpFlt.changeSign();
460 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
461 APFloat TmpFlt2(+1.0);
462 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
463 addLegalFPImmediate(TmpFlt2); // FLD1
464 TmpFlt2.changeSign();
465 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
466 }
467
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000468 if (!UnsafeFPMath) {
469 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
470 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
471 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000472
Dan Gohman2f7b1982007-10-11 23:21:31 +0000473 // Always use a library call for pow.
474 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
475 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
476 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
477
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 // First set operation action for all vector types to expand. Then we
479 // will selectively turn on ones that can be effectively codegen'd.
480 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
481 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
482 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
485 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
486 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000505 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000509 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000510 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000513 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 }
520
521 if (Subtarget->hasMMX()) {
522 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
523 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
524 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
525 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
526
527 // FIXME: add MMX packed arithmetics
528
529 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
530 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
531 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
532 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
533
534 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
535 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
536 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000537 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538
539 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
540 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
541
542 setOperationAction(ISD::AND, MVT::v8i8, Promote);
543 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
544 setOperationAction(ISD::AND, MVT::v4i16, Promote);
545 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
546 setOperationAction(ISD::AND, MVT::v2i32, Promote);
547 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
548 setOperationAction(ISD::AND, MVT::v1i64, Legal);
549
550 setOperationAction(ISD::OR, MVT::v8i8, Promote);
551 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
552 setOperationAction(ISD::OR, MVT::v4i16, Promote);
553 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
554 setOperationAction(ISD::OR, MVT::v2i32, Promote);
555 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
556 setOperationAction(ISD::OR, MVT::v1i64, Legal);
557
558 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
559 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
560 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
561 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
562 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
563 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
564 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
565
566 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
567 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
568 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
569 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
570 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
571 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
572 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
573
574 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
575 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
576 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
577 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
578
579 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
580 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
581 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
582 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
583
584 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
585 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
587 }
588
589 if (Subtarget->hasSSE1()) {
590 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
591
592 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
593 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
594 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
595 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
596 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
597 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
599 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
600 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
601 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
602 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
603 }
604
605 if (Subtarget->hasSSE2()) {
606 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
607 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
608 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
609 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
610 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
611
612 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
613 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
614 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
615 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
616 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
617 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
618 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
619 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
620 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
621 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
622 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
623 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
624 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
625 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
626 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627
628 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
630 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
631 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
633
634 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
635 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000636 // Do not attempt to custom lower non-power-of-2 vectors
637 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
638 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
640 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
641 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
642 }
643 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
644 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
646 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000647 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000649 if (Subtarget->is64Bit()) {
650 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000651 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000652 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653
654 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
655 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
656 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
657 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
658 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
659 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
660 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
661 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
662 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
663 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
664 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
665 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
666 }
667
Chris Lattner3bc08502008-01-17 19:59:44 +0000668 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000669
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 // Custom lower v2i64 and v2f64 selects.
671 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
672 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
673 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
674 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
675 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000676
677 if (Subtarget->hasSSE41()) {
678 // FIXME: Do we need to handle scalar-to-vector here?
679 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
680
681 // i8 and i16 vectors are custom , because the source register and source
682 // source memory operand types are not the same width. f32 vectors are
683 // custom since the immediate controlling the insert encodes additional
684 // information.
685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
686 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
687 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
689
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
692 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
694
695 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
697 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000698 }
699 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700
701 // We want to custom lower some of our intrinsics.
702 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
703
704 // We have target-specific dag combine patterns for the following nodes:
705 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
706 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000707 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708
709 computeRegisterProperties();
710
711 // FIXME: These should be based on subtarget info. Plus, the values should
712 // be smaller when we are in optimizing for size mode.
713 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
714 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
715 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
716 allowUnalignedMemoryAccesses = true; // x86 supports it!
717}
718
Evan Cheng5a67b812008-01-23 23:17:41 +0000719/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
720/// the desired ByVal argument alignment.
721static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
722 if (MaxAlign == 16)
723 return;
724 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
725 if (VTy->getBitWidth() == 128)
726 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000727 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
728 unsigned EltAlign = 0;
729 getMaxByValAlign(ATy->getElementType(), EltAlign);
730 if (EltAlign > MaxAlign)
731 MaxAlign = EltAlign;
732 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
733 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
734 unsigned EltAlign = 0;
735 getMaxByValAlign(STy->getElementType(i), EltAlign);
736 if (EltAlign > MaxAlign)
737 MaxAlign = EltAlign;
738 if (MaxAlign == 16)
739 break;
740 }
741 }
742 return;
743}
744
745/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
746/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000747/// that contain SSE vectors are placed at 16-byte boundaries while the rest
748/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000749unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
750 if (Subtarget->is64Bit())
751 return getTargetData()->getABITypeAlignment(Ty);
752 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000753 if (Subtarget->hasSSE1())
754 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000755 return Align;
756}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757
Evan Cheng6fb06762007-11-09 01:32:10 +0000758/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
759/// jumptable.
760SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
761 SelectionDAG &DAG) const {
762 if (usesGlobalOffsetTable())
763 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
764 if (!Subtarget->isPICStyleRIPRel())
765 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
766 return Table;
767}
768
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769//===----------------------------------------------------------------------===//
770// Return Value Calling Convention Implementation
771//===----------------------------------------------------------------------===//
772
773#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000774
775/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
776/// exists skip possible ISD:TokenFactor.
777static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000778 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000779 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000780 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000781 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000782 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000783 return Chain.getOperand(0);
784 }
785 return Chain;
786}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000787
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788/// LowerRET - Lower an ISD::RET node.
789SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
790 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
791
792 SmallVector<CCValAssign, 16> RVLocs;
793 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
794 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
795 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
796 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000797
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 // If this is the first return lowered for this function, add the regs to the
799 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000800 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 for (unsigned i = 0; i != RVLocs.size(); ++i)
802 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000803 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000807 // Handle tail call return.
808 Chain = GetPossiblePreceedingTailCall(Chain);
809 if (Chain.getOpcode() == X86ISD::TAILCALL) {
810 SDOperand TailCall = Chain;
811 SDOperand TargetAddress = TailCall.getOperand(1);
812 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000813 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000814 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
815 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
816 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
817 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
818 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000819 assert(StackAdjustment.getOpcode() == ISD::Constant &&
820 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000821
822 SmallVector<SDOperand,8> Operands;
823 Operands.push_back(Chain.getOperand(0));
824 Operands.push_back(TargetAddress);
825 Operands.push_back(StackAdjustment);
826 // Copy registers used by the call. Last operand is a flag so it is not
827 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000828 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000829 Operands.push_back(Chain.getOperand(i));
830 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000831 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
832 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000833 }
834
835 // Regular return.
836 SDOperand Flag;
837
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 // Copy the result values into the output registers.
839 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
840 RVLocs[0].getLocReg() != X86::ST0) {
841 for (unsigned i = 0; i != RVLocs.size(); ++i) {
842 CCValAssign &VA = RVLocs[i];
843 assert(VA.isRegLoc() && "Can only return in registers!");
844 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
845 Flag);
846 Flag = Chain.getValue(1);
847 }
848 } else {
849 // We need to handle a destination of ST0 specially, because it isn't really
850 // a register.
851 SDOperand Value = Op.getOperand(1);
852
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000853 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
854 // This will get legalized into a load/store if it can't get optimized away.
855 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
856 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857
858 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
859 SDOperand Ops[] = { Chain, Value };
860 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
861 Flag = Chain.getValue(1);
862 }
863
864 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
865 if (Flag.Val)
866 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
867 else
868 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
869}
870
871
872/// LowerCallResult - Lower the result values of an ISD::CALL into the
873/// appropriate copies out of appropriate physical registers. This assumes that
874/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
875/// being lowered. The returns a SDNode with the same number of values as the
876/// ISD::CALL.
877SDNode *X86TargetLowering::
878LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
879 unsigned CallingConv, SelectionDAG &DAG) {
880
881 // Assign locations to each value returned by this call.
882 SmallVector<CCValAssign, 16> RVLocs;
883 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
884 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
885 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
886
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 SmallVector<SDOperand, 8> ResultVals;
888
889 // Copy all of the result registers out of their specified physreg.
890 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
891 for (unsigned i = 0; i != RVLocs.size(); ++i) {
892 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
893 RVLocs[i].getValVT(), InFlag).getValue(1);
894 InFlag = Chain.getValue(2);
895 ResultVals.push_back(Chain.getValue(0));
896 }
897 } else {
898 // Copies from the FP stack are special, as ST0 isn't a valid register
899 // before the fp stackifier runs.
900
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000901 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
902 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
903 // the specified value type.
904 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
905 if (isScalarFPTypeInSSEReg(GetResultTy))
906 GetResultTy = MVT::f80;
907 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
908
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 SDOperand GROps[] = { Chain, InFlag };
910 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
911 Chain = RetVal.getValue(1);
912 InFlag = RetVal.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000913
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000914 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
915 // there.
916 if (GetResultTy != RVLocs[0].getValVT())
917 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
918 // This truncation won't change the value.
919 DAG.getIntPtrConstant(1));
920
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 ResultVals.push_back(RetVal);
922 }
923
924 // Merge everything together with a MERGE_VALUES node.
925 ResultVals.push_back(Chain);
926 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
927 &ResultVals[0], ResultVals.size()).Val;
928}
929
Evan Cheng931a8f42008-01-29 19:34:22 +0000930/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
931/// ISD::CALL where the results are known to be in two 64-bit registers,
932/// e.g. XMM0 and XMM1. This simplify store the two values back to the
933/// fixed stack slot allocated for StructRet.
934SDNode *X86TargetLowering::
935LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
936 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
937 MVT::ValueType VT, SelectionDAG &DAG) {
938 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
939 Chain = RetVal1.getValue(1);
940 InFlag = RetVal1.getValue(2);
941 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
942 Chain = RetVal2.getValue(1);
943 InFlag = RetVal2.getValue(2);
944 SDOperand FIN = TheCall->getOperand(5);
945 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
946 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
947 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
948 return Chain.Val;
949}
950
951/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
952/// where the results are known to be in ST0 and ST1.
953SDNode *X86TargetLowering::
954LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
955 SDNode *TheCall, SelectionDAG &DAG) {
956 SmallVector<SDOperand, 8> ResultVals;
957 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
958 SDVTList Tys = DAG.getVTList(VTs, 4);
959 SDOperand Ops[] = { Chain, InFlag };
960 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
961 Chain = RetVal.getValue(2);
962 SDOperand FIN = TheCall->getOperand(5);
963 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
964 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
965 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
966 return Chain.Val;
967}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968
969//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000970// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971//===----------------------------------------------------------------------===//
972// StdCall calling convention seems to be standard for many Windows' API
973// routines and around. It differs from C calling convention just a little:
974// callee should clean up the stack, not caller. Symbols should be also
975// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000976// For info on fast calling convention see Fast Calling Convention (tail call)
977// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978
979/// AddLiveIn - This helper function adds the specified physical register to the
980/// MachineFunction as a live in value. It also creates a corresponding virtual
981/// register for it.
982static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
983 const TargetRegisterClass *RC) {
984 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000985 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
986 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 return VReg;
988}
989
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000990/// CallIsStructReturn - Determines whether a CALL node uses struct return
991/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000992static bool CallIsStructReturn(SDOperand Op) {
993 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
994 if (!NumOps)
995 return false;
996
997 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
998 return Flags->getValue() & ISD::ParamFlags::StructReturn;
999}
1000
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001001/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1002/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001003static bool ArgsAreStructReturn(SDOperand Op) {
1004 unsigned NumArgs = Op.Val->getNumValues() - 1;
1005 if (!NumArgs)
1006 return false;
1007
1008 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1009 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1010}
1011
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001012/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1013/// callee to pop its own arguments. Callee pop is necessary to support tail
1014/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001015bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1016 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1017 if (IsVarArg)
1018 return false;
1019
1020 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1021 default:
1022 return false;
1023 case CallingConv::X86_StdCall:
1024 return !Subtarget->is64Bit();
1025 case CallingConv::X86_FastCall:
1026 return !Subtarget->is64Bit();
1027 case CallingConv::Fast:
1028 return PerformTailCallOpt;
1029 }
1030}
1031
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001032/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1033/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001034CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1035 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1036
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001037 if (Subtarget->is64Bit()) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001038 if (CC == CallingConv::Fast && PerformTailCallOpt)
1039 return CC_X86_64_TailCall;
1040 else
1041 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001042 }
1043
Gordon Henriksen18ace102008-01-05 16:56:59 +00001044 if (CC == CallingConv::X86_FastCall)
1045 return CC_X86_32_FastCall;
1046 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1047 return CC_X86_32_TailCall;
1048 else
1049 return CC_X86_32_C;
1050}
1051
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001052/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1053/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001054NameDecorationStyle
1055X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1056 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1057 if (CC == CallingConv::X86_FastCall)
1058 return FastCall;
1059 else if (CC == CallingConv::X86_StdCall)
1060 return StdCall;
1061 return None;
1062}
1063
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001064/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1065/// possibly be overwritten when lowering the outgoing arguments in a tail
1066/// call. Currently the implementation of this call is very conservative and
1067/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1068/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001069static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1070 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001071 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001072 FrameIndexSDNode * FrameIdxNode = NULL;
1073 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001074 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1075 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001076 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1077 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1078 (Op.getOpcode() == ISD::LOAD &&
1079 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1080 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1081 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001082 return true;
1083 return false;
1084}
1085
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001086/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1087/// in a register before calling.
1088bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1089 return !IsTailCall && !Is64Bit &&
1090 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1091 Subtarget->isPICStyleGOT();
1092}
1093
1094
1095/// CallRequiresFnAddressInReg - Check whether the call requires the function
1096/// address to be loaded in a register.
1097bool
1098X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1099 return !Is64Bit && IsTailCall &&
1100 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1101 Subtarget->isPICStyleGOT();
1102}
1103
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001104/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1105/// arguments to force loading and guarantee that arguments sourcing from
1106/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001107static SDOperand
1108CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1109 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1110 SelectionDAG &DAG,
1111 MachineFunction &MF,
1112 const TargetLowering * TL) {
1113
1114 SDOperand InFlag;
1115 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1116 SDOperand Arg = TailCallClobberedVRegs[i].second;
1117 unsigned Idx = TailCallClobberedVRegs[i].first;
1118 unsigned VReg =
1119 MF.getRegInfo().
1120 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1121 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1122 InFlag = Chain.getValue(1);
1123 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1124 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1125 Chain = Arg.getValue(1);
1126 InFlag = Arg.getValue(2);
1127 }
1128 return Chain;
1129}
1130
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001131/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1132/// by "Src" to address "Dst" with size and alignment information specified by
1133/// the specific parameter attribute. The copy will be passed as a byval function
1134/// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001135static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001136CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1137 unsigned Flags, SelectionDAG &DAG) {
1138 unsigned Align = 1 <<
1139 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1140 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001141 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001142 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1143 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001144 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001145 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001146}
1147
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001148SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1149 const CCValAssign &VA,
1150 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001151 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001152 SDOperand Root, unsigned i) {
1153 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001154 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001155 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Evan Cheng3e42a522008-01-10 02:24:25 +00001156 bool isByVal = Flags & ISD::ParamFlags::ByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001157 bool isImmutable = !AlwaysUseMutable && !isByVal;
Evan Cheng3e42a522008-01-10 02:24:25 +00001158
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001159 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1160 // changed with more analysis.
1161 // In case of tail call optimization mark all arguments mutable. Since they
1162 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001163 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001164 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001165 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001166 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001167 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001168 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001169 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001170}
1171
Gordon Henriksen18ace102008-01-05 16:56:59 +00001172SDOperand
1173X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001175 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1176
1177 const Function* Fn = MF.getFunction();
1178 if (Fn->hasExternalLinkage() &&
1179 Subtarget->isTargetCygMing() &&
1180 Fn->getName() == "main")
1181 FuncInfo->setForceFramePointer(true);
1182
1183 // Decorate the function name.
1184 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1185
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186 MachineFrameInfo *MFI = MF.getFrameInfo();
1187 SDOperand Root = Op.getOperand(0);
1188 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001189 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001190 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001191
1192 assert(!(isVarArg && CC == CallingConv::Fast) &&
1193 "Var args not supported with calling convention fastcc");
1194
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195 // Assign locations to all of the incoming arguments.
1196 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001197 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001198 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 SmallVector<SDOperand, 8> ArgValues;
1201 unsigned LastVal = ~0U;
1202 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1203 CCValAssign &VA = ArgLocs[i];
1204 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1205 // places.
1206 assert(VA.getValNo() != LastVal &&
1207 "Don't support value assigned to multiple locs yet");
1208 LastVal = VA.getValNo();
1209
1210 if (VA.isRegLoc()) {
1211 MVT::ValueType RegVT = VA.getLocVT();
1212 TargetRegisterClass *RC;
1213 if (RegVT == MVT::i32)
1214 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001215 else if (Is64Bit && RegVT == MVT::i64)
1216 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001217 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001218 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001219 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221 else {
1222 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001223 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1224 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1225 RegVT = MVT::i64;
1226 } else
1227 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001229
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001230 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1231 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1232
1233 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1234 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1235 // right size.
1236 if (VA.getLocInfo() == CCValAssign::SExt)
1237 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1238 DAG.getValueType(VA.getValVT()));
1239 else if (VA.getLocInfo() == CCValAssign::ZExt)
1240 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1241 DAG.getValueType(VA.getValVT()));
1242
1243 if (VA.getLocInfo() != CCValAssign::Full)
1244 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1245
Gordon Henriksen18ace102008-01-05 16:56:59 +00001246 // Handle MMX values passed in GPRs.
1247 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1248 MVT::getSizeInBits(RegVT) == 64)
1249 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1250
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251 ArgValues.push_back(ArgValue);
1252 } else {
1253 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001254 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 }
1256 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001259 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001260 if (CC == CallingConv::Fast)
1261 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262
1263 // If the function takes variable number of arguments, make a frame index for
1264 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001265 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001266 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1267 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1268 }
1269 if (Is64Bit) {
1270 static const unsigned GPR64ArgRegs[] = {
1271 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1272 };
1273 static const unsigned XMMArgRegs[] = {
1274 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1275 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1276 };
1277
1278 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1279 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1280
1281 // For X86-64, if there are vararg parameters that are passed via
1282 // registers, then we must store them to their spots on the stack so they
1283 // may be loaded by deferencing the result of va_next.
1284 VarArgsGPOffset = NumIntRegs * 8;
1285 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1286 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1287
1288 // Store the integer parameter registers.
1289 SmallVector<SDOperand, 8> MemOps;
1290 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1291 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001292 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001293 for (; NumIntRegs != 6; ++NumIntRegs) {
1294 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1295 X86::GR64RegisterClass);
1296 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001297 SDOperand Store =
1298 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001299 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001300 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001301 MemOps.push_back(Store);
1302 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001303 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001304 }
1305
1306 // Now store the XMM (fp + vector) parameter registers.
1307 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001308 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001309 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1310 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1311 X86::VR128RegisterClass);
1312 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001313 SDOperand Store =
1314 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001315 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001316 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001317 MemOps.push_back(Store);
1318 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001319 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 }
1321 if (!MemOps.empty())
1322 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1323 &MemOps[0], MemOps.size());
1324 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001325 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001326
1327 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1328 // arguments and the arguments after the retaddr has been pushed are
1329 // aligned.
1330 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1331 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1332 (StackSize & 7) == 0)
1333 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001335 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001336
Gordon Henriksen18ace102008-01-05 16:56:59 +00001337 // Some CCs need callee pop.
1338 if (IsCalleePop(Op)) {
1339 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001340 BytesCallerReserves = 0;
1341 } else {
1342 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001344 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001345 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 BytesCallerReserves = StackSize;
1347 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001348
Gordon Henriksen18ace102008-01-05 16:56:59 +00001349 if (!Is64Bit) {
1350 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1351 if (CC == CallingConv::X86_FastCall)
1352 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1353 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354
Anton Korobeynikove844e472007-08-15 17:12:32 +00001355 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356
1357 // Return the new list of results.
1358 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1359 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1360}
1361
Evan Chengbc077bf2008-01-10 00:09:10 +00001362SDOperand
1363X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1364 const SDOperand &StackPtr,
1365 const CCValAssign &VA,
1366 SDOperand Chain,
1367 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001368 unsigned LocMemOffset = VA.getLocMemOffset();
1369 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001370 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1371 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1372 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1373 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001374 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001375 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001376 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001377 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001378}
1379
Evan Cheng931a8f42008-01-29 19:34:22 +00001380/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1381/// struct return call to the specified function. X86-64 ABI specifies
1382/// some SRet calls are actually returned in registers. Since current
1383/// LLVM cannot represent multi-value calls, they are represent as
1384/// calls where the results are passed in a hidden struct provided by
1385/// the caller. This function examines the type of the struct to
1386/// determine the correct way to implement the call.
1387X86::X86_64SRet
1388X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1389 // FIXME: Disabled for now.
1390 return X86::InMemory;
1391
1392 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1393 const Type *RTy = PTy->getElementType();
1394 unsigned Size = getTargetData()->getABITypeSize(RTy);
1395 if (Size != 16 && Size != 32)
1396 return X86::InMemory;
1397
1398 if (Size == 32) {
1399 const StructType *STy = dyn_cast<StructType>(RTy);
1400 if (!STy) return X86::InMemory;
1401 if (STy->getNumElements() == 2 &&
1402 STy->getElementType(0) == Type::X86_FP80Ty &&
1403 STy->getElementType(1) == Type::X86_FP80Ty)
1404 return X86::InX87;
1405 }
1406
1407 bool AllFP = true;
1408 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1409 I != E; ++I) {
1410 const Type *STy = I->get();
1411 if (!STy->isFPOrFPVector()) {
1412 AllFP = false;
1413 break;
1414 }
1415 }
1416
1417 if (AllFP)
1418 return X86::InSSE;
1419 return X86::InGPR64;
1420}
1421
1422void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1423 CCAssignFn *Fn,
1424 CCState &CCInfo) {
1425 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1426 for (unsigned i = 1; i != NumOps; ++i) {
1427 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1428 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1429 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1430 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1431 cerr << "Call operand #" << i << " has unhandled type "
1432 << MVT::getValueTypeString(ArgVT) << "\n";
1433 abort();
1434 }
1435 }
1436}
1437
Gordon Henriksen18ace102008-01-05 16:56:59 +00001438SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1439 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001440 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001441 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001442 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001444 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1445 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001446 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001447 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001448 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001449
1450 assert(!(isVarArg && CC == CallingConv::Fast) &&
1451 "Var args not supported with calling convention fastcc");
1452
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 // Analyze operands of the call, assigning locations to each operand.
1454 SmallVector<CCValAssign, 16> ArgLocs;
1455 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001456 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1457
1458 X86::X86_64SRet SRetMethod = X86::InMemory;
1459 if (Is64Bit && IsStructRet)
1460 // FIXME: We can't figure out type of the sret structure for indirect
1461 // calls. We need to copy more information from CallSite to the ISD::CALL
1462 // node.
1463 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1464 SRetMethod =
1465 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1466
1467 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1468 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1469 // a sret call.
1470 if (SRetMethod != X86::InMemory)
1471 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1472 else
1473 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474
1475 // Get a count of how many bytes are to be pushed on the stack.
1476 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001477 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001478 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479
Gordon Henriksen18ace102008-01-05 16:56:59 +00001480 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1481 // arguments and the arguments after the retaddr has been pushed are aligned.
1482 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1483 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1484 (NumBytes & 7) == 0)
1485 NumBytes += 4;
1486
1487 int FPDiff = 0;
1488 if (IsTailCall) {
1489 // Lower arguments at fp - stackoffset + fpdiff.
1490 unsigned NumBytesCallerPushed =
1491 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1492 FPDiff = NumBytesCallerPushed - NumBytes;
1493
1494 // Set the delta of movement of the returnaddr stackslot.
1495 // But only set if delta is greater than previous delta.
1496 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1497 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1498 }
1499
Chris Lattner5872a362008-01-17 07:00:52 +00001500 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001502 SDOperand RetAddrFrIdx;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001503 if (IsTailCall) {
1504 // Adjust the Return address stack slot.
1505 if (FPDiff) {
1506 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1507 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1508 // Load the "old" Return address.
1509 RetAddrFrIdx =
1510 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001511 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1512 }
1513 }
1514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001516 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517 SmallVector<SDOperand, 8> MemOpChains;
1518
1519 SDOperand StackPtr;
1520
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001521 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001522 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001523 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1524 CCValAssign &VA = ArgLocs[i];
1525 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1526
1527 // Promote the value if needed.
1528 switch (VA.getLocInfo()) {
1529 default: assert(0 && "Unknown loc info!");
1530 case CCValAssign::Full: break;
1531 case CCValAssign::SExt:
1532 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1533 break;
1534 case CCValAssign::ZExt:
1535 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1536 break;
1537 case CCValAssign::AExt:
1538 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1539 break;
1540 }
1541
1542 if (VA.isRegLoc()) {
1543 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1544 } else {
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001545 if (!IsTailCall) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001546 assert(VA.isMemLoc());
1547 if (StackPtr.Val == 0)
1548 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1549
1550 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1551 Arg));
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001552 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1553 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001554 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 }
1556 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557
1558 if (!MemOpChains.empty())
1559 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1560 &MemOpChains[0], MemOpChains.size());
1561
1562 // Build a sequence of copy-to-reg nodes chained together with token chain
1563 // and flag operands which copy the outgoing args into registers.
1564 SDOperand InFlag;
1565 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1566 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1567 InFlag);
1568 InFlag = Chain.getValue(1);
1569 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001572 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001573 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1574 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1575 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1576 InFlag);
1577 InFlag = Chain.getValue(1);
1578 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001579 // If we are tail calling and generating PIC/GOT style code load the address
1580 // of the callee into ecx. The value in ecx is used as target of the tail
1581 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1582 // calls on PIC/GOT architectures. Normally we would just put the address of
1583 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1584 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001585 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001586 // Note: The actual moving to ecx is done further down.
1587 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1588 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1589 !G->getGlobal()->hasProtectedVisibility())
1590 Callee = LowerGlobalAddress(Callee, DAG);
1591 else if (isa<ExternalSymbolSDNode>(Callee))
1592 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001594
Gordon Henriksen18ace102008-01-05 16:56:59 +00001595 if (Is64Bit && isVarArg) {
1596 // From AMD64 ABI document:
1597 // For calls that may call functions that use varargs or stdargs
1598 // (prototype-less calls or calls to functions containing ellipsis (...) in
1599 // the declaration) %al is used as hidden argument to specify the number
1600 // of SSE registers used. The contents of %al do not need to match exactly
1601 // the number of registers, but must be an ubound on the number of SSE
1602 // registers used and is in the range 0 - 8 inclusive.
1603
1604 // Count the number of XMM registers allocated.
1605 static const unsigned XMMArgRegs[] = {
1606 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1607 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1608 };
1609 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1610
1611 Chain = DAG.getCopyToReg(Chain, X86::AL,
1612 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1613 InFlag = Chain.getValue(1);
1614 }
1615
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001616
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001617 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001618 if (IsTailCall) {
1619 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001620 SDOperand FIN;
1621 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001622 // Do not flag preceeding copytoreg stuff together with the following stuff.
1623 InFlag = SDOperand();
1624
1625 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1626 DAG, MF, this);
1627
Gordon Henriksen18ace102008-01-05 16:56:59 +00001628 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1629 CCValAssign &VA = ArgLocs[i];
1630 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001631 assert(VA.isMemLoc());
1632 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001633 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1634 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001635 // Create frame index.
1636 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1637 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1638 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1639 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001640
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001641 // Find virtual register for this argument.
1642 bool Found=false;
1643 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1644 if (TailCallClobberedVRegs[idx].first==i) {
1645 Arg = TailCallClobberedVRegs[idx].second;
1646 Found=true;
1647 break;
1648 }
1649 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1650 (Found==true && "No corresponding Argument was found"));
1651
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001653 // Copy relative to framepointer.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001654 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001655 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001656 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001657 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001658 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001659 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001660 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001661 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001662 }
1663 }
1664
1665 if (!MemOpChains2.empty())
1666 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001667 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001668
1669 // Store the return address to the appropriate stack slot.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001670 if (FPDiff) {
1671 // Calculate the new stack slot for the return address.
1672 int SlotSize = Is64Bit ? 8 : 4;
1673 int NewReturnAddrFI =
1674 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1675 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1676 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1677 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1678 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1679 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001680 }
1681
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 // If the callee is a GlobalAddress node (quite common, every direct call is)
1683 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1684 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1685 // We should use extra load for direct calls to dllimported functions in
1686 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 if ((IsTailCall || !Is64Bit ||
1688 getTargetMachine().getCodeModel() != CodeModel::Large)
1689 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1690 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001692 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 if (IsTailCall || !Is64Bit ||
1694 getTargetMachine().getCodeModel() != CodeModel::Large)
1695 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1696 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001697 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1698
1699 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001700 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001701 Callee,InFlag);
1702 Callee = DAG.getRegister(Opc, getPointerTy());
1703 // Add register as live out.
1704 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001705 }
1706
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707 // Returns a chain & a flag for retval copy to use.
1708 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1709 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710
1711 if (IsTailCall) {
1712 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001713 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1714 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715 if (InFlag.Val)
1716 Ops.push_back(InFlag);
1717 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1718 InFlag = Chain.getValue(1);
1719
1720 // Returns a chain & a flag for retval copy to use.
1721 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1722 Ops.clear();
1723 }
1724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001725 Ops.push_back(Chain);
1726 Ops.push_back(Callee);
1727
Gordon Henriksen18ace102008-01-05 16:56:59 +00001728 if (IsTailCall)
1729 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730
1731 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001732 if (!IsTailCall && !Is64Bit &&
1733 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734 Subtarget->isPICStyleGOT())
1735 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001736
Gordon Henriksen18ace102008-01-05 16:56:59 +00001737 // Add argument registers to the end of the list so that they are known live
1738 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001739 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1740 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1741 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001742
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001743 if (InFlag.Val)
1744 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001745
Gordon Henriksen18ace102008-01-05 16:56:59 +00001746 if (IsTailCall) {
1747 assert(InFlag.Val &&
1748 "Flag must be set. Depend on flag being set in LowerRET");
1749 Chain = DAG.getNode(X86ISD::TAILCALL,
1750 Op.Val->getVTList(), &Ops[0], Ops.size());
1751
1752 return SDOperand(Chain.Val, Op.ResNo);
1753 }
1754
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001755 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756 InFlag = Chain.getValue(1);
1757
1758 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759 unsigned NumBytesForCalleeToPush;
1760 if (IsCalleePop(Op))
1761 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001762 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001763 // If this is is a call to a struct-return function, the callee
1764 // pops the hidden struct pointer, so we have to push it back.
1765 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001766 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001767 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001768 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001769
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001770 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001771 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001772 DAG.getIntPtrConstant(NumBytes),
1773 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001774 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001775 InFlag = Chain.getValue(1);
1776
1777 // Handle result values, copying them out of physregs into vregs that we
1778 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001779 switch (SRetMethod) {
1780 default:
1781 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1782 case X86::InGPR64:
1783 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1784 X86::RAX, X86::RDX,
1785 MVT::i64, DAG), Op.ResNo);
1786 case X86::InSSE:
1787 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1788 X86::XMM0, X86::XMM1,
1789 MVT::f64, DAG), Op.ResNo);
1790 case X86::InX87:
1791 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1792 Op.ResNo);
1793 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001794}
1795
1796
1797//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001798// Fast Calling Convention (tail call) implementation
1799//===----------------------------------------------------------------------===//
1800
1801// Like std call, callee cleans arguments, convention except that ECX is
1802// reserved for storing the tail called function address. Only 2 registers are
1803// free for argument passing (inreg). Tail call optimization is performed
1804// provided:
1805// * tailcallopt is enabled
1806// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001807// On X86_64 architecture with GOT-style position independent code only local
1808// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001809// To keep the stack aligned according to platform abi the function
1810// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1811// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001812// If a tail called function callee has more arguments than the caller the
1813// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001814// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001815// original REtADDR, but before the saved framepointer or the spilled registers
1816// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1817// stack layout:
1818// arg1
1819// arg2
1820// RETADDR
1821// [ new RETADDR
1822// move area ]
1823// (possible EBP)
1824// ESI
1825// EDI
1826// local1 ..
1827
1828/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1829/// for a 16 byte align requirement.
1830unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1831 SelectionDAG& DAG) {
1832 if (PerformTailCallOpt) {
1833 MachineFunction &MF = DAG.getMachineFunction();
1834 const TargetMachine &TM = MF.getTarget();
1835 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1836 unsigned StackAlignment = TFI.getStackAlignment();
1837 uint64_t AlignMask = StackAlignment - 1;
1838 int64_t Offset = StackSize;
1839 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1840 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1841 // Number smaller than 12 so just add the difference.
1842 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1843 } else {
1844 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1845 Offset = ((~AlignMask) & Offset) + StackAlignment +
1846 (StackAlignment-SlotSize);
1847 }
1848 StackSize = Offset;
1849 }
1850 return StackSize;
1851}
1852
1853/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001854/// following the call is a return. A function is eligible if caller/callee
1855/// calling conventions match, currently only fastcc supports tail calls, and
1856/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001857bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1858 SDOperand Ret,
1859 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001860 if (!PerformTailCallOpt)
1861 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001862
1863 // Check whether CALL node immediatly preceeds the RET node and whether the
1864 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001865 unsigned NumOps = Ret.getNumOperands();
1866 if ((NumOps == 1 &&
1867 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1868 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001869 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001870 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1871 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001872 MachineFunction &MF = DAG.getMachineFunction();
1873 unsigned CallerCC = MF.getFunction()->getCallingConv();
1874 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1875 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1876 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001877 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001878 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001879 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001880 return true;
1881
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001882 // Can only do local tail calls (in same module, hidden or protected) on
1883 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001884 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1885 return G->getGlobal()->hasHiddenVisibility()
1886 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001887 }
1888 }
Evan Chenge7a87392007-11-02 01:26:22 +00001889
1890 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001891}
1892
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001893//===----------------------------------------------------------------------===//
1894// Other Lowering Hooks
1895//===----------------------------------------------------------------------===//
1896
1897
1898SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001899 MachineFunction &MF = DAG.getMachineFunction();
1900 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1901 int ReturnAddrIndex = FuncInfo->getRAIndex();
1902
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001903 if (ReturnAddrIndex == 0) {
1904 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001905 if (Subtarget->is64Bit())
1906 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1907 else
1908 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001909
1910 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001911 }
1912
1913 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1914}
1915
1916
1917
1918/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1919/// specific condition code. It returns a false if it cannot do a direct
1920/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1921/// needed.
1922static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1923 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1924 SelectionDAG &DAG) {
1925 X86CC = X86::COND_INVALID;
1926 if (!isFP) {
1927 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1928 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1929 // X > -1 -> X == 0, jump !sign.
1930 RHS = DAG.getConstant(0, RHS.getValueType());
1931 X86CC = X86::COND_NS;
1932 return true;
1933 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1934 // X < 0 -> X == 0, jump on sign.
1935 X86CC = X86::COND_S;
1936 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001937 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1938 // X < 1 -> X <= 0
1939 RHS = DAG.getConstant(0, RHS.getValueType());
1940 X86CC = X86::COND_LE;
1941 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942 }
1943 }
1944
1945 switch (SetCCOpcode) {
1946 default: break;
1947 case ISD::SETEQ: X86CC = X86::COND_E; break;
1948 case ISD::SETGT: X86CC = X86::COND_G; break;
1949 case ISD::SETGE: X86CC = X86::COND_GE; break;
1950 case ISD::SETLT: X86CC = X86::COND_L; break;
1951 case ISD::SETLE: X86CC = X86::COND_LE; break;
1952 case ISD::SETNE: X86CC = X86::COND_NE; break;
1953 case ISD::SETULT: X86CC = X86::COND_B; break;
1954 case ISD::SETUGT: X86CC = X86::COND_A; break;
1955 case ISD::SETULE: X86CC = X86::COND_BE; break;
1956 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1957 }
1958 } else {
1959 // On a floating point condition, the flags are set as follows:
1960 // ZF PF CF op
1961 // 0 | 0 | 0 | X > Y
1962 // 0 | 0 | 1 | X < Y
1963 // 1 | 0 | 0 | X == Y
1964 // 1 | 1 | 1 | unordered
1965 bool Flip = false;
1966 switch (SetCCOpcode) {
1967 default: break;
1968 case ISD::SETUEQ:
1969 case ISD::SETEQ: X86CC = X86::COND_E; break;
1970 case ISD::SETOLT: Flip = true; // Fallthrough
1971 case ISD::SETOGT:
1972 case ISD::SETGT: X86CC = X86::COND_A; break;
1973 case ISD::SETOLE: Flip = true; // Fallthrough
1974 case ISD::SETOGE:
1975 case ISD::SETGE: X86CC = X86::COND_AE; break;
1976 case ISD::SETUGT: Flip = true; // Fallthrough
1977 case ISD::SETULT:
1978 case ISD::SETLT: X86CC = X86::COND_B; break;
1979 case ISD::SETUGE: Flip = true; // Fallthrough
1980 case ISD::SETULE:
1981 case ISD::SETLE: X86CC = X86::COND_BE; break;
1982 case ISD::SETONE:
1983 case ISD::SETNE: X86CC = X86::COND_NE; break;
1984 case ISD::SETUO: X86CC = X86::COND_P; break;
1985 case ISD::SETO: X86CC = X86::COND_NP; break;
1986 }
1987 if (Flip)
1988 std::swap(LHS, RHS);
1989 }
1990
1991 return X86CC != X86::COND_INVALID;
1992}
1993
1994/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1995/// code. Current x86 isa includes the following FP cmov instructions:
1996/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1997static bool hasFPCMov(unsigned X86CC) {
1998 switch (X86CC) {
1999 default:
2000 return false;
2001 case X86::COND_B:
2002 case X86::COND_BE:
2003 case X86::COND_E:
2004 case X86::COND_P:
2005 case X86::COND_A:
2006 case X86::COND_AE:
2007 case X86::COND_NE:
2008 case X86::COND_NP:
2009 return true;
2010 }
2011}
2012
2013/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2014/// true if Op is undef or if its value falls within the specified range (L, H].
2015static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2016 if (Op.getOpcode() == ISD::UNDEF)
2017 return true;
2018
2019 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2020 return (Val >= Low && Val < Hi);
2021}
2022
2023/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2024/// true if Op is undef or if its value equal to the specified value.
2025static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2026 if (Op.getOpcode() == ISD::UNDEF)
2027 return true;
2028 return cast<ConstantSDNode>(Op)->getValue() == Val;
2029}
2030
2031/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2032/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2033bool X86::isPSHUFDMask(SDNode *N) {
2034 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2035
Dan Gohman7dc19012007-08-02 21:17:01 +00002036 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002037 return false;
2038
2039 // Check if the value doesn't reference the second vector.
2040 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2041 SDOperand Arg = N->getOperand(i);
2042 if (Arg.getOpcode() == ISD::UNDEF) continue;
2043 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002044 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002045 return false;
2046 }
2047
2048 return true;
2049}
2050
2051/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2052/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2053bool X86::isPSHUFHWMask(SDNode *N) {
2054 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2055
2056 if (N->getNumOperands() != 8)
2057 return false;
2058
2059 // Lower quadword copied in order.
2060 for (unsigned i = 0; i != 4; ++i) {
2061 SDOperand Arg = N->getOperand(i);
2062 if (Arg.getOpcode() == ISD::UNDEF) continue;
2063 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2064 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2065 return false;
2066 }
2067
2068 // Upper quadword shuffled.
2069 for (unsigned i = 4; i != 8; ++i) {
2070 SDOperand Arg = N->getOperand(i);
2071 if (Arg.getOpcode() == ISD::UNDEF) continue;
2072 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2073 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2074 if (Val < 4 || Val > 7)
2075 return false;
2076 }
2077
2078 return true;
2079}
2080
2081/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2082/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2083bool X86::isPSHUFLWMask(SDNode *N) {
2084 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2085
2086 if (N->getNumOperands() != 8)
2087 return false;
2088
2089 // Upper quadword copied in order.
2090 for (unsigned i = 4; i != 8; ++i)
2091 if (!isUndefOrEqual(N->getOperand(i), i))
2092 return false;
2093
2094 // Lower quadword shuffled.
2095 for (unsigned i = 0; i != 4; ++i)
2096 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2097 return false;
2098
2099 return true;
2100}
2101
2102/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2103/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2104static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2105 if (NumElems != 2 && NumElems != 4) return false;
2106
2107 unsigned Half = NumElems / 2;
2108 for (unsigned i = 0; i < Half; ++i)
2109 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2110 return false;
2111 for (unsigned i = Half; i < NumElems; ++i)
2112 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2113 return false;
2114
2115 return true;
2116}
2117
2118bool X86::isSHUFPMask(SDNode *N) {
2119 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2120 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2121}
2122
2123/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2124/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2125/// half elements to come from vector 1 (which would equal the dest.) and
2126/// the upper half to come from vector 2.
2127static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2128 if (NumOps != 2 && NumOps != 4) return false;
2129
2130 unsigned Half = NumOps / 2;
2131 for (unsigned i = 0; i < Half; ++i)
2132 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2133 return false;
2134 for (unsigned i = Half; i < NumOps; ++i)
2135 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2136 return false;
2137 return true;
2138}
2139
2140static bool isCommutedSHUFP(SDNode *N) {
2141 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2142 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2143}
2144
2145/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2146/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2147bool X86::isMOVHLPSMask(SDNode *N) {
2148 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2149
2150 if (N->getNumOperands() != 4)
2151 return false;
2152
2153 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2154 return isUndefOrEqual(N->getOperand(0), 6) &&
2155 isUndefOrEqual(N->getOperand(1), 7) &&
2156 isUndefOrEqual(N->getOperand(2), 2) &&
2157 isUndefOrEqual(N->getOperand(3), 3);
2158}
2159
2160/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2161/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2162/// <2, 3, 2, 3>
2163bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2164 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2165
2166 if (N->getNumOperands() != 4)
2167 return false;
2168
2169 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2170 return isUndefOrEqual(N->getOperand(0), 2) &&
2171 isUndefOrEqual(N->getOperand(1), 3) &&
2172 isUndefOrEqual(N->getOperand(2), 2) &&
2173 isUndefOrEqual(N->getOperand(3), 3);
2174}
2175
2176/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2177/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2178bool X86::isMOVLPMask(SDNode *N) {
2179 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2180
2181 unsigned NumElems = N->getNumOperands();
2182 if (NumElems != 2 && NumElems != 4)
2183 return false;
2184
2185 for (unsigned i = 0; i < NumElems/2; ++i)
2186 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2187 return false;
2188
2189 for (unsigned i = NumElems/2; i < NumElems; ++i)
2190 if (!isUndefOrEqual(N->getOperand(i), i))
2191 return false;
2192
2193 return true;
2194}
2195
2196/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2197/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2198/// and MOVLHPS.
2199bool X86::isMOVHPMask(SDNode *N) {
2200 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2201
2202 unsigned NumElems = N->getNumOperands();
2203 if (NumElems != 2 && NumElems != 4)
2204 return false;
2205
2206 for (unsigned i = 0; i < NumElems/2; ++i)
2207 if (!isUndefOrEqual(N->getOperand(i), i))
2208 return false;
2209
2210 for (unsigned i = 0; i < NumElems/2; ++i) {
2211 SDOperand Arg = N->getOperand(i + NumElems/2);
2212 if (!isUndefOrEqual(Arg, i + NumElems))
2213 return false;
2214 }
2215
2216 return true;
2217}
2218
2219/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2220/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2221bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2222 bool V2IsSplat = false) {
2223 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2224 return false;
2225
2226 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2227 SDOperand BitI = Elts[i];
2228 SDOperand BitI1 = Elts[i+1];
2229 if (!isUndefOrEqual(BitI, j))
2230 return false;
2231 if (V2IsSplat) {
2232 if (isUndefOrEqual(BitI1, NumElts))
2233 return false;
2234 } else {
2235 if (!isUndefOrEqual(BitI1, j + NumElts))
2236 return false;
2237 }
2238 }
2239
2240 return true;
2241}
2242
2243bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2244 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2245 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2246}
2247
2248/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2249/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2250bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2251 bool V2IsSplat = false) {
2252 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2253 return false;
2254
2255 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2256 SDOperand BitI = Elts[i];
2257 SDOperand BitI1 = Elts[i+1];
2258 if (!isUndefOrEqual(BitI, j + NumElts/2))
2259 return false;
2260 if (V2IsSplat) {
2261 if (isUndefOrEqual(BitI1, NumElts))
2262 return false;
2263 } else {
2264 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2265 return false;
2266 }
2267 }
2268
2269 return true;
2270}
2271
2272bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2273 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2274 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2275}
2276
2277/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2278/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2279/// <0, 0, 1, 1>
2280bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2281 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2282
2283 unsigned NumElems = N->getNumOperands();
2284 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2285 return false;
2286
2287 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2288 SDOperand BitI = N->getOperand(i);
2289 SDOperand BitI1 = N->getOperand(i+1);
2290
2291 if (!isUndefOrEqual(BitI, j))
2292 return false;
2293 if (!isUndefOrEqual(BitI1, j))
2294 return false;
2295 }
2296
2297 return true;
2298}
2299
2300/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2301/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2302/// <2, 2, 3, 3>
2303bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2304 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2305
2306 unsigned NumElems = N->getNumOperands();
2307 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2308 return false;
2309
2310 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2311 SDOperand BitI = N->getOperand(i);
2312 SDOperand BitI1 = N->getOperand(i + 1);
2313
2314 if (!isUndefOrEqual(BitI, j))
2315 return false;
2316 if (!isUndefOrEqual(BitI1, j))
2317 return false;
2318 }
2319
2320 return true;
2321}
2322
2323/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2324/// specifies a shuffle of elements that is suitable for input to MOVSS,
2325/// MOVSD, and MOVD, i.e. setting the lowest element.
2326static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002327 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002328 return false;
2329
2330 if (!isUndefOrEqual(Elts[0], NumElts))
2331 return false;
2332
2333 for (unsigned i = 1; i < NumElts; ++i) {
2334 if (!isUndefOrEqual(Elts[i], i))
2335 return false;
2336 }
2337
2338 return true;
2339}
2340
2341bool X86::isMOVLMask(SDNode *N) {
2342 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2343 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2344}
2345
2346/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2347/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2348/// element of vector 2 and the other elements to come from vector 1 in order.
2349static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2350 bool V2IsSplat = false,
2351 bool V2IsUndef = false) {
2352 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2353 return false;
2354
2355 if (!isUndefOrEqual(Ops[0], 0))
2356 return false;
2357
2358 for (unsigned i = 1; i < NumOps; ++i) {
2359 SDOperand Arg = Ops[i];
2360 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2361 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2362 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2363 return false;
2364 }
2365
2366 return true;
2367}
2368
2369static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2370 bool V2IsUndef = false) {
2371 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2372 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2373 V2IsSplat, V2IsUndef);
2374}
2375
2376/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2377/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2378bool X86::isMOVSHDUPMask(SDNode *N) {
2379 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2380
2381 if (N->getNumOperands() != 4)
2382 return false;
2383
2384 // Expect 1, 1, 3, 3
2385 for (unsigned i = 0; i < 2; ++i) {
2386 SDOperand Arg = N->getOperand(i);
2387 if (Arg.getOpcode() == ISD::UNDEF) continue;
2388 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2389 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2390 if (Val != 1) return false;
2391 }
2392
2393 bool HasHi = false;
2394 for (unsigned i = 2; i < 4; ++i) {
2395 SDOperand Arg = N->getOperand(i);
2396 if (Arg.getOpcode() == ISD::UNDEF) continue;
2397 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2398 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2399 if (Val != 3) return false;
2400 HasHi = true;
2401 }
2402
2403 // Don't use movshdup if it can be done with a shufps.
2404 return HasHi;
2405}
2406
2407/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2408/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2409bool X86::isMOVSLDUPMask(SDNode *N) {
2410 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2411
2412 if (N->getNumOperands() != 4)
2413 return false;
2414
2415 // Expect 0, 0, 2, 2
2416 for (unsigned i = 0; i < 2; ++i) {
2417 SDOperand Arg = N->getOperand(i);
2418 if (Arg.getOpcode() == ISD::UNDEF) continue;
2419 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2420 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2421 if (Val != 0) return false;
2422 }
2423
2424 bool HasHi = false;
2425 for (unsigned i = 2; i < 4; ++i) {
2426 SDOperand Arg = N->getOperand(i);
2427 if (Arg.getOpcode() == ISD::UNDEF) continue;
2428 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2429 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2430 if (Val != 2) return false;
2431 HasHi = true;
2432 }
2433
2434 // Don't use movshdup if it can be done with a shufps.
2435 return HasHi;
2436}
2437
2438/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2439/// specifies a identity operation on the LHS or RHS.
2440static bool isIdentityMask(SDNode *N, bool RHS = false) {
2441 unsigned NumElems = N->getNumOperands();
2442 for (unsigned i = 0; i < NumElems; ++i)
2443 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2444 return false;
2445 return true;
2446}
2447
2448/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2449/// a splat of a single element.
2450static bool isSplatMask(SDNode *N) {
2451 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2452
2453 // This is a splat operation if each element of the permute is the same, and
2454 // if the value doesn't reference the second vector.
2455 unsigned NumElems = N->getNumOperands();
2456 SDOperand ElementBase;
2457 unsigned i = 0;
2458 for (; i != NumElems; ++i) {
2459 SDOperand Elt = N->getOperand(i);
2460 if (isa<ConstantSDNode>(Elt)) {
2461 ElementBase = Elt;
2462 break;
2463 }
2464 }
2465
2466 if (!ElementBase.Val)
2467 return false;
2468
2469 for (; i != NumElems; ++i) {
2470 SDOperand Arg = N->getOperand(i);
2471 if (Arg.getOpcode() == ISD::UNDEF) continue;
2472 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2473 if (Arg != ElementBase) return false;
2474 }
2475
2476 // Make sure it is a splat of the first vector operand.
2477 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2478}
2479
2480/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2481/// a splat of a single element and it's a 2 or 4 element mask.
2482bool X86::isSplatMask(SDNode *N) {
2483 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2484
2485 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2486 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2487 return false;
2488 return ::isSplatMask(N);
2489}
2490
2491/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2492/// specifies a splat of zero element.
2493bool X86::isSplatLoMask(SDNode *N) {
2494 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2495
2496 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2497 if (!isUndefOrEqual(N->getOperand(i), 0))
2498 return false;
2499 return true;
2500}
2501
2502/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2503/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2504/// instructions.
2505unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2506 unsigned NumOperands = N->getNumOperands();
2507 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2508 unsigned Mask = 0;
2509 for (unsigned i = 0; i < NumOperands; ++i) {
2510 unsigned Val = 0;
2511 SDOperand Arg = N->getOperand(NumOperands-i-1);
2512 if (Arg.getOpcode() != ISD::UNDEF)
2513 Val = cast<ConstantSDNode>(Arg)->getValue();
2514 if (Val >= NumOperands) Val -= NumOperands;
2515 Mask |= Val;
2516 if (i != NumOperands - 1)
2517 Mask <<= Shift;
2518 }
2519
2520 return Mask;
2521}
2522
2523/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2524/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2525/// instructions.
2526unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2527 unsigned Mask = 0;
2528 // 8 nodes, but we only care about the last 4.
2529 for (unsigned i = 7; i >= 4; --i) {
2530 unsigned Val = 0;
2531 SDOperand Arg = N->getOperand(i);
2532 if (Arg.getOpcode() != ISD::UNDEF)
2533 Val = cast<ConstantSDNode>(Arg)->getValue();
2534 Mask |= (Val - 4);
2535 if (i != 4)
2536 Mask <<= 2;
2537 }
2538
2539 return Mask;
2540}
2541
2542/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2543/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2544/// instructions.
2545unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2546 unsigned Mask = 0;
2547 // 8 nodes, but we only care about the first 4.
2548 for (int i = 3; i >= 0; --i) {
2549 unsigned Val = 0;
2550 SDOperand Arg = N->getOperand(i);
2551 if (Arg.getOpcode() != ISD::UNDEF)
2552 Val = cast<ConstantSDNode>(Arg)->getValue();
2553 Mask |= Val;
2554 if (i != 0)
2555 Mask <<= 2;
2556 }
2557
2558 return Mask;
2559}
2560
2561/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2562/// specifies a 8 element shuffle that can be broken into a pair of
2563/// PSHUFHW and PSHUFLW.
2564static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2565 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2566
2567 if (N->getNumOperands() != 8)
2568 return false;
2569
2570 // Lower quadword shuffled.
2571 for (unsigned i = 0; i != 4; ++i) {
2572 SDOperand Arg = N->getOperand(i);
2573 if (Arg.getOpcode() == ISD::UNDEF) continue;
2574 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2575 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002576 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002577 return false;
2578 }
2579
2580 // Upper quadword shuffled.
2581 for (unsigned i = 4; i != 8; ++i) {
2582 SDOperand Arg = N->getOperand(i);
2583 if (Arg.getOpcode() == ISD::UNDEF) continue;
2584 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2585 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2586 if (Val < 4 || Val > 7)
2587 return false;
2588 }
2589
2590 return true;
2591}
2592
Chris Lattnere6aa3862007-11-25 00:24:49 +00002593/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594/// values in ther permute mask.
2595static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2596 SDOperand &V2, SDOperand &Mask,
2597 SelectionDAG &DAG) {
2598 MVT::ValueType VT = Op.getValueType();
2599 MVT::ValueType MaskVT = Mask.getValueType();
2600 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2601 unsigned NumElems = Mask.getNumOperands();
2602 SmallVector<SDOperand, 8> MaskVec;
2603
2604 for (unsigned i = 0; i != NumElems; ++i) {
2605 SDOperand Arg = Mask.getOperand(i);
2606 if (Arg.getOpcode() == ISD::UNDEF) {
2607 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2608 continue;
2609 }
2610 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2611 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2612 if (Val < NumElems)
2613 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2614 else
2615 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2616 }
2617
2618 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002619 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2621}
2622
Evan Chenga6769df2007-12-07 21:30:01 +00002623/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2624/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002625static
2626SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2627 MVT::ValueType MaskVT = Mask.getValueType();
2628 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2629 unsigned NumElems = Mask.getNumOperands();
2630 SmallVector<SDOperand, 8> MaskVec;
2631 for (unsigned i = 0; i != NumElems; ++i) {
2632 SDOperand Arg = Mask.getOperand(i);
2633 if (Arg.getOpcode() == ISD::UNDEF) {
2634 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2635 continue;
2636 }
2637 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2638 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2639 if (Val < NumElems)
2640 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2641 else
2642 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2643 }
2644 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2645}
2646
2647
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2649/// match movhlps. The lower half elements should come from upper half of
2650/// V1 (and in order), and the upper half elements should come from the upper
2651/// half of V2 (and in order).
2652static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2653 unsigned NumElems = Mask->getNumOperands();
2654 if (NumElems != 4)
2655 return false;
2656 for (unsigned i = 0, e = 2; i != e; ++i)
2657 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2658 return false;
2659 for (unsigned i = 2; i != 4; ++i)
2660 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2661 return false;
2662 return true;
2663}
2664
2665/// isScalarLoadToVector - Returns true if the node is a scalar load that
2666/// is promoted to a vector.
2667static inline bool isScalarLoadToVector(SDNode *N) {
2668 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2669 N = N->getOperand(0).Val;
2670 return ISD::isNON_EXTLoad(N);
2671 }
2672 return false;
2673}
2674
2675/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2676/// match movlp{s|d}. The lower half elements should come from lower half of
2677/// V1 (and in order), and the upper half elements should come from the upper
2678/// half of V2 (and in order). And since V1 will become the source of the
2679/// MOVLP, it must be either a vector load or a scalar load to vector.
2680static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2681 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2682 return false;
2683 // Is V2 is a vector load, don't do this transformation. We will try to use
2684 // load folding shufps op.
2685 if (ISD::isNON_EXTLoad(V2))
2686 return false;
2687
2688 unsigned NumElems = Mask->getNumOperands();
2689 if (NumElems != 2 && NumElems != 4)
2690 return false;
2691 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2692 if (!isUndefOrEqual(Mask->getOperand(i), i))
2693 return false;
2694 for (unsigned i = NumElems/2; i != NumElems; ++i)
2695 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2696 return false;
2697 return true;
2698}
2699
2700/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2701/// all the same.
2702static bool isSplatVector(SDNode *N) {
2703 if (N->getOpcode() != ISD::BUILD_VECTOR)
2704 return false;
2705
2706 SDOperand SplatValue = N->getOperand(0);
2707 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2708 if (N->getOperand(i) != SplatValue)
2709 return false;
2710 return true;
2711}
2712
2713/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2714/// to an undef.
2715static bool isUndefShuffle(SDNode *N) {
2716 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2717 return false;
2718
2719 SDOperand V1 = N->getOperand(0);
2720 SDOperand V2 = N->getOperand(1);
2721 SDOperand Mask = N->getOperand(2);
2722 unsigned NumElems = Mask.getNumOperands();
2723 for (unsigned i = 0; i != NumElems; ++i) {
2724 SDOperand Arg = Mask.getOperand(i);
2725 if (Arg.getOpcode() != ISD::UNDEF) {
2726 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2727 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2728 return false;
2729 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2730 return false;
2731 }
2732 }
2733 return true;
2734}
2735
2736/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2737/// constant +0.0.
2738static inline bool isZeroNode(SDOperand Elt) {
2739 return ((isa<ConstantSDNode>(Elt) &&
2740 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2741 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002742 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002743}
2744
2745/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2746/// to an zero vector.
2747static bool isZeroShuffle(SDNode *N) {
2748 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2749 return false;
2750
2751 SDOperand V1 = N->getOperand(0);
2752 SDOperand V2 = N->getOperand(1);
2753 SDOperand Mask = N->getOperand(2);
2754 unsigned NumElems = Mask.getNumOperands();
2755 for (unsigned i = 0; i != NumElems; ++i) {
2756 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002757 if (Arg.getOpcode() == ISD::UNDEF)
2758 continue;
2759
2760 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2761 if (Idx < NumElems) {
2762 unsigned Opc = V1.Val->getOpcode();
2763 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2764 continue;
2765 if (Opc != ISD::BUILD_VECTOR ||
2766 !isZeroNode(V1.Val->getOperand(Idx)))
2767 return false;
2768 } else if (Idx >= NumElems) {
2769 unsigned Opc = V2.Val->getOpcode();
2770 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2771 continue;
2772 if (Opc != ISD::BUILD_VECTOR ||
2773 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2774 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002775 }
2776 }
2777 return true;
2778}
2779
2780/// getZeroVector - Returns a vector of specified type with all zero elements.
2781///
2782static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2783 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002784
2785 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2786 // type. This ensures they get CSE'd.
2787 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2788 SDOperand Vec;
2789 if (MVT::getSizeInBits(VT) == 64) // MMX
2790 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2791 else // SSE
2792 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2793 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002794}
2795
Chris Lattnere6aa3862007-11-25 00:24:49 +00002796/// getOnesVector - Returns a vector of specified type with all bits set.
2797///
2798static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2799 assert(MVT::isVector(VT) && "Expected a vector type");
2800
2801 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2802 // type. This ensures they get CSE'd.
2803 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2804 SDOperand Vec;
2805 if (MVT::getSizeInBits(VT) == 64) // MMX
2806 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2807 else // SSE
2808 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2809 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2810}
2811
2812
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2814/// that point to V2 points to its first element.
2815static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2816 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2817
2818 bool Changed = false;
2819 SmallVector<SDOperand, 8> MaskVec;
2820 unsigned NumElems = Mask.getNumOperands();
2821 for (unsigned i = 0; i != NumElems; ++i) {
2822 SDOperand Arg = Mask.getOperand(i);
2823 if (Arg.getOpcode() != ISD::UNDEF) {
2824 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2825 if (Val > NumElems) {
2826 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2827 Changed = true;
2828 }
2829 }
2830 MaskVec.push_back(Arg);
2831 }
2832
2833 if (Changed)
2834 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2835 &MaskVec[0], MaskVec.size());
2836 return Mask;
2837}
2838
2839/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2840/// operation of specified width.
2841static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2842 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2843 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2844
2845 SmallVector<SDOperand, 8> MaskVec;
2846 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2847 for (unsigned i = 1; i != NumElems; ++i)
2848 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2849 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2850}
2851
2852/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2853/// of specified width.
2854static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2855 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2856 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2857 SmallVector<SDOperand, 8> MaskVec;
2858 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2859 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2860 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2861 }
2862 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2863}
2864
2865/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2866/// of specified width.
2867static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2868 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2869 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2870 unsigned Half = NumElems/2;
2871 SmallVector<SDOperand, 8> MaskVec;
2872 for (unsigned i = 0; i != Half; ++i) {
2873 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2874 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2875 }
2876 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2877}
2878
2879/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2880///
2881static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2882 SDOperand V1 = Op.getOperand(0);
2883 SDOperand Mask = Op.getOperand(2);
2884 MVT::ValueType VT = Op.getValueType();
2885 unsigned NumElems = Mask.getNumOperands();
2886 Mask = getUnpacklMask(NumElems, DAG);
2887 while (NumElems != 4) {
2888 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2889 NumElems >>= 1;
2890 }
2891 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2892
Chris Lattnere6aa3862007-11-25 00:24:49 +00002893 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2895 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2896 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2897}
2898
2899/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002900/// vector of zero or undef vector. This produces a shuffle where the low
2901/// element of V2 is swizzled into the zero/undef vector, landing at element
2902/// 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 +00002903static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2904 unsigned NumElems, unsigned Idx,
2905 bool isZero, SelectionDAG &DAG) {
2906 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2907 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2908 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002909 SmallVector<SDOperand, 16> MaskVec;
2910 for (unsigned i = 0; i != NumElems; ++i)
2911 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2912 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2913 else
2914 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2916 &MaskVec[0], MaskVec.size());
2917 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2918}
2919
2920/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2921///
2922static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2923 unsigned NumNonZero, unsigned NumZero,
2924 SelectionDAG &DAG, TargetLowering &TLI) {
2925 if (NumNonZero > 8)
2926 return SDOperand();
2927
2928 SDOperand V(0, 0);
2929 bool First = true;
2930 for (unsigned i = 0; i < 16; ++i) {
2931 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2932 if (ThisIsNonZero && First) {
2933 if (NumZero)
2934 V = getZeroVector(MVT::v8i16, DAG);
2935 else
2936 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2937 First = false;
2938 }
2939
2940 if ((i & 1) != 0) {
2941 SDOperand ThisElt(0, 0), LastElt(0, 0);
2942 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2943 if (LastIsNonZero) {
2944 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2945 }
2946 if (ThisIsNonZero) {
2947 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2948 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2949 ThisElt, DAG.getConstant(8, MVT::i8));
2950 if (LastIsNonZero)
2951 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2952 } else
2953 ThisElt = LastElt;
2954
2955 if (ThisElt.Val)
2956 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002957 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 }
2959 }
2960
2961 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2962}
2963
2964/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2965///
2966static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2967 unsigned NumNonZero, unsigned NumZero,
2968 SelectionDAG &DAG, TargetLowering &TLI) {
2969 if (NumNonZero > 4)
2970 return SDOperand();
2971
2972 SDOperand V(0, 0);
2973 bool First = true;
2974 for (unsigned i = 0; i < 8; ++i) {
2975 bool isNonZero = (NonZeros & (1 << i)) != 0;
2976 if (isNonZero) {
2977 if (First) {
2978 if (NumZero)
2979 V = getZeroVector(MVT::v8i16, DAG);
2980 else
2981 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2982 First = false;
2983 }
2984 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002985 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 }
2987 }
2988
2989 return V;
2990}
2991
2992SDOperand
2993X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002994 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2995 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2996 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2997 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2998 // eliminated on x86-32 hosts.
2999 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3000 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001
Chris Lattnere6aa3862007-11-25 00:24:49 +00003002 if (ISD::isBuildVectorAllOnes(Op.Val))
3003 return getOnesVector(Op.getValueType(), DAG);
3004 return getZeroVector(Op.getValueType(), DAG);
3005 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006
3007 MVT::ValueType VT = Op.getValueType();
3008 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3009 unsigned EVTBits = MVT::getSizeInBits(EVT);
3010
3011 unsigned NumElems = Op.getNumOperands();
3012 unsigned NumZero = 0;
3013 unsigned NumNonZero = 0;
3014 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00003015 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00003016 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 for (unsigned i = 0; i < NumElems; ++i) {
3018 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003019 if (Elt.getOpcode() == ISD::UNDEF)
3020 continue;
3021 Values.insert(Elt);
3022 if (Elt.getOpcode() != ISD::Constant &&
3023 Elt.getOpcode() != ISD::ConstantFP)
3024 HasNonImms = true;
3025 if (isZeroNode(Elt))
3026 NumZero++;
3027 else {
3028 NonZeros |= (1 << i);
3029 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003030 }
3031 }
3032
3033 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003034 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3035 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003036 }
3037
3038 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3039 if (Values.size() == 1)
3040 return SDOperand();
3041
3042 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00003043 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 unsigned Idx = CountTrailingZeros_32(NonZeros);
3045 SDOperand Item = Op.getOperand(Idx);
3046 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3047 if (Idx == 0)
3048 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3049 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3050 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00003051 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
3052 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003053
3054 if (EVTBits == 32) {
3055 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3056 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3057 DAG);
3058 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3059 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3060 SmallVector<SDOperand, 8> MaskVec;
3061 for (unsigned i = 0; i < NumElems; i++)
3062 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3063 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3064 &MaskVec[0], MaskVec.size());
3065 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3066 DAG.getNode(ISD::UNDEF, VT), Mask);
3067 }
3068 }
3069
Dan Gohman21463242007-07-24 22:55:08 +00003070 // A vector full of immediates; various special cases are already
3071 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00003072 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00003073 return SDOperand();
3074
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003075 // Let legalizer expand 2-wide build_vectors.
3076 if (EVTBits == 64)
3077 return SDOperand();
3078
3079 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3080 if (EVTBits == 8 && NumElems == 16) {
3081 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3082 *this);
3083 if (V.Val) return V;
3084 }
3085
3086 if (EVTBits == 16 && NumElems == 8) {
3087 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3088 *this);
3089 if (V.Val) return V;
3090 }
3091
3092 // If element VT is == 32 bits, turn it into a number of shuffles.
3093 SmallVector<SDOperand, 8> V;
3094 V.resize(NumElems);
3095 if (NumElems == 4 && NumZero > 0) {
3096 for (unsigned i = 0; i < 4; ++i) {
3097 bool isZero = !(NonZeros & (1 << i));
3098 if (isZero)
3099 V[i] = getZeroVector(VT, DAG);
3100 else
3101 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3102 }
3103
3104 for (unsigned i = 0; i < 2; ++i) {
3105 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3106 default: break;
3107 case 0:
3108 V[i] = V[i*2]; // Must be a zero vector.
3109 break;
3110 case 1:
3111 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3112 getMOVLMask(NumElems, DAG));
3113 break;
3114 case 2:
3115 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3116 getMOVLMask(NumElems, DAG));
3117 break;
3118 case 3:
3119 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3120 getUnpacklMask(NumElems, DAG));
3121 break;
3122 }
3123 }
3124
3125 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3126 // clears the upper bits.
3127 // FIXME: we can do the same for v4f32 case when we know both parts of
3128 // the lower half come from scalar_to_vector (loadf32). We should do
3129 // that in post legalizer dag combiner with target specific hooks.
3130 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3131 return V[0];
3132 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3133 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3134 SmallVector<SDOperand, 8> MaskVec;
3135 bool Reverse = (NonZeros & 0x3) == 2;
3136 for (unsigned i = 0; i < 2; ++i)
3137 if (Reverse)
3138 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3139 else
3140 MaskVec.push_back(DAG.getConstant(i, EVT));
3141 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3142 for (unsigned i = 0; i < 2; ++i)
3143 if (Reverse)
3144 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3145 else
3146 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3147 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3148 &MaskVec[0], MaskVec.size());
3149 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3150 }
3151
3152 if (Values.size() > 2) {
3153 // Expand into a number of unpckl*.
3154 // e.g. for v4f32
3155 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3156 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3157 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3158 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3159 for (unsigned i = 0; i < NumElems; ++i)
3160 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3161 NumElems >>= 1;
3162 while (NumElems != 0) {
3163 for (unsigned i = 0; i < NumElems; ++i)
3164 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3165 UnpckMask);
3166 NumElems >>= 1;
3167 }
3168 return V[0];
3169 }
3170
3171 return SDOperand();
3172}
3173
Evan Chengfca29242007-12-07 08:07:39 +00003174static
3175SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3176 SDOperand PermMask, SelectionDAG &DAG,
3177 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003178 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003179 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3180 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003181 MVT::ValueType PtrVT = TLI.getPointerTy();
3182 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3183 PermMask.Val->op_end());
3184
3185 // First record which half of which vector the low elements come from.
3186 SmallVector<unsigned, 4> LowQuad(4);
3187 for (unsigned i = 0; i < 4; ++i) {
3188 SDOperand Elt = MaskElts[i];
3189 if (Elt.getOpcode() == ISD::UNDEF)
3190 continue;
3191 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3192 int QuadIdx = EltIdx / 4;
3193 ++LowQuad[QuadIdx];
3194 }
3195 int BestLowQuad = -1;
3196 unsigned MaxQuad = 1;
3197 for (unsigned i = 0; i < 4; ++i) {
3198 if (LowQuad[i] > MaxQuad) {
3199 BestLowQuad = i;
3200 MaxQuad = LowQuad[i];
3201 }
Evan Chengfca29242007-12-07 08:07:39 +00003202 }
3203
Evan Cheng75184a92007-12-11 01:46:18 +00003204 // Record which half of which vector the high elements come from.
3205 SmallVector<unsigned, 4> HighQuad(4);
3206 for (unsigned i = 4; i < 8; ++i) {
3207 SDOperand Elt = MaskElts[i];
3208 if (Elt.getOpcode() == ISD::UNDEF)
3209 continue;
3210 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3211 int QuadIdx = EltIdx / 4;
3212 ++HighQuad[QuadIdx];
3213 }
3214 int BestHighQuad = -1;
3215 MaxQuad = 1;
3216 for (unsigned i = 0; i < 4; ++i) {
3217 if (HighQuad[i] > MaxQuad) {
3218 BestHighQuad = i;
3219 MaxQuad = HighQuad[i];
3220 }
3221 }
3222
3223 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3224 if (BestLowQuad != -1 || BestHighQuad != -1) {
3225 // First sort the 4 chunks in order using shufpd.
3226 SmallVector<SDOperand, 8> MaskVec;
3227 if (BestLowQuad != -1)
3228 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3229 else
3230 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3231 if (BestHighQuad != -1)
3232 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3233 else
3234 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3235 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3236 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3237 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3238 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3239 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3240
3241 // Now sort high and low parts separately.
3242 BitVector InOrder(8);
3243 if (BestLowQuad != -1) {
3244 // Sort lower half in order using PSHUFLW.
3245 MaskVec.clear();
3246 bool AnyOutOrder = false;
3247 for (unsigned i = 0; i != 4; ++i) {
3248 SDOperand Elt = MaskElts[i];
3249 if (Elt.getOpcode() == ISD::UNDEF) {
3250 MaskVec.push_back(Elt);
3251 InOrder.set(i);
3252 } else {
3253 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3254 if (EltIdx != i)
3255 AnyOutOrder = true;
3256 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3257 // If this element is in the right place after this shuffle, then
3258 // remember it.
3259 if ((int)(EltIdx / 4) == BestLowQuad)
3260 InOrder.set(i);
3261 }
3262 }
3263 if (AnyOutOrder) {
3264 for (unsigned i = 4; i != 8; ++i)
3265 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3266 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3267 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3268 }
3269 }
3270
3271 if (BestHighQuad != -1) {
3272 // Sort high half in order using PSHUFHW if possible.
3273 MaskVec.clear();
3274 for (unsigned i = 0; i != 4; ++i)
3275 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3276 bool AnyOutOrder = false;
3277 for (unsigned i = 4; i != 8; ++i) {
3278 SDOperand Elt = MaskElts[i];
3279 if (Elt.getOpcode() == ISD::UNDEF) {
3280 MaskVec.push_back(Elt);
3281 InOrder.set(i);
3282 } else {
3283 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3284 if (EltIdx != i)
3285 AnyOutOrder = true;
3286 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3287 // If this element is in the right place after this shuffle, then
3288 // remember it.
3289 if ((int)(EltIdx / 4) == BestHighQuad)
3290 InOrder.set(i);
3291 }
3292 }
3293 if (AnyOutOrder) {
3294 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3295 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3296 }
3297 }
3298
3299 // The other elements are put in the right place using pextrw and pinsrw.
3300 for (unsigned i = 0; i != 8; ++i) {
3301 if (InOrder[i])
3302 continue;
3303 SDOperand Elt = MaskElts[i];
3304 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3305 if (EltIdx == i)
3306 continue;
3307 SDOperand ExtOp = (EltIdx < 8)
3308 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3309 DAG.getConstant(EltIdx, PtrVT))
3310 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3311 DAG.getConstant(EltIdx - 8, PtrVT));
3312 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3313 DAG.getConstant(i, PtrVT));
3314 }
3315 return NewV;
3316 }
3317
3318 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3319 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003320 // First, let's find out how many elements are already in the right order.
3321 unsigned V1InOrder = 0;
3322 unsigned V1FromV1 = 0;
3323 unsigned V2InOrder = 0;
3324 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003325 SmallVector<SDOperand, 8> V1Elts;
3326 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003327 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003328 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003329 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003330 V1Elts.push_back(Elt);
3331 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003332 ++V1InOrder;
3333 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003334 continue;
3335 }
3336 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3337 if (EltIdx == i) {
3338 V1Elts.push_back(Elt);
3339 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3340 ++V1InOrder;
3341 } else if (EltIdx == i+8) {
3342 V1Elts.push_back(Elt);
3343 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3344 ++V2InOrder;
3345 } else if (EltIdx < 8) {
3346 V1Elts.push_back(Elt);
3347 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003348 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003349 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3350 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003351 }
3352 }
3353
3354 if (V2InOrder > V1InOrder) {
3355 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3356 std::swap(V1, V2);
3357 std::swap(V1Elts, V2Elts);
3358 std::swap(V1FromV1, V2FromV2);
3359 }
3360
Evan Cheng75184a92007-12-11 01:46:18 +00003361 if ((V1FromV1 + V1InOrder) != 8) {
3362 // Some elements are from V2.
3363 if (V1FromV1) {
3364 // If there are elements that are from V1 but out of place,
3365 // then first sort them in place
3366 SmallVector<SDOperand, 8> MaskVec;
3367 for (unsigned i = 0; i < 8; ++i) {
3368 SDOperand Elt = V1Elts[i];
3369 if (Elt.getOpcode() == ISD::UNDEF) {
3370 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3371 continue;
3372 }
3373 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3374 if (EltIdx >= 8)
3375 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3376 else
3377 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3378 }
3379 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3380 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003381 }
Evan Cheng75184a92007-12-11 01:46:18 +00003382
3383 NewV = V1;
3384 for (unsigned i = 0; i < 8; ++i) {
3385 SDOperand Elt = V1Elts[i];
3386 if (Elt.getOpcode() == ISD::UNDEF)
3387 continue;
3388 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3389 if (EltIdx < 8)
3390 continue;
3391 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3392 DAG.getConstant(EltIdx - 8, PtrVT));
3393 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3394 DAG.getConstant(i, PtrVT));
3395 }
3396 return NewV;
3397 } else {
3398 // All elements are from V1.
3399 NewV = V1;
3400 for (unsigned i = 0; i < 8; ++i) {
3401 SDOperand Elt = V1Elts[i];
3402 if (Elt.getOpcode() == ISD::UNDEF)
3403 continue;
3404 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3405 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3406 DAG.getConstant(EltIdx, PtrVT));
3407 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3408 DAG.getConstant(i, PtrVT));
3409 }
3410 return NewV;
3411 }
3412}
3413
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003414/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3415/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3416/// done when every pair / quad of shuffle mask elements point to elements in
3417/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003418/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3419static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003420SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3421 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003422 SDOperand PermMask, SelectionDAG &DAG,
3423 TargetLowering &TLI) {
3424 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003425 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3426 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3427 MVT::ValueType NewVT = MaskVT;
3428 switch (VT) {
3429 case MVT::v4f32: NewVT = MVT::v2f64; break;
3430 case MVT::v4i32: NewVT = MVT::v2i64; break;
3431 case MVT::v8i16: NewVT = MVT::v4i32; break;
3432 case MVT::v16i8: NewVT = MVT::v4i32; break;
3433 default: assert(false && "Unexpected!");
3434 }
3435
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003436 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003437 if (MVT::isInteger(VT))
3438 NewVT = MVT::v2i64;
3439 else
3440 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003441 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003442 unsigned Scale = NumElems / NewWidth;
3443 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003444 for (unsigned i = 0; i < NumElems; i += Scale) {
3445 unsigned StartIdx = ~0U;
3446 for (unsigned j = 0; j < Scale; ++j) {
3447 SDOperand Elt = PermMask.getOperand(i+j);
3448 if (Elt.getOpcode() == ISD::UNDEF)
3449 continue;
3450 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3451 if (StartIdx == ~0U)
3452 StartIdx = EltIdx - (EltIdx % Scale);
3453 if (EltIdx != StartIdx + j)
3454 return SDOperand();
3455 }
3456 if (StartIdx == ~0U)
3457 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3458 else
3459 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003460 }
3461
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003462 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3463 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3464 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3465 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3466 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003467}
3468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003469SDOperand
3470X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3471 SDOperand V1 = Op.getOperand(0);
3472 SDOperand V2 = Op.getOperand(1);
3473 SDOperand PermMask = Op.getOperand(2);
3474 MVT::ValueType VT = Op.getValueType();
3475 unsigned NumElems = PermMask.getNumOperands();
3476 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3477 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3478 bool V1IsSplat = false;
3479 bool V2IsSplat = false;
3480
3481 if (isUndefShuffle(Op.Val))
3482 return DAG.getNode(ISD::UNDEF, VT);
3483
3484 if (isZeroShuffle(Op.Val))
3485 return getZeroVector(VT, DAG);
3486
3487 if (isIdentityMask(PermMask.Val))
3488 return V1;
3489 else if (isIdentityMask(PermMask.Val, true))
3490 return V2;
3491
3492 if (isSplatMask(PermMask.Val)) {
3493 if (NumElems <= 4) return Op;
3494 // Promote it to a v4i32 splat.
3495 return PromoteSplat(Op, DAG);
3496 }
3497
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003498 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3499 // do it!
3500 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3501 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3502 if (NewOp.Val)
3503 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3504 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3505 // FIXME: Figure out a cleaner way to do this.
3506 // Try to make use of movq to zero out the top part.
3507 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3508 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3509 if (NewOp.Val) {
3510 SDOperand NewV1 = NewOp.getOperand(0);
3511 SDOperand NewV2 = NewOp.getOperand(1);
3512 SDOperand NewMask = NewOp.getOperand(2);
3513 if (isCommutedMOVL(NewMask.Val, true, false)) {
3514 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3515 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3516 NewV1, NewV2, getMOVLMask(2, DAG));
3517 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3518 }
3519 }
3520 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3521 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3522 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3523 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3524 }
3525 }
3526
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003527 if (X86::isMOVLMask(PermMask.Val))
3528 return (V1IsUndef) ? V2 : Op;
3529
3530 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3531 X86::isMOVSLDUPMask(PermMask.Val) ||
3532 X86::isMOVHLPSMask(PermMask.Val) ||
3533 X86::isMOVHPMask(PermMask.Val) ||
3534 X86::isMOVLPMask(PermMask.Val))
3535 return Op;
3536
3537 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3538 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3539 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3540
3541 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003542 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3543 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003544 V1IsSplat = isSplatVector(V1.Val);
3545 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003546
3547 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003548 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3549 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3550 std::swap(V1IsSplat, V2IsSplat);
3551 std::swap(V1IsUndef, V2IsUndef);
3552 Commuted = true;
3553 }
3554
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003555 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003556 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3557 if (V2IsUndef) return V1;
3558 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3559 if (V2IsSplat) {
3560 // V2 is a splat, so the mask may be malformed. That is, it may point
3561 // to any V2 element. The instruction selectior won't like this. Get
3562 // a corrected mask and commute to form a proper MOVS{S|D}.
3563 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3564 if (NewMask.Val != PermMask.Val)
3565 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3566 }
3567 return Op;
3568 }
3569
3570 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3571 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3572 X86::isUNPCKLMask(PermMask.Val) ||
3573 X86::isUNPCKHMask(PermMask.Val))
3574 return Op;
3575
3576 if (V2IsSplat) {
3577 // Normalize mask so all entries that point to V2 points to its first
3578 // element then try to match unpck{h|l} again. If match, return a
3579 // new vector_shuffle with the corrected mask.
3580 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3581 if (NewMask.Val != PermMask.Val) {
3582 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3583 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3584 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3585 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3586 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3587 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3588 }
3589 }
3590 }
3591
3592 // Normalize the node to match x86 shuffle ops if needed
3593 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3594 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3595
3596 if (Commuted) {
3597 // Commute is back and try unpck* again.
3598 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3599 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3600 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3601 X86::isUNPCKLMask(PermMask.Val) ||
3602 X86::isUNPCKHMask(PermMask.Val))
3603 return Op;
3604 }
3605
3606 // If VT is integer, try PSHUF* first, then SHUFP*.
3607 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003608 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3609 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3610 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3611 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003612 X86::isPSHUFHWMask(PermMask.Val) ||
3613 X86::isPSHUFLWMask(PermMask.Val)) {
3614 if (V2.getOpcode() != ISD::UNDEF)
3615 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3616 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3617 return Op;
3618 }
3619
3620 if (X86::isSHUFPMask(PermMask.Val) &&
3621 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3622 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003623 } else {
3624 // Floating point cases in the other order.
3625 if (X86::isSHUFPMask(PermMask.Val))
3626 return Op;
3627 if (X86::isPSHUFDMask(PermMask.Val) ||
3628 X86::isPSHUFHWMask(PermMask.Val) ||
3629 X86::isPSHUFLWMask(PermMask.Val)) {
3630 if (V2.getOpcode() != ISD::UNDEF)
3631 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3632 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3633 return Op;
3634 }
3635 }
3636
Evan Cheng75184a92007-12-11 01:46:18 +00003637 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3638 if (VT == MVT::v8i16) {
3639 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3640 if (NewOp.Val)
3641 return NewOp;
3642 }
3643
3644 // Handle all 4 wide cases with a number of shuffles.
3645 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003646 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003647 MVT::ValueType MaskVT = PermMask.getValueType();
3648 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3649 SmallVector<std::pair<int, int>, 8> Locs;
3650 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003651 SmallVector<SDOperand, 8> Mask1(NumElems,
3652 DAG.getNode(ISD::UNDEF, MaskEVT));
3653 SmallVector<SDOperand, 8> Mask2(NumElems,
3654 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003655 unsigned NumHi = 0;
3656 unsigned NumLo = 0;
3657 // If no more than two elements come from either vector. This can be
3658 // implemented with two shuffles. First shuffle gather the elements.
3659 // The second shuffle, which takes the first shuffle as both of its
3660 // vector operands, put the elements into the right order.
3661 for (unsigned i = 0; i != NumElems; ++i) {
3662 SDOperand Elt = PermMask.getOperand(i);
3663 if (Elt.getOpcode() == ISD::UNDEF) {
3664 Locs[i] = std::make_pair(-1, -1);
3665 } else {
3666 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3667 if (Val < NumElems) {
3668 Locs[i] = std::make_pair(0, NumLo);
3669 Mask1[NumLo] = Elt;
3670 NumLo++;
3671 } else {
3672 Locs[i] = std::make_pair(1, NumHi);
3673 if (2+NumHi < NumElems)
3674 Mask1[2+NumHi] = Elt;
3675 NumHi++;
3676 }
3677 }
3678 }
3679 if (NumLo <= 2 && NumHi <= 2) {
3680 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3681 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3682 &Mask1[0], Mask1.size()));
3683 for (unsigned i = 0; i != NumElems; ++i) {
3684 if (Locs[i].first == -1)
3685 continue;
3686 else {
3687 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3688 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3689 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3690 }
3691 }
3692
3693 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3694 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3695 &Mask2[0], Mask2.size()));
3696 }
3697
3698 // Break it into (shuffle shuffle_hi, shuffle_lo).
3699 Locs.clear();
3700 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3701 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3702 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3703 unsigned MaskIdx = 0;
3704 unsigned LoIdx = 0;
3705 unsigned HiIdx = NumElems/2;
3706 for (unsigned i = 0; i != NumElems; ++i) {
3707 if (i == NumElems/2) {
3708 MaskPtr = &HiMask;
3709 MaskIdx = 1;
3710 LoIdx = 0;
3711 HiIdx = NumElems/2;
3712 }
3713 SDOperand Elt = PermMask.getOperand(i);
3714 if (Elt.getOpcode() == ISD::UNDEF) {
3715 Locs[i] = std::make_pair(-1, -1);
3716 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3717 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3718 (*MaskPtr)[LoIdx] = Elt;
3719 LoIdx++;
3720 } else {
3721 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3722 (*MaskPtr)[HiIdx] = Elt;
3723 HiIdx++;
3724 }
3725 }
3726
3727 SDOperand LoShuffle =
3728 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3729 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3730 &LoMask[0], LoMask.size()));
3731 SDOperand HiShuffle =
3732 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3733 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3734 &HiMask[0], HiMask.size()));
3735 SmallVector<SDOperand, 8> MaskOps;
3736 for (unsigned i = 0; i != NumElems; ++i) {
3737 if (Locs[i].first == -1) {
3738 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3739 } else {
3740 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3741 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3742 }
3743 }
3744 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3745 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3746 &MaskOps[0], MaskOps.size()));
3747 }
3748
3749 return SDOperand();
3750}
3751
3752SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003753X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3754 SelectionDAG &DAG) {
3755 MVT::ValueType VT = Op.getValueType();
3756 if (MVT::getSizeInBits(VT) == 8) {
3757 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3758 Op.getOperand(0), Op.getOperand(1));
3759 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3760 DAG.getValueType(VT));
3761 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3762 } else if (MVT::getSizeInBits(VT) == 16) {
3763 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3764 Op.getOperand(0), Op.getOperand(1));
3765 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3766 DAG.getValueType(VT));
3767 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3768 }
3769 return SDOperand();
3770}
3771
3772
3773SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003774X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3775 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3776 return SDOperand();
3777
Nate Begemand77e59e2008-02-11 04:19:36 +00003778 if (Subtarget->hasSSE41())
3779 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3780
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003781 MVT::ValueType VT = Op.getValueType();
3782 // TODO: handle v16i8.
3783 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003784 SDOperand Vec = Op.getOperand(0);
3785 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3786 if (Idx == 0)
3787 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3788 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3789 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3790 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003791 // Transform it so it match pextrw which produces a 32-bit result.
3792 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3793 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3794 Op.getOperand(0), Op.getOperand(1));
3795 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3796 DAG.getValueType(VT));
3797 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3798 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003799 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3800 if (Idx == 0)
3801 return Op;
3802 // SHUFPS the element to the lowest double word, then movss.
3803 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3804 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003805 IdxVec.
3806 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3807 IdxVec.
3808 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3809 IdxVec.
3810 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3811 IdxVec.
3812 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003813 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3814 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003815 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003816 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3817 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3818 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003819 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003820 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003821 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3822 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3823 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003824 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3825 if (Idx == 0)
3826 return Op;
3827
3828 // UNPCKHPD the element to the lowest double word, then movsd.
3829 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3830 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3831 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3832 SmallVector<SDOperand, 8> IdxVec;
3833 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003834 IdxVec.
3835 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003836 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3837 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003838 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003839 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3840 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3841 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003842 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843 }
3844
3845 return SDOperand();
3846}
3847
3848SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003849X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3850 MVT::ValueType VT = Op.getValueType();
3851 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3852
3853 SDOperand N0 = Op.getOperand(0);
3854 SDOperand N1 = Op.getOperand(1);
3855 SDOperand N2 = Op.getOperand(2);
3856
3857 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3858 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3859 : X86ISD::PINSRW;
3860 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3861 // argument.
3862 if (N1.getValueType() != MVT::i32)
3863 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3864 if (N2.getValueType() != MVT::i32)
3865 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3866 return DAG.getNode(Opc, VT, N0, N1, N2);
3867 } else if (EVT == MVT::f32) {
3868 // Bits [7:6] of the constant are the source select. This will always be
3869 // zero here. The DAG Combiner may combine an extract_elt index into these
3870 // bits. For example (insert (extract, 3), 2) could be matched by putting
3871 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3872 // Bits [5:4] of the constant are the destination select. This is the
3873 // value of the incoming immediate.
3874 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3875 // combine either bitwise AND or insert of float 0.0 to set these bits.
3876 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3877 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3878 }
3879 return SDOperand();
3880}
3881
3882SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003883X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003884 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003885 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003886
3887 if (Subtarget->hasSSE41())
3888 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3889
Evan Chenge12a7eb2007-12-12 07:55:34 +00003890 if (EVT == MVT::i8)
3891 return SDOperand();
3892
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 SDOperand N0 = Op.getOperand(0);
3894 SDOperand N1 = Op.getOperand(1);
3895 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003896
3897 if (MVT::getSizeInBits(EVT) == 16) {
3898 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3899 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900 if (N1.getValueType() != MVT::i32)
3901 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3902 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003903 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003904 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003906 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003907}
3908
3909SDOperand
3910X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3911 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003912 MVT::ValueType VT = MVT::v2i32;
3913 switch (Op.getValueType()) {
3914 default: break;
3915 case MVT::v16i8:
3916 case MVT::v8i16:
3917 VT = MVT::v4i32;
3918 break;
3919 }
3920 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3921 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922}
3923
3924// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3925// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3926// one of the above mentioned nodes. It has to be wrapped because otherwise
3927// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3928// be used to form addressing mode. These wrapped nodes will be selected
3929// into MOV32ri.
3930SDOperand
3931X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3932 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3933 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3934 getPointerTy(),
3935 CP->getAlignment());
3936 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3937 // With PIC, the address is actually $g + Offset.
3938 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3939 !Subtarget->isPICStyleRIPRel()) {
3940 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3941 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3942 Result);
3943 }
3944
3945 return Result;
3946}
3947
3948SDOperand
3949X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3950 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3951 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003952 // If it's a debug information descriptor, don't mess with it.
3953 if (DAG.isVerifiedDebugInfoDesc(Op))
3954 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003955 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3956 // With PIC, the address is actually $g + Offset.
3957 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3958 !Subtarget->isPICStyleRIPRel()) {
3959 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3960 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3961 Result);
3962 }
3963
3964 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3965 // load the value at address GV, not the value of GV itself. This means that
3966 // the GlobalAddress must be in the base or index register of the address, not
3967 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3968 // The same applies for external symbols during PIC codegen
3969 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003970 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003971 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972
3973 return Result;
3974}
3975
3976// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3977static SDOperand
3978LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3979 const MVT::ValueType PtrVT) {
3980 SDOperand InFlag;
3981 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3982 DAG.getNode(X86ISD::GlobalBaseReg,
3983 PtrVT), InFlag);
3984 InFlag = Chain.getValue(1);
3985
3986 // emit leal symbol@TLSGD(,%ebx,1), %eax
3987 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3988 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3989 GA->getValueType(0),
3990 GA->getOffset());
3991 SDOperand Ops[] = { Chain, TGA, InFlag };
3992 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3993 InFlag = Result.getValue(2);
3994 Chain = Result.getValue(1);
3995
3996 // call ___tls_get_addr. This function receives its argument in
3997 // the register EAX.
3998 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3999 InFlag = Chain.getValue(1);
4000
4001 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4002 SDOperand Ops1[] = { Chain,
4003 DAG.getTargetExternalSymbol("___tls_get_addr",
4004 PtrVT),
4005 DAG.getRegister(X86::EAX, PtrVT),
4006 DAG.getRegister(X86::EBX, PtrVT),
4007 InFlag };
4008 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4009 InFlag = Chain.getValue(1);
4010
4011 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4012}
4013
4014// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4015// "local exec" model.
4016static SDOperand
4017LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4018 const MVT::ValueType PtrVT) {
4019 // Get the Thread Pointer
4020 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4021 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4022 // exec)
4023 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4024 GA->getValueType(0),
4025 GA->getOffset());
4026 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4027
4028 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004029 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004030 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004031
4032 // The address of the thread local variable is the add of the thread
4033 // pointer with the offset of the variable.
4034 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4035}
4036
4037SDOperand
4038X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4039 // TODO: implement the "local dynamic" model
4040 // TODO: implement the "initial exec"model for pic executables
4041 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4042 "TLS not implemented for non-ELF and 64-bit targets");
4043 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4044 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4045 // otherwise use the "Local Exec"TLS Model
4046 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4047 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4048 else
4049 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4050}
4051
4052SDOperand
4053X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4054 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4055 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4056 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4057 // With PIC, the address is actually $g + Offset.
4058 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4059 !Subtarget->isPICStyleRIPRel()) {
4060 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4061 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4062 Result);
4063 }
4064
4065 return Result;
4066}
4067
4068SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4069 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4070 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4071 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4072 // With PIC, the address is actually $g + Offset.
4073 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4074 !Subtarget->isPICStyleRIPRel()) {
4075 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4076 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4077 Result);
4078 }
4079
4080 return Result;
4081}
4082
Chris Lattner62814a32007-10-17 06:02:13 +00004083/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4084/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004085SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00004086 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4087 "Not an i64 shift!");
4088 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4089 SDOperand ShOpLo = Op.getOperand(0);
4090 SDOperand ShOpHi = Op.getOperand(1);
4091 SDOperand ShAmt = Op.getOperand(2);
4092 SDOperand Tmp1 = isSRA ?
4093 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4094 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004095
Chris Lattner62814a32007-10-17 06:02:13 +00004096 SDOperand Tmp2, Tmp3;
4097 if (Op.getOpcode() == ISD::SHL_PARTS) {
4098 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4099 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4100 } else {
4101 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4102 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4103 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004104
Chris Lattner62814a32007-10-17 06:02:13 +00004105 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4106 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4107 DAG.getConstant(32, MVT::i8));
4108 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4109 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110
Chris Lattner62814a32007-10-17 06:02:13 +00004111 SDOperand Hi, Lo;
4112 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4113 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4114 SmallVector<SDOperand, 4> Ops;
4115 if (Op.getOpcode() == ISD::SHL_PARTS) {
4116 Ops.push_back(Tmp2);
4117 Ops.push_back(Tmp3);
4118 Ops.push_back(CC);
4119 Ops.push_back(Cond);
4120 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004121
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004122 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004123 Ops.push_back(Tmp3);
4124 Ops.push_back(Tmp1);
4125 Ops.push_back(CC);
4126 Ops.push_back(Cond);
4127 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4128 } else {
4129 Ops.push_back(Tmp2);
4130 Ops.push_back(Tmp3);
4131 Ops.push_back(CC);
4132 Ops.push_back(Cond);
4133 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4134
4135 Ops.clear();
4136 Ops.push_back(Tmp3);
4137 Ops.push_back(Tmp1);
4138 Ops.push_back(CC);
4139 Ops.push_back(Cond);
4140 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4141 }
4142
4143 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4144 Ops.clear();
4145 Ops.push_back(Lo);
4146 Ops.push_back(Hi);
4147 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004148}
4149
4150SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004152 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4153 "Unknown SINT_TO_FP to lower!");
4154
4155 // These are really Legal; caller falls through into that case.
4156 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4157 return SDOperand();
4158 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4159 Subtarget->is64Bit())
4160 return SDOperand();
4161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4163 MachineFunction &MF = DAG.getMachineFunction();
4164 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4165 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4166 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004167 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004168 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004169 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004170
4171 // Build the FILD
4172 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004173 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004174 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004175 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4176 else
4177 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4178 SmallVector<SDOperand, 8> Ops;
4179 Ops.push_back(Chain);
4180 Ops.push_back(StackSlot);
4181 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004182 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4183 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184
Dale Johannesen2fc20782007-09-14 22:26:36 +00004185 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004186 Chain = Result.getValue(1);
4187 SDOperand InFlag = Result.getValue(2);
4188
4189 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4190 // shouldn't be necessary except that RFP cannot be live across
4191 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4192 MachineFunction &MF = DAG.getMachineFunction();
4193 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4194 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4195 Tys = DAG.getVTList(MVT::Other);
4196 SmallVector<SDOperand, 8> Ops;
4197 Ops.push_back(Chain);
4198 Ops.push_back(Result);
4199 Ops.push_back(StackSlot);
4200 Ops.push_back(DAG.getValueType(Op.getValueType()));
4201 Ops.push_back(InFlag);
4202 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004203 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004204 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205 }
4206
4207 return Result;
4208}
4209
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004210std::pair<SDOperand,SDOperand> X86TargetLowering::
4211FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004212 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4213 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214
Dale Johannesen2fc20782007-09-14 22:26:36 +00004215 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004216 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004217 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004218 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004219 if (Subtarget->is64Bit() &&
4220 Op.getValueType() == MVT::i64 &&
4221 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004222 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004223
Evan Cheng05441e62007-10-15 20:11:21 +00004224 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4225 // stack slot.
4226 MachineFunction &MF = DAG.getMachineFunction();
4227 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4228 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4229 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230 unsigned Opc;
4231 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004232 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4233 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4234 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4235 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004236 }
4237
4238 SDOperand Chain = DAG.getEntryNode();
4239 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004240 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004242 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004243 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4245 SDOperand Ops[] = {
4246 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4247 };
4248 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4249 Chain = Value.getValue(1);
4250 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4251 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4252 }
4253
4254 // Build the FP_TO_INT*_IN_MEM
4255 SDOperand Ops[] = { Chain, Value, StackSlot };
4256 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4257
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004258 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259}
4260
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004261SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004262 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4263 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4264 if (FIST.Val == 0) return SDOperand();
4265
4266 // Load the result.
4267 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4268}
4269
4270SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4271 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4272 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4273 if (FIST.Val == 0) return 0;
4274
4275 // Return an i64 load from the stack slot.
4276 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4277
4278 // Use a MERGE_VALUES node to drop the chain result value.
4279 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4280}
4281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4283 MVT::ValueType VT = Op.getValueType();
4284 MVT::ValueType EltVT = VT;
4285 if (MVT::isVector(VT))
4286 EltVT = MVT::getVectorElementType(VT);
4287 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4288 std::vector<Constant*> CV;
4289 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004290 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004291 CV.push_back(C);
4292 CV.push_back(C);
4293 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004294 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004295 CV.push_back(C);
4296 CV.push_back(C);
4297 CV.push_back(C);
4298 CV.push_back(C);
4299 }
Dan Gohman11821702007-07-27 17:16:43 +00004300 Constant *C = ConstantVector::get(CV);
4301 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004302 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004303 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004304 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4306}
4307
4308SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4309 MVT::ValueType VT = Op.getValueType();
4310 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004311 unsigned EltNum = 1;
4312 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004314 EltNum = MVT::getVectorNumElements(VT);
4315 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4317 std::vector<Constant*> CV;
4318 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004319 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320 CV.push_back(C);
4321 CV.push_back(C);
4322 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004323 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004324 CV.push_back(C);
4325 CV.push_back(C);
4326 CV.push_back(C);
4327 CV.push_back(C);
4328 }
Dan Gohman11821702007-07-27 17:16:43 +00004329 Constant *C = ConstantVector::get(CV);
4330 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004331 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004332 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004333 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004334 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004335 return DAG.getNode(ISD::BIT_CONVERT, VT,
4336 DAG.getNode(ISD::XOR, MVT::v2i64,
4337 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4338 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4339 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004340 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4341 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342}
4343
4344SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4345 SDOperand Op0 = Op.getOperand(0);
4346 SDOperand Op1 = Op.getOperand(1);
4347 MVT::ValueType VT = Op.getValueType();
4348 MVT::ValueType SrcVT = Op1.getValueType();
4349 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4350
4351 // If second operand is smaller, extend it first.
4352 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4353 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4354 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004355 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004357 // And if it is bigger, shrink it first.
4358 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004359 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004360 SrcVT = VT;
4361 SrcTy = MVT::getTypeForValueType(SrcVT);
4362 }
4363
4364 // At this point the operands and the result should have the same
4365 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366
4367 // First get the sign bit of second operand.
4368 std::vector<Constant*> CV;
4369 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004370 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4371 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004372 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004373 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4374 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4375 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4376 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377 }
Dan Gohman11821702007-07-27 17:16:43 +00004378 Constant *C = ConstantVector::get(CV);
4379 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004380 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004381 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004382 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004383 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4384
4385 // Shift sign bit right or left if the two operands have different types.
4386 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4387 // Op0 is MVT::f32, Op1 is MVT::f64.
4388 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4389 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4390 DAG.getConstant(32, MVT::i32));
4391 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4392 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004393 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004394 }
4395
4396 // Clear first operand sign bit.
4397 CV.clear();
4398 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004399 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4400 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004401 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004402 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4403 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4404 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4405 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 }
Dan Gohman11821702007-07-27 17:16:43 +00004407 C = ConstantVector::get(CV);
4408 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004409 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004410 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004411 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4413
4414 // Or the value with the sign bit.
4415 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4416}
4417
Evan Cheng621216e2007-09-29 00:00:36 +00004418SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004419 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004420 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004421 SDOperand Op0 = Op.getOperand(0);
4422 SDOperand Op1 = Op.getOperand(1);
4423 SDOperand CC = Op.getOperand(2);
4424 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4425 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4426 unsigned X86CC;
4427
Evan Cheng950aac02007-09-25 01:57:46 +00004428 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004429 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004430 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4431 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004432 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004433 }
Evan Cheng950aac02007-09-25 01:57:46 +00004434
4435 assert(isFP && "Illegal integer SetCC!");
4436
Evan Cheng621216e2007-09-29 00:00:36 +00004437 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004438 switch (SetCCOpcode) {
4439 default: assert(false && "Illegal floating point SetCC!");
4440 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004441 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004442 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004443 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004444 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4445 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4446 }
4447 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004448 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004449 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004450 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004451 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4452 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4453 }
4454 }
4455}
4456
4457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4459 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004460 SDOperand Cond = Op.getOperand(0);
4461 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462
4463 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004464 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465
Evan Cheng50d37ab2007-10-08 22:16:29 +00004466 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4467 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 if (Cond.getOpcode() == X86ISD::SETCC) {
4469 CC = Cond.getOperand(0);
4470
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 SDOperand Cmp = Cond.getOperand(1);
4472 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004473 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004474
Evan Cheng50d37ab2007-10-08 22:16:29 +00004475 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004476 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004477 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004478 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004479
Evan Cheng621216e2007-09-29 00:00:36 +00004480 if ((Opc == X86ISD::CMP ||
4481 Opc == X86ISD::COMI ||
4482 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004483 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004484 addTest = false;
4485 }
4486 }
4487
4488 if (addTest) {
4489 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004490 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004491 }
4492
4493 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4494 MVT::Flag);
4495 SmallVector<SDOperand, 4> Ops;
4496 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4497 // condition is true.
4498 Ops.push_back(Op.getOperand(2));
4499 Ops.push_back(Op.getOperand(1));
4500 Ops.push_back(CC);
4501 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004502 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004503}
4504
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4506 bool addTest = true;
4507 SDOperand Chain = Op.getOperand(0);
4508 SDOperand Cond = Op.getOperand(1);
4509 SDOperand Dest = Op.getOperand(2);
4510 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511
4512 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004513 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004514
Evan Cheng50d37ab2007-10-08 22:16:29 +00004515 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4516 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517 if (Cond.getOpcode() == X86ISD::SETCC) {
4518 CC = Cond.getOperand(0);
4519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004520 SDOperand Cmp = Cond.getOperand(1);
4521 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004522 if (Opc == X86ISD::CMP ||
4523 Opc == X86ISD::COMI ||
4524 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004525 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004526 addTest = false;
4527 }
4528 }
4529
4530 if (addTest) {
4531 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004532 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004533 }
Evan Cheng621216e2007-09-29 00:00:36 +00004534 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004535 Chain, Op.getOperand(2), CC, Cond);
4536}
4537
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538
4539// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4540// Calls to _alloca is needed to probe the stack when allocating more than 4k
4541// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4542// that the guard pages used by the OS virtual memory manager are allocated in
4543// correct sequence.
4544SDOperand
4545X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4546 SelectionDAG &DAG) {
4547 assert(Subtarget->isTargetCygMing() &&
4548 "This should be used only on Cygwin/Mingw targets");
4549
4550 // Get the inputs.
4551 SDOperand Chain = Op.getOperand(0);
4552 SDOperand Size = Op.getOperand(1);
4553 // FIXME: Ensure alignment here
4554
4555 SDOperand Flag;
4556
4557 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004558 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559
4560 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4561 Flag = Chain.getValue(1);
4562
4563 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4564 SDOperand Ops[] = { Chain,
4565 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4566 DAG.getRegister(X86::EAX, IntPtr),
4567 Flag };
4568 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4569 Flag = Chain.getValue(1);
4570
4571 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4572
4573 std::vector<MVT::ValueType> Tys;
4574 Tys.push_back(SPTy);
4575 Tys.push_back(MVT::Other);
4576 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4577 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4578}
4579
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4581 SDOperand InFlag(0, 0);
4582 SDOperand Chain = Op.getOperand(0);
4583 unsigned Align =
4584 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4585 if (Align == 0) Align = 1;
4586
4587 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004588 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004589 // The libc version is likely to be faster for these cases. It can use the
4590 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004592 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593 MVT::ValueType IntPtr = getPointerTy();
4594 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4595 TargetLowering::ArgListTy Args;
4596 TargetLowering::ArgListEntry Entry;
4597 Entry.Node = Op.getOperand(1);
4598 Entry.Ty = IntPtrTy;
4599 Args.push_back(Entry);
4600 // Extend the unsigned i8 argument to be an int value for the call.
4601 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4602 Entry.Ty = IntPtrTy;
4603 Args.push_back(Entry);
4604 Entry.Node = Op.getOperand(3);
4605 Args.push_back(Entry);
4606 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004607 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4608 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609 return CallResult.second;
4610 }
4611
4612 MVT::ValueType AVT;
4613 SDOperand Count;
4614 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4615 unsigned BytesLeft = 0;
4616 bool TwoRepStos = false;
4617 if (ValC) {
4618 unsigned ValReg;
4619 uint64_t Val = ValC->getValue() & 255;
4620
4621 // If the value is a constant, then we can potentially use larger sets.
4622 switch (Align & 3) {
4623 case 2: // WORD aligned
4624 AVT = MVT::i16;
4625 ValReg = X86::AX;
4626 Val = (Val << 8) | Val;
4627 break;
4628 case 0: // DWORD aligned
4629 AVT = MVT::i32;
4630 ValReg = X86::EAX;
4631 Val = (Val << 8) | Val;
4632 Val = (Val << 16) | Val;
4633 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4634 AVT = MVT::i64;
4635 ValReg = X86::RAX;
4636 Val = (Val << 32) | Val;
4637 }
4638 break;
4639 default: // Byte aligned
4640 AVT = MVT::i8;
4641 ValReg = X86::AL;
4642 Count = Op.getOperand(3);
4643 break;
4644 }
4645
4646 if (AVT > MVT::i8) {
4647 if (I) {
4648 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004649 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004650 BytesLeft = I->getValue() % UBytes;
4651 } else {
4652 assert(AVT >= MVT::i32 &&
4653 "Do not use rep;stos if not at least DWORD aligned");
4654 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4655 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4656 TwoRepStos = true;
4657 }
4658 }
4659
4660 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4661 InFlag);
4662 InFlag = Chain.getValue(1);
4663 } else {
4664 AVT = MVT::i8;
4665 Count = Op.getOperand(3);
4666 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4667 InFlag = Chain.getValue(1);
4668 }
4669
4670 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4671 Count, InFlag);
4672 InFlag = Chain.getValue(1);
4673 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4674 Op.getOperand(1), InFlag);
4675 InFlag = Chain.getValue(1);
4676
4677 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4678 SmallVector<SDOperand, 8> Ops;
4679 Ops.push_back(Chain);
4680 Ops.push_back(DAG.getValueType(AVT));
4681 Ops.push_back(InFlag);
4682 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4683
4684 if (TwoRepStos) {
4685 InFlag = Chain.getValue(1);
4686 Count = Op.getOperand(3);
4687 MVT::ValueType CVT = Count.getValueType();
4688 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4689 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4690 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4691 Left, InFlag);
4692 InFlag = Chain.getValue(1);
4693 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4694 Ops.clear();
4695 Ops.push_back(Chain);
4696 Ops.push_back(DAG.getValueType(MVT::i8));
4697 Ops.push_back(InFlag);
4698 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4699 } else if (BytesLeft) {
4700 // Issue stores for the last 1 - 7 bytes.
4701 SDOperand Value;
4702 unsigned Val = ValC->getValue() & 255;
4703 unsigned Offset = I->getValue() - BytesLeft;
4704 SDOperand DstAddr = Op.getOperand(1);
4705 MVT::ValueType AddrVT = DstAddr.getValueType();
4706 if (BytesLeft >= 4) {
4707 Val = (Val << 8) | Val;
4708 Val = (Val << 16) | Val;
4709 Value = DAG.getConstant(Val, MVT::i32);
4710 Chain = DAG.getStore(Chain, Value,
4711 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4712 DAG.getConstant(Offset, AddrVT)),
4713 NULL, 0);
4714 BytesLeft -= 4;
4715 Offset += 4;
4716 }
4717 if (BytesLeft >= 2) {
4718 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4719 Chain = DAG.getStore(Chain, Value,
4720 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4721 DAG.getConstant(Offset, AddrVT)),
4722 NULL, 0);
4723 BytesLeft -= 2;
4724 Offset += 2;
4725 }
4726 if (BytesLeft == 1) {
4727 Value = DAG.getConstant(Val, MVT::i8);
4728 Chain = DAG.getStore(Chain, Value,
4729 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4730 DAG.getConstant(Offset, AddrVT)),
4731 NULL, 0);
4732 }
4733 }
4734
4735 return Chain;
4736}
4737
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004738SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4739 SDOperand Dest,
4740 SDOperand Source,
4741 unsigned Size,
4742 unsigned Align,
4743 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004744 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004745 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004746 switch (Align & 3) {
4747 case 2: // WORD aligned
4748 AVT = MVT::i16;
4749 break;
4750 case 0: // DWORD aligned
4751 AVT = MVT::i32;
4752 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4753 AVT = MVT::i64;
4754 break;
4755 default: // Byte aligned
4756 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 break;
4758 }
4759
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004760 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004761 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004762 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763
4764 SDOperand InFlag(0, 0);
4765 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4766 Count, InFlag);
4767 InFlag = Chain.getValue(1);
4768 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004769 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 InFlag = Chain.getValue(1);
4771 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004772 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004773 InFlag = Chain.getValue(1);
4774
4775 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4776 SmallVector<SDOperand, 8> Ops;
4777 Ops.push_back(Chain);
4778 Ops.push_back(DAG.getValueType(AVT));
4779 Ops.push_back(InFlag);
4780 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4781
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004782 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004783 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004784 unsigned Offset = Size - BytesLeft;
4785 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004786 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004787 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004788 MVT::ValueType SrcVT = SrcAddr.getValueType();
4789 SDOperand Value;
4790 if (BytesLeft >= 4) {
4791 Value = DAG.getLoad(MVT::i32, Chain,
4792 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4793 DAG.getConstant(Offset, SrcVT)),
4794 NULL, 0);
4795 Chain = Value.getValue(1);
4796 Chain = DAG.getStore(Chain, Value,
4797 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4798 DAG.getConstant(Offset, DstVT)),
4799 NULL, 0);
4800 BytesLeft -= 4;
4801 Offset += 4;
4802 }
4803 if (BytesLeft >= 2) {
4804 Value = DAG.getLoad(MVT::i16, Chain,
4805 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4806 DAG.getConstant(Offset, SrcVT)),
4807 NULL, 0);
4808 Chain = Value.getValue(1);
4809 Chain = DAG.getStore(Chain, Value,
4810 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4811 DAG.getConstant(Offset, DstVT)),
4812 NULL, 0);
4813 BytesLeft -= 2;
4814 Offset += 2;
4815 }
4816
4817 if (BytesLeft == 1) {
4818 Value = DAG.getLoad(MVT::i8, Chain,
4819 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4820 DAG.getConstant(Offset, SrcVT)),
4821 NULL, 0);
4822 Chain = Value.getValue(1);
4823 Chain = DAG.getStore(Chain, Value,
4824 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4825 DAG.getConstant(Offset, DstVT)),
4826 NULL, 0);
4827 }
4828 }
4829
4830 return Chain;
4831}
4832
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004833/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4834SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004835 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004836 SDOperand TheChain = N->getOperand(0);
4837 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004838 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004839 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4840 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4841 MVT::i64, rax.getValue(2));
4842 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843 DAG.getConstant(32, MVT::i8));
4844 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004845 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 };
4847
4848 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004849 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004850 }
4851
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004852 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4853 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4854 MVT::i32, eax.getValue(2));
4855 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4856 SDOperand Ops[] = { eax, edx };
4857 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4858
4859 // Use a MERGE_VALUES to return the value and chain.
4860 Ops[1] = edx.getValue(1);
4861 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4862 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004863}
4864
4865SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004866 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004867
4868 if (!Subtarget->is64Bit()) {
4869 // vastart just stores the address of the VarArgsFrameIndex slot into the
4870 // memory location argument.
4871 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004872 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 }
4874
4875 // __va_list_tag:
4876 // gp_offset (0 - 6 * 8)
4877 // fp_offset (48 - 48 + 8 * 16)
4878 // overflow_arg_area (point to parameters coming in memory).
4879 // reg_save_area
4880 SmallVector<SDOperand, 8> MemOps;
4881 SDOperand FIN = Op.getOperand(1);
4882 // Store gp_offset
4883 SDOperand Store = DAG.getStore(Op.getOperand(0),
4884 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004885 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886 MemOps.push_back(Store);
4887
4888 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004889 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004890 Store = DAG.getStore(Op.getOperand(0),
4891 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004892 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893 MemOps.push_back(Store);
4894
4895 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004896 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004897 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004898 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004899 MemOps.push_back(Store);
4900
4901 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004902 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004904 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004905 MemOps.push_back(Store);
4906 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4907}
4908
4909SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4910 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4911 SDOperand Chain = Op.getOperand(0);
4912 SDOperand DstPtr = Op.getOperand(1);
4913 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004914 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4915 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916
Dan Gohman12a9c082008-02-06 22:27:42 +00004917 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004918 Chain = SrcPtr.getValue(1);
4919 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004920 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004921 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004922 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004923 if (i == 2)
4924 break;
4925 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004926 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004928 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004929 }
4930 return Chain;
4931}
4932
4933SDOperand
4934X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4935 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4936 switch (IntNo) {
4937 default: return SDOperand(); // Don't custom lower most intrinsics.
4938 // Comparison intrinsics.
4939 case Intrinsic::x86_sse_comieq_ss:
4940 case Intrinsic::x86_sse_comilt_ss:
4941 case Intrinsic::x86_sse_comile_ss:
4942 case Intrinsic::x86_sse_comigt_ss:
4943 case Intrinsic::x86_sse_comige_ss:
4944 case Intrinsic::x86_sse_comineq_ss:
4945 case Intrinsic::x86_sse_ucomieq_ss:
4946 case Intrinsic::x86_sse_ucomilt_ss:
4947 case Intrinsic::x86_sse_ucomile_ss:
4948 case Intrinsic::x86_sse_ucomigt_ss:
4949 case Intrinsic::x86_sse_ucomige_ss:
4950 case Intrinsic::x86_sse_ucomineq_ss:
4951 case Intrinsic::x86_sse2_comieq_sd:
4952 case Intrinsic::x86_sse2_comilt_sd:
4953 case Intrinsic::x86_sse2_comile_sd:
4954 case Intrinsic::x86_sse2_comigt_sd:
4955 case Intrinsic::x86_sse2_comige_sd:
4956 case Intrinsic::x86_sse2_comineq_sd:
4957 case Intrinsic::x86_sse2_ucomieq_sd:
4958 case Intrinsic::x86_sse2_ucomilt_sd:
4959 case Intrinsic::x86_sse2_ucomile_sd:
4960 case Intrinsic::x86_sse2_ucomigt_sd:
4961 case Intrinsic::x86_sse2_ucomige_sd:
4962 case Intrinsic::x86_sse2_ucomineq_sd: {
4963 unsigned Opc = 0;
4964 ISD::CondCode CC = ISD::SETCC_INVALID;
4965 switch (IntNo) {
4966 default: break;
4967 case Intrinsic::x86_sse_comieq_ss:
4968 case Intrinsic::x86_sse2_comieq_sd:
4969 Opc = X86ISD::COMI;
4970 CC = ISD::SETEQ;
4971 break;
4972 case Intrinsic::x86_sse_comilt_ss:
4973 case Intrinsic::x86_sse2_comilt_sd:
4974 Opc = X86ISD::COMI;
4975 CC = ISD::SETLT;
4976 break;
4977 case Intrinsic::x86_sse_comile_ss:
4978 case Intrinsic::x86_sse2_comile_sd:
4979 Opc = X86ISD::COMI;
4980 CC = ISD::SETLE;
4981 break;
4982 case Intrinsic::x86_sse_comigt_ss:
4983 case Intrinsic::x86_sse2_comigt_sd:
4984 Opc = X86ISD::COMI;
4985 CC = ISD::SETGT;
4986 break;
4987 case Intrinsic::x86_sse_comige_ss:
4988 case Intrinsic::x86_sse2_comige_sd:
4989 Opc = X86ISD::COMI;
4990 CC = ISD::SETGE;
4991 break;
4992 case Intrinsic::x86_sse_comineq_ss:
4993 case Intrinsic::x86_sse2_comineq_sd:
4994 Opc = X86ISD::COMI;
4995 CC = ISD::SETNE;
4996 break;
4997 case Intrinsic::x86_sse_ucomieq_ss:
4998 case Intrinsic::x86_sse2_ucomieq_sd:
4999 Opc = X86ISD::UCOMI;
5000 CC = ISD::SETEQ;
5001 break;
5002 case Intrinsic::x86_sse_ucomilt_ss:
5003 case Intrinsic::x86_sse2_ucomilt_sd:
5004 Opc = X86ISD::UCOMI;
5005 CC = ISD::SETLT;
5006 break;
5007 case Intrinsic::x86_sse_ucomile_ss:
5008 case Intrinsic::x86_sse2_ucomile_sd:
5009 Opc = X86ISD::UCOMI;
5010 CC = ISD::SETLE;
5011 break;
5012 case Intrinsic::x86_sse_ucomigt_ss:
5013 case Intrinsic::x86_sse2_ucomigt_sd:
5014 Opc = X86ISD::UCOMI;
5015 CC = ISD::SETGT;
5016 break;
5017 case Intrinsic::x86_sse_ucomige_ss:
5018 case Intrinsic::x86_sse2_ucomige_sd:
5019 Opc = X86ISD::UCOMI;
5020 CC = ISD::SETGE;
5021 break;
5022 case Intrinsic::x86_sse_ucomineq_ss:
5023 case Intrinsic::x86_sse2_ucomineq_sd:
5024 Opc = X86ISD::UCOMI;
5025 CC = ISD::SETNE;
5026 break;
5027 }
5028
5029 unsigned X86CC;
5030 SDOperand LHS = Op.getOperand(1);
5031 SDOperand RHS = Op.getOperand(2);
5032 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5033
Evan Cheng621216e2007-09-29 00:00:36 +00005034 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5035 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5036 DAG.getConstant(X86CC, MVT::i8), Cond);
5037 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038 }
5039 }
5040}
5041
5042SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5043 // Depths > 0 not supported yet!
5044 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5045 return SDOperand();
5046
5047 // Just load the return address
5048 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5049 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5050}
5051
5052SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5053 // Depths > 0 not supported yet!
5054 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5055 return SDOperand();
5056
5057 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5058 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005059 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060}
5061
5062SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5063 SelectionDAG &DAG) {
5064 // Is not yet supported on x86-64
5065 if (Subtarget->is64Bit())
5066 return SDOperand();
5067
Chris Lattner5872a362008-01-17 07:00:52 +00005068 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005069}
5070
5071SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5072{
5073 assert(!Subtarget->is64Bit() &&
5074 "Lowering of eh_return builtin is not supported yet on x86-64");
5075
5076 MachineFunction &MF = DAG.getMachineFunction();
5077 SDOperand Chain = Op.getOperand(0);
5078 SDOperand Offset = Op.getOperand(1);
5079 SDOperand Handler = Op.getOperand(2);
5080
5081 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5082 getPointerTy());
5083
5084 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005085 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005086 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5087 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5088 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005089 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005090
5091 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5092 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5093}
5094
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005095SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5096 SelectionDAG &DAG) {
5097 SDOperand Root = Op.getOperand(0);
5098 SDOperand Trmp = Op.getOperand(1); // trampoline
5099 SDOperand FPtr = Op.getOperand(2); // nested function
5100 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5101
Dan Gohman12a9c082008-02-06 22:27:42 +00005102 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005103
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005104 const X86InstrInfo *TII =
5105 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5106
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005107 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005108 SDOperand OutChains[6];
5109
5110 // Large code-model.
5111
5112 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5113 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5114
5115 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005116 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005117 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005118 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005119
5120 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5121
5122 // Load the pointer to the nested function into R11.
5123 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5124 SDOperand Addr = Trmp;
5125 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005126 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005127
5128 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005129 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005130
5131 // Load the 'nest' parameter value into R10.
5132 // R10 is specified in X86CallingConv.td
5133 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5134 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5135 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005136 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005137
5138 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005139 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005140
5141 // Jump to the nested function.
5142 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5143 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5144 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005145 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005146
5147 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5148 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5149 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005150 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005151
5152 SDOperand Ops[] =
5153 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5154 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005155 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005156 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005157 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5158 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005159 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005160
5161 switch (CC) {
5162 default:
5163 assert(0 && "Unsupported calling convention");
5164 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005165 case CallingConv::X86_StdCall: {
5166 // Pass 'nest' parameter in ECX.
5167 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005168 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005169
5170 // Check that ECX wasn't needed by an 'inreg' parameter.
5171 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005172 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005173
5174 if (Attrs && !Func->isVarArg()) {
5175 unsigned InRegCount = 0;
5176 unsigned Idx = 1;
5177
5178 for (FunctionType::param_iterator I = FTy->param_begin(),
5179 E = FTy->param_end(); I != E; ++I, ++Idx)
5180 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5181 // FIXME: should only count parameters that are lowered to integers.
5182 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5183
5184 if (InRegCount > 2) {
5185 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5186 abort();
5187 }
5188 }
5189 break;
5190 }
5191 case CallingConv::X86_FastCall:
5192 // Pass 'nest' parameter in EAX.
5193 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005194 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005195 break;
5196 }
5197
5198 SDOperand OutChains[4];
5199 SDOperand Addr, Disp;
5200
5201 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5202 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5203
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005204 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5205 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005206 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005207 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005208 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005209
5210 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005211 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005212
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005213 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005214 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5215 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005216 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005217
5218 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005219 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005220
Duncan Sands7407a9f2007-09-11 14:10:23 +00005221 SDOperand Ops[] =
5222 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5223 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005224 }
5225}
5226
Dan Gohman819574c2008-01-31 00:41:03 +00005227SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005228 /*
5229 The rounding mode is in bits 11:10 of FPSR, and has the following
5230 settings:
5231 00 Round to nearest
5232 01 Round to -inf
5233 10 Round to +inf
5234 11 Round to 0
5235
5236 FLT_ROUNDS, on the other hand, expects the following:
5237 -1 Undefined
5238 0 Round to 0
5239 1 Round to nearest
5240 2 Round to +inf
5241 3 Round to -inf
5242
5243 To perform the conversion, we do:
5244 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5245 */
5246
5247 MachineFunction &MF = DAG.getMachineFunction();
5248 const TargetMachine &TM = MF.getTarget();
5249 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5250 unsigned StackAlignment = TFI.getStackAlignment();
5251 MVT::ValueType VT = Op.getValueType();
5252
5253 // Save FP Control Word to stack slot
5254 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5255 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5256
5257 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5258 DAG.getEntryNode(), StackSlot);
5259
5260 // Load FP Control Word from stack slot
5261 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5262
5263 // Transform as necessary
5264 SDOperand CWD1 =
5265 DAG.getNode(ISD::SRL, MVT::i16,
5266 DAG.getNode(ISD::AND, MVT::i16,
5267 CWD, DAG.getConstant(0x800, MVT::i16)),
5268 DAG.getConstant(11, MVT::i8));
5269 SDOperand CWD2 =
5270 DAG.getNode(ISD::SRL, MVT::i16,
5271 DAG.getNode(ISD::AND, MVT::i16,
5272 CWD, DAG.getConstant(0x400, MVT::i16)),
5273 DAG.getConstant(9, MVT::i8));
5274
5275 SDOperand RetVal =
5276 DAG.getNode(ISD::AND, MVT::i16,
5277 DAG.getNode(ISD::ADD, MVT::i16,
5278 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5279 DAG.getConstant(1, MVT::i16)),
5280 DAG.getConstant(3, MVT::i16));
5281
5282
5283 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5284 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5285}
5286
Evan Cheng48679f42007-12-14 02:13:44 +00005287SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5288 MVT::ValueType VT = Op.getValueType();
5289 MVT::ValueType OpVT = VT;
5290 unsigned NumBits = MVT::getSizeInBits(VT);
5291
5292 Op = Op.getOperand(0);
5293 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005294 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005295 OpVT = MVT::i32;
5296 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5297 }
Evan Cheng48679f42007-12-14 02:13:44 +00005298
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005299 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5300 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5301 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5302
5303 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5304 SmallVector<SDOperand, 4> Ops;
5305 Ops.push_back(Op);
5306 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5307 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5308 Ops.push_back(Op.getValue(1));
5309 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5310
5311 // Finally xor with NumBits-1.
5312 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5313
Evan Cheng48679f42007-12-14 02:13:44 +00005314 if (VT == MVT::i8)
5315 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5316 return Op;
5317}
5318
5319SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5320 MVT::ValueType VT = Op.getValueType();
5321 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005322 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005323
5324 Op = Op.getOperand(0);
5325 if (VT == MVT::i8) {
5326 OpVT = MVT::i32;
5327 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5328 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005329
5330 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5331 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5332 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5333
5334 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5335 SmallVector<SDOperand, 4> Ops;
5336 Ops.push_back(Op);
5337 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5338 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5339 Ops.push_back(Op.getValue(1));
5340 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5341
Evan Cheng48679f42007-12-14 02:13:44 +00005342 if (VT == MVT::i8)
5343 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5344 return Op;
5345}
5346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005347/// LowerOperation - Provide custom lowering hooks for some operations.
5348///
5349SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5350 switch (Op.getOpcode()) {
5351 default: assert(0 && "Should not custom lower this!");
5352 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5353 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5354 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5355 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5356 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5357 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5358 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5359 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5360 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5361 case ISD::SHL_PARTS:
5362 case ISD::SRA_PARTS:
5363 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5364 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5365 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5366 case ISD::FABS: return LowerFABS(Op, DAG);
5367 case ISD::FNEG: return LowerFNEG(Op, DAG);
5368 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005369 case ISD::SETCC: return LowerSETCC(Op, DAG);
5370 case ISD::SELECT: return LowerSELECT(Op, DAG);
5371 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005372 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5373 case ISD::CALL: return LowerCALL(Op, DAG);
5374 case ISD::RET: return LowerRET(Op, DAG);
5375 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5376 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5377 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005378 case ISD::VASTART: return LowerVASTART(Op, DAG);
5379 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5380 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5381 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5382 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5383 case ISD::FRAME_TO_ARGS_OFFSET:
5384 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5385 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5386 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005387 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005388 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005389 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5390 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005391
5392 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5393 case ISD::READCYCLECOUNTER:
5394 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005396}
5397
5398/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5399SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5400 switch (N->getOpcode()) {
5401 default: assert(0 && "Should not custom lower this!");
5402 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5403 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5404 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005405}
5406
5407const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5408 switch (Opcode) {
5409 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005410 case X86ISD::BSF: return "X86ISD::BSF";
5411 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005412 case X86ISD::SHLD: return "X86ISD::SHLD";
5413 case X86ISD::SHRD: return "X86ISD::SHRD";
5414 case X86ISD::FAND: return "X86ISD::FAND";
5415 case X86ISD::FOR: return "X86ISD::FOR";
5416 case X86ISD::FXOR: return "X86ISD::FXOR";
5417 case X86ISD::FSRL: return "X86ISD::FSRL";
5418 case X86ISD::FILD: return "X86ISD::FILD";
5419 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5420 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5421 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5422 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5423 case X86ISD::FLD: return "X86ISD::FLD";
5424 case X86ISD::FST: return "X86ISD::FST";
5425 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng931a8f42008-01-29 19:34:22 +00005426 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5428 case X86ISD::CALL: return "X86ISD::CALL";
5429 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5430 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5431 case X86ISD::CMP: return "X86ISD::CMP";
5432 case X86ISD::COMI: return "X86ISD::COMI";
5433 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5434 case X86ISD::SETCC: return "X86ISD::SETCC";
5435 case X86ISD::CMOV: return "X86ISD::CMOV";
5436 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5437 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5438 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5439 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005440 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5441 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005442 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005443 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005444 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5445 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005446 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5447 case X86ISD::FMAX: return "X86ISD::FMAX";
5448 case X86ISD::FMIN: return "X86ISD::FMIN";
5449 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5450 case X86ISD::FRCP: return "X86ISD::FRCP";
5451 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5452 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5453 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005454 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005455 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005456 }
5457}
5458
5459// isLegalAddressingMode - Return true if the addressing mode represented
5460// by AM is legal for this target, for a load/store of the specified type.
5461bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5462 const Type *Ty) const {
5463 // X86 supports extremely general addressing modes.
5464
5465 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5466 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5467 return false;
5468
5469 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005470 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005471 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5472 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005473
5474 // X86-64 only supports addr of globals in small code model.
5475 if (Subtarget->is64Bit()) {
5476 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5477 return false;
5478 // If lower 4G is not available, then we must use rip-relative addressing.
5479 if (AM.BaseOffs || AM.Scale > 1)
5480 return false;
5481 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005482 }
5483
5484 switch (AM.Scale) {
5485 case 0:
5486 case 1:
5487 case 2:
5488 case 4:
5489 case 8:
5490 // These scales always work.
5491 break;
5492 case 3:
5493 case 5:
5494 case 9:
5495 // These scales are formed with basereg+scalereg. Only accept if there is
5496 // no basereg yet.
5497 if (AM.HasBaseReg)
5498 return false;
5499 break;
5500 default: // Other stuff never works.
5501 return false;
5502 }
5503
5504 return true;
5505}
5506
5507
Evan Cheng27a820a2007-10-26 01:56:11 +00005508bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5509 if (!Ty1->isInteger() || !Ty2->isInteger())
5510 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005511 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5512 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5513 if (NumBits1 <= NumBits2)
5514 return false;
5515 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005516}
5517
Evan Cheng9decb332007-10-29 19:58:20 +00005518bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5519 MVT::ValueType VT2) const {
5520 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5521 return false;
5522 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5523 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5524 if (NumBits1 <= NumBits2)
5525 return false;
5526 return Subtarget->is64Bit() || NumBits1 < 64;
5527}
Evan Cheng27a820a2007-10-26 01:56:11 +00005528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529/// isShuffleMaskLegal - Targets can use this to indicate that they only
5530/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5531/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5532/// are assumed to be legal.
5533bool
5534X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5535 // Only do shuffles on 128-bit vector types for now.
5536 if (MVT::getSizeInBits(VT) == 64) return false;
5537 return (Mask.Val->getNumOperands() <= 4 ||
5538 isIdentityMask(Mask.Val) ||
5539 isIdentityMask(Mask.Val, true) ||
5540 isSplatMask(Mask.Val) ||
5541 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5542 X86::isUNPCKLMask(Mask.Val) ||
5543 X86::isUNPCKHMask(Mask.Val) ||
5544 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5545 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5546}
5547
5548bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5549 MVT::ValueType EVT,
5550 SelectionDAG &DAG) const {
5551 unsigned NumElts = BVOps.size();
5552 // Only do shuffles on 128-bit vector types for now.
5553 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5554 if (NumElts == 2) return true;
5555 if (NumElts == 4) {
5556 return (isMOVLMask(&BVOps[0], 4) ||
5557 isCommutedMOVL(&BVOps[0], 4, true) ||
5558 isSHUFPMask(&BVOps[0], 4) ||
5559 isCommutedSHUFP(&BVOps[0], 4));
5560 }
5561 return false;
5562}
5563
5564//===----------------------------------------------------------------------===//
5565// X86 Scheduler Hooks
5566//===----------------------------------------------------------------------===//
5567
5568MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005569X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5570 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5572 switch (MI->getOpcode()) {
5573 default: assert(false && "Unexpected instr type to insert");
5574 case X86::CMOV_FR32:
5575 case X86::CMOV_FR64:
5576 case X86::CMOV_V4F32:
5577 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005578 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579 // To "insert" a SELECT_CC instruction, we actually have to insert the
5580 // diamond control-flow pattern. The incoming instruction knows the
5581 // destination vreg to set, the condition code register to branch on, the
5582 // true/false values to select between, and a branch opcode to use.
5583 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5584 ilist<MachineBasicBlock>::iterator It = BB;
5585 ++It;
5586
5587 // thisMBB:
5588 // ...
5589 // TrueVal = ...
5590 // cmpTY ccX, r1, r2
5591 // bCC copy1MBB
5592 // fallthrough --> copy0MBB
5593 MachineBasicBlock *thisMBB = BB;
5594 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5595 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5596 unsigned Opc =
5597 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5598 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5599 MachineFunction *F = BB->getParent();
5600 F->getBasicBlockList().insert(It, copy0MBB);
5601 F->getBasicBlockList().insert(It, sinkMBB);
5602 // Update machine-CFG edges by first adding all successors of the current
5603 // block to the new block which will contain the Phi node for the select.
5604 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5605 e = BB->succ_end(); i != e; ++i)
5606 sinkMBB->addSuccessor(*i);
5607 // Next, remove all successors of the current block, and add the true
5608 // and fallthrough blocks as its successors.
5609 while(!BB->succ_empty())
5610 BB->removeSuccessor(BB->succ_begin());
5611 BB->addSuccessor(copy0MBB);
5612 BB->addSuccessor(sinkMBB);
5613
5614 // copy0MBB:
5615 // %FalseValue = ...
5616 // # fallthrough to sinkMBB
5617 BB = copy0MBB;
5618
5619 // Update machine-CFG edges
5620 BB->addSuccessor(sinkMBB);
5621
5622 // sinkMBB:
5623 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5624 // ...
5625 BB = sinkMBB;
5626 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5627 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5628 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5629
5630 delete MI; // The pseudo instruction is gone now.
5631 return BB;
5632 }
5633
5634 case X86::FP32_TO_INT16_IN_MEM:
5635 case X86::FP32_TO_INT32_IN_MEM:
5636 case X86::FP32_TO_INT64_IN_MEM:
5637 case X86::FP64_TO_INT16_IN_MEM:
5638 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005639 case X86::FP64_TO_INT64_IN_MEM:
5640 case X86::FP80_TO_INT16_IN_MEM:
5641 case X86::FP80_TO_INT32_IN_MEM:
5642 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005643 // Change the floating point control register to use "round towards zero"
5644 // mode when truncating to an integer value.
5645 MachineFunction *F = BB->getParent();
5646 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5647 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5648
5649 // Load the old value of the high byte of the control word...
5650 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005651 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005652 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5653
5654 // Set the high part to be round to zero...
5655 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5656 .addImm(0xC7F);
5657
5658 // Reload the modified control word now...
5659 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5660
5661 // Restore the memory image of control word to original value
5662 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5663 .addReg(OldCW);
5664
5665 // Get the X86 opcode to use.
5666 unsigned Opc;
5667 switch (MI->getOpcode()) {
5668 default: assert(0 && "illegal opcode!");
5669 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5670 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5671 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5672 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5673 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5674 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005675 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5676 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5677 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005678 }
5679
5680 X86AddressMode AM;
5681 MachineOperand &Op = MI->getOperand(0);
5682 if (Op.isRegister()) {
5683 AM.BaseType = X86AddressMode::RegBase;
5684 AM.Base.Reg = Op.getReg();
5685 } else {
5686 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005687 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005688 }
5689 Op = MI->getOperand(1);
5690 if (Op.isImmediate())
5691 AM.Scale = Op.getImm();
5692 Op = MI->getOperand(2);
5693 if (Op.isImmediate())
5694 AM.IndexReg = Op.getImm();
5695 Op = MI->getOperand(3);
5696 if (Op.isGlobalAddress()) {
5697 AM.GV = Op.getGlobal();
5698 } else {
5699 AM.Disp = Op.getImm();
5700 }
5701 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5702 .addReg(MI->getOperand(4).getReg());
5703
5704 // Reload the original control word now.
5705 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5706
5707 delete MI; // The pseudo instruction is gone now.
5708 return BB;
5709 }
5710 }
5711}
5712
5713//===----------------------------------------------------------------------===//
5714// X86 Optimization Hooks
5715//===----------------------------------------------------------------------===//
5716
5717void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005718 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005719 APInt &KnownZero,
5720 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005721 const SelectionDAG &DAG,
5722 unsigned Depth) const {
5723 unsigned Opc = Op.getOpcode();
5724 assert((Opc >= ISD::BUILTIN_OP_END ||
5725 Opc == ISD::INTRINSIC_WO_CHAIN ||
5726 Opc == ISD::INTRINSIC_W_CHAIN ||
5727 Opc == ISD::INTRINSIC_VOID) &&
5728 "Should use MaskedValueIsZero if you don't know whether Op"
5729 " is a target node!");
5730
Dan Gohman1d79e432008-02-13 23:07:24 +00005731 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 switch (Opc) {
5733 default: break;
5734 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005735 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5736 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005737 break;
5738 }
5739}
5740
5741/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5742/// element of the result of the vector shuffle.
5743static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5744 MVT::ValueType VT = N->getValueType(0);
5745 SDOperand PermMask = N->getOperand(2);
5746 unsigned NumElems = PermMask.getNumOperands();
5747 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5748 i %= NumElems;
5749 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5750 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005751 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005752 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5753 SDOperand Idx = PermMask.getOperand(i);
5754 if (Idx.getOpcode() == ISD::UNDEF)
5755 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5756 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5757 }
5758 return SDOperand();
5759}
5760
5761/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5762/// node is a GlobalAddress + an offset.
5763static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5764 unsigned Opc = N->getOpcode();
5765 if (Opc == X86ISD::Wrapper) {
5766 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5767 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5768 return true;
5769 }
5770 } else if (Opc == ISD::ADD) {
5771 SDOperand N1 = N->getOperand(0);
5772 SDOperand N2 = N->getOperand(1);
5773 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5774 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5775 if (V) {
5776 Offset += V->getSignExtended();
5777 return true;
5778 }
5779 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5780 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5781 if (V) {
5782 Offset += V->getSignExtended();
5783 return true;
5784 }
5785 }
5786 }
5787 return false;
5788}
5789
5790/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5791/// + Dist * Size.
5792static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5793 MachineFrameInfo *MFI) {
5794 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5795 return false;
5796
5797 SDOperand Loc = N->getOperand(1);
5798 SDOperand BaseLoc = Base->getOperand(1);
5799 if (Loc.getOpcode() == ISD::FrameIndex) {
5800 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5801 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005802 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5803 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005804 int FS = MFI->getObjectSize(FI);
5805 int BFS = MFI->getObjectSize(BFI);
5806 if (FS != BFS || FS != Size) return false;
5807 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5808 } else {
5809 GlobalValue *GV1 = NULL;
5810 GlobalValue *GV2 = NULL;
5811 int64_t Offset1 = 0;
5812 int64_t Offset2 = 0;
5813 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5814 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5815 if (isGA1 && isGA2 && GV1 == GV2)
5816 return Offset1 == (Offset2 + Dist*Size);
5817 }
5818
5819 return false;
5820}
5821
5822static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5823 const X86Subtarget *Subtarget) {
5824 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005825 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005826 if (isGAPlusOffset(Base, GV, Offset))
5827 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005828 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005829 return false;
5830}
5831
5832
5833/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5834/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5835/// if the load addresses are consecutive, non-overlapping, and in the right
5836/// order.
5837static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5838 const X86Subtarget *Subtarget) {
5839 MachineFunction &MF = DAG.getMachineFunction();
5840 MachineFrameInfo *MFI = MF.getFrameInfo();
5841 MVT::ValueType VT = N->getValueType(0);
5842 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5843 SDOperand PermMask = N->getOperand(2);
5844 int NumElems = (int)PermMask.getNumOperands();
5845 SDNode *Base = NULL;
5846 for (int i = 0; i < NumElems; ++i) {
5847 SDOperand Idx = PermMask.getOperand(i);
5848 if (Idx.getOpcode() == ISD::UNDEF) {
5849 if (!Base) return SDOperand();
5850 } else {
5851 SDOperand Arg =
5852 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5853 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5854 return SDOperand();
5855 if (!Base)
5856 Base = Arg.Val;
5857 else if (!isConsecutiveLoad(Arg.Val, Base,
5858 i, MVT::getSizeInBits(EVT)/8,MFI))
5859 return SDOperand();
5860 }
5861 }
5862
5863 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005864 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005865 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005866 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005867 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005868 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005869 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5870 LD->getSrcValueOffset(), LD->isVolatile(),
5871 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005872 }
5873}
5874
5875/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5876static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5877 const X86Subtarget *Subtarget) {
5878 SDOperand Cond = N->getOperand(0);
5879
5880 // If we have SSE[12] support, try to form min/max nodes.
5881 if (Subtarget->hasSSE2() &&
5882 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5883 if (Cond.getOpcode() == ISD::SETCC) {
5884 // Get the LHS/RHS of the select.
5885 SDOperand LHS = N->getOperand(1);
5886 SDOperand RHS = N->getOperand(2);
5887 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5888
5889 unsigned Opcode = 0;
5890 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5891 switch (CC) {
5892 default: break;
5893 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5894 case ISD::SETULE:
5895 case ISD::SETLE:
5896 if (!UnsafeFPMath) break;
5897 // FALL THROUGH.
5898 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5899 case ISD::SETLT:
5900 Opcode = X86ISD::FMIN;
5901 break;
5902
5903 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5904 case ISD::SETUGT:
5905 case ISD::SETGT:
5906 if (!UnsafeFPMath) break;
5907 // FALL THROUGH.
5908 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5909 case ISD::SETGE:
5910 Opcode = X86ISD::FMAX;
5911 break;
5912 }
5913 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5914 switch (CC) {
5915 default: break;
5916 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5917 case ISD::SETUGT:
5918 case ISD::SETGT:
5919 if (!UnsafeFPMath) break;
5920 // FALL THROUGH.
5921 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5922 case ISD::SETGE:
5923 Opcode = X86ISD::FMIN;
5924 break;
5925
5926 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5927 case ISD::SETULE:
5928 case ISD::SETLE:
5929 if (!UnsafeFPMath) break;
5930 // FALL THROUGH.
5931 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5932 case ISD::SETLT:
5933 Opcode = X86ISD::FMAX;
5934 break;
5935 }
5936 }
5937
5938 if (Opcode)
5939 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5940 }
5941
5942 }
5943
5944 return SDOperand();
5945}
5946
Chris Lattnerce84ae42008-02-22 02:09:43 +00005947/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
5948static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
5949 const X86Subtarget *Subtarget) {
5950 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
5951 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00005952 // A preferable solution to the general problem is to figure out the right
5953 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00005954 if (MVT::isVector(St->getValue().getValueType()) &&
5955 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00005956 isa<LoadSDNode>(St->getValue()) &&
5957 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
5958 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005959 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00005960 LoadSDNode *Ld = 0;
5961 int TokenFactorIndex = -1;
5962 SmallVector<SDOperand, 8> Ops;
5963 SDNode* ChainVal = St->getChain().Val;
5964 // Must be a store of a load. We currently handle two cases: the load
5965 // is a direct child, and it's under an intervening TokenFactor. It is
5966 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00005967 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00005968 Ld = cast<LoadSDNode>(St->getChain());
5969 else if (St->getValue().hasOneUse() &&
5970 ChainVal->getOpcode() == ISD::TokenFactor) {
5971 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005972 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00005973 TokenFactorIndex = i;
5974 Ld = cast<LoadSDNode>(St->getValue());
5975 } else
5976 Ops.push_back(ChainVal->getOperand(i));
5977 }
5978 }
5979 if (Ld) {
5980 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
5981 if (Subtarget->is64Bit()) {
5982 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
5983 Ld->getBasePtr(), Ld->getSrcValue(),
5984 Ld->getSrcValueOffset(), Ld->isVolatile(),
5985 Ld->getAlignment());
5986 SDOperand NewChain = NewLd.getValue(1);
5987 if (TokenFactorIndex != -1) {
5988 Ops.push_back(NewLd);
5989 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
5990 Ops.size());
5991 }
5992 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
5993 St->getSrcValue(), St->getSrcValueOffset(),
5994 St->isVolatile(), St->getAlignment());
5995 }
5996
5997 // Otherwise, lower to two 32-bit copies.
5998 SDOperand LoAddr = Ld->getBasePtr();
5999 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6000 DAG.getConstant(MVT::i32, 4));
6001
6002 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6003 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6004 Ld->isVolatile(), Ld->getAlignment());
6005 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6006 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6007 Ld->isVolatile(),
6008 MinAlign(Ld->getAlignment(), 4));
6009
6010 SDOperand NewChain = LoLd.getValue(1);
6011 if (TokenFactorIndex != -1) {
6012 Ops.push_back(LoLd);
6013 Ops.push_back(HiLd);
6014 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6015 Ops.size());
6016 }
6017
6018 LoAddr = St->getBasePtr();
6019 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6020 DAG.getConstant(MVT::i32, 4));
6021
6022 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006023 St->getSrcValue(), St->getSrcValueOffset(),
6024 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006025 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6026 St->getSrcValue(), St->getSrcValueOffset()+4,
6027 St->isVolatile(),
6028 MinAlign(St->getAlignment(), 4));
6029 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006030 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006031 }
6032 return SDOperand();
6033}
6034
Chris Lattner470d5dc2008-01-25 06:14:17 +00006035/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6036/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006037static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006038 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6039 // F[X]OR(0.0, x) -> x
6040 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006041 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6042 if (C->getValueAPF().isPosZero())
6043 return N->getOperand(1);
6044 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6045 if (C->getValueAPF().isPosZero())
6046 return N->getOperand(0);
6047 return SDOperand();
6048}
6049
6050/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6051static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6052 // FAND(0.0, x) -> 0.0
6053 // FAND(x, 0.0) -> 0.0
6054 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6055 if (C->getValueAPF().isPosZero())
6056 return N->getOperand(0);
6057 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6058 if (C->getValueAPF().isPosZero())
6059 return N->getOperand(1);
6060 return SDOperand();
6061}
6062
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006063
6064SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6065 DAGCombinerInfo &DCI) const {
6066 SelectionDAG &DAG = DCI.DAG;
6067 switch (N->getOpcode()) {
6068 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006069 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6070 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006071 case ISD::STORE:
6072 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006073 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006074 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6075 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006076 }
6077
6078 return SDOperand();
6079}
6080
6081//===----------------------------------------------------------------------===//
6082// X86 Inline Assembly Support
6083//===----------------------------------------------------------------------===//
6084
6085/// getConstraintType - Given a constraint letter, return the type of
6086/// constraint it is for this target.
6087X86TargetLowering::ConstraintType
6088X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6089 if (Constraint.size() == 1) {
6090 switch (Constraint[0]) {
6091 case 'A':
6092 case 'r':
6093 case 'R':
6094 case 'l':
6095 case 'q':
6096 case 'Q':
6097 case 'x':
6098 case 'Y':
6099 return C_RegisterClass;
6100 default:
6101 break;
6102 }
6103 }
6104 return TargetLowering::getConstraintType(Constraint);
6105}
6106
Dale Johannesene99fc902008-01-29 02:21:21 +00006107/// LowerXConstraint - try to replace an X constraint, which matches anything,
6108/// with another that has more specific requirements based on the type of the
6109/// corresponding operand.
6110void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6111 std::string& s) const {
6112 if (MVT::isFloatingPoint(ConstraintVT)) {
6113 if (Subtarget->hasSSE2())
6114 s = "Y";
6115 else if (Subtarget->hasSSE1())
6116 s = "x";
6117 else
6118 s = "f";
6119 } else
6120 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6121}
6122
Chris Lattnera531abc2007-08-25 00:47:38 +00006123/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6124/// vector. If it is invalid, don't add anything to Ops.
6125void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6126 char Constraint,
6127 std::vector<SDOperand>&Ops,
6128 SelectionDAG &DAG) {
6129 SDOperand Result(0, 0);
6130
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006131 switch (Constraint) {
6132 default: break;
6133 case 'I':
6134 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006135 if (C->getValue() <= 31) {
6136 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6137 break;
6138 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006139 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006140 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006141 case 'N':
6142 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006143 if (C->getValue() <= 255) {
6144 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6145 break;
6146 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006147 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006148 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006149 case 'i': {
6150 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006151 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6152 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6153 break;
6154 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006155
6156 // If we are in non-pic codegen mode, we allow the address of a global (with
6157 // an optional displacement) to be used with 'i'.
6158 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6159 int64_t Offset = 0;
6160
6161 // Match either (GA) or (GA+C)
6162 if (GA) {
6163 Offset = GA->getOffset();
6164 } else if (Op.getOpcode() == ISD::ADD) {
6165 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6166 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6167 if (C && GA) {
6168 Offset = GA->getOffset()+C->getValue();
6169 } else {
6170 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6171 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6172 if (C && GA)
6173 Offset = GA->getOffset()+C->getValue();
6174 else
6175 C = 0, GA = 0;
6176 }
6177 }
6178
6179 if (GA) {
6180 // If addressing this global requires a load (e.g. in PIC mode), we can't
6181 // match.
6182 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6183 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006184 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006185
6186 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6187 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006188 Result = Op;
6189 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006190 }
6191
6192 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006193 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006194 }
6195 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006196
6197 if (Result.Val) {
6198 Ops.push_back(Result);
6199 return;
6200 }
6201 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006202}
6203
6204std::vector<unsigned> X86TargetLowering::
6205getRegClassForInlineAsmConstraint(const std::string &Constraint,
6206 MVT::ValueType VT) const {
6207 if (Constraint.size() == 1) {
6208 // FIXME: not handling fp-stack yet!
6209 switch (Constraint[0]) { // GCC X86 Constraint Letters
6210 default: break; // Unknown constraint letter
6211 case 'A': // EAX/EDX
6212 if (VT == MVT::i32 || VT == MVT::i64)
6213 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6214 break;
6215 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6216 case 'Q': // Q_REGS
6217 if (VT == MVT::i32)
6218 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6219 else if (VT == MVT::i16)
6220 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6221 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006222 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006223 else if (VT == MVT::i64)
6224 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6225 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006226 }
6227 }
6228
6229 return std::vector<unsigned>();
6230}
6231
6232std::pair<unsigned, const TargetRegisterClass*>
6233X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6234 MVT::ValueType VT) const {
6235 // First, see if this is a constraint that directly corresponds to an LLVM
6236 // register class.
6237 if (Constraint.size() == 1) {
6238 // GCC Constraint Letters
6239 switch (Constraint[0]) {
6240 default: break;
6241 case 'r': // GENERAL_REGS
6242 case 'R': // LEGACY_REGS
6243 case 'l': // INDEX_REGS
6244 if (VT == MVT::i64 && Subtarget->is64Bit())
6245 return std::make_pair(0U, X86::GR64RegisterClass);
6246 if (VT == MVT::i32)
6247 return std::make_pair(0U, X86::GR32RegisterClass);
6248 else if (VT == MVT::i16)
6249 return std::make_pair(0U, X86::GR16RegisterClass);
6250 else if (VT == MVT::i8)
6251 return std::make_pair(0U, X86::GR8RegisterClass);
6252 break;
6253 case 'y': // MMX_REGS if MMX allowed.
6254 if (!Subtarget->hasMMX()) break;
6255 return std::make_pair(0U, X86::VR64RegisterClass);
6256 break;
6257 case 'Y': // SSE_REGS if SSE2 allowed
6258 if (!Subtarget->hasSSE2()) break;
6259 // FALL THROUGH.
6260 case 'x': // SSE_REGS if SSE1 allowed
6261 if (!Subtarget->hasSSE1()) break;
6262
6263 switch (VT) {
6264 default: break;
6265 // Scalar SSE types.
6266 case MVT::f32:
6267 case MVT::i32:
6268 return std::make_pair(0U, X86::FR32RegisterClass);
6269 case MVT::f64:
6270 case MVT::i64:
6271 return std::make_pair(0U, X86::FR64RegisterClass);
6272 // Vector types.
6273 case MVT::v16i8:
6274 case MVT::v8i16:
6275 case MVT::v4i32:
6276 case MVT::v2i64:
6277 case MVT::v4f32:
6278 case MVT::v2f64:
6279 return std::make_pair(0U, X86::VR128RegisterClass);
6280 }
6281 break;
6282 }
6283 }
6284
6285 // Use the default implementation in TargetLowering to convert the register
6286 // constraint into a member of a register class.
6287 std::pair<unsigned, const TargetRegisterClass*> Res;
6288 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6289
6290 // Not found as a standard register?
6291 if (Res.second == 0) {
6292 // GCC calls "st(0)" just plain "st".
6293 if (StringsEqualNoCase("{st}", Constraint)) {
6294 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006295 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006296 }
6297
6298 return Res;
6299 }
6300
6301 // Otherwise, check to see if this is a register class of the wrong value
6302 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6303 // turn into {ax},{dx}.
6304 if (Res.second->hasType(VT))
6305 return Res; // Correct type already, nothing to do.
6306
6307 // All of the single-register GCC register classes map their values onto
6308 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6309 // really want an 8-bit or 32-bit register, map to the appropriate register
6310 // class and return the appropriate register.
6311 if (Res.second != X86::GR16RegisterClass)
6312 return Res;
6313
6314 if (VT == MVT::i8) {
6315 unsigned DestReg = 0;
6316 switch (Res.first) {
6317 default: break;
6318 case X86::AX: DestReg = X86::AL; break;
6319 case X86::DX: DestReg = X86::DL; break;
6320 case X86::CX: DestReg = X86::CL; break;
6321 case X86::BX: DestReg = X86::BL; break;
6322 }
6323 if (DestReg) {
6324 Res.first = DestReg;
6325 Res.second = Res.second = X86::GR8RegisterClass;
6326 }
6327 } else if (VT == MVT::i32) {
6328 unsigned DestReg = 0;
6329 switch (Res.first) {
6330 default: break;
6331 case X86::AX: DestReg = X86::EAX; break;
6332 case X86::DX: DestReg = X86::EDX; break;
6333 case X86::CX: DestReg = X86::ECX; break;
6334 case X86::BX: DestReg = X86::EBX; break;
6335 case X86::SI: DestReg = X86::ESI; break;
6336 case X86::DI: DestReg = X86::EDI; break;
6337 case X86::BP: DestReg = X86::EBP; break;
6338 case X86::SP: DestReg = X86::ESP; break;
6339 }
6340 if (DestReg) {
6341 Res.first = DestReg;
6342 Res.second = Res.second = X86::GR32RegisterClass;
6343 }
6344 } else if (VT == MVT::i64) {
6345 unsigned DestReg = 0;
6346 switch (Res.first) {
6347 default: break;
6348 case X86::AX: DestReg = X86::RAX; break;
6349 case X86::DX: DestReg = X86::RDX; break;
6350 case X86::CX: DestReg = X86::RCX; break;
6351 case X86::BX: DestReg = X86::RBX; break;
6352 case X86::SI: DestReg = X86::RSI; break;
6353 case X86::DI: DestReg = X86::RDI; break;
6354 case X86::BP: DestReg = X86::RBP; break;
6355 case X86::SP: DestReg = X86::RSP; break;
6356 }
6357 if (DestReg) {
6358 Res.first = DestReg;
6359 Res.second = Res.second = X86::GR64RegisterClass;
6360 }
6361 }
6362
6363 return Res;
6364}