blob: 179f600d4964c948ae4718289f500948f365ce67 [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
29#include "llvm/CodeGen/CallingConvLower.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000040#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/StringExtras.h"
Dale Johannesen98738822008-02-22 22:17:59 +000042#include "llvm/ParamAttrsList.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043using namespace llvm;
44
45X86TargetLowering::X86TargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
47 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000048 X86ScalarSSEf64 = Subtarget->hasSSE2();
49 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000051
Chris Lattnerdec9cb52008-01-24 08:07:48 +000052 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 RegInfo = TM.getRegisterInfo();
55
56 // Set up the TargetLowering object.
57
58 // X86 is weird, it always uses i8 for shift amounts and setcc results.
59 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 setSetCCResultContents(ZeroOrOneSetCCResult);
61 setSchedulingPreference(SchedulingForRegPressure);
62 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
63 setStackPointerRegisterToSaveRestore(X86StackPtr);
64
65 if (Subtarget->isTargetDarwin()) {
66 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
67 setUseUnderscoreSetJmp(false);
68 setUseUnderscoreLongJmp(false);
69 } else if (Subtarget->isTargetMingw()) {
70 // MS runtime is weird: it exports _setjmp, but longjmp!
71 setUseUnderscoreSetJmp(true);
72 setUseUnderscoreLongJmp(false);
73 } else {
74 setUseUnderscoreSetJmp(true);
75 setUseUnderscoreLongJmp(true);
76 }
77
78 // Set up the register classes.
79 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
80 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
81 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
82 if (Subtarget->is64Bit())
83 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
84
Duncan Sands082524c2008-01-23 20:39:46 +000085 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086
Chris Lattner3bc08502008-01-17 19:59:44 +000087 // We don't accept any truncstore of integer registers.
88 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
89 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
91 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
94
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
96 // operation.
97 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
98 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
100
101 if (Subtarget->is64Bit()) {
102 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
103 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
104 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000105 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
107 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
108 else
109 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
110 }
111
112 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
113 // this operation.
114 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
115 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
116 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000117 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000119 // f32 and f64 cases are Legal, f80 case is not
120 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
121 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
123 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
124 }
125
Dale Johannesen958b08b2007-09-19 23:55:34 +0000126 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
127 // are Legal, f80 is custom lowered.
128 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
129 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130
131 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
132 // this operation.
133 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
134 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
135
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000136 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000138 // f32 and f64 cases are Legal, f80 case is not
139 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 } else {
141 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
142 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
143 }
144
145 // Handle FP_TO_UINT by promoting the destination to a larger signed
146 // conversion.
147 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
148 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
150
151 if (Subtarget->is64Bit()) {
152 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
153 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
154 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000155 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 // Expand FP_TO_UINT into a select.
157 // FIXME: We would like to use a Custom expander here eventually to do
158 // the optimal thing for SSE vs. the default expansion in the legalizer.
159 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
160 else
161 // With SSE3 we can use fisttpll to convert to a signed i64.
162 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
163 }
164
165 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000166 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
168 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
169 }
170
Dan Gohman8450d862008-02-18 19:34:53 +0000171 // Scalar integer divide and remainder are lowered to use operations that
172 // produce two results, to match the available instructions. This exposes
173 // the two-result form to trivial CSE, which is able to combine x/y and x%y
174 // into a single instruction.
175 //
176 // Scalar integer multiply-high is also lowered to use two-result
177 // operations, to match the available instructions. However, plain multiply
178 // (low) operations are left as Legal, as there are single-result
179 // instructions for this in x86. Using the two-result multiply instructions
180 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000181 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
182 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
183 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
184 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::SREM , MVT::i8 , Expand);
186 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000187 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
188 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
189 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
190 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::SREM , MVT::i16 , Expand);
192 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000193 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
194 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
195 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
196 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::SREM , MVT::i32 , Expand);
198 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000199 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
200 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
201 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
202 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::SREM , MVT::i64 , Expand);
204 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000205
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
207 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
208 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
209 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
210 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
211 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
216 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000217 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000219 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000220 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000226 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
227 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000229 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
230 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 if (Subtarget->is64Bit()) {
232 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000233 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
234 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 }
236
237 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
238 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
239
240 // These should be promoted to a larger select which is supported.
241 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
242 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
243 // X86 wants to expand cmov itself.
244 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
245 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000248 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000254 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
257 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
258 }
259 // X86 ret instruction may pop stack.
260 setOperationAction(ISD::RET , MVT::Other, Custom);
261 if (!Subtarget->is64Bit())
262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
263
264 // Darwin ABI issue.
265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
269 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
270 if (Subtarget->is64Bit()) {
271 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
272 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
273 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
274 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
275 }
276 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
277 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
278 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
279 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000280 if (Subtarget->is64Bit()) {
281 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
282 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
283 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
284 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 // X86 wants to expand memset / memcpy itself.
286 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
287 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
288
Evan Cheng8d51ab32008-03-10 19:38:10 +0000289 if (Subtarget->hasSSE1())
290 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000291
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000292 if (!Subtarget->hasSSE2())
293 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
294
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +0000295 setOperationAction(ISD::ATOMIC_LCS , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_LCS , MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_LCS , MVT::i32, Custom);
Andrew Lenharthbd7d3262008-03-04 21:13:33 +0000298 setOperationAction(ISD::ATOMIC_LCS , MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000299
Evan Cheng2e28d622008-02-02 04:07:54 +0000300 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 // FIXME - use subtarget debug flags
303 if (!Subtarget->isTargetDarwin() &&
304 !Subtarget->isTargetELF() &&
305 !Subtarget->isTargetCygMing())
306 setOperationAction(ISD::LABEL, MVT::Other, Expand);
307
308 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
309 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
310 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
311 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
312 if (Subtarget->is64Bit()) {
313 // FIXME: Verify
314 setExceptionPointerRegister(X86::RAX);
315 setExceptionSelectorRegister(X86::RDX);
316 } else {
317 setExceptionPointerRegister(X86::EAX);
318 setExceptionSelectorRegister(X86::EDX);
319 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000320 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
Duncan Sands7407a9f2007-09-11 14:10:23 +0000322 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000323
Chris Lattner56b941f2008-01-15 21:58:22 +0000324 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000325
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
327 setOperationAction(ISD::VASTART , MVT::Other, Custom);
328 setOperationAction(ISD::VAARG , MVT::Other, Expand);
329 setOperationAction(ISD::VAEND , MVT::Other, Expand);
330 if (Subtarget->is64Bit())
331 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
332 else
333 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
334
335 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
336 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
337 if (Subtarget->is64Bit())
338 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
339 if (Subtarget->isTargetCygMing())
340 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
341 else
342 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
343
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000344 if (X86ScalarSSEf64) {
345 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 // Set up the FP register classes.
347 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
348 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
349
350 // Use ANDPD to simulate FABS.
351 setOperationAction(ISD::FABS , MVT::f64, Custom);
352 setOperationAction(ISD::FABS , MVT::f32, Custom);
353
354 // Use XORP to simulate FNEG.
355 setOperationAction(ISD::FNEG , MVT::f64, Custom);
356 setOperationAction(ISD::FNEG , MVT::f32, Custom);
357
358 // Use ANDPD and ORPD to simulate FCOPYSIGN.
359 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
360 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
361
362 // We don't support sin/cos/fmod
363 setOperationAction(ISD::FSIN , MVT::f64, Expand);
364 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 setOperationAction(ISD::FSIN , MVT::f32, Expand);
366 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367
368 // Expand FP immediates into loads from the stack, except for the special
369 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000370 addLegalFPImmediate(APFloat(+0.0)); // xorpd
371 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000372
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000373 // Floating truncations from f80 and extensions to f80 go through memory.
374 // If optimizing, we lie about this though and handle it in
375 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
376 if (Fast) {
377 setConvertAction(MVT::f32, MVT::f80, Expand);
378 setConvertAction(MVT::f64, MVT::f80, Expand);
379 setConvertAction(MVT::f80, MVT::f32, Expand);
380 setConvertAction(MVT::f80, MVT::f64, Expand);
381 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000382 } else if (X86ScalarSSEf32) {
383 // Use SSE for f32, x87 for f64.
384 // Set up the FP register classes.
385 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
386 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
387
388 // Use ANDPS to simulate FABS.
389 setOperationAction(ISD::FABS , MVT::f32, Custom);
390
391 // Use XORP to simulate FNEG.
392 setOperationAction(ISD::FNEG , MVT::f32, Custom);
393
394 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
395
396 // Use ANDPS and ORPS to simulate FCOPYSIGN.
397 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
398 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
399
400 // We don't support sin/cos/fmod
401 setOperationAction(ISD::FSIN , MVT::f32, Expand);
402 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000403
Nate Begemane2ba64f2008-02-14 08:57:00 +0000404 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000405 addLegalFPImmediate(APFloat(+0.0f)); // xorps
406 addLegalFPImmediate(APFloat(+0.0)); // FLD0
407 addLegalFPImmediate(APFloat(+1.0)); // FLD1
408 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
409 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
410
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000411 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
412 // this though and handle it in InstructionSelectPreprocess so that
413 // dagcombine2 can hack on these.
414 if (Fast) {
415 setConvertAction(MVT::f32, MVT::f64, Expand);
416 setConvertAction(MVT::f32, MVT::f80, Expand);
417 setConvertAction(MVT::f80, MVT::f32, Expand);
418 setConvertAction(MVT::f64, MVT::f32, Expand);
419 // And x87->x87 truncations also.
420 setConvertAction(MVT::f80, MVT::f64, Expand);
421 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000422
423 if (!UnsafeFPMath) {
424 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
425 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
426 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000428 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 // Set up the FP register classes.
430 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
431 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
432
433 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
434 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
435 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
436 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000437
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000438 // Floating truncations go through memory. If optimizing, we lie about
439 // this though and handle it in InstructionSelectPreprocess so that
440 // dagcombine2 can hack on these.
441 if (Fast) {
442 setConvertAction(MVT::f80, MVT::f32, Expand);
443 setConvertAction(MVT::f64, MVT::f32, Expand);
444 setConvertAction(MVT::f80, MVT::f64, Expand);
445 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446
447 if (!UnsafeFPMath) {
448 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
449 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
450 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000451 addLegalFPImmediate(APFloat(+0.0)); // FLD0
452 addLegalFPImmediate(APFloat(+1.0)); // FLD1
453 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
454 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000455 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
456 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
457 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
458 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 }
460
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000461 // Long double always uses X87.
462 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000463 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
464 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000465 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000466 APFloat TmpFlt(+0.0);
467 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
468 addLegalFPImmediate(TmpFlt); // FLD0
469 TmpFlt.changeSign();
470 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
471 APFloat TmpFlt2(+1.0);
472 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
473 addLegalFPImmediate(TmpFlt2); // FLD1
474 TmpFlt2.changeSign();
475 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
476 }
477
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000478 if (!UnsafeFPMath) {
479 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
480 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
481 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000482
Dan Gohman2f7b1982007-10-11 23:21:31 +0000483 // Always use a library call for pow.
484 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
485 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
486 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
487
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 // First set operation action for all vector types to expand. Then we
489 // will selectively turn on ones that can be effectively codegen'd.
490 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
491 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
492 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000515 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000519 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000520 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
521 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000523 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
524 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
525 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
526 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
527 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
528 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 }
530
531 if (Subtarget->hasMMX()) {
532 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
533 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
534 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
535 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
536
537 // FIXME: add MMX packed arithmetics
538
539 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
540 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
541 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
542 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
543
544 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
545 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
546 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000547 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548
549 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
550 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
551
552 setOperationAction(ISD::AND, MVT::v8i8, Promote);
553 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
554 setOperationAction(ISD::AND, MVT::v4i16, Promote);
555 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
556 setOperationAction(ISD::AND, MVT::v2i32, Promote);
557 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
558 setOperationAction(ISD::AND, MVT::v1i64, Legal);
559
560 setOperationAction(ISD::OR, MVT::v8i8, Promote);
561 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
562 setOperationAction(ISD::OR, MVT::v4i16, Promote);
563 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
564 setOperationAction(ISD::OR, MVT::v2i32, Promote);
565 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
566 setOperationAction(ISD::OR, MVT::v1i64, Legal);
567
568 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
569 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
570 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
571 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
572 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
573 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
574 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
575
576 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
577 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
578 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
579 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
580 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
581 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
582 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
583
584 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
585 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
586 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
587 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
588
589 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
590 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
591 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
592 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
593
594 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
595 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
597 }
598
599 if (Subtarget->hasSSE1()) {
600 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
601
602 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
603 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
604 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
605 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
606 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
607 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
609 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
610 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
611 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
612 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
613 }
614
615 if (Subtarget->hasSSE2()) {
616 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
617 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
618 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
619 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
620 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
621
622 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
623 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
624 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
625 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
626 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
627 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
628 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
629 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
630 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
631 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
632 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
633 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
634 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
635 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
636 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637
638 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
641 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
643
644 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
645 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000646 // Do not attempt to custom lower non-power-of-2 vectors
647 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
648 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
650 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
651 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
652 }
653 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
654 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
655 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
656 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000657 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000659 if (Subtarget->is64Bit()) {
660 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000661 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000662 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663
664 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
665 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
666 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
667 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
668 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
669 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
670 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
671 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
672 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
673 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
674 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
675 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
676 }
677
Chris Lattner3bc08502008-01-17 19:59:44 +0000678 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000679
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 // Custom lower v2i64 and v2f64 selects.
681 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
682 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
683 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
684 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
685 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000686
687 if (Subtarget->hasSSE41()) {
688 // FIXME: Do we need to handle scalar-to-vector here?
689 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
690
691 // i8 and i16 vectors are custom , because the source register and source
692 // source memory operand types are not the same width. f32 vectors are
693 // custom since the immediate controlling the insert encodes additional
694 // information.
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
697 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
698 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
699
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
702 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
703 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
704
705 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000706 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
707 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000708 }
709 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710
711 // We want to custom lower some of our intrinsics.
712 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
713
714 // We have target-specific dag combine patterns for the following nodes:
715 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
716 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000717 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718
719 computeRegisterProperties();
720
721 // FIXME: These should be based on subtarget info. Plus, the values should
722 // be smaller when we are in optimizing for size mode.
723 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
724 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
725 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
726 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000727 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728}
729
Scott Michel502151f2008-03-10 15:42:14 +0000730
731MVT::ValueType
732X86TargetLowering::getSetCCResultType(const SDOperand &) const {
733 return MVT::i8;
734}
735
736
Evan Cheng5a67b812008-01-23 23:17:41 +0000737/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
738/// the desired ByVal argument alignment.
739static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
740 if (MaxAlign == 16)
741 return;
742 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
743 if (VTy->getBitWidth() == 128)
744 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000745 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
746 unsigned EltAlign = 0;
747 getMaxByValAlign(ATy->getElementType(), EltAlign);
748 if (EltAlign > MaxAlign)
749 MaxAlign = EltAlign;
750 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
751 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
752 unsigned EltAlign = 0;
753 getMaxByValAlign(STy->getElementType(i), EltAlign);
754 if (EltAlign > MaxAlign)
755 MaxAlign = EltAlign;
756 if (MaxAlign == 16)
757 break;
758 }
759 }
760 return;
761}
762
763/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
764/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000765/// that contain SSE vectors are placed at 16-byte boundaries while the rest
766/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000767unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
768 if (Subtarget->is64Bit())
769 return getTargetData()->getABITypeAlignment(Ty);
770 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000771 if (Subtarget->hasSSE1())
772 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000773 return Align;
774}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775
Evan Cheng6fb06762007-11-09 01:32:10 +0000776/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
777/// jumptable.
778SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
779 SelectionDAG &DAG) const {
780 if (usesGlobalOffsetTable())
781 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
782 if (!Subtarget->isPICStyleRIPRel())
783 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
784 return Table;
785}
786
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787//===----------------------------------------------------------------------===//
788// Return Value Calling Convention Implementation
789//===----------------------------------------------------------------------===//
790
791#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000792
793/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
794/// exists skip possible ISD:TokenFactor.
795static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000796 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000797 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000798 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000799 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000800 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000801 return Chain.getOperand(0);
802 }
803 return Chain;
804}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000805
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806/// LowerRET - Lower an ISD::RET node.
807SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
808 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
809
810 SmallVector<CCValAssign, 16> RVLocs;
811 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
812 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
813 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
814 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000815
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 // If this is the first return lowered for this function, add the regs to the
817 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000818 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 for (unsigned i = 0; i != RVLocs.size(); ++i)
820 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000821 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000825 // Handle tail call return.
826 Chain = GetPossiblePreceedingTailCall(Chain);
827 if (Chain.getOpcode() == X86ISD::TAILCALL) {
828 SDOperand TailCall = Chain;
829 SDOperand TargetAddress = TailCall.getOperand(1);
830 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000831 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000832 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
833 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
834 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
835 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
836 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000837 assert(StackAdjustment.getOpcode() == ISD::Constant &&
838 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000839
840 SmallVector<SDOperand,8> Operands;
841 Operands.push_back(Chain.getOperand(0));
842 Operands.push_back(TargetAddress);
843 Operands.push_back(StackAdjustment);
844 // Copy registers used by the call. Last operand is a flag so it is not
845 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000846 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000847 Operands.push_back(Chain.getOperand(i));
848 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000849 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
850 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000851 }
852
853 // Regular return.
854 SDOperand Flag;
855
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000857 for (unsigned i = 0; i != RVLocs.size(); ++i) {
858 CCValAssign &VA = RVLocs[i];
859 assert(VA.isRegLoc() && "Can only return in registers!");
860 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000862 // If this is a copy from an xmm register to ST(0), use an FPExtend to
863 // change the value to the FP stack register class.
864 if (RVLocs[i].getLocReg() == X86::ST0 &&
865 isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
866 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000868 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 Flag = Chain.getValue(1);
870 }
871
872 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
873 if (Flag.Val)
874 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
875 else
876 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
877}
878
879
880/// LowerCallResult - Lower the result values of an ISD::CALL into the
881/// appropriate copies out of appropriate physical registers. This assumes that
882/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
883/// being lowered. The returns a SDNode with the same number of values as the
884/// ISD::CALL.
885SDNode *X86TargetLowering::
886LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
887 unsigned CallingConv, SelectionDAG &DAG) {
888
889 // Assign locations to each value returned by this call.
890 SmallVector<CCValAssign, 16> RVLocs;
891 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
892 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
893 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
894
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 SmallVector<SDOperand, 8> ResultVals;
896
897 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000898 for (unsigned i = 0; i != RVLocs.size(); ++i) {
899 MVT::ValueType CopyVT = RVLocs[i].getValVT();
900
901 // If this is a call to a function that returns an fp value on the floating
902 // point stack, but where we prefer to use the value in xmm registers, copy
903 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
904 if (RVLocs[i].getLocReg() == X86::ST0 &&
905 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
906 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000909 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
910 CopyVT, InFlag).getValue(1);
911 SDOperand Val = Chain.getValue(0);
912 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000913
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000914 if (CopyVT != RVLocs[i].getValVT()) {
915 // Round the F80 the right size, which also moves to the appropriate xmm
916 // register.
917 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
918 // This truncation won't change the value.
919 DAG.getIntPtrConstant(1));
920 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000921
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000922 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 }
924
925 // Merge everything together with a MERGE_VALUES node.
926 ResultVals.push_back(Chain);
927 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
928 &ResultVals[0], ResultVals.size()).Val;
929}
930
Evan Cheng931a8f42008-01-29 19:34:22 +0000931/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
932/// ISD::CALL where the results are known to be in two 64-bit registers,
933/// e.g. XMM0 and XMM1. This simplify store the two values back to the
934/// fixed stack slot allocated for StructRet.
935SDNode *X86TargetLowering::
936LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
937 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
938 MVT::ValueType VT, SelectionDAG &DAG) {
939 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
940 Chain = RetVal1.getValue(1);
941 InFlag = RetVal1.getValue(2);
942 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
943 Chain = RetVal2.getValue(1);
944 InFlag = RetVal2.getValue(2);
945 SDOperand FIN = TheCall->getOperand(5);
946 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
947 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
948 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
949 return Chain.Val;
950}
951
952/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
953/// where the results are known to be in ST0 and ST1.
954SDNode *X86TargetLowering::
955LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
956 SDNode *TheCall, SelectionDAG &DAG) {
957 SmallVector<SDOperand, 8> ResultVals;
958 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
959 SDVTList Tys = DAG.getVTList(VTs, 4);
960 SDOperand Ops[] = { Chain, InFlag };
Chris Lattner5d294e52008-03-09 07:05:32 +0000961 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_ST0_ST1, Tys, Ops, 2);
Evan Cheng931a8f42008-01-29 19:34:22 +0000962 Chain = RetVal.getValue(2);
963 SDOperand FIN = TheCall->getOperand(5);
964 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
965 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
966 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
967 return Chain.Val;
968}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969
970//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000971// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972//===----------------------------------------------------------------------===//
973// StdCall calling convention seems to be standard for many Windows' API
974// routines and around. It differs from C calling convention just a little:
975// callee should clean up the stack, not caller. Symbols should be also
976// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000977// For info on fast calling convention see Fast Calling Convention (tail call)
978// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979
980/// AddLiveIn - This helper function adds the specified physical register to the
981/// MachineFunction as a live in value. It also creates a corresponding virtual
982/// register for it.
983static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
984 const TargetRegisterClass *RC) {
985 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000986 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
987 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 return VReg;
989}
990
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000991/// CallIsStructReturn - Determines whether a CALL node uses struct return
992/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000993static bool CallIsStructReturn(SDOperand Op) {
994 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
995 if (!NumOps)
996 return false;
997
998 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
999 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1000}
1001
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001002/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1003/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001004static bool ArgsAreStructReturn(SDOperand Op) {
1005 unsigned NumArgs = Op.Val->getNumValues() - 1;
1006 if (!NumArgs)
1007 return false;
1008
1009 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1010 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1011}
1012
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001013/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1014/// callee to pop its own arguments. Callee pop is necessary to support tail
1015/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001016bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1017 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1018 if (IsVarArg)
1019 return false;
1020
1021 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1022 default:
1023 return false;
1024 case CallingConv::X86_StdCall:
1025 return !Subtarget->is64Bit();
1026 case CallingConv::X86_FastCall:
1027 return !Subtarget->is64Bit();
1028 case CallingConv::Fast:
1029 return PerformTailCallOpt;
1030 }
1031}
1032
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001033/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1034/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001035CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1036 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1037
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001038 if (Subtarget->is64Bit()) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001039 if (CC == CallingConv::Fast && PerformTailCallOpt)
1040 return CC_X86_64_TailCall;
1041 else
1042 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001043 }
1044
Gordon Henriksen18ace102008-01-05 16:56:59 +00001045 if (CC == CallingConv::X86_FastCall)
1046 return CC_X86_32_FastCall;
1047 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1048 return CC_X86_32_TailCall;
1049 else
1050 return CC_X86_32_C;
1051}
1052
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001053/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1054/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001055NameDecorationStyle
1056X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1057 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1058 if (CC == CallingConv::X86_FastCall)
1059 return FastCall;
1060 else if (CC == CallingConv::X86_StdCall)
1061 return StdCall;
1062 return None;
1063}
1064
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001065/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1066/// possibly be overwritten when lowering the outgoing arguments in a tail
1067/// call. Currently the implementation of this call is very conservative and
1068/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1069/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001070static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1071 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001072 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001073 FrameIndexSDNode * FrameIdxNode = NULL;
1074 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001075 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1076 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001077 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1078 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1079 (Op.getOpcode() == ISD::LOAD &&
1080 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1081 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1082 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001083 return true;
1084 return false;
1085}
1086
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001087/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1088/// in a register before calling.
1089bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1090 return !IsTailCall && !Is64Bit &&
1091 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1092 Subtarget->isPICStyleGOT();
1093}
1094
1095
1096/// CallRequiresFnAddressInReg - Check whether the call requires the function
1097/// address to be loaded in a register.
1098bool
1099X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1100 return !Is64Bit && IsTailCall &&
1101 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1102 Subtarget->isPICStyleGOT();
1103}
1104
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001105/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1106/// arguments to force loading and guarantee that arguments sourcing from
1107/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001108static SDOperand
1109CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1110 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1111 SelectionDAG &DAG,
1112 MachineFunction &MF,
1113 const TargetLowering * TL) {
1114
1115 SDOperand InFlag;
1116 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1117 SDOperand Arg = TailCallClobberedVRegs[i].second;
1118 unsigned Idx = TailCallClobberedVRegs[i].first;
1119 unsigned VReg =
1120 MF.getRegInfo().
1121 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1122 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1123 InFlag = Chain.getValue(1);
1124 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1125 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1126 Chain = Arg.getValue(1);
1127 InFlag = Arg.getValue(2);
1128 }
1129 return Chain;
1130}
1131
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001132/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1133/// by "Src" to address "Dst" with size and alignment information specified by
1134/// the specific parameter attribute. The copy will be passed as a byval function
1135/// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001136static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001137CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Dale Johannesen322e3b72008-03-10 02:17:22 +00001138 ISD::ParamFlags::ParamFlagsTy Flags,
1139 SelectionDAG &DAG) {
1140 unsigned Align = ISD::ParamFlags::One <<
Evan Cheng5817a0e2008-01-12 01:08:07 +00001141 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1142 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001143 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001144 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1145 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001146 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001147 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001148}
1149
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001150SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1151 const CCValAssign &VA,
1152 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001153 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001154 SDOperand Root, unsigned i) {
1155 // Create the nodes corresponding to a load from this parameter slot.
Dale Johannesen322e3b72008-03-10 02:17:22 +00001156 ISD::ParamFlags::ParamFlagsTy Flags =
1157 cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001158 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Evan Cheng3e42a522008-01-10 02:24:25 +00001159 bool isByVal = Flags & ISD::ParamFlags::ByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001160 bool isImmutable = !AlwaysUseMutable && !isByVal;
Evan Cheng3e42a522008-01-10 02:24:25 +00001161
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001162 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1163 // changed with more analysis.
1164 // In case of tail call optimization mark all arguments mutable. Since they
1165 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001166 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001167 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001168 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001169 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001170 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001171 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001172 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001173}
1174
Gordon Henriksen18ace102008-01-05 16:56:59 +00001175SDOperand
1176X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001178 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1179
1180 const Function* Fn = MF.getFunction();
1181 if (Fn->hasExternalLinkage() &&
1182 Subtarget->isTargetCygMing() &&
1183 Fn->getName() == "main")
1184 FuncInfo->setForceFramePointer(true);
1185
1186 // Decorate the function name.
1187 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1188
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189 MachineFrameInfo *MFI = MF.getFrameInfo();
1190 SDOperand Root = Op.getOperand(0);
1191 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001192 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001193 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001194
1195 assert(!(isVarArg && CC == CallingConv::Fast) &&
1196 "Var args not supported with calling convention fastcc");
1197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 // Assign locations to all of the incoming arguments.
1199 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001200 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001201 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 SmallVector<SDOperand, 8> ArgValues;
1204 unsigned LastVal = ~0U;
1205 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1206 CCValAssign &VA = ArgLocs[i];
1207 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1208 // places.
1209 assert(VA.getValNo() != LastVal &&
1210 "Don't support value assigned to multiple locs yet");
1211 LastVal = VA.getValNo();
1212
1213 if (VA.isRegLoc()) {
1214 MVT::ValueType RegVT = VA.getLocVT();
1215 TargetRegisterClass *RC;
1216 if (RegVT == MVT::i32)
1217 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001218 else if (Is64Bit && RegVT == MVT::i64)
1219 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001220 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001221 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001222 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001223 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224 else {
1225 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001226 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1227 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1228 RegVT = MVT::i64;
1229 } else
1230 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1234 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1235
1236 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1237 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1238 // right size.
1239 if (VA.getLocInfo() == CCValAssign::SExt)
1240 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1241 DAG.getValueType(VA.getValVT()));
1242 else if (VA.getLocInfo() == CCValAssign::ZExt)
1243 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1244 DAG.getValueType(VA.getValVT()));
1245
1246 if (VA.getLocInfo() != CCValAssign::Full)
1247 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1248
Gordon Henriksen18ace102008-01-05 16:56:59 +00001249 // Handle MMX values passed in GPRs.
1250 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1251 MVT::getSizeInBits(RegVT) == 64)
1252 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1253
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001254 ArgValues.push_back(ArgValue);
1255 } else {
1256 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001257 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 }
1259 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001262 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001263 if (CC == CallingConv::Fast)
1264 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265
1266 // If the function takes variable number of arguments, make a frame index for
1267 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001268 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001269 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1270 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1271 }
1272 if (Is64Bit) {
1273 static const unsigned GPR64ArgRegs[] = {
1274 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1275 };
1276 static const unsigned XMMArgRegs[] = {
1277 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1278 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1279 };
1280
1281 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1282 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1283
1284 // For X86-64, if there are vararg parameters that are passed via
1285 // registers, then we must store them to their spots on the stack so they
1286 // may be loaded by deferencing the result of va_next.
1287 VarArgsGPOffset = NumIntRegs * 8;
1288 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1289 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1290
1291 // Store the integer parameter registers.
1292 SmallVector<SDOperand, 8> MemOps;
1293 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1294 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001295 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001296 for (; NumIntRegs != 6; ++NumIntRegs) {
1297 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1298 X86::GR64RegisterClass);
1299 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001300 SDOperand Store =
1301 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001302 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001303 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001304 MemOps.push_back(Store);
1305 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001306 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001307 }
1308
1309 // Now store the XMM (fp + vector) parameter registers.
1310 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001311 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001312 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1313 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1314 X86::VR128RegisterClass);
1315 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001316 SDOperand Store =
1317 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001318 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001319 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 MemOps.push_back(Store);
1321 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001322 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001323 }
1324 if (!MemOps.empty())
1325 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1326 &MemOps[0], MemOps.size());
1327 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001328 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329
1330 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1331 // arguments and the arguments after the retaddr has been pushed are
1332 // aligned.
1333 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1334 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1335 (StackSize & 7) == 0)
1336 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001338 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001339
Gordon Henriksen18ace102008-01-05 16:56:59 +00001340 // Some CCs need callee pop.
1341 if (IsCalleePop(Op)) {
1342 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 BytesCallerReserves = 0;
1344 } else {
1345 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 BytesCallerReserves = StackSize;
1350 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001351
Gordon Henriksen18ace102008-01-05 16:56:59 +00001352 if (!Is64Bit) {
1353 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1354 if (CC == CallingConv::X86_FastCall)
1355 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1356 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357
Anton Korobeynikove844e472007-08-15 17:12:32 +00001358 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359
1360 // Return the new list of results.
1361 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1362 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1363}
1364
Evan Chengbc077bf2008-01-10 00:09:10 +00001365SDOperand
1366X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1367 const SDOperand &StackPtr,
1368 const CCValAssign &VA,
1369 SDOperand Chain,
1370 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001371 unsigned LocMemOffset = VA.getLocMemOffset();
1372 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001373 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1374 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Dale Johannesen322e3b72008-03-10 02:17:22 +00001375 ISD::ParamFlags::ParamFlagsTy Flags =
1376 cast<ConstantSDNode>(FlagsOp)->getValue();
Evan Chengbc077bf2008-01-10 00:09:10 +00001377 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001378 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001379 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001380 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001381 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001382}
1383
Evan Cheng931a8f42008-01-29 19:34:22 +00001384/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1385/// struct return call to the specified function. X86-64 ABI specifies
1386/// some SRet calls are actually returned in registers. Since current
1387/// LLVM cannot represent multi-value calls, they are represent as
1388/// calls where the results are passed in a hidden struct provided by
1389/// the caller. This function examines the type of the struct to
1390/// determine the correct way to implement the call.
1391X86::X86_64SRet
1392X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1393 // FIXME: Disabled for now.
1394 return X86::InMemory;
1395
1396 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1397 const Type *RTy = PTy->getElementType();
1398 unsigned Size = getTargetData()->getABITypeSize(RTy);
1399 if (Size != 16 && Size != 32)
1400 return X86::InMemory;
1401
1402 if (Size == 32) {
1403 const StructType *STy = dyn_cast<StructType>(RTy);
1404 if (!STy) return X86::InMemory;
1405 if (STy->getNumElements() == 2 &&
1406 STy->getElementType(0) == Type::X86_FP80Ty &&
1407 STy->getElementType(1) == Type::X86_FP80Ty)
1408 return X86::InX87;
1409 }
1410
1411 bool AllFP = true;
1412 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1413 I != E; ++I) {
1414 const Type *STy = I->get();
1415 if (!STy->isFPOrFPVector()) {
1416 AllFP = false;
1417 break;
1418 }
1419 }
1420
1421 if (AllFP)
1422 return X86::InSSE;
1423 return X86::InGPR64;
1424}
1425
1426void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1427 CCAssignFn *Fn,
1428 CCState &CCInfo) {
1429 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1430 for (unsigned i = 1; i != NumOps; ++i) {
1431 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1432 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1433 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1434 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1435 cerr << "Call operand #" << i << " has unhandled type "
1436 << MVT::getValueTypeString(ArgVT) << "\n";
1437 abort();
1438 }
1439 }
1440}
1441
Gordon Henriksen18ace102008-01-05 16:56:59 +00001442SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1443 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001444 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001446 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001448 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1449 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001451 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001452 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001453
1454 assert(!(isVarArg && CC == CallingConv::Fast) &&
1455 "Var args not supported with calling convention fastcc");
1456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457 // Analyze operands of the call, assigning locations to each operand.
1458 SmallVector<CCValAssign, 16> ArgLocs;
1459 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001460 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1461
1462 X86::X86_64SRet SRetMethod = X86::InMemory;
1463 if (Is64Bit && IsStructRet)
1464 // FIXME: We can't figure out type of the sret structure for indirect
1465 // calls. We need to copy more information from CallSite to the ISD::CALL
1466 // node.
1467 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1468 SRetMethod =
1469 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1470
1471 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1472 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1473 // a sret call.
1474 if (SRetMethod != X86::InMemory)
1475 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1476 else
1477 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478
1479 // Get a count of how many bytes are to be pushed on the stack.
1480 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001481 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001482 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483
Gordon Henriksen18ace102008-01-05 16:56:59 +00001484 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1485 // arguments and the arguments after the retaddr has been pushed are aligned.
1486 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1487 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1488 (NumBytes & 7) == 0)
1489 NumBytes += 4;
1490
1491 int FPDiff = 0;
1492 if (IsTailCall) {
1493 // Lower arguments at fp - stackoffset + fpdiff.
1494 unsigned NumBytesCallerPushed =
1495 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1496 FPDiff = NumBytesCallerPushed - NumBytes;
1497
1498 // Set the delta of movement of the returnaddr stackslot.
1499 // But only set if delta is greater than previous delta.
1500 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1501 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1502 }
1503
Chris Lattner5872a362008-01-17 07:00:52 +00001504 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001506 SDOperand RetAddrFrIdx;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001507 if (IsTailCall) {
1508 // Adjust the Return address stack slot.
1509 if (FPDiff) {
1510 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1511 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1512 // Load the "old" Return address.
1513 RetAddrFrIdx =
1514 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001515 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1516 }
1517 }
1518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001520 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 SmallVector<SDOperand, 8> MemOpChains;
1522
1523 SDOperand StackPtr;
1524
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001525 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001526 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1528 CCValAssign &VA = ArgLocs[i];
1529 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1530
1531 // Promote the value if needed.
1532 switch (VA.getLocInfo()) {
1533 default: assert(0 && "Unknown loc info!");
1534 case CCValAssign::Full: break;
1535 case CCValAssign::SExt:
1536 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1537 break;
1538 case CCValAssign::ZExt:
1539 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1540 break;
1541 case CCValAssign::AExt:
1542 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1543 break;
1544 }
1545
1546 if (VA.isRegLoc()) {
1547 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1548 } else {
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001549 if (!IsTailCall) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001550 assert(VA.isMemLoc());
1551 if (StackPtr.Val == 0)
1552 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1553
1554 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1555 Arg));
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001556 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1557 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001558 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559 }
1560 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561
1562 if (!MemOpChains.empty())
1563 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1564 &MemOpChains[0], MemOpChains.size());
1565
1566 // Build a sequence of copy-to-reg nodes chained together with token chain
1567 // and flag operands which copy the outgoing args into registers.
1568 SDOperand InFlag;
1569 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1570 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1571 InFlag);
1572 InFlag = Chain.getValue(1);
1573 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001574
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001575 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001576 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001577 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1578 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1579 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1580 InFlag);
1581 InFlag = Chain.getValue(1);
1582 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001583 // If we are tail calling and generating PIC/GOT style code load the address
1584 // of the callee into ecx. The value in ecx is used as target of the tail
1585 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1586 // calls on PIC/GOT architectures. Normally we would just put the address of
1587 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1588 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001589 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001590 // Note: The actual moving to ecx is done further down.
1591 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1592 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1593 !G->getGlobal()->hasProtectedVisibility())
1594 Callee = LowerGlobalAddress(Callee, DAG);
1595 else if (isa<ExternalSymbolSDNode>(Callee))
1596 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001597 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001598
Gordon Henriksen18ace102008-01-05 16:56:59 +00001599 if (Is64Bit && isVarArg) {
1600 // From AMD64 ABI document:
1601 // For calls that may call functions that use varargs or stdargs
1602 // (prototype-less calls or calls to functions containing ellipsis (...) in
1603 // the declaration) %al is used as hidden argument to specify the number
1604 // of SSE registers used. The contents of %al do not need to match exactly
1605 // the number of registers, but must be an ubound on the number of SSE
1606 // registers used and is in the range 0 - 8 inclusive.
1607
1608 // Count the number of XMM registers allocated.
1609 static const unsigned XMMArgRegs[] = {
1610 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1611 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1612 };
1613 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1614
1615 Chain = DAG.getCopyToReg(Chain, X86::AL,
1616 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1617 InFlag = Chain.getValue(1);
1618 }
1619
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001620
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001621 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001622 if (IsTailCall) {
1623 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001624 SDOperand FIN;
1625 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001626 // Do not flag preceeding copytoreg stuff together with the following stuff.
1627 InFlag = SDOperand();
1628
1629 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1630 DAG, MF, this);
1631
Gordon Henriksen18ace102008-01-05 16:56:59 +00001632 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1633 CCValAssign &VA = ArgLocs[i];
1634 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001635 assert(VA.isMemLoc());
1636 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Dale Johannesen322e3b72008-03-10 02:17:22 +00001638 ISD::ParamFlags::ParamFlagsTy Flags =
1639 cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001640 // Create frame index.
1641 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1642 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1643 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1644 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001645
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001646 // Find virtual register for this argument.
1647 bool Found=false;
1648 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1649 if (TailCallClobberedVRegs[idx].first==i) {
1650 Arg = TailCallClobberedVRegs[idx].second;
1651 Found=true;
1652 break;
1653 }
1654 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1655 (Found==true && "No corresponding Argument was found"));
1656
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001658 // Copy relative to framepointer.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001659 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001660 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001661 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001662 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001663 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001664 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001665 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001666 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 }
1668 }
1669
1670 if (!MemOpChains2.empty())
1671 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001672 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673
1674 // Store the return address to the appropriate stack slot.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001675 if (FPDiff) {
1676 // Calculate the new stack slot for the return address.
1677 int SlotSize = Is64Bit ? 8 : 4;
1678 int NewReturnAddrFI =
1679 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1680 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1681 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1682 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1683 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1684 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001685 }
1686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001687 // If the callee is a GlobalAddress node (quite common, every direct call is)
1688 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1689 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1690 // We should use extra load for direct calls to dllimported functions in
1691 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001692 if ((IsTailCall || !Is64Bit ||
1693 getTargetMachine().getCodeModel() != CodeModel::Large)
1694 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1695 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001696 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001697 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001698 if (IsTailCall || !Is64Bit ||
1699 getTargetMachine().getCodeModel() != CodeModel::Large)
1700 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1701 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1703
1704 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001705 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 Callee,InFlag);
1707 Callee = DAG.getRegister(Opc, getPointerTy());
1708 // Add register as live out.
1709 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001710 }
1711
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001712 // Returns a chain & a flag for retval copy to use.
1713 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1714 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715
1716 if (IsTailCall) {
1717 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001718 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1719 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720 if (InFlag.Val)
1721 Ops.push_back(InFlag);
1722 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1723 InFlag = Chain.getValue(1);
1724
1725 // Returns a chain & a flag for retval copy to use.
1726 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1727 Ops.clear();
1728 }
1729
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730 Ops.push_back(Chain);
1731 Ops.push_back(Callee);
1732
Gordon Henriksen18ace102008-01-05 16:56:59 +00001733 if (IsTailCall)
1734 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735
1736 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001737 if (!IsTailCall && !Is64Bit &&
1738 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001739 Subtarget->isPICStyleGOT())
1740 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001741
Gordon Henriksen18ace102008-01-05 16:56:59 +00001742 // Add argument registers to the end of the list so that they are known live
1743 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001744 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1745 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1746 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001747
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001748 if (InFlag.Val)
1749 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001750
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751 if (IsTailCall) {
1752 assert(InFlag.Val &&
1753 "Flag must be set. Depend on flag being set in LowerRET");
1754 Chain = DAG.getNode(X86ISD::TAILCALL,
1755 Op.Val->getVTList(), &Ops[0], Ops.size());
1756
1757 return SDOperand(Chain.Val, Op.ResNo);
1758 }
1759
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001760 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 InFlag = Chain.getValue(1);
1762
1763 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764 unsigned NumBytesForCalleeToPush;
1765 if (IsCalleePop(Op))
1766 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001767 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001768 // If this is is a call to a struct-return function, the callee
1769 // pops the hidden struct pointer, so we have to push it back.
1770 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001771 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001773 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001774
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001775 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001776 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001777 DAG.getIntPtrConstant(NumBytes),
1778 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001779 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001780 InFlag = Chain.getValue(1);
1781
1782 // Handle result values, copying them out of physregs into vregs that we
1783 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001784 switch (SRetMethod) {
1785 default:
1786 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1787 case X86::InGPR64:
1788 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1789 X86::RAX, X86::RDX,
1790 MVT::i64, DAG), Op.ResNo);
1791 case X86::InSSE:
1792 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1793 X86::XMM0, X86::XMM1,
1794 MVT::f64, DAG), Op.ResNo);
1795 case X86::InX87:
1796 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1797 Op.ResNo);
1798 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001799}
1800
1801
1802//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001803// Fast Calling Convention (tail call) implementation
1804//===----------------------------------------------------------------------===//
1805
1806// Like std call, callee cleans arguments, convention except that ECX is
1807// reserved for storing the tail called function address. Only 2 registers are
1808// free for argument passing (inreg). Tail call optimization is performed
1809// provided:
1810// * tailcallopt is enabled
1811// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001812// On X86_64 architecture with GOT-style position independent code only local
1813// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001814// To keep the stack aligned according to platform abi the function
1815// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1816// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001817// If a tail called function callee has more arguments than the caller the
1818// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001819// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001820// original REtADDR, but before the saved framepointer or the spilled registers
1821// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1822// stack layout:
1823// arg1
1824// arg2
1825// RETADDR
1826// [ new RETADDR
1827// move area ]
1828// (possible EBP)
1829// ESI
1830// EDI
1831// local1 ..
1832
1833/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1834/// for a 16 byte align requirement.
1835unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1836 SelectionDAG& DAG) {
1837 if (PerformTailCallOpt) {
1838 MachineFunction &MF = DAG.getMachineFunction();
1839 const TargetMachine &TM = MF.getTarget();
1840 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1841 unsigned StackAlignment = TFI.getStackAlignment();
1842 uint64_t AlignMask = StackAlignment - 1;
1843 int64_t Offset = StackSize;
1844 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1845 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1846 // Number smaller than 12 so just add the difference.
1847 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1848 } else {
1849 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1850 Offset = ((~AlignMask) & Offset) + StackAlignment +
1851 (StackAlignment-SlotSize);
1852 }
1853 StackSize = Offset;
1854 }
1855 return StackSize;
1856}
1857
1858/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001859/// following the call is a return. A function is eligible if caller/callee
1860/// calling conventions match, currently only fastcc supports tail calls, and
1861/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001862bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1863 SDOperand Ret,
1864 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001865 if (!PerformTailCallOpt)
1866 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001867
1868 // Check whether CALL node immediatly preceeds the RET node and whether the
1869 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001870 unsigned NumOps = Ret.getNumOperands();
1871 if ((NumOps == 1 &&
1872 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1873 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001874 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001875 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1876 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001877 MachineFunction &MF = DAG.getMachineFunction();
1878 unsigned CallerCC = MF.getFunction()->getCallingConv();
1879 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1880 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1881 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001882 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001883 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001884 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001885 return true;
1886
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001887 // Can only do local tail calls (in same module, hidden or protected) on
1888 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001889 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1890 return G->getGlobal()->hasHiddenVisibility()
1891 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001892 }
1893 }
Evan Chenge7a87392007-11-02 01:26:22 +00001894
1895 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001896}
1897
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898//===----------------------------------------------------------------------===//
1899// Other Lowering Hooks
1900//===----------------------------------------------------------------------===//
1901
1902
1903SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001904 MachineFunction &MF = DAG.getMachineFunction();
1905 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1906 int ReturnAddrIndex = FuncInfo->getRAIndex();
1907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001908 if (ReturnAddrIndex == 0) {
1909 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001910 if (Subtarget->is64Bit())
1911 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1912 else
1913 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001914
1915 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001916 }
1917
1918 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1919}
1920
1921
1922
1923/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1924/// specific condition code. It returns a false if it cannot do a direct
1925/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1926/// needed.
1927static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1928 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1929 SelectionDAG &DAG) {
1930 X86CC = X86::COND_INVALID;
1931 if (!isFP) {
1932 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1933 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1934 // X > -1 -> X == 0, jump !sign.
1935 RHS = DAG.getConstant(0, RHS.getValueType());
1936 X86CC = X86::COND_NS;
1937 return true;
1938 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1939 // X < 0 -> X == 0, jump on sign.
1940 X86CC = X86::COND_S;
1941 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001942 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1943 // X < 1 -> X <= 0
1944 RHS = DAG.getConstant(0, RHS.getValueType());
1945 X86CC = X86::COND_LE;
1946 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001947 }
1948 }
1949
1950 switch (SetCCOpcode) {
1951 default: break;
1952 case ISD::SETEQ: X86CC = X86::COND_E; break;
1953 case ISD::SETGT: X86CC = X86::COND_G; break;
1954 case ISD::SETGE: X86CC = X86::COND_GE; break;
1955 case ISD::SETLT: X86CC = X86::COND_L; break;
1956 case ISD::SETLE: X86CC = X86::COND_LE; break;
1957 case ISD::SETNE: X86CC = X86::COND_NE; break;
1958 case ISD::SETULT: X86CC = X86::COND_B; break;
1959 case ISD::SETUGT: X86CC = X86::COND_A; break;
1960 case ISD::SETULE: X86CC = X86::COND_BE; break;
1961 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1962 }
1963 } else {
1964 // On a floating point condition, the flags are set as follows:
1965 // ZF PF CF op
1966 // 0 | 0 | 0 | X > Y
1967 // 0 | 0 | 1 | X < Y
1968 // 1 | 0 | 0 | X == Y
1969 // 1 | 1 | 1 | unordered
1970 bool Flip = false;
1971 switch (SetCCOpcode) {
1972 default: break;
1973 case ISD::SETUEQ:
1974 case ISD::SETEQ: X86CC = X86::COND_E; break;
1975 case ISD::SETOLT: Flip = true; // Fallthrough
1976 case ISD::SETOGT:
1977 case ISD::SETGT: X86CC = X86::COND_A; break;
1978 case ISD::SETOLE: Flip = true; // Fallthrough
1979 case ISD::SETOGE:
1980 case ISD::SETGE: X86CC = X86::COND_AE; break;
1981 case ISD::SETUGT: Flip = true; // Fallthrough
1982 case ISD::SETULT:
1983 case ISD::SETLT: X86CC = X86::COND_B; break;
1984 case ISD::SETUGE: Flip = true; // Fallthrough
1985 case ISD::SETULE:
1986 case ISD::SETLE: X86CC = X86::COND_BE; break;
1987 case ISD::SETONE:
1988 case ISD::SETNE: X86CC = X86::COND_NE; break;
1989 case ISD::SETUO: X86CC = X86::COND_P; break;
1990 case ISD::SETO: X86CC = X86::COND_NP; break;
1991 }
1992 if (Flip)
1993 std::swap(LHS, RHS);
1994 }
1995
1996 return X86CC != X86::COND_INVALID;
1997}
1998
1999/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2000/// code. Current x86 isa includes the following FP cmov instructions:
2001/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2002static bool hasFPCMov(unsigned X86CC) {
2003 switch (X86CC) {
2004 default:
2005 return false;
2006 case X86::COND_B:
2007 case X86::COND_BE:
2008 case X86::COND_E:
2009 case X86::COND_P:
2010 case X86::COND_A:
2011 case X86::COND_AE:
2012 case X86::COND_NE:
2013 case X86::COND_NP:
2014 return true;
2015 }
2016}
2017
2018/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2019/// true if Op is undef or if its value falls within the specified range (L, H].
2020static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2021 if (Op.getOpcode() == ISD::UNDEF)
2022 return true;
2023
2024 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2025 return (Val >= Low && Val < Hi);
2026}
2027
2028/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2029/// true if Op is undef or if its value equal to the specified value.
2030static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2031 if (Op.getOpcode() == ISD::UNDEF)
2032 return true;
2033 return cast<ConstantSDNode>(Op)->getValue() == Val;
2034}
2035
2036/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2037/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2038bool X86::isPSHUFDMask(SDNode *N) {
2039 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2040
Dan Gohman7dc19012007-08-02 21:17:01 +00002041 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002042 return false;
2043
2044 // Check if the value doesn't reference the second vector.
2045 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2046 SDOperand Arg = N->getOperand(i);
2047 if (Arg.getOpcode() == ISD::UNDEF) continue;
2048 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002049 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002050 return false;
2051 }
2052
2053 return true;
2054}
2055
2056/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2057/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2058bool X86::isPSHUFHWMask(SDNode *N) {
2059 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2060
2061 if (N->getNumOperands() != 8)
2062 return false;
2063
2064 // Lower quadword copied in order.
2065 for (unsigned i = 0; i != 4; ++i) {
2066 SDOperand Arg = N->getOperand(i);
2067 if (Arg.getOpcode() == ISD::UNDEF) continue;
2068 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2069 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2070 return false;
2071 }
2072
2073 // Upper quadword shuffled.
2074 for (unsigned i = 4; i != 8; ++i) {
2075 SDOperand Arg = N->getOperand(i);
2076 if (Arg.getOpcode() == ISD::UNDEF) continue;
2077 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2078 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2079 if (Val < 4 || Val > 7)
2080 return false;
2081 }
2082
2083 return true;
2084}
2085
2086/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2087/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2088bool X86::isPSHUFLWMask(SDNode *N) {
2089 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2090
2091 if (N->getNumOperands() != 8)
2092 return false;
2093
2094 // Upper quadword copied in order.
2095 for (unsigned i = 4; i != 8; ++i)
2096 if (!isUndefOrEqual(N->getOperand(i), i))
2097 return false;
2098
2099 // Lower quadword shuffled.
2100 for (unsigned i = 0; i != 4; ++i)
2101 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2102 return false;
2103
2104 return true;
2105}
2106
2107/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2108/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2109static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2110 if (NumElems != 2 && NumElems != 4) return false;
2111
2112 unsigned Half = NumElems / 2;
2113 for (unsigned i = 0; i < Half; ++i)
2114 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2115 return false;
2116 for (unsigned i = Half; i < NumElems; ++i)
2117 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2118 return false;
2119
2120 return true;
2121}
2122
2123bool X86::isSHUFPMask(SDNode *N) {
2124 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2125 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2126}
2127
2128/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2129/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2130/// half elements to come from vector 1 (which would equal the dest.) and
2131/// the upper half to come from vector 2.
2132static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2133 if (NumOps != 2 && NumOps != 4) return false;
2134
2135 unsigned Half = NumOps / 2;
2136 for (unsigned i = 0; i < Half; ++i)
2137 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2138 return false;
2139 for (unsigned i = Half; i < NumOps; ++i)
2140 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2141 return false;
2142 return true;
2143}
2144
2145static bool isCommutedSHUFP(SDNode *N) {
2146 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2147 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2148}
2149
2150/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2151/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2152bool X86::isMOVHLPSMask(SDNode *N) {
2153 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2154
2155 if (N->getNumOperands() != 4)
2156 return false;
2157
2158 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2159 return isUndefOrEqual(N->getOperand(0), 6) &&
2160 isUndefOrEqual(N->getOperand(1), 7) &&
2161 isUndefOrEqual(N->getOperand(2), 2) &&
2162 isUndefOrEqual(N->getOperand(3), 3);
2163}
2164
2165/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2166/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2167/// <2, 3, 2, 3>
2168bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2169 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2170
2171 if (N->getNumOperands() != 4)
2172 return false;
2173
2174 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2175 return isUndefOrEqual(N->getOperand(0), 2) &&
2176 isUndefOrEqual(N->getOperand(1), 3) &&
2177 isUndefOrEqual(N->getOperand(2), 2) &&
2178 isUndefOrEqual(N->getOperand(3), 3);
2179}
2180
2181/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2182/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2183bool X86::isMOVLPMask(SDNode *N) {
2184 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2185
2186 unsigned NumElems = N->getNumOperands();
2187 if (NumElems != 2 && NumElems != 4)
2188 return false;
2189
2190 for (unsigned i = 0; i < NumElems/2; ++i)
2191 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2192 return false;
2193
2194 for (unsigned i = NumElems/2; i < NumElems; ++i)
2195 if (!isUndefOrEqual(N->getOperand(i), i))
2196 return false;
2197
2198 return true;
2199}
2200
2201/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2202/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2203/// and MOVLHPS.
2204bool X86::isMOVHPMask(SDNode *N) {
2205 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2206
2207 unsigned NumElems = N->getNumOperands();
2208 if (NumElems != 2 && NumElems != 4)
2209 return false;
2210
2211 for (unsigned i = 0; i < NumElems/2; ++i)
2212 if (!isUndefOrEqual(N->getOperand(i), i))
2213 return false;
2214
2215 for (unsigned i = 0; i < NumElems/2; ++i) {
2216 SDOperand Arg = N->getOperand(i + NumElems/2);
2217 if (!isUndefOrEqual(Arg, i + NumElems))
2218 return false;
2219 }
2220
2221 return true;
2222}
2223
2224/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2225/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2226bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2227 bool V2IsSplat = false) {
2228 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2229 return false;
2230
2231 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2232 SDOperand BitI = Elts[i];
2233 SDOperand BitI1 = Elts[i+1];
2234 if (!isUndefOrEqual(BitI, j))
2235 return false;
2236 if (V2IsSplat) {
2237 if (isUndefOrEqual(BitI1, NumElts))
2238 return false;
2239 } else {
2240 if (!isUndefOrEqual(BitI1, j + NumElts))
2241 return false;
2242 }
2243 }
2244
2245 return true;
2246}
2247
2248bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2249 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2250 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2251}
2252
2253/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2254/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2255bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2256 bool V2IsSplat = false) {
2257 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2258 return false;
2259
2260 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2261 SDOperand BitI = Elts[i];
2262 SDOperand BitI1 = Elts[i+1];
2263 if (!isUndefOrEqual(BitI, j + NumElts/2))
2264 return false;
2265 if (V2IsSplat) {
2266 if (isUndefOrEqual(BitI1, NumElts))
2267 return false;
2268 } else {
2269 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2270 return false;
2271 }
2272 }
2273
2274 return true;
2275}
2276
2277bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2278 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2279 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2280}
2281
2282/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2283/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2284/// <0, 0, 1, 1>
2285bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2286 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2287
2288 unsigned NumElems = N->getNumOperands();
2289 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2290 return false;
2291
2292 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2293 SDOperand BitI = N->getOperand(i);
2294 SDOperand BitI1 = N->getOperand(i+1);
2295
2296 if (!isUndefOrEqual(BitI, j))
2297 return false;
2298 if (!isUndefOrEqual(BitI1, j))
2299 return false;
2300 }
2301
2302 return true;
2303}
2304
2305/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2306/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2307/// <2, 2, 3, 3>
2308bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2309 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2310
2311 unsigned NumElems = N->getNumOperands();
2312 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2313 return false;
2314
2315 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2316 SDOperand BitI = N->getOperand(i);
2317 SDOperand BitI1 = N->getOperand(i + 1);
2318
2319 if (!isUndefOrEqual(BitI, j))
2320 return false;
2321 if (!isUndefOrEqual(BitI1, j))
2322 return false;
2323 }
2324
2325 return true;
2326}
2327
2328/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2329/// specifies a shuffle of elements that is suitable for input to MOVSS,
2330/// MOVSD, and MOVD, i.e. setting the lowest element.
2331static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002332 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002333 return false;
2334
2335 if (!isUndefOrEqual(Elts[0], NumElts))
2336 return false;
2337
2338 for (unsigned i = 1; i < NumElts; ++i) {
2339 if (!isUndefOrEqual(Elts[i], i))
2340 return false;
2341 }
2342
2343 return true;
2344}
2345
2346bool X86::isMOVLMask(SDNode *N) {
2347 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2348 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2349}
2350
2351/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2352/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2353/// element of vector 2 and the other elements to come from vector 1 in order.
2354static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2355 bool V2IsSplat = false,
2356 bool V2IsUndef = false) {
2357 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2358 return false;
2359
2360 if (!isUndefOrEqual(Ops[0], 0))
2361 return false;
2362
2363 for (unsigned i = 1; i < NumOps; ++i) {
2364 SDOperand Arg = Ops[i];
2365 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2366 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2367 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2368 return false;
2369 }
2370
2371 return true;
2372}
2373
2374static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2375 bool V2IsUndef = false) {
2376 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2377 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2378 V2IsSplat, V2IsUndef);
2379}
2380
2381/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2382/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2383bool X86::isMOVSHDUPMask(SDNode *N) {
2384 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2385
2386 if (N->getNumOperands() != 4)
2387 return false;
2388
2389 // Expect 1, 1, 3, 3
2390 for (unsigned i = 0; i < 2; ++i) {
2391 SDOperand Arg = N->getOperand(i);
2392 if (Arg.getOpcode() == ISD::UNDEF) continue;
2393 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2394 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2395 if (Val != 1) return false;
2396 }
2397
2398 bool HasHi = false;
2399 for (unsigned i = 2; i < 4; ++i) {
2400 SDOperand Arg = N->getOperand(i);
2401 if (Arg.getOpcode() == ISD::UNDEF) continue;
2402 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2403 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2404 if (Val != 3) return false;
2405 HasHi = true;
2406 }
2407
2408 // Don't use movshdup if it can be done with a shufps.
2409 return HasHi;
2410}
2411
2412/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2413/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2414bool X86::isMOVSLDUPMask(SDNode *N) {
2415 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2416
2417 if (N->getNumOperands() != 4)
2418 return false;
2419
2420 // Expect 0, 0, 2, 2
2421 for (unsigned i = 0; i < 2; ++i) {
2422 SDOperand Arg = N->getOperand(i);
2423 if (Arg.getOpcode() == ISD::UNDEF) continue;
2424 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2425 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2426 if (Val != 0) return false;
2427 }
2428
2429 bool HasHi = false;
2430 for (unsigned i = 2; i < 4; ++i) {
2431 SDOperand Arg = N->getOperand(i);
2432 if (Arg.getOpcode() == ISD::UNDEF) continue;
2433 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2434 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2435 if (Val != 2) return false;
2436 HasHi = true;
2437 }
2438
2439 // Don't use movshdup if it can be done with a shufps.
2440 return HasHi;
2441}
2442
2443/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2444/// specifies a identity operation on the LHS or RHS.
2445static bool isIdentityMask(SDNode *N, bool RHS = false) {
2446 unsigned NumElems = N->getNumOperands();
2447 for (unsigned i = 0; i < NumElems; ++i)
2448 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2449 return false;
2450 return true;
2451}
2452
2453/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2454/// a splat of a single element.
2455static bool isSplatMask(SDNode *N) {
2456 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2457
2458 // This is a splat operation if each element of the permute is the same, and
2459 // if the value doesn't reference the second vector.
2460 unsigned NumElems = N->getNumOperands();
2461 SDOperand ElementBase;
2462 unsigned i = 0;
2463 for (; i != NumElems; ++i) {
2464 SDOperand Elt = N->getOperand(i);
2465 if (isa<ConstantSDNode>(Elt)) {
2466 ElementBase = Elt;
2467 break;
2468 }
2469 }
2470
2471 if (!ElementBase.Val)
2472 return false;
2473
2474 for (; i != NumElems; ++i) {
2475 SDOperand Arg = N->getOperand(i);
2476 if (Arg.getOpcode() == ISD::UNDEF) continue;
2477 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2478 if (Arg != ElementBase) return false;
2479 }
2480
2481 // Make sure it is a splat of the first vector operand.
2482 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2483}
2484
2485/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2486/// a splat of a single element and it's a 2 or 4 element mask.
2487bool X86::isSplatMask(SDNode *N) {
2488 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2489
2490 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2491 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2492 return false;
2493 return ::isSplatMask(N);
2494}
2495
2496/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2497/// specifies a splat of zero element.
2498bool X86::isSplatLoMask(SDNode *N) {
2499 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2500
2501 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2502 if (!isUndefOrEqual(N->getOperand(i), 0))
2503 return false;
2504 return true;
2505}
2506
2507/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2508/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2509/// instructions.
2510unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2511 unsigned NumOperands = N->getNumOperands();
2512 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2513 unsigned Mask = 0;
2514 for (unsigned i = 0; i < NumOperands; ++i) {
2515 unsigned Val = 0;
2516 SDOperand Arg = N->getOperand(NumOperands-i-1);
2517 if (Arg.getOpcode() != ISD::UNDEF)
2518 Val = cast<ConstantSDNode>(Arg)->getValue();
2519 if (Val >= NumOperands) Val -= NumOperands;
2520 Mask |= Val;
2521 if (i != NumOperands - 1)
2522 Mask <<= Shift;
2523 }
2524
2525 return Mask;
2526}
2527
2528/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2529/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2530/// instructions.
2531unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2532 unsigned Mask = 0;
2533 // 8 nodes, but we only care about the last 4.
2534 for (unsigned i = 7; i >= 4; --i) {
2535 unsigned Val = 0;
2536 SDOperand Arg = N->getOperand(i);
2537 if (Arg.getOpcode() != ISD::UNDEF)
2538 Val = cast<ConstantSDNode>(Arg)->getValue();
2539 Mask |= (Val - 4);
2540 if (i != 4)
2541 Mask <<= 2;
2542 }
2543
2544 return Mask;
2545}
2546
2547/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2548/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2549/// instructions.
2550unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2551 unsigned Mask = 0;
2552 // 8 nodes, but we only care about the first 4.
2553 for (int i = 3; i >= 0; --i) {
2554 unsigned Val = 0;
2555 SDOperand Arg = N->getOperand(i);
2556 if (Arg.getOpcode() != ISD::UNDEF)
2557 Val = cast<ConstantSDNode>(Arg)->getValue();
2558 Mask |= Val;
2559 if (i != 0)
2560 Mask <<= 2;
2561 }
2562
2563 return Mask;
2564}
2565
2566/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2567/// specifies a 8 element shuffle that can be broken into a pair of
2568/// PSHUFHW and PSHUFLW.
2569static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2570 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2571
2572 if (N->getNumOperands() != 8)
2573 return false;
2574
2575 // Lower quadword shuffled.
2576 for (unsigned i = 0; i != 4; ++i) {
2577 SDOperand Arg = N->getOperand(i);
2578 if (Arg.getOpcode() == ISD::UNDEF) continue;
2579 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2580 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002581 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582 return false;
2583 }
2584
2585 // Upper quadword shuffled.
2586 for (unsigned i = 4; i != 8; ++i) {
2587 SDOperand Arg = N->getOperand(i);
2588 if (Arg.getOpcode() == ISD::UNDEF) continue;
2589 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2590 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2591 if (Val < 4 || Val > 7)
2592 return false;
2593 }
2594
2595 return true;
2596}
2597
Chris Lattnere6aa3862007-11-25 00:24:49 +00002598/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599/// values in ther permute mask.
2600static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2601 SDOperand &V2, SDOperand &Mask,
2602 SelectionDAG &DAG) {
2603 MVT::ValueType VT = Op.getValueType();
2604 MVT::ValueType MaskVT = Mask.getValueType();
2605 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2606 unsigned NumElems = Mask.getNumOperands();
2607 SmallVector<SDOperand, 8> MaskVec;
2608
2609 for (unsigned i = 0; i != NumElems; ++i) {
2610 SDOperand Arg = Mask.getOperand(i);
2611 if (Arg.getOpcode() == ISD::UNDEF) {
2612 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2613 continue;
2614 }
2615 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2616 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2617 if (Val < NumElems)
2618 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2619 else
2620 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2621 }
2622
2623 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002624 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002625 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2626}
2627
Evan Chenga6769df2007-12-07 21:30:01 +00002628/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2629/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002630static
2631SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2632 MVT::ValueType MaskVT = Mask.getValueType();
2633 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2634 unsigned NumElems = Mask.getNumOperands();
2635 SmallVector<SDOperand, 8> MaskVec;
2636 for (unsigned i = 0; i != NumElems; ++i) {
2637 SDOperand Arg = Mask.getOperand(i);
2638 if (Arg.getOpcode() == ISD::UNDEF) {
2639 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2640 continue;
2641 }
2642 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2643 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2644 if (Val < NumElems)
2645 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2646 else
2647 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2648 }
2649 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2650}
2651
2652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2654/// match movhlps. The lower half elements should come from upper half of
2655/// V1 (and in order), and the upper half elements should come from the upper
2656/// half of V2 (and in order).
2657static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2658 unsigned NumElems = Mask->getNumOperands();
2659 if (NumElems != 4)
2660 return false;
2661 for (unsigned i = 0, e = 2; i != e; ++i)
2662 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2663 return false;
2664 for (unsigned i = 2; i != 4; ++i)
2665 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2666 return false;
2667 return true;
2668}
2669
2670/// isScalarLoadToVector - Returns true if the node is a scalar load that
2671/// is promoted to a vector.
2672static inline bool isScalarLoadToVector(SDNode *N) {
2673 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2674 N = N->getOperand(0).Val;
2675 return ISD::isNON_EXTLoad(N);
2676 }
2677 return false;
2678}
2679
2680/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2681/// match movlp{s|d}. The lower half elements should come from lower half of
2682/// V1 (and in order), and the upper half elements should come from the upper
2683/// half of V2 (and in order). And since V1 will become the source of the
2684/// MOVLP, it must be either a vector load or a scalar load to vector.
2685static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2686 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2687 return false;
2688 // Is V2 is a vector load, don't do this transformation. We will try to use
2689 // load folding shufps op.
2690 if (ISD::isNON_EXTLoad(V2))
2691 return false;
2692
2693 unsigned NumElems = Mask->getNumOperands();
2694 if (NumElems != 2 && NumElems != 4)
2695 return false;
2696 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2697 if (!isUndefOrEqual(Mask->getOperand(i), i))
2698 return false;
2699 for (unsigned i = NumElems/2; i != NumElems; ++i)
2700 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2701 return false;
2702 return true;
2703}
2704
2705/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2706/// all the same.
2707static bool isSplatVector(SDNode *N) {
2708 if (N->getOpcode() != ISD::BUILD_VECTOR)
2709 return false;
2710
2711 SDOperand SplatValue = N->getOperand(0);
2712 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2713 if (N->getOperand(i) != SplatValue)
2714 return false;
2715 return true;
2716}
2717
2718/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2719/// to an undef.
2720static bool isUndefShuffle(SDNode *N) {
2721 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2722 return false;
2723
2724 SDOperand V1 = N->getOperand(0);
2725 SDOperand V2 = N->getOperand(1);
2726 SDOperand Mask = N->getOperand(2);
2727 unsigned NumElems = Mask.getNumOperands();
2728 for (unsigned i = 0; i != NumElems; ++i) {
2729 SDOperand Arg = Mask.getOperand(i);
2730 if (Arg.getOpcode() != ISD::UNDEF) {
2731 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2732 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2733 return false;
2734 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2735 return false;
2736 }
2737 }
2738 return true;
2739}
2740
2741/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2742/// constant +0.0.
2743static inline bool isZeroNode(SDOperand Elt) {
2744 return ((isa<ConstantSDNode>(Elt) &&
2745 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2746 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002747 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748}
2749
2750/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2751/// to an zero vector.
2752static bool isZeroShuffle(SDNode *N) {
2753 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2754 return false;
2755
2756 SDOperand V1 = N->getOperand(0);
2757 SDOperand V2 = N->getOperand(1);
2758 SDOperand Mask = N->getOperand(2);
2759 unsigned NumElems = Mask.getNumOperands();
2760 for (unsigned i = 0; i != NumElems; ++i) {
2761 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002762 if (Arg.getOpcode() == ISD::UNDEF)
2763 continue;
2764
2765 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2766 if (Idx < NumElems) {
2767 unsigned Opc = V1.Val->getOpcode();
2768 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2769 continue;
2770 if (Opc != ISD::BUILD_VECTOR ||
2771 !isZeroNode(V1.Val->getOperand(Idx)))
2772 return false;
2773 } else if (Idx >= NumElems) {
2774 unsigned Opc = V2.Val->getOpcode();
2775 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2776 continue;
2777 if (Opc != ISD::BUILD_VECTOR ||
2778 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2779 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002780 }
2781 }
2782 return true;
2783}
2784
2785/// getZeroVector - Returns a vector of specified type with all zero elements.
2786///
2787static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2788 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002789
2790 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2791 // type. This ensures they get CSE'd.
2792 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2793 SDOperand Vec;
2794 if (MVT::getSizeInBits(VT) == 64) // MMX
2795 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2796 else // SSE
2797 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2798 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002799}
2800
Chris Lattnere6aa3862007-11-25 00:24:49 +00002801/// getOnesVector - Returns a vector of specified type with all bits set.
2802///
2803static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2804 assert(MVT::isVector(VT) && "Expected a vector type");
2805
2806 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2807 // type. This ensures they get CSE'd.
2808 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2809 SDOperand Vec;
2810 if (MVT::getSizeInBits(VT) == 64) // MMX
2811 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2812 else // SSE
2813 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2814 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2815}
2816
2817
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2819/// that point to V2 points to its first element.
2820static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2821 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2822
2823 bool Changed = false;
2824 SmallVector<SDOperand, 8> MaskVec;
2825 unsigned NumElems = Mask.getNumOperands();
2826 for (unsigned i = 0; i != NumElems; ++i) {
2827 SDOperand Arg = Mask.getOperand(i);
2828 if (Arg.getOpcode() != ISD::UNDEF) {
2829 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2830 if (Val > NumElems) {
2831 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2832 Changed = true;
2833 }
2834 }
2835 MaskVec.push_back(Arg);
2836 }
2837
2838 if (Changed)
2839 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2840 &MaskVec[0], MaskVec.size());
2841 return Mask;
2842}
2843
2844/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2845/// operation of specified width.
2846static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2847 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2848 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2849
2850 SmallVector<SDOperand, 8> MaskVec;
2851 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2852 for (unsigned i = 1; i != NumElems; ++i)
2853 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2854 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2855}
2856
2857/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2858/// of specified width.
2859static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2860 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2861 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2862 SmallVector<SDOperand, 8> MaskVec;
2863 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2864 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2865 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2866 }
2867 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2868}
2869
2870/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2871/// of specified width.
2872static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2873 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2874 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2875 unsigned Half = NumElems/2;
2876 SmallVector<SDOperand, 8> MaskVec;
2877 for (unsigned i = 0; i != Half; ++i) {
2878 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2879 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2880 }
2881 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2882}
2883
Chris Lattner2d91b962008-03-09 01:05:04 +00002884/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2885/// element #0 of a vector with the specified index, leaving the rest of the
2886/// elements in place.
2887static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2888 SelectionDAG &DAG) {
2889 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2890 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2891 SmallVector<SDOperand, 8> MaskVec;
2892 // Element #0 of the result gets the elt we are replacing.
2893 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2894 for (unsigned i = 1; i != NumElems; ++i)
2895 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2896 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2897}
2898
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2900///
2901static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2902 SDOperand V1 = Op.getOperand(0);
2903 SDOperand Mask = Op.getOperand(2);
2904 MVT::ValueType VT = Op.getValueType();
2905 unsigned NumElems = Mask.getNumOperands();
2906 Mask = getUnpacklMask(NumElems, DAG);
2907 while (NumElems != 4) {
2908 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2909 NumElems >>= 1;
2910 }
2911 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2912
Chris Lattnere6aa3862007-11-25 00:24:49 +00002913 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2915 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2916 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2917}
2918
2919/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002920/// vector of zero or undef vector. This produces a shuffle where the low
2921/// element of V2 is swizzled into the zero/undef vector, landing at element
2922/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Chris Lattner2d91b962008-03-09 01:05:04 +00002923static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002924 bool isZero, SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002925 MVT::ValueType VT = V2.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002926 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002927 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2929 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002930 SmallVector<SDOperand, 16> MaskVec;
2931 for (unsigned i = 0; i != NumElems; ++i)
2932 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2933 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2934 else
2935 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002936 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2937 &MaskVec[0], MaskVec.size());
2938 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2939}
2940
2941/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2942///
2943static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2944 unsigned NumNonZero, unsigned NumZero,
2945 SelectionDAG &DAG, TargetLowering &TLI) {
2946 if (NumNonZero > 8)
2947 return SDOperand();
2948
2949 SDOperand V(0, 0);
2950 bool First = true;
2951 for (unsigned i = 0; i < 16; ++i) {
2952 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2953 if (ThisIsNonZero && First) {
2954 if (NumZero)
2955 V = getZeroVector(MVT::v8i16, DAG);
2956 else
2957 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2958 First = false;
2959 }
2960
2961 if ((i & 1) != 0) {
2962 SDOperand ThisElt(0, 0), LastElt(0, 0);
2963 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2964 if (LastIsNonZero) {
2965 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2966 }
2967 if (ThisIsNonZero) {
2968 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2969 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2970 ThisElt, DAG.getConstant(8, MVT::i8));
2971 if (LastIsNonZero)
2972 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2973 } else
2974 ThisElt = LastElt;
2975
2976 if (ThisElt.Val)
2977 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002978 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002979 }
2980 }
2981
2982 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2983}
2984
2985/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2986///
2987static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2988 unsigned NumNonZero, unsigned NumZero,
2989 SelectionDAG &DAG, TargetLowering &TLI) {
2990 if (NumNonZero > 4)
2991 return SDOperand();
2992
2993 SDOperand V(0, 0);
2994 bool First = true;
2995 for (unsigned i = 0; i < 8; ++i) {
2996 bool isNonZero = (NonZeros & (1 << i)) != 0;
2997 if (isNonZero) {
2998 if (First) {
2999 if (NumZero)
3000 V = getZeroVector(MVT::v8i16, DAG);
3001 else
3002 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3003 First = false;
3004 }
3005 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003006 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007 }
3008 }
3009
3010 return V;
3011}
3012
3013SDOperand
3014X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003015 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3016 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3017 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3018 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3019 // eliminated on x86-32 hosts.
3020 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3021 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003022
Chris Lattnere6aa3862007-11-25 00:24:49 +00003023 if (ISD::isBuildVectorAllOnes(Op.Val))
3024 return getOnesVector(Op.getValueType(), DAG);
3025 return getZeroVector(Op.getValueType(), DAG);
3026 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003027
3028 MVT::ValueType VT = Op.getValueType();
3029 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3030 unsigned EVTBits = MVT::getSizeInBits(EVT);
3031
3032 unsigned NumElems = Op.getNumOperands();
3033 unsigned NumZero = 0;
3034 unsigned NumNonZero = 0;
3035 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003036 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003037 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003038 for (unsigned i = 0; i < NumElems; ++i) {
3039 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003040 if (Elt.getOpcode() == ISD::UNDEF)
3041 continue;
3042 Values.insert(Elt);
3043 if (Elt.getOpcode() != ISD::Constant &&
3044 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003045 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003046 if (isZeroNode(Elt))
3047 NumZero++;
3048 else {
3049 NonZeros |= (1 << i);
3050 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 }
3052 }
3053
3054 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003055 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3056 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 }
3058
Chris Lattner66a4dda2008-03-09 05:42:06 +00003059 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003060 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061 unsigned Idx = CountTrailingZeros_32(NonZeros);
3062 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003063
Chris Lattner2d91b962008-03-09 01:05:04 +00003064 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3065 // the value are obviously zero, truncate the value to i32 and do the
3066 // insertion that way. Only do this if the value is non-constant or if the
3067 // value is a constant being inserted into element 0. It is cheaper to do
3068 // a constant pool load than it is to do a movd + shuffle.
3069 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3070 (!IsAllConstants || Idx == 0)) {
3071 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3072 // Handle MMX and SSE both.
3073 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3074 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3075
3076 // Truncate the value (which may itself be a constant) to i32, and
3077 // convert it to a vector with movd (S2V+shuffle to zero extend).
3078 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3079 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3080 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3081
3082 // Now we have our 32-bit value zero extended in the low element of
3083 // a vector. If Idx != 0, swizzle it into place.
3084 if (Idx != 0) {
3085 SDOperand Ops[] = {
3086 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3087 getSwapEltZeroMask(VecElts, Idx, DAG)
3088 };
3089 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3090 }
3091 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3092 }
3093 }
3094
Chris Lattnerac914892008-03-08 22:59:52 +00003095 // If we have a constant or non-constant insertion into the low element of
3096 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3097 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3098 // depending on what the source datatype is. Because we can only get here
3099 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3100 if (Idx == 0 &&
3101 // Don't do this for i64 values on x86-32.
3102 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003103 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003104 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003105 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003106 }
3107
3108 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003109 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003110
Chris Lattnerac914892008-03-08 22:59:52 +00003111 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3112 // is a non-constant being inserted into an element other than the low one,
3113 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3114 // movd/movss) to move this into the low element, then shuffle it into
3115 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003117 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3118
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003119 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003120 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003121 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3122 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3123 SmallVector<SDOperand, 8> MaskVec;
3124 for (unsigned i = 0; i < NumElems; i++)
3125 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3126 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3127 &MaskVec[0], MaskVec.size());
3128 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3129 DAG.getNode(ISD::UNDEF, VT), Mask);
3130 }
3131 }
3132
Chris Lattner66a4dda2008-03-09 05:42:06 +00003133 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3134 if (Values.size() == 1)
3135 return SDOperand();
3136
Dan Gohman21463242007-07-24 22:55:08 +00003137 // A vector full of immediates; various special cases are already
3138 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003139 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003140 return SDOperand();
3141
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003142 // Let legalizer expand 2-wide build_vectors.
3143 if (EVTBits == 64)
3144 return SDOperand();
3145
3146 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3147 if (EVTBits == 8 && NumElems == 16) {
3148 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3149 *this);
3150 if (V.Val) return V;
3151 }
3152
3153 if (EVTBits == 16 && NumElems == 8) {
3154 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3155 *this);
3156 if (V.Val) return V;
3157 }
3158
3159 // If element VT is == 32 bits, turn it into a number of shuffles.
3160 SmallVector<SDOperand, 8> V;
3161 V.resize(NumElems);
3162 if (NumElems == 4 && NumZero > 0) {
3163 for (unsigned i = 0; i < 4; ++i) {
3164 bool isZero = !(NonZeros & (1 << i));
3165 if (isZero)
3166 V[i] = getZeroVector(VT, DAG);
3167 else
3168 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3169 }
3170
3171 for (unsigned i = 0; i < 2; ++i) {
3172 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3173 default: break;
3174 case 0:
3175 V[i] = V[i*2]; // Must be a zero vector.
3176 break;
3177 case 1:
3178 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3179 getMOVLMask(NumElems, DAG));
3180 break;
3181 case 2:
3182 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3183 getMOVLMask(NumElems, DAG));
3184 break;
3185 case 3:
3186 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3187 getUnpacklMask(NumElems, DAG));
3188 break;
3189 }
3190 }
3191
3192 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3193 // clears the upper bits.
3194 // FIXME: we can do the same for v4f32 case when we know both parts of
3195 // the lower half come from scalar_to_vector (loadf32). We should do
3196 // that in post legalizer dag combiner with target specific hooks.
3197 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3198 return V[0];
3199 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3200 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3201 SmallVector<SDOperand, 8> MaskVec;
3202 bool Reverse = (NonZeros & 0x3) == 2;
3203 for (unsigned i = 0; i < 2; ++i)
3204 if (Reverse)
3205 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3206 else
3207 MaskVec.push_back(DAG.getConstant(i, EVT));
3208 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3209 for (unsigned i = 0; i < 2; ++i)
3210 if (Reverse)
3211 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3212 else
3213 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3214 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3215 &MaskVec[0], MaskVec.size());
3216 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3217 }
3218
3219 if (Values.size() > 2) {
3220 // Expand into a number of unpckl*.
3221 // e.g. for v4f32
3222 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3223 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3224 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3225 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3226 for (unsigned i = 0; i < NumElems; ++i)
3227 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3228 NumElems >>= 1;
3229 while (NumElems != 0) {
3230 for (unsigned i = 0; i < NumElems; ++i)
3231 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3232 UnpckMask);
3233 NumElems >>= 1;
3234 }
3235 return V[0];
3236 }
3237
3238 return SDOperand();
3239}
3240
Evan Chengfca29242007-12-07 08:07:39 +00003241static
3242SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3243 SDOperand PermMask, SelectionDAG &DAG,
3244 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003245 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003246 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3247 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003248 MVT::ValueType PtrVT = TLI.getPointerTy();
3249 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3250 PermMask.Val->op_end());
3251
3252 // First record which half of which vector the low elements come from.
3253 SmallVector<unsigned, 4> LowQuad(4);
3254 for (unsigned i = 0; i < 4; ++i) {
3255 SDOperand Elt = MaskElts[i];
3256 if (Elt.getOpcode() == ISD::UNDEF)
3257 continue;
3258 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3259 int QuadIdx = EltIdx / 4;
3260 ++LowQuad[QuadIdx];
3261 }
3262 int BestLowQuad = -1;
3263 unsigned MaxQuad = 1;
3264 for (unsigned i = 0; i < 4; ++i) {
3265 if (LowQuad[i] > MaxQuad) {
3266 BestLowQuad = i;
3267 MaxQuad = LowQuad[i];
3268 }
Evan Chengfca29242007-12-07 08:07:39 +00003269 }
3270
Evan Cheng75184a92007-12-11 01:46:18 +00003271 // Record which half of which vector the high elements come from.
3272 SmallVector<unsigned, 4> HighQuad(4);
3273 for (unsigned i = 4; i < 8; ++i) {
3274 SDOperand Elt = MaskElts[i];
3275 if (Elt.getOpcode() == ISD::UNDEF)
3276 continue;
3277 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3278 int QuadIdx = EltIdx / 4;
3279 ++HighQuad[QuadIdx];
3280 }
3281 int BestHighQuad = -1;
3282 MaxQuad = 1;
3283 for (unsigned i = 0; i < 4; ++i) {
3284 if (HighQuad[i] > MaxQuad) {
3285 BestHighQuad = i;
3286 MaxQuad = HighQuad[i];
3287 }
3288 }
3289
3290 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3291 if (BestLowQuad != -1 || BestHighQuad != -1) {
3292 // First sort the 4 chunks in order using shufpd.
3293 SmallVector<SDOperand, 8> MaskVec;
3294 if (BestLowQuad != -1)
3295 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3296 else
3297 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3298 if (BestHighQuad != -1)
3299 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3300 else
3301 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3302 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3303 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3304 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3305 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3306 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3307
3308 // Now sort high and low parts separately.
3309 BitVector InOrder(8);
3310 if (BestLowQuad != -1) {
3311 // Sort lower half in order using PSHUFLW.
3312 MaskVec.clear();
3313 bool AnyOutOrder = false;
3314 for (unsigned i = 0; i != 4; ++i) {
3315 SDOperand Elt = MaskElts[i];
3316 if (Elt.getOpcode() == ISD::UNDEF) {
3317 MaskVec.push_back(Elt);
3318 InOrder.set(i);
3319 } else {
3320 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3321 if (EltIdx != i)
3322 AnyOutOrder = true;
3323 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3324 // If this element is in the right place after this shuffle, then
3325 // remember it.
3326 if ((int)(EltIdx / 4) == BestLowQuad)
3327 InOrder.set(i);
3328 }
3329 }
3330 if (AnyOutOrder) {
3331 for (unsigned i = 4; i != 8; ++i)
3332 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3333 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3334 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3335 }
3336 }
3337
3338 if (BestHighQuad != -1) {
3339 // Sort high half in order using PSHUFHW if possible.
3340 MaskVec.clear();
3341 for (unsigned i = 0; i != 4; ++i)
3342 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3343 bool AnyOutOrder = false;
3344 for (unsigned i = 4; i != 8; ++i) {
3345 SDOperand Elt = MaskElts[i];
3346 if (Elt.getOpcode() == ISD::UNDEF) {
3347 MaskVec.push_back(Elt);
3348 InOrder.set(i);
3349 } else {
3350 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3351 if (EltIdx != i)
3352 AnyOutOrder = true;
3353 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3354 // If this element is in the right place after this shuffle, then
3355 // remember it.
3356 if ((int)(EltIdx / 4) == BestHighQuad)
3357 InOrder.set(i);
3358 }
3359 }
3360 if (AnyOutOrder) {
3361 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3362 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3363 }
3364 }
3365
3366 // The other elements are put in the right place using pextrw and pinsrw.
3367 for (unsigned i = 0; i != 8; ++i) {
3368 if (InOrder[i])
3369 continue;
3370 SDOperand Elt = MaskElts[i];
3371 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3372 if (EltIdx == i)
3373 continue;
3374 SDOperand ExtOp = (EltIdx < 8)
3375 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3376 DAG.getConstant(EltIdx, PtrVT))
3377 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3378 DAG.getConstant(EltIdx - 8, PtrVT));
3379 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3380 DAG.getConstant(i, PtrVT));
3381 }
3382 return NewV;
3383 }
3384
3385 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3386 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003387 // First, let's find out how many elements are already in the right order.
3388 unsigned V1InOrder = 0;
3389 unsigned V1FromV1 = 0;
3390 unsigned V2InOrder = 0;
3391 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003392 SmallVector<SDOperand, 8> V1Elts;
3393 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003394 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003395 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003396 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003397 V1Elts.push_back(Elt);
3398 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003399 ++V1InOrder;
3400 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003401 continue;
3402 }
3403 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3404 if (EltIdx == i) {
3405 V1Elts.push_back(Elt);
3406 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3407 ++V1InOrder;
3408 } else if (EltIdx == i+8) {
3409 V1Elts.push_back(Elt);
3410 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3411 ++V2InOrder;
3412 } else if (EltIdx < 8) {
3413 V1Elts.push_back(Elt);
3414 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003415 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003416 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3417 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003418 }
3419 }
3420
3421 if (V2InOrder > V1InOrder) {
3422 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3423 std::swap(V1, V2);
3424 std::swap(V1Elts, V2Elts);
3425 std::swap(V1FromV1, V2FromV2);
3426 }
3427
Evan Cheng75184a92007-12-11 01:46:18 +00003428 if ((V1FromV1 + V1InOrder) != 8) {
3429 // Some elements are from V2.
3430 if (V1FromV1) {
3431 // If there are elements that are from V1 but out of place,
3432 // then first sort them in place
3433 SmallVector<SDOperand, 8> MaskVec;
3434 for (unsigned i = 0; i < 8; ++i) {
3435 SDOperand Elt = V1Elts[i];
3436 if (Elt.getOpcode() == ISD::UNDEF) {
3437 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3438 continue;
3439 }
3440 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3441 if (EltIdx >= 8)
3442 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3443 else
3444 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3445 }
3446 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3447 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003448 }
Evan Cheng75184a92007-12-11 01:46:18 +00003449
3450 NewV = V1;
3451 for (unsigned i = 0; i < 8; ++i) {
3452 SDOperand Elt = V1Elts[i];
3453 if (Elt.getOpcode() == ISD::UNDEF)
3454 continue;
3455 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3456 if (EltIdx < 8)
3457 continue;
3458 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3459 DAG.getConstant(EltIdx - 8, PtrVT));
3460 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3461 DAG.getConstant(i, PtrVT));
3462 }
3463 return NewV;
3464 } else {
3465 // All elements are from V1.
3466 NewV = V1;
3467 for (unsigned i = 0; i < 8; ++i) {
3468 SDOperand Elt = V1Elts[i];
3469 if (Elt.getOpcode() == ISD::UNDEF)
3470 continue;
3471 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3472 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3473 DAG.getConstant(EltIdx, PtrVT));
3474 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3475 DAG.getConstant(i, PtrVT));
3476 }
3477 return NewV;
3478 }
3479}
3480
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003481/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3482/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3483/// done when every pair / quad of shuffle mask elements point to elements in
3484/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003485/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3486static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003487SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3488 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003489 SDOperand PermMask, SelectionDAG &DAG,
3490 TargetLowering &TLI) {
3491 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003492 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3493 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3494 MVT::ValueType NewVT = MaskVT;
3495 switch (VT) {
3496 case MVT::v4f32: NewVT = MVT::v2f64; break;
3497 case MVT::v4i32: NewVT = MVT::v2i64; break;
3498 case MVT::v8i16: NewVT = MVT::v4i32; break;
3499 case MVT::v16i8: NewVT = MVT::v4i32; break;
3500 default: assert(false && "Unexpected!");
3501 }
3502
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003503 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003504 if (MVT::isInteger(VT))
3505 NewVT = MVT::v2i64;
3506 else
3507 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003508 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003509 unsigned Scale = NumElems / NewWidth;
3510 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003511 for (unsigned i = 0; i < NumElems; i += Scale) {
3512 unsigned StartIdx = ~0U;
3513 for (unsigned j = 0; j < Scale; ++j) {
3514 SDOperand Elt = PermMask.getOperand(i+j);
3515 if (Elt.getOpcode() == ISD::UNDEF)
3516 continue;
3517 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3518 if (StartIdx == ~0U)
3519 StartIdx = EltIdx - (EltIdx % Scale);
3520 if (EltIdx != StartIdx + j)
3521 return SDOperand();
3522 }
3523 if (StartIdx == ~0U)
3524 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3525 else
3526 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003527 }
3528
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003529 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3530 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3531 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3532 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3533 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003534}
3535
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003536SDOperand
3537X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3538 SDOperand V1 = Op.getOperand(0);
3539 SDOperand V2 = Op.getOperand(1);
3540 SDOperand PermMask = Op.getOperand(2);
3541 MVT::ValueType VT = Op.getValueType();
3542 unsigned NumElems = PermMask.getNumOperands();
3543 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3544 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3545 bool V1IsSplat = false;
3546 bool V2IsSplat = false;
3547
3548 if (isUndefShuffle(Op.Val))
3549 return DAG.getNode(ISD::UNDEF, VT);
3550
3551 if (isZeroShuffle(Op.Val))
3552 return getZeroVector(VT, DAG);
3553
3554 if (isIdentityMask(PermMask.Val))
3555 return V1;
3556 else if (isIdentityMask(PermMask.Val, true))
3557 return V2;
3558
3559 if (isSplatMask(PermMask.Val)) {
3560 if (NumElems <= 4) return Op;
3561 // Promote it to a v4i32 splat.
3562 return PromoteSplat(Op, DAG);
3563 }
3564
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003565 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3566 // do it!
3567 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3568 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3569 if (NewOp.Val)
3570 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3571 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3572 // FIXME: Figure out a cleaner way to do this.
3573 // Try to make use of movq to zero out the top part.
3574 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3575 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3576 if (NewOp.Val) {
3577 SDOperand NewV1 = NewOp.getOperand(0);
3578 SDOperand NewV2 = NewOp.getOperand(1);
3579 SDOperand NewMask = NewOp.getOperand(2);
3580 if (isCommutedMOVL(NewMask.Val, true, false)) {
3581 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3582 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3583 NewV1, NewV2, getMOVLMask(2, DAG));
3584 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3585 }
3586 }
3587 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3588 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3589 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3590 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3591 }
3592 }
3593
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003594 if (X86::isMOVLMask(PermMask.Val))
3595 return (V1IsUndef) ? V2 : Op;
3596
3597 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3598 X86::isMOVSLDUPMask(PermMask.Val) ||
3599 X86::isMOVHLPSMask(PermMask.Val) ||
3600 X86::isMOVHPMask(PermMask.Val) ||
3601 X86::isMOVLPMask(PermMask.Val))
3602 return Op;
3603
3604 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3605 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3606 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3607
3608 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003609 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3610 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003611 V1IsSplat = isSplatVector(V1.Val);
3612 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003613
3614 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3616 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3617 std::swap(V1IsSplat, V2IsSplat);
3618 std::swap(V1IsUndef, V2IsUndef);
3619 Commuted = true;
3620 }
3621
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003622 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003623 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3624 if (V2IsUndef) return V1;
3625 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3626 if (V2IsSplat) {
3627 // V2 is a splat, so the mask may be malformed. That is, it may point
3628 // to any V2 element. The instruction selectior won't like this. Get
3629 // a corrected mask and commute to form a proper MOVS{S|D}.
3630 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3631 if (NewMask.Val != PermMask.Val)
3632 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3633 }
3634 return Op;
3635 }
3636
3637 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3638 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3639 X86::isUNPCKLMask(PermMask.Val) ||
3640 X86::isUNPCKHMask(PermMask.Val))
3641 return Op;
3642
3643 if (V2IsSplat) {
3644 // Normalize mask so all entries that point to V2 points to its first
3645 // element then try to match unpck{h|l} again. If match, return a
3646 // new vector_shuffle with the corrected mask.
3647 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3648 if (NewMask.Val != PermMask.Val) {
3649 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3650 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3651 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3652 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3653 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3654 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3655 }
3656 }
3657 }
3658
3659 // Normalize the node to match x86 shuffle ops if needed
3660 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3661 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3662
3663 if (Commuted) {
3664 // Commute is back and try unpck* again.
3665 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3666 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3667 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3668 X86::isUNPCKLMask(PermMask.Val) ||
3669 X86::isUNPCKHMask(PermMask.Val))
3670 return Op;
3671 }
3672
3673 // If VT is integer, try PSHUF* first, then SHUFP*.
3674 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003675 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3676 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3677 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3678 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003679 X86::isPSHUFHWMask(PermMask.Val) ||
3680 X86::isPSHUFLWMask(PermMask.Val)) {
3681 if (V2.getOpcode() != ISD::UNDEF)
3682 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3683 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3684 return Op;
3685 }
3686
3687 if (X86::isSHUFPMask(PermMask.Val) &&
3688 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3689 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003690 } else {
3691 // Floating point cases in the other order.
3692 if (X86::isSHUFPMask(PermMask.Val))
3693 return Op;
3694 if (X86::isPSHUFDMask(PermMask.Val) ||
3695 X86::isPSHUFHWMask(PermMask.Val) ||
3696 X86::isPSHUFLWMask(PermMask.Val)) {
3697 if (V2.getOpcode() != ISD::UNDEF)
3698 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3699 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3700 return Op;
3701 }
3702 }
3703
Evan Cheng75184a92007-12-11 01:46:18 +00003704 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3705 if (VT == MVT::v8i16) {
3706 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3707 if (NewOp.Val)
3708 return NewOp;
3709 }
3710
3711 // Handle all 4 wide cases with a number of shuffles.
3712 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003713 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003714 MVT::ValueType MaskVT = PermMask.getValueType();
3715 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3716 SmallVector<std::pair<int, int>, 8> Locs;
3717 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003718 SmallVector<SDOperand, 8> Mask1(NumElems,
3719 DAG.getNode(ISD::UNDEF, MaskEVT));
3720 SmallVector<SDOperand, 8> Mask2(NumElems,
3721 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003722 unsigned NumHi = 0;
3723 unsigned NumLo = 0;
3724 // If no more than two elements come from either vector. This can be
3725 // implemented with two shuffles. First shuffle gather the elements.
3726 // The second shuffle, which takes the first shuffle as both of its
3727 // vector operands, put the elements into the right order.
3728 for (unsigned i = 0; i != NumElems; ++i) {
3729 SDOperand Elt = PermMask.getOperand(i);
3730 if (Elt.getOpcode() == ISD::UNDEF) {
3731 Locs[i] = std::make_pair(-1, -1);
3732 } else {
3733 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3734 if (Val < NumElems) {
3735 Locs[i] = std::make_pair(0, NumLo);
3736 Mask1[NumLo] = Elt;
3737 NumLo++;
3738 } else {
3739 Locs[i] = std::make_pair(1, NumHi);
3740 if (2+NumHi < NumElems)
3741 Mask1[2+NumHi] = Elt;
3742 NumHi++;
3743 }
3744 }
3745 }
3746 if (NumLo <= 2 && NumHi <= 2) {
3747 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3748 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3749 &Mask1[0], Mask1.size()));
3750 for (unsigned i = 0; i != NumElems; ++i) {
3751 if (Locs[i].first == -1)
3752 continue;
3753 else {
3754 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3755 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3756 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3757 }
3758 }
3759
3760 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3761 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3762 &Mask2[0], Mask2.size()));
3763 }
3764
3765 // Break it into (shuffle shuffle_hi, shuffle_lo).
3766 Locs.clear();
3767 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3768 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3769 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3770 unsigned MaskIdx = 0;
3771 unsigned LoIdx = 0;
3772 unsigned HiIdx = NumElems/2;
3773 for (unsigned i = 0; i != NumElems; ++i) {
3774 if (i == NumElems/2) {
3775 MaskPtr = &HiMask;
3776 MaskIdx = 1;
3777 LoIdx = 0;
3778 HiIdx = NumElems/2;
3779 }
3780 SDOperand Elt = PermMask.getOperand(i);
3781 if (Elt.getOpcode() == ISD::UNDEF) {
3782 Locs[i] = std::make_pair(-1, -1);
3783 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3784 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3785 (*MaskPtr)[LoIdx] = Elt;
3786 LoIdx++;
3787 } else {
3788 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3789 (*MaskPtr)[HiIdx] = Elt;
3790 HiIdx++;
3791 }
3792 }
3793
3794 SDOperand LoShuffle =
3795 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3796 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3797 &LoMask[0], LoMask.size()));
3798 SDOperand HiShuffle =
3799 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3800 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3801 &HiMask[0], HiMask.size()));
3802 SmallVector<SDOperand, 8> MaskOps;
3803 for (unsigned i = 0; i != NumElems; ++i) {
3804 if (Locs[i].first == -1) {
3805 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3806 } else {
3807 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3808 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3809 }
3810 }
3811 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3812 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3813 &MaskOps[0], MaskOps.size()));
3814 }
3815
3816 return SDOperand();
3817}
3818
3819SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003820X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3821 SelectionDAG &DAG) {
3822 MVT::ValueType VT = Op.getValueType();
3823 if (MVT::getSizeInBits(VT) == 8) {
3824 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3825 Op.getOperand(0), Op.getOperand(1));
3826 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3827 DAG.getValueType(VT));
3828 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3829 } else if (MVT::getSizeInBits(VT) == 16) {
3830 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3831 Op.getOperand(0), Op.getOperand(1));
3832 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3833 DAG.getValueType(VT));
3834 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3835 }
3836 return SDOperand();
3837}
3838
3839
3840SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3842 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3843 return SDOperand();
3844
Nate Begemand77e59e2008-02-11 04:19:36 +00003845 if (Subtarget->hasSSE41())
3846 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3847
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003848 MVT::ValueType VT = Op.getValueType();
3849 // TODO: handle v16i8.
3850 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003851 SDOperand Vec = Op.getOperand(0);
3852 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3853 if (Idx == 0)
3854 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3855 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3856 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3857 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003858 // Transform it so it match pextrw which produces a 32-bit result.
3859 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3860 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3861 Op.getOperand(0), Op.getOperand(1));
3862 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3863 DAG.getValueType(VT));
3864 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3865 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003866 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3867 if (Idx == 0)
3868 return Op;
3869 // SHUFPS the element to the lowest double word, then movss.
3870 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3871 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003872 IdxVec.
3873 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3874 IdxVec.
3875 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3876 IdxVec.
3877 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3878 IdxVec.
3879 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003880 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3881 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003882 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003883 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3884 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3885 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003886 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003888 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3889 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3890 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3892 if (Idx == 0)
3893 return Op;
3894
3895 // UNPCKHPD the element to the lowest double word, then movsd.
3896 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3897 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3898 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3899 SmallVector<SDOperand, 8> IdxVec;
3900 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003901 IdxVec.
3902 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003903 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3904 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003905 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3907 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3908 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003909 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003910 }
3911
3912 return SDOperand();
3913}
3914
3915SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003916X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3917 MVT::ValueType VT = Op.getValueType();
3918 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3919
3920 SDOperand N0 = Op.getOperand(0);
3921 SDOperand N1 = Op.getOperand(1);
3922 SDOperand N2 = Op.getOperand(2);
3923
3924 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3925 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3926 : X86ISD::PINSRW;
3927 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3928 // argument.
3929 if (N1.getValueType() != MVT::i32)
3930 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3931 if (N2.getValueType() != MVT::i32)
3932 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3933 return DAG.getNode(Opc, VT, N0, N1, N2);
3934 } else if (EVT == MVT::f32) {
3935 // Bits [7:6] of the constant are the source select. This will always be
3936 // zero here. The DAG Combiner may combine an extract_elt index into these
3937 // bits. For example (insert (extract, 3), 2) could be matched by putting
3938 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3939 // Bits [5:4] of the constant are the destination select. This is the
3940 // value of the incoming immediate.
3941 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3942 // combine either bitwise AND or insert of float 0.0 to set these bits.
3943 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3944 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3945 }
3946 return SDOperand();
3947}
3948
3949SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003950X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003952 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003953
3954 if (Subtarget->hasSSE41())
3955 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3956
Evan Chenge12a7eb2007-12-12 07:55:34 +00003957 if (EVT == MVT::i8)
3958 return SDOperand();
3959
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960 SDOperand N0 = Op.getOperand(0);
3961 SDOperand N1 = Op.getOperand(1);
3962 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003963
3964 if (MVT::getSizeInBits(EVT) == 16) {
3965 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3966 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003967 if (N1.getValueType() != MVT::i32)
3968 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3969 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003970 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003971 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003973 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974}
3975
3976SDOperand
3977X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3978 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003979 MVT::ValueType VT = MVT::v2i32;
3980 switch (Op.getValueType()) {
3981 default: break;
3982 case MVT::v16i8:
3983 case MVT::v8i16:
3984 VT = MVT::v4i32;
3985 break;
3986 }
3987 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3988 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003989}
3990
3991// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3992// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3993// one of the above mentioned nodes. It has to be wrapped because otherwise
3994// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3995// be used to form addressing mode. These wrapped nodes will be selected
3996// into MOV32ri.
3997SDOperand
3998X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3999 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4000 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4001 getPointerTy(),
4002 CP->getAlignment());
4003 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4004 // With PIC, the address is actually $g + Offset.
4005 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4006 !Subtarget->isPICStyleRIPRel()) {
4007 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4008 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4009 Result);
4010 }
4011
4012 return Result;
4013}
4014
4015SDOperand
4016X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4017 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4018 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00004019 // If it's a debug information descriptor, don't mess with it.
4020 if (DAG.isVerifiedDebugInfoDesc(Op))
4021 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4023 // With PIC, the address is actually $g + Offset.
4024 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4025 !Subtarget->isPICStyleRIPRel()) {
4026 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4027 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4028 Result);
4029 }
4030
4031 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4032 // load the value at address GV, not the value of GV itself. This means that
4033 // the GlobalAddress must be in the base or index register of the address, not
4034 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4035 // The same applies for external symbols during PIC codegen
4036 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004037 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004038 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039
4040 return Result;
4041}
4042
4043// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4044static SDOperand
4045LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4046 const MVT::ValueType PtrVT) {
4047 SDOperand InFlag;
4048 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4049 DAG.getNode(X86ISD::GlobalBaseReg,
4050 PtrVT), InFlag);
4051 InFlag = Chain.getValue(1);
4052
4053 // emit leal symbol@TLSGD(,%ebx,1), %eax
4054 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4055 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4056 GA->getValueType(0),
4057 GA->getOffset());
4058 SDOperand Ops[] = { Chain, TGA, InFlag };
4059 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4060 InFlag = Result.getValue(2);
4061 Chain = Result.getValue(1);
4062
4063 // call ___tls_get_addr. This function receives its argument in
4064 // the register EAX.
4065 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4066 InFlag = Chain.getValue(1);
4067
4068 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4069 SDOperand Ops1[] = { Chain,
4070 DAG.getTargetExternalSymbol("___tls_get_addr",
4071 PtrVT),
4072 DAG.getRegister(X86::EAX, PtrVT),
4073 DAG.getRegister(X86::EBX, PtrVT),
4074 InFlag };
4075 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4076 InFlag = Chain.getValue(1);
4077
4078 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4079}
4080
4081// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4082// "local exec" model.
4083static SDOperand
4084LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4085 const MVT::ValueType PtrVT) {
4086 // Get the Thread Pointer
4087 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4088 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4089 // exec)
4090 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4091 GA->getValueType(0),
4092 GA->getOffset());
4093 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4094
4095 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004096 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004097 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098
4099 // The address of the thread local variable is the add of the thread
4100 // pointer with the offset of the variable.
4101 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4102}
4103
4104SDOperand
4105X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4106 // TODO: implement the "local dynamic" model
4107 // TODO: implement the "initial exec"model for pic executables
4108 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4109 "TLS not implemented for non-ELF and 64-bit targets");
4110 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4111 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4112 // otherwise use the "Local Exec"TLS Model
4113 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4114 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4115 else
4116 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4117}
4118
4119SDOperand
4120X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4121 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4122 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4123 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4124 // With PIC, the address is actually $g + Offset.
4125 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4126 !Subtarget->isPICStyleRIPRel()) {
4127 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4128 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4129 Result);
4130 }
4131
4132 return Result;
4133}
4134
4135SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4136 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4137 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4138 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4139 // With PIC, the address is actually $g + Offset.
4140 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4141 !Subtarget->isPICStyleRIPRel()) {
4142 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4143 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4144 Result);
4145 }
4146
4147 return Result;
4148}
4149
Chris Lattner62814a32007-10-17 06:02:13 +00004150/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4151/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004153 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4154 MVT::ValueType VT = Op.getValueType();
4155 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004156 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4157 SDOperand ShOpLo = Op.getOperand(0);
4158 SDOperand ShOpHi = Op.getOperand(1);
4159 SDOperand ShAmt = Op.getOperand(2);
4160 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004161 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4162 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004163
Chris Lattner62814a32007-10-17 06:02:13 +00004164 SDOperand Tmp2, Tmp3;
4165 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004166 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4167 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004168 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004169 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4170 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004171 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172
Chris Lattner62814a32007-10-17 06:02:13 +00004173 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4174 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004175 DAG.getConstant(VTBits, MVT::i8));
4176 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004177 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178
Chris Lattner62814a32007-10-17 06:02:13 +00004179 SDOperand Hi, Lo;
4180 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004181 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004182 SmallVector<SDOperand, 4> Ops;
4183 if (Op.getOpcode() == ISD::SHL_PARTS) {
4184 Ops.push_back(Tmp2);
4185 Ops.push_back(Tmp3);
4186 Ops.push_back(CC);
4187 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004188 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004191 Ops.push_back(Tmp3);
4192 Ops.push_back(Tmp1);
4193 Ops.push_back(CC);
4194 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004195 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004196 } else {
4197 Ops.push_back(Tmp2);
4198 Ops.push_back(Tmp3);
4199 Ops.push_back(CC);
4200 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004201 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004202
4203 Ops.clear();
4204 Ops.push_back(Tmp3);
4205 Ops.push_back(Tmp1);
4206 Ops.push_back(CC);
4207 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004208 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004209 }
4210
Dan Gohman092014e2008-03-03 22:22:09 +00004211 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004212 Ops.clear();
4213 Ops.push_back(Lo);
4214 Ops.push_back(Hi);
4215 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004216}
4217
4218SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004220 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4221 "Unknown SINT_TO_FP to lower!");
4222
4223 // These are really Legal; caller falls through into that case.
4224 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4225 return SDOperand();
4226 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4227 Subtarget->is64Bit())
4228 return SDOperand();
4229
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4231 MachineFunction &MF = DAG.getMachineFunction();
4232 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4233 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4234 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004235 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004236 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004237 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004238
4239 // Build the FILD
4240 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004241 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004242 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4244 else
4245 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4246 SmallVector<SDOperand, 8> Ops;
4247 Ops.push_back(Chain);
4248 Ops.push_back(StackSlot);
4249 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004250 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4251 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252
Dale Johannesen2fc20782007-09-14 22:26:36 +00004253 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254 Chain = Result.getValue(1);
4255 SDOperand InFlag = Result.getValue(2);
4256
4257 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4258 // shouldn't be necessary except that RFP cannot be live across
4259 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4260 MachineFunction &MF = DAG.getMachineFunction();
4261 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4262 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4263 Tys = DAG.getVTList(MVT::Other);
4264 SmallVector<SDOperand, 8> Ops;
4265 Ops.push_back(Chain);
4266 Ops.push_back(Result);
4267 Ops.push_back(StackSlot);
4268 Ops.push_back(DAG.getValueType(Op.getValueType()));
4269 Ops.push_back(InFlag);
4270 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004271 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004272 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004273 }
4274
4275 return Result;
4276}
4277
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004278std::pair<SDOperand,SDOperand> X86TargetLowering::
4279FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004280 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4281 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282
Dale Johannesen2fc20782007-09-14 22:26:36 +00004283 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004284 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004285 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004286 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004287 if (Subtarget->is64Bit() &&
4288 Op.getValueType() == MVT::i64 &&
4289 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004290 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004291
Evan Cheng05441e62007-10-15 20:11:21 +00004292 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4293 // stack slot.
4294 MachineFunction &MF = DAG.getMachineFunction();
4295 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4296 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4297 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004298 unsigned Opc;
4299 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004300 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4301 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4302 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4303 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304 }
4305
4306 SDOperand Chain = DAG.getEntryNode();
4307 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004308 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004309 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004310 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004311 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4313 SDOperand Ops[] = {
4314 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4315 };
4316 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4317 Chain = Value.getValue(1);
4318 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4319 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4320 }
4321
4322 // Build the FP_TO_INT*_IN_MEM
4323 SDOperand Ops[] = { Chain, Value, StackSlot };
4324 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4325
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004326 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004327}
4328
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004329SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004330 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4331 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4332 if (FIST.Val == 0) return SDOperand();
4333
4334 // Load the result.
4335 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4336}
4337
4338SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4339 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4340 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4341 if (FIST.Val == 0) return 0;
4342
4343 // Return an i64 load from the stack slot.
4344 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4345
4346 // Use a MERGE_VALUES node to drop the chain result value.
4347 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4348}
4349
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4351 MVT::ValueType VT = Op.getValueType();
4352 MVT::ValueType EltVT = VT;
4353 if (MVT::isVector(VT))
4354 EltVT = MVT::getVectorElementType(VT);
4355 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4356 std::vector<Constant*> CV;
4357 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004358 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 CV.push_back(C);
4360 CV.push_back(C);
4361 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004362 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363 CV.push_back(C);
4364 CV.push_back(C);
4365 CV.push_back(C);
4366 CV.push_back(C);
4367 }
Dan Gohman11821702007-07-27 17:16:43 +00004368 Constant *C = ConstantVector::get(CV);
4369 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004370 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004371 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004372 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4374}
4375
4376SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4377 MVT::ValueType VT = Op.getValueType();
4378 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004379 unsigned EltNum = 1;
4380 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004381 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004382 EltNum = MVT::getVectorNumElements(VT);
4383 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4385 std::vector<Constant*> CV;
4386 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004387 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 CV.push_back(C);
4389 CV.push_back(C);
4390 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004391 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004392 CV.push_back(C);
4393 CV.push_back(C);
4394 CV.push_back(C);
4395 CV.push_back(C);
4396 }
Dan Gohman11821702007-07-27 17:16:43 +00004397 Constant *C = ConstantVector::get(CV);
4398 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004399 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004400 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004401 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004402 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004403 return DAG.getNode(ISD::BIT_CONVERT, VT,
4404 DAG.getNode(ISD::XOR, MVT::v2i64,
4405 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4406 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4407 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004408 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4409 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410}
4411
4412SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4413 SDOperand Op0 = Op.getOperand(0);
4414 SDOperand Op1 = Op.getOperand(1);
4415 MVT::ValueType VT = Op.getValueType();
4416 MVT::ValueType SrcVT = Op1.getValueType();
4417 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4418
4419 // If second operand is smaller, extend it first.
4420 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4421 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4422 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004423 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004425 // And if it is bigger, shrink it first.
4426 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004427 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004428 SrcVT = VT;
4429 SrcTy = MVT::getTypeForValueType(SrcVT);
4430 }
4431
4432 // At this point the operands and the result should have the same
4433 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004434
4435 // First get the sign bit of second operand.
4436 std::vector<Constant*> CV;
4437 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004438 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4439 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004440 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004441 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4442 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4443 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4444 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445 }
Dan Gohman11821702007-07-27 17:16:43 +00004446 Constant *C = ConstantVector::get(CV);
4447 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004448 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004449 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004450 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4452
4453 // Shift sign bit right or left if the two operands have different types.
4454 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4455 // Op0 is MVT::f32, Op1 is MVT::f64.
4456 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4457 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4458 DAG.getConstant(32, MVT::i32));
4459 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4460 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004461 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 }
4463
4464 // Clear first operand sign bit.
4465 CV.clear();
4466 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004467 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4468 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004469 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004470 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4471 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4472 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4473 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 }
Dan Gohman11821702007-07-27 17:16:43 +00004475 C = ConstantVector::get(CV);
4476 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004477 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004478 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004479 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4481
4482 // Or the value with the sign bit.
4483 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4484}
4485
Evan Cheng621216e2007-09-29 00:00:36 +00004486SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004487 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004488 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004489 SDOperand Op0 = Op.getOperand(0);
4490 SDOperand Op1 = Op.getOperand(1);
4491 SDOperand CC = Op.getOperand(2);
4492 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4493 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4494 unsigned X86CC;
4495
Evan Cheng950aac02007-09-25 01:57:46 +00004496 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004497 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004498 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4499 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004500 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004501 }
Evan Cheng950aac02007-09-25 01:57:46 +00004502
4503 assert(isFP && "Illegal integer SetCC!");
4504
Evan Cheng621216e2007-09-29 00:00:36 +00004505 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004506 switch (SetCCOpcode) {
4507 default: assert(false && "Illegal floating point SetCC!");
4508 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004509 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004510 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004511 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004512 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4513 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4514 }
4515 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004516 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004517 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004518 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004519 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4520 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4521 }
4522 }
4523}
4524
4525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4527 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 SDOperand Cond = Op.getOperand(0);
4529 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530
4531 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004532 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004533
Evan Cheng50d37ab2007-10-08 22:16:29 +00004534 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4535 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004536 if (Cond.getOpcode() == X86ISD::SETCC) {
4537 CC = Cond.getOperand(0);
4538
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539 SDOperand Cmp = Cond.getOperand(1);
4540 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004541 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004542
Evan Cheng50d37ab2007-10-08 22:16:29 +00004543 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004544 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004545 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004546 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004547
Evan Cheng621216e2007-09-29 00:00:36 +00004548 if ((Opc == X86ISD::CMP ||
4549 Opc == X86ISD::COMI ||
4550 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004551 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004552 addTest = false;
4553 }
4554 }
4555
4556 if (addTest) {
4557 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004558 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004559 }
4560
4561 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4562 MVT::Flag);
4563 SmallVector<SDOperand, 4> Ops;
4564 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4565 // condition is true.
4566 Ops.push_back(Op.getOperand(2));
4567 Ops.push_back(Op.getOperand(1));
4568 Ops.push_back(CC);
4569 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004570 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004571}
4572
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4574 bool addTest = true;
4575 SDOperand Chain = Op.getOperand(0);
4576 SDOperand Cond = Op.getOperand(1);
4577 SDOperand Dest = Op.getOperand(2);
4578 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579
4580 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004581 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582
Evan Cheng50d37ab2007-10-08 22:16:29 +00004583 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4584 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 if (Cond.getOpcode() == X86ISD::SETCC) {
4586 CC = Cond.getOperand(0);
4587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004588 SDOperand Cmp = Cond.getOperand(1);
4589 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004590 if (Opc == X86ISD::CMP ||
4591 Opc == X86ISD::COMI ||
4592 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004593 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004594 addTest = false;
4595 }
4596 }
4597
4598 if (addTest) {
4599 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004600 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004601 }
Evan Cheng621216e2007-09-29 00:00:36 +00004602 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004603 Chain, Op.getOperand(2), CC, Cond);
4604}
4605
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004606
4607// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4608// Calls to _alloca is needed to probe the stack when allocating more than 4k
4609// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4610// that the guard pages used by the OS virtual memory manager are allocated in
4611// correct sequence.
4612SDOperand
4613X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4614 SelectionDAG &DAG) {
4615 assert(Subtarget->isTargetCygMing() &&
4616 "This should be used only on Cygwin/Mingw targets");
4617
4618 // Get the inputs.
4619 SDOperand Chain = Op.getOperand(0);
4620 SDOperand Size = Op.getOperand(1);
4621 // FIXME: Ensure alignment here
4622
4623 SDOperand Flag;
4624
4625 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004626 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004627
4628 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4629 Flag = Chain.getValue(1);
4630
4631 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4632 SDOperand Ops[] = { Chain,
4633 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4634 DAG.getRegister(X86::EAX, IntPtr),
4635 Flag };
4636 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4637 Flag = Chain.getValue(1);
4638
4639 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4640
4641 std::vector<MVT::ValueType> Tys;
4642 Tys.push_back(SPTy);
4643 Tys.push_back(MVT::Other);
4644 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4645 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4646}
4647
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004648SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4649 SDOperand InFlag(0, 0);
4650 SDOperand Chain = Op.getOperand(0);
4651 unsigned Align =
4652 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4653 if (Align == 0) Align = 1;
4654
4655 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004656 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004657 // The libc version is likely to be faster for these cases. It can use the
4658 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004660 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661 MVT::ValueType IntPtr = getPointerTy();
4662 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4663 TargetLowering::ArgListTy Args;
4664 TargetLowering::ArgListEntry Entry;
4665 Entry.Node = Op.getOperand(1);
4666 Entry.Ty = IntPtrTy;
4667 Args.push_back(Entry);
4668 // Extend the unsigned i8 argument to be an int value for the call.
4669 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4670 Entry.Ty = IntPtrTy;
4671 Args.push_back(Entry);
4672 Entry.Node = Op.getOperand(3);
4673 Args.push_back(Entry);
4674 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004675 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4676 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 return CallResult.second;
4678 }
4679
4680 MVT::ValueType AVT;
4681 SDOperand Count;
4682 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4683 unsigned BytesLeft = 0;
4684 bool TwoRepStos = false;
4685 if (ValC) {
4686 unsigned ValReg;
4687 uint64_t Val = ValC->getValue() & 255;
4688
4689 // If the value is a constant, then we can potentially use larger sets.
4690 switch (Align & 3) {
4691 case 2: // WORD aligned
4692 AVT = MVT::i16;
4693 ValReg = X86::AX;
4694 Val = (Val << 8) | Val;
4695 break;
4696 case 0: // DWORD aligned
4697 AVT = MVT::i32;
4698 ValReg = X86::EAX;
4699 Val = (Val << 8) | Val;
4700 Val = (Val << 16) | Val;
4701 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4702 AVT = MVT::i64;
4703 ValReg = X86::RAX;
4704 Val = (Val << 32) | Val;
4705 }
4706 break;
4707 default: // Byte aligned
4708 AVT = MVT::i8;
4709 ValReg = X86::AL;
4710 Count = Op.getOperand(3);
4711 break;
4712 }
4713
4714 if (AVT > MVT::i8) {
4715 if (I) {
4716 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004717 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004718 BytesLeft = I->getValue() % UBytes;
4719 } else {
4720 assert(AVT >= MVT::i32 &&
4721 "Do not use rep;stos if not at least DWORD aligned");
4722 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4723 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4724 TwoRepStos = true;
4725 }
4726 }
4727
4728 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4729 InFlag);
4730 InFlag = Chain.getValue(1);
4731 } else {
4732 AVT = MVT::i8;
4733 Count = Op.getOperand(3);
4734 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4735 InFlag = Chain.getValue(1);
4736 }
4737
4738 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4739 Count, InFlag);
4740 InFlag = Chain.getValue(1);
4741 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4742 Op.getOperand(1), InFlag);
4743 InFlag = Chain.getValue(1);
4744
4745 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4746 SmallVector<SDOperand, 8> Ops;
4747 Ops.push_back(Chain);
4748 Ops.push_back(DAG.getValueType(AVT));
4749 Ops.push_back(InFlag);
4750 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4751
4752 if (TwoRepStos) {
4753 InFlag = Chain.getValue(1);
4754 Count = Op.getOperand(3);
4755 MVT::ValueType CVT = Count.getValueType();
4756 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4757 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4758 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4759 Left, InFlag);
4760 InFlag = Chain.getValue(1);
4761 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4762 Ops.clear();
4763 Ops.push_back(Chain);
4764 Ops.push_back(DAG.getValueType(MVT::i8));
4765 Ops.push_back(InFlag);
4766 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4767 } else if (BytesLeft) {
4768 // Issue stores for the last 1 - 7 bytes.
4769 SDOperand Value;
4770 unsigned Val = ValC->getValue() & 255;
4771 unsigned Offset = I->getValue() - BytesLeft;
4772 SDOperand DstAddr = Op.getOperand(1);
4773 MVT::ValueType AddrVT = DstAddr.getValueType();
4774 if (BytesLeft >= 4) {
4775 Val = (Val << 8) | Val;
4776 Val = (Val << 16) | Val;
4777 Value = DAG.getConstant(Val, MVT::i32);
4778 Chain = DAG.getStore(Chain, Value,
4779 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4780 DAG.getConstant(Offset, AddrVT)),
4781 NULL, 0);
4782 BytesLeft -= 4;
4783 Offset += 4;
4784 }
4785 if (BytesLeft >= 2) {
4786 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4787 Chain = DAG.getStore(Chain, Value,
4788 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4789 DAG.getConstant(Offset, AddrVT)),
4790 NULL, 0);
4791 BytesLeft -= 2;
4792 Offset += 2;
4793 }
4794 if (BytesLeft == 1) {
4795 Value = DAG.getConstant(Val, MVT::i8);
4796 Chain = DAG.getStore(Chain, Value,
4797 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4798 DAG.getConstant(Offset, AddrVT)),
4799 NULL, 0);
4800 }
4801 }
4802
4803 return Chain;
4804}
4805
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004806SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4807 SDOperand Dest,
4808 SDOperand Source,
4809 unsigned Size,
4810 unsigned Align,
4811 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004813 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004814 switch (Align & 3) {
4815 case 2: // WORD aligned
4816 AVT = MVT::i16;
4817 break;
4818 case 0: // DWORD aligned
4819 AVT = MVT::i32;
4820 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4821 AVT = MVT::i64;
4822 break;
4823 default: // Byte aligned
4824 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825 break;
4826 }
4827
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004828 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004829 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004830 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004831
4832 SDOperand InFlag(0, 0);
4833 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4834 Count, InFlag);
4835 InFlag = Chain.getValue(1);
4836 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004837 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004838 InFlag = Chain.getValue(1);
4839 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004840 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841 InFlag = Chain.getValue(1);
4842
4843 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4844 SmallVector<SDOperand, 8> Ops;
4845 Ops.push_back(Chain);
4846 Ops.push_back(DAG.getValueType(AVT));
4847 Ops.push_back(InFlag);
4848 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4849
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004850 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004852 unsigned Offset = Size - BytesLeft;
4853 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004854 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004855 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004856 MVT::ValueType SrcVT = SrcAddr.getValueType();
4857 SDOperand Value;
4858 if (BytesLeft >= 4) {
4859 Value = DAG.getLoad(MVT::i32, Chain,
4860 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4861 DAG.getConstant(Offset, SrcVT)),
4862 NULL, 0);
4863 Chain = Value.getValue(1);
4864 Chain = DAG.getStore(Chain, Value,
4865 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4866 DAG.getConstant(Offset, DstVT)),
4867 NULL, 0);
4868 BytesLeft -= 4;
4869 Offset += 4;
4870 }
4871 if (BytesLeft >= 2) {
4872 Value = DAG.getLoad(MVT::i16, Chain,
4873 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4874 DAG.getConstant(Offset, SrcVT)),
4875 NULL, 0);
4876 Chain = Value.getValue(1);
4877 Chain = DAG.getStore(Chain, Value,
4878 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4879 DAG.getConstant(Offset, DstVT)),
4880 NULL, 0);
4881 BytesLeft -= 2;
4882 Offset += 2;
4883 }
4884
4885 if (BytesLeft == 1) {
4886 Value = DAG.getLoad(MVT::i8, Chain,
4887 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4888 DAG.getConstant(Offset, SrcVT)),
4889 NULL, 0);
4890 Chain = Value.getValue(1);
4891 Chain = DAG.getStore(Chain, Value,
4892 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4893 DAG.getConstant(Offset, DstVT)),
4894 NULL, 0);
4895 }
4896 }
4897
4898 return Chain;
4899}
4900
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004901/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4902SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004904 SDOperand TheChain = N->getOperand(0);
4905 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004906 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004907 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4908 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4909 MVT::i64, rax.getValue(2));
4910 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004911 DAG.getConstant(32, MVT::i8));
4912 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004913 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004914 };
4915
4916 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004917 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004918 }
4919
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004920 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4921 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4922 MVT::i32, eax.getValue(2));
4923 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4924 SDOperand Ops[] = { eax, edx };
4925 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4926
4927 // Use a MERGE_VALUES to return the value and chain.
4928 Ops[1] = edx.getValue(1);
4929 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4930 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004931}
4932
4933SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004934 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935
4936 if (!Subtarget->is64Bit()) {
4937 // vastart just stores the address of the VarArgsFrameIndex slot into the
4938 // memory location argument.
4939 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004940 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941 }
4942
4943 // __va_list_tag:
4944 // gp_offset (0 - 6 * 8)
4945 // fp_offset (48 - 48 + 8 * 16)
4946 // overflow_arg_area (point to parameters coming in memory).
4947 // reg_save_area
4948 SmallVector<SDOperand, 8> MemOps;
4949 SDOperand FIN = Op.getOperand(1);
4950 // Store gp_offset
4951 SDOperand Store = DAG.getStore(Op.getOperand(0),
4952 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004953 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954 MemOps.push_back(Store);
4955
4956 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004957 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958 Store = DAG.getStore(Op.getOperand(0),
4959 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004960 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004961 MemOps.push_back(Store);
4962
4963 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004964 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004965 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004966 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004967 MemOps.push_back(Store);
4968
4969 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004970 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004972 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973 MemOps.push_back(Store);
4974 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4975}
4976
4977SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4978 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4979 SDOperand Chain = Op.getOperand(0);
4980 SDOperand DstPtr = Op.getOperand(1);
4981 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004982 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4983 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984
Dan Gohman12a9c082008-02-06 22:27:42 +00004985 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986 Chain = SrcPtr.getValue(1);
4987 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004988 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004990 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991 if (i == 2)
4992 break;
4993 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004994 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004995 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004996 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004997 }
4998 return Chain;
4999}
5000
5001SDOperand
5002X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5003 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5004 switch (IntNo) {
5005 default: return SDOperand(); // Don't custom lower most intrinsics.
5006 // Comparison intrinsics.
5007 case Intrinsic::x86_sse_comieq_ss:
5008 case Intrinsic::x86_sse_comilt_ss:
5009 case Intrinsic::x86_sse_comile_ss:
5010 case Intrinsic::x86_sse_comigt_ss:
5011 case Intrinsic::x86_sse_comige_ss:
5012 case Intrinsic::x86_sse_comineq_ss:
5013 case Intrinsic::x86_sse_ucomieq_ss:
5014 case Intrinsic::x86_sse_ucomilt_ss:
5015 case Intrinsic::x86_sse_ucomile_ss:
5016 case Intrinsic::x86_sse_ucomigt_ss:
5017 case Intrinsic::x86_sse_ucomige_ss:
5018 case Intrinsic::x86_sse_ucomineq_ss:
5019 case Intrinsic::x86_sse2_comieq_sd:
5020 case Intrinsic::x86_sse2_comilt_sd:
5021 case Intrinsic::x86_sse2_comile_sd:
5022 case Intrinsic::x86_sse2_comigt_sd:
5023 case Intrinsic::x86_sse2_comige_sd:
5024 case Intrinsic::x86_sse2_comineq_sd:
5025 case Intrinsic::x86_sse2_ucomieq_sd:
5026 case Intrinsic::x86_sse2_ucomilt_sd:
5027 case Intrinsic::x86_sse2_ucomile_sd:
5028 case Intrinsic::x86_sse2_ucomigt_sd:
5029 case Intrinsic::x86_sse2_ucomige_sd:
5030 case Intrinsic::x86_sse2_ucomineq_sd: {
5031 unsigned Opc = 0;
5032 ISD::CondCode CC = ISD::SETCC_INVALID;
5033 switch (IntNo) {
5034 default: break;
5035 case Intrinsic::x86_sse_comieq_ss:
5036 case Intrinsic::x86_sse2_comieq_sd:
5037 Opc = X86ISD::COMI;
5038 CC = ISD::SETEQ;
5039 break;
5040 case Intrinsic::x86_sse_comilt_ss:
5041 case Intrinsic::x86_sse2_comilt_sd:
5042 Opc = X86ISD::COMI;
5043 CC = ISD::SETLT;
5044 break;
5045 case Intrinsic::x86_sse_comile_ss:
5046 case Intrinsic::x86_sse2_comile_sd:
5047 Opc = X86ISD::COMI;
5048 CC = ISD::SETLE;
5049 break;
5050 case Intrinsic::x86_sse_comigt_ss:
5051 case Intrinsic::x86_sse2_comigt_sd:
5052 Opc = X86ISD::COMI;
5053 CC = ISD::SETGT;
5054 break;
5055 case Intrinsic::x86_sse_comige_ss:
5056 case Intrinsic::x86_sse2_comige_sd:
5057 Opc = X86ISD::COMI;
5058 CC = ISD::SETGE;
5059 break;
5060 case Intrinsic::x86_sse_comineq_ss:
5061 case Intrinsic::x86_sse2_comineq_sd:
5062 Opc = X86ISD::COMI;
5063 CC = ISD::SETNE;
5064 break;
5065 case Intrinsic::x86_sse_ucomieq_ss:
5066 case Intrinsic::x86_sse2_ucomieq_sd:
5067 Opc = X86ISD::UCOMI;
5068 CC = ISD::SETEQ;
5069 break;
5070 case Intrinsic::x86_sse_ucomilt_ss:
5071 case Intrinsic::x86_sse2_ucomilt_sd:
5072 Opc = X86ISD::UCOMI;
5073 CC = ISD::SETLT;
5074 break;
5075 case Intrinsic::x86_sse_ucomile_ss:
5076 case Intrinsic::x86_sse2_ucomile_sd:
5077 Opc = X86ISD::UCOMI;
5078 CC = ISD::SETLE;
5079 break;
5080 case Intrinsic::x86_sse_ucomigt_ss:
5081 case Intrinsic::x86_sse2_ucomigt_sd:
5082 Opc = X86ISD::UCOMI;
5083 CC = ISD::SETGT;
5084 break;
5085 case Intrinsic::x86_sse_ucomige_ss:
5086 case Intrinsic::x86_sse2_ucomige_sd:
5087 Opc = X86ISD::UCOMI;
5088 CC = ISD::SETGE;
5089 break;
5090 case Intrinsic::x86_sse_ucomineq_ss:
5091 case Intrinsic::x86_sse2_ucomineq_sd:
5092 Opc = X86ISD::UCOMI;
5093 CC = ISD::SETNE;
5094 break;
5095 }
5096
5097 unsigned X86CC;
5098 SDOperand LHS = Op.getOperand(1);
5099 SDOperand RHS = Op.getOperand(2);
5100 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5101
Evan Cheng621216e2007-09-29 00:00:36 +00005102 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5103 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5104 DAG.getConstant(X86CC, MVT::i8), Cond);
5105 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005106 }
5107 }
5108}
5109
5110SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5111 // Depths > 0 not supported yet!
5112 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5113 return SDOperand();
5114
5115 // Just load the return address
5116 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5117 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5118}
5119
5120SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5121 // Depths > 0 not supported yet!
5122 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5123 return SDOperand();
5124
5125 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5126 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005127 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128}
5129
5130SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5131 SelectionDAG &DAG) {
5132 // Is not yet supported on x86-64
5133 if (Subtarget->is64Bit())
5134 return SDOperand();
5135
Chris Lattner5872a362008-01-17 07:00:52 +00005136 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137}
5138
5139SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5140{
5141 assert(!Subtarget->is64Bit() &&
5142 "Lowering of eh_return builtin is not supported yet on x86-64");
5143
5144 MachineFunction &MF = DAG.getMachineFunction();
5145 SDOperand Chain = Op.getOperand(0);
5146 SDOperand Offset = Op.getOperand(1);
5147 SDOperand Handler = Op.getOperand(2);
5148
5149 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5150 getPointerTy());
5151
5152 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005153 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005154 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5155 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5156 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005157 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005158
5159 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5160 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5161}
5162
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005163SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5164 SelectionDAG &DAG) {
5165 SDOperand Root = Op.getOperand(0);
5166 SDOperand Trmp = Op.getOperand(1); // trampoline
5167 SDOperand FPtr = Op.getOperand(2); // nested function
5168 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5169
Dan Gohman12a9c082008-02-06 22:27:42 +00005170 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005171
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005172 const X86InstrInfo *TII =
5173 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5174
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005175 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005176 SDOperand OutChains[6];
5177
5178 // Large code-model.
5179
5180 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5181 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5182
5183 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005184 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005185 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005186 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005187
5188 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5189
5190 // Load the pointer to the nested function into R11.
5191 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5192 SDOperand Addr = Trmp;
5193 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005194 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005195
5196 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005197 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005198
5199 // Load the 'nest' parameter value into R10.
5200 // R10 is specified in X86CallingConv.td
5201 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5202 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5203 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005204 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005205
5206 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005207 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005208
5209 // Jump to the nested function.
5210 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5211 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5212 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005213 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005214
5215 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5216 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5217 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005218 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005219
5220 SDOperand Ops[] =
5221 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5222 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005223 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005224 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005225 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5226 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005227 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005228
5229 switch (CC) {
5230 default:
5231 assert(0 && "Unsupported calling convention");
5232 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005233 case CallingConv::X86_StdCall: {
5234 // Pass 'nest' parameter in ECX.
5235 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005236 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005237
5238 // Check that ECX wasn't needed by an 'inreg' parameter.
5239 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005240 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005241
5242 if (Attrs && !Func->isVarArg()) {
5243 unsigned InRegCount = 0;
5244 unsigned Idx = 1;
5245
5246 for (FunctionType::param_iterator I = FTy->param_begin(),
5247 E = FTy->param_end(); I != E; ++I, ++Idx)
5248 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5249 // FIXME: should only count parameters that are lowered to integers.
5250 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5251
5252 if (InRegCount > 2) {
5253 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5254 abort();
5255 }
5256 }
5257 break;
5258 }
5259 case CallingConv::X86_FastCall:
5260 // Pass 'nest' parameter in EAX.
5261 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005262 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005263 break;
5264 }
5265
5266 SDOperand OutChains[4];
5267 SDOperand Addr, Disp;
5268
5269 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5270 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5271
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005272 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5273 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005274 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005275 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005276 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005277
5278 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005279 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005280
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005281 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005282 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5283 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005284 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005285
5286 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005287 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005288
Duncan Sands7407a9f2007-09-11 14:10:23 +00005289 SDOperand Ops[] =
5290 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5291 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005292 }
5293}
5294
Dan Gohman819574c2008-01-31 00:41:03 +00005295SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005296 /*
5297 The rounding mode is in bits 11:10 of FPSR, and has the following
5298 settings:
5299 00 Round to nearest
5300 01 Round to -inf
5301 10 Round to +inf
5302 11 Round to 0
5303
5304 FLT_ROUNDS, on the other hand, expects the following:
5305 -1 Undefined
5306 0 Round to 0
5307 1 Round to nearest
5308 2 Round to +inf
5309 3 Round to -inf
5310
5311 To perform the conversion, we do:
5312 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5313 */
5314
5315 MachineFunction &MF = DAG.getMachineFunction();
5316 const TargetMachine &TM = MF.getTarget();
5317 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5318 unsigned StackAlignment = TFI.getStackAlignment();
5319 MVT::ValueType VT = Op.getValueType();
5320
5321 // Save FP Control Word to stack slot
5322 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5323 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5324
5325 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5326 DAG.getEntryNode(), StackSlot);
5327
5328 // Load FP Control Word from stack slot
5329 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5330
5331 // Transform as necessary
5332 SDOperand CWD1 =
5333 DAG.getNode(ISD::SRL, MVT::i16,
5334 DAG.getNode(ISD::AND, MVT::i16,
5335 CWD, DAG.getConstant(0x800, MVT::i16)),
5336 DAG.getConstant(11, MVT::i8));
5337 SDOperand CWD2 =
5338 DAG.getNode(ISD::SRL, MVT::i16,
5339 DAG.getNode(ISD::AND, MVT::i16,
5340 CWD, DAG.getConstant(0x400, MVT::i16)),
5341 DAG.getConstant(9, MVT::i8));
5342
5343 SDOperand RetVal =
5344 DAG.getNode(ISD::AND, MVT::i16,
5345 DAG.getNode(ISD::ADD, MVT::i16,
5346 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5347 DAG.getConstant(1, MVT::i16)),
5348 DAG.getConstant(3, MVT::i16));
5349
5350
5351 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5352 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5353}
5354
Evan Cheng48679f42007-12-14 02:13:44 +00005355SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5356 MVT::ValueType VT = Op.getValueType();
5357 MVT::ValueType OpVT = VT;
5358 unsigned NumBits = MVT::getSizeInBits(VT);
5359
5360 Op = Op.getOperand(0);
5361 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005362 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005363 OpVT = MVT::i32;
5364 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5365 }
Evan Cheng48679f42007-12-14 02:13:44 +00005366
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005367 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5368 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5369 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5370
5371 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5372 SmallVector<SDOperand, 4> Ops;
5373 Ops.push_back(Op);
5374 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5375 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5376 Ops.push_back(Op.getValue(1));
5377 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5378
5379 // Finally xor with NumBits-1.
5380 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5381
Evan Cheng48679f42007-12-14 02:13:44 +00005382 if (VT == MVT::i8)
5383 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5384 return Op;
5385}
5386
5387SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5388 MVT::ValueType VT = Op.getValueType();
5389 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005390 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005391
5392 Op = Op.getOperand(0);
5393 if (VT == MVT::i8) {
5394 OpVT = MVT::i32;
5395 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5396 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005397
5398 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5399 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5400 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5401
5402 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5403 SmallVector<SDOperand, 4> Ops;
5404 Ops.push_back(Op);
5405 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5406 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5407 Ops.push_back(Op.getValue(1));
5408 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5409
Evan Cheng48679f42007-12-14 02:13:44 +00005410 if (VT == MVT::i8)
5411 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5412 return Op;
5413}
5414
Andrew Lenharth81580822008-03-05 01:15:49 +00005415SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005416 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005417 unsigned Reg = 0;
5418 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005419 switch(T) {
5420 case MVT::i8: Reg = X86::AL; size = 1; break;
5421 case MVT::i16: Reg = X86::AX; size = 2; break;
5422 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005423 case MVT::i64:
5424 if (Subtarget->is64Bit()) {
5425 Reg = X86::RAX; size = 8;
5426 } else //Should go away when LowerType stuff lands
5427 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5428 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005429 };
5430 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005431 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005432 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005433 Op.getOperand(1),
5434 Op.getOperand(2),
5435 DAG.getTargetConstant(size, MVT::i8),
5436 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005437 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5438 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5439 SDOperand cpOut =
5440 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5441 return cpOut;
5442}
5443
Andrew Lenharth81580822008-03-05 01:15:49 +00005444SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5445 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5446 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5447 SDOperand cpInL, cpInH;
5448 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5449 DAG.getConstant(0, MVT::i32));
5450 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5451 DAG.getConstant(1, MVT::i32));
5452 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5453 cpInL, SDOperand());
5454 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5455 cpInH, cpInL.getValue(1));
5456 SDOperand swapInL, swapInH;
5457 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5458 DAG.getConstant(0, MVT::i32));
5459 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5460 DAG.getConstant(1, MVT::i32));
5461 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5462 swapInL, cpInH.getValue(1));
5463 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5464 swapInH, swapInL.getValue(1));
5465 SDOperand Ops[] = { swapInH.getValue(0),
5466 Op->getOperand(1),
5467 swapInH.getValue(1)};
5468 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5469 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5470 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5471 Result.getValue(1));
5472 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5473 cpOutL.getValue(2));
5474 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5475 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5476 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5477 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5478}
5479
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005480/// LowerOperation - Provide custom lowering hooks for some operations.
5481///
5482SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5483 switch (Op.getOpcode()) {
5484 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005485 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005486 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5487 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5488 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5489 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5490 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5491 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5492 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5493 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5494 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5495 case ISD::SHL_PARTS:
5496 case ISD::SRA_PARTS:
5497 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5498 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5499 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5500 case ISD::FABS: return LowerFABS(Op, DAG);
5501 case ISD::FNEG: return LowerFNEG(Op, DAG);
5502 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005503 case ISD::SETCC: return LowerSETCC(Op, DAG);
5504 case ISD::SELECT: return LowerSELECT(Op, DAG);
5505 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005506 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5507 case ISD::CALL: return LowerCALL(Op, DAG);
5508 case ISD::RET: return LowerRET(Op, DAG);
5509 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5510 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5511 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005512 case ISD::VASTART: return LowerVASTART(Op, DAG);
5513 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5514 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5515 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5516 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5517 case ISD::FRAME_TO_ARGS_OFFSET:
5518 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5519 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5520 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005521 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005522 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005523 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5524 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005525
5526 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5527 case ISD::READCYCLECOUNTER:
5528 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005530}
5531
5532/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5533SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5534 switch (N->getOpcode()) {
5535 default: assert(0 && "Should not custom lower this!");
5536 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5537 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005538 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005539 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005540}
5541
5542const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5543 switch (Opcode) {
5544 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005545 case X86ISD::BSF: return "X86ISD::BSF";
5546 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005547 case X86ISD::SHLD: return "X86ISD::SHLD";
5548 case X86ISD::SHRD: return "X86ISD::SHRD";
5549 case X86ISD::FAND: return "X86ISD::FAND";
5550 case X86ISD::FOR: return "X86ISD::FOR";
5551 case X86ISD::FXOR: return "X86ISD::FXOR";
5552 case X86ISD::FSRL: return "X86ISD::FSRL";
5553 case X86ISD::FILD: return "X86ISD::FILD";
5554 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5555 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5556 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5557 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5558 case X86ISD::FLD: return "X86ISD::FLD";
5559 case X86ISD::FST: return "X86ISD::FST";
Chris Lattner5d294e52008-03-09 07:05:32 +00005560 case X86ISD::FP_GET_ST0_ST1: return "X86ISD::FP_GET_ST0_ST1";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005561 case X86ISD::CALL: return "X86ISD::CALL";
5562 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5563 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5564 case X86ISD::CMP: return "X86ISD::CMP";
5565 case X86ISD::COMI: return "X86ISD::COMI";
5566 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5567 case X86ISD::SETCC: return "X86ISD::SETCC";
5568 case X86ISD::CMOV: return "X86ISD::CMOV";
5569 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5570 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5571 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5572 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005573 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5574 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005575 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005577 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5578 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5580 case X86ISD::FMAX: return "X86ISD::FMAX";
5581 case X86ISD::FMIN: return "X86ISD::FMIN";
5582 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5583 case X86ISD::FRCP: return "X86ISD::FRCP";
5584 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5585 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5586 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005587 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005588 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005589 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005590 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591 }
5592}
5593
5594// isLegalAddressingMode - Return true if the addressing mode represented
5595// by AM is legal for this target, for a load/store of the specified type.
5596bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5597 const Type *Ty) const {
5598 // X86 supports extremely general addressing modes.
5599
5600 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5601 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5602 return false;
5603
5604 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005605 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005606 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5607 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005608
5609 // X86-64 only supports addr of globals in small code model.
5610 if (Subtarget->is64Bit()) {
5611 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5612 return false;
5613 // If lower 4G is not available, then we must use rip-relative addressing.
5614 if (AM.BaseOffs || AM.Scale > 1)
5615 return false;
5616 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005617 }
5618
5619 switch (AM.Scale) {
5620 case 0:
5621 case 1:
5622 case 2:
5623 case 4:
5624 case 8:
5625 // These scales always work.
5626 break;
5627 case 3:
5628 case 5:
5629 case 9:
5630 // These scales are formed with basereg+scalereg. Only accept if there is
5631 // no basereg yet.
5632 if (AM.HasBaseReg)
5633 return false;
5634 break;
5635 default: // Other stuff never works.
5636 return false;
5637 }
5638
5639 return true;
5640}
5641
5642
Evan Cheng27a820a2007-10-26 01:56:11 +00005643bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5644 if (!Ty1->isInteger() || !Ty2->isInteger())
5645 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005646 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5647 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5648 if (NumBits1 <= NumBits2)
5649 return false;
5650 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005651}
5652
Evan Cheng9decb332007-10-29 19:58:20 +00005653bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5654 MVT::ValueType VT2) const {
5655 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5656 return false;
5657 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5658 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5659 if (NumBits1 <= NumBits2)
5660 return false;
5661 return Subtarget->is64Bit() || NumBits1 < 64;
5662}
Evan Cheng27a820a2007-10-26 01:56:11 +00005663
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005664/// isShuffleMaskLegal - Targets can use this to indicate that they only
5665/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5666/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5667/// are assumed to be legal.
5668bool
5669X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5670 // Only do shuffles on 128-bit vector types for now.
5671 if (MVT::getSizeInBits(VT) == 64) return false;
5672 return (Mask.Val->getNumOperands() <= 4 ||
5673 isIdentityMask(Mask.Val) ||
5674 isIdentityMask(Mask.Val, true) ||
5675 isSplatMask(Mask.Val) ||
5676 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5677 X86::isUNPCKLMask(Mask.Val) ||
5678 X86::isUNPCKHMask(Mask.Val) ||
5679 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5680 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5681}
5682
5683bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5684 MVT::ValueType EVT,
5685 SelectionDAG &DAG) const {
5686 unsigned NumElts = BVOps.size();
5687 // Only do shuffles on 128-bit vector types for now.
5688 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5689 if (NumElts == 2) return true;
5690 if (NumElts == 4) {
5691 return (isMOVLMask(&BVOps[0], 4) ||
5692 isCommutedMOVL(&BVOps[0], 4, true) ||
5693 isSHUFPMask(&BVOps[0], 4) ||
5694 isCommutedSHUFP(&BVOps[0], 4));
5695 }
5696 return false;
5697}
5698
5699//===----------------------------------------------------------------------===//
5700// X86 Scheduler Hooks
5701//===----------------------------------------------------------------------===//
5702
5703MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005704X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5705 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005706 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5707 switch (MI->getOpcode()) {
5708 default: assert(false && "Unexpected instr type to insert");
5709 case X86::CMOV_FR32:
5710 case X86::CMOV_FR64:
5711 case X86::CMOV_V4F32:
5712 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005713 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005714 // To "insert" a SELECT_CC instruction, we actually have to insert the
5715 // diamond control-flow pattern. The incoming instruction knows the
5716 // destination vreg to set, the condition code register to branch on, the
5717 // true/false values to select between, and a branch opcode to use.
5718 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5719 ilist<MachineBasicBlock>::iterator It = BB;
5720 ++It;
5721
5722 // thisMBB:
5723 // ...
5724 // TrueVal = ...
5725 // cmpTY ccX, r1, r2
5726 // bCC copy1MBB
5727 // fallthrough --> copy0MBB
5728 MachineBasicBlock *thisMBB = BB;
5729 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5730 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5731 unsigned Opc =
5732 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5733 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5734 MachineFunction *F = BB->getParent();
5735 F->getBasicBlockList().insert(It, copy0MBB);
5736 F->getBasicBlockList().insert(It, sinkMBB);
5737 // Update machine-CFG edges by first adding all successors of the current
5738 // block to the new block which will contain the Phi node for the select.
5739 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5740 e = BB->succ_end(); i != e; ++i)
5741 sinkMBB->addSuccessor(*i);
5742 // Next, remove all successors of the current block, and add the true
5743 // and fallthrough blocks as its successors.
5744 while(!BB->succ_empty())
5745 BB->removeSuccessor(BB->succ_begin());
5746 BB->addSuccessor(copy0MBB);
5747 BB->addSuccessor(sinkMBB);
5748
5749 // copy0MBB:
5750 // %FalseValue = ...
5751 // # fallthrough to sinkMBB
5752 BB = copy0MBB;
5753
5754 // Update machine-CFG edges
5755 BB->addSuccessor(sinkMBB);
5756
5757 // sinkMBB:
5758 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5759 // ...
5760 BB = sinkMBB;
5761 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5762 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5763 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5764
5765 delete MI; // The pseudo instruction is gone now.
5766 return BB;
5767 }
5768
5769 case X86::FP32_TO_INT16_IN_MEM:
5770 case X86::FP32_TO_INT32_IN_MEM:
5771 case X86::FP32_TO_INT64_IN_MEM:
5772 case X86::FP64_TO_INT16_IN_MEM:
5773 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005774 case X86::FP64_TO_INT64_IN_MEM:
5775 case X86::FP80_TO_INT16_IN_MEM:
5776 case X86::FP80_TO_INT32_IN_MEM:
5777 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005778 // Change the floating point control register to use "round towards zero"
5779 // mode when truncating to an integer value.
5780 MachineFunction *F = BB->getParent();
5781 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5782 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5783
5784 // Load the old value of the high byte of the control word...
5785 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005786 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5788
5789 // Set the high part to be round to zero...
5790 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5791 .addImm(0xC7F);
5792
5793 // Reload the modified control word now...
5794 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5795
5796 // Restore the memory image of control word to original value
5797 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5798 .addReg(OldCW);
5799
5800 // Get the X86 opcode to use.
5801 unsigned Opc;
5802 switch (MI->getOpcode()) {
5803 default: assert(0 && "illegal opcode!");
5804 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5805 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5806 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5807 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5808 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5809 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005810 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5811 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5812 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005813 }
5814
5815 X86AddressMode AM;
5816 MachineOperand &Op = MI->getOperand(0);
5817 if (Op.isRegister()) {
5818 AM.BaseType = X86AddressMode::RegBase;
5819 AM.Base.Reg = Op.getReg();
5820 } else {
5821 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005822 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005823 }
5824 Op = MI->getOperand(1);
5825 if (Op.isImmediate())
5826 AM.Scale = Op.getImm();
5827 Op = MI->getOperand(2);
5828 if (Op.isImmediate())
5829 AM.IndexReg = Op.getImm();
5830 Op = MI->getOperand(3);
5831 if (Op.isGlobalAddress()) {
5832 AM.GV = Op.getGlobal();
5833 } else {
5834 AM.Disp = Op.getImm();
5835 }
5836 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5837 .addReg(MI->getOperand(4).getReg());
5838
5839 // Reload the original control word now.
5840 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5841
5842 delete MI; // The pseudo instruction is gone now.
5843 return BB;
5844 }
5845 }
5846}
5847
5848//===----------------------------------------------------------------------===//
5849// X86 Optimization Hooks
5850//===----------------------------------------------------------------------===//
5851
5852void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005853 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005854 APInt &KnownZero,
5855 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856 const SelectionDAG &DAG,
5857 unsigned Depth) const {
5858 unsigned Opc = Op.getOpcode();
5859 assert((Opc >= ISD::BUILTIN_OP_END ||
5860 Opc == ISD::INTRINSIC_WO_CHAIN ||
5861 Opc == ISD::INTRINSIC_W_CHAIN ||
5862 Opc == ISD::INTRINSIC_VOID) &&
5863 "Should use MaskedValueIsZero if you don't know whether Op"
5864 " is a target node!");
5865
Dan Gohman1d79e432008-02-13 23:07:24 +00005866 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005867 switch (Opc) {
5868 default: break;
5869 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005870 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5871 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005872 break;
5873 }
5874}
5875
5876/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5877/// element of the result of the vector shuffle.
5878static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5879 MVT::ValueType VT = N->getValueType(0);
5880 SDOperand PermMask = N->getOperand(2);
5881 unsigned NumElems = PermMask.getNumOperands();
5882 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5883 i %= NumElems;
5884 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5885 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005886 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005887 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5888 SDOperand Idx = PermMask.getOperand(i);
5889 if (Idx.getOpcode() == ISD::UNDEF)
5890 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5891 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5892 }
5893 return SDOperand();
5894}
5895
5896/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5897/// node is a GlobalAddress + an offset.
5898static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5899 unsigned Opc = N->getOpcode();
5900 if (Opc == X86ISD::Wrapper) {
5901 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5902 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5903 return true;
5904 }
5905 } else if (Opc == ISD::ADD) {
5906 SDOperand N1 = N->getOperand(0);
5907 SDOperand N2 = N->getOperand(1);
5908 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5909 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5910 if (V) {
5911 Offset += V->getSignExtended();
5912 return true;
5913 }
5914 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5915 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5916 if (V) {
5917 Offset += V->getSignExtended();
5918 return true;
5919 }
5920 }
5921 }
5922 return false;
5923}
5924
5925/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5926/// + Dist * Size.
5927static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5928 MachineFrameInfo *MFI) {
5929 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5930 return false;
5931
5932 SDOperand Loc = N->getOperand(1);
5933 SDOperand BaseLoc = Base->getOperand(1);
5934 if (Loc.getOpcode() == ISD::FrameIndex) {
5935 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5936 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005937 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5938 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005939 int FS = MFI->getObjectSize(FI);
5940 int BFS = MFI->getObjectSize(BFI);
5941 if (FS != BFS || FS != Size) return false;
5942 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5943 } else {
5944 GlobalValue *GV1 = NULL;
5945 GlobalValue *GV2 = NULL;
5946 int64_t Offset1 = 0;
5947 int64_t Offset2 = 0;
5948 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5949 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5950 if (isGA1 && isGA2 && GV1 == GV2)
5951 return Offset1 == (Offset2 + Dist*Size);
5952 }
5953
5954 return false;
5955}
5956
5957static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5958 const X86Subtarget *Subtarget) {
5959 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005960 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961 if (isGAPlusOffset(Base, GV, Offset))
5962 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005963 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005964 return false;
5965}
5966
5967
5968/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5969/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5970/// if the load addresses are consecutive, non-overlapping, and in the right
5971/// order.
5972static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5973 const X86Subtarget *Subtarget) {
5974 MachineFunction &MF = DAG.getMachineFunction();
5975 MachineFrameInfo *MFI = MF.getFrameInfo();
5976 MVT::ValueType VT = N->getValueType(0);
5977 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5978 SDOperand PermMask = N->getOperand(2);
5979 int NumElems = (int)PermMask.getNumOperands();
5980 SDNode *Base = NULL;
5981 for (int i = 0; i < NumElems; ++i) {
5982 SDOperand Idx = PermMask.getOperand(i);
5983 if (Idx.getOpcode() == ISD::UNDEF) {
5984 if (!Base) return SDOperand();
5985 } else {
5986 SDOperand Arg =
5987 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5988 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5989 return SDOperand();
5990 if (!Base)
5991 Base = Arg.Val;
5992 else if (!isConsecutiveLoad(Arg.Val, Base,
5993 i, MVT::getSizeInBits(EVT)/8,MFI))
5994 return SDOperand();
5995 }
5996 }
5997
5998 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005999 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006000 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006001 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006002 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006003 } else {
Dan Gohman11821702007-07-27 17:16:43 +00006004 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6005 LD->getSrcValueOffset(), LD->isVolatile(),
6006 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006007 }
6008}
6009
6010/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6011static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6012 const X86Subtarget *Subtarget) {
6013 SDOperand Cond = N->getOperand(0);
6014
6015 // If we have SSE[12] support, try to form min/max nodes.
6016 if (Subtarget->hasSSE2() &&
6017 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6018 if (Cond.getOpcode() == ISD::SETCC) {
6019 // Get the LHS/RHS of the select.
6020 SDOperand LHS = N->getOperand(1);
6021 SDOperand RHS = N->getOperand(2);
6022 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6023
6024 unsigned Opcode = 0;
6025 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6026 switch (CC) {
6027 default: break;
6028 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6029 case ISD::SETULE:
6030 case ISD::SETLE:
6031 if (!UnsafeFPMath) break;
6032 // FALL THROUGH.
6033 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6034 case ISD::SETLT:
6035 Opcode = X86ISD::FMIN;
6036 break;
6037
6038 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6039 case ISD::SETUGT:
6040 case ISD::SETGT:
6041 if (!UnsafeFPMath) break;
6042 // FALL THROUGH.
6043 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6044 case ISD::SETGE:
6045 Opcode = X86ISD::FMAX;
6046 break;
6047 }
6048 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6049 switch (CC) {
6050 default: break;
6051 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6052 case ISD::SETUGT:
6053 case ISD::SETGT:
6054 if (!UnsafeFPMath) break;
6055 // FALL THROUGH.
6056 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6057 case ISD::SETGE:
6058 Opcode = X86ISD::FMIN;
6059 break;
6060
6061 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6062 case ISD::SETULE:
6063 case ISD::SETLE:
6064 if (!UnsafeFPMath) break;
6065 // FALL THROUGH.
6066 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6067 case ISD::SETLT:
6068 Opcode = X86ISD::FMAX;
6069 break;
6070 }
6071 }
6072
6073 if (Opcode)
6074 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6075 }
6076
6077 }
6078
6079 return SDOperand();
6080}
6081
Chris Lattnerce84ae42008-02-22 02:09:43 +00006082/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6083static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6084 const X86Subtarget *Subtarget) {
6085 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6086 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006087 // A preferable solution to the general problem is to figure out the right
6088 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00006089 if (MVT::isVector(St->getValue().getValueType()) &&
6090 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006091 isa<LoadSDNode>(St->getValue()) &&
6092 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6093 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006094 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006095 LoadSDNode *Ld = 0;
6096 int TokenFactorIndex = -1;
6097 SmallVector<SDOperand, 8> Ops;
6098 SDNode* ChainVal = St->getChain().Val;
6099 // Must be a store of a load. We currently handle two cases: the load
6100 // is a direct child, and it's under an intervening TokenFactor. It is
6101 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006102 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006103 Ld = cast<LoadSDNode>(St->getChain());
6104 else if (St->getValue().hasOneUse() &&
6105 ChainVal->getOpcode() == ISD::TokenFactor) {
6106 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006107 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006108 TokenFactorIndex = i;
6109 Ld = cast<LoadSDNode>(St->getValue());
6110 } else
6111 Ops.push_back(ChainVal->getOperand(i));
6112 }
6113 }
6114 if (Ld) {
6115 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6116 if (Subtarget->is64Bit()) {
6117 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6118 Ld->getBasePtr(), Ld->getSrcValue(),
6119 Ld->getSrcValueOffset(), Ld->isVolatile(),
6120 Ld->getAlignment());
6121 SDOperand NewChain = NewLd.getValue(1);
6122 if (TokenFactorIndex != -1) {
6123 Ops.push_back(NewLd);
6124 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6125 Ops.size());
6126 }
6127 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6128 St->getSrcValue(), St->getSrcValueOffset(),
6129 St->isVolatile(), St->getAlignment());
6130 }
6131
6132 // Otherwise, lower to two 32-bit copies.
6133 SDOperand LoAddr = Ld->getBasePtr();
6134 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6135 DAG.getConstant(MVT::i32, 4));
6136
6137 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6138 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6139 Ld->isVolatile(), Ld->getAlignment());
6140 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6141 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6142 Ld->isVolatile(),
6143 MinAlign(Ld->getAlignment(), 4));
6144
6145 SDOperand NewChain = LoLd.getValue(1);
6146 if (TokenFactorIndex != -1) {
6147 Ops.push_back(LoLd);
6148 Ops.push_back(HiLd);
6149 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6150 Ops.size());
6151 }
6152
6153 LoAddr = St->getBasePtr();
6154 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6155 DAG.getConstant(MVT::i32, 4));
6156
6157 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006158 St->getSrcValue(), St->getSrcValueOffset(),
6159 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006160 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6161 St->getSrcValue(), St->getSrcValueOffset()+4,
6162 St->isVolatile(),
6163 MinAlign(St->getAlignment(), 4));
6164 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006165 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006166 }
6167 return SDOperand();
6168}
6169
Chris Lattner470d5dc2008-01-25 06:14:17 +00006170/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6171/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006172static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006173 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6174 // F[X]OR(0.0, x) -> x
6175 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006176 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6177 if (C->getValueAPF().isPosZero())
6178 return N->getOperand(1);
6179 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6180 if (C->getValueAPF().isPosZero())
6181 return N->getOperand(0);
6182 return SDOperand();
6183}
6184
6185/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6186static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6187 // FAND(0.0, x) -> 0.0
6188 // FAND(x, 0.0) -> 0.0
6189 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6190 if (C->getValueAPF().isPosZero())
6191 return N->getOperand(0);
6192 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6193 if (C->getValueAPF().isPosZero())
6194 return N->getOperand(1);
6195 return SDOperand();
6196}
6197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006198
6199SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6200 DAGCombinerInfo &DCI) const {
6201 SelectionDAG &DAG = DCI.DAG;
6202 switch (N->getOpcode()) {
6203 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006204 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6205 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006206 case ISD::STORE:
6207 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006208 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006209 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6210 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006211 }
6212
6213 return SDOperand();
6214}
6215
6216//===----------------------------------------------------------------------===//
6217// X86 Inline Assembly Support
6218//===----------------------------------------------------------------------===//
6219
6220/// getConstraintType - Given a constraint letter, return the type of
6221/// constraint it is for this target.
6222X86TargetLowering::ConstraintType
6223X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6224 if (Constraint.size() == 1) {
6225 switch (Constraint[0]) {
6226 case 'A':
6227 case 'r':
6228 case 'R':
6229 case 'l':
6230 case 'q':
6231 case 'Q':
6232 case 'x':
6233 case 'Y':
6234 return C_RegisterClass;
6235 default:
6236 break;
6237 }
6238 }
6239 return TargetLowering::getConstraintType(Constraint);
6240}
6241
Dale Johannesene99fc902008-01-29 02:21:21 +00006242/// LowerXConstraint - try to replace an X constraint, which matches anything,
6243/// with another that has more specific requirements based on the type of the
6244/// corresponding operand.
6245void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6246 std::string& s) const {
6247 if (MVT::isFloatingPoint(ConstraintVT)) {
6248 if (Subtarget->hasSSE2())
6249 s = "Y";
6250 else if (Subtarget->hasSSE1())
6251 s = "x";
6252 else
6253 s = "f";
6254 } else
6255 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6256}
6257
Chris Lattnera531abc2007-08-25 00:47:38 +00006258/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6259/// vector. If it is invalid, don't add anything to Ops.
6260void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6261 char Constraint,
6262 std::vector<SDOperand>&Ops,
6263 SelectionDAG &DAG) {
6264 SDOperand Result(0, 0);
6265
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006266 switch (Constraint) {
6267 default: break;
6268 case 'I':
6269 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006270 if (C->getValue() <= 31) {
6271 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6272 break;
6273 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006274 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006275 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006276 case 'N':
6277 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006278 if (C->getValue() <= 255) {
6279 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6280 break;
6281 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006282 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006283 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006284 case 'i': {
6285 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006286 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6287 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6288 break;
6289 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290
6291 // If we are in non-pic codegen mode, we allow the address of a global (with
6292 // an optional displacement) to be used with 'i'.
6293 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6294 int64_t Offset = 0;
6295
6296 // Match either (GA) or (GA+C)
6297 if (GA) {
6298 Offset = GA->getOffset();
6299 } else if (Op.getOpcode() == ISD::ADD) {
6300 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6301 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6302 if (C && GA) {
6303 Offset = GA->getOffset()+C->getValue();
6304 } else {
6305 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6306 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6307 if (C && GA)
6308 Offset = GA->getOffset()+C->getValue();
6309 else
6310 C = 0, GA = 0;
6311 }
6312 }
6313
6314 if (GA) {
6315 // If addressing this global requires a load (e.g. in PIC mode), we can't
6316 // match.
6317 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6318 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006319 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006320
6321 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6322 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006323 Result = Op;
6324 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006325 }
6326
6327 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006328 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006329 }
6330 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006331
6332 if (Result.Val) {
6333 Ops.push_back(Result);
6334 return;
6335 }
6336 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006337}
6338
6339std::vector<unsigned> X86TargetLowering::
6340getRegClassForInlineAsmConstraint(const std::string &Constraint,
6341 MVT::ValueType VT) const {
6342 if (Constraint.size() == 1) {
6343 // FIXME: not handling fp-stack yet!
6344 switch (Constraint[0]) { // GCC X86 Constraint Letters
6345 default: break; // Unknown constraint letter
6346 case 'A': // EAX/EDX
6347 if (VT == MVT::i32 || VT == MVT::i64)
6348 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6349 break;
6350 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6351 case 'Q': // Q_REGS
6352 if (VT == MVT::i32)
6353 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6354 else if (VT == MVT::i16)
6355 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6356 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006357 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006358 else if (VT == MVT::i64)
6359 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6360 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006361 }
6362 }
6363
6364 return std::vector<unsigned>();
6365}
6366
6367std::pair<unsigned, const TargetRegisterClass*>
6368X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6369 MVT::ValueType VT) const {
6370 // First, see if this is a constraint that directly corresponds to an LLVM
6371 // register class.
6372 if (Constraint.size() == 1) {
6373 // GCC Constraint Letters
6374 switch (Constraint[0]) {
6375 default: break;
6376 case 'r': // GENERAL_REGS
6377 case 'R': // LEGACY_REGS
6378 case 'l': // INDEX_REGS
6379 if (VT == MVT::i64 && Subtarget->is64Bit())
6380 return std::make_pair(0U, X86::GR64RegisterClass);
6381 if (VT == MVT::i32)
6382 return std::make_pair(0U, X86::GR32RegisterClass);
6383 else if (VT == MVT::i16)
6384 return std::make_pair(0U, X86::GR16RegisterClass);
6385 else if (VT == MVT::i8)
6386 return std::make_pair(0U, X86::GR8RegisterClass);
6387 break;
6388 case 'y': // MMX_REGS if MMX allowed.
6389 if (!Subtarget->hasMMX()) break;
6390 return std::make_pair(0U, X86::VR64RegisterClass);
6391 break;
6392 case 'Y': // SSE_REGS if SSE2 allowed
6393 if (!Subtarget->hasSSE2()) break;
6394 // FALL THROUGH.
6395 case 'x': // SSE_REGS if SSE1 allowed
6396 if (!Subtarget->hasSSE1()) break;
6397
6398 switch (VT) {
6399 default: break;
6400 // Scalar SSE types.
6401 case MVT::f32:
6402 case MVT::i32:
6403 return std::make_pair(0U, X86::FR32RegisterClass);
6404 case MVT::f64:
6405 case MVT::i64:
6406 return std::make_pair(0U, X86::FR64RegisterClass);
6407 // Vector types.
6408 case MVT::v16i8:
6409 case MVT::v8i16:
6410 case MVT::v4i32:
6411 case MVT::v2i64:
6412 case MVT::v4f32:
6413 case MVT::v2f64:
6414 return std::make_pair(0U, X86::VR128RegisterClass);
6415 }
6416 break;
6417 }
6418 }
6419
6420 // Use the default implementation in TargetLowering to convert the register
6421 // constraint into a member of a register class.
6422 std::pair<unsigned, const TargetRegisterClass*> Res;
6423 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6424
6425 // Not found as a standard register?
6426 if (Res.second == 0) {
6427 // GCC calls "st(0)" just plain "st".
6428 if (StringsEqualNoCase("{st}", Constraint)) {
6429 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006430 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006431 }
6432
6433 return Res;
6434 }
6435
6436 // Otherwise, check to see if this is a register class of the wrong value
6437 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6438 // turn into {ax},{dx}.
6439 if (Res.second->hasType(VT))
6440 return Res; // Correct type already, nothing to do.
6441
6442 // All of the single-register GCC register classes map their values onto
6443 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6444 // really want an 8-bit or 32-bit register, map to the appropriate register
6445 // class and return the appropriate register.
6446 if (Res.second != X86::GR16RegisterClass)
6447 return Res;
6448
6449 if (VT == MVT::i8) {
6450 unsigned DestReg = 0;
6451 switch (Res.first) {
6452 default: break;
6453 case X86::AX: DestReg = X86::AL; break;
6454 case X86::DX: DestReg = X86::DL; break;
6455 case X86::CX: DestReg = X86::CL; break;
6456 case X86::BX: DestReg = X86::BL; break;
6457 }
6458 if (DestReg) {
6459 Res.first = DestReg;
6460 Res.second = Res.second = X86::GR8RegisterClass;
6461 }
6462 } else if (VT == MVT::i32) {
6463 unsigned DestReg = 0;
6464 switch (Res.first) {
6465 default: break;
6466 case X86::AX: DestReg = X86::EAX; break;
6467 case X86::DX: DestReg = X86::EDX; break;
6468 case X86::CX: DestReg = X86::ECX; break;
6469 case X86::BX: DestReg = X86::EBX; break;
6470 case X86::SI: DestReg = X86::ESI; break;
6471 case X86::DI: DestReg = X86::EDI; break;
6472 case X86::BP: DestReg = X86::EBP; break;
6473 case X86::SP: DestReg = X86::ESP; break;
6474 }
6475 if (DestReg) {
6476 Res.first = DestReg;
6477 Res.second = Res.second = X86::GR32RegisterClass;
6478 }
6479 } else if (VT == MVT::i64) {
6480 unsigned DestReg = 0;
6481 switch (Res.first) {
6482 default: break;
6483 case X86::AX: DestReg = X86::RAX; break;
6484 case X86::DX: DestReg = X86::RDX; break;
6485 case X86::CX: DestReg = X86::RCX; break;
6486 case X86::BX: DestReg = X86::RBX; break;
6487 case X86::SI: DestReg = X86::RSI; break;
6488 case X86::DI: DestReg = X86::RDI; break;
6489 case X86::BP: DestReg = X86::RBP; break;
6490 case X86::SP: DestReg = X86::RSP; break;
6491 }
6492 if (DestReg) {
6493 Res.first = DestReg;
6494 Res.second = Res.second = X86::GR64RegisterClass;
6495 }
6496 }
6497
6498 return Res;
6499}