blob: 52fec8a92f11b4916dad3d1f24276a321a5ec2ab [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
Gordon Henriksen18ace102008-01-05 16:56:59 +0000990// Determines whether a CALL node uses struct return semantics.
991static bool CallIsStructReturn(SDOperand Op) {
992 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
993 if (!NumOps)
994 return false;
995
996 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
997 return Flags->getValue() & ISD::ParamFlags::StructReturn;
998}
999
1000// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
1001static bool ArgsAreStructReturn(SDOperand Op) {
1002 unsigned NumArgs = Op.Val->getNumValues() - 1;
1003 if (!NumArgs)
1004 return false;
1005
1006 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1007 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1008}
1009
1010// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
1011// its own arguments. Callee pop is necessary to support tail calls.
1012bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1013 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1014 if (IsVarArg)
1015 return false;
1016
1017 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1018 default:
1019 return false;
1020 case CallingConv::X86_StdCall:
1021 return !Subtarget->is64Bit();
1022 case CallingConv::X86_FastCall:
1023 return !Subtarget->is64Bit();
1024 case CallingConv::Fast:
1025 return PerformTailCallOpt;
1026 }
1027}
1028
1029// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
1030CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1031 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1032
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001033 if (Subtarget->is64Bit()) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001034 if (CC == CallingConv::Fast && PerformTailCallOpt)
1035 return CC_X86_64_TailCall;
1036 else
1037 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001038 }
1039
Gordon Henriksen18ace102008-01-05 16:56:59 +00001040 if (CC == CallingConv::X86_FastCall)
1041 return CC_X86_32_FastCall;
1042 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1043 return CC_X86_32_TailCall;
1044 else
1045 return CC_X86_32_C;
1046}
1047
1048// Selects the appropriate decoration to apply to a MachineFunction containing a
1049// given FORMAL_ARGUMENTS node.
1050NameDecorationStyle
1051X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1052 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1053 if (CC == CallingConv::X86_FastCall)
1054 return FastCall;
1055 else if (CC == CallingConv::X86_StdCall)
1056 return StdCall;
1057 return None;
1058}
1059
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001060// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly
1061// be overwritten when lowering the outgoing arguments in a tail call. Currently
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001062// the implementation of this call is very conservative and assumes all
1063// arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001064// registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001065static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1066 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001067 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001068 FrameIndexSDNode * FrameIdxNode = NULL;
1069 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001070 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1071 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001072 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1073 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1074 (Op.getOpcode() == ISD::LOAD &&
1075 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1076 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1077 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001078 return true;
1079 return false;
1080}
1081
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001082// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1083// arguments to force loading and guarantee that arguments sourcing from
1084// incomming parameters are not overwriting each other.
1085static SDOperand
1086CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1087 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1088 SelectionDAG &DAG,
1089 MachineFunction &MF,
1090 const TargetLowering * TL) {
1091
1092 SDOperand InFlag;
1093 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1094 SDOperand Arg = TailCallClobberedVRegs[i].second;
1095 unsigned Idx = TailCallClobberedVRegs[i].first;
1096 unsigned VReg =
1097 MF.getRegInfo().
1098 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1099 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1100 InFlag = Chain.getValue(1);
1101 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1102 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1103 Chain = Arg.getValue(1);
1104 InFlag = Arg.getValue(2);
1105 }
1106 return Chain;
1107}
1108
Evan Cheng5817a0e2008-01-12 01:08:07 +00001109// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1110// by "Src" to address "Dst" with size and alignment information specified by
1111// the specific parameter attribute. The copy will be passed as a byval function
1112// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001113static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001114CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1115 unsigned Flags, SelectionDAG &DAG) {
1116 unsigned Align = 1 <<
1117 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1118 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001119 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001120 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1121 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001122 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001123 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001124}
1125
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001126SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1127 const CCValAssign &VA,
1128 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001129 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001130 SDOperand Root, unsigned i) {
1131 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001132 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001133 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Evan Cheng3e42a522008-01-10 02:24:25 +00001134 bool isByVal = Flags & ISD::ParamFlags::ByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001135 bool isImmutable = !AlwaysUseMutable && !isByVal;
Evan Cheng3e42a522008-01-10 02:24:25 +00001136
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001137 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1138 // changed with more analysis.
1139 // In case of tail call optimization mark all arguments mutable. Since they
1140 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001141 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001142 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001143 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001144 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001145 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001146 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001147 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001148}
1149
Gordon Henriksen18ace102008-01-05 16:56:59 +00001150SDOperand
1151X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001153 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1154
1155 const Function* Fn = MF.getFunction();
1156 if (Fn->hasExternalLinkage() &&
1157 Subtarget->isTargetCygMing() &&
1158 Fn->getName() == "main")
1159 FuncInfo->setForceFramePointer(true);
1160
1161 // Decorate the function name.
1162 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 MachineFrameInfo *MFI = MF.getFrameInfo();
1165 SDOperand Root = Op.getOperand(0);
1166 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001167 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001168 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001169
1170 assert(!(isVarArg && CC == CallingConv::Fast) &&
1171 "Var args not supported with calling convention fastcc");
1172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 // Assign locations to all of the incoming arguments.
1174 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001175 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001176 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001177
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178 SmallVector<SDOperand, 8> ArgValues;
1179 unsigned LastVal = ~0U;
1180 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1181 CCValAssign &VA = ArgLocs[i];
1182 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1183 // places.
1184 assert(VA.getValNo() != LastVal &&
1185 "Don't support value assigned to multiple locs yet");
1186 LastVal = VA.getValNo();
1187
1188 if (VA.isRegLoc()) {
1189 MVT::ValueType RegVT = VA.getLocVT();
1190 TargetRegisterClass *RC;
1191 if (RegVT == MVT::i32)
1192 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001193 else if (Is64Bit && RegVT == MVT::i64)
1194 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001195 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001196 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001197 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001198 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199 else {
1200 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001201 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1202 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1203 RegVT = MVT::i64;
1204 } else
1205 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001207
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1209 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1210
1211 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1212 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1213 // right size.
1214 if (VA.getLocInfo() == CCValAssign::SExt)
1215 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1216 DAG.getValueType(VA.getValVT()));
1217 else if (VA.getLocInfo() == CCValAssign::ZExt)
1218 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1219 DAG.getValueType(VA.getValVT()));
1220
1221 if (VA.getLocInfo() != CCValAssign::Full)
1222 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1223
Gordon Henriksen18ace102008-01-05 16:56:59 +00001224 // Handle MMX values passed in GPRs.
1225 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1226 MVT::getSizeInBits(RegVT) == 64)
1227 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1228
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229 ArgValues.push_back(ArgValue);
1230 } else {
1231 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001232 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 }
1234 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001237 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001238 if (CC == CallingConv::Fast)
1239 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240
1241 // If the function takes variable number of arguments, make a frame index for
1242 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001243 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001244 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1245 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1246 }
1247 if (Is64Bit) {
1248 static const unsigned GPR64ArgRegs[] = {
1249 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1250 };
1251 static const unsigned XMMArgRegs[] = {
1252 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1253 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1254 };
1255
1256 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1257 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1258
1259 // For X86-64, if there are vararg parameters that are passed via
1260 // registers, then we must store them to their spots on the stack so they
1261 // may be loaded by deferencing the result of va_next.
1262 VarArgsGPOffset = NumIntRegs * 8;
1263 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1264 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1265
1266 // Store the integer parameter registers.
1267 SmallVector<SDOperand, 8> MemOps;
1268 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1269 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001270 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001271 for (; NumIntRegs != 6; ++NumIntRegs) {
1272 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1273 X86::GR64RegisterClass);
1274 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001275 SDOperand Store =
1276 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001277 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001278 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001279 MemOps.push_back(Store);
1280 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001281 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001282 }
1283
1284 // Now store the XMM (fp + vector) parameter registers.
1285 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001286 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001287 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1288 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1289 X86::VR128RegisterClass);
1290 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001291 SDOperand Store =
1292 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001293 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001294 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001295 MemOps.push_back(Store);
1296 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001297 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001298 }
1299 if (!MemOps.empty())
1300 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1301 &MemOps[0], MemOps.size());
1302 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001303 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001304
1305 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1306 // arguments and the arguments after the retaddr has been pushed are
1307 // aligned.
1308 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1309 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1310 (StackSize & 7) == 0)
1311 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001313 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001314
Gordon Henriksen18ace102008-01-05 16:56:59 +00001315 // Some CCs need callee pop.
1316 if (IsCalleePop(Op)) {
1317 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 BytesCallerReserves = 0;
1319 } else {
1320 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001322 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001323 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 BytesCallerReserves = StackSize;
1325 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001326
Gordon Henriksen18ace102008-01-05 16:56:59 +00001327 if (!Is64Bit) {
1328 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1329 if (CC == CallingConv::X86_FastCall)
1330 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1331 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332
Anton Korobeynikove844e472007-08-15 17:12:32 +00001333 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334
1335 // Return the new list of results.
1336 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1337 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1338}
1339
Evan Chengbc077bf2008-01-10 00:09:10 +00001340SDOperand
1341X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1342 const SDOperand &StackPtr,
1343 const CCValAssign &VA,
1344 SDOperand Chain,
1345 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001346 unsigned LocMemOffset = VA.getLocMemOffset();
1347 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001348 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1349 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1350 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1351 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001352 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001353 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001354 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001355 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001356}
1357
Evan Cheng931a8f42008-01-29 19:34:22 +00001358/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1359/// struct return call to the specified function. X86-64 ABI specifies
1360/// some SRet calls are actually returned in registers. Since current
1361/// LLVM cannot represent multi-value calls, they are represent as
1362/// calls where the results are passed in a hidden struct provided by
1363/// the caller. This function examines the type of the struct to
1364/// determine the correct way to implement the call.
1365X86::X86_64SRet
1366X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1367 // FIXME: Disabled for now.
1368 return X86::InMemory;
1369
1370 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1371 const Type *RTy = PTy->getElementType();
1372 unsigned Size = getTargetData()->getABITypeSize(RTy);
1373 if (Size != 16 && Size != 32)
1374 return X86::InMemory;
1375
1376 if (Size == 32) {
1377 const StructType *STy = dyn_cast<StructType>(RTy);
1378 if (!STy) return X86::InMemory;
1379 if (STy->getNumElements() == 2 &&
1380 STy->getElementType(0) == Type::X86_FP80Ty &&
1381 STy->getElementType(1) == Type::X86_FP80Ty)
1382 return X86::InX87;
1383 }
1384
1385 bool AllFP = true;
1386 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1387 I != E; ++I) {
1388 const Type *STy = I->get();
1389 if (!STy->isFPOrFPVector()) {
1390 AllFP = false;
1391 break;
1392 }
1393 }
1394
1395 if (AllFP)
1396 return X86::InSSE;
1397 return X86::InGPR64;
1398}
1399
1400void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1401 CCAssignFn *Fn,
1402 CCState &CCInfo) {
1403 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1404 for (unsigned i = 1; i != NumOps; ++i) {
1405 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1406 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1407 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1408 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1409 cerr << "Call operand #" << i << " has unhandled type "
1410 << MVT::getValueTypeString(ArgVT) << "\n";
1411 abort();
1412 }
1413 }
1414}
1415
Gordon Henriksen18ace102008-01-05 16:56:59 +00001416SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1417 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001418 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001419 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001420 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001421 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001422 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1423 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001425 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001426 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001427
1428 assert(!(isVarArg && CC == CallingConv::Fast) &&
1429 "Var args not supported with calling convention fastcc");
1430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 // Analyze operands of the call, assigning locations to each operand.
1432 SmallVector<CCValAssign, 16> ArgLocs;
1433 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001434 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1435
1436 X86::X86_64SRet SRetMethod = X86::InMemory;
1437 if (Is64Bit && IsStructRet)
1438 // FIXME: We can't figure out type of the sret structure for indirect
1439 // calls. We need to copy more information from CallSite to the ISD::CALL
1440 // node.
1441 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1442 SRetMethod =
1443 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1444
1445 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1446 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1447 // a sret call.
1448 if (SRetMethod != X86::InMemory)
1449 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1450 else
1451 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452
1453 // Get a count of how many bytes are to be pushed on the stack.
1454 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001455 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001456 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457
Gordon Henriksen18ace102008-01-05 16:56:59 +00001458 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1459 // arguments and the arguments after the retaddr has been pushed are aligned.
1460 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1461 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1462 (NumBytes & 7) == 0)
1463 NumBytes += 4;
1464
1465 int FPDiff = 0;
1466 if (IsTailCall) {
1467 // Lower arguments at fp - stackoffset + fpdiff.
1468 unsigned NumBytesCallerPushed =
1469 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1470 FPDiff = NumBytesCallerPushed - NumBytes;
1471
1472 // Set the delta of movement of the returnaddr stackslot.
1473 // But only set if delta is greater than previous delta.
1474 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1475 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1476 }
1477
Chris Lattner5872a362008-01-17 07:00:52 +00001478 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001480 SDOperand RetAddrFrIdx;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001481 if (IsTailCall) {
1482 // Adjust the Return address stack slot.
1483 if (FPDiff) {
1484 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1485 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1486 // Load the "old" Return address.
1487 RetAddrFrIdx =
1488 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001489 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1490 }
1491 }
1492
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001494 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 SmallVector<SDOperand, 8> MemOpChains;
1496
1497 SDOperand StackPtr;
1498
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001499 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001500 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1502 CCValAssign &VA = ArgLocs[i];
1503 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1504
1505 // Promote the value if needed.
1506 switch (VA.getLocInfo()) {
1507 default: assert(0 && "Unknown loc info!");
1508 case CCValAssign::Full: break;
1509 case CCValAssign::SExt:
1510 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1511 break;
1512 case CCValAssign::ZExt:
1513 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1514 break;
1515 case CCValAssign::AExt:
1516 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1517 break;
1518 }
1519
1520 if (VA.isRegLoc()) {
1521 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1522 } else {
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001523 if (!IsTailCall) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001524 assert(VA.isMemLoc());
1525 if (StackPtr.Val == 0)
1526 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1527
1528 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1529 Arg));
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001530 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1531 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001532 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001533 }
1534 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535
1536 if (!MemOpChains.empty())
1537 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1538 &MemOpChains[0], MemOpChains.size());
1539
1540 // Build a sequence of copy-to-reg nodes chained together with token chain
1541 // and flag operands which copy the outgoing args into registers.
1542 SDOperand InFlag;
1543 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1544 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1545 InFlag);
1546 InFlag = Chain.getValue(1);
1547 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001548
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1550 // GOT pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001551 // Does not work with tail call since ebx is not restored correctly by
1552 // tailcaller. TODO: at least for x86 - verify for x86-64
1553 if (!IsTailCall && !Is64Bit &&
1554 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 Subtarget->isPICStyleGOT()) {
1556 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1557 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1558 InFlag);
1559 InFlag = Chain.getValue(1);
1560 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001561
Gordon Henriksen18ace102008-01-05 16:56:59 +00001562 if (Is64Bit && isVarArg) {
1563 // From AMD64 ABI document:
1564 // For calls that may call functions that use varargs or stdargs
1565 // (prototype-less calls or calls to functions containing ellipsis (...) in
1566 // the declaration) %al is used as hidden argument to specify the number
1567 // of SSE registers used. The contents of %al do not need to match exactly
1568 // the number of registers, but must be an ubound on the number of SSE
1569 // registers used and is in the range 0 - 8 inclusive.
1570
1571 // Count the number of XMM registers allocated.
1572 static const unsigned XMMArgRegs[] = {
1573 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1574 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1575 };
1576 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1577
1578 Chain = DAG.getCopyToReg(Chain, X86::AL,
1579 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1580 InFlag = Chain.getValue(1);
1581 }
1582
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001583
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001584 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001585 if (IsTailCall) {
1586 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001587 SDOperand FIN;
1588 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001589 // Do not flag preceeding copytoreg stuff together with the following stuff.
1590 InFlag = SDOperand();
1591
1592 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1593 DAG, MF, this);
1594
Gordon Henriksen18ace102008-01-05 16:56:59 +00001595 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1596 CCValAssign &VA = ArgLocs[i];
1597 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001598 assert(VA.isMemLoc());
1599 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001600 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1601 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001602 // Create frame index.
1603 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1604 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1605 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1606 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001607
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001608 // Find virtual register for this argument.
1609 bool Found=false;
1610 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1611 if (TailCallClobberedVRegs[idx].first==i) {
1612 Arg = TailCallClobberedVRegs[idx].second;
1613 Found=true;
1614 break;
1615 }
1616 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1617 (Found==true && "No corresponding Argument was found"));
1618
Gordon Henriksen18ace102008-01-05 16:56:59 +00001619 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001620 // Copy relative to framepointer.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001621 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001622 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001623 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001624 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001625 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001626 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001627 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001628 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001629 }
1630 }
1631
1632 if (!MemOpChains2.empty())
1633 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001634 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001635
1636 // Store the return address to the appropriate stack slot.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001637 if (FPDiff) {
1638 // Calculate the new stack slot for the return address.
1639 int SlotSize = Is64Bit ? 8 : 4;
1640 int NewReturnAddrFI =
1641 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1642 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1643 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1644 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1645 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1646 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001647 }
1648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 // If the callee is a GlobalAddress node (quite common, every direct call is)
1650 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1651 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1652 // We should use extra load for direct calls to dllimported functions in
1653 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 if ((IsTailCall || !Is64Bit ||
1655 getTargetMachine().getCodeModel() != CodeModel::Large)
1656 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1657 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001659 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 if (IsTailCall || !Is64Bit ||
1661 getTargetMachine().getCodeModel() != CodeModel::Large)
1662 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1663 } else if (IsTailCall) {
1664 assert(Callee.getOpcode() == ISD::LOAD &&
1665 "Function destination must be loaded into virtual register");
1666 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1667
1668 Chain = DAG.getCopyToReg(Chain,
1669 DAG.getRegister(Opc, getPointerTy()) ,
1670 Callee,InFlag);
1671 Callee = DAG.getRegister(Opc, getPointerTy());
1672 // Add register as live out.
1673 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001674 }
1675
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676 // Returns a chain & a flag for retval copy to use.
1677 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1678 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001679
1680 if (IsTailCall) {
1681 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001682 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1683 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001684 if (InFlag.Val)
1685 Ops.push_back(InFlag);
1686 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1687 InFlag = Chain.getValue(1);
1688
1689 // Returns a chain & a flag for retval copy to use.
1690 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1691 Ops.clear();
1692 }
1693
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 Ops.push_back(Chain);
1695 Ops.push_back(Callee);
1696
Gordon Henriksen18ace102008-01-05 16:56:59 +00001697 if (IsTailCall)
1698 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699
1700 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001701 if (!IsTailCall && !Is64Bit &&
1702 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703 Subtarget->isPICStyleGOT())
1704 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001705
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 // Add argument registers to the end of the list so that they are known live
1707 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001708 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1709 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1710 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001711
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001712 if (InFlag.Val)
1713 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001714
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715 if (IsTailCall) {
1716 assert(InFlag.Val &&
1717 "Flag must be set. Depend on flag being set in LowerRET");
1718 Chain = DAG.getNode(X86ISD::TAILCALL,
1719 Op.Val->getVTList(), &Ops[0], Ops.size());
1720
1721 return SDOperand(Chain.Val, Op.ResNo);
1722 }
1723
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001724 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001725 InFlag = Chain.getValue(1);
1726
1727 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001728 unsigned NumBytesForCalleeToPush;
1729 if (IsCalleePop(Op))
1730 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001731 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001732 // If this is is a call to a struct-return function, the callee
1733 // pops the hidden struct pointer, so we have to push it back.
1734 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001735 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001736 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001737 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001739 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001740 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001741 DAG.getIntPtrConstant(NumBytes),
1742 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001743 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001744 InFlag = Chain.getValue(1);
1745
1746 // Handle result values, copying them out of physregs into vregs that we
1747 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001748 switch (SRetMethod) {
1749 default:
1750 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1751 case X86::InGPR64:
1752 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1753 X86::RAX, X86::RDX,
1754 MVT::i64, DAG), Op.ResNo);
1755 case X86::InSSE:
1756 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1757 X86::XMM0, X86::XMM1,
1758 MVT::f64, DAG), Op.ResNo);
1759 case X86::InX87:
1760 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1761 Op.ResNo);
1762 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001763}
1764
1765
1766//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001767// Fast Calling Convention (tail call) implementation
1768//===----------------------------------------------------------------------===//
1769
1770// Like std call, callee cleans arguments, convention except that ECX is
1771// reserved for storing the tail called function address. Only 2 registers are
1772// free for argument passing (inreg). Tail call optimization is performed
1773// provided:
1774// * tailcallopt is enabled
1775// * caller/callee are fastcc
1776// * elf/pic is disabled OR
1777// * elf/pic enabled + callee is in module + callee has
1778// visibility protected or hidden
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001779// To keep the stack aligned according to platform abi the function
1780// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1781// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001782// If a tail called function callee has more arguments than the caller the
1783// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001784// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001785// original REtADDR, but before the saved framepointer or the spilled registers
1786// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1787// stack layout:
1788// arg1
1789// arg2
1790// RETADDR
1791// [ new RETADDR
1792// move area ]
1793// (possible EBP)
1794// ESI
1795// EDI
1796// local1 ..
1797
1798/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1799/// for a 16 byte align requirement.
1800unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1801 SelectionDAG& DAG) {
1802 if (PerformTailCallOpt) {
1803 MachineFunction &MF = DAG.getMachineFunction();
1804 const TargetMachine &TM = MF.getTarget();
1805 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1806 unsigned StackAlignment = TFI.getStackAlignment();
1807 uint64_t AlignMask = StackAlignment - 1;
1808 int64_t Offset = StackSize;
1809 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1810 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1811 // Number smaller than 12 so just add the difference.
1812 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1813 } else {
1814 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1815 Offset = ((~AlignMask) & Offset) + StackAlignment +
1816 (StackAlignment-SlotSize);
1817 }
1818 StackSize = Offset;
1819 }
1820 return StackSize;
1821}
1822
1823/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001824/// following the call is a return. A function is eligible if caller/callee
1825/// calling conventions match, currently only fastcc supports tail calls, and
1826/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001827bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1828 SDOperand Ret,
1829 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001830 if (!PerformTailCallOpt)
1831 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001832
1833 // Check whether CALL node immediatly preceeds the RET node and whether the
1834 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001835 unsigned NumOps = Ret.getNumOperands();
1836 if ((NumOps == 1 &&
1837 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1838 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001839 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001840 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1841 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001842 MachineFunction &MF = DAG.getMachineFunction();
1843 unsigned CallerCC = MF.getFunction()->getCallingConv();
1844 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1845 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1846 SDOperand Callee = Call.getOperand(4);
1847 // On elf/pic %ebx needs to be livein.
Evan Chenge7a87392007-11-02 01:26:22 +00001848 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1849 !Subtarget->isPICStyleGOT())
1850 return true;
1851
1852 // Can only do local tail calls with PIC.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001853 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1854 return G->getGlobal()->hasHiddenVisibility()
1855 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001856 }
1857 }
Evan Chenge7a87392007-11-02 01:26:22 +00001858
1859 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001860}
1861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001862//===----------------------------------------------------------------------===//
1863// Other Lowering Hooks
1864//===----------------------------------------------------------------------===//
1865
1866
1867SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001868 MachineFunction &MF = DAG.getMachineFunction();
1869 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1870 int ReturnAddrIndex = FuncInfo->getRAIndex();
1871
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001872 if (ReturnAddrIndex == 0) {
1873 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001874 if (Subtarget->is64Bit())
1875 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1876 else
1877 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001878
1879 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001880 }
1881
1882 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1883}
1884
1885
1886
1887/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1888/// specific condition code. It returns a false if it cannot do a direct
1889/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1890/// needed.
1891static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1892 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1893 SelectionDAG &DAG) {
1894 X86CC = X86::COND_INVALID;
1895 if (!isFP) {
1896 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1897 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1898 // X > -1 -> X == 0, jump !sign.
1899 RHS = DAG.getConstant(0, RHS.getValueType());
1900 X86CC = X86::COND_NS;
1901 return true;
1902 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1903 // X < 0 -> X == 0, jump on sign.
1904 X86CC = X86::COND_S;
1905 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001906 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1907 // X < 1 -> X <= 0
1908 RHS = DAG.getConstant(0, RHS.getValueType());
1909 X86CC = X86::COND_LE;
1910 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001911 }
1912 }
1913
1914 switch (SetCCOpcode) {
1915 default: break;
1916 case ISD::SETEQ: X86CC = X86::COND_E; break;
1917 case ISD::SETGT: X86CC = X86::COND_G; break;
1918 case ISD::SETGE: X86CC = X86::COND_GE; break;
1919 case ISD::SETLT: X86CC = X86::COND_L; break;
1920 case ISD::SETLE: X86CC = X86::COND_LE; break;
1921 case ISD::SETNE: X86CC = X86::COND_NE; break;
1922 case ISD::SETULT: X86CC = X86::COND_B; break;
1923 case ISD::SETUGT: X86CC = X86::COND_A; break;
1924 case ISD::SETULE: X86CC = X86::COND_BE; break;
1925 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1926 }
1927 } else {
1928 // On a floating point condition, the flags are set as follows:
1929 // ZF PF CF op
1930 // 0 | 0 | 0 | X > Y
1931 // 0 | 0 | 1 | X < Y
1932 // 1 | 0 | 0 | X == Y
1933 // 1 | 1 | 1 | unordered
1934 bool Flip = false;
1935 switch (SetCCOpcode) {
1936 default: break;
1937 case ISD::SETUEQ:
1938 case ISD::SETEQ: X86CC = X86::COND_E; break;
1939 case ISD::SETOLT: Flip = true; // Fallthrough
1940 case ISD::SETOGT:
1941 case ISD::SETGT: X86CC = X86::COND_A; break;
1942 case ISD::SETOLE: Flip = true; // Fallthrough
1943 case ISD::SETOGE:
1944 case ISD::SETGE: X86CC = X86::COND_AE; break;
1945 case ISD::SETUGT: Flip = true; // Fallthrough
1946 case ISD::SETULT:
1947 case ISD::SETLT: X86CC = X86::COND_B; break;
1948 case ISD::SETUGE: Flip = true; // Fallthrough
1949 case ISD::SETULE:
1950 case ISD::SETLE: X86CC = X86::COND_BE; break;
1951 case ISD::SETONE:
1952 case ISD::SETNE: X86CC = X86::COND_NE; break;
1953 case ISD::SETUO: X86CC = X86::COND_P; break;
1954 case ISD::SETO: X86CC = X86::COND_NP; break;
1955 }
1956 if (Flip)
1957 std::swap(LHS, RHS);
1958 }
1959
1960 return X86CC != X86::COND_INVALID;
1961}
1962
1963/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1964/// code. Current x86 isa includes the following FP cmov instructions:
1965/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1966static bool hasFPCMov(unsigned X86CC) {
1967 switch (X86CC) {
1968 default:
1969 return false;
1970 case X86::COND_B:
1971 case X86::COND_BE:
1972 case X86::COND_E:
1973 case X86::COND_P:
1974 case X86::COND_A:
1975 case X86::COND_AE:
1976 case X86::COND_NE:
1977 case X86::COND_NP:
1978 return true;
1979 }
1980}
1981
1982/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1983/// true if Op is undef or if its value falls within the specified range (L, H].
1984static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1985 if (Op.getOpcode() == ISD::UNDEF)
1986 return true;
1987
1988 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1989 return (Val >= Low && Val < Hi);
1990}
1991
1992/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1993/// true if Op is undef or if its value equal to the specified value.
1994static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1995 if (Op.getOpcode() == ISD::UNDEF)
1996 return true;
1997 return cast<ConstantSDNode>(Op)->getValue() == Val;
1998}
1999
2000/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2001/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2002bool X86::isPSHUFDMask(SDNode *N) {
2003 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2004
Dan Gohman7dc19012007-08-02 21:17:01 +00002005 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002006 return false;
2007
2008 // Check if the value doesn't reference the second vector.
2009 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2010 SDOperand Arg = N->getOperand(i);
2011 if (Arg.getOpcode() == ISD::UNDEF) continue;
2012 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002013 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002014 return false;
2015 }
2016
2017 return true;
2018}
2019
2020/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2021/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2022bool X86::isPSHUFHWMask(SDNode *N) {
2023 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2024
2025 if (N->getNumOperands() != 8)
2026 return false;
2027
2028 // Lower quadword copied in order.
2029 for (unsigned i = 0; i != 4; ++i) {
2030 SDOperand Arg = N->getOperand(i);
2031 if (Arg.getOpcode() == ISD::UNDEF) continue;
2032 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2033 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2034 return false;
2035 }
2036
2037 // Upper quadword shuffled.
2038 for (unsigned i = 4; i != 8; ++i) {
2039 SDOperand Arg = N->getOperand(i);
2040 if (Arg.getOpcode() == ISD::UNDEF) continue;
2041 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2042 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2043 if (Val < 4 || Val > 7)
2044 return false;
2045 }
2046
2047 return true;
2048}
2049
2050/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2051/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2052bool X86::isPSHUFLWMask(SDNode *N) {
2053 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2054
2055 if (N->getNumOperands() != 8)
2056 return false;
2057
2058 // Upper quadword copied in order.
2059 for (unsigned i = 4; i != 8; ++i)
2060 if (!isUndefOrEqual(N->getOperand(i), i))
2061 return false;
2062
2063 // Lower quadword shuffled.
2064 for (unsigned i = 0; i != 4; ++i)
2065 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2066 return false;
2067
2068 return true;
2069}
2070
2071/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2072/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2073static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2074 if (NumElems != 2 && NumElems != 4) return false;
2075
2076 unsigned Half = NumElems / 2;
2077 for (unsigned i = 0; i < Half; ++i)
2078 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2079 return false;
2080 for (unsigned i = Half; i < NumElems; ++i)
2081 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2082 return false;
2083
2084 return true;
2085}
2086
2087bool X86::isSHUFPMask(SDNode *N) {
2088 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2089 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2090}
2091
2092/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2093/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2094/// half elements to come from vector 1 (which would equal the dest.) and
2095/// the upper half to come from vector 2.
2096static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2097 if (NumOps != 2 && NumOps != 4) return false;
2098
2099 unsigned Half = NumOps / 2;
2100 for (unsigned i = 0; i < Half; ++i)
2101 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2102 return false;
2103 for (unsigned i = Half; i < NumOps; ++i)
2104 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2105 return false;
2106 return true;
2107}
2108
2109static bool isCommutedSHUFP(SDNode *N) {
2110 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2111 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2112}
2113
2114/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2115/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2116bool X86::isMOVHLPSMask(SDNode *N) {
2117 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2118
2119 if (N->getNumOperands() != 4)
2120 return false;
2121
2122 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2123 return isUndefOrEqual(N->getOperand(0), 6) &&
2124 isUndefOrEqual(N->getOperand(1), 7) &&
2125 isUndefOrEqual(N->getOperand(2), 2) &&
2126 isUndefOrEqual(N->getOperand(3), 3);
2127}
2128
2129/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2130/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2131/// <2, 3, 2, 3>
2132bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2133 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2134
2135 if (N->getNumOperands() != 4)
2136 return false;
2137
2138 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2139 return isUndefOrEqual(N->getOperand(0), 2) &&
2140 isUndefOrEqual(N->getOperand(1), 3) &&
2141 isUndefOrEqual(N->getOperand(2), 2) &&
2142 isUndefOrEqual(N->getOperand(3), 3);
2143}
2144
2145/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2146/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2147bool X86::isMOVLPMask(SDNode *N) {
2148 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2149
2150 unsigned NumElems = N->getNumOperands();
2151 if (NumElems != 2 && NumElems != 4)
2152 return false;
2153
2154 for (unsigned i = 0; i < NumElems/2; ++i)
2155 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2156 return false;
2157
2158 for (unsigned i = NumElems/2; i < NumElems; ++i)
2159 if (!isUndefOrEqual(N->getOperand(i), i))
2160 return false;
2161
2162 return true;
2163}
2164
2165/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2166/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2167/// and MOVLHPS.
2168bool X86::isMOVHPMask(SDNode *N) {
2169 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2170
2171 unsigned NumElems = N->getNumOperands();
2172 if (NumElems != 2 && NumElems != 4)
2173 return false;
2174
2175 for (unsigned i = 0; i < NumElems/2; ++i)
2176 if (!isUndefOrEqual(N->getOperand(i), i))
2177 return false;
2178
2179 for (unsigned i = 0; i < NumElems/2; ++i) {
2180 SDOperand Arg = N->getOperand(i + NumElems/2);
2181 if (!isUndefOrEqual(Arg, i + NumElems))
2182 return false;
2183 }
2184
2185 return true;
2186}
2187
2188/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2189/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2190bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2191 bool V2IsSplat = false) {
2192 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2193 return false;
2194
2195 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2196 SDOperand BitI = Elts[i];
2197 SDOperand BitI1 = Elts[i+1];
2198 if (!isUndefOrEqual(BitI, j))
2199 return false;
2200 if (V2IsSplat) {
2201 if (isUndefOrEqual(BitI1, NumElts))
2202 return false;
2203 } else {
2204 if (!isUndefOrEqual(BitI1, j + NumElts))
2205 return false;
2206 }
2207 }
2208
2209 return true;
2210}
2211
2212bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2213 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2214 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2215}
2216
2217/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2218/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2219bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2220 bool V2IsSplat = false) {
2221 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2222 return false;
2223
2224 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2225 SDOperand BitI = Elts[i];
2226 SDOperand BitI1 = Elts[i+1];
2227 if (!isUndefOrEqual(BitI, j + NumElts/2))
2228 return false;
2229 if (V2IsSplat) {
2230 if (isUndefOrEqual(BitI1, NumElts))
2231 return false;
2232 } else {
2233 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2234 return false;
2235 }
2236 }
2237
2238 return true;
2239}
2240
2241bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2242 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2243 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2244}
2245
2246/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2247/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2248/// <0, 0, 1, 1>
2249bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2250 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2251
2252 unsigned NumElems = N->getNumOperands();
2253 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2254 return false;
2255
2256 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2257 SDOperand BitI = N->getOperand(i);
2258 SDOperand BitI1 = N->getOperand(i+1);
2259
2260 if (!isUndefOrEqual(BitI, j))
2261 return false;
2262 if (!isUndefOrEqual(BitI1, j))
2263 return false;
2264 }
2265
2266 return true;
2267}
2268
2269/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2270/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2271/// <2, 2, 3, 3>
2272bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2273 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2274
2275 unsigned NumElems = N->getNumOperands();
2276 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2277 return false;
2278
2279 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2280 SDOperand BitI = N->getOperand(i);
2281 SDOperand BitI1 = N->getOperand(i + 1);
2282
2283 if (!isUndefOrEqual(BitI, j))
2284 return false;
2285 if (!isUndefOrEqual(BitI1, j))
2286 return false;
2287 }
2288
2289 return true;
2290}
2291
2292/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2293/// specifies a shuffle of elements that is suitable for input to MOVSS,
2294/// MOVSD, and MOVD, i.e. setting the lowest element.
2295static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002296 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 return false;
2298
2299 if (!isUndefOrEqual(Elts[0], NumElts))
2300 return false;
2301
2302 for (unsigned i = 1; i < NumElts; ++i) {
2303 if (!isUndefOrEqual(Elts[i], i))
2304 return false;
2305 }
2306
2307 return true;
2308}
2309
2310bool X86::isMOVLMask(SDNode *N) {
2311 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2312 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2313}
2314
2315/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2316/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2317/// element of vector 2 and the other elements to come from vector 1 in order.
2318static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2319 bool V2IsSplat = false,
2320 bool V2IsUndef = false) {
2321 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2322 return false;
2323
2324 if (!isUndefOrEqual(Ops[0], 0))
2325 return false;
2326
2327 for (unsigned i = 1; i < NumOps; ++i) {
2328 SDOperand Arg = Ops[i];
2329 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2330 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2331 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2332 return false;
2333 }
2334
2335 return true;
2336}
2337
2338static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2339 bool V2IsUndef = false) {
2340 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2341 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2342 V2IsSplat, V2IsUndef);
2343}
2344
2345/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2346/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2347bool X86::isMOVSHDUPMask(SDNode *N) {
2348 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2349
2350 if (N->getNumOperands() != 4)
2351 return false;
2352
2353 // Expect 1, 1, 3, 3
2354 for (unsigned i = 0; i < 2; ++i) {
2355 SDOperand Arg = N->getOperand(i);
2356 if (Arg.getOpcode() == ISD::UNDEF) continue;
2357 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2358 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2359 if (Val != 1) return false;
2360 }
2361
2362 bool HasHi = false;
2363 for (unsigned i = 2; i < 4; ++i) {
2364 SDOperand Arg = N->getOperand(i);
2365 if (Arg.getOpcode() == ISD::UNDEF) continue;
2366 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2367 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2368 if (Val != 3) return false;
2369 HasHi = true;
2370 }
2371
2372 // Don't use movshdup if it can be done with a shufps.
2373 return HasHi;
2374}
2375
2376/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2377/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2378bool X86::isMOVSLDUPMask(SDNode *N) {
2379 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2380
2381 if (N->getNumOperands() != 4)
2382 return false;
2383
2384 // Expect 0, 0, 2, 2
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 != 0) 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 != 2) 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/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2408/// specifies a identity operation on the LHS or RHS.
2409static bool isIdentityMask(SDNode *N, bool RHS = false) {
2410 unsigned NumElems = N->getNumOperands();
2411 for (unsigned i = 0; i < NumElems; ++i)
2412 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2413 return false;
2414 return true;
2415}
2416
2417/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2418/// a splat of a single element.
2419static bool isSplatMask(SDNode *N) {
2420 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2421
2422 // This is a splat operation if each element of the permute is the same, and
2423 // if the value doesn't reference the second vector.
2424 unsigned NumElems = N->getNumOperands();
2425 SDOperand ElementBase;
2426 unsigned i = 0;
2427 for (; i != NumElems; ++i) {
2428 SDOperand Elt = N->getOperand(i);
2429 if (isa<ConstantSDNode>(Elt)) {
2430 ElementBase = Elt;
2431 break;
2432 }
2433 }
2434
2435 if (!ElementBase.Val)
2436 return false;
2437
2438 for (; i != NumElems; ++i) {
2439 SDOperand Arg = N->getOperand(i);
2440 if (Arg.getOpcode() == ISD::UNDEF) continue;
2441 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2442 if (Arg != ElementBase) return false;
2443 }
2444
2445 // Make sure it is a splat of the first vector operand.
2446 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2447}
2448
2449/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2450/// a splat of a single element and it's a 2 or 4 element mask.
2451bool X86::isSplatMask(SDNode *N) {
2452 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2453
2454 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2455 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2456 return false;
2457 return ::isSplatMask(N);
2458}
2459
2460/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2461/// specifies a splat of zero element.
2462bool X86::isSplatLoMask(SDNode *N) {
2463 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2464
2465 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2466 if (!isUndefOrEqual(N->getOperand(i), 0))
2467 return false;
2468 return true;
2469}
2470
2471/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2472/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2473/// instructions.
2474unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2475 unsigned NumOperands = N->getNumOperands();
2476 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2477 unsigned Mask = 0;
2478 for (unsigned i = 0; i < NumOperands; ++i) {
2479 unsigned Val = 0;
2480 SDOperand Arg = N->getOperand(NumOperands-i-1);
2481 if (Arg.getOpcode() != ISD::UNDEF)
2482 Val = cast<ConstantSDNode>(Arg)->getValue();
2483 if (Val >= NumOperands) Val -= NumOperands;
2484 Mask |= Val;
2485 if (i != NumOperands - 1)
2486 Mask <<= Shift;
2487 }
2488
2489 return Mask;
2490}
2491
2492/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2493/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2494/// instructions.
2495unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2496 unsigned Mask = 0;
2497 // 8 nodes, but we only care about the last 4.
2498 for (unsigned i = 7; i >= 4; --i) {
2499 unsigned Val = 0;
2500 SDOperand Arg = N->getOperand(i);
2501 if (Arg.getOpcode() != ISD::UNDEF)
2502 Val = cast<ConstantSDNode>(Arg)->getValue();
2503 Mask |= (Val - 4);
2504 if (i != 4)
2505 Mask <<= 2;
2506 }
2507
2508 return Mask;
2509}
2510
2511/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2512/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2513/// instructions.
2514unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2515 unsigned Mask = 0;
2516 // 8 nodes, but we only care about the first 4.
2517 for (int i = 3; i >= 0; --i) {
2518 unsigned Val = 0;
2519 SDOperand Arg = N->getOperand(i);
2520 if (Arg.getOpcode() != ISD::UNDEF)
2521 Val = cast<ConstantSDNode>(Arg)->getValue();
2522 Mask |= Val;
2523 if (i != 0)
2524 Mask <<= 2;
2525 }
2526
2527 return Mask;
2528}
2529
2530/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2531/// specifies a 8 element shuffle that can be broken into a pair of
2532/// PSHUFHW and PSHUFLW.
2533static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2534 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2535
2536 if (N->getNumOperands() != 8)
2537 return false;
2538
2539 // Lower quadword shuffled.
2540 for (unsigned i = 0; i != 4; ++i) {
2541 SDOperand Arg = N->getOperand(i);
2542 if (Arg.getOpcode() == ISD::UNDEF) continue;
2543 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2544 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002545 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 return false;
2547 }
2548
2549 // Upper quadword shuffled.
2550 for (unsigned i = 4; i != 8; ++i) {
2551 SDOperand Arg = N->getOperand(i);
2552 if (Arg.getOpcode() == ISD::UNDEF) continue;
2553 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2554 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2555 if (Val < 4 || Val > 7)
2556 return false;
2557 }
2558
2559 return true;
2560}
2561
Chris Lattnere6aa3862007-11-25 00:24:49 +00002562/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563/// values in ther permute mask.
2564static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2565 SDOperand &V2, SDOperand &Mask,
2566 SelectionDAG &DAG) {
2567 MVT::ValueType VT = Op.getValueType();
2568 MVT::ValueType MaskVT = Mask.getValueType();
2569 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2570 unsigned NumElems = Mask.getNumOperands();
2571 SmallVector<SDOperand, 8> MaskVec;
2572
2573 for (unsigned i = 0; i != NumElems; ++i) {
2574 SDOperand Arg = Mask.getOperand(i);
2575 if (Arg.getOpcode() == ISD::UNDEF) {
2576 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2577 continue;
2578 }
2579 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2580 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2581 if (Val < NumElems)
2582 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2583 else
2584 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2585 }
2586
2587 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002588 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002589 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2590}
2591
Evan Chenga6769df2007-12-07 21:30:01 +00002592/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2593/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002594static
2595SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2596 MVT::ValueType MaskVT = Mask.getValueType();
2597 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2598 unsigned NumElems = Mask.getNumOperands();
2599 SmallVector<SDOperand, 8> MaskVec;
2600 for (unsigned i = 0; i != NumElems; ++i) {
2601 SDOperand Arg = Mask.getOperand(i);
2602 if (Arg.getOpcode() == ISD::UNDEF) {
2603 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2604 continue;
2605 }
2606 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2607 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2608 if (Val < NumElems)
2609 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2610 else
2611 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2612 }
2613 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2614}
2615
2616
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002617/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2618/// match movhlps. The lower half elements should come from upper half of
2619/// V1 (and in order), and the upper half elements should come from the upper
2620/// half of V2 (and in order).
2621static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2622 unsigned NumElems = Mask->getNumOperands();
2623 if (NumElems != 4)
2624 return false;
2625 for (unsigned i = 0, e = 2; i != e; ++i)
2626 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2627 return false;
2628 for (unsigned i = 2; i != 4; ++i)
2629 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2630 return false;
2631 return true;
2632}
2633
2634/// isScalarLoadToVector - Returns true if the node is a scalar load that
2635/// is promoted to a vector.
2636static inline bool isScalarLoadToVector(SDNode *N) {
2637 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2638 N = N->getOperand(0).Val;
2639 return ISD::isNON_EXTLoad(N);
2640 }
2641 return false;
2642}
2643
2644/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2645/// match movlp{s|d}. The lower half elements should come from lower half of
2646/// V1 (and in order), and the upper half elements should come from the upper
2647/// half of V2 (and in order). And since V1 will become the source of the
2648/// MOVLP, it must be either a vector load or a scalar load to vector.
2649static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2650 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2651 return false;
2652 // Is V2 is a vector load, don't do this transformation. We will try to use
2653 // load folding shufps op.
2654 if (ISD::isNON_EXTLoad(V2))
2655 return false;
2656
2657 unsigned NumElems = Mask->getNumOperands();
2658 if (NumElems != 2 && NumElems != 4)
2659 return false;
2660 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2661 if (!isUndefOrEqual(Mask->getOperand(i), i))
2662 return false;
2663 for (unsigned i = NumElems/2; i != NumElems; ++i)
2664 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2665 return false;
2666 return true;
2667}
2668
2669/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2670/// all the same.
2671static bool isSplatVector(SDNode *N) {
2672 if (N->getOpcode() != ISD::BUILD_VECTOR)
2673 return false;
2674
2675 SDOperand SplatValue = N->getOperand(0);
2676 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2677 if (N->getOperand(i) != SplatValue)
2678 return false;
2679 return true;
2680}
2681
2682/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2683/// to an undef.
2684static bool isUndefShuffle(SDNode *N) {
2685 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2686 return false;
2687
2688 SDOperand V1 = N->getOperand(0);
2689 SDOperand V2 = N->getOperand(1);
2690 SDOperand Mask = N->getOperand(2);
2691 unsigned NumElems = Mask.getNumOperands();
2692 for (unsigned i = 0; i != NumElems; ++i) {
2693 SDOperand Arg = Mask.getOperand(i);
2694 if (Arg.getOpcode() != ISD::UNDEF) {
2695 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2696 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2697 return false;
2698 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2699 return false;
2700 }
2701 }
2702 return true;
2703}
2704
2705/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2706/// constant +0.0.
2707static inline bool isZeroNode(SDOperand Elt) {
2708 return ((isa<ConstantSDNode>(Elt) &&
2709 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2710 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002711 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712}
2713
2714/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2715/// to an zero vector.
2716static bool isZeroShuffle(SDNode *N) {
2717 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2718 return false;
2719
2720 SDOperand V1 = N->getOperand(0);
2721 SDOperand V2 = N->getOperand(1);
2722 SDOperand Mask = N->getOperand(2);
2723 unsigned NumElems = Mask.getNumOperands();
2724 for (unsigned i = 0; i != NumElems; ++i) {
2725 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002726 if (Arg.getOpcode() == ISD::UNDEF)
2727 continue;
2728
2729 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2730 if (Idx < NumElems) {
2731 unsigned Opc = V1.Val->getOpcode();
2732 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2733 continue;
2734 if (Opc != ISD::BUILD_VECTOR ||
2735 !isZeroNode(V1.Val->getOperand(Idx)))
2736 return false;
2737 } else if (Idx >= NumElems) {
2738 unsigned Opc = V2.Val->getOpcode();
2739 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2740 continue;
2741 if (Opc != ISD::BUILD_VECTOR ||
2742 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2743 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 }
2745 }
2746 return true;
2747}
2748
2749/// getZeroVector - Returns a vector of specified type with all zero elements.
2750///
2751static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2752 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002753
2754 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2755 // type. This ensures they get CSE'd.
2756 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2757 SDOperand Vec;
2758 if (MVT::getSizeInBits(VT) == 64) // MMX
2759 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2760 else // SSE
2761 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2762 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763}
2764
Chris Lattnere6aa3862007-11-25 00:24:49 +00002765/// getOnesVector - Returns a vector of specified type with all bits set.
2766///
2767static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2768 assert(MVT::isVector(VT) && "Expected a vector type");
2769
2770 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2771 // type. This ensures they get CSE'd.
2772 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2773 SDOperand Vec;
2774 if (MVT::getSizeInBits(VT) == 64) // MMX
2775 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2776 else // SSE
2777 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2778 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2779}
2780
2781
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2783/// that point to V2 points to its first element.
2784static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2785 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2786
2787 bool Changed = false;
2788 SmallVector<SDOperand, 8> MaskVec;
2789 unsigned NumElems = Mask.getNumOperands();
2790 for (unsigned i = 0; i != NumElems; ++i) {
2791 SDOperand Arg = Mask.getOperand(i);
2792 if (Arg.getOpcode() != ISD::UNDEF) {
2793 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2794 if (Val > NumElems) {
2795 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2796 Changed = true;
2797 }
2798 }
2799 MaskVec.push_back(Arg);
2800 }
2801
2802 if (Changed)
2803 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2804 &MaskVec[0], MaskVec.size());
2805 return Mask;
2806}
2807
2808/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2809/// operation of specified width.
2810static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2811 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2812 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2813
2814 SmallVector<SDOperand, 8> MaskVec;
2815 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2816 for (unsigned i = 1; i != NumElems; ++i)
2817 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2818 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2819}
2820
2821/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2822/// of specified width.
2823static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2824 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2825 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2826 SmallVector<SDOperand, 8> MaskVec;
2827 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2828 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2829 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2830 }
2831 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2832}
2833
2834/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2835/// of specified width.
2836static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2837 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2838 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2839 unsigned Half = NumElems/2;
2840 SmallVector<SDOperand, 8> MaskVec;
2841 for (unsigned i = 0; i != Half; ++i) {
2842 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2843 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2844 }
2845 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2846}
2847
2848/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2849///
2850static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2851 SDOperand V1 = Op.getOperand(0);
2852 SDOperand Mask = Op.getOperand(2);
2853 MVT::ValueType VT = Op.getValueType();
2854 unsigned NumElems = Mask.getNumOperands();
2855 Mask = getUnpacklMask(NumElems, DAG);
2856 while (NumElems != 4) {
2857 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2858 NumElems >>= 1;
2859 }
2860 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2861
Chris Lattnere6aa3862007-11-25 00:24:49 +00002862 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002863 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2864 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2865 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2866}
2867
2868/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002869/// vector of zero or undef vector. This produces a shuffle where the low
2870/// element of V2 is swizzled into the zero/undef vector, landing at element
2871/// 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 +00002872static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2873 unsigned NumElems, unsigned Idx,
2874 bool isZero, SelectionDAG &DAG) {
2875 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2876 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2877 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002878 SmallVector<SDOperand, 16> MaskVec;
2879 for (unsigned i = 0; i != NumElems; ++i)
2880 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2881 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2882 else
2883 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002884 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2885 &MaskVec[0], MaskVec.size());
2886 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2887}
2888
2889/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2890///
2891static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2892 unsigned NumNonZero, unsigned NumZero,
2893 SelectionDAG &DAG, TargetLowering &TLI) {
2894 if (NumNonZero > 8)
2895 return SDOperand();
2896
2897 SDOperand V(0, 0);
2898 bool First = true;
2899 for (unsigned i = 0; i < 16; ++i) {
2900 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2901 if (ThisIsNonZero && First) {
2902 if (NumZero)
2903 V = getZeroVector(MVT::v8i16, DAG);
2904 else
2905 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2906 First = false;
2907 }
2908
2909 if ((i & 1) != 0) {
2910 SDOperand ThisElt(0, 0), LastElt(0, 0);
2911 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2912 if (LastIsNonZero) {
2913 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2914 }
2915 if (ThisIsNonZero) {
2916 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2917 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2918 ThisElt, DAG.getConstant(8, MVT::i8));
2919 if (LastIsNonZero)
2920 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2921 } else
2922 ThisElt = LastElt;
2923
2924 if (ThisElt.Val)
2925 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002926 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002927 }
2928 }
2929
2930 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2931}
2932
2933/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2934///
2935static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2936 unsigned NumNonZero, unsigned NumZero,
2937 SelectionDAG &DAG, TargetLowering &TLI) {
2938 if (NumNonZero > 4)
2939 return SDOperand();
2940
2941 SDOperand V(0, 0);
2942 bool First = true;
2943 for (unsigned i = 0; i < 8; ++i) {
2944 bool isNonZero = (NonZeros & (1 << i)) != 0;
2945 if (isNonZero) {
2946 if (First) {
2947 if (NumZero)
2948 V = getZeroVector(MVT::v8i16, DAG);
2949 else
2950 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2951 First = false;
2952 }
2953 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002954 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 }
2956 }
2957
2958 return V;
2959}
2960
2961SDOperand
2962X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002963 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2964 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2965 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2966 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2967 // eliminated on x86-32 hosts.
2968 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2969 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970
Chris Lattnere6aa3862007-11-25 00:24:49 +00002971 if (ISD::isBuildVectorAllOnes(Op.Val))
2972 return getOnesVector(Op.getValueType(), DAG);
2973 return getZeroVector(Op.getValueType(), DAG);
2974 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002975
2976 MVT::ValueType VT = Op.getValueType();
2977 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2978 unsigned EVTBits = MVT::getSizeInBits(EVT);
2979
2980 unsigned NumElems = Op.getNumOperands();
2981 unsigned NumZero = 0;
2982 unsigned NumNonZero = 0;
2983 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00002984 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00002985 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 for (unsigned i = 0; i < NumElems; ++i) {
2987 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00002988 if (Elt.getOpcode() == ISD::UNDEF)
2989 continue;
2990 Values.insert(Elt);
2991 if (Elt.getOpcode() != ISD::Constant &&
2992 Elt.getOpcode() != ISD::ConstantFP)
2993 HasNonImms = true;
2994 if (isZeroNode(Elt))
2995 NumZero++;
2996 else {
2997 NonZeros |= (1 << i);
2998 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 }
3000 }
3001
3002 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003003 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3004 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005 }
3006
3007 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3008 if (Values.size() == 1)
3009 return SDOperand();
3010
3011 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00003012 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013 unsigned Idx = CountTrailingZeros_32(NonZeros);
3014 SDOperand Item = Op.getOperand(Idx);
3015 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3016 if (Idx == 0)
3017 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3018 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3019 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00003020 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
3021 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003022
3023 if (EVTBits == 32) {
3024 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3025 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3026 DAG);
3027 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3028 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3029 SmallVector<SDOperand, 8> MaskVec;
3030 for (unsigned i = 0; i < NumElems; i++)
3031 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3032 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3033 &MaskVec[0], MaskVec.size());
3034 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3035 DAG.getNode(ISD::UNDEF, VT), Mask);
3036 }
3037 }
3038
Dan Gohman21463242007-07-24 22:55:08 +00003039 // A vector full of immediates; various special cases are already
3040 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00003041 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00003042 return SDOperand();
3043
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 // Let legalizer expand 2-wide build_vectors.
3045 if (EVTBits == 64)
3046 return SDOperand();
3047
3048 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3049 if (EVTBits == 8 && NumElems == 16) {
3050 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3051 *this);
3052 if (V.Val) return V;
3053 }
3054
3055 if (EVTBits == 16 && NumElems == 8) {
3056 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3057 *this);
3058 if (V.Val) return V;
3059 }
3060
3061 // If element VT is == 32 bits, turn it into a number of shuffles.
3062 SmallVector<SDOperand, 8> V;
3063 V.resize(NumElems);
3064 if (NumElems == 4 && NumZero > 0) {
3065 for (unsigned i = 0; i < 4; ++i) {
3066 bool isZero = !(NonZeros & (1 << i));
3067 if (isZero)
3068 V[i] = getZeroVector(VT, DAG);
3069 else
3070 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3071 }
3072
3073 for (unsigned i = 0; i < 2; ++i) {
3074 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3075 default: break;
3076 case 0:
3077 V[i] = V[i*2]; // Must be a zero vector.
3078 break;
3079 case 1:
3080 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3081 getMOVLMask(NumElems, DAG));
3082 break;
3083 case 2:
3084 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3085 getMOVLMask(NumElems, DAG));
3086 break;
3087 case 3:
3088 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3089 getUnpacklMask(NumElems, DAG));
3090 break;
3091 }
3092 }
3093
3094 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3095 // clears the upper bits.
3096 // FIXME: we can do the same for v4f32 case when we know both parts of
3097 // the lower half come from scalar_to_vector (loadf32). We should do
3098 // that in post legalizer dag combiner with target specific hooks.
3099 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3100 return V[0];
3101 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3102 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3103 SmallVector<SDOperand, 8> MaskVec;
3104 bool Reverse = (NonZeros & 0x3) == 2;
3105 for (unsigned i = 0; i < 2; ++i)
3106 if (Reverse)
3107 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3108 else
3109 MaskVec.push_back(DAG.getConstant(i, EVT));
3110 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3111 for (unsigned i = 0; i < 2; ++i)
3112 if (Reverse)
3113 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3114 else
3115 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3116 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3117 &MaskVec[0], MaskVec.size());
3118 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3119 }
3120
3121 if (Values.size() > 2) {
3122 // Expand into a number of unpckl*.
3123 // e.g. for v4f32
3124 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3125 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3126 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3127 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3128 for (unsigned i = 0; i < NumElems; ++i)
3129 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3130 NumElems >>= 1;
3131 while (NumElems != 0) {
3132 for (unsigned i = 0; i < NumElems; ++i)
3133 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3134 UnpckMask);
3135 NumElems >>= 1;
3136 }
3137 return V[0];
3138 }
3139
3140 return SDOperand();
3141}
3142
Evan Chengfca29242007-12-07 08:07:39 +00003143static
3144SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3145 SDOperand PermMask, SelectionDAG &DAG,
3146 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003147 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003148 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3149 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003150 MVT::ValueType PtrVT = TLI.getPointerTy();
3151 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3152 PermMask.Val->op_end());
3153
3154 // First record which half of which vector the low elements come from.
3155 SmallVector<unsigned, 4> LowQuad(4);
3156 for (unsigned i = 0; i < 4; ++i) {
3157 SDOperand Elt = MaskElts[i];
3158 if (Elt.getOpcode() == ISD::UNDEF)
3159 continue;
3160 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3161 int QuadIdx = EltIdx / 4;
3162 ++LowQuad[QuadIdx];
3163 }
3164 int BestLowQuad = -1;
3165 unsigned MaxQuad = 1;
3166 for (unsigned i = 0; i < 4; ++i) {
3167 if (LowQuad[i] > MaxQuad) {
3168 BestLowQuad = i;
3169 MaxQuad = LowQuad[i];
3170 }
Evan Chengfca29242007-12-07 08:07:39 +00003171 }
3172
Evan Cheng75184a92007-12-11 01:46:18 +00003173 // Record which half of which vector the high elements come from.
3174 SmallVector<unsigned, 4> HighQuad(4);
3175 for (unsigned i = 4; i < 8; ++i) {
3176 SDOperand Elt = MaskElts[i];
3177 if (Elt.getOpcode() == ISD::UNDEF)
3178 continue;
3179 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3180 int QuadIdx = EltIdx / 4;
3181 ++HighQuad[QuadIdx];
3182 }
3183 int BestHighQuad = -1;
3184 MaxQuad = 1;
3185 for (unsigned i = 0; i < 4; ++i) {
3186 if (HighQuad[i] > MaxQuad) {
3187 BestHighQuad = i;
3188 MaxQuad = HighQuad[i];
3189 }
3190 }
3191
3192 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3193 if (BestLowQuad != -1 || BestHighQuad != -1) {
3194 // First sort the 4 chunks in order using shufpd.
3195 SmallVector<SDOperand, 8> MaskVec;
3196 if (BestLowQuad != -1)
3197 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3198 else
3199 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3200 if (BestHighQuad != -1)
3201 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3202 else
3203 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3204 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3205 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3206 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3207 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3208 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3209
3210 // Now sort high and low parts separately.
3211 BitVector InOrder(8);
3212 if (BestLowQuad != -1) {
3213 // Sort lower half in order using PSHUFLW.
3214 MaskVec.clear();
3215 bool AnyOutOrder = false;
3216 for (unsigned i = 0; i != 4; ++i) {
3217 SDOperand Elt = MaskElts[i];
3218 if (Elt.getOpcode() == ISD::UNDEF) {
3219 MaskVec.push_back(Elt);
3220 InOrder.set(i);
3221 } else {
3222 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3223 if (EltIdx != i)
3224 AnyOutOrder = true;
3225 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3226 // If this element is in the right place after this shuffle, then
3227 // remember it.
3228 if ((int)(EltIdx / 4) == BestLowQuad)
3229 InOrder.set(i);
3230 }
3231 }
3232 if (AnyOutOrder) {
3233 for (unsigned i = 4; i != 8; ++i)
3234 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3235 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3236 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3237 }
3238 }
3239
3240 if (BestHighQuad != -1) {
3241 // Sort high half in order using PSHUFHW if possible.
3242 MaskVec.clear();
3243 for (unsigned i = 0; i != 4; ++i)
3244 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3245 bool AnyOutOrder = false;
3246 for (unsigned i = 4; i != 8; ++i) {
3247 SDOperand Elt = MaskElts[i];
3248 if (Elt.getOpcode() == ISD::UNDEF) {
3249 MaskVec.push_back(Elt);
3250 InOrder.set(i);
3251 } else {
3252 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3253 if (EltIdx != i)
3254 AnyOutOrder = true;
3255 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3256 // If this element is in the right place after this shuffle, then
3257 // remember it.
3258 if ((int)(EltIdx / 4) == BestHighQuad)
3259 InOrder.set(i);
3260 }
3261 }
3262 if (AnyOutOrder) {
3263 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3264 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3265 }
3266 }
3267
3268 // The other elements are put in the right place using pextrw and pinsrw.
3269 for (unsigned i = 0; i != 8; ++i) {
3270 if (InOrder[i])
3271 continue;
3272 SDOperand Elt = MaskElts[i];
3273 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3274 if (EltIdx == i)
3275 continue;
3276 SDOperand ExtOp = (EltIdx < 8)
3277 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3278 DAG.getConstant(EltIdx, PtrVT))
3279 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3280 DAG.getConstant(EltIdx - 8, PtrVT));
3281 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3282 DAG.getConstant(i, PtrVT));
3283 }
3284 return NewV;
3285 }
3286
3287 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3288 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003289 // First, let's find out how many elements are already in the right order.
3290 unsigned V1InOrder = 0;
3291 unsigned V1FromV1 = 0;
3292 unsigned V2InOrder = 0;
3293 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003294 SmallVector<SDOperand, 8> V1Elts;
3295 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003296 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003297 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003298 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003299 V1Elts.push_back(Elt);
3300 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003301 ++V1InOrder;
3302 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003303 continue;
3304 }
3305 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3306 if (EltIdx == i) {
3307 V1Elts.push_back(Elt);
3308 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3309 ++V1InOrder;
3310 } else if (EltIdx == i+8) {
3311 V1Elts.push_back(Elt);
3312 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3313 ++V2InOrder;
3314 } else if (EltIdx < 8) {
3315 V1Elts.push_back(Elt);
3316 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003317 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003318 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3319 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003320 }
3321 }
3322
3323 if (V2InOrder > V1InOrder) {
3324 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3325 std::swap(V1, V2);
3326 std::swap(V1Elts, V2Elts);
3327 std::swap(V1FromV1, V2FromV2);
3328 }
3329
Evan Cheng75184a92007-12-11 01:46:18 +00003330 if ((V1FromV1 + V1InOrder) != 8) {
3331 // Some elements are from V2.
3332 if (V1FromV1) {
3333 // If there are elements that are from V1 but out of place,
3334 // then first sort them in place
3335 SmallVector<SDOperand, 8> MaskVec;
3336 for (unsigned i = 0; i < 8; ++i) {
3337 SDOperand Elt = V1Elts[i];
3338 if (Elt.getOpcode() == ISD::UNDEF) {
3339 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3340 continue;
3341 }
3342 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3343 if (EltIdx >= 8)
3344 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3345 else
3346 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3347 }
3348 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3349 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003350 }
Evan Cheng75184a92007-12-11 01:46:18 +00003351
3352 NewV = V1;
3353 for (unsigned i = 0; i < 8; ++i) {
3354 SDOperand Elt = V1Elts[i];
3355 if (Elt.getOpcode() == ISD::UNDEF)
3356 continue;
3357 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3358 if (EltIdx < 8)
3359 continue;
3360 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3361 DAG.getConstant(EltIdx - 8, PtrVT));
3362 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3363 DAG.getConstant(i, PtrVT));
3364 }
3365 return NewV;
3366 } else {
3367 // All elements are from V1.
3368 NewV = V1;
3369 for (unsigned i = 0; i < 8; ++i) {
3370 SDOperand Elt = V1Elts[i];
3371 if (Elt.getOpcode() == ISD::UNDEF)
3372 continue;
3373 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3374 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3375 DAG.getConstant(EltIdx, PtrVT));
3376 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3377 DAG.getConstant(i, PtrVT));
3378 }
3379 return NewV;
3380 }
3381}
3382
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003383/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3384/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3385/// done when every pair / quad of shuffle mask elements point to elements in
3386/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003387/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3388static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003389SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3390 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003391 SDOperand PermMask, SelectionDAG &DAG,
3392 TargetLowering &TLI) {
3393 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003394 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3395 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3396 MVT::ValueType NewVT = MaskVT;
3397 switch (VT) {
3398 case MVT::v4f32: NewVT = MVT::v2f64; break;
3399 case MVT::v4i32: NewVT = MVT::v2i64; break;
3400 case MVT::v8i16: NewVT = MVT::v4i32; break;
3401 case MVT::v16i8: NewVT = MVT::v4i32; break;
3402 default: assert(false && "Unexpected!");
3403 }
3404
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003405 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003406 if (MVT::isInteger(VT))
3407 NewVT = MVT::v2i64;
3408 else
3409 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003410 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003411 unsigned Scale = NumElems / NewWidth;
3412 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003413 for (unsigned i = 0; i < NumElems; i += Scale) {
3414 unsigned StartIdx = ~0U;
3415 for (unsigned j = 0; j < Scale; ++j) {
3416 SDOperand Elt = PermMask.getOperand(i+j);
3417 if (Elt.getOpcode() == ISD::UNDEF)
3418 continue;
3419 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3420 if (StartIdx == ~0U)
3421 StartIdx = EltIdx - (EltIdx % Scale);
3422 if (EltIdx != StartIdx + j)
3423 return SDOperand();
3424 }
3425 if (StartIdx == ~0U)
3426 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3427 else
3428 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003429 }
3430
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003431 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3432 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3433 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3434 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3435 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003436}
3437
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003438SDOperand
3439X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3440 SDOperand V1 = Op.getOperand(0);
3441 SDOperand V2 = Op.getOperand(1);
3442 SDOperand PermMask = Op.getOperand(2);
3443 MVT::ValueType VT = Op.getValueType();
3444 unsigned NumElems = PermMask.getNumOperands();
3445 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3446 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3447 bool V1IsSplat = false;
3448 bool V2IsSplat = false;
3449
3450 if (isUndefShuffle(Op.Val))
3451 return DAG.getNode(ISD::UNDEF, VT);
3452
3453 if (isZeroShuffle(Op.Val))
3454 return getZeroVector(VT, DAG);
3455
3456 if (isIdentityMask(PermMask.Val))
3457 return V1;
3458 else if (isIdentityMask(PermMask.Val, true))
3459 return V2;
3460
3461 if (isSplatMask(PermMask.Val)) {
3462 if (NumElems <= 4) return Op;
3463 // Promote it to a v4i32 splat.
3464 return PromoteSplat(Op, DAG);
3465 }
3466
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003467 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3468 // do it!
3469 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3470 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3471 if (NewOp.Val)
3472 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3473 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3474 // FIXME: Figure out a cleaner way to do this.
3475 // Try to make use of movq to zero out the top part.
3476 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3477 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3478 if (NewOp.Val) {
3479 SDOperand NewV1 = NewOp.getOperand(0);
3480 SDOperand NewV2 = NewOp.getOperand(1);
3481 SDOperand NewMask = NewOp.getOperand(2);
3482 if (isCommutedMOVL(NewMask.Val, true, false)) {
3483 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3484 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3485 NewV1, NewV2, getMOVLMask(2, DAG));
3486 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3487 }
3488 }
3489 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3490 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3491 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3492 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3493 }
3494 }
3495
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003496 if (X86::isMOVLMask(PermMask.Val))
3497 return (V1IsUndef) ? V2 : Op;
3498
3499 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3500 X86::isMOVSLDUPMask(PermMask.Val) ||
3501 X86::isMOVHLPSMask(PermMask.Val) ||
3502 X86::isMOVHPMask(PermMask.Val) ||
3503 X86::isMOVLPMask(PermMask.Val))
3504 return Op;
3505
3506 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3507 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3508 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3509
3510 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003511 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3512 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003513 V1IsSplat = isSplatVector(V1.Val);
3514 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003515
3516 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003517 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3518 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3519 std::swap(V1IsSplat, V2IsSplat);
3520 std::swap(V1IsUndef, V2IsUndef);
3521 Commuted = true;
3522 }
3523
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003524 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3526 if (V2IsUndef) return V1;
3527 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3528 if (V2IsSplat) {
3529 // V2 is a splat, so the mask may be malformed. That is, it may point
3530 // to any V2 element. The instruction selectior won't like this. Get
3531 // a corrected mask and commute to form a proper MOVS{S|D}.
3532 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3533 if (NewMask.Val != PermMask.Val)
3534 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3535 }
3536 return Op;
3537 }
3538
3539 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3540 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3541 X86::isUNPCKLMask(PermMask.Val) ||
3542 X86::isUNPCKHMask(PermMask.Val))
3543 return Op;
3544
3545 if (V2IsSplat) {
3546 // Normalize mask so all entries that point to V2 points to its first
3547 // element then try to match unpck{h|l} again. If match, return a
3548 // new vector_shuffle with the corrected mask.
3549 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3550 if (NewMask.Val != PermMask.Val) {
3551 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3552 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3553 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3554 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3555 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3556 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3557 }
3558 }
3559 }
3560
3561 // Normalize the node to match x86 shuffle ops if needed
3562 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3563 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3564
3565 if (Commuted) {
3566 // Commute is back and try unpck* again.
3567 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3568 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3569 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3570 X86::isUNPCKLMask(PermMask.Val) ||
3571 X86::isUNPCKHMask(PermMask.Val))
3572 return Op;
3573 }
3574
3575 // If VT is integer, try PSHUF* first, then SHUFP*.
3576 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003577 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3578 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3579 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3580 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 X86::isPSHUFHWMask(PermMask.Val) ||
3582 X86::isPSHUFLWMask(PermMask.Val)) {
3583 if (V2.getOpcode() != ISD::UNDEF)
3584 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3585 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3586 return Op;
3587 }
3588
3589 if (X86::isSHUFPMask(PermMask.Val) &&
3590 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3591 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003592 } else {
3593 // Floating point cases in the other order.
3594 if (X86::isSHUFPMask(PermMask.Val))
3595 return Op;
3596 if (X86::isPSHUFDMask(PermMask.Val) ||
3597 X86::isPSHUFHWMask(PermMask.Val) ||
3598 X86::isPSHUFLWMask(PermMask.Val)) {
3599 if (V2.getOpcode() != ISD::UNDEF)
3600 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3601 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3602 return Op;
3603 }
3604 }
3605
Evan Cheng75184a92007-12-11 01:46:18 +00003606 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3607 if (VT == MVT::v8i16) {
3608 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3609 if (NewOp.Val)
3610 return NewOp;
3611 }
3612
3613 // Handle all 4 wide cases with a number of shuffles.
3614 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003615 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003616 MVT::ValueType MaskVT = PermMask.getValueType();
3617 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3618 SmallVector<std::pair<int, int>, 8> Locs;
3619 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003620 SmallVector<SDOperand, 8> Mask1(NumElems,
3621 DAG.getNode(ISD::UNDEF, MaskEVT));
3622 SmallVector<SDOperand, 8> Mask2(NumElems,
3623 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003624 unsigned NumHi = 0;
3625 unsigned NumLo = 0;
3626 // If no more than two elements come from either vector. This can be
3627 // implemented with two shuffles. First shuffle gather the elements.
3628 // The second shuffle, which takes the first shuffle as both of its
3629 // vector operands, put the elements into the right order.
3630 for (unsigned i = 0; i != NumElems; ++i) {
3631 SDOperand Elt = PermMask.getOperand(i);
3632 if (Elt.getOpcode() == ISD::UNDEF) {
3633 Locs[i] = std::make_pair(-1, -1);
3634 } else {
3635 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3636 if (Val < NumElems) {
3637 Locs[i] = std::make_pair(0, NumLo);
3638 Mask1[NumLo] = Elt;
3639 NumLo++;
3640 } else {
3641 Locs[i] = std::make_pair(1, NumHi);
3642 if (2+NumHi < NumElems)
3643 Mask1[2+NumHi] = Elt;
3644 NumHi++;
3645 }
3646 }
3647 }
3648 if (NumLo <= 2 && NumHi <= 2) {
3649 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3650 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3651 &Mask1[0], Mask1.size()));
3652 for (unsigned i = 0; i != NumElems; ++i) {
3653 if (Locs[i].first == -1)
3654 continue;
3655 else {
3656 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3657 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3658 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3659 }
3660 }
3661
3662 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3663 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3664 &Mask2[0], Mask2.size()));
3665 }
3666
3667 // Break it into (shuffle shuffle_hi, shuffle_lo).
3668 Locs.clear();
3669 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3670 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3671 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3672 unsigned MaskIdx = 0;
3673 unsigned LoIdx = 0;
3674 unsigned HiIdx = NumElems/2;
3675 for (unsigned i = 0; i != NumElems; ++i) {
3676 if (i == NumElems/2) {
3677 MaskPtr = &HiMask;
3678 MaskIdx = 1;
3679 LoIdx = 0;
3680 HiIdx = NumElems/2;
3681 }
3682 SDOperand Elt = PermMask.getOperand(i);
3683 if (Elt.getOpcode() == ISD::UNDEF) {
3684 Locs[i] = std::make_pair(-1, -1);
3685 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3686 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3687 (*MaskPtr)[LoIdx] = Elt;
3688 LoIdx++;
3689 } else {
3690 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3691 (*MaskPtr)[HiIdx] = Elt;
3692 HiIdx++;
3693 }
3694 }
3695
3696 SDOperand LoShuffle =
3697 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3698 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3699 &LoMask[0], LoMask.size()));
3700 SDOperand HiShuffle =
3701 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3702 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3703 &HiMask[0], HiMask.size()));
3704 SmallVector<SDOperand, 8> MaskOps;
3705 for (unsigned i = 0; i != NumElems; ++i) {
3706 if (Locs[i].first == -1) {
3707 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3708 } else {
3709 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3710 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3711 }
3712 }
3713 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3714 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3715 &MaskOps[0], MaskOps.size()));
3716 }
3717
3718 return SDOperand();
3719}
3720
3721SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003722X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3723 SelectionDAG &DAG) {
3724 MVT::ValueType VT = Op.getValueType();
3725 if (MVT::getSizeInBits(VT) == 8) {
3726 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3727 Op.getOperand(0), Op.getOperand(1));
3728 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3729 DAG.getValueType(VT));
3730 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3731 } else if (MVT::getSizeInBits(VT) == 16) {
3732 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3733 Op.getOperand(0), Op.getOperand(1));
3734 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3735 DAG.getValueType(VT));
3736 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3737 }
3738 return SDOperand();
3739}
3740
3741
3742SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3744 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3745 return SDOperand();
3746
Nate Begemand77e59e2008-02-11 04:19:36 +00003747 if (Subtarget->hasSSE41())
3748 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3749
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 MVT::ValueType VT = Op.getValueType();
3751 // TODO: handle v16i8.
3752 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003753 SDOperand Vec = Op.getOperand(0);
3754 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3755 if (Idx == 0)
3756 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3757 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3758 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3759 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003760 // Transform it so it match pextrw which produces a 32-bit result.
3761 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3762 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3763 Op.getOperand(0), Op.getOperand(1));
3764 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3765 DAG.getValueType(VT));
3766 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3767 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003768 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3769 if (Idx == 0)
3770 return Op;
3771 // SHUFPS the element to the lowest double word, then movss.
3772 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3773 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003774 IdxVec.
3775 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3776 IdxVec.
3777 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3778 IdxVec.
3779 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3780 IdxVec.
3781 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3783 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003784 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3786 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3787 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003788 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003790 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3791 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3792 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003793 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3794 if (Idx == 0)
3795 return Op;
3796
3797 // UNPCKHPD the element to the lowest double word, then movsd.
3798 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3799 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3800 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3801 SmallVector<SDOperand, 8> IdxVec;
3802 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003803 IdxVec.
3804 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003805 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3806 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003807 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003808 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3809 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3810 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003811 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003812 }
3813
3814 return SDOperand();
3815}
3816
3817SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003818X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3819 MVT::ValueType VT = Op.getValueType();
3820 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3821
3822 SDOperand N0 = Op.getOperand(0);
3823 SDOperand N1 = Op.getOperand(1);
3824 SDOperand N2 = Op.getOperand(2);
3825
3826 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3827 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3828 : X86ISD::PINSRW;
3829 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3830 // argument.
3831 if (N1.getValueType() != MVT::i32)
3832 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3833 if (N2.getValueType() != MVT::i32)
3834 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3835 return DAG.getNode(Opc, VT, N0, N1, N2);
3836 } else if (EVT == MVT::f32) {
3837 // Bits [7:6] of the constant are the source select. This will always be
3838 // zero here. The DAG Combiner may combine an extract_elt index into these
3839 // bits. For example (insert (extract, 3), 2) could be matched by putting
3840 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3841 // Bits [5:4] of the constant are the destination select. This is the
3842 // value of the incoming immediate.
3843 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3844 // combine either bitwise AND or insert of float 0.0 to set these bits.
3845 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3846 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3847 }
3848 return SDOperand();
3849}
3850
3851SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003852X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003854 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003855
3856 if (Subtarget->hasSSE41())
3857 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3858
Evan Chenge12a7eb2007-12-12 07:55:34 +00003859 if (EVT == MVT::i8)
3860 return SDOperand();
3861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862 SDOperand N0 = Op.getOperand(0);
3863 SDOperand N1 = Op.getOperand(1);
3864 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003865
3866 if (MVT::getSizeInBits(EVT) == 16) {
3867 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3868 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003869 if (N1.getValueType() != MVT::i32)
3870 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3871 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003872 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003873 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003874 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003875 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003876}
3877
3878SDOperand
3879X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3880 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003881 MVT::ValueType VT = MVT::v2i32;
3882 switch (Op.getValueType()) {
3883 default: break;
3884 case MVT::v16i8:
3885 case MVT::v8i16:
3886 VT = MVT::v4i32;
3887 break;
3888 }
3889 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3890 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891}
3892
3893// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3894// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3895// one of the above mentioned nodes. It has to be wrapped because otherwise
3896// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3897// be used to form addressing mode. These wrapped nodes will be selected
3898// into MOV32ri.
3899SDOperand
3900X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3901 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3902 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3903 getPointerTy(),
3904 CP->getAlignment());
3905 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3906 // With PIC, the address is actually $g + Offset.
3907 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3908 !Subtarget->isPICStyleRIPRel()) {
3909 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3910 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3911 Result);
3912 }
3913
3914 return Result;
3915}
3916
3917SDOperand
3918X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3919 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3920 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003921 // If it's a debug information descriptor, don't mess with it.
3922 if (DAG.isVerifiedDebugInfoDesc(Op))
3923 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003924 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3925 // With PIC, the address is actually $g + Offset.
3926 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3927 !Subtarget->isPICStyleRIPRel()) {
3928 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3929 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3930 Result);
3931 }
3932
3933 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3934 // load the value at address GV, not the value of GV itself. This means that
3935 // the GlobalAddress must be in the base or index register of the address, not
3936 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3937 // The same applies for external symbols during PIC codegen
3938 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003939 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003940 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003941
3942 return Result;
3943}
3944
3945// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3946static SDOperand
3947LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3948 const MVT::ValueType PtrVT) {
3949 SDOperand InFlag;
3950 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3951 DAG.getNode(X86ISD::GlobalBaseReg,
3952 PtrVT), InFlag);
3953 InFlag = Chain.getValue(1);
3954
3955 // emit leal symbol@TLSGD(,%ebx,1), %eax
3956 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3957 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3958 GA->getValueType(0),
3959 GA->getOffset());
3960 SDOperand Ops[] = { Chain, TGA, InFlag };
3961 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3962 InFlag = Result.getValue(2);
3963 Chain = Result.getValue(1);
3964
3965 // call ___tls_get_addr. This function receives its argument in
3966 // the register EAX.
3967 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3968 InFlag = Chain.getValue(1);
3969
3970 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3971 SDOperand Ops1[] = { Chain,
3972 DAG.getTargetExternalSymbol("___tls_get_addr",
3973 PtrVT),
3974 DAG.getRegister(X86::EAX, PtrVT),
3975 DAG.getRegister(X86::EBX, PtrVT),
3976 InFlag };
3977 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3978 InFlag = Chain.getValue(1);
3979
3980 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3981}
3982
3983// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3984// "local exec" model.
3985static SDOperand
3986LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3987 const MVT::ValueType PtrVT) {
3988 // Get the Thread Pointer
3989 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3990 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3991 // exec)
3992 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3993 GA->getValueType(0),
3994 GA->getOffset());
3995 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3996
3997 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00003998 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003999 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000
4001 // The address of the thread local variable is the add of the thread
4002 // pointer with the offset of the variable.
4003 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4004}
4005
4006SDOperand
4007X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4008 // TODO: implement the "local dynamic" model
4009 // TODO: implement the "initial exec"model for pic executables
4010 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4011 "TLS not implemented for non-ELF and 64-bit targets");
4012 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4013 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4014 // otherwise use the "Local Exec"TLS Model
4015 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4016 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4017 else
4018 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4019}
4020
4021SDOperand
4022X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4023 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4024 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4025 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4026 // With PIC, the address is actually $g + Offset.
4027 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4028 !Subtarget->isPICStyleRIPRel()) {
4029 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4030 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4031 Result);
4032 }
4033
4034 return Result;
4035}
4036
4037SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4038 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4039 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4040 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4041 // With PIC, the address is actually $g + Offset.
4042 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4043 !Subtarget->isPICStyleRIPRel()) {
4044 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4045 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4046 Result);
4047 }
4048
4049 return Result;
4050}
4051
Chris Lattner62814a32007-10-17 06:02:13 +00004052/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4053/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004054SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00004055 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4056 "Not an i64 shift!");
4057 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4058 SDOperand ShOpLo = Op.getOperand(0);
4059 SDOperand ShOpHi = Op.getOperand(1);
4060 SDOperand ShAmt = Op.getOperand(2);
4061 SDOperand Tmp1 = isSRA ?
4062 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4063 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064
Chris Lattner62814a32007-10-17 06:02:13 +00004065 SDOperand Tmp2, Tmp3;
4066 if (Op.getOpcode() == ISD::SHL_PARTS) {
4067 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4068 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4069 } else {
4070 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4071 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4072 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004073
Chris Lattner62814a32007-10-17 06:02:13 +00004074 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4075 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4076 DAG.getConstant(32, MVT::i8));
4077 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4078 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079
Chris Lattner62814a32007-10-17 06:02:13 +00004080 SDOperand Hi, Lo;
4081 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4082 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4083 SmallVector<SDOperand, 4> Ops;
4084 if (Op.getOpcode() == ISD::SHL_PARTS) {
4085 Ops.push_back(Tmp2);
4086 Ops.push_back(Tmp3);
4087 Ops.push_back(CC);
4088 Ops.push_back(Cond);
4089 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004092 Ops.push_back(Tmp3);
4093 Ops.push_back(Tmp1);
4094 Ops.push_back(CC);
4095 Ops.push_back(Cond);
4096 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4097 } else {
4098 Ops.push_back(Tmp2);
4099 Ops.push_back(Tmp3);
4100 Ops.push_back(CC);
4101 Ops.push_back(Cond);
4102 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4103
4104 Ops.clear();
4105 Ops.push_back(Tmp3);
4106 Ops.push_back(Tmp1);
4107 Ops.push_back(CC);
4108 Ops.push_back(Cond);
4109 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4110 }
4111
4112 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4113 Ops.clear();
4114 Ops.push_back(Lo);
4115 Ops.push_back(Hi);
4116 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004117}
4118
4119SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4120 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4121 Op.getOperand(0).getValueType() >= MVT::i16 &&
4122 "Unknown SINT_TO_FP to lower!");
4123
4124 SDOperand Result;
4125 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4126 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4127 MachineFunction &MF = DAG.getMachineFunction();
4128 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4129 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4130 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004131 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004132 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004133 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134
Dale Johannesen2fc20782007-09-14 22:26:36 +00004135 // These are really Legal; caller falls through into that case.
Chris Lattnercf515b52008-01-16 06:24:21 +00004136 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004137 return Result;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004138 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen958b08b2007-09-19 23:55:34 +00004139 Subtarget->is64Bit())
4140 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00004141
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004142 // Build the FILD
4143 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004144 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004145 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4147 else
4148 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4149 SmallVector<SDOperand, 8> Ops;
4150 Ops.push_back(Chain);
4151 Ops.push_back(StackSlot);
4152 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00004153 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 Tys, &Ops[0], Ops.size());
4155
Dale Johannesen2fc20782007-09-14 22:26:36 +00004156 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004157 Chain = Result.getValue(1);
4158 SDOperand InFlag = Result.getValue(2);
4159
4160 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4161 // shouldn't be necessary except that RFP cannot be live across
4162 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4163 MachineFunction &MF = DAG.getMachineFunction();
4164 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4165 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4166 Tys = DAG.getVTList(MVT::Other);
4167 SmallVector<SDOperand, 8> Ops;
4168 Ops.push_back(Chain);
4169 Ops.push_back(Result);
4170 Ops.push_back(StackSlot);
4171 Ops.push_back(DAG.getValueType(Op.getValueType()));
4172 Ops.push_back(InFlag);
4173 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004174 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004175 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176 }
4177
4178 return Result;
4179}
4180
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004181std::pair<SDOperand,SDOperand> X86TargetLowering::
4182FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004183 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4184 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185
Dale Johannesen2fc20782007-09-14 22:26:36 +00004186 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004187 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004188 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004189 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004190 if (Subtarget->is64Bit() &&
4191 Op.getValueType() == MVT::i64 &&
4192 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004193 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004194
Evan Cheng05441e62007-10-15 20:11:21 +00004195 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4196 // stack slot.
4197 MachineFunction &MF = DAG.getMachineFunction();
4198 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4199 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4200 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201 unsigned Opc;
4202 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004203 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4204 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4205 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4206 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004207 }
4208
4209 SDOperand Chain = DAG.getEntryNode();
4210 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004211 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004212 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004213 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004214 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4216 SDOperand Ops[] = {
4217 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4218 };
4219 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4220 Chain = Value.getValue(1);
4221 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4222 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4223 }
4224
4225 // Build the FP_TO_INT*_IN_MEM
4226 SDOperand Ops[] = { Chain, Value, StackSlot };
4227 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4228
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004229 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230}
4231
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004232SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004233 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4234 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4235 if (FIST.Val == 0) return SDOperand();
4236
4237 // Load the result.
4238 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4239}
4240
4241SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4242 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4243 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4244 if (FIST.Val == 0) return 0;
4245
4246 // Return an i64 load from the stack slot.
4247 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4248
4249 // Use a MERGE_VALUES node to drop the chain result value.
4250 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4251}
4252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4254 MVT::ValueType VT = Op.getValueType();
4255 MVT::ValueType EltVT = VT;
4256 if (MVT::isVector(VT))
4257 EltVT = MVT::getVectorElementType(VT);
4258 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4259 std::vector<Constant*> CV;
4260 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004261 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 CV.push_back(C);
4263 CV.push_back(C);
4264 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004265 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266 CV.push_back(C);
4267 CV.push_back(C);
4268 CV.push_back(C);
4269 CV.push_back(C);
4270 }
Dan Gohman11821702007-07-27 17:16:43 +00004271 Constant *C = ConstantVector::get(CV);
4272 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004273 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004274 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004275 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004276 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4277}
4278
4279SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4280 MVT::ValueType VT = Op.getValueType();
4281 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004282 unsigned EltNum = 1;
4283 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004285 EltNum = MVT::getVectorNumElements(VT);
4286 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287 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);
Evan Cheng92b8f782007-07-19 23:36:01 +00004305 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004306 return DAG.getNode(ISD::BIT_CONVERT, VT,
4307 DAG.getNode(ISD::XOR, MVT::v2i64,
4308 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4309 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4310 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004311 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4312 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313}
4314
4315SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4316 SDOperand Op0 = Op.getOperand(0);
4317 SDOperand Op1 = Op.getOperand(1);
4318 MVT::ValueType VT = Op.getValueType();
4319 MVT::ValueType SrcVT = Op1.getValueType();
4320 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4321
4322 // If second operand is smaller, extend it first.
4323 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4324 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4325 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004326 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004327 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004328 // And if it is bigger, shrink it first.
4329 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004330 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004331 SrcVT = VT;
4332 SrcTy = MVT::getTypeForValueType(SrcVT);
4333 }
4334
4335 // At this point the operands and the result should have the same
4336 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337
4338 // First get the sign bit of second operand.
4339 std::vector<Constant*> CV;
4340 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004341 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4342 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004344 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4345 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4346 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4347 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004348 }
Dan Gohman11821702007-07-27 17:16:43 +00004349 Constant *C = ConstantVector::get(CV);
4350 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004351 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004352 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004353 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4355
4356 // Shift sign bit right or left if the two operands have different types.
4357 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4358 // Op0 is MVT::f32, Op1 is MVT::f64.
4359 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4360 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4361 DAG.getConstant(32, MVT::i32));
4362 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4363 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004364 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365 }
4366
4367 // Clear first operand sign bit.
4368 CV.clear();
4369 if (VT == 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 C = ConstantVector::get(CV);
4379 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004380 SDOperand Mask2 = DAG.getLoad(VT, 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 Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4384
4385 // Or the value with the sign bit.
4386 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4387}
4388
Evan Cheng621216e2007-09-29 00:00:36 +00004389SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004390 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004391 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004392 SDOperand Op0 = Op.getOperand(0);
4393 SDOperand Op1 = Op.getOperand(1);
4394 SDOperand CC = Op.getOperand(2);
4395 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4396 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4397 unsigned X86CC;
4398
Evan Cheng950aac02007-09-25 01:57:46 +00004399 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004400 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004401 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4402 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004403 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004404 }
Evan Cheng950aac02007-09-25 01:57:46 +00004405
4406 assert(isFP && "Illegal integer SetCC!");
4407
Evan Cheng621216e2007-09-29 00:00:36 +00004408 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004409 switch (SetCCOpcode) {
4410 default: assert(false && "Illegal floating point SetCC!");
4411 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004412 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004413 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004414 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004415 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4416 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4417 }
4418 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004419 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004420 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004421 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004422 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4423 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4424 }
4425 }
4426}
4427
4428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004429SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4430 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 SDOperand Cond = Op.getOperand(0);
4432 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433
4434 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004435 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436
Evan Cheng50d37ab2007-10-08 22:16:29 +00004437 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4438 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 if (Cond.getOpcode() == X86ISD::SETCC) {
4440 CC = Cond.getOperand(0);
4441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442 SDOperand Cmp = Cond.getOperand(1);
4443 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004444 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004445
Evan Cheng50d37ab2007-10-08 22:16:29 +00004446 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004447 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004448 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004449 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004450
Evan Cheng621216e2007-09-29 00:00:36 +00004451 if ((Opc == X86ISD::CMP ||
4452 Opc == X86ISD::COMI ||
4453 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004454 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004455 addTest = false;
4456 }
4457 }
4458
4459 if (addTest) {
4460 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004461 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004462 }
4463
4464 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4465 MVT::Flag);
4466 SmallVector<SDOperand, 4> Ops;
4467 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4468 // condition is true.
4469 Ops.push_back(Op.getOperand(2));
4470 Ops.push_back(Op.getOperand(1));
4471 Ops.push_back(CC);
4472 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004473 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004474}
4475
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004476SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4477 bool addTest = true;
4478 SDOperand Chain = Op.getOperand(0);
4479 SDOperand Cond = Op.getOperand(1);
4480 SDOperand Dest = Op.getOperand(2);
4481 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482
4483 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004484 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485
Evan Cheng50d37ab2007-10-08 22:16:29 +00004486 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4487 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488 if (Cond.getOpcode() == X86ISD::SETCC) {
4489 CC = Cond.getOperand(0);
4490
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491 SDOperand Cmp = Cond.getOperand(1);
4492 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004493 if (Opc == X86ISD::CMP ||
4494 Opc == X86ISD::COMI ||
4495 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004496 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004497 addTest = false;
4498 }
4499 }
4500
4501 if (addTest) {
4502 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004503 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004504 }
Evan Cheng621216e2007-09-29 00:00:36 +00004505 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004506 Chain, Op.getOperand(2), CC, Cond);
4507}
4508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004509
4510// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4511// Calls to _alloca is needed to probe the stack when allocating more than 4k
4512// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4513// that the guard pages used by the OS virtual memory manager are allocated in
4514// correct sequence.
4515SDOperand
4516X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4517 SelectionDAG &DAG) {
4518 assert(Subtarget->isTargetCygMing() &&
4519 "This should be used only on Cygwin/Mingw targets");
4520
4521 // Get the inputs.
4522 SDOperand Chain = Op.getOperand(0);
4523 SDOperand Size = Op.getOperand(1);
4524 // FIXME: Ensure alignment here
4525
4526 SDOperand Flag;
4527
4528 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004529 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530
4531 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4532 Flag = Chain.getValue(1);
4533
4534 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4535 SDOperand Ops[] = { Chain,
4536 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4537 DAG.getRegister(X86::EAX, IntPtr),
4538 Flag };
4539 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4540 Flag = Chain.getValue(1);
4541
4542 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4543
4544 std::vector<MVT::ValueType> Tys;
4545 Tys.push_back(SPTy);
4546 Tys.push_back(MVT::Other);
4547 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4548 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4549}
4550
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4552 SDOperand InFlag(0, 0);
4553 SDOperand Chain = Op.getOperand(0);
4554 unsigned Align =
4555 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4556 if (Align == 0) Align = 1;
4557
4558 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004559 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004560 // The libc version is likely to be faster for these cases. It can use the
4561 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004562 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004563 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004564 MVT::ValueType IntPtr = getPointerTy();
4565 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4566 TargetLowering::ArgListTy Args;
4567 TargetLowering::ArgListEntry Entry;
4568 Entry.Node = Op.getOperand(1);
4569 Entry.Ty = IntPtrTy;
4570 Args.push_back(Entry);
4571 // Extend the unsigned i8 argument to be an int value for the call.
4572 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4573 Entry.Ty = IntPtrTy;
4574 Args.push_back(Entry);
4575 Entry.Node = Op.getOperand(3);
4576 Args.push_back(Entry);
4577 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004578 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4579 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 return CallResult.second;
4581 }
4582
4583 MVT::ValueType AVT;
4584 SDOperand Count;
4585 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4586 unsigned BytesLeft = 0;
4587 bool TwoRepStos = false;
4588 if (ValC) {
4589 unsigned ValReg;
4590 uint64_t Val = ValC->getValue() & 255;
4591
4592 // If the value is a constant, then we can potentially use larger sets.
4593 switch (Align & 3) {
4594 case 2: // WORD aligned
4595 AVT = MVT::i16;
4596 ValReg = X86::AX;
4597 Val = (Val << 8) | Val;
4598 break;
4599 case 0: // DWORD aligned
4600 AVT = MVT::i32;
4601 ValReg = X86::EAX;
4602 Val = (Val << 8) | Val;
4603 Val = (Val << 16) | Val;
4604 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4605 AVT = MVT::i64;
4606 ValReg = X86::RAX;
4607 Val = (Val << 32) | Val;
4608 }
4609 break;
4610 default: // Byte aligned
4611 AVT = MVT::i8;
4612 ValReg = X86::AL;
4613 Count = Op.getOperand(3);
4614 break;
4615 }
4616
4617 if (AVT > MVT::i8) {
4618 if (I) {
4619 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004620 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621 BytesLeft = I->getValue() % UBytes;
4622 } else {
4623 assert(AVT >= MVT::i32 &&
4624 "Do not use rep;stos if not at least DWORD aligned");
4625 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4626 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4627 TwoRepStos = true;
4628 }
4629 }
4630
4631 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4632 InFlag);
4633 InFlag = Chain.getValue(1);
4634 } else {
4635 AVT = MVT::i8;
4636 Count = Op.getOperand(3);
4637 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4638 InFlag = Chain.getValue(1);
4639 }
4640
4641 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4642 Count, InFlag);
4643 InFlag = Chain.getValue(1);
4644 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4645 Op.getOperand(1), InFlag);
4646 InFlag = Chain.getValue(1);
4647
4648 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4649 SmallVector<SDOperand, 8> Ops;
4650 Ops.push_back(Chain);
4651 Ops.push_back(DAG.getValueType(AVT));
4652 Ops.push_back(InFlag);
4653 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4654
4655 if (TwoRepStos) {
4656 InFlag = Chain.getValue(1);
4657 Count = Op.getOperand(3);
4658 MVT::ValueType CVT = Count.getValueType();
4659 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4660 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4661 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4662 Left, InFlag);
4663 InFlag = Chain.getValue(1);
4664 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4665 Ops.clear();
4666 Ops.push_back(Chain);
4667 Ops.push_back(DAG.getValueType(MVT::i8));
4668 Ops.push_back(InFlag);
4669 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4670 } else if (BytesLeft) {
4671 // Issue stores for the last 1 - 7 bytes.
4672 SDOperand Value;
4673 unsigned Val = ValC->getValue() & 255;
4674 unsigned Offset = I->getValue() - BytesLeft;
4675 SDOperand DstAddr = Op.getOperand(1);
4676 MVT::ValueType AddrVT = DstAddr.getValueType();
4677 if (BytesLeft >= 4) {
4678 Val = (Val << 8) | Val;
4679 Val = (Val << 16) | Val;
4680 Value = DAG.getConstant(Val, MVT::i32);
4681 Chain = DAG.getStore(Chain, Value,
4682 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4683 DAG.getConstant(Offset, AddrVT)),
4684 NULL, 0);
4685 BytesLeft -= 4;
4686 Offset += 4;
4687 }
4688 if (BytesLeft >= 2) {
4689 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4690 Chain = DAG.getStore(Chain, Value,
4691 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4692 DAG.getConstant(Offset, AddrVT)),
4693 NULL, 0);
4694 BytesLeft -= 2;
4695 Offset += 2;
4696 }
4697 if (BytesLeft == 1) {
4698 Value = DAG.getConstant(Val, MVT::i8);
4699 Chain = DAG.getStore(Chain, Value,
4700 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4701 DAG.getConstant(Offset, AddrVT)),
4702 NULL, 0);
4703 }
4704 }
4705
4706 return Chain;
4707}
4708
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004709SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4710 SDOperand Dest,
4711 SDOperand Source,
4712 unsigned Size,
4713 unsigned Align,
4714 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004717 switch (Align & 3) {
4718 case 2: // WORD aligned
4719 AVT = MVT::i16;
4720 break;
4721 case 0: // DWORD aligned
4722 AVT = MVT::i32;
4723 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4724 AVT = MVT::i64;
4725 break;
4726 default: // Byte aligned
4727 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728 break;
4729 }
4730
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004731 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004732 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004733 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734
4735 SDOperand InFlag(0, 0);
4736 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4737 Count, InFlag);
4738 InFlag = Chain.getValue(1);
4739 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004740 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 InFlag = Chain.getValue(1);
4742 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004743 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004744 InFlag = Chain.getValue(1);
4745
4746 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4747 SmallVector<SDOperand, 8> Ops;
4748 Ops.push_back(Chain);
4749 Ops.push_back(DAG.getValueType(AVT));
4750 Ops.push_back(InFlag);
4751 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4752
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004753 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004754 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004755 unsigned Offset = Size - BytesLeft;
4756 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004758 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759 MVT::ValueType SrcVT = SrcAddr.getValueType();
4760 SDOperand Value;
4761 if (BytesLeft >= 4) {
4762 Value = DAG.getLoad(MVT::i32, Chain,
4763 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4764 DAG.getConstant(Offset, SrcVT)),
4765 NULL, 0);
4766 Chain = Value.getValue(1);
4767 Chain = DAG.getStore(Chain, Value,
4768 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4769 DAG.getConstant(Offset, DstVT)),
4770 NULL, 0);
4771 BytesLeft -= 4;
4772 Offset += 4;
4773 }
4774 if (BytesLeft >= 2) {
4775 Value = DAG.getLoad(MVT::i16, Chain,
4776 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4777 DAG.getConstant(Offset, SrcVT)),
4778 NULL, 0);
4779 Chain = Value.getValue(1);
4780 Chain = DAG.getStore(Chain, Value,
4781 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4782 DAG.getConstant(Offset, DstVT)),
4783 NULL, 0);
4784 BytesLeft -= 2;
4785 Offset += 2;
4786 }
4787
4788 if (BytesLeft == 1) {
4789 Value = DAG.getLoad(MVT::i8, Chain,
4790 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4791 DAG.getConstant(Offset, SrcVT)),
4792 NULL, 0);
4793 Chain = Value.getValue(1);
4794 Chain = DAG.getStore(Chain, Value,
4795 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4796 DAG.getConstant(Offset, DstVT)),
4797 NULL, 0);
4798 }
4799 }
4800
4801 return Chain;
4802}
4803
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004804/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4805SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004807 SDOperand TheChain = N->getOperand(0);
4808 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004809 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004810 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4811 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4812 MVT::i64, rax.getValue(2));
4813 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004814 DAG.getConstant(32, MVT::i8));
4815 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004816 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817 };
4818
4819 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004820 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004821 }
4822
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004823 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4824 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4825 MVT::i32, eax.getValue(2));
4826 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4827 SDOperand Ops[] = { eax, edx };
4828 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4829
4830 // Use a MERGE_VALUES to return the value and chain.
4831 Ops[1] = edx.getValue(1);
4832 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4833 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834}
4835
4836SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004837 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004838
4839 if (!Subtarget->is64Bit()) {
4840 // vastart just stores the address of the VarArgsFrameIndex slot into the
4841 // memory location argument.
4842 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004843 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004844 }
4845
4846 // __va_list_tag:
4847 // gp_offset (0 - 6 * 8)
4848 // fp_offset (48 - 48 + 8 * 16)
4849 // overflow_arg_area (point to parameters coming in memory).
4850 // reg_save_area
4851 SmallVector<SDOperand, 8> MemOps;
4852 SDOperand FIN = Op.getOperand(1);
4853 // Store gp_offset
4854 SDOperand Store = DAG.getStore(Op.getOperand(0),
4855 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004856 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004857 MemOps.push_back(Store);
4858
4859 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004860 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004861 Store = DAG.getStore(Op.getOperand(0),
4862 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004863 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864 MemOps.push_back(Store);
4865
4866 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004867 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004869 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870 MemOps.push_back(Store);
4871
4872 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004873 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004874 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004875 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004876 MemOps.push_back(Store);
4877 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4878}
4879
4880SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4881 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4882 SDOperand Chain = Op.getOperand(0);
4883 SDOperand DstPtr = Op.getOperand(1);
4884 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004885 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4886 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004887
Dan Gohman12a9c082008-02-06 22:27:42 +00004888 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889 Chain = SrcPtr.getValue(1);
4890 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004891 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004892 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004893 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894 if (i == 2)
4895 break;
4896 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004897 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004898 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004899 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004900 }
4901 return Chain;
4902}
4903
4904SDOperand
4905X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4906 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4907 switch (IntNo) {
4908 default: return SDOperand(); // Don't custom lower most intrinsics.
4909 // Comparison intrinsics.
4910 case Intrinsic::x86_sse_comieq_ss:
4911 case Intrinsic::x86_sse_comilt_ss:
4912 case Intrinsic::x86_sse_comile_ss:
4913 case Intrinsic::x86_sse_comigt_ss:
4914 case Intrinsic::x86_sse_comige_ss:
4915 case Intrinsic::x86_sse_comineq_ss:
4916 case Intrinsic::x86_sse_ucomieq_ss:
4917 case Intrinsic::x86_sse_ucomilt_ss:
4918 case Intrinsic::x86_sse_ucomile_ss:
4919 case Intrinsic::x86_sse_ucomigt_ss:
4920 case Intrinsic::x86_sse_ucomige_ss:
4921 case Intrinsic::x86_sse_ucomineq_ss:
4922 case Intrinsic::x86_sse2_comieq_sd:
4923 case Intrinsic::x86_sse2_comilt_sd:
4924 case Intrinsic::x86_sse2_comile_sd:
4925 case Intrinsic::x86_sse2_comigt_sd:
4926 case Intrinsic::x86_sse2_comige_sd:
4927 case Intrinsic::x86_sse2_comineq_sd:
4928 case Intrinsic::x86_sse2_ucomieq_sd:
4929 case Intrinsic::x86_sse2_ucomilt_sd:
4930 case Intrinsic::x86_sse2_ucomile_sd:
4931 case Intrinsic::x86_sse2_ucomigt_sd:
4932 case Intrinsic::x86_sse2_ucomige_sd:
4933 case Intrinsic::x86_sse2_ucomineq_sd: {
4934 unsigned Opc = 0;
4935 ISD::CondCode CC = ISD::SETCC_INVALID;
4936 switch (IntNo) {
4937 default: break;
4938 case Intrinsic::x86_sse_comieq_ss:
4939 case Intrinsic::x86_sse2_comieq_sd:
4940 Opc = X86ISD::COMI;
4941 CC = ISD::SETEQ;
4942 break;
4943 case Intrinsic::x86_sse_comilt_ss:
4944 case Intrinsic::x86_sse2_comilt_sd:
4945 Opc = X86ISD::COMI;
4946 CC = ISD::SETLT;
4947 break;
4948 case Intrinsic::x86_sse_comile_ss:
4949 case Intrinsic::x86_sse2_comile_sd:
4950 Opc = X86ISD::COMI;
4951 CC = ISD::SETLE;
4952 break;
4953 case Intrinsic::x86_sse_comigt_ss:
4954 case Intrinsic::x86_sse2_comigt_sd:
4955 Opc = X86ISD::COMI;
4956 CC = ISD::SETGT;
4957 break;
4958 case Intrinsic::x86_sse_comige_ss:
4959 case Intrinsic::x86_sse2_comige_sd:
4960 Opc = X86ISD::COMI;
4961 CC = ISD::SETGE;
4962 break;
4963 case Intrinsic::x86_sse_comineq_ss:
4964 case Intrinsic::x86_sse2_comineq_sd:
4965 Opc = X86ISD::COMI;
4966 CC = ISD::SETNE;
4967 break;
4968 case Intrinsic::x86_sse_ucomieq_ss:
4969 case Intrinsic::x86_sse2_ucomieq_sd:
4970 Opc = X86ISD::UCOMI;
4971 CC = ISD::SETEQ;
4972 break;
4973 case Intrinsic::x86_sse_ucomilt_ss:
4974 case Intrinsic::x86_sse2_ucomilt_sd:
4975 Opc = X86ISD::UCOMI;
4976 CC = ISD::SETLT;
4977 break;
4978 case Intrinsic::x86_sse_ucomile_ss:
4979 case Intrinsic::x86_sse2_ucomile_sd:
4980 Opc = X86ISD::UCOMI;
4981 CC = ISD::SETLE;
4982 break;
4983 case Intrinsic::x86_sse_ucomigt_ss:
4984 case Intrinsic::x86_sse2_ucomigt_sd:
4985 Opc = X86ISD::UCOMI;
4986 CC = ISD::SETGT;
4987 break;
4988 case Intrinsic::x86_sse_ucomige_ss:
4989 case Intrinsic::x86_sse2_ucomige_sd:
4990 Opc = X86ISD::UCOMI;
4991 CC = ISD::SETGE;
4992 break;
4993 case Intrinsic::x86_sse_ucomineq_ss:
4994 case Intrinsic::x86_sse2_ucomineq_sd:
4995 Opc = X86ISD::UCOMI;
4996 CC = ISD::SETNE;
4997 break;
4998 }
4999
5000 unsigned X86CC;
5001 SDOperand LHS = Op.getOperand(1);
5002 SDOperand RHS = Op.getOperand(2);
5003 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5004
Evan Cheng621216e2007-09-29 00:00:36 +00005005 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5006 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5007 DAG.getConstant(X86CC, MVT::i8), Cond);
5008 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 }
5010 }
5011}
5012
5013SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5014 // Depths > 0 not supported yet!
5015 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5016 return SDOperand();
5017
5018 // Just load the return address
5019 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5020 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5021}
5022
5023SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5024 // Depths > 0 not supported yet!
5025 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5026 return SDOperand();
5027
5028 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5029 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005030 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031}
5032
5033SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5034 SelectionDAG &DAG) {
5035 // Is not yet supported on x86-64
5036 if (Subtarget->is64Bit())
5037 return SDOperand();
5038
Chris Lattner5872a362008-01-17 07:00:52 +00005039 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005040}
5041
5042SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5043{
5044 assert(!Subtarget->is64Bit() &&
5045 "Lowering of eh_return builtin is not supported yet on x86-64");
5046
5047 MachineFunction &MF = DAG.getMachineFunction();
5048 SDOperand Chain = Op.getOperand(0);
5049 SDOperand Offset = Op.getOperand(1);
5050 SDOperand Handler = Op.getOperand(2);
5051
5052 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5053 getPointerTy());
5054
5055 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005056 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005057 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5058 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5059 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005060 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061
5062 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5063 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5064}
5065
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005066SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5067 SelectionDAG &DAG) {
5068 SDOperand Root = Op.getOperand(0);
5069 SDOperand Trmp = Op.getOperand(1); // trampoline
5070 SDOperand FPtr = Op.getOperand(2); // nested function
5071 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5072
Dan Gohman12a9c082008-02-06 22:27:42 +00005073 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005074
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005075 const X86InstrInfo *TII =
5076 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5077
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005078 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005079 SDOperand OutChains[6];
5080
5081 // Large code-model.
5082
5083 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5084 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5085
5086 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005087 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005088 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005089 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005090
5091 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5092
5093 // Load the pointer to the nested function into R11.
5094 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5095 SDOperand Addr = Trmp;
5096 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005097 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005098
5099 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005100 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005101
5102 // Load the 'nest' parameter value into R10.
5103 // R10 is specified in X86CallingConv.td
5104 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5105 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5106 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005107 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005108
5109 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005110 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005111
5112 // Jump to the nested function.
5113 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5114 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5115 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005116 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005117
5118 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5119 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5120 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005121 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005122
5123 SDOperand Ops[] =
5124 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5125 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005126 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005127 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005128 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5129 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005130 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005131
5132 switch (CC) {
5133 default:
5134 assert(0 && "Unsupported calling convention");
5135 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005136 case CallingConv::X86_StdCall: {
5137 // Pass 'nest' parameter in ECX.
5138 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005139 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005140
5141 // Check that ECX wasn't needed by an 'inreg' parameter.
5142 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005143 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005144
5145 if (Attrs && !Func->isVarArg()) {
5146 unsigned InRegCount = 0;
5147 unsigned Idx = 1;
5148
5149 for (FunctionType::param_iterator I = FTy->param_begin(),
5150 E = FTy->param_end(); I != E; ++I, ++Idx)
5151 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5152 // FIXME: should only count parameters that are lowered to integers.
5153 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5154
5155 if (InRegCount > 2) {
5156 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5157 abort();
5158 }
5159 }
5160 break;
5161 }
5162 case CallingConv::X86_FastCall:
5163 // Pass 'nest' parameter in EAX.
5164 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005165 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005166 break;
5167 }
5168
5169 SDOperand OutChains[4];
5170 SDOperand Addr, Disp;
5171
5172 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5173 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5174
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005175 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5176 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005177 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005178 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005179 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005180
5181 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005182 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005183
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005184 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005185 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5186 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005187 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005188
5189 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005190 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005191
Duncan Sands7407a9f2007-09-11 14:10:23 +00005192 SDOperand Ops[] =
5193 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5194 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005195 }
5196}
5197
Dan Gohman819574c2008-01-31 00:41:03 +00005198SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005199 /*
5200 The rounding mode is in bits 11:10 of FPSR, and has the following
5201 settings:
5202 00 Round to nearest
5203 01 Round to -inf
5204 10 Round to +inf
5205 11 Round to 0
5206
5207 FLT_ROUNDS, on the other hand, expects the following:
5208 -1 Undefined
5209 0 Round to 0
5210 1 Round to nearest
5211 2 Round to +inf
5212 3 Round to -inf
5213
5214 To perform the conversion, we do:
5215 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5216 */
5217
5218 MachineFunction &MF = DAG.getMachineFunction();
5219 const TargetMachine &TM = MF.getTarget();
5220 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5221 unsigned StackAlignment = TFI.getStackAlignment();
5222 MVT::ValueType VT = Op.getValueType();
5223
5224 // Save FP Control Word to stack slot
5225 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5226 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5227
5228 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5229 DAG.getEntryNode(), StackSlot);
5230
5231 // Load FP Control Word from stack slot
5232 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5233
5234 // Transform as necessary
5235 SDOperand CWD1 =
5236 DAG.getNode(ISD::SRL, MVT::i16,
5237 DAG.getNode(ISD::AND, MVT::i16,
5238 CWD, DAG.getConstant(0x800, MVT::i16)),
5239 DAG.getConstant(11, MVT::i8));
5240 SDOperand CWD2 =
5241 DAG.getNode(ISD::SRL, MVT::i16,
5242 DAG.getNode(ISD::AND, MVT::i16,
5243 CWD, DAG.getConstant(0x400, MVT::i16)),
5244 DAG.getConstant(9, MVT::i8));
5245
5246 SDOperand RetVal =
5247 DAG.getNode(ISD::AND, MVT::i16,
5248 DAG.getNode(ISD::ADD, MVT::i16,
5249 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5250 DAG.getConstant(1, MVT::i16)),
5251 DAG.getConstant(3, MVT::i16));
5252
5253
5254 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5255 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5256}
5257
Evan Cheng48679f42007-12-14 02:13:44 +00005258SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5259 MVT::ValueType VT = Op.getValueType();
5260 MVT::ValueType OpVT = VT;
5261 unsigned NumBits = MVT::getSizeInBits(VT);
5262
5263 Op = Op.getOperand(0);
5264 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005265 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005266 OpVT = MVT::i32;
5267 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5268 }
Evan Cheng48679f42007-12-14 02:13:44 +00005269
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005270 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5271 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5272 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5273
5274 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5275 SmallVector<SDOperand, 4> Ops;
5276 Ops.push_back(Op);
5277 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5278 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5279 Ops.push_back(Op.getValue(1));
5280 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5281
5282 // Finally xor with NumBits-1.
5283 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5284
Evan Cheng48679f42007-12-14 02:13:44 +00005285 if (VT == MVT::i8)
5286 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5287 return Op;
5288}
5289
5290SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5291 MVT::ValueType VT = Op.getValueType();
5292 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005293 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005294
5295 Op = Op.getOperand(0);
5296 if (VT == MVT::i8) {
5297 OpVT = MVT::i32;
5298 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5299 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005300
5301 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5302 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5303 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5304
5305 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5306 SmallVector<SDOperand, 4> Ops;
5307 Ops.push_back(Op);
5308 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5309 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5310 Ops.push_back(Op.getValue(1));
5311 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5312
Evan Cheng48679f42007-12-14 02:13:44 +00005313 if (VT == MVT::i8)
5314 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5315 return Op;
5316}
5317
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318/// LowerOperation - Provide custom lowering hooks for some operations.
5319///
5320SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5321 switch (Op.getOpcode()) {
5322 default: assert(0 && "Should not custom lower this!");
5323 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5324 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5325 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5326 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5327 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5328 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5329 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5330 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5331 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5332 case ISD::SHL_PARTS:
5333 case ISD::SRA_PARTS:
5334 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5335 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5336 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5337 case ISD::FABS: return LowerFABS(Op, DAG);
5338 case ISD::FNEG: return LowerFNEG(Op, DAG);
5339 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005340 case ISD::SETCC: return LowerSETCC(Op, DAG);
5341 case ISD::SELECT: return LowerSELECT(Op, DAG);
5342 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005343 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5344 case ISD::CALL: return LowerCALL(Op, DAG);
5345 case ISD::RET: return LowerRET(Op, DAG);
5346 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5347 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5348 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349 case ISD::VASTART: return LowerVASTART(Op, DAG);
5350 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5351 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5352 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5353 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5354 case ISD::FRAME_TO_ARGS_OFFSET:
5355 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5356 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5357 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005358 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005359 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005360 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5361 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005362
5363 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5364 case ISD::READCYCLECOUNTER:
5365 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005366 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005367}
5368
5369/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5370SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5371 switch (N->getOpcode()) {
5372 default: assert(0 && "Should not custom lower this!");
5373 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5374 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5375 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005376}
5377
5378const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5379 switch (Opcode) {
5380 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005381 case X86ISD::BSF: return "X86ISD::BSF";
5382 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005383 case X86ISD::SHLD: return "X86ISD::SHLD";
5384 case X86ISD::SHRD: return "X86ISD::SHRD";
5385 case X86ISD::FAND: return "X86ISD::FAND";
5386 case X86ISD::FOR: return "X86ISD::FOR";
5387 case X86ISD::FXOR: return "X86ISD::FXOR";
5388 case X86ISD::FSRL: return "X86ISD::FSRL";
5389 case X86ISD::FILD: return "X86ISD::FILD";
5390 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5391 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5392 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5393 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5394 case X86ISD::FLD: return "X86ISD::FLD";
5395 case X86ISD::FST: return "X86ISD::FST";
5396 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng931a8f42008-01-29 19:34:22 +00005397 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005398 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5399 case X86ISD::CALL: return "X86ISD::CALL";
5400 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5401 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5402 case X86ISD::CMP: return "X86ISD::CMP";
5403 case X86ISD::COMI: return "X86ISD::COMI";
5404 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5405 case X86ISD::SETCC: return "X86ISD::SETCC";
5406 case X86ISD::CMOV: return "X86ISD::CMOV";
5407 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5408 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5409 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5410 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005411 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5412 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005413 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005414 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005415 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5416 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005417 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5418 case X86ISD::FMAX: return "X86ISD::FMAX";
5419 case X86ISD::FMIN: return "X86ISD::FMIN";
5420 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5421 case X86ISD::FRCP: return "X86ISD::FRCP";
5422 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5423 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5424 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005425 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005426 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427 }
5428}
5429
5430// isLegalAddressingMode - Return true if the addressing mode represented
5431// by AM is legal for this target, for a load/store of the specified type.
5432bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5433 const Type *Ty) const {
5434 // X86 supports extremely general addressing modes.
5435
5436 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5437 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5438 return false;
5439
5440 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005441 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005442 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5443 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005444
5445 // X86-64 only supports addr of globals in small code model.
5446 if (Subtarget->is64Bit()) {
5447 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5448 return false;
5449 // If lower 4G is not available, then we must use rip-relative addressing.
5450 if (AM.BaseOffs || AM.Scale > 1)
5451 return false;
5452 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005453 }
5454
5455 switch (AM.Scale) {
5456 case 0:
5457 case 1:
5458 case 2:
5459 case 4:
5460 case 8:
5461 // These scales always work.
5462 break;
5463 case 3:
5464 case 5:
5465 case 9:
5466 // These scales are formed with basereg+scalereg. Only accept if there is
5467 // no basereg yet.
5468 if (AM.HasBaseReg)
5469 return false;
5470 break;
5471 default: // Other stuff never works.
5472 return false;
5473 }
5474
5475 return true;
5476}
5477
5478
Evan Cheng27a820a2007-10-26 01:56:11 +00005479bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5480 if (!Ty1->isInteger() || !Ty2->isInteger())
5481 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005482 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5483 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5484 if (NumBits1 <= NumBits2)
5485 return false;
5486 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005487}
5488
Evan Cheng9decb332007-10-29 19:58:20 +00005489bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5490 MVT::ValueType VT2) const {
5491 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5492 return false;
5493 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5494 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5495 if (NumBits1 <= NumBits2)
5496 return false;
5497 return Subtarget->is64Bit() || NumBits1 < 64;
5498}
Evan Cheng27a820a2007-10-26 01:56:11 +00005499
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005500/// isShuffleMaskLegal - Targets can use this to indicate that they only
5501/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5502/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5503/// are assumed to be legal.
5504bool
5505X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5506 // Only do shuffles on 128-bit vector types for now.
5507 if (MVT::getSizeInBits(VT) == 64) return false;
5508 return (Mask.Val->getNumOperands() <= 4 ||
5509 isIdentityMask(Mask.Val) ||
5510 isIdentityMask(Mask.Val, true) ||
5511 isSplatMask(Mask.Val) ||
5512 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5513 X86::isUNPCKLMask(Mask.Val) ||
5514 X86::isUNPCKHMask(Mask.Val) ||
5515 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5516 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5517}
5518
5519bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5520 MVT::ValueType EVT,
5521 SelectionDAG &DAG) const {
5522 unsigned NumElts = BVOps.size();
5523 // Only do shuffles on 128-bit vector types for now.
5524 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5525 if (NumElts == 2) return true;
5526 if (NumElts == 4) {
5527 return (isMOVLMask(&BVOps[0], 4) ||
5528 isCommutedMOVL(&BVOps[0], 4, true) ||
5529 isSHUFPMask(&BVOps[0], 4) ||
5530 isCommutedSHUFP(&BVOps[0], 4));
5531 }
5532 return false;
5533}
5534
5535//===----------------------------------------------------------------------===//
5536// X86 Scheduler Hooks
5537//===----------------------------------------------------------------------===//
5538
5539MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005540X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5541 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005542 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5543 switch (MI->getOpcode()) {
5544 default: assert(false && "Unexpected instr type to insert");
5545 case X86::CMOV_FR32:
5546 case X86::CMOV_FR64:
5547 case X86::CMOV_V4F32:
5548 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005549 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005550 // To "insert" a SELECT_CC instruction, we actually have to insert the
5551 // diamond control-flow pattern. The incoming instruction knows the
5552 // destination vreg to set, the condition code register to branch on, the
5553 // true/false values to select between, and a branch opcode to use.
5554 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5555 ilist<MachineBasicBlock>::iterator It = BB;
5556 ++It;
5557
5558 // thisMBB:
5559 // ...
5560 // TrueVal = ...
5561 // cmpTY ccX, r1, r2
5562 // bCC copy1MBB
5563 // fallthrough --> copy0MBB
5564 MachineBasicBlock *thisMBB = BB;
5565 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5566 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5567 unsigned Opc =
5568 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5569 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5570 MachineFunction *F = BB->getParent();
5571 F->getBasicBlockList().insert(It, copy0MBB);
5572 F->getBasicBlockList().insert(It, sinkMBB);
5573 // Update machine-CFG edges by first adding all successors of the current
5574 // block to the new block which will contain the Phi node for the select.
5575 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5576 e = BB->succ_end(); i != e; ++i)
5577 sinkMBB->addSuccessor(*i);
5578 // Next, remove all successors of the current block, and add the true
5579 // and fallthrough blocks as its successors.
5580 while(!BB->succ_empty())
5581 BB->removeSuccessor(BB->succ_begin());
5582 BB->addSuccessor(copy0MBB);
5583 BB->addSuccessor(sinkMBB);
5584
5585 // copy0MBB:
5586 // %FalseValue = ...
5587 // # fallthrough to sinkMBB
5588 BB = copy0MBB;
5589
5590 // Update machine-CFG edges
5591 BB->addSuccessor(sinkMBB);
5592
5593 // sinkMBB:
5594 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5595 // ...
5596 BB = sinkMBB;
5597 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5598 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5599 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5600
5601 delete MI; // The pseudo instruction is gone now.
5602 return BB;
5603 }
5604
5605 case X86::FP32_TO_INT16_IN_MEM:
5606 case X86::FP32_TO_INT32_IN_MEM:
5607 case X86::FP32_TO_INT64_IN_MEM:
5608 case X86::FP64_TO_INT16_IN_MEM:
5609 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005610 case X86::FP64_TO_INT64_IN_MEM:
5611 case X86::FP80_TO_INT16_IN_MEM:
5612 case X86::FP80_TO_INT32_IN_MEM:
5613 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614 // Change the floating point control register to use "round towards zero"
5615 // mode when truncating to an integer value.
5616 MachineFunction *F = BB->getParent();
5617 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5618 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5619
5620 // Load the old value of the high byte of the control word...
5621 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005622 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5624
5625 // Set the high part to be round to zero...
5626 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5627 .addImm(0xC7F);
5628
5629 // Reload the modified control word now...
5630 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5631
5632 // Restore the memory image of control word to original value
5633 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5634 .addReg(OldCW);
5635
5636 // Get the X86 opcode to use.
5637 unsigned Opc;
5638 switch (MI->getOpcode()) {
5639 default: assert(0 && "illegal opcode!");
5640 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5641 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5642 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5643 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5644 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5645 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005646 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5647 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5648 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005649 }
5650
5651 X86AddressMode AM;
5652 MachineOperand &Op = MI->getOperand(0);
5653 if (Op.isRegister()) {
5654 AM.BaseType = X86AddressMode::RegBase;
5655 AM.Base.Reg = Op.getReg();
5656 } else {
5657 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005658 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005659 }
5660 Op = MI->getOperand(1);
5661 if (Op.isImmediate())
5662 AM.Scale = Op.getImm();
5663 Op = MI->getOperand(2);
5664 if (Op.isImmediate())
5665 AM.IndexReg = Op.getImm();
5666 Op = MI->getOperand(3);
5667 if (Op.isGlobalAddress()) {
5668 AM.GV = Op.getGlobal();
5669 } else {
5670 AM.Disp = Op.getImm();
5671 }
5672 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5673 .addReg(MI->getOperand(4).getReg());
5674
5675 // Reload the original control word now.
5676 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5677
5678 delete MI; // The pseudo instruction is gone now.
5679 return BB;
5680 }
5681 }
5682}
5683
5684//===----------------------------------------------------------------------===//
5685// X86 Optimization Hooks
5686//===----------------------------------------------------------------------===//
5687
5688void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005689 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005690 APInt &KnownZero,
5691 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005692 const SelectionDAG &DAG,
5693 unsigned Depth) const {
5694 unsigned Opc = Op.getOpcode();
5695 assert((Opc >= ISD::BUILTIN_OP_END ||
5696 Opc == ISD::INTRINSIC_WO_CHAIN ||
5697 Opc == ISD::INTRINSIC_W_CHAIN ||
5698 Opc == ISD::INTRINSIC_VOID) &&
5699 "Should use MaskedValueIsZero if you don't know whether Op"
5700 " is a target node!");
5701
Dan Gohman1d79e432008-02-13 23:07:24 +00005702 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005703 switch (Opc) {
5704 default: break;
5705 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005706 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5707 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005708 break;
5709 }
5710}
5711
5712/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5713/// element of the result of the vector shuffle.
5714static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5715 MVT::ValueType VT = N->getValueType(0);
5716 SDOperand PermMask = N->getOperand(2);
5717 unsigned NumElems = PermMask.getNumOperands();
5718 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5719 i %= NumElems;
5720 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5721 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005722 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005723 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5724 SDOperand Idx = PermMask.getOperand(i);
5725 if (Idx.getOpcode() == ISD::UNDEF)
5726 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5727 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5728 }
5729 return SDOperand();
5730}
5731
5732/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5733/// node is a GlobalAddress + an offset.
5734static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5735 unsigned Opc = N->getOpcode();
5736 if (Opc == X86ISD::Wrapper) {
5737 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5738 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5739 return true;
5740 }
5741 } else if (Opc == ISD::ADD) {
5742 SDOperand N1 = N->getOperand(0);
5743 SDOperand N2 = N->getOperand(1);
5744 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5745 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5746 if (V) {
5747 Offset += V->getSignExtended();
5748 return true;
5749 }
5750 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5751 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5752 if (V) {
5753 Offset += V->getSignExtended();
5754 return true;
5755 }
5756 }
5757 }
5758 return false;
5759}
5760
5761/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5762/// + Dist * Size.
5763static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5764 MachineFrameInfo *MFI) {
5765 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5766 return false;
5767
5768 SDOperand Loc = N->getOperand(1);
5769 SDOperand BaseLoc = Base->getOperand(1);
5770 if (Loc.getOpcode() == ISD::FrameIndex) {
5771 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5772 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005773 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5774 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775 int FS = MFI->getObjectSize(FI);
5776 int BFS = MFI->getObjectSize(BFI);
5777 if (FS != BFS || FS != Size) return false;
5778 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5779 } else {
5780 GlobalValue *GV1 = NULL;
5781 GlobalValue *GV2 = NULL;
5782 int64_t Offset1 = 0;
5783 int64_t Offset2 = 0;
5784 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5785 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5786 if (isGA1 && isGA2 && GV1 == GV2)
5787 return Offset1 == (Offset2 + Dist*Size);
5788 }
5789
5790 return false;
5791}
5792
5793static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5794 const X86Subtarget *Subtarget) {
5795 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005796 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005797 if (isGAPlusOffset(Base, GV, Offset))
5798 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005799 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800 return false;
5801}
5802
5803
5804/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5805/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5806/// if the load addresses are consecutive, non-overlapping, and in the right
5807/// order.
5808static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5809 const X86Subtarget *Subtarget) {
5810 MachineFunction &MF = DAG.getMachineFunction();
5811 MachineFrameInfo *MFI = MF.getFrameInfo();
5812 MVT::ValueType VT = N->getValueType(0);
5813 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5814 SDOperand PermMask = N->getOperand(2);
5815 int NumElems = (int)PermMask.getNumOperands();
5816 SDNode *Base = NULL;
5817 for (int i = 0; i < NumElems; ++i) {
5818 SDOperand Idx = PermMask.getOperand(i);
5819 if (Idx.getOpcode() == ISD::UNDEF) {
5820 if (!Base) return SDOperand();
5821 } else {
5822 SDOperand Arg =
5823 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5824 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5825 return SDOperand();
5826 if (!Base)
5827 Base = Arg.Val;
5828 else if (!isConsecutiveLoad(Arg.Val, Base,
5829 i, MVT::getSizeInBits(EVT)/8,MFI))
5830 return SDOperand();
5831 }
5832 }
5833
5834 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005835 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005836 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005837 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005838 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005839 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005840 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5841 LD->getSrcValueOffset(), LD->isVolatile(),
5842 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005843 }
5844}
5845
5846/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5847static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5848 const X86Subtarget *Subtarget) {
5849 SDOperand Cond = N->getOperand(0);
5850
5851 // If we have SSE[12] support, try to form min/max nodes.
5852 if (Subtarget->hasSSE2() &&
5853 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5854 if (Cond.getOpcode() == ISD::SETCC) {
5855 // Get the LHS/RHS of the select.
5856 SDOperand LHS = N->getOperand(1);
5857 SDOperand RHS = N->getOperand(2);
5858 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5859
5860 unsigned Opcode = 0;
5861 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5862 switch (CC) {
5863 default: break;
5864 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5865 case ISD::SETULE:
5866 case ISD::SETLE:
5867 if (!UnsafeFPMath) break;
5868 // FALL THROUGH.
5869 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5870 case ISD::SETLT:
5871 Opcode = X86ISD::FMIN;
5872 break;
5873
5874 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5875 case ISD::SETUGT:
5876 case ISD::SETGT:
5877 if (!UnsafeFPMath) break;
5878 // FALL THROUGH.
5879 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5880 case ISD::SETGE:
5881 Opcode = X86ISD::FMAX;
5882 break;
5883 }
5884 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5885 switch (CC) {
5886 default: break;
5887 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5888 case ISD::SETUGT:
5889 case ISD::SETGT:
5890 if (!UnsafeFPMath) break;
5891 // FALL THROUGH.
5892 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5893 case ISD::SETGE:
5894 Opcode = X86ISD::FMIN;
5895 break;
5896
5897 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5898 case ISD::SETULE:
5899 case ISD::SETLE:
5900 if (!UnsafeFPMath) break;
5901 // FALL THROUGH.
5902 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5903 case ISD::SETLT:
5904 Opcode = X86ISD::FMAX;
5905 break;
5906 }
5907 }
5908
5909 if (Opcode)
5910 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5911 }
5912
5913 }
5914
5915 return SDOperand();
5916}
5917
Chris Lattnerce84ae42008-02-22 02:09:43 +00005918/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
5919static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
5920 const X86Subtarget *Subtarget) {
5921 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
5922 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00005923 // A preferable solution to the general problem is to figure out the right
5924 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00005925 if (MVT::isVector(St->getValue().getValueType()) &&
5926 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00005927 isa<LoadSDNode>(St->getValue()) &&
5928 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
5929 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005930 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00005931 LoadSDNode *Ld = 0;
5932 int TokenFactorIndex = -1;
5933 SmallVector<SDOperand, 8> Ops;
5934 SDNode* ChainVal = St->getChain().Val;
5935 // Must be a store of a load. We currently handle two cases: the load
5936 // is a direct child, and it's under an intervening TokenFactor. It is
5937 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00005938 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00005939 Ld = cast<LoadSDNode>(St->getChain());
5940 else if (St->getValue().hasOneUse() &&
5941 ChainVal->getOpcode() == ISD::TokenFactor) {
5942 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005943 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00005944 TokenFactorIndex = i;
5945 Ld = cast<LoadSDNode>(St->getValue());
5946 } else
5947 Ops.push_back(ChainVal->getOperand(i));
5948 }
5949 }
5950 if (Ld) {
5951 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
5952 if (Subtarget->is64Bit()) {
5953 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
5954 Ld->getBasePtr(), Ld->getSrcValue(),
5955 Ld->getSrcValueOffset(), Ld->isVolatile(),
5956 Ld->getAlignment());
5957 SDOperand NewChain = NewLd.getValue(1);
5958 if (TokenFactorIndex != -1) {
5959 Ops.push_back(NewLd);
5960 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
5961 Ops.size());
5962 }
5963 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
5964 St->getSrcValue(), St->getSrcValueOffset(),
5965 St->isVolatile(), St->getAlignment());
5966 }
5967
5968 // Otherwise, lower to two 32-bit copies.
5969 SDOperand LoAddr = Ld->getBasePtr();
5970 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
5971 DAG.getConstant(MVT::i32, 4));
5972
5973 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
5974 Ld->getSrcValue(), Ld->getSrcValueOffset(),
5975 Ld->isVolatile(), Ld->getAlignment());
5976 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
5977 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
5978 Ld->isVolatile(),
5979 MinAlign(Ld->getAlignment(), 4));
5980
5981 SDOperand NewChain = LoLd.getValue(1);
5982 if (TokenFactorIndex != -1) {
5983 Ops.push_back(LoLd);
5984 Ops.push_back(HiLd);
5985 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
5986 Ops.size());
5987 }
5988
5989 LoAddr = St->getBasePtr();
5990 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
5991 DAG.getConstant(MVT::i32, 4));
5992
5993 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00005994 St->getSrcValue(), St->getSrcValueOffset(),
5995 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00005996 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
5997 St->getSrcValue(), St->getSrcValueOffset()+4,
5998 St->isVolatile(),
5999 MinAlign(St->getAlignment(), 4));
6000 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006001 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006002 }
6003 return SDOperand();
6004}
6005
Chris Lattner470d5dc2008-01-25 06:14:17 +00006006/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6007/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006008static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006009 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6010 // F[X]OR(0.0, x) -> x
6011 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006012 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6013 if (C->getValueAPF().isPosZero())
6014 return N->getOperand(1);
6015 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6016 if (C->getValueAPF().isPosZero())
6017 return N->getOperand(0);
6018 return SDOperand();
6019}
6020
6021/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6022static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6023 // FAND(0.0, x) -> 0.0
6024 // FAND(x, 0.0) -> 0.0
6025 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6026 if (C->getValueAPF().isPosZero())
6027 return N->getOperand(0);
6028 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6029 if (C->getValueAPF().isPosZero())
6030 return N->getOperand(1);
6031 return SDOperand();
6032}
6033
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006034
6035SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6036 DAGCombinerInfo &DCI) const {
6037 SelectionDAG &DAG = DCI.DAG;
6038 switch (N->getOpcode()) {
6039 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006040 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6041 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006042 case ISD::STORE:
6043 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006044 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006045 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6046 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006047 }
6048
6049 return SDOperand();
6050}
6051
6052//===----------------------------------------------------------------------===//
6053// X86 Inline Assembly Support
6054//===----------------------------------------------------------------------===//
6055
6056/// getConstraintType - Given a constraint letter, return the type of
6057/// constraint it is for this target.
6058X86TargetLowering::ConstraintType
6059X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6060 if (Constraint.size() == 1) {
6061 switch (Constraint[0]) {
6062 case 'A':
6063 case 'r':
6064 case 'R':
6065 case 'l':
6066 case 'q':
6067 case 'Q':
6068 case 'x':
6069 case 'Y':
6070 return C_RegisterClass;
6071 default:
6072 break;
6073 }
6074 }
6075 return TargetLowering::getConstraintType(Constraint);
6076}
6077
Dale Johannesene99fc902008-01-29 02:21:21 +00006078/// LowerXConstraint - try to replace an X constraint, which matches anything,
6079/// with another that has more specific requirements based on the type of the
6080/// corresponding operand.
6081void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6082 std::string& s) const {
6083 if (MVT::isFloatingPoint(ConstraintVT)) {
6084 if (Subtarget->hasSSE2())
6085 s = "Y";
6086 else if (Subtarget->hasSSE1())
6087 s = "x";
6088 else
6089 s = "f";
6090 } else
6091 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6092}
6093
Chris Lattnera531abc2007-08-25 00:47:38 +00006094/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6095/// vector. If it is invalid, don't add anything to Ops.
6096void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6097 char Constraint,
6098 std::vector<SDOperand>&Ops,
6099 SelectionDAG &DAG) {
6100 SDOperand Result(0, 0);
6101
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006102 switch (Constraint) {
6103 default: break;
6104 case 'I':
6105 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006106 if (C->getValue() <= 31) {
6107 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6108 break;
6109 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006110 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006111 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006112 case 'N':
6113 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006114 if (C->getValue() <= 255) {
6115 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6116 break;
6117 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006118 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006119 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006120 case 'i': {
6121 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006122 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6123 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6124 break;
6125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126
6127 // If we are in non-pic codegen mode, we allow the address of a global (with
6128 // an optional displacement) to be used with 'i'.
6129 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6130 int64_t Offset = 0;
6131
6132 // Match either (GA) or (GA+C)
6133 if (GA) {
6134 Offset = GA->getOffset();
6135 } else if (Op.getOpcode() == ISD::ADD) {
6136 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6137 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6138 if (C && GA) {
6139 Offset = GA->getOffset()+C->getValue();
6140 } else {
6141 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6142 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6143 if (C && GA)
6144 Offset = GA->getOffset()+C->getValue();
6145 else
6146 C = 0, GA = 0;
6147 }
6148 }
6149
6150 if (GA) {
6151 // If addressing this global requires a load (e.g. in PIC mode), we can't
6152 // match.
6153 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6154 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006155 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006156
6157 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6158 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006159 Result = Op;
6160 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006161 }
6162
6163 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006164 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006165 }
6166 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006167
6168 if (Result.Val) {
6169 Ops.push_back(Result);
6170 return;
6171 }
6172 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006173}
6174
6175std::vector<unsigned> X86TargetLowering::
6176getRegClassForInlineAsmConstraint(const std::string &Constraint,
6177 MVT::ValueType VT) const {
6178 if (Constraint.size() == 1) {
6179 // FIXME: not handling fp-stack yet!
6180 switch (Constraint[0]) { // GCC X86 Constraint Letters
6181 default: break; // Unknown constraint letter
6182 case 'A': // EAX/EDX
6183 if (VT == MVT::i32 || VT == MVT::i64)
6184 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6185 break;
6186 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6187 case 'Q': // Q_REGS
6188 if (VT == MVT::i32)
6189 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6190 else if (VT == MVT::i16)
6191 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6192 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006193 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006194 else if (VT == MVT::i64)
6195 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6196 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006197 }
6198 }
6199
6200 return std::vector<unsigned>();
6201}
6202
6203std::pair<unsigned, const TargetRegisterClass*>
6204X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6205 MVT::ValueType VT) const {
6206 // First, see if this is a constraint that directly corresponds to an LLVM
6207 // register class.
6208 if (Constraint.size() == 1) {
6209 // GCC Constraint Letters
6210 switch (Constraint[0]) {
6211 default: break;
6212 case 'r': // GENERAL_REGS
6213 case 'R': // LEGACY_REGS
6214 case 'l': // INDEX_REGS
6215 if (VT == MVT::i64 && Subtarget->is64Bit())
6216 return std::make_pair(0U, X86::GR64RegisterClass);
6217 if (VT == MVT::i32)
6218 return std::make_pair(0U, X86::GR32RegisterClass);
6219 else if (VT == MVT::i16)
6220 return std::make_pair(0U, X86::GR16RegisterClass);
6221 else if (VT == MVT::i8)
6222 return std::make_pair(0U, X86::GR8RegisterClass);
6223 break;
6224 case 'y': // MMX_REGS if MMX allowed.
6225 if (!Subtarget->hasMMX()) break;
6226 return std::make_pair(0U, X86::VR64RegisterClass);
6227 break;
6228 case 'Y': // SSE_REGS if SSE2 allowed
6229 if (!Subtarget->hasSSE2()) break;
6230 // FALL THROUGH.
6231 case 'x': // SSE_REGS if SSE1 allowed
6232 if (!Subtarget->hasSSE1()) break;
6233
6234 switch (VT) {
6235 default: break;
6236 // Scalar SSE types.
6237 case MVT::f32:
6238 case MVT::i32:
6239 return std::make_pair(0U, X86::FR32RegisterClass);
6240 case MVT::f64:
6241 case MVT::i64:
6242 return std::make_pair(0U, X86::FR64RegisterClass);
6243 // Vector types.
6244 case MVT::v16i8:
6245 case MVT::v8i16:
6246 case MVT::v4i32:
6247 case MVT::v2i64:
6248 case MVT::v4f32:
6249 case MVT::v2f64:
6250 return std::make_pair(0U, X86::VR128RegisterClass);
6251 }
6252 break;
6253 }
6254 }
6255
6256 // Use the default implementation in TargetLowering to convert the register
6257 // constraint into a member of a register class.
6258 std::pair<unsigned, const TargetRegisterClass*> Res;
6259 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6260
6261 // Not found as a standard register?
6262 if (Res.second == 0) {
6263 // GCC calls "st(0)" just plain "st".
6264 if (StringsEqualNoCase("{st}", Constraint)) {
6265 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006266 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006267 }
6268
6269 return Res;
6270 }
6271
6272 // Otherwise, check to see if this is a register class of the wrong value
6273 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6274 // turn into {ax},{dx}.
6275 if (Res.second->hasType(VT))
6276 return Res; // Correct type already, nothing to do.
6277
6278 // All of the single-register GCC register classes map their values onto
6279 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6280 // really want an 8-bit or 32-bit register, map to the appropriate register
6281 // class and return the appropriate register.
6282 if (Res.second != X86::GR16RegisterClass)
6283 return Res;
6284
6285 if (VT == MVT::i8) {
6286 unsigned DestReg = 0;
6287 switch (Res.first) {
6288 default: break;
6289 case X86::AX: DestReg = X86::AL; break;
6290 case X86::DX: DestReg = X86::DL; break;
6291 case X86::CX: DestReg = X86::CL; break;
6292 case X86::BX: DestReg = X86::BL; break;
6293 }
6294 if (DestReg) {
6295 Res.first = DestReg;
6296 Res.second = Res.second = X86::GR8RegisterClass;
6297 }
6298 } else if (VT == MVT::i32) {
6299 unsigned DestReg = 0;
6300 switch (Res.first) {
6301 default: break;
6302 case X86::AX: DestReg = X86::EAX; break;
6303 case X86::DX: DestReg = X86::EDX; break;
6304 case X86::CX: DestReg = X86::ECX; break;
6305 case X86::BX: DestReg = X86::EBX; break;
6306 case X86::SI: DestReg = X86::ESI; break;
6307 case X86::DI: DestReg = X86::EDI; break;
6308 case X86::BP: DestReg = X86::EBP; break;
6309 case X86::SP: DestReg = X86::ESP; break;
6310 }
6311 if (DestReg) {
6312 Res.first = DestReg;
6313 Res.second = Res.second = X86::GR32RegisterClass;
6314 }
6315 } else if (VT == MVT::i64) {
6316 unsigned DestReg = 0;
6317 switch (Res.first) {
6318 default: break;
6319 case X86::AX: DestReg = X86::RAX; break;
6320 case X86::DX: DestReg = X86::RDX; break;
6321 case X86::CX: DestReg = X86::RCX; break;
6322 case X86::BX: DestReg = X86::RBX; break;
6323 case X86::SI: DestReg = X86::RSI; break;
6324 case X86::DI: DestReg = X86::RDI; break;
6325 case X86::BP: DestReg = X86::RBP; break;
6326 case X86::SP: DestReg = X86::RSP; break;
6327 }
6328 if (DestReg) {
6329 Res.first = DestReg;
6330 Res.second = Res.second = X86::GR64RegisterClass;
6331 }
6332 }
6333
6334 return Res;
6335}