blob: 9140539e3828c238b29cfaa4a93d6b97308e52af [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"
Chris Lattner1b989192007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000036#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000038#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/ADT/StringExtras.h"
Duncan Sandsd8455ca2007-07-27 20:02:49 +000040#include "llvm/ParameterAttributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041using namespace llvm;
42
43X86TargetLowering::X86TargetLowering(TargetMachine &TM)
44 : TargetLowering(TM) {
45 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000046 X86ScalarSSEf64 = Subtarget->hasSSE2();
47 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000049
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
51 RegInfo = TM.getRegisterInfo();
52
53 // Set up the TargetLowering object.
54
55 // X86 is weird, it always uses i8 for shift amounts and setcc results.
56 setShiftAmountType(MVT::i8);
57 setSetCCResultType(MVT::i8);
58 setSetCCResultContents(ZeroOrOneSetCCResult);
59 setSchedulingPreference(SchedulingForRegPressure);
60 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
61 setStackPointerRegisterToSaveRestore(X86StackPtr);
62
63 if (Subtarget->isTargetDarwin()) {
64 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
65 setUseUnderscoreSetJmp(false);
66 setUseUnderscoreLongJmp(false);
67 } else if (Subtarget->isTargetMingw()) {
68 // MS runtime is weird: it exports _setjmp, but longjmp!
69 setUseUnderscoreSetJmp(true);
70 setUseUnderscoreLongJmp(false);
71 } else {
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(true);
74 }
75
76 // Set up the register classes.
77 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
78 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
79 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
80 if (Subtarget->is64Bit())
81 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
82
Duncan Sands082524c2008-01-23 20:39:46 +000083 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
Chris Lattner3bc08502008-01-17 19:59:44 +000085 // We don't accept any truncstore of integer registers.
86 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
87 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
88 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
89 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
90 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
91 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
92
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
94 // operation.
95 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
96 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
97 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
98
99 if (Subtarget->is64Bit()) {
100 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
101 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
102 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000103 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
105 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
106 else
107 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
108 }
109
110 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
111 // this operation.
112 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
113 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
114 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000115 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000117 // f32 and f64 cases are Legal, f80 case is not
118 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
119 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 }
123
Dale Johannesen958b08b2007-09-19 23:55:34 +0000124 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
125 // are Legal, f80 is custom lowered.
126 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
127 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
129 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
130 // this operation.
131 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
132 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
133
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000134 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000136 // f32 and f64 cases are Legal, f80 case is not
137 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 } else {
139 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
141 }
142
143 // Handle FP_TO_UINT by promoting the destination to a larger signed
144 // conversion.
145 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
146 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
147 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
148
149 if (Subtarget->is64Bit()) {
150 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
151 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
152 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000153 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 // Expand FP_TO_UINT into a select.
155 // FIXME: We would like to use a Custom expander here eventually to do
156 // the optimal thing for SSE vs. the default expansion in the legalizer.
157 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
158 else
159 // With SSE3 we can use fisttpll to convert to a signed i64.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
161 }
162
163 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000164 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
166 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
167 }
168
Dan Gohman5a199552007-10-08 18:33:35 +0000169 // Scalar integer multiply, multiply-high, divide, and remainder are
170 // lowered to use operations that produce two results, to match the
171 // available instructions. This exposes the two-result form to trivial
172 // CSE, which is able to combine x/y and x%y into a single instruction,
173 // for example. The single-result multiply instructions are introduced
174 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
175 // is not needed.
176 setOperationAction(ISD::MUL , MVT::i8 , Expand);
177 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
178 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
179 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
180 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
181 setOperationAction(ISD::SREM , MVT::i8 , Expand);
182 setOperationAction(ISD::UREM , MVT::i8 , Expand);
183 setOperationAction(ISD::MUL , MVT::i16 , Expand);
184 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
185 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
186 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
187 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
188 setOperationAction(ISD::SREM , MVT::i16 , Expand);
189 setOperationAction(ISD::UREM , MVT::i16 , Expand);
190 setOperationAction(ISD::MUL , MVT::i32 , Expand);
191 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
192 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
193 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
194 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
195 setOperationAction(ISD::SREM , MVT::i32 , Expand);
196 setOperationAction(ISD::UREM , MVT::i32 , Expand);
197 setOperationAction(ISD::MUL , MVT::i64 , Expand);
198 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
199 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
200 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
201 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
202 setOperationAction(ISD::SREM , MVT::i64 , Expand);
203 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000204
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
206 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
207 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
208 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
209 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
210 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
215 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
216 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000217 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
218
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000220 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
221 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000226 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
227 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 if (Subtarget->is64Bit()) {
229 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000230 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
231 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 }
233
234 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
235 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
236
237 // These should be promoted to a larger select which is supported.
238 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
239 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
240 // X86 wants to expand cmov itself.
241 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
242 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
243 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
244 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000245 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
247 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
248 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
249 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
250 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000251 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 if (Subtarget->is64Bit()) {
253 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
254 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
255 }
256 // X86 ret instruction may pop stack.
257 setOperationAction(ISD::RET , MVT::Other, Custom);
258 if (!Subtarget->is64Bit())
259 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
260
261 // Darwin ABI issue.
262 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
263 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
264 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
265 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
266 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
267 if (Subtarget->is64Bit()) {
268 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
269 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
270 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
271 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
272 }
273 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
274 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
275 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
276 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
277 // X86 wants to expand memset / memcpy itself.
278 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
279 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
280
Dan Gohman21442852007-09-25 15:10:49 +0000281 // Use the default ISD::LOCATION expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 // FIXME - use subtarget debug flags
284 if (!Subtarget->isTargetDarwin() &&
285 !Subtarget->isTargetELF() &&
286 !Subtarget->isTargetCygMing())
287 setOperationAction(ISD::LABEL, MVT::Other, Expand);
288
289 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
290 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
291 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
292 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
293 if (Subtarget->is64Bit()) {
294 // FIXME: Verify
295 setExceptionPointerRegister(X86::RAX);
296 setExceptionSelectorRegister(X86::RDX);
297 } else {
298 setExceptionPointerRegister(X86::EAX);
299 setExceptionSelectorRegister(X86::EDX);
300 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000301 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302
Duncan Sands7407a9f2007-09-11 14:10:23 +0000303 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000304
Chris Lattner56b941f2008-01-15 21:58:22 +0000305 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000306
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
308 setOperationAction(ISD::VASTART , MVT::Other, Custom);
309 setOperationAction(ISD::VAARG , MVT::Other, Expand);
310 setOperationAction(ISD::VAEND , MVT::Other, Expand);
311 if (Subtarget->is64Bit())
312 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
313 else
314 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
315
316 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
317 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
318 if (Subtarget->is64Bit())
319 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
320 if (Subtarget->isTargetCygMing())
321 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
322 else
323 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
324
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000325 if (X86ScalarSSEf64) {
326 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 // Set up the FP register classes.
328 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
329 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
330
331 // Use ANDPD to simulate FABS.
332 setOperationAction(ISD::FABS , MVT::f64, Custom);
333 setOperationAction(ISD::FABS , MVT::f32, Custom);
334
335 // Use XORP to simulate FNEG.
336 setOperationAction(ISD::FNEG , MVT::f64, Custom);
337 setOperationAction(ISD::FNEG , MVT::f32, Custom);
338
339 // Use ANDPD and ORPD to simulate FCOPYSIGN.
340 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
341 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
342
343 // We don't support sin/cos/fmod
344 setOperationAction(ISD::FSIN , MVT::f64, Expand);
345 setOperationAction(ISD::FCOS , MVT::f64, Expand);
346 setOperationAction(ISD::FREM , MVT::f64, Expand);
347 setOperationAction(ISD::FSIN , MVT::f32, Expand);
348 setOperationAction(ISD::FCOS , MVT::f32, Expand);
349 setOperationAction(ISD::FREM , MVT::f32, Expand);
350
351 // Expand FP immediates into loads from the stack, except for the special
352 // cases we handle.
353 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
354 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000355 addLegalFPImmediate(APFloat(+0.0)); // xorpd
356 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000357
358 // Conversions to long double (in X87) go through memory.
359 setConvertAction(MVT::f32, MVT::f80, Expand);
360 setConvertAction(MVT::f64, MVT::f80, Expand);
361
362 // Conversions from long double (in X87) go through memory.
363 setConvertAction(MVT::f80, MVT::f32, Expand);
364 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000365 } else if (X86ScalarSSEf32) {
366 // Use SSE for f32, x87 for f64.
367 // Set up the FP register classes.
368 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
369 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
370
371 // Use ANDPS to simulate FABS.
372 setOperationAction(ISD::FABS , MVT::f32, Custom);
373
374 // Use XORP to simulate FNEG.
375 setOperationAction(ISD::FNEG , MVT::f32, Custom);
376
377 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
378
379 // Use ANDPS and ORPS to simulate FCOPYSIGN.
380 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
381 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
382
383 // We don't support sin/cos/fmod
384 setOperationAction(ISD::FSIN , MVT::f32, Expand);
385 setOperationAction(ISD::FCOS , MVT::f32, Expand);
386 setOperationAction(ISD::FREM , MVT::f32, Expand);
387
388 // Expand FP immediates into loads from the stack, except for the special
389 // cases we handle.
390 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
391 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
392 addLegalFPImmediate(APFloat(+0.0f)); // xorps
393 addLegalFPImmediate(APFloat(+0.0)); // FLD0
394 addLegalFPImmediate(APFloat(+1.0)); // FLD1
395 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
396 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
397
398 // SSE->x87 conversions go through memory.
399 setConvertAction(MVT::f32, MVT::f64, Expand);
400 setConvertAction(MVT::f32, MVT::f80, Expand);
401
402 // x87->SSE truncations need to go through memory.
403 setConvertAction(MVT::f80, MVT::f32, Expand);
404 setConvertAction(MVT::f64, MVT::f32, Expand);
405 // And x87->x87 truncations also.
406 setConvertAction(MVT::f80, MVT::f64, Expand);
407
408 if (!UnsafeFPMath) {
409 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
410 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
411 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000413 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 // Set up the FP register classes.
415 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
416 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
417
418 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
419 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
420 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
421 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000422
423 // Floating truncations need to go through memory.
424 setConvertAction(MVT::f80, MVT::f32, Expand);
425 setConvertAction(MVT::f64, MVT::f32, Expand);
426 setConvertAction(MVT::f80, MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427
428 if (!UnsafeFPMath) {
429 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
430 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
431 }
432
433 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
434 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000435 addLegalFPImmediate(APFloat(+0.0)); // FLD0
436 addLegalFPImmediate(APFloat(+1.0)); // FLD1
437 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
438 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000439 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
440 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
441 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
442 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 }
444
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000445 // Long double always uses X87.
446 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000447 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
448 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
449 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000450 if (!UnsafeFPMath) {
451 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
452 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
453 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000454
Dan Gohman2f7b1982007-10-11 23:21:31 +0000455 // Always use a library call for pow.
456 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
457 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
458 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
459
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 // First set operation action for all vector types to expand. Then we
461 // will selectively turn on ones that can be effectively codegen'd.
462 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
463 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
464 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
465 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
466 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
467 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
468 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
469 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
470 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
471 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
475 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
476 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
477 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
478 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
479 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
480 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
481 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
482 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
485 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
486 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000487 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000491 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000492 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000495 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 }
502
503 if (Subtarget->hasMMX()) {
504 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
505 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
506 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
507 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
508
509 // FIXME: add MMX packed arithmetics
510
511 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
512 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
513 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
514 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
515
516 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
517 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
518 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000519 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520
521 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
522 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
523
524 setOperationAction(ISD::AND, MVT::v8i8, Promote);
525 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
526 setOperationAction(ISD::AND, MVT::v4i16, Promote);
527 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
528 setOperationAction(ISD::AND, MVT::v2i32, Promote);
529 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
530 setOperationAction(ISD::AND, MVT::v1i64, Legal);
531
532 setOperationAction(ISD::OR, MVT::v8i8, Promote);
533 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
534 setOperationAction(ISD::OR, MVT::v4i16, Promote);
535 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
536 setOperationAction(ISD::OR, MVT::v2i32, Promote);
537 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
538 setOperationAction(ISD::OR, MVT::v1i64, Legal);
539
540 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
541 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
542 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
543 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
544 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
545 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
546 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
547
548 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
549 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
550 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
551 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
552 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
553 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
554 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
555
556 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
557 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
558 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
559 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
560
561 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
562 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
563 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
564 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
565
566 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
567 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
568 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
569 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
570 }
571
572 if (Subtarget->hasSSE1()) {
573 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
574
575 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
576 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
577 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
578 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
579 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
580 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
582 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
583 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
584 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
585 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
586 }
587
588 if (Subtarget->hasSSE2()) {
589 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
590 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
591 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
592 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
593 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
594
595 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
596 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
597 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
598 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
599 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
600 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
601 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
602 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
603 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
604 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
605 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
606 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
607 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
608 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
609 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610
611 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
612 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
613 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
614 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
615 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
616 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
617
618 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
619 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000620 // Do not attempt to custom lower non-power-of-2 vectors
621 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
622 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
624 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
625 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
626 }
627 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
628 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
629 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
630 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
631 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000632 if (Subtarget->is64Bit())
633 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634
635 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
636 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
637 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
638 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
639 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
640 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
641 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
642 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
643 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
644 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
645 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
646 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
647 }
648
Chris Lattner3bc08502008-01-17 19:59:44 +0000649 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
650
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 // Custom lower v2i64 and v2f64 selects.
652 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
653 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
654 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
655 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
656 }
657
658 // We want to custom lower some of our intrinsics.
659 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
660
661 // We have target-specific dag combine patterns for the following nodes:
662 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
663 setTargetDAGCombine(ISD::SELECT);
664
665 computeRegisterProperties();
666
667 // FIXME: These should be based on subtarget info. Plus, the values should
668 // be smaller when we are in optimizing for size mode.
669 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
670 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
671 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
672 allowUnalignedMemoryAccesses = true; // x86 supports it!
673}
674
Evan Cheng5a67b812008-01-23 23:17:41 +0000675/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
676/// the desired ByVal argument alignment.
677static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
678 if (MaxAlign == 16)
679 return;
680 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
681 if (VTy->getBitWidth() == 128)
682 MaxAlign = 16;
683 else if (VTy->getBitWidth() == 64)
684 if (MaxAlign < 8)
685 MaxAlign = 8;
686 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
687 unsigned EltAlign = 0;
688 getMaxByValAlign(ATy->getElementType(), EltAlign);
689 if (EltAlign > MaxAlign)
690 MaxAlign = EltAlign;
691 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
692 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
693 unsigned EltAlign = 0;
694 getMaxByValAlign(STy->getElementType(i), EltAlign);
695 if (EltAlign > MaxAlign)
696 MaxAlign = EltAlign;
697 if (MaxAlign == 16)
698 break;
699 }
700 }
701 return;
702}
703
704/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
705/// function arguments in the caller parameter area. For X86, aggregates
706/// that contains are placed at 16-byte boundaries while the rest are at
707/// 4-byte boundaries.
708unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
709 if (Subtarget->is64Bit())
710 return getTargetData()->getABITypeAlignment(Ty);
711 unsigned Align = 4;
712 getMaxByValAlign(Ty, Align);
713 return Align;
714}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715
Evan Cheng6fb06762007-11-09 01:32:10 +0000716/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
717/// jumptable.
718SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
719 SelectionDAG &DAG) const {
720 if (usesGlobalOffsetTable())
721 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
722 if (!Subtarget->isPICStyleRIPRel())
723 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
724 return Table;
725}
726
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727//===----------------------------------------------------------------------===//
728// Return Value Calling Convention Implementation
729//===----------------------------------------------------------------------===//
730
731#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000732
733/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
734/// exists skip possible ISD:TokenFactor.
735static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000736 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000737 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000738 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000739 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000740 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000741 return Chain.getOperand(0);
742 }
743 return Chain;
744}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000745
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746/// LowerRET - Lower an ISD::RET node.
747SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
748 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
749
750 SmallVector<CCValAssign, 16> RVLocs;
751 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
752 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
753 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
754 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 // If this is the first return lowered for this function, add the regs to the
757 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000758 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 for (unsigned i = 0; i != RVLocs.size(); ++i)
760 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000761 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000765 // Handle tail call return.
766 Chain = GetPossiblePreceedingTailCall(Chain);
767 if (Chain.getOpcode() == X86ISD::TAILCALL) {
768 SDOperand TailCall = Chain;
769 SDOperand TargetAddress = TailCall.getOperand(1);
770 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000771 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000772 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
773 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
774 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
775 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
776 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000777 assert(StackAdjustment.getOpcode() == ISD::Constant &&
778 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000779
780 SmallVector<SDOperand,8> Operands;
781 Operands.push_back(Chain.getOperand(0));
782 Operands.push_back(TargetAddress);
783 Operands.push_back(StackAdjustment);
784 // Copy registers used by the call. Last operand is a flag so it is not
785 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000786 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000787 Operands.push_back(Chain.getOperand(i));
788 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000789 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
790 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000791 }
792
793 // Regular return.
794 SDOperand Flag;
795
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 // Copy the result values into the output registers.
797 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
798 RVLocs[0].getLocReg() != X86::ST0) {
799 for (unsigned i = 0; i != RVLocs.size(); ++i) {
800 CCValAssign &VA = RVLocs[i];
801 assert(VA.isRegLoc() && "Can only return in registers!");
802 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
803 Flag);
804 Flag = Chain.getValue(1);
805 }
806 } else {
807 // We need to handle a destination of ST0 specially, because it isn't really
808 // a register.
809 SDOperand Value = Op.getOperand(1);
810
811 // If this is an FP return with ScalarSSE, we need to move the value from
812 // an XMM register onto the fp-stack.
Chris Lattnercf515b52008-01-16 06:24:21 +0000813 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 SDOperand MemLoc;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000815
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 // If this is a load into a scalarsse value, don't store the loaded value
817 // back to the stack, only to reload it: just replace the scalar-sse load.
818 if (ISD::isNON_EXTLoad(Value.Val) &&
Chris Lattnerf51fdb52008-01-16 05:56:59 +0000819 Chain.reachesChainWithoutSideEffects(Value.getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 Chain = Value.getOperand(0);
821 MemLoc = Value.getOperand(1);
822 } else {
823 // Spill the value to memory and reload it into top of stack.
824 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
825 MachineFunction &MF = DAG.getMachineFunction();
826 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
827 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
828 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
829 }
830 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
831 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
832 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
833 Chain = Value.getValue(1);
834 }
835
836 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
837 SDOperand Ops[] = { Chain, Value };
838 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
839 Flag = Chain.getValue(1);
840 }
841
842 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
843 if (Flag.Val)
844 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
845 else
846 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
847}
848
849
850/// LowerCallResult - Lower the result values of an ISD::CALL into the
851/// appropriate copies out of appropriate physical registers. This assumes that
852/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
853/// being lowered. The returns a SDNode with the same number of values as the
854/// ISD::CALL.
855SDNode *X86TargetLowering::
856LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
857 unsigned CallingConv, SelectionDAG &DAG) {
858
859 // Assign locations to each value returned by this call.
860 SmallVector<CCValAssign, 16> RVLocs;
861 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
862 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
863 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
864
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 SmallVector<SDOperand, 8> ResultVals;
866
867 // Copy all of the result registers out of their specified physreg.
868 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
869 for (unsigned i = 0; i != RVLocs.size(); ++i) {
870 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
871 RVLocs[i].getValVT(), InFlag).getValue(1);
872 InFlag = Chain.getValue(2);
873 ResultVals.push_back(Chain.getValue(0));
874 }
875 } else {
876 // Copies from the FP stack are special, as ST0 isn't a valid register
877 // before the fp stackifier runs.
878
879 // Copy ST0 into an RFP register with FP_GET_RESULT.
880 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
881 SDOperand GROps[] = { Chain, InFlag };
882 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
883 Chain = RetVal.getValue(1);
884 InFlag = RetVal.getValue(2);
885
886 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
887 // an XMM register.
Chris Lattnercf515b52008-01-16 06:24:21 +0000888 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Chris Lattner40758732007-12-29 06:41:28 +0000889 SDOperand StoreLoc;
890 const Value *SrcVal = 0;
891 int SrcValOffset = 0;
Chris Lattner8b815c22007-12-29 06:57:38 +0000892 MVT::ValueType RetStoreVT = RVLocs[0].getValVT();
Chris Lattner40758732007-12-29 06:41:28 +0000893
894 // Determine where to store the value. If the call result is directly
895 // used by a store, see if we can store directly into the location. In
896 // this case, we'll end up producing a fst + movss[load] + movss[store] to
897 // the same location, and the two movss's will be nuked as dead. This
898 // optimizes common things like "*D = atof(..)" to not need an
899 // intermediate stack slot.
900 if (SDOperand(TheCall, 0).hasOneUse() &&
901 SDOperand(TheCall, 1).hasOneUse()) {
Chris Lattner8b815c22007-12-29 06:57:38 +0000902 // In addition to direct uses, we also support a FP_ROUND that uses the
903 // value, if it is directly stored somewhere.
904 SDNode *User = *TheCall->use_begin();
905 if (User->getOpcode() == ISD::FP_ROUND && User->hasOneUse())
906 User = *User->use_begin();
907
Chris Lattner40758732007-12-29 06:41:28 +0000908 // Ok, we have one use of the value and one use of the chain. See if
909 // they are the same node: a store.
Chris Lattner8b815c22007-12-29 06:57:38 +0000910 if (StoreSDNode *N = dyn_cast<StoreSDNode>(User)) {
911 // Verify that the value being stored is either the call or a
912 // truncation of the call.
913 SDNode *StoreVal = N->getValue().Val;
914 if (StoreVal == TheCall)
915 ; // ok.
916 else if (StoreVal->getOpcode() == ISD::FP_ROUND &&
917 StoreVal->hasOneUse() &&
918 StoreVal->getOperand(0).Val == TheCall)
919 ; // ok.
920 else
921 N = 0; // not ok.
922
923 if (N && N->getChain().Val == TheCall &&
Chris Lattner40758732007-12-29 06:41:28 +0000924 !N->isVolatile() && !N->isTruncatingStore() &&
925 N->getAddressingMode() == ISD::UNINDEXED) {
926 StoreLoc = N->getBasePtr();
927 SrcVal = N->getSrcValue();
928 SrcValOffset = N->getSrcValueOffset();
Chris Lattner8b815c22007-12-29 06:57:38 +0000929 RetStoreVT = N->getValue().getValueType();
Chris Lattner40758732007-12-29 06:41:28 +0000930 }
931 }
932 }
933
934 // If we weren't able to optimize the result, just create a temporary
935 // stack slot.
936 if (StoreLoc.Val == 0) {
937 MachineFunction &MF = DAG.getMachineFunction();
938 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
939 StoreLoc = DAG.getFrameIndex(SSFI, getPointerTy());
940 }
941
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
943 // shouldn't be necessary except that RFP cannot be live across
Chris Lattner40758732007-12-29 06:41:28 +0000944 // multiple blocks (which could happen if a select gets lowered into
945 // multiple blocks and scheduled in between them). When stackifier is
946 // fixed, they can be uncoupled.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 SDOperand Ops[] = {
Chris Lattner8b815c22007-12-29 06:57:38 +0000948 Chain, RetVal, StoreLoc, DAG.getValueType(RetStoreVT), InFlag
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 };
950 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner8b815c22007-12-29 06:57:38 +0000951 RetVal = DAG.getLoad(RetStoreVT, Chain,
Chris Lattner40758732007-12-29 06:41:28 +0000952 StoreLoc, SrcVal, SrcValOffset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 Chain = RetVal.getValue(1);
Chris Lattner8b815c22007-12-29 06:57:38 +0000954
955 // If we optimized a truncate, then extend the result back to its desired
956 // type.
957 if (RVLocs[0].getValVT() != RetStoreVT)
958 RetVal = DAG.getNode(ISD::FP_EXTEND, RVLocs[0].getValVT(), RetVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 }
960 ResultVals.push_back(RetVal);
961 }
962
963 // Merge everything together with a MERGE_VALUES node.
964 ResultVals.push_back(Chain);
965 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
966 &ResultVals[0], ResultVals.size()).Val;
967}
968
969
970//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000971// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972//===----------------------------------------------------------------------===//
973// StdCall calling convention seems to be standard for many Windows' API
974// routines and around. It differs from C calling convention just a little:
975// callee should clean up the stack, not caller. Symbols should be also
976// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000977// For info on fast calling convention see Fast Calling Convention (tail call)
978// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979
980/// AddLiveIn - This helper function adds the specified physical register to the
981/// MachineFunction as a live in value. It also creates a corresponding virtual
982/// register for it.
983static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
984 const TargetRegisterClass *RC) {
985 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000986 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
987 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 return VReg;
989}
990
Gordon Henriksen18ace102008-01-05 16:56:59 +0000991// Determines whether a CALL node uses struct return semantics.
992static bool CallIsStructReturn(SDOperand Op) {
993 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
994 if (!NumOps)
995 return false;
996
997 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
998 return Flags->getValue() & ISD::ParamFlags::StructReturn;
999}
1000
1001// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
1002static bool ArgsAreStructReturn(SDOperand Op) {
1003 unsigned NumArgs = Op.Val->getNumValues() - 1;
1004 if (!NumArgs)
1005 return false;
1006
1007 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1008 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1009}
1010
1011// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
1012// its own arguments. Callee pop is necessary to support tail calls.
1013bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1014 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1015 if (IsVarArg)
1016 return false;
1017
1018 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1019 default:
1020 return false;
1021 case CallingConv::X86_StdCall:
1022 return !Subtarget->is64Bit();
1023 case CallingConv::X86_FastCall:
1024 return !Subtarget->is64Bit();
1025 case CallingConv::Fast:
1026 return PerformTailCallOpt;
1027 }
1028}
1029
1030// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
1031CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1032 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1033
1034 if (Subtarget->is64Bit())
1035 if (CC == CallingConv::Fast && PerformTailCallOpt)
1036 return CC_X86_64_TailCall;
1037 else
1038 return CC_X86_64_C;
1039
1040 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 Schwaighofer449b01a2008-01-11 16:49:42 +00001060
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001061// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly
1062// be overwritten when lowering the outgoing arguments in a tail call. Currently
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001063// the implementation of this call is very conservative and assumes all
1064// arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001065// registers would be overwritten by direct lowering.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001066// Possible improvement:
1067// Check FORMAL_ARGUMENTS corresponding MERGE_VALUES for CopyFromReg nodes
1068// indicating inreg passed arguments which also need not be lowered to a safe
1069// stack slot.
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001070static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001071 RegisterSDNode * OpReg = NULL;
1072 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1073 (Op.getOpcode()== ISD::CopyFromReg &&
1074 (OpReg = cast<RegisterSDNode>(Op.getOperand(1))) &&
1075 OpReg->getReg() >= MRegisterInfo::FirstVirtualRegister))
1076 return true;
1077 return false;
1078}
1079
Evan Cheng5817a0e2008-01-12 01:08:07 +00001080// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1081// by "Src" to address "Dst" with size and alignment information specified by
1082// the specific parameter attribute. The copy will be passed as a byval function
1083// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001084static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001085CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1086 unsigned Flags, SelectionDAG &DAG) {
1087 unsigned Align = 1 <<
1088 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1089 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001090 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001091 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1092 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001093 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001094 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001095}
1096
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001097SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1098 const CCValAssign &VA,
1099 MachineFrameInfo *MFI,
1100 SDOperand Root, unsigned i) {
1101 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001102 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1103 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1104
1105 // FIXME: For now, all byval parameter objects are marked mutable. This
1106 // can be changed with more analysis.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001107 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Evan Cheng3e42a522008-01-10 02:24:25 +00001108 VA.getLocMemOffset(), !isByVal);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001109 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001110 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001111 return FIN;
Evan Cheng3e42a522008-01-10 02:24:25 +00001112 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001113}
1114
Gordon Henriksen18ace102008-01-05 16:56:59 +00001115SDOperand
1116X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001118 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1119
1120 const Function* Fn = MF.getFunction();
1121 if (Fn->hasExternalLinkage() &&
1122 Subtarget->isTargetCygMing() &&
1123 Fn->getName() == "main")
1124 FuncInfo->setForceFramePointer(true);
1125
1126 // Decorate the function name.
1127 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1128
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129 MachineFrameInfo *MFI = MF.getFrameInfo();
1130 SDOperand Root = Op.getOperand(0);
1131 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001132 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001133 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001134
1135 assert(!(isVarArg && CC == CallingConv::Fast) &&
1136 "Var args not supported with calling convention fastcc");
1137
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 // Assign locations to all of the incoming arguments.
1139 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001140 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001141 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001142
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 SmallVector<SDOperand, 8> ArgValues;
1144 unsigned LastVal = ~0U;
1145 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1146 CCValAssign &VA = ArgLocs[i];
1147 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1148 // places.
1149 assert(VA.getValNo() != LastVal &&
1150 "Don't support value assigned to multiple locs yet");
1151 LastVal = VA.getValNo();
1152
1153 if (VA.isRegLoc()) {
1154 MVT::ValueType RegVT = VA.getLocVT();
1155 TargetRegisterClass *RC;
1156 if (RegVT == MVT::i32)
1157 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001158 else if (Is64Bit && RegVT == MVT::i64)
1159 RC = X86::GR64RegisterClass;
1160 else if (Is64Bit && RegVT == MVT::f32)
1161 RC = X86::FR32RegisterClass;
1162 else if (Is64Bit && RegVT == MVT::f64)
1163 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 else {
1165 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001166 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1167 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1168 RegVT = MVT::i64;
1169 } else
1170 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001171 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1174 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1175
1176 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1177 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1178 // right size.
1179 if (VA.getLocInfo() == CCValAssign::SExt)
1180 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1181 DAG.getValueType(VA.getValVT()));
1182 else if (VA.getLocInfo() == CCValAssign::ZExt)
1183 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1184 DAG.getValueType(VA.getValVT()));
1185
1186 if (VA.getLocInfo() != CCValAssign::Full)
1187 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1188
Gordon Henriksen18ace102008-01-05 16:56:59 +00001189 // Handle MMX values passed in GPRs.
1190 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1191 MVT::getSizeInBits(RegVT) == 64)
1192 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001194 ArgValues.push_back(ArgValue);
1195 } else {
1196 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001197 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 }
1199 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001200
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001201 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001202 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001203 if (CC == CallingConv::Fast)
1204 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001205
1206 // If the function takes variable number of arguments, make a frame index for
1207 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001208 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001209 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1210 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1211 }
1212 if (Is64Bit) {
1213 static const unsigned GPR64ArgRegs[] = {
1214 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1215 };
1216 static const unsigned XMMArgRegs[] = {
1217 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1218 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1219 };
1220
1221 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1222 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1223
1224 // For X86-64, if there are vararg parameters that are passed via
1225 // registers, then we must store them to their spots on the stack so they
1226 // may be loaded by deferencing the result of va_next.
1227 VarArgsGPOffset = NumIntRegs * 8;
1228 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1229 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1230
1231 // Store the integer parameter registers.
1232 SmallVector<SDOperand, 8> MemOps;
1233 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1234 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001235 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001236 for (; NumIntRegs != 6; ++NumIntRegs) {
1237 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1238 X86::GR64RegisterClass);
1239 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1240 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1241 MemOps.push_back(Store);
1242 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001243 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001244 }
1245
1246 // Now store the XMM (fp + vector) parameter registers.
1247 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001248 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001249 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1250 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1251 X86::VR128RegisterClass);
1252 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1253 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1254 MemOps.push_back(Store);
1255 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001256 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001257 }
1258 if (!MemOps.empty())
1259 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1260 &MemOps[0], MemOps.size());
1261 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001262 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001263
1264 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1265 // arguments and the arguments after the retaddr has been pushed are
1266 // aligned.
1267 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1268 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1269 (StackSize & 7) == 0)
1270 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001272 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001273
Gordon Henriksen18ace102008-01-05 16:56:59 +00001274 // Some CCs need callee pop.
1275 if (IsCalleePop(Op)) {
1276 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277 BytesCallerReserves = 0;
1278 } else {
1279 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001281 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 BytesCallerReserves = StackSize;
1284 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001285
Gordon Henriksen18ace102008-01-05 16:56:59 +00001286 if (!Is64Bit) {
1287 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1288 if (CC == CallingConv::X86_FastCall)
1289 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1290 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291
Anton Korobeynikove844e472007-08-15 17:12:32 +00001292 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001293
1294 // Return the new list of results.
1295 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1296 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1297}
1298
Evan Chengbc077bf2008-01-10 00:09:10 +00001299SDOperand
1300X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1301 const SDOperand &StackPtr,
1302 const CCValAssign &VA,
1303 SDOperand Chain,
1304 SDOperand Arg) {
Chris Lattner5872a362008-01-17 07:00:52 +00001305 SDOperand PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset());
Evan Chengbc077bf2008-01-10 00:09:10 +00001306 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1307 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1308 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1309 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001310 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001311 }
Evan Cheng5817a0e2008-01-12 01:08:07 +00001312 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
Evan Chengbc077bf2008-01-10 00:09:10 +00001313}
1314
Gordon Henriksen18ace102008-01-05 16:56:59 +00001315SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1316 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001318 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1321 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001323 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001324
1325 assert(!(isVarArg && CC == CallingConv::Fast) &&
1326 "Var args not supported with calling convention fastcc");
1327
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 // Analyze operands of the call, assigning locations to each operand.
1329 SmallVector<CCValAssign, 16> ArgLocs;
1330 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001331 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332
1333 // Get a count of how many bytes are to be pushed on the stack.
1334 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001335 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001336 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337
Gordon Henriksen18ace102008-01-05 16:56:59 +00001338 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1339 // arguments and the arguments after the retaddr has been pushed are aligned.
1340 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1341 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1342 (NumBytes & 7) == 0)
1343 NumBytes += 4;
1344
1345 int FPDiff = 0;
1346 if (IsTailCall) {
1347 // Lower arguments at fp - stackoffset + fpdiff.
1348 unsigned NumBytesCallerPushed =
1349 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1350 FPDiff = NumBytesCallerPushed - NumBytes;
1351
1352 // Set the delta of movement of the returnaddr stackslot.
1353 // But only set if delta is greater than previous delta.
1354 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1355 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1356 }
1357
Chris Lattner5872a362008-01-17 07:00:52 +00001358 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1361 if (IsTailCall) {
1362 // Adjust the Return address stack slot.
1363 if (FPDiff) {
1364 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1365 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1366 // Load the "old" Return address.
1367 RetAddrFrIdx =
1368 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1369 // Calculate the new stack slot for the return address.
1370 int SlotSize = Is64Bit ? 8 : 4;
1371 int NewReturnAddrFI =
1372 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1373 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1374 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1375 }
1376 }
1377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001378 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1379 SmallVector<SDOperand, 8> MemOpChains;
1380
1381 SDOperand StackPtr;
1382
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001383 // Walk the register/memloc assignments, inserting copies/loads. For tail
1384 // calls, lower arguments which could otherwise be possibly overwritten to the
1385 // stack slot where they would go on normal function calls.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1387 CCValAssign &VA = ArgLocs[i];
1388 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1389
1390 // Promote the value if needed.
1391 switch (VA.getLocInfo()) {
1392 default: assert(0 && "Unknown loc info!");
1393 case CCValAssign::Full: break;
1394 case CCValAssign::SExt:
1395 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1396 break;
1397 case CCValAssign::ZExt:
1398 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1399 break;
1400 case CCValAssign::AExt:
1401 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1402 break;
1403 }
1404
1405 if (VA.isRegLoc()) {
1406 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1407 } else {
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001408 if (!IsTailCall || IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001409 assert(VA.isMemLoc());
1410 if (StackPtr.Val == 0)
1411 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1412
1413 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1414 Arg));
1415 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 }
1417 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418
1419 if (!MemOpChains.empty())
1420 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1421 &MemOpChains[0], MemOpChains.size());
1422
1423 // Build a sequence of copy-to-reg nodes chained together with token chain
1424 // and flag operands which copy the outgoing args into registers.
1425 SDOperand InFlag;
1426 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1427 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1428 InFlag);
1429 InFlag = Chain.getValue(1);
1430 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001431
1432 if (IsTailCall)
1433 InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output?
1434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1436 // GOT pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001437 // Does not work with tail call since ebx is not restored correctly by
1438 // tailcaller. TODO: at least for x86 - verify for x86-64
1439 if (!IsTailCall && !Is64Bit &&
1440 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001441 Subtarget->isPICStyleGOT()) {
1442 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1443 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1444 InFlag);
1445 InFlag = Chain.getValue(1);
1446 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001447
Gordon Henriksen18ace102008-01-05 16:56:59 +00001448 if (Is64Bit && isVarArg) {
1449 // From AMD64 ABI document:
1450 // For calls that may call functions that use varargs or stdargs
1451 // (prototype-less calls or calls to functions containing ellipsis (...) in
1452 // the declaration) %al is used as hidden argument to specify the number
1453 // of SSE registers used. The contents of %al do not need to match exactly
1454 // the number of registers, but must be an ubound on the number of SSE
1455 // registers used and is in the range 0 - 8 inclusive.
1456
1457 // Count the number of XMM registers allocated.
1458 static const unsigned XMMArgRegs[] = {
1459 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1460 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1461 };
1462 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1463
1464 Chain = DAG.getCopyToReg(Chain, X86::AL,
1465 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1466 InFlag = Chain.getValue(1);
1467 }
1468
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001469 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001470 if (IsTailCall) {
1471 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001472 SDOperand FIN;
1473 int FI = 0;
1474 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1475 CCValAssign &VA = ArgLocs[i];
1476 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001477 assert(VA.isMemLoc());
1478 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001479 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1480 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001481 // Create frame index.
1482 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1483 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1484 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1485 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001486 SDOperand Source = Arg;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001487 if (IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001488 // Copy from stack slots to stack slot of a tail called function. This
1489 // needs to be done because if we would lower the arguments directly
1490 // to their real stack slot we might end up overwriting each other.
1491 // Get source stack slot.
Chris Lattner5872a362008-01-17 07:00:52 +00001492 Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001493 if (StackPtr.Val == 0)
1494 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1495 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1496 if ((Flags & ISD::ParamFlags::ByVal)==0)
Duncan Sands22981632008-01-13 21:20:29 +00001497 Source = DAG.getLoad(VA.getValVT(), Chain, Source, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001498 }
1499
Gordon Henriksen18ace102008-01-05 16:56:59 +00001500 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001501 // Copy relative to framepointer.
1502 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
1503 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001504 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001505 // Store relative to framepointer.
1506 MemOpChains2.push_back(DAG.getStore(Chain, Source, FIN, NULL, 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001507 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001508 }
1509 }
1510
1511 if (!MemOpChains2.empty())
1512 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001513 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001514
1515 // Store the return address to the appropriate stack slot.
1516 if (FPDiff)
1517 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1518 }
1519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 // If the callee is a GlobalAddress node (quite common, every direct call is)
1521 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1522 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1523 // We should use extra load for direct calls to dllimported functions in
1524 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001525 if ((IsTailCall || !Is64Bit ||
1526 getTargetMachine().getCodeModel() != CodeModel::Large)
1527 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1528 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001530 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001531 if (IsTailCall || !Is64Bit ||
1532 getTargetMachine().getCodeModel() != CodeModel::Large)
1533 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1534 } else if (IsTailCall) {
1535 assert(Callee.getOpcode() == ISD::LOAD &&
1536 "Function destination must be loaded into virtual register");
1537 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1538
1539 Chain = DAG.getCopyToReg(Chain,
1540 DAG.getRegister(Opc, getPointerTy()) ,
1541 Callee,InFlag);
1542 Callee = DAG.getRegister(Opc, getPointerTy());
1543 // Add register as live out.
1544 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001545 }
1546
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 // Returns a chain & a flag for retval copy to use.
1548 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1549 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001550
1551 if (IsTailCall) {
1552 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001553 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1554 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001555 if (InFlag.Val)
1556 Ops.push_back(InFlag);
1557 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1558 InFlag = Chain.getValue(1);
1559
1560 // Returns a chain & a flag for retval copy to use.
1561 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1562 Ops.clear();
1563 }
1564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001565 Ops.push_back(Chain);
1566 Ops.push_back(Callee);
1567
Gordon Henriksen18ace102008-01-05 16:56:59 +00001568 if (IsTailCall)
1569 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570
1571 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001572 if (!IsTailCall && !Is64Bit &&
1573 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 Subtarget->isPICStyleGOT())
1575 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001576
Gordon Henriksen18ace102008-01-05 16:56:59 +00001577 // Add argument registers to the end of the list so that they are known live
1578 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001579 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1580 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1581 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001582
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583 if (InFlag.Val)
1584 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001585
Gordon Henriksen18ace102008-01-05 16:56:59 +00001586 if (IsTailCall) {
1587 assert(InFlag.Val &&
1588 "Flag must be set. Depend on flag being set in LowerRET");
1589 Chain = DAG.getNode(X86ISD::TAILCALL,
1590 Op.Val->getVTList(), &Ops[0], Ops.size());
1591
1592 return SDOperand(Chain.Val, Op.ResNo);
1593 }
1594
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001595 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001596 InFlag = Chain.getValue(1);
1597
1598 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001599 unsigned NumBytesForCalleeToPush;
1600 if (IsCalleePop(Op))
1601 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
1602 else if (!Is64Bit && CallIsStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 // If this is is a call to a struct-return function, the callee
1604 // pops the hidden struct pointer, so we have to push it back.
1605 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001606 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001607 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001608 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001609
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001610 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001611 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001612 DAG.getIntPtrConstant(NumBytes),
1613 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001614 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615 InFlag = Chain.getValue(1);
1616
1617 // Handle result values, copying them out of physregs into vregs that we
1618 // return.
1619 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1620}
1621
1622
1623//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001624// Fast Calling Convention (tail call) implementation
1625//===----------------------------------------------------------------------===//
1626
1627// Like std call, callee cleans arguments, convention except that ECX is
1628// reserved for storing the tail called function address. Only 2 registers are
1629// free for argument passing (inreg). Tail call optimization is performed
1630// provided:
1631// * tailcallopt is enabled
1632// * caller/callee are fastcc
1633// * elf/pic is disabled OR
1634// * elf/pic enabled + callee is in module + callee has
1635// visibility protected or hidden
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001636// To keep the stack aligned according to platform abi the function
1637// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1638// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001639// If a tail called function callee has more arguments than the caller the
1640// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001641// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001642// original REtADDR, but before the saved framepointer or the spilled registers
1643// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1644// stack layout:
1645// arg1
1646// arg2
1647// RETADDR
1648// [ new RETADDR
1649// move area ]
1650// (possible EBP)
1651// ESI
1652// EDI
1653// local1 ..
1654
1655/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1656/// for a 16 byte align requirement.
1657unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1658 SelectionDAG& DAG) {
1659 if (PerformTailCallOpt) {
1660 MachineFunction &MF = DAG.getMachineFunction();
1661 const TargetMachine &TM = MF.getTarget();
1662 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1663 unsigned StackAlignment = TFI.getStackAlignment();
1664 uint64_t AlignMask = StackAlignment - 1;
1665 int64_t Offset = StackSize;
1666 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1667 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1668 // Number smaller than 12 so just add the difference.
1669 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1670 } else {
1671 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1672 Offset = ((~AlignMask) & Offset) + StackAlignment +
1673 (StackAlignment-SlotSize);
1674 }
1675 StackSize = Offset;
1676 }
1677 return StackSize;
1678}
1679
1680/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001681/// following the call is a return. A function is eligible if caller/callee
1682/// calling conventions match, currently only fastcc supports tail calls, and
1683/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001684bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1685 SDOperand Ret,
1686 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001687 if (!PerformTailCallOpt)
1688 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001689
1690 // Check whether CALL node immediatly preceeds the RET node and whether the
1691 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001692 unsigned NumOps = Ret.getNumOperands();
1693 if ((NumOps == 1 &&
1694 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1695 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001696 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001697 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1698 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001699 MachineFunction &MF = DAG.getMachineFunction();
1700 unsigned CallerCC = MF.getFunction()->getCallingConv();
1701 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1702 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1703 SDOperand Callee = Call.getOperand(4);
1704 // On elf/pic %ebx needs to be livein.
Evan Chenge7a87392007-11-02 01:26:22 +00001705 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1706 !Subtarget->isPICStyleGOT())
1707 return true;
1708
1709 // Can only do local tail calls with PIC.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1711 return G->getGlobal()->hasHiddenVisibility()
1712 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001713 }
1714 }
Evan Chenge7a87392007-11-02 01:26:22 +00001715
1716 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001717}
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719//===----------------------------------------------------------------------===//
1720// Other Lowering Hooks
1721//===----------------------------------------------------------------------===//
1722
1723
1724SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001725 MachineFunction &MF = DAG.getMachineFunction();
1726 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1727 int ReturnAddrIndex = FuncInfo->getRAIndex();
1728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001729 if (ReturnAddrIndex == 0) {
1730 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731 if (Subtarget->is64Bit())
1732 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1733 else
1734 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001735
1736 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737 }
1738
1739 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1740}
1741
1742
1743
1744/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1745/// specific condition code. It returns a false if it cannot do a direct
1746/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1747/// needed.
1748static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1749 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1750 SelectionDAG &DAG) {
1751 X86CC = X86::COND_INVALID;
1752 if (!isFP) {
1753 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1754 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1755 // X > -1 -> X == 0, jump !sign.
1756 RHS = DAG.getConstant(0, RHS.getValueType());
1757 X86CC = X86::COND_NS;
1758 return true;
1759 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1760 // X < 0 -> X == 0, jump on sign.
1761 X86CC = X86::COND_S;
1762 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001763 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1764 // X < 1 -> X <= 0
1765 RHS = DAG.getConstant(0, RHS.getValueType());
1766 X86CC = X86::COND_LE;
1767 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001768 }
1769 }
1770
1771 switch (SetCCOpcode) {
1772 default: break;
1773 case ISD::SETEQ: X86CC = X86::COND_E; break;
1774 case ISD::SETGT: X86CC = X86::COND_G; break;
1775 case ISD::SETGE: X86CC = X86::COND_GE; break;
1776 case ISD::SETLT: X86CC = X86::COND_L; break;
1777 case ISD::SETLE: X86CC = X86::COND_LE; break;
1778 case ISD::SETNE: X86CC = X86::COND_NE; break;
1779 case ISD::SETULT: X86CC = X86::COND_B; break;
1780 case ISD::SETUGT: X86CC = X86::COND_A; break;
1781 case ISD::SETULE: X86CC = X86::COND_BE; break;
1782 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1783 }
1784 } else {
1785 // On a floating point condition, the flags are set as follows:
1786 // ZF PF CF op
1787 // 0 | 0 | 0 | X > Y
1788 // 0 | 0 | 1 | X < Y
1789 // 1 | 0 | 0 | X == Y
1790 // 1 | 1 | 1 | unordered
1791 bool Flip = false;
1792 switch (SetCCOpcode) {
1793 default: break;
1794 case ISD::SETUEQ:
1795 case ISD::SETEQ: X86CC = X86::COND_E; break;
1796 case ISD::SETOLT: Flip = true; // Fallthrough
1797 case ISD::SETOGT:
1798 case ISD::SETGT: X86CC = X86::COND_A; break;
1799 case ISD::SETOLE: Flip = true; // Fallthrough
1800 case ISD::SETOGE:
1801 case ISD::SETGE: X86CC = X86::COND_AE; break;
1802 case ISD::SETUGT: Flip = true; // Fallthrough
1803 case ISD::SETULT:
1804 case ISD::SETLT: X86CC = X86::COND_B; break;
1805 case ISD::SETUGE: Flip = true; // Fallthrough
1806 case ISD::SETULE:
1807 case ISD::SETLE: X86CC = X86::COND_BE; break;
1808 case ISD::SETONE:
1809 case ISD::SETNE: X86CC = X86::COND_NE; break;
1810 case ISD::SETUO: X86CC = X86::COND_P; break;
1811 case ISD::SETO: X86CC = X86::COND_NP; break;
1812 }
1813 if (Flip)
1814 std::swap(LHS, RHS);
1815 }
1816
1817 return X86CC != X86::COND_INVALID;
1818}
1819
1820/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1821/// code. Current x86 isa includes the following FP cmov instructions:
1822/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1823static bool hasFPCMov(unsigned X86CC) {
1824 switch (X86CC) {
1825 default:
1826 return false;
1827 case X86::COND_B:
1828 case X86::COND_BE:
1829 case X86::COND_E:
1830 case X86::COND_P:
1831 case X86::COND_A:
1832 case X86::COND_AE:
1833 case X86::COND_NE:
1834 case X86::COND_NP:
1835 return true;
1836 }
1837}
1838
1839/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1840/// true if Op is undef or if its value falls within the specified range (L, H].
1841static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1842 if (Op.getOpcode() == ISD::UNDEF)
1843 return true;
1844
1845 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1846 return (Val >= Low && Val < Hi);
1847}
1848
1849/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1850/// true if Op is undef or if its value equal to the specified value.
1851static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1852 if (Op.getOpcode() == ISD::UNDEF)
1853 return true;
1854 return cast<ConstantSDNode>(Op)->getValue() == Val;
1855}
1856
1857/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1858/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1859bool X86::isPSHUFDMask(SDNode *N) {
1860 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1861
Dan Gohman7dc19012007-08-02 21:17:01 +00001862 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001863 return false;
1864
1865 // Check if the value doesn't reference the second vector.
1866 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1867 SDOperand Arg = N->getOperand(i);
1868 if (Arg.getOpcode() == ISD::UNDEF) continue;
1869 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00001870 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001871 return false;
1872 }
1873
1874 return true;
1875}
1876
1877/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1878/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1879bool X86::isPSHUFHWMask(SDNode *N) {
1880 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1881
1882 if (N->getNumOperands() != 8)
1883 return false;
1884
1885 // Lower quadword copied in order.
1886 for (unsigned i = 0; i != 4; ++i) {
1887 SDOperand Arg = N->getOperand(i);
1888 if (Arg.getOpcode() == ISD::UNDEF) continue;
1889 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1890 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1891 return false;
1892 }
1893
1894 // Upper quadword shuffled.
1895 for (unsigned i = 4; i != 8; ++i) {
1896 SDOperand Arg = N->getOperand(i);
1897 if (Arg.getOpcode() == ISD::UNDEF) continue;
1898 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1899 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1900 if (Val < 4 || Val > 7)
1901 return false;
1902 }
1903
1904 return true;
1905}
1906
1907/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1908/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1909bool X86::isPSHUFLWMask(SDNode *N) {
1910 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1911
1912 if (N->getNumOperands() != 8)
1913 return false;
1914
1915 // Upper quadword copied in order.
1916 for (unsigned i = 4; i != 8; ++i)
1917 if (!isUndefOrEqual(N->getOperand(i), i))
1918 return false;
1919
1920 // Lower quadword shuffled.
1921 for (unsigned i = 0; i != 4; ++i)
1922 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
1923 return false;
1924
1925 return true;
1926}
1927
1928/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1929/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1930static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
1931 if (NumElems != 2 && NumElems != 4) return false;
1932
1933 unsigned Half = NumElems / 2;
1934 for (unsigned i = 0; i < Half; ++i)
1935 if (!isUndefOrInRange(Elems[i], 0, NumElems))
1936 return false;
1937 for (unsigned i = Half; i < NumElems; ++i)
1938 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
1939 return false;
1940
1941 return true;
1942}
1943
1944bool X86::isSHUFPMask(SDNode *N) {
1945 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1946 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
1947}
1948
1949/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
1950/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1951/// half elements to come from vector 1 (which would equal the dest.) and
1952/// the upper half to come from vector 2.
1953static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1954 if (NumOps != 2 && NumOps != 4) return false;
1955
1956 unsigned Half = NumOps / 2;
1957 for (unsigned i = 0; i < Half; ++i)
1958 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
1959 return false;
1960 for (unsigned i = Half; i < NumOps; ++i)
1961 if (!isUndefOrInRange(Ops[i], 0, NumOps))
1962 return false;
1963 return true;
1964}
1965
1966static bool isCommutedSHUFP(SDNode *N) {
1967 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1968 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
1969}
1970
1971/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1972/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1973bool X86::isMOVHLPSMask(SDNode *N) {
1974 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1975
1976 if (N->getNumOperands() != 4)
1977 return false;
1978
1979 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1980 return isUndefOrEqual(N->getOperand(0), 6) &&
1981 isUndefOrEqual(N->getOperand(1), 7) &&
1982 isUndefOrEqual(N->getOperand(2), 2) &&
1983 isUndefOrEqual(N->getOperand(3), 3);
1984}
1985
1986/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1987/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1988/// <2, 3, 2, 3>
1989bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1990 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1991
1992 if (N->getNumOperands() != 4)
1993 return false;
1994
1995 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1996 return isUndefOrEqual(N->getOperand(0), 2) &&
1997 isUndefOrEqual(N->getOperand(1), 3) &&
1998 isUndefOrEqual(N->getOperand(2), 2) &&
1999 isUndefOrEqual(N->getOperand(3), 3);
2000}
2001
2002/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2003/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2004bool X86::isMOVLPMask(SDNode *N) {
2005 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2006
2007 unsigned NumElems = N->getNumOperands();
2008 if (NumElems != 2 && NumElems != 4)
2009 return false;
2010
2011 for (unsigned i = 0; i < NumElems/2; ++i)
2012 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2013 return false;
2014
2015 for (unsigned i = NumElems/2; i < NumElems; ++i)
2016 if (!isUndefOrEqual(N->getOperand(i), i))
2017 return false;
2018
2019 return true;
2020}
2021
2022/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2023/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2024/// and MOVLHPS.
2025bool X86::isMOVHPMask(SDNode *N) {
2026 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2027
2028 unsigned NumElems = N->getNumOperands();
2029 if (NumElems != 2 && NumElems != 4)
2030 return false;
2031
2032 for (unsigned i = 0; i < NumElems/2; ++i)
2033 if (!isUndefOrEqual(N->getOperand(i), i))
2034 return false;
2035
2036 for (unsigned i = 0; i < NumElems/2; ++i) {
2037 SDOperand Arg = N->getOperand(i + NumElems/2);
2038 if (!isUndefOrEqual(Arg, i + NumElems))
2039 return false;
2040 }
2041
2042 return true;
2043}
2044
2045/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2046/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2047bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2048 bool V2IsSplat = false) {
2049 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2050 return false;
2051
2052 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2053 SDOperand BitI = Elts[i];
2054 SDOperand BitI1 = Elts[i+1];
2055 if (!isUndefOrEqual(BitI, j))
2056 return false;
2057 if (V2IsSplat) {
2058 if (isUndefOrEqual(BitI1, NumElts))
2059 return false;
2060 } else {
2061 if (!isUndefOrEqual(BitI1, j + NumElts))
2062 return false;
2063 }
2064 }
2065
2066 return true;
2067}
2068
2069bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2070 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2071 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2072}
2073
2074/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2075/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2076bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2077 bool V2IsSplat = false) {
2078 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2079 return false;
2080
2081 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2082 SDOperand BitI = Elts[i];
2083 SDOperand BitI1 = Elts[i+1];
2084 if (!isUndefOrEqual(BitI, j + NumElts/2))
2085 return false;
2086 if (V2IsSplat) {
2087 if (isUndefOrEqual(BitI1, NumElts))
2088 return false;
2089 } else {
2090 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2091 return false;
2092 }
2093 }
2094
2095 return true;
2096}
2097
2098bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2099 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2100 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2101}
2102
2103/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2104/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2105/// <0, 0, 1, 1>
2106bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2107 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2108
2109 unsigned NumElems = N->getNumOperands();
2110 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2111 return false;
2112
2113 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2114 SDOperand BitI = N->getOperand(i);
2115 SDOperand BitI1 = N->getOperand(i+1);
2116
2117 if (!isUndefOrEqual(BitI, j))
2118 return false;
2119 if (!isUndefOrEqual(BitI1, j))
2120 return false;
2121 }
2122
2123 return true;
2124}
2125
2126/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2127/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2128/// <2, 2, 3, 3>
2129bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2130 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2131
2132 unsigned NumElems = N->getNumOperands();
2133 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2134 return false;
2135
2136 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2137 SDOperand BitI = N->getOperand(i);
2138 SDOperand BitI1 = N->getOperand(i + 1);
2139
2140 if (!isUndefOrEqual(BitI, j))
2141 return false;
2142 if (!isUndefOrEqual(BitI1, j))
2143 return false;
2144 }
2145
2146 return true;
2147}
2148
2149/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2150/// specifies a shuffle of elements that is suitable for input to MOVSS,
2151/// MOVSD, and MOVD, i.e. setting the lowest element.
2152static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002153 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 return false;
2155
2156 if (!isUndefOrEqual(Elts[0], NumElts))
2157 return false;
2158
2159 for (unsigned i = 1; i < NumElts; ++i) {
2160 if (!isUndefOrEqual(Elts[i], i))
2161 return false;
2162 }
2163
2164 return true;
2165}
2166
2167bool X86::isMOVLMask(SDNode *N) {
2168 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2169 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2170}
2171
2172/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2173/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2174/// element of vector 2 and the other elements to come from vector 1 in order.
2175static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2176 bool V2IsSplat = false,
2177 bool V2IsUndef = false) {
2178 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2179 return false;
2180
2181 if (!isUndefOrEqual(Ops[0], 0))
2182 return false;
2183
2184 for (unsigned i = 1; i < NumOps; ++i) {
2185 SDOperand Arg = Ops[i];
2186 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2187 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2188 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2189 return false;
2190 }
2191
2192 return true;
2193}
2194
2195static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2196 bool V2IsUndef = false) {
2197 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2198 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2199 V2IsSplat, V2IsUndef);
2200}
2201
2202/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2203/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2204bool X86::isMOVSHDUPMask(SDNode *N) {
2205 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2206
2207 if (N->getNumOperands() != 4)
2208 return false;
2209
2210 // Expect 1, 1, 3, 3
2211 for (unsigned i = 0; i < 2; ++i) {
2212 SDOperand Arg = N->getOperand(i);
2213 if (Arg.getOpcode() == ISD::UNDEF) continue;
2214 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2215 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2216 if (Val != 1) return false;
2217 }
2218
2219 bool HasHi = false;
2220 for (unsigned i = 2; i < 4; ++i) {
2221 SDOperand Arg = N->getOperand(i);
2222 if (Arg.getOpcode() == ISD::UNDEF) continue;
2223 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2224 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2225 if (Val != 3) return false;
2226 HasHi = true;
2227 }
2228
2229 // Don't use movshdup if it can be done with a shufps.
2230 return HasHi;
2231}
2232
2233/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2234/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2235bool X86::isMOVSLDUPMask(SDNode *N) {
2236 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2237
2238 if (N->getNumOperands() != 4)
2239 return false;
2240
2241 // Expect 0, 0, 2, 2
2242 for (unsigned i = 0; i < 2; ++i) {
2243 SDOperand Arg = N->getOperand(i);
2244 if (Arg.getOpcode() == ISD::UNDEF) continue;
2245 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2246 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2247 if (Val != 0) return false;
2248 }
2249
2250 bool HasHi = false;
2251 for (unsigned i = 2; i < 4; ++i) {
2252 SDOperand Arg = N->getOperand(i);
2253 if (Arg.getOpcode() == ISD::UNDEF) continue;
2254 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2255 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2256 if (Val != 2) return false;
2257 HasHi = true;
2258 }
2259
2260 // Don't use movshdup if it can be done with a shufps.
2261 return HasHi;
2262}
2263
2264/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2265/// specifies a identity operation on the LHS or RHS.
2266static bool isIdentityMask(SDNode *N, bool RHS = false) {
2267 unsigned NumElems = N->getNumOperands();
2268 for (unsigned i = 0; i < NumElems; ++i)
2269 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2270 return false;
2271 return true;
2272}
2273
2274/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2275/// a splat of a single element.
2276static bool isSplatMask(SDNode *N) {
2277 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2278
2279 // This is a splat operation if each element of the permute is the same, and
2280 // if the value doesn't reference the second vector.
2281 unsigned NumElems = N->getNumOperands();
2282 SDOperand ElementBase;
2283 unsigned i = 0;
2284 for (; i != NumElems; ++i) {
2285 SDOperand Elt = N->getOperand(i);
2286 if (isa<ConstantSDNode>(Elt)) {
2287 ElementBase = Elt;
2288 break;
2289 }
2290 }
2291
2292 if (!ElementBase.Val)
2293 return false;
2294
2295 for (; i != NumElems; ++i) {
2296 SDOperand Arg = N->getOperand(i);
2297 if (Arg.getOpcode() == ISD::UNDEF) continue;
2298 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2299 if (Arg != ElementBase) return false;
2300 }
2301
2302 // Make sure it is a splat of the first vector operand.
2303 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2304}
2305
2306/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2307/// a splat of a single element and it's a 2 or 4 element mask.
2308bool X86::isSplatMask(SDNode *N) {
2309 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2310
2311 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2312 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2313 return false;
2314 return ::isSplatMask(N);
2315}
2316
2317/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2318/// specifies a splat of zero element.
2319bool X86::isSplatLoMask(SDNode *N) {
2320 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2321
2322 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2323 if (!isUndefOrEqual(N->getOperand(i), 0))
2324 return false;
2325 return true;
2326}
2327
2328/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2329/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2330/// instructions.
2331unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2332 unsigned NumOperands = N->getNumOperands();
2333 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2334 unsigned Mask = 0;
2335 for (unsigned i = 0; i < NumOperands; ++i) {
2336 unsigned Val = 0;
2337 SDOperand Arg = N->getOperand(NumOperands-i-1);
2338 if (Arg.getOpcode() != ISD::UNDEF)
2339 Val = cast<ConstantSDNode>(Arg)->getValue();
2340 if (Val >= NumOperands) Val -= NumOperands;
2341 Mask |= Val;
2342 if (i != NumOperands - 1)
2343 Mask <<= Shift;
2344 }
2345
2346 return Mask;
2347}
2348
2349/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2350/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2351/// instructions.
2352unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2353 unsigned Mask = 0;
2354 // 8 nodes, but we only care about the last 4.
2355 for (unsigned i = 7; i >= 4; --i) {
2356 unsigned Val = 0;
2357 SDOperand Arg = N->getOperand(i);
2358 if (Arg.getOpcode() != ISD::UNDEF)
2359 Val = cast<ConstantSDNode>(Arg)->getValue();
2360 Mask |= (Val - 4);
2361 if (i != 4)
2362 Mask <<= 2;
2363 }
2364
2365 return Mask;
2366}
2367
2368/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2369/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2370/// instructions.
2371unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2372 unsigned Mask = 0;
2373 // 8 nodes, but we only care about the first 4.
2374 for (int i = 3; i >= 0; --i) {
2375 unsigned Val = 0;
2376 SDOperand Arg = N->getOperand(i);
2377 if (Arg.getOpcode() != ISD::UNDEF)
2378 Val = cast<ConstantSDNode>(Arg)->getValue();
2379 Mask |= Val;
2380 if (i != 0)
2381 Mask <<= 2;
2382 }
2383
2384 return Mask;
2385}
2386
2387/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2388/// specifies a 8 element shuffle that can be broken into a pair of
2389/// PSHUFHW and PSHUFLW.
2390static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2391 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2392
2393 if (N->getNumOperands() != 8)
2394 return false;
2395
2396 // Lower quadword shuffled.
2397 for (unsigned i = 0; i != 4; ++i) {
2398 SDOperand Arg = N->getOperand(i);
2399 if (Arg.getOpcode() == ISD::UNDEF) continue;
2400 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2401 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002402 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002403 return false;
2404 }
2405
2406 // Upper quadword shuffled.
2407 for (unsigned i = 4; i != 8; ++i) {
2408 SDOperand Arg = N->getOperand(i);
2409 if (Arg.getOpcode() == ISD::UNDEF) continue;
2410 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2411 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2412 if (Val < 4 || Val > 7)
2413 return false;
2414 }
2415
2416 return true;
2417}
2418
Chris Lattnere6aa3862007-11-25 00:24:49 +00002419/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002420/// values in ther permute mask.
2421static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2422 SDOperand &V2, SDOperand &Mask,
2423 SelectionDAG &DAG) {
2424 MVT::ValueType VT = Op.getValueType();
2425 MVT::ValueType MaskVT = Mask.getValueType();
2426 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2427 unsigned NumElems = Mask.getNumOperands();
2428 SmallVector<SDOperand, 8> MaskVec;
2429
2430 for (unsigned i = 0; i != NumElems; ++i) {
2431 SDOperand Arg = Mask.getOperand(i);
2432 if (Arg.getOpcode() == ISD::UNDEF) {
2433 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2434 continue;
2435 }
2436 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2437 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2438 if (Val < NumElems)
2439 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2440 else
2441 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2442 }
2443
2444 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002445 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2447}
2448
Evan Chenga6769df2007-12-07 21:30:01 +00002449/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2450/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002451static
2452SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2453 MVT::ValueType MaskVT = Mask.getValueType();
2454 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2455 unsigned NumElems = Mask.getNumOperands();
2456 SmallVector<SDOperand, 8> MaskVec;
2457 for (unsigned i = 0; i != NumElems; ++i) {
2458 SDOperand Arg = Mask.getOperand(i);
2459 if (Arg.getOpcode() == ISD::UNDEF) {
2460 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2461 continue;
2462 }
2463 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2464 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2465 if (Val < NumElems)
2466 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2467 else
2468 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2469 }
2470 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2471}
2472
2473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2475/// match movhlps. The lower half elements should come from upper half of
2476/// V1 (and in order), and the upper half elements should come from the upper
2477/// half of V2 (and in order).
2478static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2479 unsigned NumElems = Mask->getNumOperands();
2480 if (NumElems != 4)
2481 return false;
2482 for (unsigned i = 0, e = 2; i != e; ++i)
2483 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2484 return false;
2485 for (unsigned i = 2; i != 4; ++i)
2486 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2487 return false;
2488 return true;
2489}
2490
2491/// isScalarLoadToVector - Returns true if the node is a scalar load that
2492/// is promoted to a vector.
2493static inline bool isScalarLoadToVector(SDNode *N) {
2494 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2495 N = N->getOperand(0).Val;
2496 return ISD::isNON_EXTLoad(N);
2497 }
2498 return false;
2499}
2500
2501/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2502/// match movlp{s|d}. The lower half elements should come from lower half of
2503/// V1 (and in order), and the upper half elements should come from the upper
2504/// half of V2 (and in order). And since V1 will become the source of the
2505/// MOVLP, it must be either a vector load or a scalar load to vector.
2506static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2507 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2508 return false;
2509 // Is V2 is a vector load, don't do this transformation. We will try to use
2510 // load folding shufps op.
2511 if (ISD::isNON_EXTLoad(V2))
2512 return false;
2513
2514 unsigned NumElems = Mask->getNumOperands();
2515 if (NumElems != 2 && NumElems != 4)
2516 return false;
2517 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2518 if (!isUndefOrEqual(Mask->getOperand(i), i))
2519 return false;
2520 for (unsigned i = NumElems/2; i != NumElems; ++i)
2521 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2522 return false;
2523 return true;
2524}
2525
2526/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2527/// all the same.
2528static bool isSplatVector(SDNode *N) {
2529 if (N->getOpcode() != ISD::BUILD_VECTOR)
2530 return false;
2531
2532 SDOperand SplatValue = N->getOperand(0);
2533 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2534 if (N->getOperand(i) != SplatValue)
2535 return false;
2536 return true;
2537}
2538
2539/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2540/// to an undef.
2541static bool isUndefShuffle(SDNode *N) {
2542 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2543 return false;
2544
2545 SDOperand V1 = N->getOperand(0);
2546 SDOperand V2 = N->getOperand(1);
2547 SDOperand Mask = N->getOperand(2);
2548 unsigned NumElems = Mask.getNumOperands();
2549 for (unsigned i = 0; i != NumElems; ++i) {
2550 SDOperand Arg = Mask.getOperand(i);
2551 if (Arg.getOpcode() != ISD::UNDEF) {
2552 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2553 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2554 return false;
2555 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2556 return false;
2557 }
2558 }
2559 return true;
2560}
2561
2562/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2563/// constant +0.0.
2564static inline bool isZeroNode(SDOperand Elt) {
2565 return ((isa<ConstantSDNode>(Elt) &&
2566 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2567 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002568 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002569}
2570
2571/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2572/// to an zero vector.
2573static bool isZeroShuffle(SDNode *N) {
2574 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2575 return false;
2576
2577 SDOperand V1 = N->getOperand(0);
2578 SDOperand V2 = N->getOperand(1);
2579 SDOperand Mask = N->getOperand(2);
2580 unsigned NumElems = Mask.getNumOperands();
2581 for (unsigned i = 0; i != NumElems; ++i) {
2582 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002583 if (Arg.getOpcode() == ISD::UNDEF)
2584 continue;
2585
2586 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2587 if (Idx < NumElems) {
2588 unsigned Opc = V1.Val->getOpcode();
2589 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2590 continue;
2591 if (Opc != ISD::BUILD_VECTOR ||
2592 !isZeroNode(V1.Val->getOperand(Idx)))
2593 return false;
2594 } else if (Idx >= NumElems) {
2595 unsigned Opc = V2.Val->getOpcode();
2596 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2597 continue;
2598 if (Opc != ISD::BUILD_VECTOR ||
2599 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2600 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 }
2602 }
2603 return true;
2604}
2605
2606/// getZeroVector - Returns a vector of specified type with all zero elements.
2607///
2608static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2609 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002610
2611 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2612 // type. This ensures they get CSE'd.
2613 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2614 SDOperand Vec;
2615 if (MVT::getSizeInBits(VT) == 64) // MMX
2616 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2617 else // SSE
2618 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2619 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620}
2621
Chris Lattnere6aa3862007-11-25 00:24:49 +00002622/// getOnesVector - Returns a vector of specified type with all bits set.
2623///
2624static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2625 assert(MVT::isVector(VT) && "Expected a vector type");
2626
2627 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2628 // type. This ensures they get CSE'd.
2629 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2630 SDOperand Vec;
2631 if (MVT::getSizeInBits(VT) == 64) // MMX
2632 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2633 else // SSE
2634 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2635 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2636}
2637
2638
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2640/// that point to V2 points to its first element.
2641static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2642 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2643
2644 bool Changed = false;
2645 SmallVector<SDOperand, 8> MaskVec;
2646 unsigned NumElems = Mask.getNumOperands();
2647 for (unsigned i = 0; i != NumElems; ++i) {
2648 SDOperand Arg = Mask.getOperand(i);
2649 if (Arg.getOpcode() != ISD::UNDEF) {
2650 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2651 if (Val > NumElems) {
2652 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2653 Changed = true;
2654 }
2655 }
2656 MaskVec.push_back(Arg);
2657 }
2658
2659 if (Changed)
2660 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2661 &MaskVec[0], MaskVec.size());
2662 return Mask;
2663}
2664
2665/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2666/// operation of specified width.
2667static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2668 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2669 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2670
2671 SmallVector<SDOperand, 8> MaskVec;
2672 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2673 for (unsigned i = 1; i != NumElems; ++i)
2674 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2675 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2676}
2677
2678/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2679/// of specified width.
2680static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2681 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2682 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2683 SmallVector<SDOperand, 8> MaskVec;
2684 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2685 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2686 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2687 }
2688 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2689}
2690
2691/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2692/// of specified width.
2693static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2694 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2695 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2696 unsigned Half = NumElems/2;
2697 SmallVector<SDOperand, 8> MaskVec;
2698 for (unsigned i = 0; i != Half; ++i) {
2699 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2700 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2701 }
2702 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2703}
2704
2705/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2706///
2707static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2708 SDOperand V1 = Op.getOperand(0);
2709 SDOperand Mask = Op.getOperand(2);
2710 MVT::ValueType VT = Op.getValueType();
2711 unsigned NumElems = Mask.getNumOperands();
2712 Mask = getUnpacklMask(NumElems, DAG);
2713 while (NumElems != 4) {
2714 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2715 NumElems >>= 1;
2716 }
2717 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2718
Chris Lattnere6aa3862007-11-25 00:24:49 +00002719 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2721 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2722 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2723}
2724
2725/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002726/// vector of zero or undef vector. This produces a shuffle where the low
2727/// element of V2 is swizzled into the zero/undef vector, landing at element
2728/// 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 +00002729static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2730 unsigned NumElems, unsigned Idx,
2731 bool isZero, SelectionDAG &DAG) {
2732 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2733 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2734 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002735 SmallVector<SDOperand, 16> MaskVec;
2736 for (unsigned i = 0; i != NumElems; ++i)
2737 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2738 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2739 else
2740 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2742 &MaskVec[0], MaskVec.size());
2743 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2744}
2745
2746/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2747///
2748static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2749 unsigned NumNonZero, unsigned NumZero,
2750 SelectionDAG &DAG, TargetLowering &TLI) {
2751 if (NumNonZero > 8)
2752 return SDOperand();
2753
2754 SDOperand V(0, 0);
2755 bool First = true;
2756 for (unsigned i = 0; i < 16; ++i) {
2757 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2758 if (ThisIsNonZero && First) {
2759 if (NumZero)
2760 V = getZeroVector(MVT::v8i16, DAG);
2761 else
2762 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2763 First = false;
2764 }
2765
2766 if ((i & 1) != 0) {
2767 SDOperand ThisElt(0, 0), LastElt(0, 0);
2768 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2769 if (LastIsNonZero) {
2770 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2771 }
2772 if (ThisIsNonZero) {
2773 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2774 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2775 ThisElt, DAG.getConstant(8, MVT::i8));
2776 if (LastIsNonZero)
2777 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2778 } else
2779 ThisElt = LastElt;
2780
2781 if (ThisElt.Val)
2782 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002783 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784 }
2785 }
2786
2787 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2788}
2789
2790/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2791///
2792static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2793 unsigned NumNonZero, unsigned NumZero,
2794 SelectionDAG &DAG, TargetLowering &TLI) {
2795 if (NumNonZero > 4)
2796 return SDOperand();
2797
2798 SDOperand V(0, 0);
2799 bool First = true;
2800 for (unsigned i = 0; i < 8; ++i) {
2801 bool isNonZero = (NonZeros & (1 << i)) != 0;
2802 if (isNonZero) {
2803 if (First) {
2804 if (NumZero)
2805 V = getZeroVector(MVT::v8i16, DAG);
2806 else
2807 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2808 First = false;
2809 }
2810 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002811 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812 }
2813 }
2814
2815 return V;
2816}
2817
2818SDOperand
2819X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002820 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2821 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2822 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2823 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2824 // eliminated on x86-32 hosts.
2825 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2826 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002827
Chris Lattnere6aa3862007-11-25 00:24:49 +00002828 if (ISD::isBuildVectorAllOnes(Op.Val))
2829 return getOnesVector(Op.getValueType(), DAG);
2830 return getZeroVector(Op.getValueType(), DAG);
2831 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002832
2833 MVT::ValueType VT = Op.getValueType();
2834 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2835 unsigned EVTBits = MVT::getSizeInBits(EVT);
2836
2837 unsigned NumElems = Op.getNumOperands();
2838 unsigned NumZero = 0;
2839 unsigned NumNonZero = 0;
2840 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00002841 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00002842 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 for (unsigned i = 0; i < NumElems; ++i) {
2844 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00002845 if (Elt.getOpcode() == ISD::UNDEF)
2846 continue;
2847 Values.insert(Elt);
2848 if (Elt.getOpcode() != ISD::Constant &&
2849 Elt.getOpcode() != ISD::ConstantFP)
2850 HasNonImms = true;
2851 if (isZeroNode(Elt))
2852 NumZero++;
2853 else {
2854 NonZeros |= (1 << i);
2855 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856 }
2857 }
2858
2859 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002860 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2861 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862 }
2863
2864 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2865 if (Values.size() == 1)
2866 return SDOperand();
2867
2868 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00002869 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 unsigned Idx = CountTrailingZeros_32(NonZeros);
2871 SDOperand Item = Op.getOperand(Idx);
2872 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2873 if (Idx == 0)
2874 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2875 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2876 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00002877 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
2878 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002879
2880 if (EVTBits == 32) {
2881 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2882 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2883 DAG);
2884 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2885 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2886 SmallVector<SDOperand, 8> MaskVec;
2887 for (unsigned i = 0; i < NumElems; i++)
2888 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2889 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2890 &MaskVec[0], MaskVec.size());
2891 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2892 DAG.getNode(ISD::UNDEF, VT), Mask);
2893 }
2894 }
2895
Dan Gohman21463242007-07-24 22:55:08 +00002896 // A vector full of immediates; various special cases are already
2897 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00002898 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00002899 return SDOperand();
2900
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 // Let legalizer expand 2-wide build_vectors.
2902 if (EVTBits == 64)
2903 return SDOperand();
2904
2905 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2906 if (EVTBits == 8 && NumElems == 16) {
2907 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2908 *this);
2909 if (V.Val) return V;
2910 }
2911
2912 if (EVTBits == 16 && NumElems == 8) {
2913 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2914 *this);
2915 if (V.Val) return V;
2916 }
2917
2918 // If element VT is == 32 bits, turn it into a number of shuffles.
2919 SmallVector<SDOperand, 8> V;
2920 V.resize(NumElems);
2921 if (NumElems == 4 && NumZero > 0) {
2922 for (unsigned i = 0; i < 4; ++i) {
2923 bool isZero = !(NonZeros & (1 << i));
2924 if (isZero)
2925 V[i] = getZeroVector(VT, DAG);
2926 else
2927 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2928 }
2929
2930 for (unsigned i = 0; i < 2; ++i) {
2931 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2932 default: break;
2933 case 0:
2934 V[i] = V[i*2]; // Must be a zero vector.
2935 break;
2936 case 1:
2937 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2938 getMOVLMask(NumElems, DAG));
2939 break;
2940 case 2:
2941 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2942 getMOVLMask(NumElems, DAG));
2943 break;
2944 case 3:
2945 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2946 getUnpacklMask(NumElems, DAG));
2947 break;
2948 }
2949 }
2950
2951 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
2952 // clears the upper bits.
2953 // FIXME: we can do the same for v4f32 case when we know both parts of
2954 // the lower half come from scalar_to_vector (loadf32). We should do
2955 // that in post legalizer dag combiner with target specific hooks.
2956 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2957 return V[0];
2958 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2959 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2960 SmallVector<SDOperand, 8> MaskVec;
2961 bool Reverse = (NonZeros & 0x3) == 2;
2962 for (unsigned i = 0; i < 2; ++i)
2963 if (Reverse)
2964 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2965 else
2966 MaskVec.push_back(DAG.getConstant(i, EVT));
2967 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2968 for (unsigned i = 0; i < 2; ++i)
2969 if (Reverse)
2970 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2971 else
2972 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2973 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2974 &MaskVec[0], MaskVec.size());
2975 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2976 }
2977
2978 if (Values.size() > 2) {
2979 // Expand into a number of unpckl*.
2980 // e.g. for v4f32
2981 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2982 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2983 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2984 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2985 for (unsigned i = 0; i < NumElems; ++i)
2986 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2987 NumElems >>= 1;
2988 while (NumElems != 0) {
2989 for (unsigned i = 0; i < NumElems; ++i)
2990 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2991 UnpckMask);
2992 NumElems >>= 1;
2993 }
2994 return V[0];
2995 }
2996
2997 return SDOperand();
2998}
2999
Evan Chengfca29242007-12-07 08:07:39 +00003000static
3001SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3002 SDOperand PermMask, SelectionDAG &DAG,
3003 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003004 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003005 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3006 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003007 MVT::ValueType PtrVT = TLI.getPointerTy();
3008 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3009 PermMask.Val->op_end());
3010
3011 // First record which half of which vector the low elements come from.
3012 SmallVector<unsigned, 4> LowQuad(4);
3013 for (unsigned i = 0; i < 4; ++i) {
3014 SDOperand Elt = MaskElts[i];
3015 if (Elt.getOpcode() == ISD::UNDEF)
3016 continue;
3017 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3018 int QuadIdx = EltIdx / 4;
3019 ++LowQuad[QuadIdx];
3020 }
3021 int BestLowQuad = -1;
3022 unsigned MaxQuad = 1;
3023 for (unsigned i = 0; i < 4; ++i) {
3024 if (LowQuad[i] > MaxQuad) {
3025 BestLowQuad = i;
3026 MaxQuad = LowQuad[i];
3027 }
Evan Chengfca29242007-12-07 08:07:39 +00003028 }
3029
Evan Cheng75184a92007-12-11 01:46:18 +00003030 // Record which half of which vector the high elements come from.
3031 SmallVector<unsigned, 4> HighQuad(4);
3032 for (unsigned i = 4; i < 8; ++i) {
3033 SDOperand Elt = MaskElts[i];
3034 if (Elt.getOpcode() == ISD::UNDEF)
3035 continue;
3036 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3037 int QuadIdx = EltIdx / 4;
3038 ++HighQuad[QuadIdx];
3039 }
3040 int BestHighQuad = -1;
3041 MaxQuad = 1;
3042 for (unsigned i = 0; i < 4; ++i) {
3043 if (HighQuad[i] > MaxQuad) {
3044 BestHighQuad = i;
3045 MaxQuad = HighQuad[i];
3046 }
3047 }
3048
3049 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3050 if (BestLowQuad != -1 || BestHighQuad != -1) {
3051 // First sort the 4 chunks in order using shufpd.
3052 SmallVector<SDOperand, 8> MaskVec;
3053 if (BestLowQuad != -1)
3054 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3055 else
3056 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3057 if (BestHighQuad != -1)
3058 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3059 else
3060 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3061 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3062 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3063 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3064 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3065 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3066
3067 // Now sort high and low parts separately.
3068 BitVector InOrder(8);
3069 if (BestLowQuad != -1) {
3070 // Sort lower half in order using PSHUFLW.
3071 MaskVec.clear();
3072 bool AnyOutOrder = false;
3073 for (unsigned i = 0; i != 4; ++i) {
3074 SDOperand Elt = MaskElts[i];
3075 if (Elt.getOpcode() == ISD::UNDEF) {
3076 MaskVec.push_back(Elt);
3077 InOrder.set(i);
3078 } else {
3079 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3080 if (EltIdx != i)
3081 AnyOutOrder = true;
3082 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3083 // If this element is in the right place after this shuffle, then
3084 // remember it.
3085 if ((int)(EltIdx / 4) == BestLowQuad)
3086 InOrder.set(i);
3087 }
3088 }
3089 if (AnyOutOrder) {
3090 for (unsigned i = 4; i != 8; ++i)
3091 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3092 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3093 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3094 }
3095 }
3096
3097 if (BestHighQuad != -1) {
3098 // Sort high half in order using PSHUFHW if possible.
3099 MaskVec.clear();
3100 for (unsigned i = 0; i != 4; ++i)
3101 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3102 bool AnyOutOrder = false;
3103 for (unsigned i = 4; i != 8; ++i) {
3104 SDOperand Elt = MaskElts[i];
3105 if (Elt.getOpcode() == ISD::UNDEF) {
3106 MaskVec.push_back(Elt);
3107 InOrder.set(i);
3108 } else {
3109 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3110 if (EltIdx != i)
3111 AnyOutOrder = true;
3112 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3113 // If this element is in the right place after this shuffle, then
3114 // remember it.
3115 if ((int)(EltIdx / 4) == BestHighQuad)
3116 InOrder.set(i);
3117 }
3118 }
3119 if (AnyOutOrder) {
3120 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3121 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3122 }
3123 }
3124
3125 // The other elements are put in the right place using pextrw and pinsrw.
3126 for (unsigned i = 0; i != 8; ++i) {
3127 if (InOrder[i])
3128 continue;
3129 SDOperand Elt = MaskElts[i];
3130 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3131 if (EltIdx == i)
3132 continue;
3133 SDOperand ExtOp = (EltIdx < 8)
3134 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3135 DAG.getConstant(EltIdx, PtrVT))
3136 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3137 DAG.getConstant(EltIdx - 8, PtrVT));
3138 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3139 DAG.getConstant(i, PtrVT));
3140 }
3141 return NewV;
3142 }
3143
3144 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3145 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003146 // First, let's find out how many elements are already in the right order.
3147 unsigned V1InOrder = 0;
3148 unsigned V1FromV1 = 0;
3149 unsigned V2InOrder = 0;
3150 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003151 SmallVector<SDOperand, 8> V1Elts;
3152 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003153 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003154 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003155 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003156 V1Elts.push_back(Elt);
3157 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003158 ++V1InOrder;
3159 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003160 continue;
3161 }
3162 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3163 if (EltIdx == i) {
3164 V1Elts.push_back(Elt);
3165 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3166 ++V1InOrder;
3167 } else if (EltIdx == i+8) {
3168 V1Elts.push_back(Elt);
3169 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3170 ++V2InOrder;
3171 } else if (EltIdx < 8) {
3172 V1Elts.push_back(Elt);
3173 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003174 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003175 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3176 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003177 }
3178 }
3179
3180 if (V2InOrder > V1InOrder) {
3181 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3182 std::swap(V1, V2);
3183 std::swap(V1Elts, V2Elts);
3184 std::swap(V1FromV1, V2FromV2);
3185 }
3186
Evan Cheng75184a92007-12-11 01:46:18 +00003187 if ((V1FromV1 + V1InOrder) != 8) {
3188 // Some elements are from V2.
3189 if (V1FromV1) {
3190 // If there are elements that are from V1 but out of place,
3191 // then first sort them in place
3192 SmallVector<SDOperand, 8> MaskVec;
3193 for (unsigned i = 0; i < 8; ++i) {
3194 SDOperand Elt = V1Elts[i];
3195 if (Elt.getOpcode() == ISD::UNDEF) {
3196 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3197 continue;
3198 }
3199 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3200 if (EltIdx >= 8)
3201 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3202 else
3203 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3204 }
3205 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3206 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003207 }
Evan Cheng75184a92007-12-11 01:46:18 +00003208
3209 NewV = V1;
3210 for (unsigned i = 0; i < 8; ++i) {
3211 SDOperand Elt = V1Elts[i];
3212 if (Elt.getOpcode() == ISD::UNDEF)
3213 continue;
3214 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3215 if (EltIdx < 8)
3216 continue;
3217 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3218 DAG.getConstant(EltIdx - 8, PtrVT));
3219 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3220 DAG.getConstant(i, PtrVT));
3221 }
3222 return NewV;
3223 } else {
3224 // All elements are from V1.
3225 NewV = V1;
3226 for (unsigned i = 0; i < 8; ++i) {
3227 SDOperand Elt = V1Elts[i];
3228 if (Elt.getOpcode() == ISD::UNDEF)
3229 continue;
3230 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3231 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3232 DAG.getConstant(EltIdx, PtrVT));
3233 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3234 DAG.getConstant(i, PtrVT));
3235 }
3236 return NewV;
3237 }
3238}
3239
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003240/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3241/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3242/// done when every pair / quad of shuffle mask elements point to elements in
3243/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003244/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3245static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003246SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3247 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003248 SDOperand PermMask, SelectionDAG &DAG,
3249 TargetLowering &TLI) {
3250 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003251 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3252 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3253 MVT::ValueType NewVT = MaskVT;
3254 switch (VT) {
3255 case MVT::v4f32: NewVT = MVT::v2f64; break;
3256 case MVT::v4i32: NewVT = MVT::v2i64; break;
3257 case MVT::v8i16: NewVT = MVT::v4i32; break;
3258 case MVT::v16i8: NewVT = MVT::v4i32; break;
3259 default: assert(false && "Unexpected!");
3260 }
3261
3262 if (NewWidth == 2)
3263 if (MVT::isInteger(VT))
3264 NewVT = MVT::v2i64;
3265 else
3266 NewVT = MVT::v2f64;
3267 unsigned Scale = NumElems / NewWidth;
3268 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003269 for (unsigned i = 0; i < NumElems; i += Scale) {
3270 unsigned StartIdx = ~0U;
3271 for (unsigned j = 0; j < Scale; ++j) {
3272 SDOperand Elt = PermMask.getOperand(i+j);
3273 if (Elt.getOpcode() == ISD::UNDEF)
3274 continue;
3275 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3276 if (StartIdx == ~0U)
3277 StartIdx = EltIdx - (EltIdx % Scale);
3278 if (EltIdx != StartIdx + j)
3279 return SDOperand();
3280 }
3281 if (StartIdx == ~0U)
3282 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3283 else
3284 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003285 }
3286
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003287 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3288 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3289 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3290 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3291 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003292}
3293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294SDOperand
3295X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3296 SDOperand V1 = Op.getOperand(0);
3297 SDOperand V2 = Op.getOperand(1);
3298 SDOperand PermMask = Op.getOperand(2);
3299 MVT::ValueType VT = Op.getValueType();
3300 unsigned NumElems = PermMask.getNumOperands();
3301 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3302 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3303 bool V1IsSplat = false;
3304 bool V2IsSplat = false;
3305
3306 if (isUndefShuffle(Op.Val))
3307 return DAG.getNode(ISD::UNDEF, VT);
3308
3309 if (isZeroShuffle(Op.Val))
3310 return getZeroVector(VT, DAG);
3311
3312 if (isIdentityMask(PermMask.Val))
3313 return V1;
3314 else if (isIdentityMask(PermMask.Val, true))
3315 return V2;
3316
3317 if (isSplatMask(PermMask.Val)) {
3318 if (NumElems <= 4) return Op;
3319 // Promote it to a v4i32 splat.
3320 return PromoteSplat(Op, DAG);
3321 }
3322
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003323 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3324 // do it!
3325 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3326 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3327 if (NewOp.Val)
3328 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3329 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3330 // FIXME: Figure out a cleaner way to do this.
3331 // Try to make use of movq to zero out the top part.
3332 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3333 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3334 if (NewOp.Val) {
3335 SDOperand NewV1 = NewOp.getOperand(0);
3336 SDOperand NewV2 = NewOp.getOperand(1);
3337 SDOperand NewMask = NewOp.getOperand(2);
3338 if (isCommutedMOVL(NewMask.Val, true, false)) {
3339 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3340 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3341 NewV1, NewV2, getMOVLMask(2, DAG));
3342 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3343 }
3344 }
3345 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3346 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3347 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3348 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3349 }
3350 }
3351
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003352 if (X86::isMOVLMask(PermMask.Val))
3353 return (V1IsUndef) ? V2 : Op;
3354
3355 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3356 X86::isMOVSLDUPMask(PermMask.Val) ||
3357 X86::isMOVHLPSMask(PermMask.Val) ||
3358 X86::isMOVHPMask(PermMask.Val) ||
3359 X86::isMOVLPMask(PermMask.Val))
3360 return Op;
3361
3362 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3363 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3364 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3365
3366 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003367 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3368 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369 V1IsSplat = isSplatVector(V1.Val);
3370 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003371
3372 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003373 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3374 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3375 std::swap(V1IsSplat, V2IsSplat);
3376 std::swap(V1IsUndef, V2IsUndef);
3377 Commuted = true;
3378 }
3379
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003380 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3382 if (V2IsUndef) return V1;
3383 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3384 if (V2IsSplat) {
3385 // V2 is a splat, so the mask may be malformed. That is, it may point
3386 // to any V2 element. The instruction selectior won't like this. Get
3387 // a corrected mask and commute to form a proper MOVS{S|D}.
3388 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3389 if (NewMask.Val != PermMask.Val)
3390 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3391 }
3392 return Op;
3393 }
3394
3395 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3396 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3397 X86::isUNPCKLMask(PermMask.Val) ||
3398 X86::isUNPCKHMask(PermMask.Val))
3399 return Op;
3400
3401 if (V2IsSplat) {
3402 // Normalize mask so all entries that point to V2 points to its first
3403 // element then try to match unpck{h|l} again. If match, return a
3404 // new vector_shuffle with the corrected mask.
3405 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3406 if (NewMask.Val != PermMask.Val) {
3407 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3408 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3409 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3410 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3411 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3412 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3413 }
3414 }
3415 }
3416
3417 // Normalize the node to match x86 shuffle ops if needed
3418 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3419 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3420
3421 if (Commuted) {
3422 // Commute is back and try unpck* again.
3423 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3424 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3425 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3426 X86::isUNPCKLMask(PermMask.Val) ||
3427 X86::isUNPCKHMask(PermMask.Val))
3428 return Op;
3429 }
3430
3431 // If VT is integer, try PSHUF* first, then SHUFP*.
3432 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003433 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3434 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3435 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3436 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 X86::isPSHUFHWMask(PermMask.Val) ||
3438 X86::isPSHUFLWMask(PermMask.Val)) {
3439 if (V2.getOpcode() != ISD::UNDEF)
3440 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3441 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3442 return Op;
3443 }
3444
3445 if (X86::isSHUFPMask(PermMask.Val) &&
3446 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3447 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003448 } else {
3449 // Floating point cases in the other order.
3450 if (X86::isSHUFPMask(PermMask.Val))
3451 return Op;
3452 if (X86::isPSHUFDMask(PermMask.Val) ||
3453 X86::isPSHUFHWMask(PermMask.Val) ||
3454 X86::isPSHUFLWMask(PermMask.Val)) {
3455 if (V2.getOpcode() != ISD::UNDEF)
3456 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3457 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3458 return Op;
3459 }
3460 }
3461
Evan Cheng75184a92007-12-11 01:46:18 +00003462 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3463 if (VT == MVT::v8i16) {
3464 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3465 if (NewOp.Val)
3466 return NewOp;
3467 }
3468
3469 // Handle all 4 wide cases with a number of shuffles.
3470 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003471 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003472 MVT::ValueType MaskVT = PermMask.getValueType();
3473 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3474 SmallVector<std::pair<int, int>, 8> Locs;
3475 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003476 SmallVector<SDOperand, 8> Mask1(NumElems,
3477 DAG.getNode(ISD::UNDEF, MaskEVT));
3478 SmallVector<SDOperand, 8> Mask2(NumElems,
3479 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003480 unsigned NumHi = 0;
3481 unsigned NumLo = 0;
3482 // If no more than two elements come from either vector. This can be
3483 // implemented with two shuffles. First shuffle gather the elements.
3484 // The second shuffle, which takes the first shuffle as both of its
3485 // vector operands, put the elements into the right order.
3486 for (unsigned i = 0; i != NumElems; ++i) {
3487 SDOperand Elt = PermMask.getOperand(i);
3488 if (Elt.getOpcode() == ISD::UNDEF) {
3489 Locs[i] = std::make_pair(-1, -1);
3490 } else {
3491 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3492 if (Val < NumElems) {
3493 Locs[i] = std::make_pair(0, NumLo);
3494 Mask1[NumLo] = Elt;
3495 NumLo++;
3496 } else {
3497 Locs[i] = std::make_pair(1, NumHi);
3498 if (2+NumHi < NumElems)
3499 Mask1[2+NumHi] = Elt;
3500 NumHi++;
3501 }
3502 }
3503 }
3504 if (NumLo <= 2 && NumHi <= 2) {
3505 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3506 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3507 &Mask1[0], Mask1.size()));
3508 for (unsigned i = 0; i != NumElems; ++i) {
3509 if (Locs[i].first == -1)
3510 continue;
3511 else {
3512 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3513 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3514 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3515 }
3516 }
3517
3518 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3519 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3520 &Mask2[0], Mask2.size()));
3521 }
3522
3523 // Break it into (shuffle shuffle_hi, shuffle_lo).
3524 Locs.clear();
3525 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3526 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3527 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3528 unsigned MaskIdx = 0;
3529 unsigned LoIdx = 0;
3530 unsigned HiIdx = NumElems/2;
3531 for (unsigned i = 0; i != NumElems; ++i) {
3532 if (i == NumElems/2) {
3533 MaskPtr = &HiMask;
3534 MaskIdx = 1;
3535 LoIdx = 0;
3536 HiIdx = NumElems/2;
3537 }
3538 SDOperand Elt = PermMask.getOperand(i);
3539 if (Elt.getOpcode() == ISD::UNDEF) {
3540 Locs[i] = std::make_pair(-1, -1);
3541 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3542 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3543 (*MaskPtr)[LoIdx] = Elt;
3544 LoIdx++;
3545 } else {
3546 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3547 (*MaskPtr)[HiIdx] = Elt;
3548 HiIdx++;
3549 }
3550 }
3551
3552 SDOperand LoShuffle =
3553 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3554 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3555 &LoMask[0], LoMask.size()));
3556 SDOperand HiShuffle =
3557 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3558 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3559 &HiMask[0], HiMask.size()));
3560 SmallVector<SDOperand, 8> MaskOps;
3561 for (unsigned i = 0; i != NumElems; ++i) {
3562 if (Locs[i].first == -1) {
3563 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3564 } else {
3565 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3566 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3567 }
3568 }
3569 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3570 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3571 &MaskOps[0], MaskOps.size()));
3572 }
3573
3574 return SDOperand();
3575}
3576
3577SDOperand
3578X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3579 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3580 return SDOperand();
3581
3582 MVT::ValueType VT = Op.getValueType();
3583 // TODO: handle v16i8.
3584 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003585 SDOperand Vec = Op.getOperand(0);
3586 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3587 if (Idx == 0)
3588 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3589 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3590 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3591 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003592 // Transform it so it match pextrw which produces a 32-bit result.
3593 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3594 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3595 Op.getOperand(0), Op.getOperand(1));
3596 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3597 DAG.getValueType(VT));
3598 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3599 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003600 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3601 if (Idx == 0)
3602 return Op;
3603 // SHUFPS the element to the lowest double word, then movss.
3604 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3605 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003606 IdxVec.
3607 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3608 IdxVec.
3609 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3610 IdxVec.
3611 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3612 IdxVec.
3613 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003614 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3615 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003616 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003617 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3618 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3619 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003620 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003621 } else if (MVT::getSizeInBits(VT) == 64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003622 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3623 if (Idx == 0)
3624 return Op;
3625
3626 // UNPCKHPD the element to the lowest double word, then movsd.
3627 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3628 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3629 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3630 SmallVector<SDOperand, 8> IdxVec;
3631 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003632 IdxVec.
3633 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003634 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3635 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003636 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003637 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3638 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3639 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003640 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003641 }
3642
3643 return SDOperand();
3644}
3645
3646SDOperand
3647X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003648 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003649 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3650 if (EVT == MVT::i8)
3651 return SDOperand();
3652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003653 SDOperand N0 = Op.getOperand(0);
3654 SDOperand N1 = Op.getOperand(1);
3655 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003656
3657 if (MVT::getSizeInBits(EVT) == 16) {
3658 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3659 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003660 if (N1.getValueType() != MVT::i32)
3661 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3662 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003663 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003664 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003665 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003666 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003667}
3668
3669SDOperand
3670X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3671 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3672 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3673}
3674
3675// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3676// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3677// one of the above mentioned nodes. It has to be wrapped because otherwise
3678// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3679// be used to form addressing mode. These wrapped nodes will be selected
3680// into MOV32ri.
3681SDOperand
3682X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3683 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3684 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3685 getPointerTy(),
3686 CP->getAlignment());
3687 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3688 // With PIC, the address is actually $g + Offset.
3689 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3690 !Subtarget->isPICStyleRIPRel()) {
3691 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3692 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3693 Result);
3694 }
3695
3696 return Result;
3697}
3698
3699SDOperand
3700X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3701 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3702 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3703 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3704 // With PIC, the address is actually $g + Offset.
3705 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3706 !Subtarget->isPICStyleRIPRel()) {
3707 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3708 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3709 Result);
3710 }
3711
3712 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3713 // load the value at address GV, not the value of GV itself. This means that
3714 // the GlobalAddress must be in the base or index register of the address, not
3715 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3716 // The same applies for external symbols during PIC codegen
3717 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3718 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
3719
3720 return Result;
3721}
3722
3723// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3724static SDOperand
3725LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3726 const MVT::ValueType PtrVT) {
3727 SDOperand InFlag;
3728 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3729 DAG.getNode(X86ISD::GlobalBaseReg,
3730 PtrVT), InFlag);
3731 InFlag = Chain.getValue(1);
3732
3733 // emit leal symbol@TLSGD(,%ebx,1), %eax
3734 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3735 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3736 GA->getValueType(0),
3737 GA->getOffset());
3738 SDOperand Ops[] = { Chain, TGA, InFlag };
3739 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3740 InFlag = Result.getValue(2);
3741 Chain = Result.getValue(1);
3742
3743 // call ___tls_get_addr. This function receives its argument in
3744 // the register EAX.
3745 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3746 InFlag = Chain.getValue(1);
3747
3748 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3749 SDOperand Ops1[] = { Chain,
3750 DAG.getTargetExternalSymbol("___tls_get_addr",
3751 PtrVT),
3752 DAG.getRegister(X86::EAX, PtrVT),
3753 DAG.getRegister(X86::EBX, PtrVT),
3754 InFlag };
3755 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3756 InFlag = Chain.getValue(1);
3757
3758 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3759}
3760
3761// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3762// "local exec" model.
3763static SDOperand
3764LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3765 const MVT::ValueType PtrVT) {
3766 // Get the Thread Pointer
3767 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3768 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3769 // exec)
3770 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3771 GA->getValueType(0),
3772 GA->getOffset());
3773 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3774
3775 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3776 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3777
3778 // The address of the thread local variable is the add of the thread
3779 // pointer with the offset of the variable.
3780 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3781}
3782
3783SDOperand
3784X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3785 // TODO: implement the "local dynamic" model
3786 // TODO: implement the "initial exec"model for pic executables
3787 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3788 "TLS not implemented for non-ELF and 64-bit targets");
3789 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3790 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3791 // otherwise use the "Local Exec"TLS Model
3792 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3793 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3794 else
3795 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3796}
3797
3798SDOperand
3799X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3800 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3801 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3802 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3803 // With PIC, the address is actually $g + Offset.
3804 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3805 !Subtarget->isPICStyleRIPRel()) {
3806 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3807 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3808 Result);
3809 }
3810
3811 return Result;
3812}
3813
3814SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3815 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3816 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3817 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3818 // With PIC, the address is actually $g + Offset.
3819 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3820 !Subtarget->isPICStyleRIPRel()) {
3821 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3822 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3823 Result);
3824 }
3825
3826 return Result;
3827}
3828
Chris Lattner62814a32007-10-17 06:02:13 +00003829/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
3830/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003831SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00003832 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3833 "Not an i64 shift!");
3834 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3835 SDOperand ShOpLo = Op.getOperand(0);
3836 SDOperand ShOpHi = Op.getOperand(1);
3837 SDOperand ShAmt = Op.getOperand(2);
3838 SDOperand Tmp1 = isSRA ?
3839 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3840 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841
Chris Lattner62814a32007-10-17 06:02:13 +00003842 SDOperand Tmp2, Tmp3;
3843 if (Op.getOpcode() == ISD::SHL_PARTS) {
3844 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3845 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3846 } else {
3847 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3848 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3849 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003850
Chris Lattner62814a32007-10-17 06:02:13 +00003851 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3852 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3853 DAG.getConstant(32, MVT::i8));
3854 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3855 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003856
Chris Lattner62814a32007-10-17 06:02:13 +00003857 SDOperand Hi, Lo;
3858 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3859 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3860 SmallVector<SDOperand, 4> Ops;
3861 if (Op.getOpcode() == ISD::SHL_PARTS) {
3862 Ops.push_back(Tmp2);
3863 Ops.push_back(Tmp3);
3864 Ops.push_back(CC);
3865 Ops.push_back(Cond);
3866 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003868 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00003869 Ops.push_back(Tmp3);
3870 Ops.push_back(Tmp1);
3871 Ops.push_back(CC);
3872 Ops.push_back(Cond);
3873 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3874 } else {
3875 Ops.push_back(Tmp2);
3876 Ops.push_back(Tmp3);
3877 Ops.push_back(CC);
3878 Ops.push_back(Cond);
3879 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3880
3881 Ops.clear();
3882 Ops.push_back(Tmp3);
3883 Ops.push_back(Tmp1);
3884 Ops.push_back(CC);
3885 Ops.push_back(Cond);
3886 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3887 }
3888
3889 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3890 Ops.clear();
3891 Ops.push_back(Lo);
3892 Ops.push_back(Hi);
3893 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003894}
3895
3896SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3897 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3898 Op.getOperand(0).getValueType() >= MVT::i16 &&
3899 "Unknown SINT_TO_FP to lower!");
3900
3901 SDOperand Result;
3902 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3903 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3904 MachineFunction &MF = DAG.getMachineFunction();
3905 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3906 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3907 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
3908 StackSlot, NULL, 0);
3909
Dale Johannesen2fc20782007-09-14 22:26:36 +00003910 // These are really Legal; caller falls through into that case.
Chris Lattnercf515b52008-01-16 06:24:21 +00003911 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003912 return Result;
Chris Lattnerfca7f222008-01-16 06:19:45 +00003913 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen958b08b2007-09-19 23:55:34 +00003914 Subtarget->is64Bit())
3915 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00003916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917 // Build the FILD
3918 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00003919 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00003920 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3922 else
3923 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
3924 SmallVector<SDOperand, 8> Ops;
3925 Ops.push_back(Chain);
3926 Ops.push_back(StackSlot);
3927 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00003928 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003929 Tys, &Ops[0], Ops.size());
3930
Dale Johannesen2fc20782007-09-14 22:26:36 +00003931 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932 Chain = Result.getValue(1);
3933 SDOperand InFlag = Result.getValue(2);
3934
3935 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3936 // shouldn't be necessary except that RFP cannot be live across
3937 // multiple blocks. When stackifier is fixed, they can be uncoupled.
3938 MachineFunction &MF = DAG.getMachineFunction();
3939 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3940 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3941 Tys = DAG.getVTList(MVT::Other);
3942 SmallVector<SDOperand, 8> Ops;
3943 Ops.push_back(Chain);
3944 Ops.push_back(Result);
3945 Ops.push_back(StackSlot);
3946 Ops.push_back(DAG.getValueType(Op.getValueType()));
3947 Ops.push_back(InFlag);
3948 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
3949 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
3950 }
3951
3952 return Result;
3953}
3954
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003955std::pair<SDOperand,SDOperand> X86TargetLowering::
3956FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3958 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003959
Dale Johannesen2fc20782007-09-14 22:26:36 +00003960 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00003961 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00003962 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003963 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00003964 if (Subtarget->is64Bit() &&
3965 Op.getValueType() == MVT::i64 &&
3966 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003967 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00003968
Evan Cheng05441e62007-10-15 20:11:21 +00003969 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3970 // stack slot.
3971 MachineFunction &MF = DAG.getMachineFunction();
3972 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3973 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3974 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003975 unsigned Opc;
3976 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00003977 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3978 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3979 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3980 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981 }
3982
3983 SDOperand Chain = DAG.getEntryNode();
3984 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00003985 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3987 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
3988 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
3989 SDOperand Ops[] = {
3990 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3991 };
3992 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
3993 Chain = Value.getValue(1);
3994 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3995 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3996 }
3997
3998 // Build the FP_TO_INT*_IN_MEM
3999 SDOperand Ops[] = { Chain, Value, StackSlot };
4000 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4001
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004002 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003}
4004
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004005SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004006 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4007 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4008 if (FIST.Val == 0) return SDOperand();
4009
4010 // Load the result.
4011 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4012}
4013
4014SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4015 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4016 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4017 if (FIST.Val == 0) return 0;
4018
4019 // Return an i64 load from the stack slot.
4020 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4021
4022 // Use a MERGE_VALUES node to drop the chain result value.
4023 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4024}
4025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4027 MVT::ValueType VT = Op.getValueType();
4028 MVT::ValueType EltVT = VT;
4029 if (MVT::isVector(VT))
4030 EltVT = MVT::getVectorElementType(VT);
4031 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4032 std::vector<Constant*> CV;
4033 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004034 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 CV.push_back(C);
4036 CV.push_back(C);
4037 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004038 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039 CV.push_back(C);
4040 CV.push_back(C);
4041 CV.push_back(C);
4042 CV.push_back(C);
4043 }
Dan Gohman11821702007-07-27 17:16:43 +00004044 Constant *C = ConstantVector::get(CV);
4045 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4046 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4047 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4049}
4050
4051SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4052 MVT::ValueType VT = Op.getValueType();
4053 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004054 unsigned EltNum = 1;
4055 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004056 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004057 EltNum = MVT::getVectorNumElements(VT);
4058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4060 std::vector<Constant*> CV;
4061 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004062 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004063 CV.push_back(C);
4064 CV.push_back(C);
4065 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004066 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004067 CV.push_back(C);
4068 CV.push_back(C);
4069 CV.push_back(C);
4070 CV.push_back(C);
4071 }
Dan Gohman11821702007-07-27 17:16:43 +00004072 Constant *C = ConstantVector::get(CV);
4073 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4074 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4075 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004076 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004077 return DAG.getNode(ISD::BIT_CONVERT, VT,
4078 DAG.getNode(ISD::XOR, MVT::v2i64,
4079 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4080 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4081 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004082 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4083 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004084}
4085
4086SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4087 SDOperand Op0 = Op.getOperand(0);
4088 SDOperand Op1 = Op.getOperand(1);
4089 MVT::ValueType VT = Op.getValueType();
4090 MVT::ValueType SrcVT = Op1.getValueType();
4091 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4092
4093 // If second operand is smaller, extend it first.
4094 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4095 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4096 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004097 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004099 // And if it is bigger, shrink it first.
4100 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004101 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004102 SrcVT = VT;
4103 SrcTy = MVT::getTypeForValueType(SrcVT);
4104 }
4105
4106 // At this point the operands and the result should have the same
4107 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108
4109 // First get the sign bit of second operand.
4110 std::vector<Constant*> CV;
4111 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004112 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4113 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004115 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4116 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4117 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4118 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 }
Dan Gohman11821702007-07-27 17:16:43 +00004120 Constant *C = ConstantVector::get(CV);
4121 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4122 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4123 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4125
4126 // Shift sign bit right or left if the two operands have different types.
4127 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4128 // Op0 is MVT::f32, Op1 is MVT::f64.
4129 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4130 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4131 DAG.getConstant(32, MVT::i32));
4132 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4133 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004134 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135 }
4136
4137 // Clear first operand sign bit.
4138 CV.clear();
4139 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004140 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4141 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004142 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004143 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4144 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4145 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4146 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 }
Dan Gohman11821702007-07-27 17:16:43 +00004148 C = ConstantVector::get(CV);
4149 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4150 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4151 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4153
4154 // Or the value with the sign bit.
4155 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4156}
4157
Evan Cheng621216e2007-09-29 00:00:36 +00004158SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004159 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004160 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004161 SDOperand Op0 = Op.getOperand(0);
4162 SDOperand Op1 = Op.getOperand(1);
4163 SDOperand CC = Op.getOperand(2);
4164 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4165 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4166 unsigned X86CC;
4167
Evan Cheng950aac02007-09-25 01:57:46 +00004168 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004169 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004170 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4171 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004172 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004173 }
Evan Cheng950aac02007-09-25 01:57:46 +00004174
4175 assert(isFP && "Illegal integer SetCC!");
4176
Evan Cheng621216e2007-09-29 00:00:36 +00004177 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004178 switch (SetCCOpcode) {
4179 default: assert(false && "Illegal floating point SetCC!");
4180 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004181 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004182 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004183 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004184 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4185 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4186 }
4187 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004188 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004189 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004190 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004191 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4192 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4193 }
4194 }
4195}
4196
4197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4199 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 SDOperand Cond = Op.getOperand(0);
4201 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202
4203 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004204 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205
Evan Cheng50d37ab2007-10-08 22:16:29 +00004206 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4207 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208 if (Cond.getOpcode() == X86ISD::SETCC) {
4209 CC = Cond.getOperand(0);
4210
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211 SDOperand Cmp = Cond.getOperand(1);
4212 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004213 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004214
Evan Cheng50d37ab2007-10-08 22:16:29 +00004215 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004216 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004217 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004218 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004219
Evan Cheng621216e2007-09-29 00:00:36 +00004220 if ((Opc == X86ISD::CMP ||
4221 Opc == X86ISD::COMI ||
4222 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004223 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004224 addTest = false;
4225 }
4226 }
4227
4228 if (addTest) {
4229 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004230 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004231 }
4232
4233 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4234 MVT::Flag);
4235 SmallVector<SDOperand, 4> Ops;
4236 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4237 // condition is true.
4238 Ops.push_back(Op.getOperand(2));
4239 Ops.push_back(Op.getOperand(1));
4240 Ops.push_back(CC);
4241 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004242 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004243}
4244
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4246 bool addTest = true;
4247 SDOperand Chain = Op.getOperand(0);
4248 SDOperand Cond = Op.getOperand(1);
4249 SDOperand Dest = Op.getOperand(2);
4250 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251
4252 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004253 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254
Evan Cheng50d37ab2007-10-08 22:16:29 +00004255 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4256 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004257 if (Cond.getOpcode() == X86ISD::SETCC) {
4258 CC = Cond.getOperand(0);
4259
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 SDOperand Cmp = Cond.getOperand(1);
4261 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004262 if (Opc == X86ISD::CMP ||
4263 Opc == X86ISD::COMI ||
4264 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004265 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004266 addTest = false;
4267 }
4268 }
4269
4270 if (addTest) {
4271 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004272 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004273 }
Evan Cheng621216e2007-09-29 00:00:36 +00004274 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004275 Chain, Op.getOperand(2), CC, Cond);
4276}
4277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278
4279// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4280// Calls to _alloca is needed to probe the stack when allocating more than 4k
4281// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4282// that the guard pages used by the OS virtual memory manager are allocated in
4283// correct sequence.
4284SDOperand
4285X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4286 SelectionDAG &DAG) {
4287 assert(Subtarget->isTargetCygMing() &&
4288 "This should be used only on Cygwin/Mingw targets");
4289
4290 // Get the inputs.
4291 SDOperand Chain = Op.getOperand(0);
4292 SDOperand Size = Op.getOperand(1);
4293 // FIXME: Ensure alignment here
4294
4295 SDOperand Flag;
4296
4297 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004298 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004299
4300 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4301 Flag = Chain.getValue(1);
4302
4303 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4304 SDOperand Ops[] = { Chain,
4305 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4306 DAG.getRegister(X86::EAX, IntPtr),
4307 Flag };
4308 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4309 Flag = Chain.getValue(1);
4310
4311 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4312
4313 std::vector<MVT::ValueType> Tys;
4314 Tys.push_back(SPTy);
4315 Tys.push_back(MVT::Other);
4316 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4317 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4318}
4319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4321 SDOperand InFlag(0, 0);
4322 SDOperand Chain = Op.getOperand(0);
4323 unsigned Align =
4324 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4325 if (Align == 0) Align = 1;
4326
4327 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004328 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004329 // The libc version is likely to be faster for these cases. It can use the
4330 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004331 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004332 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333 MVT::ValueType IntPtr = getPointerTy();
4334 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4335 TargetLowering::ArgListTy Args;
4336 TargetLowering::ArgListEntry Entry;
4337 Entry.Node = Op.getOperand(1);
4338 Entry.Ty = IntPtrTy;
4339 Args.push_back(Entry);
4340 // Extend the unsigned i8 argument to be an int value for the call.
4341 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4342 Entry.Ty = IntPtrTy;
4343 Args.push_back(Entry);
4344 Entry.Node = Op.getOperand(3);
4345 Args.push_back(Entry);
4346 std::pair<SDOperand,SDOperand> CallResult =
4347 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
4348 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4349 return CallResult.second;
4350 }
4351
4352 MVT::ValueType AVT;
4353 SDOperand Count;
4354 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4355 unsigned BytesLeft = 0;
4356 bool TwoRepStos = false;
4357 if (ValC) {
4358 unsigned ValReg;
4359 uint64_t Val = ValC->getValue() & 255;
4360
4361 // If the value is a constant, then we can potentially use larger sets.
4362 switch (Align & 3) {
4363 case 2: // WORD aligned
4364 AVT = MVT::i16;
4365 ValReg = X86::AX;
4366 Val = (Val << 8) | Val;
4367 break;
4368 case 0: // DWORD aligned
4369 AVT = MVT::i32;
4370 ValReg = X86::EAX;
4371 Val = (Val << 8) | Val;
4372 Val = (Val << 16) | Val;
4373 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4374 AVT = MVT::i64;
4375 ValReg = X86::RAX;
4376 Val = (Val << 32) | Val;
4377 }
4378 break;
4379 default: // Byte aligned
4380 AVT = MVT::i8;
4381 ValReg = X86::AL;
4382 Count = Op.getOperand(3);
4383 break;
4384 }
4385
4386 if (AVT > MVT::i8) {
4387 if (I) {
4388 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004389 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390 BytesLeft = I->getValue() % UBytes;
4391 } else {
4392 assert(AVT >= MVT::i32 &&
4393 "Do not use rep;stos if not at least DWORD aligned");
4394 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4395 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4396 TwoRepStos = true;
4397 }
4398 }
4399
4400 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4401 InFlag);
4402 InFlag = Chain.getValue(1);
4403 } else {
4404 AVT = MVT::i8;
4405 Count = Op.getOperand(3);
4406 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4407 InFlag = Chain.getValue(1);
4408 }
4409
4410 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4411 Count, InFlag);
4412 InFlag = Chain.getValue(1);
4413 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4414 Op.getOperand(1), InFlag);
4415 InFlag = Chain.getValue(1);
4416
4417 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4418 SmallVector<SDOperand, 8> Ops;
4419 Ops.push_back(Chain);
4420 Ops.push_back(DAG.getValueType(AVT));
4421 Ops.push_back(InFlag);
4422 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4423
4424 if (TwoRepStos) {
4425 InFlag = Chain.getValue(1);
4426 Count = Op.getOperand(3);
4427 MVT::ValueType CVT = Count.getValueType();
4428 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4429 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4430 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4431 Left, InFlag);
4432 InFlag = Chain.getValue(1);
4433 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4434 Ops.clear();
4435 Ops.push_back(Chain);
4436 Ops.push_back(DAG.getValueType(MVT::i8));
4437 Ops.push_back(InFlag);
4438 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4439 } else if (BytesLeft) {
4440 // Issue stores for the last 1 - 7 bytes.
4441 SDOperand Value;
4442 unsigned Val = ValC->getValue() & 255;
4443 unsigned Offset = I->getValue() - BytesLeft;
4444 SDOperand DstAddr = Op.getOperand(1);
4445 MVT::ValueType AddrVT = DstAddr.getValueType();
4446 if (BytesLeft >= 4) {
4447 Val = (Val << 8) | Val;
4448 Val = (Val << 16) | Val;
4449 Value = DAG.getConstant(Val, MVT::i32);
4450 Chain = DAG.getStore(Chain, Value,
4451 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4452 DAG.getConstant(Offset, AddrVT)),
4453 NULL, 0);
4454 BytesLeft -= 4;
4455 Offset += 4;
4456 }
4457 if (BytesLeft >= 2) {
4458 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4459 Chain = DAG.getStore(Chain, Value,
4460 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4461 DAG.getConstant(Offset, AddrVT)),
4462 NULL, 0);
4463 BytesLeft -= 2;
4464 Offset += 2;
4465 }
4466 if (BytesLeft == 1) {
4467 Value = DAG.getConstant(Val, MVT::i8);
4468 Chain = DAG.getStore(Chain, Value,
4469 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4470 DAG.getConstant(Offset, AddrVT)),
4471 NULL, 0);
4472 }
4473 }
4474
4475 return Chain;
4476}
4477
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004478SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4479 SDOperand Dest,
4480 SDOperand Source,
4481 unsigned Size,
4482 unsigned Align,
4483 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004486 switch (Align & 3) {
4487 case 2: // WORD aligned
4488 AVT = MVT::i16;
4489 break;
4490 case 0: // DWORD aligned
4491 AVT = MVT::i32;
4492 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4493 AVT = MVT::i64;
4494 break;
4495 default: // Byte aligned
4496 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 break;
4498 }
4499
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004500 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004501 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004502 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503
4504 SDOperand InFlag(0, 0);
4505 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4506 Count, InFlag);
4507 InFlag = Chain.getValue(1);
4508 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004509 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510 InFlag = Chain.getValue(1);
4511 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004512 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513 InFlag = Chain.getValue(1);
4514
4515 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4516 SmallVector<SDOperand, 8> Ops;
4517 Ops.push_back(Chain);
4518 Ops.push_back(DAG.getValueType(AVT));
4519 Ops.push_back(InFlag);
4520 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4521
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004522 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004524 unsigned Offset = Size - BytesLeft;
4525 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004527 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 MVT::ValueType SrcVT = SrcAddr.getValueType();
4529 SDOperand Value;
4530 if (BytesLeft >= 4) {
4531 Value = DAG.getLoad(MVT::i32, Chain,
4532 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4533 DAG.getConstant(Offset, SrcVT)),
4534 NULL, 0);
4535 Chain = Value.getValue(1);
4536 Chain = DAG.getStore(Chain, Value,
4537 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4538 DAG.getConstant(Offset, DstVT)),
4539 NULL, 0);
4540 BytesLeft -= 4;
4541 Offset += 4;
4542 }
4543 if (BytesLeft >= 2) {
4544 Value = DAG.getLoad(MVT::i16, Chain,
4545 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4546 DAG.getConstant(Offset, SrcVT)),
4547 NULL, 0);
4548 Chain = Value.getValue(1);
4549 Chain = DAG.getStore(Chain, Value,
4550 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4551 DAG.getConstant(Offset, DstVT)),
4552 NULL, 0);
4553 BytesLeft -= 2;
4554 Offset += 2;
4555 }
4556
4557 if (BytesLeft == 1) {
4558 Value = DAG.getLoad(MVT::i8, Chain,
4559 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4560 DAG.getConstant(Offset, SrcVT)),
4561 NULL, 0);
4562 Chain = Value.getValue(1);
4563 Chain = DAG.getStore(Chain, Value,
4564 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4565 DAG.getConstant(Offset, DstVT)),
4566 NULL, 0);
4567 }
4568 }
4569
4570 return Chain;
4571}
4572
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004573/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4574SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004576 SDOperand TheChain = N->getOperand(0);
4577 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004579 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4580 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4581 MVT::i64, rax.getValue(2));
4582 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583 DAG.getConstant(32, MVT::i8));
4584 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004585 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586 };
4587
4588 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004589 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004590 }
4591
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004592 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4593 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4594 MVT::i32, eax.getValue(2));
4595 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4596 SDOperand Ops[] = { eax, edx };
4597 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4598
4599 // Use a MERGE_VALUES to return the value and chain.
4600 Ops[1] = edx.getValue(1);
4601 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4602 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004603}
4604
4605SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4606 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4607
4608 if (!Subtarget->is64Bit()) {
4609 // vastart just stores the address of the VarArgsFrameIndex slot into the
4610 // memory location argument.
4611 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4612 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4613 SV->getOffset());
4614 }
4615
4616 // __va_list_tag:
4617 // gp_offset (0 - 6 * 8)
4618 // fp_offset (48 - 48 + 8 * 16)
4619 // overflow_arg_area (point to parameters coming in memory).
4620 // reg_save_area
4621 SmallVector<SDOperand, 8> MemOps;
4622 SDOperand FIN = Op.getOperand(1);
4623 // Store gp_offset
4624 SDOperand Store = DAG.getStore(Op.getOperand(0),
4625 DAG.getConstant(VarArgsGPOffset, MVT::i32),
4626 FIN, SV->getValue(), SV->getOffset());
4627 MemOps.push_back(Store);
4628
4629 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004630 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631 Store = DAG.getStore(Op.getOperand(0),
4632 DAG.getConstant(VarArgsFPOffset, MVT::i32),
4633 FIN, SV->getValue(), SV->getOffset());
4634 MemOps.push_back(Store);
4635
4636 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004637 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4639 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4640 SV->getOffset());
4641 MemOps.push_back(Store);
4642
4643 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004644 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004645 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4646 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4647 SV->getOffset());
4648 MemOps.push_back(Store);
4649 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4650}
4651
4652SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4653 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4654 SDOperand Chain = Op.getOperand(0);
4655 SDOperand DstPtr = Op.getOperand(1);
4656 SDOperand SrcPtr = Op.getOperand(2);
4657 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4658 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4659
4660 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4661 SrcSV->getValue(), SrcSV->getOffset());
4662 Chain = SrcPtr.getValue(1);
4663 for (unsigned i = 0; i < 3; ++i) {
4664 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4665 SrcSV->getValue(), SrcSV->getOffset());
4666 Chain = Val.getValue(1);
4667 Chain = DAG.getStore(Chain, Val, DstPtr,
4668 DstSV->getValue(), DstSV->getOffset());
4669 if (i == 2)
4670 break;
4671 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004672 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004674 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004675 }
4676 return Chain;
4677}
4678
4679SDOperand
4680X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4681 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4682 switch (IntNo) {
4683 default: return SDOperand(); // Don't custom lower most intrinsics.
4684 // Comparison intrinsics.
4685 case Intrinsic::x86_sse_comieq_ss:
4686 case Intrinsic::x86_sse_comilt_ss:
4687 case Intrinsic::x86_sse_comile_ss:
4688 case Intrinsic::x86_sse_comigt_ss:
4689 case Intrinsic::x86_sse_comige_ss:
4690 case Intrinsic::x86_sse_comineq_ss:
4691 case Intrinsic::x86_sse_ucomieq_ss:
4692 case Intrinsic::x86_sse_ucomilt_ss:
4693 case Intrinsic::x86_sse_ucomile_ss:
4694 case Intrinsic::x86_sse_ucomigt_ss:
4695 case Intrinsic::x86_sse_ucomige_ss:
4696 case Intrinsic::x86_sse_ucomineq_ss:
4697 case Intrinsic::x86_sse2_comieq_sd:
4698 case Intrinsic::x86_sse2_comilt_sd:
4699 case Intrinsic::x86_sse2_comile_sd:
4700 case Intrinsic::x86_sse2_comigt_sd:
4701 case Intrinsic::x86_sse2_comige_sd:
4702 case Intrinsic::x86_sse2_comineq_sd:
4703 case Intrinsic::x86_sse2_ucomieq_sd:
4704 case Intrinsic::x86_sse2_ucomilt_sd:
4705 case Intrinsic::x86_sse2_ucomile_sd:
4706 case Intrinsic::x86_sse2_ucomigt_sd:
4707 case Intrinsic::x86_sse2_ucomige_sd:
4708 case Intrinsic::x86_sse2_ucomineq_sd: {
4709 unsigned Opc = 0;
4710 ISD::CondCode CC = ISD::SETCC_INVALID;
4711 switch (IntNo) {
4712 default: break;
4713 case Intrinsic::x86_sse_comieq_ss:
4714 case Intrinsic::x86_sse2_comieq_sd:
4715 Opc = X86ISD::COMI;
4716 CC = ISD::SETEQ;
4717 break;
4718 case Intrinsic::x86_sse_comilt_ss:
4719 case Intrinsic::x86_sse2_comilt_sd:
4720 Opc = X86ISD::COMI;
4721 CC = ISD::SETLT;
4722 break;
4723 case Intrinsic::x86_sse_comile_ss:
4724 case Intrinsic::x86_sse2_comile_sd:
4725 Opc = X86ISD::COMI;
4726 CC = ISD::SETLE;
4727 break;
4728 case Intrinsic::x86_sse_comigt_ss:
4729 case Intrinsic::x86_sse2_comigt_sd:
4730 Opc = X86ISD::COMI;
4731 CC = ISD::SETGT;
4732 break;
4733 case Intrinsic::x86_sse_comige_ss:
4734 case Intrinsic::x86_sse2_comige_sd:
4735 Opc = X86ISD::COMI;
4736 CC = ISD::SETGE;
4737 break;
4738 case Intrinsic::x86_sse_comineq_ss:
4739 case Intrinsic::x86_sse2_comineq_sd:
4740 Opc = X86ISD::COMI;
4741 CC = ISD::SETNE;
4742 break;
4743 case Intrinsic::x86_sse_ucomieq_ss:
4744 case Intrinsic::x86_sse2_ucomieq_sd:
4745 Opc = X86ISD::UCOMI;
4746 CC = ISD::SETEQ;
4747 break;
4748 case Intrinsic::x86_sse_ucomilt_ss:
4749 case Intrinsic::x86_sse2_ucomilt_sd:
4750 Opc = X86ISD::UCOMI;
4751 CC = ISD::SETLT;
4752 break;
4753 case Intrinsic::x86_sse_ucomile_ss:
4754 case Intrinsic::x86_sse2_ucomile_sd:
4755 Opc = X86ISD::UCOMI;
4756 CC = ISD::SETLE;
4757 break;
4758 case Intrinsic::x86_sse_ucomigt_ss:
4759 case Intrinsic::x86_sse2_ucomigt_sd:
4760 Opc = X86ISD::UCOMI;
4761 CC = ISD::SETGT;
4762 break;
4763 case Intrinsic::x86_sse_ucomige_ss:
4764 case Intrinsic::x86_sse2_ucomige_sd:
4765 Opc = X86ISD::UCOMI;
4766 CC = ISD::SETGE;
4767 break;
4768 case Intrinsic::x86_sse_ucomineq_ss:
4769 case Intrinsic::x86_sse2_ucomineq_sd:
4770 Opc = X86ISD::UCOMI;
4771 CC = ISD::SETNE;
4772 break;
4773 }
4774
4775 unsigned X86CC;
4776 SDOperand LHS = Op.getOperand(1);
4777 SDOperand RHS = Op.getOperand(2);
4778 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4779
Evan Cheng621216e2007-09-29 00:00:36 +00004780 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4781 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4782 DAG.getConstant(X86CC, MVT::i8), Cond);
4783 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784 }
4785 }
4786}
4787
4788SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4789 // Depths > 0 not supported yet!
4790 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4791 return SDOperand();
4792
4793 // Just load the return address
4794 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4795 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4796}
4797
4798SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4799 // Depths > 0 not supported yet!
4800 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4801 return SDOperand();
4802
4803 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4804 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00004805 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806}
4807
4808SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4809 SelectionDAG &DAG) {
4810 // Is not yet supported on x86-64
4811 if (Subtarget->is64Bit())
4812 return SDOperand();
4813
Chris Lattner5872a362008-01-17 07:00:52 +00004814 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004815}
4816
4817SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4818{
4819 assert(!Subtarget->is64Bit() &&
4820 "Lowering of eh_return builtin is not supported yet on x86-64");
4821
4822 MachineFunction &MF = DAG.getMachineFunction();
4823 SDOperand Chain = Op.getOperand(0);
4824 SDOperand Offset = Op.getOperand(1);
4825 SDOperand Handler = Op.getOperand(2);
4826
4827 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4828 getPointerTy());
4829
4830 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00004831 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004832 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4833 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4834 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00004835 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836
4837 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4838 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4839}
4840
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004841SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4842 SelectionDAG &DAG) {
4843 SDOperand Root = Op.getOperand(0);
4844 SDOperand Trmp = Op.getOperand(1); // trampoline
4845 SDOperand FPtr = Op.getOperand(2); // nested function
4846 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4847
4848 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4849
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00004850 const X86InstrInfo *TII =
4851 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4852
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004853 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00004854 SDOperand OutChains[6];
4855
4856 // Large code-model.
4857
4858 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
4859 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
4860
4861 const unsigned char N86R10 =
4862 ((X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
4863 const unsigned char N86R11 =
4864 ((X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
4865
4866 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
4867
4868 // Load the pointer to the nested function into R11.
4869 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
4870 SDOperand Addr = Trmp;
4871 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
4872 TrmpSV->getValue(), TrmpSV->getOffset());
4873
4874 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
4875 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpSV->getValue(),
4876 TrmpSV->getOffset() + 2, false, 2);
4877
4878 // Load the 'nest' parameter value into R10.
4879 // R10 is specified in X86CallingConv.td
4880 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
4881 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
4882 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
4883 TrmpSV->getValue(), TrmpSV->getOffset() + 10);
4884
4885 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
4886 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4887 TrmpSV->getOffset() + 12, false, 2);
4888
4889 // Jump to the nested function.
4890 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
4891 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
4892 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
4893 TrmpSV->getValue(), TrmpSV->getOffset() + 20);
4894
4895 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
4896 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
4897 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
4898 TrmpSV->getValue(), TrmpSV->getOffset() + 22);
4899
4900 SDOperand Ops[] =
4901 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
4902 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004903 } else {
4904 Function *Func = (Function *)
4905 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4906 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00004907 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004908
4909 switch (CC) {
4910 default:
4911 assert(0 && "Unsupported calling convention");
4912 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004913 case CallingConv::X86_StdCall: {
4914 // Pass 'nest' parameter in ECX.
4915 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00004916 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004917
4918 // Check that ECX wasn't needed by an 'inreg' parameter.
4919 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00004920 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004921
4922 if (Attrs && !Func->isVarArg()) {
4923 unsigned InRegCount = 0;
4924 unsigned Idx = 1;
4925
4926 for (FunctionType::param_iterator I = FTy->param_begin(),
4927 E = FTy->param_end(); I != E; ++I, ++Idx)
4928 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4929 // FIXME: should only count parameters that are lowered to integers.
4930 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4931
4932 if (InRegCount > 2) {
4933 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4934 abort();
4935 }
4936 }
4937 break;
4938 }
4939 case CallingConv::X86_FastCall:
4940 // Pass 'nest' parameter in EAX.
4941 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00004942 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004943 break;
4944 }
4945
4946 SDOperand OutChains[4];
4947 SDOperand Addr, Disp;
4948
4949 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4950 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4951
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00004952 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
4953 const unsigned char N86Reg =
4954 ((X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00004955 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004956 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4957
4958 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4959 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4960 TrmpSV->getOffset() + 1, false, 1);
4961
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00004962 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004963 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4964 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4965 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4966
4967 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4968 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4969 TrmpSV->getOffset() + 6, false, 1);
4970
Duncan Sands7407a9f2007-09-11 14:10:23 +00004971 SDOperand Ops[] =
4972 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4973 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00004974 }
4975}
4976
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00004977SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
4978 /*
4979 The rounding mode is in bits 11:10 of FPSR, and has the following
4980 settings:
4981 00 Round to nearest
4982 01 Round to -inf
4983 10 Round to +inf
4984 11 Round to 0
4985
4986 FLT_ROUNDS, on the other hand, expects the following:
4987 -1 Undefined
4988 0 Round to 0
4989 1 Round to nearest
4990 2 Round to +inf
4991 3 Round to -inf
4992
4993 To perform the conversion, we do:
4994 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
4995 */
4996
4997 MachineFunction &MF = DAG.getMachineFunction();
4998 const TargetMachine &TM = MF.getTarget();
4999 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5000 unsigned StackAlignment = TFI.getStackAlignment();
5001 MVT::ValueType VT = Op.getValueType();
5002
5003 // Save FP Control Word to stack slot
5004 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5005 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5006
5007 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5008 DAG.getEntryNode(), StackSlot);
5009
5010 // Load FP Control Word from stack slot
5011 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5012
5013 // Transform as necessary
5014 SDOperand CWD1 =
5015 DAG.getNode(ISD::SRL, MVT::i16,
5016 DAG.getNode(ISD::AND, MVT::i16,
5017 CWD, DAG.getConstant(0x800, MVT::i16)),
5018 DAG.getConstant(11, MVT::i8));
5019 SDOperand CWD2 =
5020 DAG.getNode(ISD::SRL, MVT::i16,
5021 DAG.getNode(ISD::AND, MVT::i16,
5022 CWD, DAG.getConstant(0x400, MVT::i16)),
5023 DAG.getConstant(9, MVT::i8));
5024
5025 SDOperand RetVal =
5026 DAG.getNode(ISD::AND, MVT::i16,
5027 DAG.getNode(ISD::ADD, MVT::i16,
5028 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5029 DAG.getConstant(1, MVT::i16)),
5030 DAG.getConstant(3, MVT::i16));
5031
5032
5033 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5034 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5035}
5036
Evan Cheng48679f42007-12-14 02:13:44 +00005037SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5038 MVT::ValueType VT = Op.getValueType();
5039 MVT::ValueType OpVT = VT;
5040 unsigned NumBits = MVT::getSizeInBits(VT);
5041
5042 Op = Op.getOperand(0);
5043 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005044 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005045 OpVT = MVT::i32;
5046 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5047 }
Evan Cheng48679f42007-12-14 02:13:44 +00005048
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005049 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5050 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5051 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5052
5053 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5054 SmallVector<SDOperand, 4> Ops;
5055 Ops.push_back(Op);
5056 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5057 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5058 Ops.push_back(Op.getValue(1));
5059 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5060
5061 // Finally xor with NumBits-1.
5062 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5063
Evan Cheng48679f42007-12-14 02:13:44 +00005064 if (VT == MVT::i8)
5065 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5066 return Op;
5067}
5068
5069SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5070 MVT::ValueType VT = Op.getValueType();
5071 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005072 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005073
5074 Op = Op.getOperand(0);
5075 if (VT == MVT::i8) {
5076 OpVT = MVT::i32;
5077 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5078 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005079
5080 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5081 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5082 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5083
5084 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5085 SmallVector<SDOperand, 4> Ops;
5086 Ops.push_back(Op);
5087 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5088 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5089 Ops.push_back(Op.getValue(1));
5090 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5091
Evan Cheng48679f42007-12-14 02:13:44 +00005092 if (VT == MVT::i8)
5093 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5094 return Op;
5095}
5096
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005097/// LowerOperation - Provide custom lowering hooks for some operations.
5098///
5099SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5100 switch (Op.getOpcode()) {
5101 default: assert(0 && "Should not custom lower this!");
5102 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5103 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5104 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5105 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5106 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5107 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5108 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5109 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5110 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5111 case ISD::SHL_PARTS:
5112 case ISD::SRA_PARTS:
5113 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5114 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5115 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5116 case ISD::FABS: return LowerFABS(Op, DAG);
5117 case ISD::FNEG: return LowerFNEG(Op, DAG);
5118 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005119 case ISD::SETCC: return LowerSETCC(Op, DAG);
5120 case ISD::SELECT: return LowerSELECT(Op, DAG);
5121 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005122 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5123 case ISD::CALL: return LowerCALL(Op, DAG);
5124 case ISD::RET: return LowerRET(Op, DAG);
5125 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5126 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5127 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128 case ISD::VASTART: return LowerVASTART(Op, DAG);
5129 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5130 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5131 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5132 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5133 case ISD::FRAME_TO_ARGS_OFFSET:
5134 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5135 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5136 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005137 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005138 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005139 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5140 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005141
5142 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5143 case ISD::READCYCLECOUNTER:
5144 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005145 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005146}
5147
5148/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5149SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5150 switch (N->getOpcode()) {
5151 default: assert(0 && "Should not custom lower this!");
5152 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5153 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5154 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005155}
5156
5157const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5158 switch (Opcode) {
5159 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005160 case X86ISD::BSF: return "X86ISD::BSF";
5161 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005162 case X86ISD::SHLD: return "X86ISD::SHLD";
5163 case X86ISD::SHRD: return "X86ISD::SHRD";
5164 case X86ISD::FAND: return "X86ISD::FAND";
5165 case X86ISD::FOR: return "X86ISD::FOR";
5166 case X86ISD::FXOR: return "X86ISD::FXOR";
5167 case X86ISD::FSRL: return "X86ISD::FSRL";
5168 case X86ISD::FILD: return "X86ISD::FILD";
5169 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5170 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5171 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5172 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5173 case X86ISD::FLD: return "X86ISD::FLD";
5174 case X86ISD::FST: return "X86ISD::FST";
5175 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
5176 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5177 case X86ISD::CALL: return "X86ISD::CALL";
5178 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5179 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5180 case X86ISD::CMP: return "X86ISD::CMP";
5181 case X86ISD::COMI: return "X86ISD::COMI";
5182 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5183 case X86ISD::SETCC: return "X86ISD::SETCC";
5184 case X86ISD::CMOV: return "X86ISD::CMOV";
5185 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5186 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5187 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5188 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5190 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5191 case X86ISD::S2VEC: return "X86ISD::S2VEC";
5192 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
5193 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5194 case X86ISD::FMAX: return "X86ISD::FMAX";
5195 case X86ISD::FMIN: return "X86ISD::FMIN";
5196 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5197 case X86ISD::FRCP: return "X86ISD::FRCP";
5198 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5199 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5200 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005201 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005202 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005203 }
5204}
5205
5206// isLegalAddressingMode - Return true if the addressing mode represented
5207// by AM is legal for this target, for a load/store of the specified type.
5208bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5209 const Type *Ty) const {
5210 // X86 supports extremely general addressing modes.
5211
5212 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5213 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5214 return false;
5215
5216 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005217 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005218 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5219 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005220
5221 // X86-64 only supports addr of globals in small code model.
5222 if (Subtarget->is64Bit()) {
5223 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5224 return false;
5225 // If lower 4G is not available, then we must use rip-relative addressing.
5226 if (AM.BaseOffs || AM.Scale > 1)
5227 return false;
5228 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 }
5230
5231 switch (AM.Scale) {
5232 case 0:
5233 case 1:
5234 case 2:
5235 case 4:
5236 case 8:
5237 // These scales always work.
5238 break;
5239 case 3:
5240 case 5:
5241 case 9:
5242 // These scales are formed with basereg+scalereg. Only accept if there is
5243 // no basereg yet.
5244 if (AM.HasBaseReg)
5245 return false;
5246 break;
5247 default: // Other stuff never works.
5248 return false;
5249 }
5250
5251 return true;
5252}
5253
5254
Evan Cheng27a820a2007-10-26 01:56:11 +00005255bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5256 if (!Ty1->isInteger() || !Ty2->isInteger())
5257 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005258 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5259 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5260 if (NumBits1 <= NumBits2)
5261 return false;
5262 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005263}
5264
Evan Cheng9decb332007-10-29 19:58:20 +00005265bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5266 MVT::ValueType VT2) const {
5267 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5268 return false;
5269 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5270 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5271 if (NumBits1 <= NumBits2)
5272 return false;
5273 return Subtarget->is64Bit() || NumBits1 < 64;
5274}
Evan Cheng27a820a2007-10-26 01:56:11 +00005275
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005276/// isShuffleMaskLegal - Targets can use this to indicate that they only
5277/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5278/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5279/// are assumed to be legal.
5280bool
5281X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5282 // Only do shuffles on 128-bit vector types for now.
5283 if (MVT::getSizeInBits(VT) == 64) return false;
5284 return (Mask.Val->getNumOperands() <= 4 ||
5285 isIdentityMask(Mask.Val) ||
5286 isIdentityMask(Mask.Val, true) ||
5287 isSplatMask(Mask.Val) ||
5288 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5289 X86::isUNPCKLMask(Mask.Val) ||
5290 X86::isUNPCKHMask(Mask.Val) ||
5291 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5292 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5293}
5294
5295bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5296 MVT::ValueType EVT,
5297 SelectionDAG &DAG) const {
5298 unsigned NumElts = BVOps.size();
5299 // Only do shuffles on 128-bit vector types for now.
5300 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5301 if (NumElts == 2) return true;
5302 if (NumElts == 4) {
5303 return (isMOVLMask(&BVOps[0], 4) ||
5304 isCommutedMOVL(&BVOps[0], 4, true) ||
5305 isSHUFPMask(&BVOps[0], 4) ||
5306 isCommutedSHUFP(&BVOps[0], 4));
5307 }
5308 return false;
5309}
5310
5311//===----------------------------------------------------------------------===//
5312// X86 Scheduler Hooks
5313//===----------------------------------------------------------------------===//
5314
5315MachineBasicBlock *
5316X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5317 MachineBasicBlock *BB) {
5318 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5319 switch (MI->getOpcode()) {
5320 default: assert(false && "Unexpected instr type to insert");
5321 case X86::CMOV_FR32:
5322 case X86::CMOV_FR64:
5323 case X86::CMOV_V4F32:
5324 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005325 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005326 // To "insert" a SELECT_CC instruction, we actually have to insert the
5327 // diamond control-flow pattern. The incoming instruction knows the
5328 // destination vreg to set, the condition code register to branch on, the
5329 // true/false values to select between, and a branch opcode to use.
5330 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5331 ilist<MachineBasicBlock>::iterator It = BB;
5332 ++It;
5333
5334 // thisMBB:
5335 // ...
5336 // TrueVal = ...
5337 // cmpTY ccX, r1, r2
5338 // bCC copy1MBB
5339 // fallthrough --> copy0MBB
5340 MachineBasicBlock *thisMBB = BB;
5341 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5342 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5343 unsigned Opc =
5344 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5345 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5346 MachineFunction *F = BB->getParent();
5347 F->getBasicBlockList().insert(It, copy0MBB);
5348 F->getBasicBlockList().insert(It, sinkMBB);
5349 // Update machine-CFG edges by first adding all successors of the current
5350 // block to the new block which will contain the Phi node for the select.
5351 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5352 e = BB->succ_end(); i != e; ++i)
5353 sinkMBB->addSuccessor(*i);
5354 // Next, remove all successors of the current block, and add the true
5355 // and fallthrough blocks as its successors.
5356 while(!BB->succ_empty())
5357 BB->removeSuccessor(BB->succ_begin());
5358 BB->addSuccessor(copy0MBB);
5359 BB->addSuccessor(sinkMBB);
5360
5361 // copy0MBB:
5362 // %FalseValue = ...
5363 // # fallthrough to sinkMBB
5364 BB = copy0MBB;
5365
5366 // Update machine-CFG edges
5367 BB->addSuccessor(sinkMBB);
5368
5369 // sinkMBB:
5370 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5371 // ...
5372 BB = sinkMBB;
5373 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5374 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5375 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5376
5377 delete MI; // The pseudo instruction is gone now.
5378 return BB;
5379 }
5380
5381 case X86::FP32_TO_INT16_IN_MEM:
5382 case X86::FP32_TO_INT32_IN_MEM:
5383 case X86::FP32_TO_INT64_IN_MEM:
5384 case X86::FP64_TO_INT16_IN_MEM:
5385 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005386 case X86::FP64_TO_INT64_IN_MEM:
5387 case X86::FP80_TO_INT16_IN_MEM:
5388 case X86::FP80_TO_INT32_IN_MEM:
5389 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005390 // Change the floating point control register to use "round towards zero"
5391 // mode when truncating to an integer value.
5392 MachineFunction *F = BB->getParent();
5393 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5394 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5395
5396 // Load the old value of the high byte of the control word...
5397 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005398 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005399 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5400
5401 // Set the high part to be round to zero...
5402 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5403 .addImm(0xC7F);
5404
5405 // Reload the modified control word now...
5406 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5407
5408 // Restore the memory image of control word to original value
5409 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5410 .addReg(OldCW);
5411
5412 // Get the X86 opcode to use.
5413 unsigned Opc;
5414 switch (MI->getOpcode()) {
5415 default: assert(0 && "illegal opcode!");
5416 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5417 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5418 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5419 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5420 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5421 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005422 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5423 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5424 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005425 }
5426
5427 X86AddressMode AM;
5428 MachineOperand &Op = MI->getOperand(0);
5429 if (Op.isRegister()) {
5430 AM.BaseType = X86AddressMode::RegBase;
5431 AM.Base.Reg = Op.getReg();
5432 } else {
5433 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005434 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005435 }
5436 Op = MI->getOperand(1);
5437 if (Op.isImmediate())
5438 AM.Scale = Op.getImm();
5439 Op = MI->getOperand(2);
5440 if (Op.isImmediate())
5441 AM.IndexReg = Op.getImm();
5442 Op = MI->getOperand(3);
5443 if (Op.isGlobalAddress()) {
5444 AM.GV = Op.getGlobal();
5445 } else {
5446 AM.Disp = Op.getImm();
5447 }
5448 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5449 .addReg(MI->getOperand(4).getReg());
5450
5451 // Reload the original control word now.
5452 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5453
5454 delete MI; // The pseudo instruction is gone now.
5455 return BB;
5456 }
5457 }
5458}
5459
5460//===----------------------------------------------------------------------===//
5461// X86 Optimization Hooks
5462//===----------------------------------------------------------------------===//
5463
5464void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5465 uint64_t Mask,
5466 uint64_t &KnownZero,
5467 uint64_t &KnownOne,
5468 const SelectionDAG &DAG,
5469 unsigned Depth) const {
5470 unsigned Opc = Op.getOpcode();
5471 assert((Opc >= ISD::BUILTIN_OP_END ||
5472 Opc == ISD::INTRINSIC_WO_CHAIN ||
5473 Opc == ISD::INTRINSIC_W_CHAIN ||
5474 Opc == ISD::INTRINSIC_VOID) &&
5475 "Should use MaskedValueIsZero if you don't know whether Op"
5476 " is a target node!");
5477
5478 KnownZero = KnownOne = 0; // Don't know anything.
5479 switch (Opc) {
5480 default: break;
5481 case X86ISD::SETCC:
5482 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5483 break;
5484 }
5485}
5486
5487/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5488/// element of the result of the vector shuffle.
5489static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5490 MVT::ValueType VT = N->getValueType(0);
5491 SDOperand PermMask = N->getOperand(2);
5492 unsigned NumElems = PermMask.getNumOperands();
5493 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5494 i %= NumElems;
5495 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5496 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005497 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005498 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5499 SDOperand Idx = PermMask.getOperand(i);
5500 if (Idx.getOpcode() == ISD::UNDEF)
5501 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5502 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5503 }
5504 return SDOperand();
5505}
5506
5507/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5508/// node is a GlobalAddress + an offset.
5509static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5510 unsigned Opc = N->getOpcode();
5511 if (Opc == X86ISD::Wrapper) {
5512 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5513 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5514 return true;
5515 }
5516 } else if (Opc == ISD::ADD) {
5517 SDOperand N1 = N->getOperand(0);
5518 SDOperand N2 = N->getOperand(1);
5519 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5520 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5521 if (V) {
5522 Offset += V->getSignExtended();
5523 return true;
5524 }
5525 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5526 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5527 if (V) {
5528 Offset += V->getSignExtended();
5529 return true;
5530 }
5531 }
5532 }
5533 return false;
5534}
5535
5536/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5537/// + Dist * Size.
5538static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5539 MachineFrameInfo *MFI) {
5540 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5541 return false;
5542
5543 SDOperand Loc = N->getOperand(1);
5544 SDOperand BaseLoc = Base->getOperand(1);
5545 if (Loc.getOpcode() == ISD::FrameIndex) {
5546 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5547 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005548 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5549 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005550 int FS = MFI->getObjectSize(FI);
5551 int BFS = MFI->getObjectSize(BFI);
5552 if (FS != BFS || FS != Size) return false;
5553 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5554 } else {
5555 GlobalValue *GV1 = NULL;
5556 GlobalValue *GV2 = NULL;
5557 int64_t Offset1 = 0;
5558 int64_t Offset2 = 0;
5559 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5560 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5561 if (isGA1 && isGA2 && GV1 == GV2)
5562 return Offset1 == (Offset2 + Dist*Size);
5563 }
5564
5565 return false;
5566}
5567
5568static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5569 const X86Subtarget *Subtarget) {
5570 GlobalValue *GV;
5571 int64_t Offset;
5572 if (isGAPlusOffset(Base, GV, Offset))
5573 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5574 else {
5575 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman53491e92007-07-23 20:24:29 +00005576 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005577 if (BFI < 0)
5578 // Fixed objects do not specify alignment, however the offsets are known.
5579 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5580 (MFI->getObjectOffset(BFI) % 16) == 0);
5581 else
5582 return MFI->getObjectAlignment(BFI) >= 16;
5583 }
5584 return false;
5585}
5586
5587
5588/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5589/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5590/// if the load addresses are consecutive, non-overlapping, and in the right
5591/// order.
5592static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5593 const X86Subtarget *Subtarget) {
5594 MachineFunction &MF = DAG.getMachineFunction();
5595 MachineFrameInfo *MFI = MF.getFrameInfo();
5596 MVT::ValueType VT = N->getValueType(0);
5597 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5598 SDOperand PermMask = N->getOperand(2);
5599 int NumElems = (int)PermMask.getNumOperands();
5600 SDNode *Base = NULL;
5601 for (int i = 0; i < NumElems; ++i) {
5602 SDOperand Idx = PermMask.getOperand(i);
5603 if (Idx.getOpcode() == ISD::UNDEF) {
5604 if (!Base) return SDOperand();
5605 } else {
5606 SDOperand Arg =
5607 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5608 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5609 return SDOperand();
5610 if (!Base)
5611 Base = Arg.Val;
5612 else if (!isConsecutiveLoad(Arg.Val, Base,
5613 i, MVT::getSizeInBits(EVT)/8,MFI))
5614 return SDOperand();
5615 }
5616 }
5617
5618 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005619 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005621 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005622 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005624 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5625 LD->getSrcValueOffset(), LD->isVolatile(),
5626 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627 }
5628}
5629
5630/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5631static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5632 const X86Subtarget *Subtarget) {
5633 SDOperand Cond = N->getOperand(0);
5634
5635 // If we have SSE[12] support, try to form min/max nodes.
5636 if (Subtarget->hasSSE2() &&
5637 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5638 if (Cond.getOpcode() == ISD::SETCC) {
5639 // Get the LHS/RHS of the select.
5640 SDOperand LHS = N->getOperand(1);
5641 SDOperand RHS = N->getOperand(2);
5642 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5643
5644 unsigned Opcode = 0;
5645 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5646 switch (CC) {
5647 default: break;
5648 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5649 case ISD::SETULE:
5650 case ISD::SETLE:
5651 if (!UnsafeFPMath) break;
5652 // FALL THROUGH.
5653 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5654 case ISD::SETLT:
5655 Opcode = X86ISD::FMIN;
5656 break;
5657
5658 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5659 case ISD::SETUGT:
5660 case ISD::SETGT:
5661 if (!UnsafeFPMath) break;
5662 // FALL THROUGH.
5663 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5664 case ISD::SETGE:
5665 Opcode = X86ISD::FMAX;
5666 break;
5667 }
5668 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5669 switch (CC) {
5670 default: break;
5671 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5672 case ISD::SETUGT:
5673 case ISD::SETGT:
5674 if (!UnsafeFPMath) break;
5675 // FALL THROUGH.
5676 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5677 case ISD::SETGE:
5678 Opcode = X86ISD::FMIN;
5679 break;
5680
5681 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5682 case ISD::SETULE:
5683 case ISD::SETLE:
5684 if (!UnsafeFPMath) break;
5685 // FALL THROUGH.
5686 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5687 case ISD::SETLT:
5688 Opcode = X86ISD::FMAX;
5689 break;
5690 }
5691 }
5692
5693 if (Opcode)
5694 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5695 }
5696
5697 }
5698
5699 return SDOperand();
5700}
5701
5702
5703SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5704 DAGCombinerInfo &DCI) const {
5705 SelectionDAG &DAG = DCI.DAG;
5706 switch (N->getOpcode()) {
5707 default: break;
5708 case ISD::VECTOR_SHUFFLE:
5709 return PerformShuffleCombine(N, DAG, Subtarget);
5710 case ISD::SELECT:
5711 return PerformSELECTCombine(N, DAG, Subtarget);
5712 }
5713
5714 return SDOperand();
5715}
5716
5717//===----------------------------------------------------------------------===//
5718// X86 Inline Assembly Support
5719//===----------------------------------------------------------------------===//
5720
5721/// getConstraintType - Given a constraint letter, return the type of
5722/// constraint it is for this target.
5723X86TargetLowering::ConstraintType
5724X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5725 if (Constraint.size() == 1) {
5726 switch (Constraint[0]) {
5727 case 'A':
5728 case 'r':
5729 case 'R':
5730 case 'l':
5731 case 'q':
5732 case 'Q':
5733 case 'x':
5734 case 'Y':
5735 return C_RegisterClass;
5736 default:
5737 break;
5738 }
5739 }
5740 return TargetLowering::getConstraintType(Constraint);
5741}
5742
Chris Lattnera531abc2007-08-25 00:47:38 +00005743/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5744/// vector. If it is invalid, don't add anything to Ops.
5745void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5746 char Constraint,
5747 std::vector<SDOperand>&Ops,
5748 SelectionDAG &DAG) {
5749 SDOperand Result(0, 0);
5750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005751 switch (Constraint) {
5752 default: break;
5753 case 'I':
5754 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005755 if (C->getValue() <= 31) {
5756 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5757 break;
5758 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005759 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005760 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005761 case 'N':
5762 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005763 if (C->getValue() <= 255) {
5764 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5765 break;
5766 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005767 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005768 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005769 case 'i': {
5770 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00005771 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5772 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5773 break;
5774 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775
5776 // If we are in non-pic codegen mode, we allow the address of a global (with
5777 // an optional displacement) to be used with 'i'.
5778 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5779 int64_t Offset = 0;
5780
5781 // Match either (GA) or (GA+C)
5782 if (GA) {
5783 Offset = GA->getOffset();
5784 } else if (Op.getOpcode() == ISD::ADD) {
5785 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5786 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5787 if (C && GA) {
5788 Offset = GA->getOffset()+C->getValue();
5789 } else {
5790 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5791 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5792 if (C && GA)
5793 Offset = GA->getOffset()+C->getValue();
5794 else
5795 C = 0, GA = 0;
5796 }
5797 }
5798
5799 if (GA) {
5800 // If addressing this global requires a load (e.g. in PIC mode), we can't
5801 // match.
5802 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5803 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00005804 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005805
5806 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5807 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00005808 Result = Op;
5809 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005810 }
5811
5812 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00005813 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005814 }
5815 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005816
5817 if (Result.Val) {
5818 Ops.push_back(Result);
5819 return;
5820 }
5821 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822}
5823
5824std::vector<unsigned> X86TargetLowering::
5825getRegClassForInlineAsmConstraint(const std::string &Constraint,
5826 MVT::ValueType VT) const {
5827 if (Constraint.size() == 1) {
5828 // FIXME: not handling fp-stack yet!
5829 switch (Constraint[0]) { // GCC X86 Constraint Letters
5830 default: break; // Unknown constraint letter
5831 case 'A': // EAX/EDX
5832 if (VT == MVT::i32 || VT == MVT::i64)
5833 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5834 break;
5835 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5836 case 'Q': // Q_REGS
5837 if (VT == MVT::i32)
5838 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5839 else if (VT == MVT::i16)
5840 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5841 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00005842 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00005843 else if (VT == MVT::i64)
5844 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
5845 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846 }
5847 }
5848
5849 return std::vector<unsigned>();
5850}
5851
5852std::pair<unsigned, const TargetRegisterClass*>
5853X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5854 MVT::ValueType VT) const {
5855 // First, see if this is a constraint that directly corresponds to an LLVM
5856 // register class.
5857 if (Constraint.size() == 1) {
5858 // GCC Constraint Letters
5859 switch (Constraint[0]) {
5860 default: break;
5861 case 'r': // GENERAL_REGS
5862 case 'R': // LEGACY_REGS
5863 case 'l': // INDEX_REGS
5864 if (VT == MVT::i64 && Subtarget->is64Bit())
5865 return std::make_pair(0U, X86::GR64RegisterClass);
5866 if (VT == MVT::i32)
5867 return std::make_pair(0U, X86::GR32RegisterClass);
5868 else if (VT == MVT::i16)
5869 return std::make_pair(0U, X86::GR16RegisterClass);
5870 else if (VT == MVT::i8)
5871 return std::make_pair(0U, X86::GR8RegisterClass);
5872 break;
5873 case 'y': // MMX_REGS if MMX allowed.
5874 if (!Subtarget->hasMMX()) break;
5875 return std::make_pair(0U, X86::VR64RegisterClass);
5876 break;
5877 case 'Y': // SSE_REGS if SSE2 allowed
5878 if (!Subtarget->hasSSE2()) break;
5879 // FALL THROUGH.
5880 case 'x': // SSE_REGS if SSE1 allowed
5881 if (!Subtarget->hasSSE1()) break;
5882
5883 switch (VT) {
5884 default: break;
5885 // Scalar SSE types.
5886 case MVT::f32:
5887 case MVT::i32:
5888 return std::make_pair(0U, X86::FR32RegisterClass);
5889 case MVT::f64:
5890 case MVT::i64:
5891 return std::make_pair(0U, X86::FR64RegisterClass);
5892 // Vector types.
5893 case MVT::v16i8:
5894 case MVT::v8i16:
5895 case MVT::v4i32:
5896 case MVT::v2i64:
5897 case MVT::v4f32:
5898 case MVT::v2f64:
5899 return std::make_pair(0U, X86::VR128RegisterClass);
5900 }
5901 break;
5902 }
5903 }
5904
5905 // Use the default implementation in TargetLowering to convert the register
5906 // constraint into a member of a register class.
5907 std::pair<unsigned, const TargetRegisterClass*> Res;
5908 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5909
5910 // Not found as a standard register?
5911 if (Res.second == 0) {
5912 // GCC calls "st(0)" just plain "st".
5913 if (StringsEqualNoCase("{st}", Constraint)) {
5914 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00005915 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005916 }
5917
5918 return Res;
5919 }
5920
5921 // Otherwise, check to see if this is a register class of the wrong value
5922 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5923 // turn into {ax},{dx}.
5924 if (Res.second->hasType(VT))
5925 return Res; // Correct type already, nothing to do.
5926
5927 // All of the single-register GCC register classes map their values onto
5928 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5929 // really want an 8-bit or 32-bit register, map to the appropriate register
5930 // class and return the appropriate register.
5931 if (Res.second != X86::GR16RegisterClass)
5932 return Res;
5933
5934 if (VT == MVT::i8) {
5935 unsigned DestReg = 0;
5936 switch (Res.first) {
5937 default: break;
5938 case X86::AX: DestReg = X86::AL; break;
5939 case X86::DX: DestReg = X86::DL; break;
5940 case X86::CX: DestReg = X86::CL; break;
5941 case X86::BX: DestReg = X86::BL; break;
5942 }
5943 if (DestReg) {
5944 Res.first = DestReg;
5945 Res.second = Res.second = X86::GR8RegisterClass;
5946 }
5947 } else if (VT == MVT::i32) {
5948 unsigned DestReg = 0;
5949 switch (Res.first) {
5950 default: break;
5951 case X86::AX: DestReg = X86::EAX; break;
5952 case X86::DX: DestReg = X86::EDX; break;
5953 case X86::CX: DestReg = X86::ECX; break;
5954 case X86::BX: DestReg = X86::EBX; break;
5955 case X86::SI: DestReg = X86::ESI; break;
5956 case X86::DI: DestReg = X86::EDI; break;
5957 case X86::BP: DestReg = X86::EBP; break;
5958 case X86::SP: DestReg = X86::ESP; break;
5959 }
5960 if (DestReg) {
5961 Res.first = DestReg;
5962 Res.second = Res.second = X86::GR32RegisterClass;
5963 }
5964 } else if (VT == MVT::i64) {
5965 unsigned DestReg = 0;
5966 switch (Res.first) {
5967 default: break;
5968 case X86::AX: DestReg = X86::RAX; break;
5969 case X86::DX: DestReg = X86::RDX; break;
5970 case X86::CX: DestReg = X86::RCX; break;
5971 case X86::BX: DestReg = X86::RBX; break;
5972 case X86::SI: DestReg = X86::RSI; break;
5973 case X86::DI: DestReg = X86::RDI; break;
5974 case X86::BP: DestReg = X86::RBP; break;
5975 case X86::SP: DestReg = X86::RSP; break;
5976 }
5977 if (DestReg) {
5978 Res.first = DestReg;
5979 Res.second = Res.second = X86::GR64RegisterClass;
5980 }
5981 }
5982
5983 return Res;
5984}