blob: 3a4dc23b9a57ead5741baf8a5676484453623313 [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"
42using namespace llvm;
43
44X86TargetLowering::X86TargetLowering(TargetMachine &TM)
45 : TargetLowering(TM) {
46 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000047 X86ScalarSSEf64 = Subtarget->hasSSE2();
48 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000050
Chris Lattnerdec9cb52008-01-24 08:07:48 +000051 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052
53 RegInfo = TM.getRegisterInfo();
54
55 // Set up the TargetLowering object.
56
57 // X86 is weird, it always uses i8 for shift amounts and setcc results.
58 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 setSetCCResultContents(ZeroOrOneSetCCResult);
60 setSchedulingPreference(SchedulingForRegPressure);
61 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
62 setStackPointerRegisterToSaveRestore(X86StackPtr);
63
64 if (Subtarget->isTargetDarwin()) {
65 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
66 setUseUnderscoreSetJmp(false);
67 setUseUnderscoreLongJmp(false);
68 } else if (Subtarget->isTargetMingw()) {
69 // MS runtime is weird: it exports _setjmp, but longjmp!
70 setUseUnderscoreSetJmp(true);
71 setUseUnderscoreLongJmp(false);
72 } else {
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
75 }
76
77 // Set up the register classes.
78 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
79 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
80 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
81 if (Subtarget->is64Bit())
82 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
83
Duncan Sands082524c2008-01-23 20:39:46 +000084 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085
Chris Lattner3bc08502008-01-17 19:59:44 +000086 // We don't accept any truncstore of integer registers.
87 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
88 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
89 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
90 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
93
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
95 // operation.
96 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
97 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
98 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
99
100 if (Subtarget->is64Bit()) {
101 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
102 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
103 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000104 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
106 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
107 else
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
109 }
110
111 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
112 // this operation.
113 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
114 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
115 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000116 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000118 // f32 and f64 cases are Legal, f80 case is not
119 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
120 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 }
124
Dale Johannesen958b08b2007-09-19 23:55:34 +0000125 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
126 // are Legal, f80 is custom lowered.
127 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
128 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129
130 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
131 // this operation.
132 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
133 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
134
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000135 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000137 // f32 and f64 cases are Legal, f80 case is not
138 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 } else {
140 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
142 }
143
144 // Handle FP_TO_UINT by promoting the destination to a larger signed
145 // conversion.
146 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
147 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
148 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
149
150 if (Subtarget->is64Bit()) {
151 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
152 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
153 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000154 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 // Expand FP_TO_UINT into a select.
156 // FIXME: We would like to use a Custom expander here eventually to do
157 // the optimal thing for SSE vs. the default expansion in the legalizer.
158 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
159 else
160 // With SSE3 we can use fisttpll to convert to a signed i64.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
162 }
163
164 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000165 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
167 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
168 }
169
Dan Gohman8450d862008-02-18 19:34:53 +0000170 // Scalar integer divide and remainder are lowered to use operations that
171 // produce two results, to match the available instructions. This exposes
172 // the two-result form to trivial CSE, which is able to combine x/y and x%y
173 // into a single instruction.
174 //
175 // Scalar integer multiply-high is also lowered to use two-result
176 // operations, to match the available instructions. However, plain multiply
177 // (low) operations are left as Legal, as there are single-result
178 // instructions for this in x86. Using the two-result multiply instructions
179 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000180 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
181 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
182 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
183 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
184 setOperationAction(ISD::SREM , MVT::i8 , Expand);
185 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000186 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
187 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
188 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
189 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
190 setOperationAction(ISD::SREM , MVT::i16 , Expand);
191 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000192 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
193 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
194 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
195 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
196 setOperationAction(ISD::SREM , MVT::i32 , Expand);
197 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000198 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
199 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
200 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
201 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
202 setOperationAction(ISD::SREM , MVT::i64 , Expand);
203 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000204
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
206 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
207 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
208 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
214 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000215 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000217 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000218 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000219
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000221 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
222 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 if (Subtarget->is64Bit()) {
230 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000231 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
232 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 }
234
235 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
236 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
237
238 // These should be promoted to a larger select which is supported.
239 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
240 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
241 // X86 wants to expand cmov itself.
242 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
243 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
244 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
245 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000246 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
248 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
250 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
251 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000252 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 if (Subtarget->is64Bit()) {
254 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
255 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
256 }
257 // X86 ret instruction may pop stack.
258 setOperationAction(ISD::RET , MVT::Other, Custom);
259 if (!Subtarget->is64Bit())
260 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
261
262 // Darwin ABI issue.
263 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
264 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
265 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
266 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
267 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
268 if (Subtarget->is64Bit()) {
269 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
270 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
271 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
272 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
273 }
274 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
275 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
276 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
277 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000278 if (Subtarget->is64Bit()) {
279 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
280 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
281 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
282 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
Evan Cheng8d51ab32008-03-10 19:38:10 +0000284 if (Subtarget->hasSSE1())
285 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000286
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000287 if (!Subtarget->hasSSE2())
288 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
289
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +0000290 setOperationAction(ISD::ATOMIC_LCS , MVT::i8, Custom);
291 setOperationAction(ISD::ATOMIC_LCS , MVT::i16, Custom);
292 setOperationAction(ISD::ATOMIC_LCS , MVT::i32, Custom);
Andrew Lenharthbd7d3262008-03-04 21:13:33 +0000293 setOperationAction(ISD::ATOMIC_LCS , MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000294
Evan Cheng2e28d622008-02-02 04:07:54 +0000295 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 // FIXME - use subtarget debug flags
298 if (!Subtarget->isTargetDarwin() &&
299 !Subtarget->isTargetELF() &&
300 !Subtarget->isTargetCygMing())
301 setOperationAction(ISD::LABEL, MVT::Other, Expand);
302
303 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
304 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
305 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
306 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
307 if (Subtarget->is64Bit()) {
308 // FIXME: Verify
309 setExceptionPointerRegister(X86::RAX);
310 setExceptionSelectorRegister(X86::RDX);
311 } else {
312 setExceptionPointerRegister(X86::EAX);
313 setExceptionSelectorRegister(X86::EDX);
314 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000315 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316
Duncan Sands7407a9f2007-09-11 14:10:23 +0000317 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000318
Chris Lattner56b941f2008-01-15 21:58:22 +0000319 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000320
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
322 setOperationAction(ISD::VASTART , MVT::Other, Custom);
323 setOperationAction(ISD::VAARG , MVT::Other, Expand);
324 setOperationAction(ISD::VAEND , MVT::Other, Expand);
325 if (Subtarget->is64Bit())
326 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
327 else
328 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
329
330 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
331 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
332 if (Subtarget->is64Bit())
333 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
334 if (Subtarget->isTargetCygMing())
335 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
336 else
337 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
338
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000339 if (X86ScalarSSEf64) {
340 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 // Set up the FP register classes.
342 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
343 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
344
345 // Use ANDPD to simulate FABS.
346 setOperationAction(ISD::FABS , MVT::f64, Custom);
347 setOperationAction(ISD::FABS , MVT::f32, Custom);
348
349 // Use XORP to simulate FNEG.
350 setOperationAction(ISD::FNEG , MVT::f64, Custom);
351 setOperationAction(ISD::FNEG , MVT::f32, Custom);
352
353 // Use ANDPD and ORPD to simulate FCOPYSIGN.
354 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
355 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
356
357 // We don't support sin/cos/fmod
358 setOperationAction(ISD::FSIN , MVT::f64, Expand);
359 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 setOperationAction(ISD::FSIN , MVT::f32, Expand);
361 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362
363 // Expand FP immediates into loads from the stack, except for the special
364 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000365 addLegalFPImmediate(APFloat(+0.0)); // xorpd
366 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000367
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000368 // Floating truncations from f80 and extensions to f80 go through memory.
369 // If optimizing, we lie about this though and handle it in
370 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
371 if (Fast) {
372 setConvertAction(MVT::f32, MVT::f80, Expand);
373 setConvertAction(MVT::f64, MVT::f80, Expand);
374 setConvertAction(MVT::f80, MVT::f32, Expand);
375 setConvertAction(MVT::f80, MVT::f64, Expand);
376 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000377 } else if (X86ScalarSSEf32) {
378 // Use SSE for f32, x87 for f64.
379 // Set up the FP register classes.
380 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
381 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
382
383 // Use ANDPS to simulate FABS.
384 setOperationAction(ISD::FABS , MVT::f32, Custom);
385
386 // Use XORP to simulate FNEG.
387 setOperationAction(ISD::FNEG , MVT::f32, Custom);
388
389 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
390
391 // Use ANDPS and ORPS to simulate FCOPYSIGN.
392 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
393 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
394
395 // We don't support sin/cos/fmod
396 setOperationAction(ISD::FSIN , MVT::f32, Expand);
397 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000398
Nate Begemane2ba64f2008-02-14 08:57:00 +0000399 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000400 addLegalFPImmediate(APFloat(+0.0f)); // xorps
401 addLegalFPImmediate(APFloat(+0.0)); // FLD0
402 addLegalFPImmediate(APFloat(+1.0)); // FLD1
403 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
404 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
405
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000406 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
407 // this though and handle it in InstructionSelectPreprocess so that
408 // dagcombine2 can hack on these.
409 if (Fast) {
410 setConvertAction(MVT::f32, MVT::f64, Expand);
411 setConvertAction(MVT::f32, MVT::f80, Expand);
412 setConvertAction(MVT::f80, MVT::f32, Expand);
413 setConvertAction(MVT::f64, MVT::f32, Expand);
414 // And x87->x87 truncations also.
415 setConvertAction(MVT::f80, MVT::f64, Expand);
416 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000417
418 if (!UnsafeFPMath) {
419 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
420 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
421 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000423 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 // Set up the FP register classes.
425 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
426 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
427
428 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
429 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
430 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
431 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000432
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000433 // Floating truncations go through memory. If optimizing, we lie about
434 // this though and handle it in InstructionSelectPreprocess so that
435 // dagcombine2 can hack on these.
436 if (Fast) {
437 setConvertAction(MVT::f80, MVT::f32, Expand);
438 setConvertAction(MVT::f64, MVT::f32, Expand);
439 setConvertAction(MVT::f80, MVT::f64, Expand);
440 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441
442 if (!UnsafeFPMath) {
443 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
444 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
445 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000446 addLegalFPImmediate(APFloat(+0.0)); // FLD0
447 addLegalFPImmediate(APFloat(+1.0)); // FLD1
448 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
449 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000450 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
451 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
452 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
453 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 }
455
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000456 // Long double always uses X87.
457 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000458 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
459 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000460 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000461 APFloat TmpFlt(+0.0);
462 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
463 addLegalFPImmediate(TmpFlt); // FLD0
464 TmpFlt.changeSign();
465 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
466 APFloat TmpFlt2(+1.0);
467 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
468 addLegalFPImmediate(TmpFlt2); // FLD1
469 TmpFlt2.changeSign();
470 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
471 }
472
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000473 if (!UnsafeFPMath) {
474 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
475 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
476 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000477
Dan Gohman2f7b1982007-10-11 23:21:31 +0000478 // Always use a library call for pow.
479 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
480 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
481 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
482
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 // First set operation action for all vector types to expand. Then we
484 // will selectively turn on ones that can be effectively codegen'd.
485 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
486 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
487 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000510 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000514 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000515 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000518 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
521 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
523 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 }
525
526 if (Subtarget->hasMMX()) {
527 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
528 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
529 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
530 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
531
532 // FIXME: add MMX packed arithmetics
533
534 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
535 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
536 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
537 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
538
539 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
540 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
541 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000542 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543
544 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
545 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
546
547 setOperationAction(ISD::AND, MVT::v8i8, Promote);
548 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
549 setOperationAction(ISD::AND, MVT::v4i16, Promote);
550 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
551 setOperationAction(ISD::AND, MVT::v2i32, Promote);
552 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
553 setOperationAction(ISD::AND, MVT::v1i64, Legal);
554
555 setOperationAction(ISD::OR, MVT::v8i8, Promote);
556 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
557 setOperationAction(ISD::OR, MVT::v4i16, Promote);
558 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
559 setOperationAction(ISD::OR, MVT::v2i32, Promote);
560 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
561 setOperationAction(ISD::OR, MVT::v1i64, Legal);
562
563 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
564 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
565 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
566 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
567 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
568 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
569 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
570
571 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
572 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
573 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
574 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
575 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
576 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
577 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
578
579 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
580 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
581 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
582 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
583
584 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
585 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
586 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
587 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
588
589 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
590 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
592 }
593
594 if (Subtarget->hasSSE1()) {
595 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
596
597 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
598 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
599 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
600 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
601 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
602 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
604 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
605 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
606 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
607 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
608 }
609
610 if (Subtarget->hasSSE2()) {
611 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
612 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
613 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
614 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
615 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
616
617 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
618 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
619 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
620 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
621 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
622 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
623 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
624 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
625 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
626 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
627 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
628 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
629 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
630 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
631 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632
633 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
634 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
635 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
636 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
638
639 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
640 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000641 // Do not attempt to custom lower non-power-of-2 vectors
642 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
643 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
646 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
647 }
648 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
649 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
650 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
651 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000652 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000654 if (Subtarget->is64Bit()) {
655 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000656 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000657 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658
659 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
660 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
661 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
662 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
663 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
664 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
665 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
666 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
667 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
668 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
669 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
670 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
671 }
672
Chris Lattner3bc08502008-01-17 19:59:44 +0000673 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000674
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675 // Custom lower v2i64 and v2f64 selects.
676 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
677 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
678 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
679 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
680 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000681
682 if (Subtarget->hasSSE41()) {
683 // FIXME: Do we need to handle scalar-to-vector here?
684 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
685
686 // i8 and i16 vectors are custom , because the source register and source
687 // source memory operand types are not the same width. f32 vectors are
688 // custom since the immediate controlling the insert encodes additional
689 // information.
690 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
691 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
693 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
694
695 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
697 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000698 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000699
700 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000701 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
702 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000703 }
704 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
706 // We want to custom lower some of our intrinsics.
707 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
708
709 // We have target-specific dag combine patterns for the following nodes:
710 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
711 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000712 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713
714 computeRegisterProperties();
715
716 // FIXME: These should be based on subtarget info. Plus, the values should
717 // be smaller when we are in optimizing for size mode.
718 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
719 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
720 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
721 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000722 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723}
724
Scott Michel502151f2008-03-10 15:42:14 +0000725
726MVT::ValueType
727X86TargetLowering::getSetCCResultType(const SDOperand &) const {
728 return MVT::i8;
729}
730
731
Evan Cheng5a67b812008-01-23 23:17:41 +0000732/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
733/// the desired ByVal argument alignment.
734static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
735 if (MaxAlign == 16)
736 return;
737 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
738 if (VTy->getBitWidth() == 128)
739 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000740 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
741 unsigned EltAlign = 0;
742 getMaxByValAlign(ATy->getElementType(), EltAlign);
743 if (EltAlign > MaxAlign)
744 MaxAlign = EltAlign;
745 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
746 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
747 unsigned EltAlign = 0;
748 getMaxByValAlign(STy->getElementType(i), EltAlign);
749 if (EltAlign > MaxAlign)
750 MaxAlign = EltAlign;
751 if (MaxAlign == 16)
752 break;
753 }
754 }
755 return;
756}
757
758/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
759/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000760/// that contain SSE vectors are placed at 16-byte boundaries while the rest
761/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000762unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
763 if (Subtarget->is64Bit())
764 return getTargetData()->getABITypeAlignment(Ty);
765 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000766 if (Subtarget->hasSSE1())
767 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000768 return Align;
769}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770
Evan Cheng6fb06762007-11-09 01:32:10 +0000771/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
772/// jumptable.
773SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
774 SelectionDAG &DAG) const {
775 if (usesGlobalOffsetTable())
776 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
777 if (!Subtarget->isPICStyleRIPRel())
778 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
779 return Table;
780}
781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782//===----------------------------------------------------------------------===//
783// Return Value Calling Convention Implementation
784//===----------------------------------------------------------------------===//
785
786#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000787
788/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
789/// exists skip possible ISD:TokenFactor.
790static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000791 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000792 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000793 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000794 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000795 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000796 return Chain.getOperand(0);
797 }
798 return Chain;
799}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000800
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801/// LowerRET - Lower an ISD::RET node.
802SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
803 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
804
805 SmallVector<CCValAssign, 16> RVLocs;
806 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
807 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
808 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
809 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000810
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 // If this is the first return lowered for this function, add the regs to the
812 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000813 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 for (unsigned i = 0; i != RVLocs.size(); ++i)
815 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000816 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000820 // Handle tail call return.
821 Chain = GetPossiblePreceedingTailCall(Chain);
822 if (Chain.getOpcode() == X86ISD::TAILCALL) {
823 SDOperand TailCall = Chain;
824 SDOperand TargetAddress = TailCall.getOperand(1);
825 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000826 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000827 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
828 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
829 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
830 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
831 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000832 assert(StackAdjustment.getOpcode() == ISD::Constant &&
833 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000834
835 SmallVector<SDOperand,8> Operands;
836 Operands.push_back(Chain.getOperand(0));
837 Operands.push_back(TargetAddress);
838 Operands.push_back(StackAdjustment);
839 // Copy registers used by the call. Last operand is a flag so it is not
840 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000841 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000842 Operands.push_back(Chain.getOperand(i));
843 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000844 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
845 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000846 }
847
848 // Regular return.
849 SDOperand Flag;
850
Chris Lattnerb56cc342008-03-11 03:23:40 +0000851 SmallVector<SDOperand, 6> RetOps;
852 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
853 // Operand #1 = Bytes To Pop
854 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
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 Lattnerb56cc342008-03-11 03:23:40 +0000862 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
863 // the RET instruction and handled by the FP Stackifier.
864 if (RVLocs[i].getLocReg() == X86::ST0 ||
865 RVLocs[i].getLocReg() == X86::ST1) {
866 // If this is a copy from an xmm register to ST(0), use an FPExtend to
867 // change the value to the FP stack register class.
868 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
869 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
870 RetOps.push_back(ValToCopy);
871 // Don't emit a copytoreg.
872 continue;
873 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000875 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 Flag = Chain.getValue(1);
877 }
878
Chris Lattnerb56cc342008-03-11 03:23:40 +0000879 RetOps[0] = Chain; // Update chain.
880
881 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000883 RetOps.push_back(Flag);
884
885 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886}
887
888
889/// LowerCallResult - Lower the result values of an ISD::CALL into the
890/// appropriate copies out of appropriate physical registers. This assumes that
891/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
892/// being lowered. The returns a SDNode with the same number of values as the
893/// ISD::CALL.
894SDNode *X86TargetLowering::
895LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
896 unsigned CallingConv, SelectionDAG &DAG) {
897
898 // Assign locations to each value returned by this call.
899 SmallVector<CCValAssign, 16> RVLocs;
900 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
901 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
902 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
903
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 SmallVector<SDOperand, 8> ResultVals;
905
906 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000907 for (unsigned i = 0; i != RVLocs.size(); ++i) {
908 MVT::ValueType CopyVT = RVLocs[i].getValVT();
909
910 // If this is a call to a function that returns an fp value on the floating
911 // point stack, but where we prefer to use the value in xmm registers, copy
912 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
913 if (RVLocs[i].getLocReg() == X86::ST0 &&
914 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
915 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000918 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
919 CopyVT, InFlag).getValue(1);
920 SDOperand Val = Chain.getValue(0);
921 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000922
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000923 if (CopyVT != RVLocs[i].getValVT()) {
924 // Round the F80 the right size, which also moves to the appropriate xmm
925 // register.
926 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
927 // This truncation won't change the value.
928 DAG.getIntPtrConstant(1));
929 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000930
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000931 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 }
933
934 // Merge everything together with a MERGE_VALUES node.
935 ResultVals.push_back(Chain);
936 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
937 &ResultVals[0], ResultVals.size()).Val;
938}
939
940
941//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000942// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943//===----------------------------------------------------------------------===//
944// StdCall calling convention seems to be standard for many Windows' API
945// routines and around. It differs from C calling convention just a little:
946// callee should clean up the stack, not caller. Symbols should be also
947// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000948// For info on fast calling convention see Fast Calling Convention (tail call)
949// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950
951/// AddLiveIn - This helper function adds the specified physical register to the
952/// MachineFunction as a live in value. It also creates a corresponding virtual
953/// register for it.
954static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
955 const TargetRegisterClass *RC) {
956 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000957 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
958 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 return VReg;
960}
961
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000962/// CallIsStructReturn - Determines whether a CALL node uses struct return
963/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000964static bool CallIsStructReturn(SDOperand Op) {
965 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
966 if (!NumOps)
967 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +0000968
969 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +0000970}
971
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000972/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
973/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000974static bool ArgsAreStructReturn(SDOperand Op) {
975 unsigned NumArgs = Op.Val->getNumValues() - 1;
976 if (!NumArgs)
977 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +0000978
979 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +0000980}
981
Arnold Schwaighofera38df102008-04-12 18:11:06 +0000982/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
983/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000984/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000985bool X86TargetLowering::IsCalleePop(SDOperand Op) {
986 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
987 if (IsVarArg)
988 return false;
989
990 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
991 default:
992 return false;
993 case CallingConv::X86_StdCall:
994 return !Subtarget->is64Bit();
995 case CallingConv::X86_FastCall:
996 return !Subtarget->is64Bit();
997 case CallingConv::Fast:
998 return PerformTailCallOpt;
999 }
1000}
1001
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001002/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1003/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001004CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1005 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1006
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001007 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001008 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001009 return CC_X86_Win64_C;
1010 else {
1011 if (CC == CallingConv::Fast && PerformTailCallOpt)
1012 return CC_X86_64_TailCall;
1013 else
1014 return CC_X86_64_C;
1015 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001016 }
1017
Gordon Henriksen18ace102008-01-05 16:56:59 +00001018 if (CC == CallingConv::X86_FastCall)
1019 return CC_X86_32_FastCall;
1020 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1021 return CC_X86_32_TailCall;
1022 else
1023 return CC_X86_32_C;
1024}
1025
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001026/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1027/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001028NameDecorationStyle
1029X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1030 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1031 if (CC == CallingConv::X86_FastCall)
1032 return FastCall;
1033 else if (CC == CallingConv::X86_StdCall)
1034 return StdCall;
1035 return None;
1036}
1037
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001038/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1039/// possibly be overwritten when lowering the outgoing arguments in a tail
1040/// call. Currently the implementation of this call is very conservative and
1041/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1042/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001043static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1044 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001045 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001046 FrameIndexSDNode * FrameIdxNode = NULL;
1047 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001048 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1049 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001050 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1051 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1052 (Op.getOpcode() == ISD::LOAD &&
1053 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1054 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1055 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001056 return true;
1057 return false;
1058}
1059
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001060/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1061/// in a register before calling.
1062bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1063 return !IsTailCall && !Is64Bit &&
1064 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1065 Subtarget->isPICStyleGOT();
1066}
1067
1068
1069/// CallRequiresFnAddressInReg - Check whether the call requires the function
1070/// address to be loaded in a register.
1071bool
1072X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1073 return !Is64Bit && IsTailCall &&
1074 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1075 Subtarget->isPICStyleGOT();
1076}
1077
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001078/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1079/// arguments to force loading and guarantee that arguments sourcing from
1080/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001081static SDOperand
1082CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1083 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1084 SelectionDAG &DAG,
1085 MachineFunction &MF,
1086 const TargetLowering * TL) {
1087
1088 SDOperand InFlag;
1089 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1090 SDOperand Arg = TailCallClobberedVRegs[i].second;
1091 unsigned Idx = TailCallClobberedVRegs[i].first;
1092 unsigned VReg =
1093 MF.getRegInfo().
1094 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1095 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1096 InFlag = Chain.getValue(1);
1097 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1098 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1099 Chain = Arg.getValue(1);
1100 InFlag = Arg.getValue(2);
1101 }
1102 return Chain;
1103}
1104
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001105/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1106/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001107/// the specific parameter attribute. The copy will be passed as a byval
1108/// function parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001109static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001110CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001111 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001112 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001113 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
1114 /*AlwaysInline=*/true,
1115 NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001116}
1117
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001118SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1119 const CCValAssign &VA,
1120 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001121 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001122 SDOperand Root, unsigned i) {
1123 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001124 ISD::ArgFlagsTy Flags =
1125 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001126 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001127 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001128
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001129 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1130 // changed with more analysis.
1131 // In case of tail call optimization mark all arguments mutable. Since they
1132 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001133 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001134 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001135 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001136 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001137 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001138 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001139 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001140}
1141
Gordon Henriksen18ace102008-01-05 16:56:59 +00001142SDOperand
1143X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001145 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1146
1147 const Function* Fn = MF.getFunction();
1148 if (Fn->hasExternalLinkage() &&
1149 Subtarget->isTargetCygMing() &&
1150 Fn->getName() == "main")
1151 FuncInfo->setForceFramePointer(true);
1152
1153 // Decorate the function name.
1154 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1155
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 MachineFrameInfo *MFI = MF.getFrameInfo();
1157 SDOperand Root = Op.getOperand(0);
1158 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001159 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001160 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001161
1162 assert(!(isVarArg && CC == CallingConv::Fast) &&
1163 "Var args not supported with calling convention fastcc");
1164
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 // Assign locations to all of the incoming arguments.
1166 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001167 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001168 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170 SmallVector<SDOperand, 8> ArgValues;
1171 unsigned LastVal = ~0U;
1172 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1173 CCValAssign &VA = ArgLocs[i];
1174 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1175 // places.
1176 assert(VA.getValNo() != LastVal &&
1177 "Don't support value assigned to multiple locs yet");
1178 LastVal = VA.getValNo();
1179
1180 if (VA.isRegLoc()) {
1181 MVT::ValueType RegVT = VA.getLocVT();
1182 TargetRegisterClass *RC;
1183 if (RegVT == MVT::i32)
1184 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001185 else if (Is64Bit && RegVT == MVT::i64)
1186 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001187 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001188 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001189 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001190 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001191 else {
1192 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001193 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1194 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1195 RegVT = MVT::i64;
1196 } else
1197 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1201 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1202
1203 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1204 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1205 // right size.
1206 if (VA.getLocInfo() == CCValAssign::SExt)
1207 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1208 DAG.getValueType(VA.getValVT()));
1209 else if (VA.getLocInfo() == CCValAssign::ZExt)
1210 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1211 DAG.getValueType(VA.getValVT()));
1212
1213 if (VA.getLocInfo() != CCValAssign::Full)
1214 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1215
Gordon Henriksen18ace102008-01-05 16:56:59 +00001216 // Handle MMX values passed in GPRs.
1217 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1218 MVT::getSizeInBits(RegVT) == 64)
1219 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221 ArgValues.push_back(ArgValue);
1222 } else {
1223 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001224 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225 }
1226 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001229 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001230 if (CC == CallingConv::Fast)
1231 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232
1233 // If the function takes variable number of arguments, make a frame index for
1234 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001235 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001236 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1237 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1238 }
1239 if (Is64Bit) {
1240 static const unsigned GPR64ArgRegs[] = {
1241 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1242 };
1243 static const unsigned XMMArgRegs[] = {
1244 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1245 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1246 };
1247
1248 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1249 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1250
1251 // For X86-64, if there are vararg parameters that are passed via
1252 // registers, then we must store them to their spots on the stack so they
1253 // may be loaded by deferencing the result of va_next.
1254 VarArgsGPOffset = NumIntRegs * 8;
1255 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1256 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1257
1258 // Store the integer parameter registers.
1259 SmallVector<SDOperand, 8> MemOps;
1260 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1261 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001262 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001263 for (; NumIntRegs != 6; ++NumIntRegs) {
1264 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1265 X86::GR64RegisterClass);
1266 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001267 SDOperand Store =
1268 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001269 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001270 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001271 MemOps.push_back(Store);
1272 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001273 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001274 }
1275
1276 // Now store the XMM (fp + vector) parameter registers.
1277 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001278 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001279 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1280 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1281 X86::VR128RegisterClass);
1282 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001283 SDOperand Store =
1284 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001285 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001286 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001287 MemOps.push_back(Store);
1288 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001289 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001290 }
1291 if (!MemOps.empty())
1292 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1293 &MemOps[0], MemOps.size());
1294 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001295 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001296
1297 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1298 // arguments and the arguments after the retaddr has been pushed are
1299 // aligned.
1300 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1301 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1302 (StackSize & 7) == 0)
1303 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001305 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001306
Gordon Henriksen18ace102008-01-05 16:56:59 +00001307 // Some CCs need callee pop.
1308 if (IsCalleePop(Op)) {
1309 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310 BytesCallerReserves = 0;
1311 } else {
1312 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001314 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 BytesCallerReserves = StackSize;
1317 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001318
Gordon Henriksen18ace102008-01-05 16:56:59 +00001319 if (!Is64Bit) {
1320 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1321 if (CC == CallingConv::X86_FastCall)
1322 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1323 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324
Anton Korobeynikove844e472007-08-15 17:12:32 +00001325 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326
1327 // Return the new list of results.
1328 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1329 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1330}
1331
Evan Chengbc077bf2008-01-10 00:09:10 +00001332SDOperand
1333X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1334 const SDOperand &StackPtr,
1335 const CCValAssign &VA,
1336 SDOperand Chain,
1337 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001338 unsigned LocMemOffset = VA.getLocMemOffset();
1339 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001340 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001341 ISD::ArgFlagsTy Flags =
1342 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1343 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001344 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001345 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001346 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001347 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001348}
1349
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001350/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1351/// optimization is performed and it is required.
1352SDOperand
1353X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1354 SDOperand &OutRetAddr,
1355 SDOperand Chain,
1356 bool IsTailCall,
1357 bool Is64Bit,
1358 int FPDiff) {
1359 if (!IsTailCall || FPDiff==0) return Chain;
1360
1361 // Adjust the Return address stack slot.
1362 MVT::ValueType VT = getPointerTy();
1363 OutRetAddr = getReturnAddressFrameIndex(DAG);
1364 // Load the "old" Return address.
1365 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1366 return SDOperand(OutRetAddr.Val, 1);
1367}
1368
1369/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1370/// optimization is performed and it is required (FPDiff!=0).
1371static SDOperand
1372EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1373 SDOperand Chain, SDOperand RetAddrFrIdx,
1374 bool Is64Bit, int FPDiff) {
1375 // Store the return address to the appropriate stack slot.
1376 if (!FPDiff) return Chain;
1377 // Calculate the new stack slot for the return address.
1378 int SlotSize = Is64Bit ? 8 : 4;
1379 int NewReturnAddrFI =
1380 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1381 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1382 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1383 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1384 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1385 return Chain;
1386}
1387
1388/// CopyTailCallByValClobberedRegToVirtReg - Copy arguments with register target
1389/// which might be overwritten by later byval tail call lowering to a virtual
1390/// register.
1391bool
1392X86TargetLowering::CopyTailCallByValClobberedRegToVirtReg(bool containsByValArg,
1393 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1394 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes,
1395 std::pair<unsigned, SDOperand> &RegToPass,
1396 SDOperand &OutChain,
1397 SDOperand &OutFlag,
1398 MachineFunction &MF,
1399 SelectionDAG & DAG) {
1400 if (!containsByValArg) return false;
1401
1402 std::pair<unsigned, unsigned> ArgRegVReg;
1403 MVT::ValueType VT = RegToPass.second.getValueType();
1404
1405 ArgRegVReg.first = RegToPass.first;
1406 ArgRegVReg.second = MF.getRegInfo().createVirtualRegister(getRegClassFor(VT));
1407
1408 // Copy Argument to virtual register.
1409 OutChain = DAG.getCopyToReg(OutChain, ArgRegVReg.second,
1410 RegToPass.second, OutFlag);
1411 OutFlag = OutChain.getValue(1);
1412 // Remember virtual register and type.
1413 TailCallByValClobberedVRegs.push_back(ArgRegVReg);
1414 TailCallByValClobberedVRegTypes.push_back(VT);
1415 return true;
1416}
1417
1418
1419/// RestoreTailCallByValClobberedReg - Restore registers which were saved to
1420/// virtual registers to prevent tail call byval lowering from overwriting
1421/// parameter registers.
1422static SDOperand
1423RestoreTailCallByValClobberedRegs(SelectionDAG & DAG, SDOperand Chain,
1424 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1425 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes) {
1426 if (TailCallByValClobberedVRegs.size()==0) return Chain;
1427
1428 SmallVector<SDOperand, 8> RegOpChains;
1429 for (unsigned i = 0, e=TailCallByValClobberedVRegs.size(); i != e; i++) {
1430 SDOperand InFlag;
1431 unsigned DestReg = TailCallByValClobberedVRegs[i].first;
1432 unsigned VirtReg = TailCallByValClobberedVRegs[i].second;
1433 MVT::ValueType VT = TailCallByValClobberedVRegTypes[i];
1434 SDOperand Tmp = DAG.getCopyFromReg(Chain, VirtReg, VT, InFlag);
1435 Chain = DAG.getCopyToReg(Chain, DestReg, Tmp, InFlag);
1436 RegOpChains.push_back(Chain);
1437 }
1438 if (!RegOpChains.empty())
1439 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1440 &RegOpChains[0], RegOpChains.size());
1441 return Chain;
1442}
Evan Cheng931a8f42008-01-29 19:34:22 +00001443
Gordon Henriksen18ace102008-01-05 16:56:59 +00001444SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1445 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001446 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001448 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001450 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1451 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001453 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001454 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001455
1456 assert(!(isVarArg && CC == CallingConv::Fast) &&
1457 "Var args not supported with calling convention fastcc");
1458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001459 // Analyze operands of the call, assigning locations to each operand.
1460 SmallVector<CCValAssign, 16> ArgLocs;
1461 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001462 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463
1464 // Get a count of how many bytes are to be pushed on the stack.
1465 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001466 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001467 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468
Gordon Henriksen18ace102008-01-05 16:56:59 +00001469 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1470 // arguments and the arguments after the retaddr has been pushed are aligned.
1471 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1472 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1473 (NumBytes & 7) == 0)
1474 NumBytes += 4;
1475
1476 int FPDiff = 0;
1477 if (IsTailCall) {
1478 // Lower arguments at fp - stackoffset + fpdiff.
1479 unsigned NumBytesCallerPushed =
1480 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1481 FPDiff = NumBytesCallerPushed - NumBytes;
1482
1483 // Set the delta of movement of the returnaddr stackslot.
1484 // But only set if delta is greater than previous delta.
1485 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1486 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1487 }
1488
Chris Lattner5872a362008-01-17 07:00:52 +00001489 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001491 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001492 // Load return adress for tail calls.
1493 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1494 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001495
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001496 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001497 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001498
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499 SmallVector<SDOperand, 8> MemOpChains;
1500
1501 SDOperand StackPtr;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001502 bool containsTailCallByValArg = false;
1503 SmallVector<std::pair<unsigned, unsigned>, 8> TailCallByValClobberedVRegs;
1504 SmallVector<MVT::ValueType, 8> TailCallByValClobberedVRegTypes;
1505
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001507 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001508 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1510 CCValAssign &VA = ArgLocs[i];
1511 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001512 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1513 getArgFlags().isByVal();
1514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 // Promote the value if needed.
1516 switch (VA.getLocInfo()) {
1517 default: assert(0 && "Unknown loc info!");
1518 case CCValAssign::Full: break;
1519 case CCValAssign::SExt:
1520 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1521 break;
1522 case CCValAssign::ZExt:
1523 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1524 break;
1525 case CCValAssign::AExt:
1526 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1527 break;
1528 }
1529
1530 if (VA.isRegLoc()) {
1531 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1532 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001533 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001534 assert(VA.isMemLoc());
1535 if (StackPtr.Val == 0)
1536 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1537
1538 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1539 Arg));
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001540 // Remember fact that this call contains byval arguments.
1541 containsTailCallByValArg |= IsTailCall && isByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001542 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1543 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001544 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 }
1546 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547
1548 if (!MemOpChains.empty())
1549 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1550 &MemOpChains[0], MemOpChains.size());
1551
1552 // Build a sequence of copy-to-reg nodes chained together with token chain
1553 // and flag operands which copy the outgoing args into registers.
1554 SDOperand InFlag;
1555 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001556 // Tail call byval lowering might overwrite argument registers so arguments
1557 // passed to be copied to a virtual register for
1558 // later processing.
1559 if (CopyTailCallByValClobberedRegToVirtReg(containsTailCallByValArg,
1560 TailCallByValClobberedVRegs,
1561 TailCallByValClobberedVRegTypes,
1562 RegsToPass[i], Chain, InFlag, MF,
1563 DAG))
1564 continue;
1565
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1567 InFlag);
1568 InFlag = Chain.getValue(1);
1569 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001572 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001573 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1574 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1575 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1576 InFlag);
1577 InFlag = Chain.getValue(1);
1578 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001579 // If we are tail calling and generating PIC/GOT style code load the address
1580 // of the callee into ecx. The value in ecx is used as target of the tail
1581 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1582 // calls on PIC/GOT architectures. Normally we would just put the address of
1583 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1584 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001585 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001586 // Note: The actual moving to ecx is done further down.
1587 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1588 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1589 !G->getGlobal()->hasProtectedVisibility())
1590 Callee = LowerGlobalAddress(Callee, DAG);
1591 else if (isa<ExternalSymbolSDNode>(Callee))
1592 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001594
Gordon Henriksen18ace102008-01-05 16:56:59 +00001595 if (Is64Bit && isVarArg) {
1596 // From AMD64 ABI document:
1597 // For calls that may call functions that use varargs or stdargs
1598 // (prototype-less calls or calls to functions containing ellipsis (...) in
1599 // the declaration) %al is used as hidden argument to specify the number
1600 // of SSE registers used. The contents of %al do not need to match exactly
1601 // the number of registers, but must be an ubound on the number of SSE
1602 // registers used and is in the range 0 - 8 inclusive.
1603
1604 // Count the number of XMM registers allocated.
1605 static const unsigned XMMArgRegs[] = {
1606 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1607 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1608 };
1609 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1610
1611 Chain = DAG.getCopyToReg(Chain, X86::AL,
1612 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1613 InFlag = Chain.getValue(1);
1614 }
1615
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001616
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001617 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001618 if (IsTailCall) {
1619 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001620 SDOperand FIN;
1621 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001622 // Do not flag preceeding copytoreg stuff together with the following stuff.
1623 InFlag = SDOperand();
1624
1625 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1626 DAG, MF, this);
1627
Gordon Henriksen18ace102008-01-05 16:56:59 +00001628 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1629 CCValAssign &VA = ArgLocs[i];
1630 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001631 assert(VA.isMemLoc());
1632 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001633 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001634 ISD::ArgFlagsTy Flags =
1635 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001636 // Create frame index.
1637 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1638 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1639 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001640 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001641
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001642 // Find virtual register for this argument.
1643 bool Found=false;
1644 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1645 if (TailCallClobberedVRegs[idx].first==i) {
1646 Arg = TailCallClobberedVRegs[idx].second;
1647 Found=true;
1648 break;
1649 }
1650 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1651 (Found==true && "No corresponding Argument was found"));
Duncan Sandsc93fae32008-03-21 09:14:45 +00001652
1653 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001654 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001655 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1656 if (StackPtr.Val == 0)
1657 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1658 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1659
1660 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001661 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001662 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001663 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001664 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001665 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001666 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001667 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001668 }
1669 }
1670
1671 if (!MemOpChains2.empty())
1672 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001673 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001674
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001675 // Restore byval lowering clobbered registers.
1676 Chain = RestoreTailCallByValClobberedRegs(DAG, Chain,
1677 TailCallByValClobberedVRegs,
1678 TailCallByValClobberedVRegTypes);
1679
Gordon Henriksen18ace102008-01-05 16:56:59 +00001680 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001681 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1682 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001683 }
1684
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001685 // If the callee is a GlobalAddress node (quite common, every direct call is)
1686 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1687 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1688 // We should use extra load for direct calls to dllimported functions in
1689 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001690 if ((IsTailCall || !Is64Bit ||
1691 getTargetMachine().getCodeModel() != CodeModel::Large)
1692 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1693 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001695 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 if (IsTailCall || !Is64Bit ||
1697 getTargetMachine().getCodeModel() != CodeModel::Large)
1698 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1699 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001700 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1701
1702 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001703 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001704 Callee,InFlag);
1705 Callee = DAG.getRegister(Opc, getPointerTy());
1706 // Add register as live out.
1707 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001708 }
1709
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001710 // Returns a chain & a flag for retval copy to use.
1711 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1712 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001713
1714 if (IsTailCall) {
1715 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001716 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1717 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001718 if (InFlag.Val)
1719 Ops.push_back(InFlag);
1720 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1721 InFlag = Chain.getValue(1);
1722
1723 // Returns a chain & a flag for retval copy to use.
1724 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1725 Ops.clear();
1726 }
1727
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001728 Ops.push_back(Chain);
1729 Ops.push_back(Callee);
1730
Gordon Henriksen18ace102008-01-05 16:56:59 +00001731 if (IsTailCall)
1732 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001733
Gordon Henriksen18ace102008-01-05 16:56:59 +00001734 // Add argument registers to the end of the list so that they are known live
1735 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001736 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1737 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1738 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001739
Evan Cheng8ba45e62008-03-18 23:36:35 +00001740 // Add an implicit use GOT pointer in EBX.
1741 if (!IsTailCall && !Is64Bit &&
1742 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1743 Subtarget->isPICStyleGOT())
1744 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1745
1746 // Add an implicit use of AL for x86 vararg functions.
1747 if (Is64Bit && isVarArg)
1748 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1749
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001750 if (InFlag.Val)
1751 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001752
Gordon Henriksen18ace102008-01-05 16:56:59 +00001753 if (IsTailCall) {
1754 assert(InFlag.Val &&
1755 "Flag must be set. Depend on flag being set in LowerRET");
1756 Chain = DAG.getNode(X86ISD::TAILCALL,
1757 Op.Val->getVTList(), &Ops[0], Ops.size());
1758
1759 return SDOperand(Chain.Val, Op.ResNo);
1760 }
1761
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001762 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001763 InFlag = Chain.getValue(1);
1764
1765 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001766 unsigned NumBytesForCalleeToPush;
1767 if (IsCalleePop(Op))
1768 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001769 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001770 // If this is is a call to a struct-return function, the callee
1771 // pops the hidden struct pointer, so we have to push it back.
1772 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001773 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001774 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001775 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001776
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001777 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001778 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001779 DAG.getIntPtrConstant(NumBytes),
1780 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001781 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782 InFlag = Chain.getValue(1);
1783
1784 // Handle result values, copying them out of physregs into vregs that we
1785 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001786 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001787}
1788
1789
1790//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001791// Fast Calling Convention (tail call) implementation
1792//===----------------------------------------------------------------------===//
1793
1794// Like std call, callee cleans arguments, convention except that ECX is
1795// reserved for storing the tail called function address. Only 2 registers are
1796// free for argument passing (inreg). Tail call optimization is performed
1797// provided:
1798// * tailcallopt is enabled
1799// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001800// On X86_64 architecture with GOT-style position independent code only local
1801// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001802// To keep the stack aligned according to platform abi the function
1803// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1804// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001805// If a tail called function callee has more arguments than the caller the
1806// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001807// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001808// original REtADDR, but before the saved framepointer or the spilled registers
1809// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1810// stack layout:
1811// arg1
1812// arg2
1813// RETADDR
1814// [ new RETADDR
1815// move area ]
1816// (possible EBP)
1817// ESI
1818// EDI
1819// local1 ..
1820
1821/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1822/// for a 16 byte align requirement.
1823unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1824 SelectionDAG& DAG) {
1825 if (PerformTailCallOpt) {
1826 MachineFunction &MF = DAG.getMachineFunction();
1827 const TargetMachine &TM = MF.getTarget();
1828 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1829 unsigned StackAlignment = TFI.getStackAlignment();
1830 uint64_t AlignMask = StackAlignment - 1;
1831 int64_t Offset = StackSize;
1832 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1833 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1834 // Number smaller than 12 so just add the difference.
1835 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1836 } else {
1837 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1838 Offset = ((~AlignMask) & Offset) + StackAlignment +
1839 (StackAlignment-SlotSize);
1840 }
1841 StackSize = Offset;
1842 }
1843 return StackSize;
1844}
1845
1846/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001847/// following the call is a return. A function is eligible if caller/callee
1848/// calling conventions match, currently only fastcc supports tail calls, and
1849/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001850bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1851 SDOperand Ret,
1852 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001853 if (!PerformTailCallOpt)
1854 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001855
1856 // Check whether CALL node immediatly preceeds the RET node and whether the
1857 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001858 unsigned NumOps = Ret.getNumOperands();
1859 if ((NumOps == 1 &&
1860 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1861 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001862 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001863 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1864 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001865 MachineFunction &MF = DAG.getMachineFunction();
1866 unsigned CallerCC = MF.getFunction()->getCallingConv();
1867 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1868 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1869 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001870 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001871 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001872 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001873 return true;
1874
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001875 // Can only do local tail calls (in same module, hidden or protected) on
1876 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001877 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1878 return G->getGlobal()->hasHiddenVisibility()
1879 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001880 }
1881 }
Evan Chenge7a87392007-11-02 01:26:22 +00001882
1883 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001884}
1885
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001886//===----------------------------------------------------------------------===//
1887// Other Lowering Hooks
1888//===----------------------------------------------------------------------===//
1889
1890
1891SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001892 MachineFunction &MF = DAG.getMachineFunction();
1893 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1894 int ReturnAddrIndex = FuncInfo->getRAIndex();
1895
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001896 if (ReturnAddrIndex == 0) {
1897 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 if (Subtarget->is64Bit())
1899 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1900 else
1901 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001902
1903 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001904 }
1905
1906 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1907}
1908
1909
1910
1911/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1912/// specific condition code. It returns a false if it cannot do a direct
1913/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1914/// needed.
1915static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1916 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1917 SelectionDAG &DAG) {
1918 X86CC = X86::COND_INVALID;
1919 if (!isFP) {
1920 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1921 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1922 // X > -1 -> X == 0, jump !sign.
1923 RHS = DAG.getConstant(0, RHS.getValueType());
1924 X86CC = X86::COND_NS;
1925 return true;
1926 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1927 // X < 0 -> X == 0, jump on sign.
1928 X86CC = X86::COND_S;
1929 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001930 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1931 // X < 1 -> X <= 0
1932 RHS = DAG.getConstant(0, RHS.getValueType());
1933 X86CC = X86::COND_LE;
1934 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001935 }
1936 }
1937
1938 switch (SetCCOpcode) {
1939 default: break;
1940 case ISD::SETEQ: X86CC = X86::COND_E; break;
1941 case ISD::SETGT: X86CC = X86::COND_G; break;
1942 case ISD::SETGE: X86CC = X86::COND_GE; break;
1943 case ISD::SETLT: X86CC = X86::COND_L; break;
1944 case ISD::SETLE: X86CC = X86::COND_LE; break;
1945 case ISD::SETNE: X86CC = X86::COND_NE; break;
1946 case ISD::SETULT: X86CC = X86::COND_B; break;
1947 case ISD::SETUGT: X86CC = X86::COND_A; break;
1948 case ISD::SETULE: X86CC = X86::COND_BE; break;
1949 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1950 }
1951 } else {
1952 // On a floating point condition, the flags are set as follows:
1953 // ZF PF CF op
1954 // 0 | 0 | 0 | X > Y
1955 // 0 | 0 | 1 | X < Y
1956 // 1 | 0 | 0 | X == Y
1957 // 1 | 1 | 1 | unordered
1958 bool Flip = false;
1959 switch (SetCCOpcode) {
1960 default: break;
1961 case ISD::SETUEQ:
1962 case ISD::SETEQ: X86CC = X86::COND_E; break;
1963 case ISD::SETOLT: Flip = true; // Fallthrough
1964 case ISD::SETOGT:
1965 case ISD::SETGT: X86CC = X86::COND_A; break;
1966 case ISD::SETOLE: Flip = true; // Fallthrough
1967 case ISD::SETOGE:
1968 case ISD::SETGE: X86CC = X86::COND_AE; break;
1969 case ISD::SETUGT: Flip = true; // Fallthrough
1970 case ISD::SETULT:
1971 case ISD::SETLT: X86CC = X86::COND_B; break;
1972 case ISD::SETUGE: Flip = true; // Fallthrough
1973 case ISD::SETULE:
1974 case ISD::SETLE: X86CC = X86::COND_BE; break;
1975 case ISD::SETONE:
1976 case ISD::SETNE: X86CC = X86::COND_NE; break;
1977 case ISD::SETUO: X86CC = X86::COND_P; break;
1978 case ISD::SETO: X86CC = X86::COND_NP; break;
1979 }
1980 if (Flip)
1981 std::swap(LHS, RHS);
1982 }
1983
1984 return X86CC != X86::COND_INVALID;
1985}
1986
1987/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1988/// code. Current x86 isa includes the following FP cmov instructions:
1989/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1990static bool hasFPCMov(unsigned X86CC) {
1991 switch (X86CC) {
1992 default:
1993 return false;
1994 case X86::COND_B:
1995 case X86::COND_BE:
1996 case X86::COND_E:
1997 case X86::COND_P:
1998 case X86::COND_A:
1999 case X86::COND_AE:
2000 case X86::COND_NE:
2001 case X86::COND_NP:
2002 return true;
2003 }
2004}
2005
2006/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2007/// true if Op is undef or if its value falls within the specified range (L, H].
2008static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2009 if (Op.getOpcode() == ISD::UNDEF)
2010 return true;
2011
2012 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2013 return (Val >= Low && Val < Hi);
2014}
2015
2016/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2017/// true if Op is undef or if its value equal to the specified value.
2018static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2019 if (Op.getOpcode() == ISD::UNDEF)
2020 return true;
2021 return cast<ConstantSDNode>(Op)->getValue() == Val;
2022}
2023
2024/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2025/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2026bool X86::isPSHUFDMask(SDNode *N) {
2027 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2028
Dan Gohman7dc19012007-08-02 21:17:01 +00002029 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 return false;
2031
2032 // Check if the value doesn't reference the second vector.
2033 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2034 SDOperand Arg = N->getOperand(i);
2035 if (Arg.getOpcode() == ISD::UNDEF) continue;
2036 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002037 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002038 return false;
2039 }
2040
2041 return true;
2042}
2043
2044/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2045/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2046bool X86::isPSHUFHWMask(SDNode *N) {
2047 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2048
2049 if (N->getNumOperands() != 8)
2050 return false;
2051
2052 // Lower quadword copied in order.
2053 for (unsigned i = 0; i != 4; ++i) {
2054 SDOperand Arg = N->getOperand(i);
2055 if (Arg.getOpcode() == ISD::UNDEF) continue;
2056 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2057 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2058 return false;
2059 }
2060
2061 // Upper quadword shuffled.
2062 for (unsigned i = 4; i != 8; ++i) {
2063 SDOperand Arg = N->getOperand(i);
2064 if (Arg.getOpcode() == ISD::UNDEF) continue;
2065 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2066 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2067 if (Val < 4 || Val > 7)
2068 return false;
2069 }
2070
2071 return true;
2072}
2073
2074/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2075/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2076bool X86::isPSHUFLWMask(SDNode *N) {
2077 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2078
2079 if (N->getNumOperands() != 8)
2080 return false;
2081
2082 // Upper quadword copied in order.
2083 for (unsigned i = 4; i != 8; ++i)
2084 if (!isUndefOrEqual(N->getOperand(i), i))
2085 return false;
2086
2087 // Lower quadword shuffled.
2088 for (unsigned i = 0; i != 4; ++i)
2089 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2090 return false;
2091
2092 return true;
2093}
2094
2095/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2096/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002097static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002098 if (NumElems != 2 && NumElems != 4) return false;
2099
2100 unsigned Half = NumElems / 2;
2101 for (unsigned i = 0; i < Half; ++i)
2102 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2103 return false;
2104 for (unsigned i = Half; i < NumElems; ++i)
2105 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2106 return false;
2107
2108 return true;
2109}
2110
2111bool X86::isSHUFPMask(SDNode *N) {
2112 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2113 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2114}
2115
2116/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2117/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2118/// half elements to come from vector 1 (which would equal the dest.) and
2119/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002120static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 if (NumOps != 2 && NumOps != 4) return false;
2122
2123 unsigned Half = NumOps / 2;
2124 for (unsigned i = 0; i < Half; ++i)
2125 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2126 return false;
2127 for (unsigned i = Half; i < NumOps; ++i)
2128 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2129 return false;
2130 return true;
2131}
2132
2133static bool isCommutedSHUFP(SDNode *N) {
2134 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2135 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2136}
2137
2138/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2139/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2140bool X86::isMOVHLPSMask(SDNode *N) {
2141 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2142
2143 if (N->getNumOperands() != 4)
2144 return false;
2145
2146 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2147 return isUndefOrEqual(N->getOperand(0), 6) &&
2148 isUndefOrEqual(N->getOperand(1), 7) &&
2149 isUndefOrEqual(N->getOperand(2), 2) &&
2150 isUndefOrEqual(N->getOperand(3), 3);
2151}
2152
2153/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2154/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2155/// <2, 3, 2, 3>
2156bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2157 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2158
2159 if (N->getNumOperands() != 4)
2160 return false;
2161
2162 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2163 return isUndefOrEqual(N->getOperand(0), 2) &&
2164 isUndefOrEqual(N->getOperand(1), 3) &&
2165 isUndefOrEqual(N->getOperand(2), 2) &&
2166 isUndefOrEqual(N->getOperand(3), 3);
2167}
2168
2169/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2170/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2171bool X86::isMOVLPMask(SDNode *N) {
2172 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2173
2174 unsigned NumElems = N->getNumOperands();
2175 if (NumElems != 2 && NumElems != 4)
2176 return false;
2177
2178 for (unsigned i = 0; i < NumElems/2; ++i)
2179 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2180 return false;
2181
2182 for (unsigned i = NumElems/2; i < NumElems; ++i)
2183 if (!isUndefOrEqual(N->getOperand(i), i))
2184 return false;
2185
2186 return true;
2187}
2188
2189/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2190/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2191/// and MOVLHPS.
2192bool X86::isMOVHPMask(SDNode *N) {
2193 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2194
2195 unsigned NumElems = N->getNumOperands();
2196 if (NumElems != 2 && NumElems != 4)
2197 return false;
2198
2199 for (unsigned i = 0; i < NumElems/2; ++i)
2200 if (!isUndefOrEqual(N->getOperand(i), i))
2201 return false;
2202
2203 for (unsigned i = 0; i < NumElems/2; ++i) {
2204 SDOperand Arg = N->getOperand(i + NumElems/2);
2205 if (!isUndefOrEqual(Arg, i + NumElems))
2206 return false;
2207 }
2208
2209 return true;
2210}
2211
2212/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2213/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002214bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002215 bool V2IsSplat = false) {
2216 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2217 return false;
2218
2219 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2220 SDOperand BitI = Elts[i];
2221 SDOperand BitI1 = Elts[i+1];
2222 if (!isUndefOrEqual(BitI, j))
2223 return false;
2224 if (V2IsSplat) {
2225 if (isUndefOrEqual(BitI1, NumElts))
2226 return false;
2227 } else {
2228 if (!isUndefOrEqual(BitI1, j + NumElts))
2229 return false;
2230 }
2231 }
2232
2233 return true;
2234}
2235
2236bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2237 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2238 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2239}
2240
2241/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2242/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002243bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 bool V2IsSplat = false) {
2245 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2246 return false;
2247
2248 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2249 SDOperand BitI = Elts[i];
2250 SDOperand BitI1 = Elts[i+1];
2251 if (!isUndefOrEqual(BitI, j + NumElts/2))
2252 return false;
2253 if (V2IsSplat) {
2254 if (isUndefOrEqual(BitI1, NumElts))
2255 return false;
2256 } else {
2257 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2258 return false;
2259 }
2260 }
2261
2262 return true;
2263}
2264
2265bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2266 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2267 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2268}
2269
2270/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2271/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2272/// <0, 0, 1, 1>
2273bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2274 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2275
2276 unsigned NumElems = N->getNumOperands();
2277 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2278 return false;
2279
2280 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2281 SDOperand BitI = N->getOperand(i);
2282 SDOperand BitI1 = N->getOperand(i+1);
2283
2284 if (!isUndefOrEqual(BitI, j))
2285 return false;
2286 if (!isUndefOrEqual(BitI1, j))
2287 return false;
2288 }
2289
2290 return true;
2291}
2292
2293/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2294/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2295/// <2, 2, 3, 3>
2296bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2297 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2298
2299 unsigned NumElems = N->getNumOperands();
2300 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2301 return false;
2302
2303 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2304 SDOperand BitI = N->getOperand(i);
2305 SDOperand BitI1 = N->getOperand(i + 1);
2306
2307 if (!isUndefOrEqual(BitI, j))
2308 return false;
2309 if (!isUndefOrEqual(BitI1, j))
2310 return false;
2311 }
2312
2313 return true;
2314}
2315
2316/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2317/// specifies a shuffle of elements that is suitable for input to MOVSS,
2318/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002319static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002320 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002321 return false;
2322
2323 if (!isUndefOrEqual(Elts[0], NumElts))
2324 return false;
2325
2326 for (unsigned i = 1; i < NumElts; ++i) {
2327 if (!isUndefOrEqual(Elts[i], i))
2328 return false;
2329 }
2330
2331 return true;
2332}
2333
2334bool X86::isMOVLMask(SDNode *N) {
2335 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2336 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2337}
2338
2339/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2340/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2341/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002342static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002343 bool V2IsSplat = false,
2344 bool V2IsUndef = false) {
2345 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2346 return false;
2347
2348 if (!isUndefOrEqual(Ops[0], 0))
2349 return false;
2350
2351 for (unsigned i = 1; i < NumOps; ++i) {
2352 SDOperand Arg = Ops[i];
2353 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2354 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2355 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2356 return false;
2357 }
2358
2359 return true;
2360}
2361
2362static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2363 bool V2IsUndef = false) {
2364 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2365 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2366 V2IsSplat, V2IsUndef);
2367}
2368
2369/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2370/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2371bool X86::isMOVSHDUPMask(SDNode *N) {
2372 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2373
2374 if (N->getNumOperands() != 4)
2375 return false;
2376
2377 // Expect 1, 1, 3, 3
2378 for (unsigned i = 0; i < 2; ++i) {
2379 SDOperand Arg = N->getOperand(i);
2380 if (Arg.getOpcode() == ISD::UNDEF) continue;
2381 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2382 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2383 if (Val != 1) return false;
2384 }
2385
2386 bool HasHi = false;
2387 for (unsigned i = 2; i < 4; ++i) {
2388 SDOperand Arg = N->getOperand(i);
2389 if (Arg.getOpcode() == ISD::UNDEF) continue;
2390 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2391 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2392 if (Val != 3) return false;
2393 HasHi = true;
2394 }
2395
2396 // Don't use movshdup if it can be done with a shufps.
2397 return HasHi;
2398}
2399
2400/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2401/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2402bool X86::isMOVSLDUPMask(SDNode *N) {
2403 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2404
2405 if (N->getNumOperands() != 4)
2406 return false;
2407
2408 // Expect 0, 0, 2, 2
2409 for (unsigned i = 0; i < 2; ++i) {
2410 SDOperand Arg = N->getOperand(i);
2411 if (Arg.getOpcode() == ISD::UNDEF) continue;
2412 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2413 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2414 if (Val != 0) return false;
2415 }
2416
2417 bool HasHi = false;
2418 for (unsigned i = 2; i < 4; ++i) {
2419 SDOperand Arg = N->getOperand(i);
2420 if (Arg.getOpcode() == ISD::UNDEF) continue;
2421 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2422 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2423 if (Val != 2) return false;
2424 HasHi = true;
2425 }
2426
2427 // Don't use movshdup if it can be done with a shufps.
2428 return HasHi;
2429}
2430
2431/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2432/// specifies a identity operation on the LHS or RHS.
2433static bool isIdentityMask(SDNode *N, bool RHS = false) {
2434 unsigned NumElems = N->getNumOperands();
2435 for (unsigned i = 0; i < NumElems; ++i)
2436 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2437 return false;
2438 return true;
2439}
2440
2441/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2442/// a splat of a single element.
2443static bool isSplatMask(SDNode *N) {
2444 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2445
2446 // This is a splat operation if each element of the permute is the same, and
2447 // if the value doesn't reference the second vector.
2448 unsigned NumElems = N->getNumOperands();
2449 SDOperand ElementBase;
2450 unsigned i = 0;
2451 for (; i != NumElems; ++i) {
2452 SDOperand Elt = N->getOperand(i);
2453 if (isa<ConstantSDNode>(Elt)) {
2454 ElementBase = Elt;
2455 break;
2456 }
2457 }
2458
2459 if (!ElementBase.Val)
2460 return false;
2461
2462 for (; i != NumElems; ++i) {
2463 SDOperand Arg = N->getOperand(i);
2464 if (Arg.getOpcode() == ISD::UNDEF) continue;
2465 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2466 if (Arg != ElementBase) return false;
2467 }
2468
2469 // Make sure it is a splat of the first vector operand.
2470 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2471}
2472
2473/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2474/// a splat of a single element and it's a 2 or 4 element mask.
2475bool X86::isSplatMask(SDNode *N) {
2476 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2477
2478 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2479 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2480 return false;
2481 return ::isSplatMask(N);
2482}
2483
2484/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2485/// specifies a splat of zero element.
2486bool X86::isSplatLoMask(SDNode *N) {
2487 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2488
2489 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2490 if (!isUndefOrEqual(N->getOperand(i), 0))
2491 return false;
2492 return true;
2493}
2494
2495/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2496/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2497/// instructions.
2498unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2499 unsigned NumOperands = N->getNumOperands();
2500 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2501 unsigned Mask = 0;
2502 for (unsigned i = 0; i < NumOperands; ++i) {
2503 unsigned Val = 0;
2504 SDOperand Arg = N->getOperand(NumOperands-i-1);
2505 if (Arg.getOpcode() != ISD::UNDEF)
2506 Val = cast<ConstantSDNode>(Arg)->getValue();
2507 if (Val >= NumOperands) Val -= NumOperands;
2508 Mask |= Val;
2509 if (i != NumOperands - 1)
2510 Mask <<= Shift;
2511 }
2512
2513 return Mask;
2514}
2515
2516/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2517/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2518/// instructions.
2519unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2520 unsigned Mask = 0;
2521 // 8 nodes, but we only care about the last 4.
2522 for (unsigned i = 7; i >= 4; --i) {
2523 unsigned Val = 0;
2524 SDOperand Arg = N->getOperand(i);
2525 if (Arg.getOpcode() != ISD::UNDEF)
2526 Val = cast<ConstantSDNode>(Arg)->getValue();
2527 Mask |= (Val - 4);
2528 if (i != 4)
2529 Mask <<= 2;
2530 }
2531
2532 return Mask;
2533}
2534
2535/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2536/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2537/// instructions.
2538unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2539 unsigned Mask = 0;
2540 // 8 nodes, but we only care about the first 4.
2541 for (int i = 3; i >= 0; --i) {
2542 unsigned Val = 0;
2543 SDOperand Arg = N->getOperand(i);
2544 if (Arg.getOpcode() != ISD::UNDEF)
2545 Val = cast<ConstantSDNode>(Arg)->getValue();
2546 Mask |= Val;
2547 if (i != 0)
2548 Mask <<= 2;
2549 }
2550
2551 return Mask;
2552}
2553
2554/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2555/// specifies a 8 element shuffle that can be broken into a pair of
2556/// PSHUFHW and PSHUFLW.
2557static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2558 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2559
2560 if (N->getNumOperands() != 8)
2561 return false;
2562
2563 // Lower quadword shuffled.
2564 for (unsigned i = 0; i != 4; ++i) {
2565 SDOperand Arg = N->getOperand(i);
2566 if (Arg.getOpcode() == ISD::UNDEF) continue;
2567 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2568 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002569 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 return false;
2571 }
2572
2573 // Upper quadword shuffled.
2574 for (unsigned i = 4; i != 8; ++i) {
2575 SDOperand Arg = N->getOperand(i);
2576 if (Arg.getOpcode() == ISD::UNDEF) continue;
2577 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2578 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2579 if (Val < 4 || Val > 7)
2580 return false;
2581 }
2582
2583 return true;
2584}
2585
Chris Lattnere6aa3862007-11-25 00:24:49 +00002586/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002587/// values in ther permute mask.
2588static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2589 SDOperand &V2, SDOperand &Mask,
2590 SelectionDAG &DAG) {
2591 MVT::ValueType VT = Op.getValueType();
2592 MVT::ValueType MaskVT = Mask.getValueType();
2593 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2594 unsigned NumElems = Mask.getNumOperands();
2595 SmallVector<SDOperand, 8> MaskVec;
2596
2597 for (unsigned i = 0; i != NumElems; ++i) {
2598 SDOperand Arg = Mask.getOperand(i);
2599 if (Arg.getOpcode() == ISD::UNDEF) {
2600 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2601 continue;
2602 }
2603 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2604 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2605 if (Val < NumElems)
2606 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2607 else
2608 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2609 }
2610
2611 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002612 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002613 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2614}
2615
Evan Chenga6769df2007-12-07 21:30:01 +00002616/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2617/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002618static
2619SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2620 MVT::ValueType MaskVT = Mask.getValueType();
2621 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2622 unsigned NumElems = Mask.getNumOperands();
2623 SmallVector<SDOperand, 8> MaskVec;
2624 for (unsigned i = 0; i != NumElems; ++i) {
2625 SDOperand Arg = Mask.getOperand(i);
2626 if (Arg.getOpcode() == ISD::UNDEF) {
2627 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2628 continue;
2629 }
2630 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2631 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2632 if (Val < NumElems)
2633 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2634 else
2635 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2636 }
2637 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2638}
2639
2640
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002641/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2642/// match movhlps. The lower half elements should come from upper half of
2643/// V1 (and in order), and the upper half elements should come from the upper
2644/// half of V2 (and in order).
2645static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2646 unsigned NumElems = Mask->getNumOperands();
2647 if (NumElems != 4)
2648 return false;
2649 for (unsigned i = 0, e = 2; i != e; ++i)
2650 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2651 return false;
2652 for (unsigned i = 2; i != 4; ++i)
2653 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2654 return false;
2655 return true;
2656}
2657
2658/// isScalarLoadToVector - Returns true if the node is a scalar load that
2659/// is promoted to a vector.
2660static inline bool isScalarLoadToVector(SDNode *N) {
2661 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2662 N = N->getOperand(0).Val;
2663 return ISD::isNON_EXTLoad(N);
2664 }
2665 return false;
2666}
2667
2668/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2669/// match movlp{s|d}. The lower half elements should come from lower half of
2670/// V1 (and in order), and the upper half elements should come from the upper
2671/// half of V2 (and in order). And since V1 will become the source of the
2672/// MOVLP, it must be either a vector load or a scalar load to vector.
2673static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2674 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2675 return false;
2676 // Is V2 is a vector load, don't do this transformation. We will try to use
2677 // load folding shufps op.
2678 if (ISD::isNON_EXTLoad(V2))
2679 return false;
2680
2681 unsigned NumElems = Mask->getNumOperands();
2682 if (NumElems != 2 && NumElems != 4)
2683 return false;
2684 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2685 if (!isUndefOrEqual(Mask->getOperand(i), i))
2686 return false;
2687 for (unsigned i = NumElems/2; i != NumElems; ++i)
2688 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2689 return false;
2690 return true;
2691}
2692
2693/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2694/// all the same.
2695static bool isSplatVector(SDNode *N) {
2696 if (N->getOpcode() != ISD::BUILD_VECTOR)
2697 return false;
2698
2699 SDOperand SplatValue = N->getOperand(0);
2700 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2701 if (N->getOperand(i) != SplatValue)
2702 return false;
2703 return true;
2704}
2705
2706/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2707/// to an undef.
2708static bool isUndefShuffle(SDNode *N) {
2709 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2710 return false;
2711
2712 SDOperand V1 = N->getOperand(0);
2713 SDOperand V2 = N->getOperand(1);
2714 SDOperand Mask = N->getOperand(2);
2715 unsigned NumElems = Mask.getNumOperands();
2716 for (unsigned i = 0; i != NumElems; ++i) {
2717 SDOperand Arg = Mask.getOperand(i);
2718 if (Arg.getOpcode() != ISD::UNDEF) {
2719 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2720 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2721 return false;
2722 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2723 return false;
2724 }
2725 }
2726 return true;
2727}
2728
2729/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2730/// constant +0.0.
2731static inline bool isZeroNode(SDOperand Elt) {
2732 return ((isa<ConstantSDNode>(Elt) &&
2733 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2734 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002735 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002736}
2737
2738/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2739/// to an zero vector.
2740static bool isZeroShuffle(SDNode *N) {
2741 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2742 return false;
2743
2744 SDOperand V1 = N->getOperand(0);
2745 SDOperand V2 = N->getOperand(1);
2746 SDOperand Mask = N->getOperand(2);
2747 unsigned NumElems = Mask.getNumOperands();
2748 for (unsigned i = 0; i != NumElems; ++i) {
2749 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002750 if (Arg.getOpcode() == ISD::UNDEF)
2751 continue;
2752
2753 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2754 if (Idx < NumElems) {
2755 unsigned Opc = V1.Val->getOpcode();
2756 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2757 continue;
2758 if (Opc != ISD::BUILD_VECTOR ||
2759 !isZeroNode(V1.Val->getOperand(Idx)))
2760 return false;
2761 } else if (Idx >= NumElems) {
2762 unsigned Opc = V2.Val->getOpcode();
2763 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2764 continue;
2765 if (Opc != ISD::BUILD_VECTOR ||
2766 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2767 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 }
2769 }
2770 return true;
2771}
2772
2773/// getZeroVector - Returns a vector of specified type with all zero elements.
2774///
2775static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2776 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002777
2778 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2779 // type. This ensures they get CSE'd.
2780 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2781 SDOperand Vec;
2782 if (MVT::getSizeInBits(VT) == 64) // MMX
2783 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2784 else // SSE
2785 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2786 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002787}
2788
Chris Lattnere6aa3862007-11-25 00:24:49 +00002789/// getOnesVector - Returns a vector of specified type with all bits set.
2790///
2791static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2792 assert(MVT::isVector(VT) && "Expected a vector type");
2793
2794 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2795 // type. This ensures they get CSE'd.
2796 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2797 SDOperand Vec;
2798 if (MVT::getSizeInBits(VT) == 64) // MMX
2799 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2800 else // SSE
2801 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2802 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2803}
2804
2805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002806/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2807/// that point to V2 points to its first element.
2808static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2809 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2810
2811 bool Changed = false;
2812 SmallVector<SDOperand, 8> MaskVec;
2813 unsigned NumElems = Mask.getNumOperands();
2814 for (unsigned i = 0; i != NumElems; ++i) {
2815 SDOperand Arg = Mask.getOperand(i);
2816 if (Arg.getOpcode() != ISD::UNDEF) {
2817 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2818 if (Val > NumElems) {
2819 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2820 Changed = true;
2821 }
2822 }
2823 MaskVec.push_back(Arg);
2824 }
2825
2826 if (Changed)
2827 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2828 &MaskVec[0], MaskVec.size());
2829 return Mask;
2830}
2831
2832/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2833/// operation of specified width.
2834static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2835 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2836 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2837
2838 SmallVector<SDOperand, 8> MaskVec;
2839 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2840 for (unsigned i = 1; i != NumElems; ++i)
2841 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2842 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2843}
2844
2845/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2846/// of specified width.
2847static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2848 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2849 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2850 SmallVector<SDOperand, 8> MaskVec;
2851 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2852 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2853 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2854 }
2855 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2856}
2857
2858/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2859/// of specified width.
2860static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2861 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2862 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2863 unsigned Half = NumElems/2;
2864 SmallVector<SDOperand, 8> MaskVec;
2865 for (unsigned i = 0; i != Half; ++i) {
2866 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2867 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2868 }
2869 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2870}
2871
Chris Lattner2d91b962008-03-09 01:05:04 +00002872/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2873/// element #0 of a vector with the specified index, leaving the rest of the
2874/// elements in place.
2875static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2876 SelectionDAG &DAG) {
2877 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2878 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2879 SmallVector<SDOperand, 8> MaskVec;
2880 // Element #0 of the result gets the elt we are replacing.
2881 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2882 for (unsigned i = 1; i != NumElems; ++i)
2883 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2884 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2885}
2886
Evan Chengbf8b2c52008-04-05 00:30:36 +00002887/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2888static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
2889 MVT::ValueType PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2890 MVT::ValueType VT = Op.getValueType();
2891 if (PVT == VT)
2892 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893 SDOperand V1 = Op.getOperand(0);
2894 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002896 // Special handling of v4f32 -> v4i32.
2897 if (VT != MVT::v4f32) {
2898 Mask = getUnpacklMask(NumElems, DAG);
2899 while (NumElems > 4) {
2900 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2901 NumElems >>= 1;
2902 }
2903 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002905
Evan Chengbf8b2c52008-04-05 00:30:36 +00002906 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2907 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2908 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2910}
2911
2912/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002913/// vector of zero or undef vector. This produces a shuffle where the low
2914/// element of V2 is swizzled into the zero/undef vector, landing at element
2915/// 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 +00002916static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 bool isZero, SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002918 MVT::ValueType VT = V2.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002919 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002920 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2922 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002923 SmallVector<SDOperand, 16> MaskVec;
2924 for (unsigned i = 0; i != NumElems; ++i)
2925 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2926 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2927 else
2928 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2930 &MaskVec[0], MaskVec.size());
2931 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2932}
2933
2934/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2935///
2936static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2937 unsigned NumNonZero, unsigned NumZero,
2938 SelectionDAG &DAG, TargetLowering &TLI) {
2939 if (NumNonZero > 8)
2940 return SDOperand();
2941
2942 SDOperand V(0, 0);
2943 bool First = true;
2944 for (unsigned i = 0; i < 16; ++i) {
2945 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2946 if (ThisIsNonZero && First) {
2947 if (NumZero)
2948 V = getZeroVector(MVT::v8i16, DAG);
2949 else
2950 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2951 First = false;
2952 }
2953
2954 if ((i & 1) != 0) {
2955 SDOperand ThisElt(0, 0), LastElt(0, 0);
2956 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2957 if (LastIsNonZero) {
2958 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2959 }
2960 if (ThisIsNonZero) {
2961 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2962 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2963 ThisElt, DAG.getConstant(8, MVT::i8));
2964 if (LastIsNonZero)
2965 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2966 } else
2967 ThisElt = LastElt;
2968
2969 if (ThisElt.Val)
2970 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002971 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002972 }
2973 }
2974
2975 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2976}
2977
2978/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2979///
2980static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2981 unsigned NumNonZero, unsigned NumZero,
2982 SelectionDAG &DAG, TargetLowering &TLI) {
2983 if (NumNonZero > 4)
2984 return SDOperand();
2985
2986 SDOperand V(0, 0);
2987 bool First = true;
2988 for (unsigned i = 0; i < 8; ++i) {
2989 bool isNonZero = (NonZeros & (1 << i)) != 0;
2990 if (isNonZero) {
2991 if (First) {
2992 if (NumZero)
2993 V = getZeroVector(MVT::v8i16, DAG);
2994 else
2995 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2996 First = false;
2997 }
2998 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002999 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000 }
3001 }
3002
3003 return V;
3004}
3005
3006SDOperand
3007X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003008 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3009 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3010 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3011 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3012 // eliminated on x86-32 hosts.
3013 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3014 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015
Chris Lattnere6aa3862007-11-25 00:24:49 +00003016 if (ISD::isBuildVectorAllOnes(Op.Val))
3017 return getOnesVector(Op.getValueType(), DAG);
3018 return getZeroVector(Op.getValueType(), DAG);
3019 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020
3021 MVT::ValueType VT = Op.getValueType();
3022 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3023 unsigned EVTBits = MVT::getSizeInBits(EVT);
3024
3025 unsigned NumElems = Op.getNumOperands();
3026 unsigned NumZero = 0;
3027 unsigned NumNonZero = 0;
3028 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003029 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003030 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 for (unsigned i = 0; i < NumElems; ++i) {
3032 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003033 if (Elt.getOpcode() == ISD::UNDEF)
3034 continue;
3035 Values.insert(Elt);
3036 if (Elt.getOpcode() != ISD::Constant &&
3037 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003038 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003039 if (isZeroNode(Elt))
3040 NumZero++;
3041 else {
3042 NonZeros |= (1 << i);
3043 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 }
3045 }
3046
3047 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003048 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3049 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050 }
3051
Chris Lattner66a4dda2008-03-09 05:42:06 +00003052 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003053 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054 unsigned Idx = CountTrailingZeros_32(NonZeros);
3055 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003056
Chris Lattner2d91b962008-03-09 01:05:04 +00003057 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3058 // the value are obviously zero, truncate the value to i32 and do the
3059 // insertion that way. Only do this if the value is non-constant or if the
3060 // value is a constant being inserted into element 0. It is cheaper to do
3061 // a constant pool load than it is to do a movd + shuffle.
3062 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3063 (!IsAllConstants || Idx == 0)) {
3064 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3065 // Handle MMX and SSE both.
3066 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3067 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3068
3069 // Truncate the value (which may itself be a constant) to i32, and
3070 // convert it to a vector with movd (S2V+shuffle to zero extend).
3071 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3072 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3073 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3074
3075 // Now we have our 32-bit value zero extended in the low element of
3076 // a vector. If Idx != 0, swizzle it into place.
3077 if (Idx != 0) {
3078 SDOperand Ops[] = {
3079 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3080 getSwapEltZeroMask(VecElts, Idx, DAG)
3081 };
3082 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3083 }
3084 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3085 }
3086 }
3087
Chris Lattnerac914892008-03-08 22:59:52 +00003088 // If we have a constant or non-constant insertion into the low element of
3089 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3090 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3091 // depending on what the source datatype is. Because we can only get here
3092 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3093 if (Idx == 0 &&
3094 // Don't do this for i64 values on x86-32.
3095 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003096 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003098 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003099 }
3100
3101 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003102 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103
Chris Lattnerac914892008-03-08 22:59:52 +00003104 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3105 // is a non-constant being inserted into an element other than the low one,
3106 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3107 // movd/movss) to move this into the low element, then shuffle it into
3108 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003109 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003110 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3111
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003112 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003113 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003114 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3115 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3116 SmallVector<SDOperand, 8> MaskVec;
3117 for (unsigned i = 0; i < NumElems; i++)
3118 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3119 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3120 &MaskVec[0], MaskVec.size());
3121 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3122 DAG.getNode(ISD::UNDEF, VT), Mask);
3123 }
3124 }
3125
Chris Lattner66a4dda2008-03-09 05:42:06 +00003126 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3127 if (Values.size() == 1)
3128 return SDOperand();
3129
Dan Gohman21463242007-07-24 22:55:08 +00003130 // A vector full of immediates; various special cases are already
3131 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003132 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003133 return SDOperand();
3134
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135 // Let legalizer expand 2-wide build_vectors.
3136 if (EVTBits == 64)
3137 return SDOperand();
3138
3139 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3140 if (EVTBits == 8 && NumElems == 16) {
3141 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3142 *this);
3143 if (V.Val) return V;
3144 }
3145
3146 if (EVTBits == 16 && NumElems == 8) {
3147 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3148 *this);
3149 if (V.Val) return V;
3150 }
3151
3152 // If element VT is == 32 bits, turn it into a number of shuffles.
3153 SmallVector<SDOperand, 8> V;
3154 V.resize(NumElems);
3155 if (NumElems == 4 && NumZero > 0) {
3156 for (unsigned i = 0; i < 4; ++i) {
3157 bool isZero = !(NonZeros & (1 << i));
3158 if (isZero)
3159 V[i] = getZeroVector(VT, DAG);
3160 else
3161 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3162 }
3163
3164 for (unsigned i = 0; i < 2; ++i) {
3165 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3166 default: break;
3167 case 0:
3168 V[i] = V[i*2]; // Must be a zero vector.
3169 break;
3170 case 1:
3171 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3172 getMOVLMask(NumElems, DAG));
3173 break;
3174 case 2:
3175 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3176 getMOVLMask(NumElems, DAG));
3177 break;
3178 case 3:
3179 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3180 getUnpacklMask(NumElems, DAG));
3181 break;
3182 }
3183 }
3184
3185 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3186 // clears the upper bits.
3187 // FIXME: we can do the same for v4f32 case when we know both parts of
3188 // the lower half come from scalar_to_vector (loadf32). We should do
3189 // that in post legalizer dag combiner with target specific hooks.
3190 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3191 return V[0];
3192 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3193 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3194 SmallVector<SDOperand, 8> MaskVec;
3195 bool Reverse = (NonZeros & 0x3) == 2;
3196 for (unsigned i = 0; i < 2; ++i)
3197 if (Reverse)
3198 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3199 else
3200 MaskVec.push_back(DAG.getConstant(i, EVT));
3201 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3202 for (unsigned i = 0; i < 2; ++i)
3203 if (Reverse)
3204 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3205 else
3206 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3207 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3208 &MaskVec[0], MaskVec.size());
3209 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3210 }
3211
3212 if (Values.size() > 2) {
3213 // Expand into a number of unpckl*.
3214 // e.g. for v4f32
3215 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3216 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3217 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3218 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3219 for (unsigned i = 0; i < NumElems; ++i)
3220 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3221 NumElems >>= 1;
3222 while (NumElems != 0) {
3223 for (unsigned i = 0; i < NumElems; ++i)
3224 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3225 UnpckMask);
3226 NumElems >>= 1;
3227 }
3228 return V[0];
3229 }
3230
3231 return SDOperand();
3232}
3233
Evan Chengfca29242007-12-07 08:07:39 +00003234static
3235SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3236 SDOperand PermMask, SelectionDAG &DAG,
3237 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003238 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003239 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3240 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003241 MVT::ValueType PtrVT = TLI.getPointerTy();
3242 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3243 PermMask.Val->op_end());
3244
3245 // First record which half of which vector the low elements come from.
3246 SmallVector<unsigned, 4> LowQuad(4);
3247 for (unsigned i = 0; i < 4; ++i) {
3248 SDOperand Elt = MaskElts[i];
3249 if (Elt.getOpcode() == ISD::UNDEF)
3250 continue;
3251 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3252 int QuadIdx = EltIdx / 4;
3253 ++LowQuad[QuadIdx];
3254 }
3255 int BestLowQuad = -1;
3256 unsigned MaxQuad = 1;
3257 for (unsigned i = 0; i < 4; ++i) {
3258 if (LowQuad[i] > MaxQuad) {
3259 BestLowQuad = i;
3260 MaxQuad = LowQuad[i];
3261 }
Evan Chengfca29242007-12-07 08:07:39 +00003262 }
3263
Evan Cheng75184a92007-12-11 01:46:18 +00003264 // Record which half of which vector the high elements come from.
3265 SmallVector<unsigned, 4> HighQuad(4);
3266 for (unsigned i = 4; i < 8; ++i) {
3267 SDOperand Elt = MaskElts[i];
3268 if (Elt.getOpcode() == ISD::UNDEF)
3269 continue;
3270 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3271 int QuadIdx = EltIdx / 4;
3272 ++HighQuad[QuadIdx];
3273 }
3274 int BestHighQuad = -1;
3275 MaxQuad = 1;
3276 for (unsigned i = 0; i < 4; ++i) {
3277 if (HighQuad[i] > MaxQuad) {
3278 BestHighQuad = i;
3279 MaxQuad = HighQuad[i];
3280 }
3281 }
3282
3283 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3284 if (BestLowQuad != -1 || BestHighQuad != -1) {
3285 // First sort the 4 chunks in order using shufpd.
3286 SmallVector<SDOperand, 8> MaskVec;
3287 if (BestLowQuad != -1)
3288 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3289 else
3290 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3291 if (BestHighQuad != -1)
3292 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3293 else
3294 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3295 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3296 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3297 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3298 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3299 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3300
3301 // Now sort high and low parts separately.
3302 BitVector InOrder(8);
3303 if (BestLowQuad != -1) {
3304 // Sort lower half in order using PSHUFLW.
3305 MaskVec.clear();
3306 bool AnyOutOrder = false;
3307 for (unsigned i = 0; i != 4; ++i) {
3308 SDOperand Elt = MaskElts[i];
3309 if (Elt.getOpcode() == ISD::UNDEF) {
3310 MaskVec.push_back(Elt);
3311 InOrder.set(i);
3312 } else {
3313 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3314 if (EltIdx != i)
3315 AnyOutOrder = true;
3316 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3317 // If this element is in the right place after this shuffle, then
3318 // remember it.
3319 if ((int)(EltIdx / 4) == BestLowQuad)
3320 InOrder.set(i);
3321 }
3322 }
3323 if (AnyOutOrder) {
3324 for (unsigned i = 4; i != 8; ++i)
3325 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3326 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3327 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3328 }
3329 }
3330
3331 if (BestHighQuad != -1) {
3332 // Sort high half in order using PSHUFHW if possible.
3333 MaskVec.clear();
3334 for (unsigned i = 0; i != 4; ++i)
3335 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3336 bool AnyOutOrder = false;
3337 for (unsigned i = 4; i != 8; ++i) {
3338 SDOperand Elt = MaskElts[i];
3339 if (Elt.getOpcode() == ISD::UNDEF) {
3340 MaskVec.push_back(Elt);
3341 InOrder.set(i);
3342 } else {
3343 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3344 if (EltIdx != i)
3345 AnyOutOrder = true;
3346 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3347 // If this element is in the right place after this shuffle, then
3348 // remember it.
3349 if ((int)(EltIdx / 4) == BestHighQuad)
3350 InOrder.set(i);
3351 }
3352 }
3353 if (AnyOutOrder) {
3354 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3355 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3356 }
3357 }
3358
3359 // The other elements are put in the right place using pextrw and pinsrw.
3360 for (unsigned i = 0; i != 8; ++i) {
3361 if (InOrder[i])
3362 continue;
3363 SDOperand Elt = MaskElts[i];
3364 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3365 if (EltIdx == i)
3366 continue;
3367 SDOperand ExtOp = (EltIdx < 8)
3368 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3369 DAG.getConstant(EltIdx, PtrVT))
3370 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3371 DAG.getConstant(EltIdx - 8, PtrVT));
3372 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3373 DAG.getConstant(i, PtrVT));
3374 }
3375 return NewV;
3376 }
3377
3378 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3379 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003380 // First, let's find out how many elements are already in the right order.
3381 unsigned V1InOrder = 0;
3382 unsigned V1FromV1 = 0;
3383 unsigned V2InOrder = 0;
3384 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003385 SmallVector<SDOperand, 8> V1Elts;
3386 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003387 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003388 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003389 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003390 V1Elts.push_back(Elt);
3391 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003392 ++V1InOrder;
3393 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003394 continue;
3395 }
3396 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3397 if (EltIdx == i) {
3398 V1Elts.push_back(Elt);
3399 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3400 ++V1InOrder;
3401 } else if (EltIdx == i+8) {
3402 V1Elts.push_back(Elt);
3403 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3404 ++V2InOrder;
3405 } else if (EltIdx < 8) {
3406 V1Elts.push_back(Elt);
3407 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003408 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003409 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3410 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003411 }
3412 }
3413
3414 if (V2InOrder > V1InOrder) {
3415 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3416 std::swap(V1, V2);
3417 std::swap(V1Elts, V2Elts);
3418 std::swap(V1FromV1, V2FromV2);
3419 }
3420
Evan Cheng75184a92007-12-11 01:46:18 +00003421 if ((V1FromV1 + V1InOrder) != 8) {
3422 // Some elements are from V2.
3423 if (V1FromV1) {
3424 // If there are elements that are from V1 but out of place,
3425 // then first sort them in place
3426 SmallVector<SDOperand, 8> MaskVec;
3427 for (unsigned i = 0; i < 8; ++i) {
3428 SDOperand Elt = V1Elts[i];
3429 if (Elt.getOpcode() == ISD::UNDEF) {
3430 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3431 continue;
3432 }
3433 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3434 if (EltIdx >= 8)
3435 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3436 else
3437 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3438 }
3439 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3440 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003441 }
Evan Cheng75184a92007-12-11 01:46:18 +00003442
3443 NewV = V1;
3444 for (unsigned i = 0; i < 8; ++i) {
3445 SDOperand Elt = V1Elts[i];
3446 if (Elt.getOpcode() == ISD::UNDEF)
3447 continue;
3448 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3449 if (EltIdx < 8)
3450 continue;
3451 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3452 DAG.getConstant(EltIdx - 8, PtrVT));
3453 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3454 DAG.getConstant(i, PtrVT));
3455 }
3456 return NewV;
3457 } else {
3458 // All elements are from V1.
3459 NewV = V1;
3460 for (unsigned i = 0; i < 8; ++i) {
3461 SDOperand Elt = V1Elts[i];
3462 if (Elt.getOpcode() == ISD::UNDEF)
3463 continue;
3464 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3465 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3466 DAG.getConstant(EltIdx, PtrVT));
3467 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3468 DAG.getConstant(i, PtrVT));
3469 }
3470 return NewV;
3471 }
3472}
3473
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003474/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3475/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3476/// done when every pair / quad of shuffle mask elements point to elements in
3477/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003478/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3479static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003480SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3481 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003482 SDOperand PermMask, SelectionDAG &DAG,
3483 TargetLowering &TLI) {
3484 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003485 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3486 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3487 MVT::ValueType NewVT = MaskVT;
3488 switch (VT) {
3489 case MVT::v4f32: NewVT = MVT::v2f64; break;
3490 case MVT::v4i32: NewVT = MVT::v2i64; break;
3491 case MVT::v8i16: NewVT = MVT::v4i32; break;
3492 case MVT::v16i8: NewVT = MVT::v4i32; break;
3493 default: assert(false && "Unexpected!");
3494 }
3495
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003496 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003497 if (MVT::isInteger(VT))
3498 NewVT = MVT::v2i64;
3499 else
3500 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003501 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003502 unsigned Scale = NumElems / NewWidth;
3503 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003504 for (unsigned i = 0; i < NumElems; i += Scale) {
3505 unsigned StartIdx = ~0U;
3506 for (unsigned j = 0; j < Scale; ++j) {
3507 SDOperand Elt = PermMask.getOperand(i+j);
3508 if (Elt.getOpcode() == ISD::UNDEF)
3509 continue;
3510 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3511 if (StartIdx == ~0U)
3512 StartIdx = EltIdx - (EltIdx % Scale);
3513 if (EltIdx != StartIdx + j)
3514 return SDOperand();
3515 }
3516 if (StartIdx == ~0U)
3517 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3518 else
3519 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003520 }
3521
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003522 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3523 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3524 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3525 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3526 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003527}
3528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529SDOperand
3530X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3531 SDOperand V1 = Op.getOperand(0);
3532 SDOperand V2 = Op.getOperand(1);
3533 SDOperand PermMask = Op.getOperand(2);
3534 MVT::ValueType VT = Op.getValueType();
3535 unsigned NumElems = PermMask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003536 bool isMMX = MVT::getSizeInBits(VT) == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003537 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3538 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3539 bool V1IsSplat = false;
3540 bool V2IsSplat = false;
3541
3542 if (isUndefShuffle(Op.Val))
3543 return DAG.getNode(ISD::UNDEF, VT);
3544
3545 if (isZeroShuffle(Op.Val))
3546 return getZeroVector(VT, DAG);
3547
3548 if (isIdentityMask(PermMask.Val))
3549 return V1;
3550 else if (isIdentityMask(PermMask.Val, true))
3551 return V2;
3552
3553 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003554 if (isMMX || NumElems < 4) return Op;
3555 // Promote it to a v4{if}32 splat.
3556 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003557 }
3558
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003559 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3560 // do it!
3561 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3562 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3563 if (NewOp.Val)
3564 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3565 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3566 // FIXME: Figure out a cleaner way to do this.
3567 // Try to make use of movq to zero out the top part.
3568 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3569 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3570 if (NewOp.Val) {
3571 SDOperand NewV1 = NewOp.getOperand(0);
3572 SDOperand NewV2 = NewOp.getOperand(1);
3573 SDOperand NewMask = NewOp.getOperand(2);
3574 if (isCommutedMOVL(NewMask.Val, true, false)) {
3575 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3576 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3577 NewV1, NewV2, getMOVLMask(2, DAG));
3578 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3579 }
3580 }
3581 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3582 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3583 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3584 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3585 }
3586 }
3587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003588 if (X86::isMOVLMask(PermMask.Val))
3589 return (V1IsUndef) ? V2 : Op;
3590
3591 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3592 X86::isMOVSLDUPMask(PermMask.Val) ||
3593 X86::isMOVHLPSMask(PermMask.Val) ||
3594 X86::isMOVHPMask(PermMask.Val) ||
3595 X86::isMOVLPMask(PermMask.Val))
3596 return Op;
3597
3598 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3599 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3600 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3601
3602 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003603 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3604 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003605 V1IsSplat = isSplatVector(V1.Val);
3606 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003607
3608 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003609 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3610 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3611 std::swap(V1IsSplat, V2IsSplat);
3612 std::swap(V1IsUndef, V2IsUndef);
3613 Commuted = true;
3614 }
3615
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003616 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003617 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3618 if (V2IsUndef) return V1;
3619 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3620 if (V2IsSplat) {
3621 // V2 is a splat, so the mask may be malformed. That is, it may point
3622 // to any V2 element. The instruction selectior won't like this. Get
3623 // a corrected mask and commute to form a proper MOVS{S|D}.
3624 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3625 if (NewMask.Val != PermMask.Val)
3626 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3627 }
3628 return Op;
3629 }
3630
3631 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3632 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3633 X86::isUNPCKLMask(PermMask.Val) ||
3634 X86::isUNPCKHMask(PermMask.Val))
3635 return Op;
3636
3637 if (V2IsSplat) {
3638 // Normalize mask so all entries that point to V2 points to its first
3639 // element then try to match unpck{h|l} again. If match, return a
3640 // new vector_shuffle with the corrected mask.
3641 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3642 if (NewMask.Val != PermMask.Val) {
3643 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3644 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3645 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3646 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3647 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3648 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3649 }
3650 }
3651 }
3652
3653 // Normalize the node to match x86 shuffle ops if needed
3654 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3655 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3656
3657 if (Commuted) {
3658 // Commute is back and try unpck* again.
3659 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3660 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3661 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3662 X86::isUNPCKLMask(PermMask.Val) ||
3663 X86::isUNPCKHMask(PermMask.Val))
3664 return Op;
3665 }
3666
Evan Chengbf8b2c52008-04-05 00:30:36 +00003667 // Try PSHUF* first, then SHUFP*.
3668 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3669 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3670 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3671 if (V2.getOpcode() != ISD::UNDEF)
3672 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3673 DAG.getNode(ISD::UNDEF, VT), PermMask);
3674 return Op;
3675 }
3676
3677 if (!isMMX) {
3678 if (Subtarget->hasSSE2() &&
3679 (X86::isPSHUFDMask(PermMask.Val) ||
3680 X86::isPSHUFHWMask(PermMask.Val) ||
3681 X86::isPSHUFLWMask(PermMask.Val))) {
3682 MVT::ValueType RVT = VT;
3683 if (VT == MVT::v4f32) {
3684 RVT = MVT::v4i32;
3685 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3686 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3687 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3688 } else if (V2.getOpcode() != ISD::UNDEF)
3689 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3690 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3691 if (RVT != VT)
3692 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003693 return Op;
3694 }
3695
Evan Chengbf8b2c52008-04-05 00:30:36 +00003696 // Binary or unary shufps.
3697 if (X86::isSHUFPMask(PermMask.Val) ||
3698 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003699 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003700 }
3701
Evan Cheng75184a92007-12-11 01:46:18 +00003702 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3703 if (VT == MVT::v8i16) {
3704 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3705 if (NewOp.Val)
3706 return NewOp;
3707 }
3708
3709 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003710 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003711 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003712 MVT::ValueType MaskVT = PermMask.getValueType();
3713 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3714 SmallVector<std::pair<int, int>, 8> Locs;
3715 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003716 SmallVector<SDOperand, 8> Mask1(NumElems,
3717 DAG.getNode(ISD::UNDEF, MaskEVT));
3718 SmallVector<SDOperand, 8> Mask2(NumElems,
3719 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003720 unsigned NumHi = 0;
3721 unsigned NumLo = 0;
3722 // If no more than two elements come from either vector. This can be
3723 // implemented with two shuffles. First shuffle gather the elements.
3724 // The second shuffle, which takes the first shuffle as both of its
3725 // vector operands, put the elements into the right order.
3726 for (unsigned i = 0; i != NumElems; ++i) {
3727 SDOperand Elt = PermMask.getOperand(i);
3728 if (Elt.getOpcode() == ISD::UNDEF) {
3729 Locs[i] = std::make_pair(-1, -1);
3730 } else {
3731 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3732 if (Val < NumElems) {
3733 Locs[i] = std::make_pair(0, NumLo);
3734 Mask1[NumLo] = Elt;
3735 NumLo++;
3736 } else {
3737 Locs[i] = std::make_pair(1, NumHi);
3738 if (2+NumHi < NumElems)
3739 Mask1[2+NumHi] = Elt;
3740 NumHi++;
3741 }
3742 }
3743 }
3744 if (NumLo <= 2 && NumHi <= 2) {
3745 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3746 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3747 &Mask1[0], Mask1.size()));
3748 for (unsigned i = 0; i != NumElems; ++i) {
3749 if (Locs[i].first == -1)
3750 continue;
3751 else {
3752 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3753 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3754 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3755 }
3756 }
3757
3758 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3759 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3760 &Mask2[0], Mask2.size()));
3761 }
3762
3763 // Break it into (shuffle shuffle_hi, shuffle_lo).
3764 Locs.clear();
3765 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3766 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3767 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3768 unsigned MaskIdx = 0;
3769 unsigned LoIdx = 0;
3770 unsigned HiIdx = NumElems/2;
3771 for (unsigned i = 0; i != NumElems; ++i) {
3772 if (i == NumElems/2) {
3773 MaskPtr = &HiMask;
3774 MaskIdx = 1;
3775 LoIdx = 0;
3776 HiIdx = NumElems/2;
3777 }
3778 SDOperand Elt = PermMask.getOperand(i);
3779 if (Elt.getOpcode() == ISD::UNDEF) {
3780 Locs[i] = std::make_pair(-1, -1);
3781 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3782 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3783 (*MaskPtr)[LoIdx] = Elt;
3784 LoIdx++;
3785 } else {
3786 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3787 (*MaskPtr)[HiIdx] = Elt;
3788 HiIdx++;
3789 }
3790 }
3791
3792 SDOperand LoShuffle =
3793 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3794 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3795 &LoMask[0], LoMask.size()));
3796 SDOperand HiShuffle =
3797 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3798 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3799 &HiMask[0], HiMask.size()));
3800 SmallVector<SDOperand, 8> MaskOps;
3801 for (unsigned i = 0; i != NumElems; ++i) {
3802 if (Locs[i].first == -1) {
3803 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3804 } else {
3805 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3806 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3807 }
3808 }
3809 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3810 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3811 &MaskOps[0], MaskOps.size()));
3812 }
3813
3814 return SDOperand();
3815}
3816
3817SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003818X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3819 SelectionDAG &DAG) {
3820 MVT::ValueType VT = Op.getValueType();
3821 if (MVT::getSizeInBits(VT) == 8) {
3822 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3823 Op.getOperand(0), Op.getOperand(1));
3824 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3825 DAG.getValueType(VT));
3826 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3827 } else if (MVT::getSizeInBits(VT) == 16) {
3828 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3829 Op.getOperand(0), Op.getOperand(1));
3830 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3831 DAG.getValueType(VT));
3832 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003833 } else if (VT == MVT::f32) {
3834 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3835 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003836 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003837 if (!Op.hasOneUse())
3838 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003839 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003840 if (User->getOpcode() != ISD::STORE &&
3841 (User->getOpcode() != ISD::BIT_CONVERT ||
3842 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003843 return SDOperand();
3844 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3845 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3846 Op.getOperand(1));
3847 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003848 }
3849 return SDOperand();
3850}
3851
3852
3853SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003854X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3855 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3856 return SDOperand();
3857
Evan Cheng6c249332008-03-24 21:52:23 +00003858 if (Subtarget->hasSSE41()) {
3859 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3860 if (Res.Val)
3861 return Res;
3862 }
Nate Begemand77e59e2008-02-11 04:19:36 +00003863
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003864 MVT::ValueType VT = Op.getValueType();
3865 // TODO: handle v16i8.
3866 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003867 SDOperand Vec = Op.getOperand(0);
3868 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3869 if (Idx == 0)
3870 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3871 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3872 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3873 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003874 // Transform it so it match pextrw which produces a 32-bit result.
3875 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3876 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3877 Op.getOperand(0), Op.getOperand(1));
3878 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3879 DAG.getValueType(VT));
3880 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3881 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003882 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3883 if (Idx == 0)
3884 return Op;
3885 // SHUFPS the element to the lowest double word, then movss.
3886 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3887 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003888 IdxVec.
3889 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3890 IdxVec.
3891 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3892 IdxVec.
3893 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3894 IdxVec.
3895 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003896 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3897 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003898 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3900 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3901 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003902 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003903 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003904 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3905 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3906 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003907 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3908 if (Idx == 0)
3909 return Op;
3910
3911 // UNPCKHPD the element to the lowest double word, then movsd.
3912 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3913 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3914 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3915 SmallVector<SDOperand, 8> IdxVec;
3916 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003917 IdxVec.
3918 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003919 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3920 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003921 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3923 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3924 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003925 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003926 }
3927
3928 return SDOperand();
3929}
3930
3931SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003932X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3933 MVT::ValueType VT = Op.getValueType();
3934 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3935
3936 SDOperand N0 = Op.getOperand(0);
3937 SDOperand N1 = Op.getOperand(1);
3938 SDOperand N2 = Op.getOperand(2);
3939
3940 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3941 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3942 : X86ISD::PINSRW;
3943 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3944 // argument.
3945 if (N1.getValueType() != MVT::i32)
3946 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3947 if (N2.getValueType() != MVT::i32)
3948 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3949 return DAG.getNode(Opc, VT, N0, N1, N2);
3950 } else if (EVT == MVT::f32) {
3951 // Bits [7:6] of the constant are the source select. This will always be
3952 // zero here. The DAG Combiner may combine an extract_elt index into these
3953 // bits. For example (insert (extract, 3), 2) could be matched by putting
3954 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3955 // Bits [5:4] of the constant are the destination select. This is the
3956 // value of the incoming immediate.
3957 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3958 // combine either bitwise AND or insert of float 0.0 to set these bits.
3959 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3960 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3961 }
3962 return SDOperand();
3963}
3964
3965SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003966X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003967 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003968 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003969
3970 if (Subtarget->hasSSE41())
3971 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3972
Evan Chenge12a7eb2007-12-12 07:55:34 +00003973 if (EVT == MVT::i8)
3974 return SDOperand();
3975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003976 SDOperand N0 = Op.getOperand(0);
3977 SDOperand N1 = Op.getOperand(1);
3978 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003979
3980 if (MVT::getSizeInBits(EVT) == 16) {
3981 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3982 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983 if (N1.getValueType() != MVT::i32)
3984 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3985 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003986 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003988 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003989 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003990}
3991
3992SDOperand
3993X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3994 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003995 MVT::ValueType VT = MVT::v2i32;
3996 switch (Op.getValueType()) {
3997 default: break;
3998 case MVT::v16i8:
3999 case MVT::v8i16:
4000 VT = MVT::v4i32;
4001 break;
4002 }
4003 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4004 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005}
4006
4007// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4008// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4009// one of the above mentioned nodes. It has to be wrapped because otherwise
4010// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4011// be used to form addressing mode. These wrapped nodes will be selected
4012// into MOV32ri.
4013SDOperand
4014X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4015 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4016 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4017 getPointerTy(),
4018 CP->getAlignment());
4019 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4020 // With PIC, the address is actually $g + Offset.
4021 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4022 !Subtarget->isPICStyleRIPRel()) {
4023 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4024 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4025 Result);
4026 }
4027
4028 return Result;
4029}
4030
4031SDOperand
4032X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4033 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4034 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00004035 // If it's a debug information descriptor, don't mess with it.
4036 if (DAG.isVerifiedDebugInfoDesc(Op))
4037 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4039 // With PIC, the address is actually $g + Offset.
4040 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4041 !Subtarget->isPICStyleRIPRel()) {
4042 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4043 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4044 Result);
4045 }
4046
4047 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4048 // load the value at address GV, not the value of GV itself. This means that
4049 // the GlobalAddress must be in the base or index register of the address, not
4050 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4051 // The same applies for external symbols during PIC codegen
4052 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004053 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004054 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055
4056 return Result;
4057}
4058
4059// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4060static SDOperand
4061LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4062 const MVT::ValueType PtrVT) {
4063 SDOperand InFlag;
4064 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4065 DAG.getNode(X86ISD::GlobalBaseReg,
4066 PtrVT), InFlag);
4067 InFlag = Chain.getValue(1);
4068
4069 // emit leal symbol@TLSGD(,%ebx,1), %eax
4070 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4071 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4072 GA->getValueType(0),
4073 GA->getOffset());
4074 SDOperand Ops[] = { Chain, TGA, InFlag };
4075 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4076 InFlag = Result.getValue(2);
4077 Chain = Result.getValue(1);
4078
4079 // call ___tls_get_addr. This function receives its argument in
4080 // the register EAX.
4081 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4082 InFlag = Chain.getValue(1);
4083
4084 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4085 SDOperand Ops1[] = { Chain,
4086 DAG.getTargetExternalSymbol("___tls_get_addr",
4087 PtrVT),
4088 DAG.getRegister(X86::EAX, PtrVT),
4089 DAG.getRegister(X86::EBX, PtrVT),
4090 InFlag };
4091 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4092 InFlag = Chain.getValue(1);
4093
4094 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4095}
4096
4097// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4098// "local exec" model.
4099static SDOperand
4100LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4101 const MVT::ValueType PtrVT) {
4102 // Get the Thread Pointer
4103 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4104 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4105 // exec)
4106 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4107 GA->getValueType(0),
4108 GA->getOffset());
4109 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4110
4111 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004112 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004113 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114
4115 // The address of the thread local variable is the add of the thread
4116 // pointer with the offset of the variable.
4117 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4118}
4119
4120SDOperand
4121X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4122 // TODO: implement the "local dynamic" model
4123 // TODO: implement the "initial exec"model for pic executables
4124 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4125 "TLS not implemented for non-ELF and 64-bit targets");
4126 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4127 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4128 // otherwise use the "Local Exec"TLS Model
4129 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4130 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4131 else
4132 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4133}
4134
4135SDOperand
4136X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4137 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4138 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4139 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4140 // With PIC, the address is actually $g + Offset.
4141 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4142 !Subtarget->isPICStyleRIPRel()) {
4143 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4144 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4145 Result);
4146 }
4147
4148 return Result;
4149}
4150
4151SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4152 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4153 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4154 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4155 // With PIC, the address is actually $g + Offset.
4156 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4157 !Subtarget->isPICStyleRIPRel()) {
4158 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4159 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4160 Result);
4161 }
4162
4163 return Result;
4164}
4165
Chris Lattner62814a32007-10-17 06:02:13 +00004166/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4167/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004168SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004169 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4170 MVT::ValueType VT = Op.getValueType();
4171 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004172 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4173 SDOperand ShOpLo = Op.getOperand(0);
4174 SDOperand ShOpHi = Op.getOperand(1);
4175 SDOperand ShAmt = Op.getOperand(2);
4176 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004177 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4178 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004179
Chris Lattner62814a32007-10-17 06:02:13 +00004180 SDOperand Tmp2, Tmp3;
4181 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004182 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4183 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004184 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004185 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4186 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004187 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188
Chris Lattner62814a32007-10-17 06:02:13 +00004189 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4190 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004191 DAG.getConstant(VTBits, MVT::i8));
4192 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004193 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194
Chris Lattner62814a32007-10-17 06:02:13 +00004195 SDOperand Hi, Lo;
4196 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004197 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004198 SmallVector<SDOperand, 4> Ops;
4199 if (Op.getOpcode() == ISD::SHL_PARTS) {
4200 Ops.push_back(Tmp2);
4201 Ops.push_back(Tmp3);
4202 Ops.push_back(CC);
4203 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004204 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004207 Ops.push_back(Tmp3);
4208 Ops.push_back(Tmp1);
4209 Ops.push_back(CC);
4210 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004211 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004212 } else {
4213 Ops.push_back(Tmp2);
4214 Ops.push_back(Tmp3);
4215 Ops.push_back(CC);
4216 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004217 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004218
4219 Ops.clear();
4220 Ops.push_back(Tmp3);
4221 Ops.push_back(Tmp1);
4222 Ops.push_back(CC);
4223 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004224 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004225 }
4226
Dan Gohman092014e2008-03-03 22:22:09 +00004227 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004228 Ops.clear();
4229 Ops.push_back(Lo);
4230 Ops.push_back(Hi);
4231 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004232}
4233
4234SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004236 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4237 "Unknown SINT_TO_FP to lower!");
4238
4239 // These are really Legal; caller falls through into that case.
4240 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4241 return SDOperand();
4242 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4243 Subtarget->is64Bit())
4244 return SDOperand();
4245
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4247 MachineFunction &MF = DAG.getMachineFunction();
4248 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4249 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4250 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004251 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004252 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004253 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254
4255 // Build the FILD
4256 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004257 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004258 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4260 else
4261 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4262 SmallVector<SDOperand, 8> Ops;
4263 Ops.push_back(Chain);
4264 Ops.push_back(StackSlot);
4265 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004266 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4267 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004268
Dale Johannesen2fc20782007-09-14 22:26:36 +00004269 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270 Chain = Result.getValue(1);
4271 SDOperand InFlag = Result.getValue(2);
4272
4273 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4274 // shouldn't be necessary except that RFP cannot be live across
4275 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4276 MachineFunction &MF = DAG.getMachineFunction();
4277 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4278 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4279 Tys = DAG.getVTList(MVT::Other);
4280 SmallVector<SDOperand, 8> Ops;
4281 Ops.push_back(Chain);
4282 Ops.push_back(Result);
4283 Ops.push_back(StackSlot);
4284 Ops.push_back(DAG.getValueType(Op.getValueType()));
4285 Ops.push_back(InFlag);
4286 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004287 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004288 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289 }
4290
4291 return Result;
4292}
4293
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004294std::pair<SDOperand,SDOperand> X86TargetLowering::
4295FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4297 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004298
Dale Johannesen2fc20782007-09-14 22:26:36 +00004299 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004300 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004301 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004302 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004303 if (Subtarget->is64Bit() &&
4304 Op.getValueType() == MVT::i64 &&
4305 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004306 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004307
Evan Cheng05441e62007-10-15 20:11:21 +00004308 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4309 // stack slot.
4310 MachineFunction &MF = DAG.getMachineFunction();
4311 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4312 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4313 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 unsigned Opc;
4315 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004316 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4317 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4318 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4319 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320 }
4321
4322 SDOperand Chain = DAG.getEntryNode();
4323 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004324 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004326 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004327 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004328 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4329 SDOperand Ops[] = {
4330 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4331 };
4332 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4333 Chain = Value.getValue(1);
4334 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4335 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4336 }
4337
4338 // Build the FP_TO_INT*_IN_MEM
4339 SDOperand Ops[] = { Chain, Value, StackSlot };
4340 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4341
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004342 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343}
4344
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004345SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004346 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4347 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4348 if (FIST.Val == 0) return SDOperand();
4349
4350 // Load the result.
4351 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4352}
4353
4354SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4355 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4356 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4357 if (FIST.Val == 0) return 0;
4358
4359 // Return an i64 load from the stack slot.
4360 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4361
4362 // Use a MERGE_VALUES node to drop the chain result value.
4363 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4364}
4365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4367 MVT::ValueType VT = Op.getValueType();
4368 MVT::ValueType EltVT = VT;
4369 if (MVT::isVector(VT))
4370 EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371 std::vector<Constant*> CV;
4372 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004373 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004374 CV.push_back(C);
4375 CV.push_back(C);
4376 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004377 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378 CV.push_back(C);
4379 CV.push_back(C);
4380 CV.push_back(C);
4381 CV.push_back(C);
4382 }
Dan Gohman11821702007-07-27 17:16:43 +00004383 Constant *C = ConstantVector::get(CV);
4384 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004385 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004386 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004387 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4389}
4390
4391SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4392 MVT::ValueType VT = Op.getValueType();
4393 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004394 unsigned EltNum = 1;
4395 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004396 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004397 EltNum = MVT::getVectorNumElements(VT);
4398 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399 std::vector<Constant*> CV;
4400 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004401 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402 CV.push_back(C);
4403 CV.push_back(C);
4404 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004405 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 CV.push_back(C);
4407 CV.push_back(C);
4408 CV.push_back(C);
4409 CV.push_back(C);
4410 }
Dan Gohman11821702007-07-27 17:16:43 +00004411 Constant *C = ConstantVector::get(CV);
4412 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004413 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004414 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004415 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004416 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004417 return DAG.getNode(ISD::BIT_CONVERT, VT,
4418 DAG.getNode(ISD::XOR, MVT::v2i64,
4419 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4420 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4421 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004422 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4423 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424}
4425
4426SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4427 SDOperand Op0 = Op.getOperand(0);
4428 SDOperand Op1 = Op.getOperand(1);
4429 MVT::ValueType VT = Op.getValueType();
4430 MVT::ValueType SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431
4432 // If second operand is smaller, extend it first.
4433 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4434 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4435 SrcVT = VT;
4436 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004437 // And if it is bigger, shrink it first.
4438 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004439 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004440 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004441 }
4442
4443 // At this point the operands and the result should have the same
4444 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445
4446 // First get the sign bit of second operand.
4447 std::vector<Constant*> CV;
4448 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004449 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4450 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004452 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4453 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4454 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4455 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004456 }
Dan Gohman11821702007-07-27 17:16:43 +00004457 Constant *C = ConstantVector::get(CV);
4458 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004459 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004460 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004461 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4463
4464 // Shift sign bit right or left if the two operands have different types.
4465 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4466 // Op0 is MVT::f32, Op1 is MVT::f64.
4467 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4468 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4469 DAG.getConstant(32, MVT::i32));
4470 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4471 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004472 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473 }
4474
4475 // Clear first operand sign bit.
4476 CV.clear();
4477 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004478 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4479 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004481 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4482 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4483 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4484 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 }
Dan Gohman11821702007-07-27 17:16:43 +00004486 C = ConstantVector::get(CV);
4487 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004488 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004489 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004490 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4492
4493 // Or the value with the sign bit.
4494 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4495}
4496
Evan Cheng621216e2007-09-29 00:00:36 +00004497SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004498 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004499 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004500 SDOperand Op0 = Op.getOperand(0);
4501 SDOperand Op1 = Op.getOperand(1);
4502 SDOperand CC = Op.getOperand(2);
4503 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4504 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4505 unsigned X86CC;
4506
Evan Cheng950aac02007-09-25 01:57:46 +00004507 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004508 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004509 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4510 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004511 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004512 }
Evan Cheng950aac02007-09-25 01:57:46 +00004513
4514 assert(isFP && "Illegal integer SetCC!");
4515
Evan Cheng621216e2007-09-29 00:00:36 +00004516 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004517 switch (SetCCOpcode) {
4518 default: assert(false && "Illegal floating point SetCC!");
4519 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004520 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004521 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004522 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004523 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4524 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4525 }
4526 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004527 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004528 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004529 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004530 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4531 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4532 }
4533 }
4534}
4535
4536
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4538 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539 SDOperand Cond = Op.getOperand(0);
4540 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541
4542 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004543 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544
Evan Cheng50d37ab2007-10-08 22:16:29 +00004545 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4546 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004547 if (Cond.getOpcode() == X86ISD::SETCC) {
4548 CC = Cond.getOperand(0);
4549
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550 SDOperand Cmp = Cond.getOperand(1);
4551 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004552 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004553
Evan Cheng50d37ab2007-10-08 22:16:29 +00004554 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004555 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004556 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004557 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004558
Evan Cheng621216e2007-09-29 00:00:36 +00004559 if ((Opc == X86ISD::CMP ||
4560 Opc == X86ISD::COMI ||
4561 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004562 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004563 addTest = false;
4564 }
4565 }
4566
4567 if (addTest) {
4568 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004569 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004570 }
4571
4572 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4573 MVT::Flag);
4574 SmallVector<SDOperand, 4> Ops;
4575 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4576 // condition is true.
4577 Ops.push_back(Op.getOperand(2));
4578 Ops.push_back(Op.getOperand(1));
4579 Ops.push_back(CC);
4580 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004581 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004582}
4583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4585 bool addTest = true;
4586 SDOperand Chain = Op.getOperand(0);
4587 SDOperand Cond = Op.getOperand(1);
4588 SDOperand Dest = Op.getOperand(2);
4589 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004590
4591 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004592 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593
Evan Cheng50d37ab2007-10-08 22:16:29 +00004594 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4595 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004596 if (Cond.getOpcode() == X86ISD::SETCC) {
4597 CC = Cond.getOperand(0);
4598
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004599 SDOperand Cmp = Cond.getOperand(1);
4600 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004601 if (Opc == X86ISD::CMP ||
4602 Opc == X86ISD::COMI ||
4603 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004604 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004605 addTest = false;
4606 }
4607 }
4608
4609 if (addTest) {
4610 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004611 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004612 }
Evan Cheng621216e2007-09-29 00:00:36 +00004613 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004614 Chain, Op.getOperand(2), CC, Cond);
4615}
4616
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617
4618// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4619// Calls to _alloca is needed to probe the stack when allocating more than 4k
4620// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4621// that the guard pages used by the OS virtual memory manager are allocated in
4622// correct sequence.
4623SDOperand
4624X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4625 SelectionDAG &DAG) {
4626 assert(Subtarget->isTargetCygMing() &&
4627 "This should be used only on Cygwin/Mingw targets");
4628
4629 // Get the inputs.
4630 SDOperand Chain = Op.getOperand(0);
4631 SDOperand Size = Op.getOperand(1);
4632 // FIXME: Ensure alignment here
4633
4634 SDOperand Flag;
4635
4636 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004637 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638
4639 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4640 Flag = Chain.getValue(1);
4641
4642 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4643 SDOperand Ops[] = { Chain,
4644 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4645 DAG.getRegister(X86::EAX, IntPtr),
4646 Flag };
4647 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4648 Flag = Chain.getValue(1);
4649
4650 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4651
4652 std::vector<MVT::ValueType> Tys;
4653 Tys.push_back(SPTy);
4654 Tys.push_back(MVT::Other);
4655 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4656 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4657}
4658
Dan Gohmane8b391e2008-04-12 04:36:06 +00004659SDOperand
4660X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4661 SDOperand Chain,
4662 SDOperand Dst, SDOperand Src,
4663 SDOperand Size, unsigned Align,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004664 const Value *DstSV, uint64_t DstOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004665 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666
Dan Gohmane8b391e2008-04-12 04:36:06 +00004667 /// If not DWORD aligned or size is more than the threshold, call the library.
4668 /// The libc version is likely to be faster for these cases. It can use the
4669 /// address value and run time information about the CPU.
4670 if ((Align & 3) == 0 ||
4671 !ConstantSize ||
4672 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4673 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004674
4675 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004676 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4677 if (const char *bzeroEntry =
4678 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
4679 MVT::ValueType IntPtr = getPointerTy();
4680 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4681 TargetLowering::ArgListTy Args;
4682 TargetLowering::ArgListEntry Entry;
4683 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004684 Entry.Ty = IntPtrTy;
4685 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004686 Entry.Node = Size;
4687 Args.push_back(Entry);
4688 std::pair<SDOperand,SDOperand> CallResult =
4689 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4690 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4691 Args, DAG);
4692 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004693 }
4694
Dan Gohmane8b391e2008-04-12 04:36:06 +00004695 // Otherwise have the target-independent code call memset.
4696 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697 }
4698
Dan Gohmane8b391e2008-04-12 04:36:06 +00004699 uint64_t SizeVal = ConstantSize->getValue();
4700 SDOperand InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 MVT::ValueType AVT;
4702 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004703 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004704 unsigned BytesLeft = 0;
4705 bool TwoRepStos = false;
4706 if (ValC) {
4707 unsigned ValReg;
4708 uint64_t Val = ValC->getValue() & 255;
4709
4710 // If the value is a constant, then we can potentially use larger sets.
4711 switch (Align & 3) {
4712 case 2: // WORD aligned
4713 AVT = MVT::i16;
4714 ValReg = X86::AX;
4715 Val = (Val << 8) | Val;
4716 break;
4717 case 0: // DWORD aligned
4718 AVT = MVT::i32;
4719 ValReg = X86::EAX;
4720 Val = (Val << 8) | Val;
4721 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004722 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004723 AVT = MVT::i64;
4724 ValReg = X86::RAX;
4725 Val = (Val << 32) | Val;
4726 }
4727 break;
4728 default: // Byte aligned
4729 AVT = MVT::i8;
4730 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00004731 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004732 break;
4733 }
4734
4735 if (AVT > MVT::i8) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004736 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4737 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
4738 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004739 }
4740
4741 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4742 InFlag);
4743 InFlag = Chain.getValue(1);
4744 } else {
4745 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00004746 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004747 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748 InFlag = Chain.getValue(1);
4749 }
4750
4751 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4752 Count, InFlag);
4753 InFlag = Chain.getValue(1);
4754 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004755 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004756 InFlag = Chain.getValue(1);
4757
4758 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4759 SmallVector<SDOperand, 8> Ops;
4760 Ops.push_back(Chain);
4761 Ops.push_back(DAG.getValueType(AVT));
4762 Ops.push_back(InFlag);
4763 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4764
4765 if (TwoRepStos) {
4766 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004767 Count = Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004768 MVT::ValueType CVT = Count.getValueType();
4769 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4770 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4771 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4772 Left, InFlag);
4773 InFlag = Chain.getValue(1);
4774 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4775 Ops.clear();
4776 Ops.push_back(Chain);
4777 Ops.push_back(DAG.getValueType(MVT::i8));
4778 Ops.push_back(InFlag);
4779 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4780 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004781 // Handle the last 1 - 7 bytes.
4782 unsigned Offset = SizeVal - BytesLeft;
4783 MVT::ValueType AddrVT = Dst.getValueType();
4784 MVT::ValueType SizeVT = Size.getValueType();
4785
4786 Chain = DAG.getMemset(Chain,
4787 DAG.getNode(ISD::ADD, AddrVT, Dst,
4788 DAG.getConstant(Offset, AddrVT)),
4789 Src,
4790 DAG.getConstant(BytesLeft, SizeVT),
4791 Align, DstSV, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004792 }
4793
Dan Gohmane8b391e2008-04-12 04:36:06 +00004794 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795 return Chain;
4796}
4797
Dan Gohmane8b391e2008-04-12 04:36:06 +00004798SDOperand
4799X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
4800 SDOperand Chain,
4801 SDOperand Dst, SDOperand Src,
4802 SDOperand Size, unsigned Align,
4803 bool AlwaysInline,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004804 const Value *DstSV, uint64_t DstOff,
4805 const Value *SrcSV, uint64_t SrcOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00004806
4807 // This requires the copy size to be a constant, preferrably
4808 // within a subtarget-specific limit.
4809 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4810 if (!ConstantSize)
4811 return SDOperand();
4812 uint64_t SizeVal = ConstantSize->getValue();
4813 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
4814 return SDOperand();
4815
4816 SmallVector<SDOperand, 4> Results;
4817
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004819 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004820 if (Align >= 8 && Subtarget->is64Bit())
4821 AVT = MVT::i64;
4822 else if (Align >= 4)
4823 AVT = MVT::i32;
4824 else if (Align >= 2)
4825 AVT = MVT::i16;
4826 else
4827 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004828
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004829 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004830 unsigned CountVal = SizeVal / UBytes;
4831 SDOperand Count = DAG.getIntPtrConstant(CountVal);
4832 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004833
4834 SDOperand InFlag(0, 0);
4835 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4836 Count, InFlag);
4837 InFlag = Chain.getValue(1);
4838 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004839 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 InFlag = Chain.getValue(1);
4841 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004842 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843 InFlag = Chain.getValue(1);
4844
4845 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4846 SmallVector<SDOperand, 8> Ops;
4847 Ops.push_back(Chain);
4848 Ops.push_back(DAG.getValueType(AVT));
4849 Ops.push_back(InFlag);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004850 Results.push_back(DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004852 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004853 // Handle the last 1 - 7 bytes.
4854 unsigned Offset = SizeVal - BytesLeft;
4855 MVT::ValueType DstVT = Dst.getValueType();
4856 MVT::ValueType SrcVT = Src.getValueType();
4857 MVT::ValueType SizeVT = Size.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004858
Dan Gohmane8b391e2008-04-12 04:36:06 +00004859 Results.push_back(DAG.getMemcpy(Chain,
4860 DAG.getNode(ISD::ADD, DstVT, Dst,
4861 DAG.getConstant(Offset,
4862 DstVT)),
4863 DAG.getNode(ISD::ADD, SrcVT, Src,
4864 DAG.getConstant(Offset,
4865 SrcVT)),
4866 DAG.getConstant(BytesLeft, SizeVT),
4867 Align, AlwaysInline,
4868 DstSV, Offset, SrcSV, Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004869 }
4870
Dan Gohmane8b391e2008-04-12 04:36:06 +00004871 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004872}
4873
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004874/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4875SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004876 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004877 SDOperand TheChain = N->getOperand(0);
4878 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004879 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004880 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4881 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4882 MVT::i64, rax.getValue(2));
4883 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 DAG.getConstant(32, MVT::i8));
4885 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004886 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004887 };
4888
4889 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004890 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004891 }
4892
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004893 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4894 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4895 MVT::i32, eax.getValue(2));
4896 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4897 SDOperand Ops[] = { eax, edx };
4898 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4899
4900 // Use a MERGE_VALUES to return the value and chain.
4901 Ops[1] = edx.getValue(1);
4902 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4903 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004904}
4905
4906SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004907 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004908
4909 if (!Subtarget->is64Bit()) {
4910 // vastart just stores the address of the VarArgsFrameIndex slot into the
4911 // memory location argument.
4912 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004913 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004914 }
4915
4916 // __va_list_tag:
4917 // gp_offset (0 - 6 * 8)
4918 // fp_offset (48 - 48 + 8 * 16)
4919 // overflow_arg_area (point to parameters coming in memory).
4920 // reg_save_area
4921 SmallVector<SDOperand, 8> MemOps;
4922 SDOperand FIN = Op.getOperand(1);
4923 // Store gp_offset
4924 SDOperand Store = DAG.getStore(Op.getOperand(0),
4925 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004926 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 MemOps.push_back(Store);
4928
4929 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004930 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004931 Store = DAG.getStore(Op.getOperand(0),
4932 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004933 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934 MemOps.push_back(Store);
4935
4936 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004937 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004938 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004939 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 MemOps.push_back(Store);
4941
4942 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004943 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004945 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004946 MemOps.push_back(Store);
4947 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4948}
4949
4950SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4951 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00004952 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953 SDOperand Chain = Op.getOperand(0);
4954 SDOperand DstPtr = Op.getOperand(1);
4955 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004956 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4957 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958
Dan Gohman840ff5c2008-04-18 20:55:41 +00004959 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
4960 DAG.getIntPtrConstant(24), 8, false,
4961 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962}
4963
4964SDOperand
4965X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4966 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4967 switch (IntNo) {
4968 default: return SDOperand(); // Don't custom lower most intrinsics.
4969 // Comparison intrinsics.
4970 case Intrinsic::x86_sse_comieq_ss:
4971 case Intrinsic::x86_sse_comilt_ss:
4972 case Intrinsic::x86_sse_comile_ss:
4973 case Intrinsic::x86_sse_comigt_ss:
4974 case Intrinsic::x86_sse_comige_ss:
4975 case Intrinsic::x86_sse_comineq_ss:
4976 case Intrinsic::x86_sse_ucomieq_ss:
4977 case Intrinsic::x86_sse_ucomilt_ss:
4978 case Intrinsic::x86_sse_ucomile_ss:
4979 case Intrinsic::x86_sse_ucomigt_ss:
4980 case Intrinsic::x86_sse_ucomige_ss:
4981 case Intrinsic::x86_sse_ucomineq_ss:
4982 case Intrinsic::x86_sse2_comieq_sd:
4983 case Intrinsic::x86_sse2_comilt_sd:
4984 case Intrinsic::x86_sse2_comile_sd:
4985 case Intrinsic::x86_sse2_comigt_sd:
4986 case Intrinsic::x86_sse2_comige_sd:
4987 case Intrinsic::x86_sse2_comineq_sd:
4988 case Intrinsic::x86_sse2_ucomieq_sd:
4989 case Intrinsic::x86_sse2_ucomilt_sd:
4990 case Intrinsic::x86_sse2_ucomile_sd:
4991 case Intrinsic::x86_sse2_ucomigt_sd:
4992 case Intrinsic::x86_sse2_ucomige_sd:
4993 case Intrinsic::x86_sse2_ucomineq_sd: {
4994 unsigned Opc = 0;
4995 ISD::CondCode CC = ISD::SETCC_INVALID;
4996 switch (IntNo) {
4997 default: break;
4998 case Intrinsic::x86_sse_comieq_ss:
4999 case Intrinsic::x86_sse2_comieq_sd:
5000 Opc = X86ISD::COMI;
5001 CC = ISD::SETEQ;
5002 break;
5003 case Intrinsic::x86_sse_comilt_ss:
5004 case Intrinsic::x86_sse2_comilt_sd:
5005 Opc = X86ISD::COMI;
5006 CC = ISD::SETLT;
5007 break;
5008 case Intrinsic::x86_sse_comile_ss:
5009 case Intrinsic::x86_sse2_comile_sd:
5010 Opc = X86ISD::COMI;
5011 CC = ISD::SETLE;
5012 break;
5013 case Intrinsic::x86_sse_comigt_ss:
5014 case Intrinsic::x86_sse2_comigt_sd:
5015 Opc = X86ISD::COMI;
5016 CC = ISD::SETGT;
5017 break;
5018 case Intrinsic::x86_sse_comige_ss:
5019 case Intrinsic::x86_sse2_comige_sd:
5020 Opc = X86ISD::COMI;
5021 CC = ISD::SETGE;
5022 break;
5023 case Intrinsic::x86_sse_comineq_ss:
5024 case Intrinsic::x86_sse2_comineq_sd:
5025 Opc = X86ISD::COMI;
5026 CC = ISD::SETNE;
5027 break;
5028 case Intrinsic::x86_sse_ucomieq_ss:
5029 case Intrinsic::x86_sse2_ucomieq_sd:
5030 Opc = X86ISD::UCOMI;
5031 CC = ISD::SETEQ;
5032 break;
5033 case Intrinsic::x86_sse_ucomilt_ss:
5034 case Intrinsic::x86_sse2_ucomilt_sd:
5035 Opc = X86ISD::UCOMI;
5036 CC = ISD::SETLT;
5037 break;
5038 case Intrinsic::x86_sse_ucomile_ss:
5039 case Intrinsic::x86_sse2_ucomile_sd:
5040 Opc = X86ISD::UCOMI;
5041 CC = ISD::SETLE;
5042 break;
5043 case Intrinsic::x86_sse_ucomigt_ss:
5044 case Intrinsic::x86_sse2_ucomigt_sd:
5045 Opc = X86ISD::UCOMI;
5046 CC = ISD::SETGT;
5047 break;
5048 case Intrinsic::x86_sse_ucomige_ss:
5049 case Intrinsic::x86_sse2_ucomige_sd:
5050 Opc = X86ISD::UCOMI;
5051 CC = ISD::SETGE;
5052 break;
5053 case Intrinsic::x86_sse_ucomineq_ss:
5054 case Intrinsic::x86_sse2_ucomineq_sd:
5055 Opc = X86ISD::UCOMI;
5056 CC = ISD::SETNE;
5057 break;
5058 }
5059
5060 unsigned X86CC;
5061 SDOperand LHS = Op.getOperand(1);
5062 SDOperand RHS = Op.getOperand(2);
5063 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5064
Evan Cheng621216e2007-09-29 00:00:36 +00005065 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5066 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5067 DAG.getConstant(X86CC, MVT::i8), Cond);
5068 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005069 }
5070 }
5071}
5072
5073SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5074 // Depths > 0 not supported yet!
5075 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5076 return SDOperand();
5077
5078 // Just load the return address
5079 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5080 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5081}
5082
5083SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5084 // Depths > 0 not supported yet!
5085 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5086 return SDOperand();
5087
5088 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5089 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005090 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005091}
5092
5093SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5094 SelectionDAG &DAG) {
5095 // Is not yet supported on x86-64
5096 if (Subtarget->is64Bit())
5097 return SDOperand();
5098
Chris Lattner5872a362008-01-17 07:00:52 +00005099 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100}
5101
5102SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5103{
5104 assert(!Subtarget->is64Bit() &&
5105 "Lowering of eh_return builtin is not supported yet on x86-64");
5106
5107 MachineFunction &MF = DAG.getMachineFunction();
5108 SDOperand Chain = Op.getOperand(0);
5109 SDOperand Offset = Op.getOperand(1);
5110 SDOperand Handler = Op.getOperand(2);
5111
5112 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5113 getPointerTy());
5114
5115 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005116 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005117 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5118 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5119 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005120 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005121
5122 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5123 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5124}
5125
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005126SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5127 SelectionDAG &DAG) {
5128 SDOperand Root = Op.getOperand(0);
5129 SDOperand Trmp = Op.getOperand(1); // trampoline
5130 SDOperand FPtr = Op.getOperand(2); // nested function
5131 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5132
Dan Gohman12a9c082008-02-06 22:27:42 +00005133 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005134
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005135 const X86InstrInfo *TII =
5136 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5137
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005138 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005139 SDOperand OutChains[6];
5140
5141 // Large code-model.
5142
5143 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5144 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5145
5146 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005147 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005148 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005149 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005150
5151 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5152
5153 // Load the pointer to the nested function into R11.
5154 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5155 SDOperand Addr = Trmp;
5156 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005157 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005158
5159 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005160 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005161
5162 // Load the 'nest' parameter value into R10.
5163 // R10 is specified in X86CallingConv.td
5164 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5165 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5166 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005167 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005168
5169 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005170 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005171
5172 // Jump to the nested function.
5173 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5174 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5175 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005176 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005177
5178 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5179 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5180 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005181 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005182
5183 SDOperand Ops[] =
5184 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5185 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005186 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005187 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005188 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5189 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005190 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005191
5192 switch (CC) {
5193 default:
5194 assert(0 && "Unsupported calling convention");
5195 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005196 case CallingConv::X86_StdCall: {
5197 // Pass 'nest' parameter in ECX.
5198 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005199 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005200
5201 // Check that ECX wasn't needed by an 'inreg' parameter.
5202 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005203 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005204
Chris Lattner1c8733e2008-03-12 17:45:29 +00005205 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005206 unsigned InRegCount = 0;
5207 unsigned Idx = 1;
5208
5209 for (FunctionType::param_iterator I = FTy->param_begin(),
5210 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005211 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005212 // FIXME: should only count parameters that are lowered to integers.
5213 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5214
5215 if (InRegCount > 2) {
5216 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5217 abort();
5218 }
5219 }
5220 break;
5221 }
5222 case CallingConv::X86_FastCall:
5223 // Pass 'nest' parameter in EAX.
5224 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005225 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005226 break;
5227 }
5228
5229 SDOperand OutChains[4];
5230 SDOperand Addr, Disp;
5231
5232 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5233 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5234
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005235 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5236 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005237 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005238 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005239 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005240
5241 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005242 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005243
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005244 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005245 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5246 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005247 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005248
5249 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005250 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005251
Duncan Sands7407a9f2007-09-11 14:10:23 +00005252 SDOperand Ops[] =
5253 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5254 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005255 }
5256}
5257
Dan Gohman819574c2008-01-31 00:41:03 +00005258SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005259 /*
5260 The rounding mode is in bits 11:10 of FPSR, and has the following
5261 settings:
5262 00 Round to nearest
5263 01 Round to -inf
5264 10 Round to +inf
5265 11 Round to 0
5266
5267 FLT_ROUNDS, on the other hand, expects the following:
5268 -1 Undefined
5269 0 Round to 0
5270 1 Round to nearest
5271 2 Round to +inf
5272 3 Round to -inf
5273
5274 To perform the conversion, we do:
5275 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5276 */
5277
5278 MachineFunction &MF = DAG.getMachineFunction();
5279 const TargetMachine &TM = MF.getTarget();
5280 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5281 unsigned StackAlignment = TFI.getStackAlignment();
5282 MVT::ValueType VT = Op.getValueType();
5283
5284 // Save FP Control Word to stack slot
5285 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5286 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5287
5288 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5289 DAG.getEntryNode(), StackSlot);
5290
5291 // Load FP Control Word from stack slot
5292 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5293
5294 // Transform as necessary
5295 SDOperand CWD1 =
5296 DAG.getNode(ISD::SRL, MVT::i16,
5297 DAG.getNode(ISD::AND, MVT::i16,
5298 CWD, DAG.getConstant(0x800, MVT::i16)),
5299 DAG.getConstant(11, MVT::i8));
5300 SDOperand CWD2 =
5301 DAG.getNode(ISD::SRL, MVT::i16,
5302 DAG.getNode(ISD::AND, MVT::i16,
5303 CWD, DAG.getConstant(0x400, MVT::i16)),
5304 DAG.getConstant(9, MVT::i8));
5305
5306 SDOperand RetVal =
5307 DAG.getNode(ISD::AND, MVT::i16,
5308 DAG.getNode(ISD::ADD, MVT::i16,
5309 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5310 DAG.getConstant(1, MVT::i16)),
5311 DAG.getConstant(3, MVT::i16));
5312
5313
5314 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5315 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5316}
5317
Evan Cheng48679f42007-12-14 02:13:44 +00005318SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5319 MVT::ValueType VT = Op.getValueType();
5320 MVT::ValueType OpVT = VT;
5321 unsigned NumBits = MVT::getSizeInBits(VT);
5322
5323 Op = Op.getOperand(0);
5324 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005325 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005326 OpVT = MVT::i32;
5327 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5328 }
Evan Cheng48679f42007-12-14 02:13:44 +00005329
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005330 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5331 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5332 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5333
5334 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5335 SmallVector<SDOperand, 4> Ops;
5336 Ops.push_back(Op);
5337 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5338 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5339 Ops.push_back(Op.getValue(1));
5340 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5341
5342 // Finally xor with NumBits-1.
5343 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5344
Evan Cheng48679f42007-12-14 02:13:44 +00005345 if (VT == MVT::i8)
5346 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5347 return Op;
5348}
5349
5350SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5351 MVT::ValueType VT = Op.getValueType();
5352 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005353 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005354
5355 Op = Op.getOperand(0);
5356 if (VT == MVT::i8) {
5357 OpVT = MVT::i32;
5358 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5359 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005360
5361 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5362 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5363 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5364
5365 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5366 SmallVector<SDOperand, 4> Ops;
5367 Ops.push_back(Op);
5368 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5369 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5370 Ops.push_back(Op.getValue(1));
5371 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5372
Evan Cheng48679f42007-12-14 02:13:44 +00005373 if (VT == MVT::i8)
5374 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5375 return Op;
5376}
5377
Andrew Lenharth81580822008-03-05 01:15:49 +00005378SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005379 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005380 unsigned Reg = 0;
5381 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005382 switch(T) {
5383 case MVT::i8: Reg = X86::AL; size = 1; break;
5384 case MVT::i16: Reg = X86::AX; size = 2; break;
5385 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005386 case MVT::i64:
5387 if (Subtarget->is64Bit()) {
5388 Reg = X86::RAX; size = 8;
5389 } else //Should go away when LowerType stuff lands
5390 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5391 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005392 };
5393 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005394 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005395 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005396 Op.getOperand(1),
5397 Op.getOperand(2),
5398 DAG.getTargetConstant(size, MVT::i8),
5399 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005400 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5401 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5402 SDOperand cpOut =
5403 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5404 return cpOut;
5405}
5406
Andrew Lenharth81580822008-03-05 01:15:49 +00005407SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5408 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5409 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5410 SDOperand cpInL, cpInH;
5411 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5412 DAG.getConstant(0, MVT::i32));
5413 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5414 DAG.getConstant(1, MVT::i32));
5415 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5416 cpInL, SDOperand());
5417 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5418 cpInH, cpInL.getValue(1));
5419 SDOperand swapInL, swapInH;
5420 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5421 DAG.getConstant(0, MVT::i32));
5422 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5423 DAG.getConstant(1, MVT::i32));
5424 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5425 swapInL, cpInH.getValue(1));
5426 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5427 swapInH, swapInL.getValue(1));
5428 SDOperand Ops[] = { swapInH.getValue(0),
5429 Op->getOperand(1),
5430 swapInH.getValue(1)};
5431 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5432 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5433 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5434 Result.getValue(1));
5435 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5436 cpOutL.getValue(2));
5437 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5438 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5439 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5440 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5441}
5442
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005443/// LowerOperation - Provide custom lowering hooks for some operations.
5444///
5445SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5446 switch (Op.getOpcode()) {
5447 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005448 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5450 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5451 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5452 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5453 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5454 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5455 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5456 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5457 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5458 case ISD::SHL_PARTS:
5459 case ISD::SRA_PARTS:
5460 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5461 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5462 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5463 case ISD::FABS: return LowerFABS(Op, DAG);
5464 case ISD::FNEG: return LowerFNEG(Op, DAG);
5465 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005466 case ISD::SETCC: return LowerSETCC(Op, DAG);
5467 case ISD::SELECT: return LowerSELECT(Op, DAG);
5468 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005469 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5470 case ISD::CALL: return LowerCALL(Op, DAG);
5471 case ISD::RET: return LowerRET(Op, DAG);
5472 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005473 case ISD::VASTART: return LowerVASTART(Op, DAG);
5474 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5475 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5476 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5477 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5478 case ISD::FRAME_TO_ARGS_OFFSET:
5479 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5480 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5481 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005482 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005483 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005484 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5485 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005486
5487 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5488 case ISD::READCYCLECOUNTER:
5489 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005490 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005491}
5492
5493/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5494SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5495 switch (N->getOpcode()) {
5496 default: assert(0 && "Should not custom lower this!");
5497 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5498 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005499 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005500 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005501}
5502
5503const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5504 switch (Opcode) {
5505 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005506 case X86ISD::BSF: return "X86ISD::BSF";
5507 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005508 case X86ISD::SHLD: return "X86ISD::SHLD";
5509 case X86ISD::SHRD: return "X86ISD::SHRD";
5510 case X86ISD::FAND: return "X86ISD::FAND";
5511 case X86ISD::FOR: return "X86ISD::FOR";
5512 case X86ISD::FXOR: return "X86ISD::FXOR";
5513 case X86ISD::FSRL: return "X86ISD::FSRL";
5514 case X86ISD::FILD: return "X86ISD::FILD";
5515 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5516 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5517 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5518 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5519 case X86ISD::FLD: return "X86ISD::FLD";
5520 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005521 case X86ISD::CALL: return "X86ISD::CALL";
5522 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5523 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5524 case X86ISD::CMP: return "X86ISD::CMP";
5525 case X86ISD::COMI: return "X86ISD::COMI";
5526 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5527 case X86ISD::SETCC: return "X86ISD::SETCC";
5528 case X86ISD::CMOV: return "X86ISD::CMOV";
5529 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5530 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5531 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5532 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005533 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5534 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005535 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005537 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5538 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5540 case X86ISD::FMAX: return "X86ISD::FMAX";
5541 case X86ISD::FMIN: return "X86ISD::FMIN";
5542 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5543 case X86ISD::FRCP: return "X86ISD::FRCP";
5544 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5545 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5546 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005547 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005548 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005549 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005550 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005551 }
5552}
5553
5554// isLegalAddressingMode - Return true if the addressing mode represented
5555// by AM is legal for this target, for a load/store of the specified type.
5556bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5557 const Type *Ty) const {
5558 // X86 supports extremely general addressing modes.
5559
5560 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5561 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5562 return false;
5563
5564 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005565 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5567 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005568
5569 // X86-64 only supports addr of globals in small code model.
5570 if (Subtarget->is64Bit()) {
5571 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5572 return false;
5573 // If lower 4G is not available, then we must use rip-relative addressing.
5574 if (AM.BaseOffs || AM.Scale > 1)
5575 return false;
5576 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005577 }
5578
5579 switch (AM.Scale) {
5580 case 0:
5581 case 1:
5582 case 2:
5583 case 4:
5584 case 8:
5585 // These scales always work.
5586 break;
5587 case 3:
5588 case 5:
5589 case 9:
5590 // These scales are formed with basereg+scalereg. Only accept if there is
5591 // no basereg yet.
5592 if (AM.HasBaseReg)
5593 return false;
5594 break;
5595 default: // Other stuff never works.
5596 return false;
5597 }
5598
5599 return true;
5600}
5601
5602
Evan Cheng27a820a2007-10-26 01:56:11 +00005603bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5604 if (!Ty1->isInteger() || !Ty2->isInteger())
5605 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005606 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5607 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00005608 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00005609 return false;
5610 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005611}
5612
Evan Cheng9decb332007-10-29 19:58:20 +00005613bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5614 MVT::ValueType VT2) const {
5615 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5616 return false;
5617 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5618 unsigned NumBits2 = MVT::getSizeInBits(VT2);
Evan Chengca0e80f2008-03-20 02:18:41 +00005619 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00005620 return false;
5621 return Subtarget->is64Bit() || NumBits1 < 64;
5622}
Evan Cheng27a820a2007-10-26 01:56:11 +00005623
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005624/// isShuffleMaskLegal - Targets can use this to indicate that they only
5625/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5626/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5627/// are assumed to be legal.
5628bool
5629X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5630 // Only do shuffles on 128-bit vector types for now.
5631 if (MVT::getSizeInBits(VT) == 64) return false;
5632 return (Mask.Val->getNumOperands() <= 4 ||
5633 isIdentityMask(Mask.Val) ||
5634 isIdentityMask(Mask.Val, true) ||
5635 isSplatMask(Mask.Val) ||
5636 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5637 X86::isUNPCKLMask(Mask.Val) ||
5638 X86::isUNPCKHMask(Mask.Val) ||
5639 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5640 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5641}
5642
Dan Gohman48d5f062008-04-09 20:09:42 +00005643bool
5644X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
5645 MVT::ValueType EVT,
5646 SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005647 unsigned NumElts = BVOps.size();
5648 // Only do shuffles on 128-bit vector types for now.
5649 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5650 if (NumElts == 2) return true;
5651 if (NumElts == 4) {
5652 return (isMOVLMask(&BVOps[0], 4) ||
5653 isCommutedMOVL(&BVOps[0], 4, true) ||
5654 isSHUFPMask(&BVOps[0], 4) ||
5655 isCommutedSHUFP(&BVOps[0], 4));
5656 }
5657 return false;
5658}
5659
5660//===----------------------------------------------------------------------===//
5661// X86 Scheduler Hooks
5662//===----------------------------------------------------------------------===//
5663
5664MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005665X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5666 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005667 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5668 switch (MI->getOpcode()) {
5669 default: assert(false && "Unexpected instr type to insert");
5670 case X86::CMOV_FR32:
5671 case X86::CMOV_FR64:
5672 case X86::CMOV_V4F32:
5673 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005674 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005675 // To "insert" a SELECT_CC instruction, we actually have to insert the
5676 // diamond control-flow pattern. The incoming instruction knows the
5677 // destination vreg to set, the condition code register to branch on, the
5678 // true/false values to select between, and a branch opcode to use.
5679 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5680 ilist<MachineBasicBlock>::iterator It = BB;
5681 ++It;
5682
5683 // thisMBB:
5684 // ...
5685 // TrueVal = ...
5686 // cmpTY ccX, r1, r2
5687 // bCC copy1MBB
5688 // fallthrough --> copy0MBB
5689 MachineBasicBlock *thisMBB = BB;
5690 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5691 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5692 unsigned Opc =
5693 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5694 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5695 MachineFunction *F = BB->getParent();
5696 F->getBasicBlockList().insert(It, copy0MBB);
5697 F->getBasicBlockList().insert(It, sinkMBB);
5698 // Update machine-CFG edges by first adding all successors of the current
5699 // block to the new block which will contain the Phi node for the select.
5700 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5701 e = BB->succ_end(); i != e; ++i)
5702 sinkMBB->addSuccessor(*i);
5703 // Next, remove all successors of the current block, and add the true
5704 // and fallthrough blocks as its successors.
5705 while(!BB->succ_empty())
5706 BB->removeSuccessor(BB->succ_begin());
5707 BB->addSuccessor(copy0MBB);
5708 BB->addSuccessor(sinkMBB);
5709
5710 // copy0MBB:
5711 // %FalseValue = ...
5712 // # fallthrough to sinkMBB
5713 BB = copy0MBB;
5714
5715 // Update machine-CFG edges
5716 BB->addSuccessor(sinkMBB);
5717
5718 // sinkMBB:
5719 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5720 // ...
5721 BB = sinkMBB;
5722 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5723 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5724 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5725
5726 delete MI; // The pseudo instruction is gone now.
5727 return BB;
5728 }
5729
5730 case X86::FP32_TO_INT16_IN_MEM:
5731 case X86::FP32_TO_INT32_IN_MEM:
5732 case X86::FP32_TO_INT64_IN_MEM:
5733 case X86::FP64_TO_INT16_IN_MEM:
5734 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005735 case X86::FP64_TO_INT64_IN_MEM:
5736 case X86::FP80_TO_INT16_IN_MEM:
5737 case X86::FP80_TO_INT32_IN_MEM:
5738 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005739 // Change the floating point control register to use "round towards zero"
5740 // mode when truncating to an integer value.
5741 MachineFunction *F = BB->getParent();
5742 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5743 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5744
5745 // Load the old value of the high byte of the control word...
5746 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005747 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005748 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5749
5750 // Set the high part to be round to zero...
5751 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5752 .addImm(0xC7F);
5753
5754 // Reload the modified control word now...
5755 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5756
5757 // Restore the memory image of control word to original value
5758 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5759 .addReg(OldCW);
5760
5761 // Get the X86 opcode to use.
5762 unsigned Opc;
5763 switch (MI->getOpcode()) {
5764 default: assert(0 && "illegal opcode!");
5765 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5766 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5767 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5768 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5769 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5770 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005771 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5772 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5773 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005774 }
5775
5776 X86AddressMode AM;
5777 MachineOperand &Op = MI->getOperand(0);
5778 if (Op.isRegister()) {
5779 AM.BaseType = X86AddressMode::RegBase;
5780 AM.Base.Reg = Op.getReg();
5781 } else {
5782 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005783 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005784 }
5785 Op = MI->getOperand(1);
5786 if (Op.isImmediate())
5787 AM.Scale = Op.getImm();
5788 Op = MI->getOperand(2);
5789 if (Op.isImmediate())
5790 AM.IndexReg = Op.getImm();
5791 Op = MI->getOperand(3);
5792 if (Op.isGlobalAddress()) {
5793 AM.GV = Op.getGlobal();
5794 } else {
5795 AM.Disp = Op.getImm();
5796 }
5797 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5798 .addReg(MI->getOperand(4).getReg());
5799
5800 // Reload the original control word now.
5801 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5802
5803 delete MI; // The pseudo instruction is gone now.
5804 return BB;
5805 }
5806 }
5807}
5808
5809//===----------------------------------------------------------------------===//
5810// X86 Optimization Hooks
5811//===----------------------------------------------------------------------===//
5812
5813void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005814 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005815 APInt &KnownZero,
5816 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005817 const SelectionDAG &DAG,
5818 unsigned Depth) const {
5819 unsigned Opc = Op.getOpcode();
5820 assert((Opc >= ISD::BUILTIN_OP_END ||
5821 Opc == ISD::INTRINSIC_WO_CHAIN ||
5822 Opc == ISD::INTRINSIC_W_CHAIN ||
5823 Opc == ISD::INTRINSIC_VOID) &&
5824 "Should use MaskedValueIsZero if you don't know whether Op"
5825 " is a target node!");
5826
Dan Gohman1d79e432008-02-13 23:07:24 +00005827 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005828 switch (Opc) {
5829 default: break;
5830 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005831 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5832 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005833 break;
5834 }
5835}
5836
5837/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5838/// element of the result of the vector shuffle.
5839static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5840 MVT::ValueType VT = N->getValueType(0);
5841 SDOperand PermMask = N->getOperand(2);
5842 unsigned NumElems = PermMask.getNumOperands();
5843 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5844 i %= NumElems;
5845 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5846 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005847 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005848 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5849 SDOperand Idx = PermMask.getOperand(i);
5850 if (Idx.getOpcode() == ISD::UNDEF)
5851 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5852 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5853 }
5854 return SDOperand();
5855}
5856
5857/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5858/// node is a GlobalAddress + an offset.
5859static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5860 unsigned Opc = N->getOpcode();
5861 if (Opc == X86ISD::Wrapper) {
5862 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5863 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5864 return true;
5865 }
5866 } else if (Opc == ISD::ADD) {
5867 SDOperand N1 = N->getOperand(0);
5868 SDOperand N2 = N->getOperand(1);
5869 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5870 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5871 if (V) {
5872 Offset += V->getSignExtended();
5873 return true;
5874 }
5875 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5876 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5877 if (V) {
5878 Offset += V->getSignExtended();
5879 return true;
5880 }
5881 }
5882 }
5883 return false;
5884}
5885
5886/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5887/// + Dist * Size.
5888static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5889 MachineFrameInfo *MFI) {
5890 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5891 return false;
5892
5893 SDOperand Loc = N->getOperand(1);
5894 SDOperand BaseLoc = Base->getOperand(1);
5895 if (Loc.getOpcode() == ISD::FrameIndex) {
5896 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5897 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005898 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5899 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005900 int FS = MFI->getObjectSize(FI);
5901 int BFS = MFI->getObjectSize(BFI);
5902 if (FS != BFS || FS != Size) return false;
5903 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5904 } else {
5905 GlobalValue *GV1 = NULL;
5906 GlobalValue *GV2 = NULL;
5907 int64_t Offset1 = 0;
5908 int64_t Offset2 = 0;
5909 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5910 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5911 if (isGA1 && isGA2 && GV1 == GV2)
5912 return Offset1 == (Offset2 + Dist*Size);
5913 }
5914
5915 return false;
5916}
5917
5918static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5919 const X86Subtarget *Subtarget) {
5920 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005921 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005922 if (isGAPlusOffset(Base, GV, Offset))
5923 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005924 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005925 return false;
5926}
5927
5928
5929/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5930/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5931/// if the load addresses are consecutive, non-overlapping, and in the right
5932/// order.
5933static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5934 const X86Subtarget *Subtarget) {
5935 MachineFunction &MF = DAG.getMachineFunction();
5936 MachineFrameInfo *MFI = MF.getFrameInfo();
5937 MVT::ValueType VT = N->getValueType(0);
5938 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5939 SDOperand PermMask = N->getOperand(2);
5940 int NumElems = (int)PermMask.getNumOperands();
5941 SDNode *Base = NULL;
5942 for (int i = 0; i < NumElems; ++i) {
5943 SDOperand Idx = PermMask.getOperand(i);
5944 if (Idx.getOpcode() == ISD::UNDEF) {
5945 if (!Base) return SDOperand();
5946 } else {
5947 SDOperand Arg =
5948 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5949 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5950 return SDOperand();
5951 if (!Base)
5952 Base = Arg.Val;
5953 else if (!isConsecutiveLoad(Arg.Val, Base,
5954 i, MVT::getSizeInBits(EVT)/8,MFI))
5955 return SDOperand();
5956 }
5957 }
5958
5959 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005960 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005962 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005963 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005964 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005965 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5966 LD->getSrcValueOffset(), LD->isVolatile(),
5967 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005968 }
5969}
5970
5971/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5972static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5973 const X86Subtarget *Subtarget) {
5974 SDOperand Cond = N->getOperand(0);
5975
5976 // If we have SSE[12] support, try to form min/max nodes.
5977 if (Subtarget->hasSSE2() &&
5978 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5979 if (Cond.getOpcode() == ISD::SETCC) {
5980 // Get the LHS/RHS of the select.
5981 SDOperand LHS = N->getOperand(1);
5982 SDOperand RHS = N->getOperand(2);
5983 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5984
5985 unsigned Opcode = 0;
5986 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5987 switch (CC) {
5988 default: break;
5989 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5990 case ISD::SETULE:
5991 case ISD::SETLE:
5992 if (!UnsafeFPMath) break;
5993 // FALL THROUGH.
5994 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5995 case ISD::SETLT:
5996 Opcode = X86ISD::FMIN;
5997 break;
5998
5999 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6000 case ISD::SETUGT:
6001 case ISD::SETGT:
6002 if (!UnsafeFPMath) break;
6003 // FALL THROUGH.
6004 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6005 case ISD::SETGE:
6006 Opcode = X86ISD::FMAX;
6007 break;
6008 }
6009 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6010 switch (CC) {
6011 default: break;
6012 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6013 case ISD::SETUGT:
6014 case ISD::SETGT:
6015 if (!UnsafeFPMath) break;
6016 // FALL THROUGH.
6017 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6018 case ISD::SETGE:
6019 Opcode = X86ISD::FMIN;
6020 break;
6021
6022 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6023 case ISD::SETULE:
6024 case ISD::SETLE:
6025 if (!UnsafeFPMath) break;
6026 // FALL THROUGH.
6027 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6028 case ISD::SETLT:
6029 Opcode = X86ISD::FMAX;
6030 break;
6031 }
6032 }
6033
6034 if (Opcode)
6035 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6036 }
6037
6038 }
6039
6040 return SDOperand();
6041}
6042
Chris Lattnerce84ae42008-02-22 02:09:43 +00006043/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6044static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6045 const X86Subtarget *Subtarget) {
6046 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6047 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006048 // A preferable solution to the general problem is to figure out the right
6049 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00006050 if (MVT::isVector(St->getValue().getValueType()) &&
6051 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006052 isa<LoadSDNode>(St->getValue()) &&
6053 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6054 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006055 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006056 LoadSDNode *Ld = 0;
6057 int TokenFactorIndex = -1;
6058 SmallVector<SDOperand, 8> Ops;
6059 SDNode* ChainVal = St->getChain().Val;
6060 // Must be a store of a load. We currently handle two cases: the load
6061 // is a direct child, and it's under an intervening TokenFactor. It is
6062 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006063 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006064 Ld = cast<LoadSDNode>(St->getChain());
6065 else if (St->getValue().hasOneUse() &&
6066 ChainVal->getOpcode() == ISD::TokenFactor) {
6067 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006068 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006069 TokenFactorIndex = i;
6070 Ld = cast<LoadSDNode>(St->getValue());
6071 } else
6072 Ops.push_back(ChainVal->getOperand(i));
6073 }
6074 }
6075 if (Ld) {
6076 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6077 if (Subtarget->is64Bit()) {
6078 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6079 Ld->getBasePtr(), Ld->getSrcValue(),
6080 Ld->getSrcValueOffset(), Ld->isVolatile(),
6081 Ld->getAlignment());
6082 SDOperand NewChain = NewLd.getValue(1);
6083 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006084 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006085 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6086 Ops.size());
6087 }
6088 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6089 St->getSrcValue(), St->getSrcValueOffset(),
6090 St->isVolatile(), St->getAlignment());
6091 }
6092
6093 // Otherwise, lower to two 32-bit copies.
6094 SDOperand LoAddr = Ld->getBasePtr();
6095 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6096 DAG.getConstant(MVT::i32, 4));
6097
6098 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6099 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6100 Ld->isVolatile(), Ld->getAlignment());
6101 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6102 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6103 Ld->isVolatile(),
6104 MinAlign(Ld->getAlignment(), 4));
6105
6106 SDOperand NewChain = LoLd.getValue(1);
6107 if (TokenFactorIndex != -1) {
6108 Ops.push_back(LoLd);
6109 Ops.push_back(HiLd);
6110 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6111 Ops.size());
6112 }
6113
6114 LoAddr = St->getBasePtr();
6115 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6116 DAG.getConstant(MVT::i32, 4));
6117
6118 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006119 St->getSrcValue(), St->getSrcValueOffset(),
6120 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006121 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6122 St->getSrcValue(), St->getSrcValueOffset()+4,
6123 St->isVolatile(),
6124 MinAlign(St->getAlignment(), 4));
6125 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006126 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006127 }
6128 return SDOperand();
6129}
6130
Chris Lattner470d5dc2008-01-25 06:14:17 +00006131/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6132/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006133static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006134 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6135 // F[X]OR(0.0, x) -> x
6136 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006137 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6138 if (C->getValueAPF().isPosZero())
6139 return N->getOperand(1);
6140 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6141 if (C->getValueAPF().isPosZero())
6142 return N->getOperand(0);
6143 return SDOperand();
6144}
6145
6146/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6147static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6148 // FAND(0.0, x) -> 0.0
6149 // FAND(x, 0.0) -> 0.0
6150 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6151 if (C->getValueAPF().isPosZero())
6152 return N->getOperand(0);
6153 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6154 if (C->getValueAPF().isPosZero())
6155 return N->getOperand(1);
6156 return SDOperand();
6157}
6158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006159
6160SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6161 DAGCombinerInfo &DCI) const {
6162 SelectionDAG &DAG = DCI.DAG;
6163 switch (N->getOpcode()) {
6164 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006165 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6166 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006167 case ISD::STORE:
6168 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006169 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006170 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6171 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006172 }
6173
6174 return SDOperand();
6175}
6176
6177//===----------------------------------------------------------------------===//
6178// X86 Inline Assembly Support
6179//===----------------------------------------------------------------------===//
6180
6181/// getConstraintType - Given a constraint letter, return the type of
6182/// constraint it is for this target.
6183X86TargetLowering::ConstraintType
6184X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6185 if (Constraint.size() == 1) {
6186 switch (Constraint[0]) {
6187 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006188 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006189 case 'r':
6190 case 'R':
6191 case 'l':
6192 case 'q':
6193 case 'Q':
6194 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006195 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006196 case 'Y':
6197 return C_RegisterClass;
6198 default:
6199 break;
6200 }
6201 }
6202 return TargetLowering::getConstraintType(Constraint);
6203}
6204
Dale Johannesene99fc902008-01-29 02:21:21 +00006205/// LowerXConstraint - try to replace an X constraint, which matches anything,
6206/// with another that has more specific requirements based on the type of the
6207/// corresponding operand.
6208void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6209 std::string& s) const {
6210 if (MVT::isFloatingPoint(ConstraintVT)) {
6211 if (Subtarget->hasSSE2())
6212 s = "Y";
6213 else if (Subtarget->hasSSE1())
6214 s = "x";
6215 else
6216 s = "f";
6217 } else
6218 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6219}
6220
Chris Lattnera531abc2007-08-25 00:47:38 +00006221/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6222/// vector. If it is invalid, don't add anything to Ops.
6223void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6224 char Constraint,
6225 std::vector<SDOperand>&Ops,
6226 SelectionDAG &DAG) {
6227 SDOperand Result(0, 0);
6228
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006229 switch (Constraint) {
6230 default: break;
6231 case 'I':
6232 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006233 if (C->getValue() <= 31) {
6234 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6235 break;
6236 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006237 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006238 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006239 case 'N':
6240 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006241 if (C->getValue() <= 255) {
6242 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6243 break;
6244 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006245 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006246 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006247 case 'i': {
6248 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006249 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6250 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6251 break;
6252 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006253
6254 // If we are in non-pic codegen mode, we allow the address of a global (with
6255 // an optional displacement) to be used with 'i'.
6256 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6257 int64_t Offset = 0;
6258
6259 // Match either (GA) or (GA+C)
6260 if (GA) {
6261 Offset = GA->getOffset();
6262 } else if (Op.getOpcode() == ISD::ADD) {
6263 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6264 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6265 if (C && GA) {
6266 Offset = GA->getOffset()+C->getValue();
6267 } else {
6268 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6269 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6270 if (C && GA)
6271 Offset = GA->getOffset()+C->getValue();
6272 else
6273 C = 0, GA = 0;
6274 }
6275 }
6276
6277 if (GA) {
6278 // If addressing this global requires a load (e.g. in PIC mode), we can't
6279 // match.
6280 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6281 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006282 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006283
6284 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6285 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006286 Result = Op;
6287 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288 }
6289
6290 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006291 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006292 }
6293 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006294
6295 if (Result.Val) {
6296 Ops.push_back(Result);
6297 return;
6298 }
6299 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006300}
6301
6302std::vector<unsigned> X86TargetLowering::
6303getRegClassForInlineAsmConstraint(const std::string &Constraint,
6304 MVT::ValueType VT) const {
6305 if (Constraint.size() == 1) {
6306 // FIXME: not handling fp-stack yet!
6307 switch (Constraint[0]) { // GCC X86 Constraint Letters
6308 default: break; // Unknown constraint letter
6309 case 'A': // EAX/EDX
6310 if (VT == MVT::i32 || VT == MVT::i64)
6311 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6312 break;
6313 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6314 case 'Q': // Q_REGS
6315 if (VT == MVT::i32)
6316 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6317 else if (VT == MVT::i16)
6318 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6319 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006320 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006321 else if (VT == MVT::i64)
6322 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6323 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006324 }
6325 }
6326
6327 return std::vector<unsigned>();
6328}
6329
6330std::pair<unsigned, const TargetRegisterClass*>
6331X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6332 MVT::ValueType VT) const {
6333 // First, see if this is a constraint that directly corresponds to an LLVM
6334 // register class.
6335 if (Constraint.size() == 1) {
6336 // GCC Constraint Letters
6337 switch (Constraint[0]) {
6338 default: break;
6339 case 'r': // GENERAL_REGS
6340 case 'R': // LEGACY_REGS
6341 case 'l': // INDEX_REGS
6342 if (VT == MVT::i64 && Subtarget->is64Bit())
6343 return std::make_pair(0U, X86::GR64RegisterClass);
6344 if (VT == MVT::i32)
6345 return std::make_pair(0U, X86::GR32RegisterClass);
6346 else if (VT == MVT::i16)
6347 return std::make_pair(0U, X86::GR16RegisterClass);
6348 else if (VT == MVT::i8)
6349 return std::make_pair(0U, X86::GR8RegisterClass);
6350 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006351 case 'f': // FP Stack registers.
6352 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6353 // value to the correct fpstack register class.
6354 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6355 return std::make_pair(0U, X86::RFP32RegisterClass);
6356 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6357 return std::make_pair(0U, X86::RFP64RegisterClass);
6358 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006359 case 'y': // MMX_REGS if MMX allowed.
6360 if (!Subtarget->hasMMX()) break;
6361 return std::make_pair(0U, X86::VR64RegisterClass);
6362 break;
6363 case 'Y': // SSE_REGS if SSE2 allowed
6364 if (!Subtarget->hasSSE2()) break;
6365 // FALL THROUGH.
6366 case 'x': // SSE_REGS if SSE1 allowed
6367 if (!Subtarget->hasSSE1()) break;
6368
6369 switch (VT) {
6370 default: break;
6371 // Scalar SSE types.
6372 case MVT::f32:
6373 case MVT::i32:
6374 return std::make_pair(0U, X86::FR32RegisterClass);
6375 case MVT::f64:
6376 case MVT::i64:
6377 return std::make_pair(0U, X86::FR64RegisterClass);
6378 // Vector types.
6379 case MVT::v16i8:
6380 case MVT::v8i16:
6381 case MVT::v4i32:
6382 case MVT::v2i64:
6383 case MVT::v4f32:
6384 case MVT::v2f64:
6385 return std::make_pair(0U, X86::VR128RegisterClass);
6386 }
6387 break;
6388 }
6389 }
6390
6391 // Use the default implementation in TargetLowering to convert the register
6392 // constraint into a member of a register class.
6393 std::pair<unsigned, const TargetRegisterClass*> Res;
6394 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6395
6396 // Not found as a standard register?
6397 if (Res.second == 0) {
6398 // GCC calls "st(0)" just plain "st".
6399 if (StringsEqualNoCase("{st}", Constraint)) {
6400 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006401 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006402 }
6403
6404 return Res;
6405 }
6406
6407 // Otherwise, check to see if this is a register class of the wrong value
6408 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6409 // turn into {ax},{dx}.
6410 if (Res.second->hasType(VT))
6411 return Res; // Correct type already, nothing to do.
6412
6413 // All of the single-register GCC register classes map their values onto
6414 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6415 // really want an 8-bit or 32-bit register, map to the appropriate register
6416 // class and return the appropriate register.
6417 if (Res.second != X86::GR16RegisterClass)
6418 return Res;
6419
6420 if (VT == MVT::i8) {
6421 unsigned DestReg = 0;
6422 switch (Res.first) {
6423 default: break;
6424 case X86::AX: DestReg = X86::AL; break;
6425 case X86::DX: DestReg = X86::DL; break;
6426 case X86::CX: DestReg = X86::CL; break;
6427 case X86::BX: DestReg = X86::BL; break;
6428 }
6429 if (DestReg) {
6430 Res.first = DestReg;
6431 Res.second = Res.second = X86::GR8RegisterClass;
6432 }
6433 } else if (VT == MVT::i32) {
6434 unsigned DestReg = 0;
6435 switch (Res.first) {
6436 default: break;
6437 case X86::AX: DestReg = X86::EAX; break;
6438 case X86::DX: DestReg = X86::EDX; break;
6439 case X86::CX: DestReg = X86::ECX; break;
6440 case X86::BX: DestReg = X86::EBX; break;
6441 case X86::SI: DestReg = X86::ESI; break;
6442 case X86::DI: DestReg = X86::EDI; break;
6443 case X86::BP: DestReg = X86::EBP; break;
6444 case X86::SP: DestReg = X86::ESP; break;
6445 }
6446 if (DestReg) {
6447 Res.first = DestReg;
6448 Res.second = Res.second = X86::GR32RegisterClass;
6449 }
6450 } else if (VT == MVT::i64) {
6451 unsigned DestReg = 0;
6452 switch (Res.first) {
6453 default: break;
6454 case X86::AX: DestReg = X86::RAX; break;
6455 case X86::DX: DestReg = X86::RDX; break;
6456 case X86::CX: DestReg = X86::RCX; break;
6457 case X86::BX: DestReg = X86::RBX; break;
6458 case X86::SI: DestReg = X86::RSI; break;
6459 case X86::DI: DestReg = X86::RDI; break;
6460 case X86::BP: DestReg = X86::RBP; break;
6461 case X86::SP: DestReg = X86::RSP; break;
6462 }
6463 if (DestReg) {
6464 Res.first = DestReg;
6465 Res.second = Res.second = X86::GR64RegisterClass;
6466 }
6467 }
6468
6469 return Res;
6470}